예제 #1
0
 def rebuildPrep(self):
     hintProg = Program()
     hintProg.fromBytecode([184, 1, 255, 133, 184, 0, 4, 141])
     prep = newTable("prep")
     prep.program = hintProg
     self.font["prep"] = prep
     return
예제 #2
0
 def test_xml_indentation(self):
     with open(TTPROGRAM_TTX, 'r', encoding='utf-8') as f:
         ttProgramXML = f.read()
     p = Program()
     p.fromBytecode(BYTECODE)
     ttfont = TestFont()
     buf = StringIO()
     writer = XMLWriter(buf)
     try:
         p.toXML(writer, ttfont)
     finally:
         output_string = buf.getvalue()
     assert output_string == ttProgramXML
예제 #3
0
    def test__bool__(self):
        p = Program()
        assert not bool(p)

        bc = array.array("B", [0])
        p.fromBytecode(bc)
        assert bool(p)

        assert p.bytecode.pop() == 0
        assert not bool(p)

        p = Program()
        asm = ['SVTCA[0]']
        p.fromAssembly(asm)
        assert bool(p)

        assert p.assembly.pop() == 'SVTCA[0]'
        assert not bool(p)
예제 #4
0
 def test_roundtrip(self):
     p = Program()
     p.fromBytecode(BYTECODE)
     asm = p.getAssembly(preserve=True)
     p.fromAssembly(asm)
     assert BYTECODE == p.getBytecode()
예제 #5
0
    def otf2ttf(self, maxErr=1.0, postFormat=2.0, reverseDirection=True):
        # maxErr = 1.0, approximation error, measured in units per em (UPM).
        # postFormat = 2.0, default `post` table format.
        # reverseDirection = True, assuming the input contours' direction is correctly set (counter-clockwise), we just flip it to clockwise.
        if self.font.sfntVersion != "OTTO" or not self.font.has_key(
                "CFF ") or not self.font.has_key("post"):
            print("WARNING: Invalid CFF-based font. --otf2ttf is now ignored.",
                  file=sys.stderr)
            self.jobs.convert_otf2ttf = False
            return

        # Convert cubic to quadratic
        quadGlyphs = {}
        glyphOrder = self.font.getGlyphOrder()
        glyphSet = self.font.getGlyphSet()
        for glyphName in glyphSet.keys():
            glyph = glyphSet[glyphName]
            ttPen = TTGlyphPen(glyphSet)
            cu2quPen = Cu2QuPen(ttPen, maxErr, reverseDirection)
            glyph.draw(cu2quPen)
            quadGlyphs[glyphName] = ttPen.glyph()

        # Create quadratic `glyf` table
        glyf = newTable("glyf")
        glyf.glyphOrder = glyphOrder
        glyf.glyphs = quadGlyphs
        self.font["glyf"] = glyf

        # Create global instruction table `prep` with basic rendering settings
        hintProg = Program()
        hintProg.fromBytecode([184, 1, 255, 133, 184, 0, 4, 141])
        prep = newTable("prep")
        prep.program = hintProg
        self.font["prep"] = prep

        # Create `gasp` table
        gasp = newTable("gasp")
        gasp.version = 1
        gasp.gaspRange = {65535: 10}
        self.font["gasp"] = gasp

        # Create partial TrueType `maxp` table (v1.0)
        maxp = newTable("maxp")
        maxp.tableVersion = 0x00010000
        maxp.maxZones = 1
        maxp.maxTwilightPoints = 0
        maxp.maxStorage = 0
        maxp.maxFunctionDefs = 0
        maxp.maxInstructionDefs = 0
        maxp.maxStackElements = 0
        maxp.maxSizeOfInstructions = 0
        maxp.maxComponentElements = max(
            len(g.components if hasattr(g, "components") else [])
            for g in glyf.glyphs.values())
        self.font["maxp"] = maxp

        # Create an empty `loca` table, which will be automatically generated upon compile
        self.font["loca"] = newTable("loca")

        # Modify `post` table
        post = self.font["post"]
        post.formatType = postFormat
        post.extraNames = []
        post.mapping = {}
        post.glyphOrder = glyphOrder

        # Change sfntVersion from CFF to TrueType
        self.font.sfntVersion = "\x00\x01\x00\x00"

        # Recalculate missing properties in `head`, `glyf`, `maxp` upon compile
        self.font.recalcBBoxes = True

        # Clean-ups
        del self.font["CFF "]
        if self.font.has_key("VORG"):
            del self.font["VORG"]
        return