2015-11-04 18:01:34 -08:00
|
|
|
from __future__ import print_function, division, absolute_import
|
|
|
|
from fontTools.misc.py23 import *
|
|
|
|
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from fontTools import ttLib
|
|
|
|
from fontTools.pens.ttGlyphPen import TTGlyphPen
|
|
|
|
|
|
|
|
|
|
|
|
class TTGlyphPenTest(unittest.TestCase):
|
2015-11-05 14:01:52 -08:00
|
|
|
|
|
|
|
def runEndToEnd(self, filename):
|
|
|
|
font = ttLib.TTFont()
|
2015-11-04 18:01:34 -08:00
|
|
|
ttx_path = os.path.join(
|
|
|
|
os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
|
2015-11-05 14:01:52 -08:00
|
|
|
'..', 'ttLib', 'testdata', filename)
|
|
|
|
font.importXML(ttx_path, quiet=True)
|
2015-11-04 18:01:34 -08:00
|
|
|
|
2015-11-05 14:01:52 -08:00
|
|
|
glyphSet = font.getGlyphSet()
|
|
|
|
glyfTable = font['glyf']
|
|
|
|
pen = TTGlyphPen(font.getGlyphSet())
|
2015-11-04 18:01:34 -08:00
|
|
|
|
2015-11-05 14:01:52 -08:00
|
|
|
for name in font.getGlyphOrder():
|
2015-11-04 18:01:34 -08:00
|
|
|
oldGlyph = glyphSet[name]
|
2015-11-05 14:01:52 -08:00
|
|
|
oldGlyph.draw(pen)
|
2015-11-04 18:01:34 -08:00
|
|
|
oldGlyph = oldGlyph._glyph
|
2015-11-05 14:01:52 -08:00
|
|
|
newGlyph = pen.glyph()
|
|
|
|
|
2015-11-04 18:01:34 -08:00
|
|
|
if hasattr(oldGlyph, 'program'):
|
|
|
|
newGlyph.program = oldGlyph.program
|
2015-11-05 14:01:52 -08:00
|
|
|
|
2015-11-04 18:01:34 -08:00
|
|
|
self.assertEqual(
|
|
|
|
oldGlyph.compile(glyfTable), newGlyph.compile(glyfTable))
|
|
|
|
|
2015-11-05 14:01:52 -08:00
|
|
|
def test_e2e_linesAndSimpleComponents(self):
|
|
|
|
self.runEndToEnd('TestTTF-Regular.ttx')
|
|
|
|
|
|
|
|
def test_e2e_curvesAndComponentTransforms(self):
|
|
|
|
self.runEndToEnd('TestTTFComplex-Regular.ttx')
|
|
|
|
|
2015-11-05 15:15:33 -08:00
|
|
|
def test_moveTo_errorWithinContour(self):
|
|
|
|
pen = TTGlyphPen(None)
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
pen.moveTo((1, 0))
|
|
|
|
|
|
|
|
def test_closePath_ignoresAnchors(self):
|
|
|
|
pen = TTGlyphPen(None)
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.closePath()
|
|
|
|
self.assertFalse(pen.points)
|
2015-11-05 15:26:24 -08:00
|
|
|
self.assertFalse(pen.types)
|
|
|
|
self.assertFalse(pen.endPts)
|
2015-11-05 15:15:33 -08:00
|
|
|
|
|
|
|
def test_endPath_sameAsClosePath(self):
|
|
|
|
pen = TTGlyphPen(None)
|
|
|
|
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((0, 1))
|
|
|
|
pen.lineTo((1, 0))
|
|
|
|
pen.closePath()
|
|
|
|
closePathGlyph = pen.glyph()
|
|
|
|
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((0, 1))
|
|
|
|
pen.lineTo((1, 0))
|
|
|
|
pen.endPath()
|
|
|
|
endPathGlyph = pen.glyph()
|
|
|
|
|
|
|
|
endPathGlyph.program = closePathGlyph.program
|
|
|
|
self.assertEqual(closePathGlyph, endPathGlyph)
|
|
|
|
|
|
|
|
def test_glyph_errorOnUnendedContour(self):
|
|
|
|
pen = TTGlyphPen(None)
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
pen.glyph()
|
|
|
|
|
|
|
|
def test_glyph_decomposes(self):
|
|
|
|
componentName = 'a'
|
|
|
|
glyphSet = {}
|
|
|
|
pen = TTGlyphPen(glyphSet)
|
|
|
|
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((0, 1))
|
|
|
|
pen.lineTo((1, 0))
|
|
|
|
pen.closePath()
|
|
|
|
glyphSet[componentName] = _TestGlyph(pen.glyph())
|
|
|
|
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((0, 1))
|
|
|
|
pen.lineTo((1, 0))
|
|
|
|
pen.closePath()
|
|
|
|
pen.addComponent(componentName, (1, 0, 0, 1, 2, 0))
|
|
|
|
compositeGlyph = pen.glyph()
|
|
|
|
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((0, 1))
|
|
|
|
pen.lineTo((1, 0))
|
|
|
|
pen.closePath()
|
|
|
|
pen.moveTo((2, 0))
|
|
|
|
pen.lineTo((2, 1))
|
|
|
|
pen.lineTo((3, 0))
|
|
|
|
pen.closePath()
|
|
|
|
plainGlyph = pen.glyph()
|
|
|
|
|
|
|
|
plainGlyph.program = compositeGlyph.program
|
|
|
|
self.assertEqual(plainGlyph, compositeGlyph)
|
|
|
|
|
|
|
|
|
|
|
|
class _TestGlyph(object):
|
|
|
|
def __init__(self, glyph):
|
|
|
|
self.coordinates = glyph.coordinates
|
|
|
|
|
|
|
|
def draw(self, pen):
|
|
|
|
pen.moveTo(self.coordinates[0])
|
|
|
|
for point in self.coordinates[1:]:
|
|
|
|
pen.lineTo(point)
|
|
|
|
pen.closePath()
|
|
|
|
|
2015-11-04 18:01:34 -08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|