Exemplo n.º 1
0
Arquivo: font.py Projeto: mjg/PyX-svn
    def processSVG(self, xml, writer, context, registry, bbox):
        if not self.ignorebbox:
            bbox += self.bbox()

        # this is too common to be warned about as text_as_path is the
        # default for svg due to the missing font support by current browsers
        #
        # if writer.text_as_path and not self.font.t1file:
        #     logger.warning("Cannot output text as path when font not given by a font file (like for builtin fonts).")

        if writer.text_as_path and self.font.t1file:
            deco.decoratedpath(self.textpath(), fillstyles=[]).processSVG(xml, writer, context, registry, bbox)
        else:
            if self.font.t1file is not None:
                if self.decode:
                    t1mapping = SVGT1file(self.font.t1file, self.glyphnames, [])
                else:
                    t1mapping = SVGT1file(self.font.t1file, [], self.charcodes)
                registry.add(t1mapping)
            else:
                if self.decode:
                    t1mapping = SVGT1mapping(self.glyphnames, [])
                else:
                    t1mapping = SVGT1mapping([], self.charcodes)

            fontname = self.font.name

            if self.decode:
                if self.kerning:
                    data = self.font.metric.resolvekernings(self.glyphnames, self.size_pt)
                else:
                    data = self.glyphnames
            else:
                data = self.charcodes
            attrs = {"x": "%f" % self.x_pt,
                     "y": "%f" % -self.y_pt,
                     "font-size": "%f" % self.size_pt,
                     "font-family": fontname,
                     "fill": context.fillcolor}
            if context.fillopacity:
                attrs["opacity"] = "%f" % context.fillopacity
            if self.slant:
                trafo.trafo_pt(matrix=((1, self.slant), (0, 1))).outputSVGattrs(attrs, writer, context, registry)
            xml.startSVGElement("text", attrs)
            tspan = False
            for i, value in enumerate(data):
                if self.kerning and i % 2:
                    if value is not None:
                        if tspan:
                            xml.endSVGElement("tspan")
                        xml.startSVGElement("tspan", {"dx": "%f" % (value + self.spaced_pt)})
                        tspan = True
                    elif self.spaced_pt:
                        if tspan:
                            xml.endSVGElement("tspan")
                        xml.startSVGElement("tspan", {"dx": "%f" % (self.spaced_pt)})
                        tspan = True
                else:
                    if i and not self.kerning and self.spaced_pt:
                        if tspan:
                            xml.endSVGElement("tspan")
                        xml.startSVGElement("tspan", {"dx": "%f" % (self.spaced_pt)})
                        tspan = True
                    if self.decode:
                        xml.characters(t1mapping.glyphnames[value])
                    else:
                        xml.characters(t1mapping.charcodes[value])
            if tspan:
                xml.endSVGElement("tspan")
            xml.endSVGElement("text")
Exemplo n.º 2
0
Arquivo: font.py Projeto: mjg/PyX-svn
    def processPS(self, file, writer, context, registry, bbox):
        if not self.ignorebbox:
            bbox += self.bbox()

        if writer.text_as_path and not self.font.t1file:
            logger.warning("Cannot output text as path when font not given by a font file (like for builtin fonts).")
        if writer.text_as_path and self.font.t1file:
            deco.decoratedpath(self.textpath(), fillstyles=[]).processPS(file, writer, context, registry, bbox)
        else:
            # register resources
            if self.font.t1file is not None:
                if self.decode:
                    registry.add(PST1file(self.font.t1file, self.glyphnames, []))
                else:
                    registry.add(PST1file(self.font.t1file, [], self.charcodes))

            fontname = self.font.name
            if self.decode:
                encodingname = self.getencodingname(writer.encodings.setdefault(self.font.name, {}))
                encoding = writer.encodings[self.font.name][encodingname]
                newfontname = "%s-%s" % (fontname, encodingname)
                registry.add(_ReEncodeFont)
                registry.add(PSreencodefont(fontname, newfontname, encoding))
                fontname = newfontname

            if self.slant:
                newfontmatrix = trafo.trafo_pt(matrix=((1, self.slant), (0, 1)))
                if self.font.t1file is not None:
                    newfontmatrix = newfontmatrix * self.font.t1file.fontmatrix
                newfontname = "%s-slant%f" % (fontname, self.slant)
                registry.add(_ChangeFontMatrix)
                registry.add(PSchangefontmatrix(fontname, newfontname, newfontmatrix))
                fontname = newfontname

            # select font if necessary
            sf = selectedfont(fontname, self.size_pt)
            if context.selectedfont is None or sf != context.selectedfont:
                context.selectedfont = sf
                sf.outputPS(file, writer)

            file.write("%f %f moveto (" % (self.x_pt, self.y_pt))
            if self.decode:
                if self.kerning:
                    data = self.font.metric.resolvekernings(self.glyphnames, self.size_pt)
                else:
                    data = self.glyphnames
            else:
                data = self.charcodes
            for i, value in enumerate(data):
                if self.kerning and i % 2:
                    if value is not None:
                        file.write(") show\n%f 0 rmoveto (" % (value+self.spaced_pt))
                    elif self.spaced_pt:
                        file.write(") show\n%f 0 rmoveto (" % self.spaced_pt)
                else:
                    if i and not self.kerning and self.spaced_pt:
                        file.write(") show\n%f 0 rmoveto (" % self.spaced_pt)
                    if self.decode:
                        value = encoding[value]
                    if 32 < value < 127 and chr(value) not in "()[]<>\\":
                        file.write("%s" % chr(value))
                    else:
                        file.write("\\%03o" % value)
            file.write(") show\n")
Exemplo n.º 3
0
Arquivo: font.py Projeto: mjg/PyX-svn
    def processPDF(self, file, writer, context, registry, bbox):
        if not self.ignorebbox:
            bbox += self.bbox()

        if writer.text_as_path and not self.font.t1file:
            logger.warning("Cannot output text as path when font not given by a font file (like for builtin fonts).")
        if writer.text_as_path and self.font.t1file:
            deco.decoratedpath(self.textpath(), fillstyles=[]).processPDF(file, writer, context, registry, bbox)
        else:
            if self.decode:
                encodingname = self.getencodingname(writer.encodings.setdefault(self.font.name, {}))
                encoding = writer.encodings[self.font.name][encodingname]
                charcodes = [encoding[glyphname] for glyphname in self.glyphnames]
            else:
                charcodes = self.charcodes

            # create resources
            fontname = self.font.name
            if self.decode:
                newfontname = "%s-%s" % (fontname, encodingname)
                _encoding = PDFencoding(encoding, newfontname)
                fontname = newfontname
            else:
                _encoding = None
            if self.font.t1file is not None:
                if self.decode:
                    fontfile = PDFfontfile(self.font.t1file, self.glyphnames, [])
                else:
                    fontfile = PDFfontfile(self.font.t1file, [], self.charcodes)
            else:
                fontfile = None
            fontdescriptor = PDFfontdescriptor(self.font.name, fontfile, self.font.metric)
            font = PDFfont(fontname, self.font.name, charcodes, fontdescriptor, _encoding, self.font.metric)

            # register resources
            if fontfile is not None:
                registry.add(fontfile)
            registry.add(fontdescriptor)
            if _encoding is not None:
                registry.add(_encoding)
            registry.add(font)

            registry.addresource("Font", fontname, font, procset="Text")

            if self.slant is None:
                slantvalue = 0
            else:
                slantvalue = self.slant

            # select font if necessary
            sf = selectedfont(fontname, self.size_pt)
            if context.selectedfont is None or sf != context.selectedfont:
                context.selectedfont = sf
                sf.outputPDF(file, writer)

            # convert inter-character spacing to font units
            spaced = self.spaced_pt*1000/self.size_pt

            if self.kerning or spaced:
                file.write("1 0 %f 1 %f %f Tm [(" % (slantvalue, self.x_pt, self.y_pt))
            else:
                file.write("1 0 %f 1 %f %f Tm (" % (slantvalue, self.x_pt, self.y_pt))
            if self.decode:
                if self.kerning:
                    data = self.font.metric.resolvekernings(self.glyphnames)
                else:
                    data = self.glyphnames
            else:
                data = self.charcodes

            for i, value in enumerate(data):
                if self.kerning and i % 2:
                    if value is not None:
                        file.write(")%f(" % (-value-spaced))
                    elif spaced:
                        file.write(")%f(" % (-spaced))
                else:
                    if i and not self.kerning and spaced:
                        file.write(")%f(" % (-spaced))
                    if self.decode:
                        value = encoding[value]
                    if 32 <= value <= 127 and chr(value) not in "()[]<>\\":
                        file.write("%s" % chr(value))
                    else:
                        file.write("\\%03o" % value)
            if self.kerning or spaced:
                file.write(")] TJ\n")
            else:
                file.write(") Tj\n")
Exemplo n.º 4
0
    def processSVG(self, xml, writer, context, registry, bbox):
        if not self.ignorebbox:
            bbox += self.bbox()

        # this is too common to be warned about as text_as_path is the
        # default for svg due to the missing font support by current browsers
        #
        # if writer.text_as_path and not self.font.t1file:
        #     logger.warning("Cannot output text as path when font not given by a font file (like for builtin fonts).")

        if writer.text_as_path and self.font.t1file:
            deco.decoratedpath(self.textpath(), fillstyles=[]).processSVG(
                xml, writer, context, registry, bbox)
        else:
            if self.font.t1file is not None:
                if self.decode:
                    t1mapping = SVGT1file(self.font.t1file, self.glyphnames,
                                          [])
                else:
                    t1mapping = SVGT1file(self.font.t1file, [], self.charcodes)
                registry.add(t1mapping)
            else:
                if self.decode:
                    t1mapping = SVGT1mapping(self.glyphnames, [])
                else:
                    t1mapping = SVGT1mapping([], self.charcodes)

            fontname = self.font.name

            if self.decode:
                if self.kerning:
                    data = self.font.metric.resolvekernings(
                        self.glyphnames, self.size_pt)
                else:
                    data = self.glyphnames
            else:
                data = self.charcodes
            attrs = {
                "x": "%f" % self.x_pt,
                "y": "%f" % -self.y_pt,
                "font-size": "%f" % self.size_pt,
                "font-family": fontname,
                "fill": context.fillcolor
            }
            if context.fillopacity:
                attrs["opacity"] = "%f" % context.fillopacity
            if self.slant:
                trafo.trafo_pt(matrix=((1, self.slant),
                                       (0, 1))).outputSVGattrs(
                                           attrs, writer, context, registry)
            xml.startSVGElement("text", attrs)
            tspan = False
            for i, value in enumerate(data):
                if self.kerning and i % 2:
                    if value is not None:
                        if tspan:
                            xml.endSVGElement("tspan")
                        xml.startSVGElement(
                            "tspan", {"dx": "%f" % (value + self.spaced_pt)})
                        tspan = True
                    elif self.spaced_pt:
                        if tspan:
                            xml.endSVGElement("tspan")
                        xml.startSVGElement("tspan",
                                            {"dx": "%f" % (self.spaced_pt)})
                        tspan = True
                else:
                    if i and not self.kerning and self.spaced_pt:
                        if tspan:
                            xml.endSVGElement("tspan")
                        xml.startSVGElement("tspan",
                                            {"dx": "%f" % (self.spaced_pt)})
                        tspan = True
                    if self.decode:
                        xml.characters(t1mapping.glyphnames[value])
                    else:
                        xml.characters(t1mapping.charcodes[value])
            if tspan:
                xml.endSVGElement("tspan")
            xml.endSVGElement("text")
Exemplo n.º 5
0
    def processPDF(self, file, writer, context, registry, bbox):
        if not self.ignorebbox:
            bbox += self.bbox()

        if writer.text_as_path and not self.font.t1file:
            logger.warning(
                "Cannot output text as path when font not given by a font file (like for builtin fonts)."
            )
        if writer.text_as_path and self.font.t1file:
            deco.decoratedpath(self.textpath(), fillstyles=[]).processPDF(
                file, writer, context, registry, bbox)
        else:
            if self.decode:
                encodingname = self.getencodingname(
                    writer.encodings.setdefault(self.font.name, {}))
                encoding = writer.encodings[self.font.name][encodingname]
                charcodes = [
                    encoding[glyphname] for glyphname in self.glyphnames
                ]
            else:
                charcodes = self.charcodes

            # create resources
            fontname = self.font.name
            if self.decode:
                newfontname = "%s-%s" % (fontname, encodingname)
                _encoding = PDFencoding(encoding, newfontname)
                fontname = newfontname
            else:
                _encoding = None
            if self.font.t1file is not None:
                if self.decode:
                    fontfile = PDFfontfile(self.font.t1file, self.glyphnames,
                                           [])
                else:
                    fontfile = PDFfontfile(self.font.t1file, [],
                                           self.charcodes)
            else:
                fontfile = None
            fontdescriptor = PDFfontdescriptor(self.font.name, fontfile,
                                               self.font.metric)
            font = PDFfont(fontname, self.font.name, charcodes, fontdescriptor,
                           _encoding, self.font.metric)

            # register resources
            if fontfile is not None:
                registry.add(fontfile)
            registry.add(fontdescriptor)
            if _encoding is not None:
                registry.add(_encoding)
            registry.add(font)

            registry.addresource("Font", fontname, font, procset="Text")

            if self.slant is None:
                slantvalue = 0
            else:
                slantvalue = self.slant

            # select font if necessary
            sf = selectedfont(fontname, self.size_pt)
            if context.selectedfont is None or sf != context.selectedfont:
                context.selectedfont = sf
                sf.outputPDF(file, writer)

            # convert inter-character spacing to font units
            spaced = self.spaced_pt * 1000 / self.size_pt

            if self.kerning or spaced:
                file.write("1 0 %f 1 %f %f Tm [(" %
                           (slantvalue, self.x_pt, self.y_pt))
            else:
                file.write("1 0 %f 1 %f %f Tm (" %
                           (slantvalue, self.x_pt, self.y_pt))
            if self.decode:
                if self.kerning:
                    data = self.font.metric.resolvekernings(self.glyphnames)
                else:
                    data = self.glyphnames
            else:
                data = self.charcodes

            for i, value in enumerate(data):
                if self.kerning and i % 2:
                    if value is not None:
                        file.write(")%f(" % (-value - spaced))
                    elif spaced:
                        file.write(")%f(" % (-spaced))
                else:
                    if i and not self.kerning and spaced:
                        file.write(")%f(" % (-spaced))
                    if self.decode:
                        value = encoding[value]
                    if 32 <= value <= 127 and chr(value) not in "()[]<>\\":
                        file.write("%s" % chr(value))
                    else:
                        file.write("\\%03o" % value)
            if self.kerning or spaced:
                file.write(")] TJ\n")
            else:
                file.write(") Tj\n")
Exemplo n.º 6
0
    def processPS(self, file, writer, context, registry, bbox):
        if not self.ignorebbox:
            bbox += self.bbox()

        if writer.text_as_path and not self.font.t1file:
            logger.warning(
                "Cannot output text as path when font not given by a font file (like for builtin fonts)."
            )
        if writer.text_as_path and self.font.t1file:
            deco.decoratedpath(self.textpath(), fillstyles=[]).processPS(
                file, writer, context, registry, bbox)
        else:
            # register resources
            if self.font.t1file is not None:
                if self.decode:
                    registry.add(
                        PST1file(self.font.t1file, self.glyphnames, []))
                else:
                    registry.add(PST1file(self.font.t1file, [],
                                          self.charcodes))

            fontname = self.font.name
            if self.decode:
                encodingname = self.getencodingname(
                    writer.encodings.setdefault(self.font.name, {}))
                encoding = writer.encodings[self.font.name][encodingname]
                newfontname = "%s-%s" % (fontname, encodingname)
                registry.add(_ReEncodeFont)
                registry.add(PSreencodefont(fontname, newfontname, encoding))
                fontname = newfontname

            if self.slant:
                newfontmatrix = trafo.trafo_pt(matrix=((1, self.slant), (0,
                                                                         1)))
                if self.font.t1file is not None:
                    newfontmatrix = newfontmatrix * self.font.t1file.fontmatrix
                newfontname = "%s-slant%f" % (fontname, self.slant)
                registry.add(_ChangeFontMatrix)
                registry.add(
                    PSchangefontmatrix(fontname, newfontname, newfontmatrix))
                fontname = newfontname

            # select font if necessary
            sf = selectedfont(fontname, self.size_pt)
            if context.selectedfont is None or sf != context.selectedfont:
                context.selectedfont = sf
                sf.outputPS(file, writer)

            file.write("%f %f moveto (" % (self.x_pt, self.y_pt))
            if self.decode:
                if self.kerning:
                    data = self.font.metric.resolvekernings(
                        self.glyphnames, self.size_pt)
                else:
                    data = self.glyphnames
            else:
                data = self.charcodes
            for i, value in enumerate(data):
                if self.kerning and i % 2:
                    if value is not None:
                        file.write(") show\n%f 0 rmoveto (" %
                                   (value + self.spaced_pt))
                    elif self.spaced_pt:
                        file.write(") show\n%f 0 rmoveto (" % self.spaced_pt)
                else:
                    if i and not self.kerning and self.spaced_pt:
                        file.write(") show\n%f 0 rmoveto (" % self.spaced_pt)
                    if self.decode:
                        value = encoding[value]
                    if 32 < value < 127 and chr(value) not in "()[]<>\\":
                        file.write("%s" % chr(value))
                    else:
                        file.write("\\%03o" % value)
            file.write(") show\n")
Exemplo n.º 7
0
    def processPDF(self, file, writer, context, registry, bbox):
        if not self.ignorebbox:
            bbox += self.bbox()

        if writer.text_as_path:
            if self.decode:
                if self.kerning:
                    data = self.font.metric.resolvekernings(self.glyphnames, self.size_pt)
                else:
                    data = self.glyphnames
            else:
                data = self.charcodes
            textpath = path.path()
            x_pt = self.x_pt
            y_pt = self.y_pt
            for i, value in enumerate(data):
                if self.kerning and i % 2:
                    if value is not None:
                        x_pt += value
                else:
                    if i:
                        x_pt += self.spaced_pt
                    glyphpath, wx_pt, wy_pt = self.font.t1file.getglyphpathwxwy_pt(value, self.size_pt, convertcharcode=not self.decode)
                    textpath += glyphpath.transformed(trafo.translate_pt(x_pt, y_pt))
                    x_pt += wx_pt
                    y_pt += wy_pt
            deco.decoratedpath(textpath, fillstyles=[]).processPDF(file, writer, context, registry, bbox)
        else:
            if self.decode:
                encodingname = self.getencodingname(writer.encodings.setdefault(self.font.name, {}))
                encoding = writer.encodings[self.font.name][encodingname]
                charcodes = [encoding[glyphname] for glyphname in self.glyphnames]
            else:
                charcodes = self.charcodes

            # create resources
            fontname = self.font.name
            if self.decode:
                newfontname = "%s-%s" % (fontname, encodingname)
                _encoding = PDFencoding(encoding, newfontname)
                fontname = newfontname
            else:
                _encoding = None
            if self.font.t1file is not None:
                if self.decode:
                    fontfile = PDFfontfile(self.font.t1file, self.glyphnames, [])
                else:
                    fontfile = PDFfontfile(self.font.t1file, [], self.charcodes)
            else:
                fontfile = None
            fontdescriptor = PDFfontdescriptor(self.font.name, fontfile, self.font.metric)
            font = PDFfont(fontname, self.font.name, charcodes, fontdescriptor, _encoding, self.font.metric)

            # register resources
            if fontfile is not None:
                registry.add(fontfile)
            registry.add(fontdescriptor)
            if _encoding is not None:
                registry.add(_encoding)
            registry.add(font)

            registry.addresource("Font", fontname, font, procset="Text")

            if self.slant is None:
                slantvalue = 0
            else:
                slantvalue = self.slant

            # select font if necessary
            sf = selectedfont(fontname, self.size_pt)
            if context.selectedfont is None or sf != context.selectedfont:
                context.selectedfont = sf
                sf.outputPDF(file, writer)

            if self.kerning:
                file.write("1 0 %f 1 %f %f Tm [(" % (slantvalue, self.x_pt, self.y_pt))
            else:
                file.write("1 0 %f 1 %f %f Tm (" % (slantvalue, self.x_pt, self.y_pt))
            if self.decode:
                if self.kerning:
                    data = self.font.metric.resolvekernings(self.glyphnames)
                else:
                    data = self.glyphnames
            else:
                data = self.charcodes
            for i, value in enumerate(data):
                if self.kerning and i % 2:
                    if value is not None:
                        file.write(")%f(" % (-value-self.spaced_pt))
                    elif self.spaced_pt:
                        file.write(")%f(" % (-self.spaced_pt))
                else:
                    if i and not self.kerning and self.spaced_pt:
                        file.write(")%f(" % (-self.spaced_pt))
                    if self.decode:
                        value = encoding[value]
                    if 32 <= value <= 127 and chr(value) not in "()[]<>\\":
                        file.write("%s" % chr(value))
                    else:
                        file.write("\\%03o" % value)
            if self.kerning:
                file.write(")] TJ\n")
            else:
                file.write(") Tj\n")
Exemplo n.º 8
0
    def processPDF(self, file, writer, context, registry, bbox):
        if not self.ignorebbox:
            bbox += self.bbox()

        if writer.text_as_path:
            if self.decode:
                if self.kerning:
                    data = self.font.metric.resolvekernings(
                        self.glyphnames, self.size_pt)
                else:
                    data = self.glyphnames
            else:
                data = self.charcodes
            textpath = path.path()
            x_pt = self.x_pt
            y_pt = self.y_pt
            for i, value in enumerate(data):
                if self.kerning and i % 2:
                    if value is not None:
                        x_pt += value
                else:
                    if i:
                        x_pt += self.spaced_pt
                    glyphpath, wx_pt, wy_pt = self.font.t1file.getglyphpathwxwy_pt(
                        value, self.size_pt, convertcharcode=not self.decode)
                    textpath += glyphpath.transformed(
                        trafo.translate_pt(x_pt, y_pt))
                    x_pt += wx_pt
                    y_pt += wy_pt
            deco.decoratedpath(textpath, fillstyles=[]).processPDF(
                file, writer, context, registry, bbox)
        else:
            if self.decode:
                encodingname = self.getencodingname(
                    writer.encodings.setdefault(self.font.name, {}))
                encoding = writer.encodings[self.font.name][encodingname]
                charcodes = [
                    encoding[glyphname] for glyphname in self.glyphnames
                ]
            else:
                charcodes = self.charcodes

            # create resources
            fontname = self.font.name
            if self.decode:
                newfontname = "%s-%s" % (fontname, encodingname)
                _encoding = PDFencoding(encoding, newfontname)
                fontname = newfontname
            else:
                _encoding = None
            if self.font.t1file is not None:
                if self.decode:
                    fontfile = PDFfontfile(self.font.t1file, self.glyphnames,
                                           [])
                else:
                    fontfile = PDFfontfile(self.font.t1file, [],
                                           self.charcodes)
            else:
                fontfile = None
            fontdescriptor = PDFfontdescriptor(self.font.name, fontfile,
                                               self.font.metric)
            font = PDFfont(fontname, self.font.name, charcodes, fontdescriptor,
                           _encoding, self.font.metric)

            # register resources
            if fontfile is not None:
                registry.add(fontfile)
            registry.add(fontdescriptor)
            if _encoding is not None:
                registry.add(_encoding)
            registry.add(font)

            registry.addresource("Font", fontname, font, procset="Text")

            if self.slant is None:
                slantvalue = 0
            else:
                slantvalue = self.slant

            # select font if necessary
            sf = selectedfont(fontname, self.size_pt)
            if context.selectedfont is None or sf != context.selectedfont:
                context.selectedfont = sf
                sf.outputPDF(file, writer)

            if self.kerning:
                file.write("1 0 %f 1 %f %f Tm [(" %
                           (slantvalue, self.x_pt, self.y_pt))
            else:
                file.write("1 0 %f 1 %f %f Tm (" %
                           (slantvalue, self.x_pt, self.y_pt))
            if self.decode:
                if self.kerning:
                    data = self.font.metric.resolvekernings(self.glyphnames)
                else:
                    data = self.glyphnames
            else:
                data = self.charcodes
            for i, value in enumerate(data):
                if self.kerning and i % 2:
                    if value is not None:
                        file.write(")%f(" % (-value - self.spaced_pt))
                    elif self.spaced_pt:
                        file.write(")%f(" % (-self.spaced_pt))
                else:
                    if i and not self.kerning and self.spaced_pt:
                        file.write(")%f(" % (-self.spaced_pt))
                    if self.decode:
                        value = encoding[value]
                    if 32 <= value <= 127 and chr(value) not in "()[]<>\\":
                        file.write("%s" % chr(value))
                    else:
                        file.write("\\%03o" % value)
            if self.kerning:
                file.write(")] TJ\n")
            else:
                file.write(") Tj\n")