Example #1
0
    def __init__(self, data1, data2eexec, data3):
        """initializes a t1font instance

        data1 and data3 are the two clear text data parts and data2 is
        the binary data part"""
        self.data1 = data1
        self._data2eexec = data2eexec
        self.data3 = data3

        # marker and value for decoded data
        self._data2 = None
        # note that data2eexec is set to none by setsubrcmds and setglyphcmds
        # this *also* denotes, that data2 is out-of-date; hence they are both
        # marked by an _ and getdata2 and getdata2eexec will properly resolve
        # the current state of decoding ...

        # marker and value for standard encoding check
        self.encoding = None

        self.name, = self.fontnamepattern.search(self.data1).groups()
        m11, m12, m21, m22, v1, v2 = list(
            map(float,
                self.fontmatrixpattern.search(self.data1).groups()[:6]))
        self.fontmatrix = trafo.trafo_pt(matrix=((m11, m12), (m21, m22)),
                                         vector=(v1, v2))
Example #2
0
 def getglyphpathwxwy_pt(self, glyph, size):
     m = self.fontmatrixpattern.search(self.data1)
     m11, m12, m21, m22, v1, v2 = map(float, m.groups()[:6])
     t = trafo.trafo_pt(matrix=((m11, m12), (m21, m22)), vector=(v1, v2)).scaled(size)
     context = T1context(self)
     p = path()
     self.updateglyphpath(glyph, p, t, context)
     wx, wy = t.apply_pt(context.wx, context.wy)
     return p, wx, wy
Example #3
0
 def getglyphpathwxwy_pt(self, glyph, size):
     m = self.fontmatrixpattern.search(self.data1)
     m11, m12, m21, m22, v1, v2 = map(float, m.groups()[:6])
     t = trafo.trafo_pt(matrix=((m11, m12), (m21, m22)),
                        vector=(v1, v2)).scaled(size)
     context = T1context(self)
     p = path()
     self.updateglyphpath(glyph, p, t, context)
     wx, wy = t.apply_pt(context.wx, context.wy)
     return p, wx, wy
Example #4
0
    def __init__(self, data1, data2eexec, data3):
        """initializes a t1font instance

        data1 and data3 are the two clear text data parts and data2 is
        the binary data part"""
        self.data1 = data1
        self._data2eexec = data2eexec
        self.data3 = data3

        # marker and value for decoded data
        self._data2 = None
        # note that data2eexec is set to none by setsubrcmds and setglyphcmds
        # this *also* denotes, that data2 is out-of-date; hence they are both
        # marked by an _ and getdata2 and getdata2eexec will properly resolve
        # the current state of decoding ...

        # marker and value for standard encoding check
        self.encoding = None

        self.name, = self.fontnamepattern.search(self.data1).groups()
        m11, m12, m21, m22, v1, v2 = list(map(float, self.fontmatrixpattern.search(self.data1).groups()[:6]))
        self.fontmatrix = trafo.trafo_pt(matrix=((m11, m12), (m21, m22)), vector=(v1, v2))
Example #5
0
File: font.py Project: 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")
Example #6
0
File: font.py Project: 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")
Example #7
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")
Example #8
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")