Esempio n. 1
0
    def getdata2(self, subrs=None, glyphs=None):
        """makes a data2 string

        subrs is a dict containing those subrs numbers as keys,
        which are to be contained in the subrsstring to be created.
        If subrs is None, all subrs in self.subrs will be used.
        The subrs dict might be modified *in place*.

        glyphs is a dict containing those glyph names as keys,
        which are to be contained in the charstringsstring to be created.
        If glyphs is None, all glyphs in self.glyphs will be used."""
        w = writer.writer(io.BytesIO())

        def addsubrs(subrs):
            if subrs is not None:
                # some adjustments to the subrs dict
                if subrs:
                    subrsmin = min(subrs)
                    subrsmax = max(subrs)
                    if self.hasflexhintsubrs and subrsmin < len(self.flexhintsubrs):
                        # According to the spec we need to keep all the flex and hint subrs
                        # as long as any of it is used.
                        for subr in range(len(self.flexhintsubrs)):
                            subrs.add(subr)
                        subrsmax = max(subrs)
                else:
                    subrsmax = -1
            else:
                # build a new subrs dict containing all subrs
                subrs = dict([(subr, 1) for subr in range(len(self.subrs))])
                subrsmax = len(self.subrs) - 1

            # build the string from all selected subrs
            w.write("%d array\n" % (subrsmax + 1))
            for subr in range(subrsmax+1):
                if subr in subrs:
                    code = self.subrs[subr]
                else:
                    code = self.emptysubr
                w.write("dup %d %d " % (subr, len(code)))
                w.write_bytes(self.subrrdtoken)
                w.write_bytes(b" ")
                w.write_bytes(code)
                w.write_bytes(b" ")
                w.write_bytes(self.subrnptoken)
                w.write_bytes(b"\n")

        def addcharstrings(glyphs):
            w.write("%d dict dup begin\n" % (glyphs is None and len(self.glyphlist) or len(glyphs)))
            for glyph in self.glyphlist:
                if glyphs is None or glyph in glyphs:
                    w.write("/%s %d " % (glyph, len(self.glyphs[glyph])))
                    w.write_bytes(self.glyphrdtoken)
                    w.write_bytes(b" ")
                    w.write_bytes(self.glyphs[glyph])
                    w.write_bytes(b" ")
                    w.write_bytes(self.glyphndtoken)
                    w.write_bytes(b"\n")
            w.write("end\n")

        if self.subrsstart < self.charstringsstart:
            w.write_bytes(self._data2[:self.subrsstart])
            addsubrs(subrs)
            w.write_bytes(self._data2[self.subrsend:self.charstringsstart])
            addcharstrings(glyphs)
            w.write_bytes(self._data2[self.charstringsend:])
        else:
            w.write_bytes(self._data2[:self.charstringsstart])
            addcharstrings(glyphs)
            w.write_bytes(self._data2[self.charstringsend:self.subrsstart])
            addsubrs(subrs)
            w.write_bytes(self._data2[self.subrsend:])
        return w.file.getvalue()
Esempio n. 2
0
    def getdata2(self, subrs=None, glyphs=None):
        """makes a data2 string

        subrs is a dict containing those subrs numbers as keys,
        which are to be contained in the subrsstring to be created.
        If subrs is None, all subrs in self.subrs will be used.
        The subrs dict might be modified *in place*.

        glyphs is a dict containing those glyph names as keys,
        which are to be contained in the charstringsstring to be created.
        If glyphs is None, all glyphs in self.glyphs will be used."""
        w = writer.writer(io.BytesIO())

        def addsubrs(subrs):
            if subrs is not None:
                # some adjustments to the subrs dict
                if subrs:
                    subrsmin = min(subrs)
                    subrsmax = max(subrs)
                    if self.hasflexhintsubrs and subrsmin < len(
                            self.flexhintsubrs):
                        # According to the spec we need to keep all the flex and hint subrs
                        # as long as any of it is used.
                        for subr in range(len(self.flexhintsubrs)):
                            subrs.add(subr)
                        subrsmax = max(subrs)
                else:
                    subrsmax = -1
            else:
                # build a new subrs dict containing all subrs
                subrs = dict([(subr, 1) for subr in range(len(self.subrs))])
                subrsmax = len(self.subrs) - 1

            # build the string from all selected subrs
            w.write("%d array\n" % (subrsmax + 1))
            for subr in range(subrsmax + 1):
                if subr in subrs:
                    code = self.subrs[subr]
                else:
                    code = self.emptysubr
                w.write("dup %d %d " % (subr, len(code)))
                w.write_bytes(self.subrrdtoken)
                w.write_bytes(b" ")
                w.write_bytes(code)
                w.write_bytes(b" ")
                w.write_bytes(self.subrnptoken)
                w.write_bytes(b"\n")

        def addcharstrings(glyphs):
            w.write("%d dict dup begin\n" %
                    (glyphs is None and len(self.glyphlist) or len(glyphs)))
            for glyph in self.glyphlist:
                if glyphs is None or glyph in glyphs:
                    w.write("/%s %d " % (glyph, len(self.glyphs[glyph])))
                    w.write_bytes(self.glyphrdtoken)
                    w.write_bytes(b" ")
                    w.write_bytes(self.glyphs[glyph])
                    w.write_bytes(b" ")
                    w.write_bytes(self.glyphndtoken)
                    w.write_bytes(b"\n")
            w.write("end\n")

        if self.subrsstart < self.charstringsstart:
            w.write_bytes(self._data2[:self.subrsstart])
            addsubrs(subrs)
            w.write_bytes(self._data2[self.subrsend:self.charstringsstart])
            addcharstrings(glyphs)
            w.write_bytes(self._data2[self.charstringsend:])
        else:
            w.write_bytes(self._data2[:self.charstringsstart])
            addcharstrings(glyphs)
            w.write_bytes(self._data2[self.charstringsend:self.subrsstart])
            addsubrs(subrs)
            w.write_bytes(self._data2[self.subrsend:])
        return w.file.getvalue()