Exemple #1
0
    def loadFont(self,
                 names,
                 src,
                 encoding="WinAnsiEncoding",
                 bold=0,
                 italic=0):

        # XXX Just works for local filenames!
        if names and src:

            file = src
            src = file.uri

            log.debug("Load font %r", src)
            if names.startswith("#"):
                names = names.strip('#')
            if type(names) is ListType:
                fontAlias = names
            else:
                fontAlias = (x.lower().strip() for x in names.split(",") if x)

            # XXX Problems with unicode here
            fontAlias = [str(x) for x in fontAlias]

            ffname = names
            fontName = fontAlias[0]
            parts = src.split(".")
            baseName, suffix = ".".join(parts[:-1]), parts[-1]
            suffix = suffix.lower()

            if suffix in ["ttc", "ttf"]:

                # determine full font name according to weight and style
                fullFontName = "%s_%d%d" % (fontName, bold, italic)

                # check if font has already been registered
                if fullFontName in self.fontList:
                    log.warning(
                        self.warning(
                            "Repeated font embed for %s, skip new embed ",
                            fullFontName))
                else:

                    # Register TTF font and special name
                    filename = file.getNamedFile()
                    file = TTFont(fullFontName, filename)
                    pdfmetrics.registerFont(file)

                    # Add or replace missing styles
                    for bold in (0, 1):
                        for italic in (0, 1):
                            if ("%s_%d%d" %
                                (fontName, bold, italic)) not in self.fontList:
                                addMapping(fontName, bold, italic,
                                           fullFontName)

                    # Register "normal" name and the place holder for style
                    self.registerFont(fontName, fontAlias + [fullFontName])

            elif suffix in ("afm", "pfb"):

                if suffix == "afm":
                    afm = file.getNamedFile()
                    tfile = pisaFileObject(baseName + ".pfb")
                    pfb = tfile.getNamedFile()
                else:
                    pfb = file.getNamedFile()
                    tfile = pisaFileObject(baseName + ".afm")
                    afm = tfile.getNamedFile()

                # determine full font name according to weight and style
                fullFontName = "%s_%d%d" % (fontName, bold, italic)

                # check if font has already been registered
                if fullFontName in self.fontList:
                    log.warning(
                        self.warning(
                            "Repeated font embed for %s, skip new embed",
                            fontName))
                else:

                    # Include font
                    face = pdfmetrics.EmbeddedType1Face(afm, pfb)
                    fontNameOriginal = face.name
                    pdfmetrics.registerTypeFace(face)
                    # print fontName, fontNameOriginal, fullFontName
                    justFont = pdfmetrics.Font(fullFontName, fontNameOriginal,
                                               encoding)
                    pdfmetrics.registerFont(justFont)

                    # Add or replace missing styles
                    for bold in (0, 1):
                        for italic in (0, 1):
                            if ("%s_%d%d" %
                                (fontName, bold, italic)) not in self.fontList:
                                addMapping(fontName, bold, italic,
                                           fontNameOriginal)

                    # Register "normal" name and the place holder for style
                    self.registerFont(
                        fontName, fontAlias + [fullFontName, fontNameOriginal])
            else:
                log.warning(self.warning("wrong attributes for <pdf:font>"))