2015-11-04 18:01:34 -08:00
|
|
|
from fontTools.misc.py23 import *
|
|
|
|
|
|
|
|
import os
|
|
|
|
import unittest
|
2018-03-01 20:10:32 +00:00
|
|
|
import struct
|
2015-11-04 18:01:34 -08:00
|
|
|
|
|
|
|
from fontTools import ttLib
|
2018-03-01 19:39:24 +00:00
|
|
|
from fontTools.misc.testTools import TestCase
|
2018-03-01 18:52:55 +00:00
|
|
|
from fontTools.pens.ttGlyphPen import TTGlyphPen, MAX_F2DOT14
|
2015-11-04 18:01:34 -08:00
|
|
|
|
|
|
|
|
2018-03-01 19:39:24 +00:00
|
|
|
class TTGlyphPenTest(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__))),
|
2017-01-16 09:36:10 +00:00
|
|
|
'..', 'ttLib', 'data', filename)
|
2016-02-01 13:39:39 +00:00
|
|
|
font.importXML(ttx_path)
|
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()
|
|
|
|
|
|
|
|
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))
|
2018-06-11 18:40:11 +01:00
|
|
|
pen.addComponent("missing", (1, 0, 0, 1, 0, 0)) # skipped
|
2015-11-05 15:15:33 -08:00
|
|
|
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()
|
|
|
|
|
|
|
|
self.assertEqual(plainGlyph, compositeGlyph)
|
|
|
|
|
2016-09-15 19:11:50 +02:00
|
|
|
def test_remove_extra_move_points(self):
|
|
|
|
pen = TTGlyphPen(None)
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((100, 0))
|
|
|
|
pen.qCurveTo((100, 50), (50, 100), (0, 0))
|
|
|
|
pen.closePath()
|
|
|
|
self.assertEqual(len(pen.points), 4)
|
|
|
|
self.assertEqual(pen.points[0], (0, 0))
|
|
|
|
|
|
|
|
def test_keep_move_point(self):
|
|
|
|
pen = TTGlyphPen(None)
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((100, 0))
|
|
|
|
pen.qCurveTo((100, 50), (50, 100), (30, 30))
|
|
|
|
# when last and move pts are different, closePath() implies a lineTo
|
|
|
|
pen.closePath()
|
|
|
|
self.assertEqual(len(pen.points), 5)
|
|
|
|
self.assertEqual(pen.points[0], (0, 0))
|
|
|
|
|
|
|
|
def test_keep_duplicate_end_point(self):
|
|
|
|
pen = TTGlyphPen(None)
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((100, 0))
|
|
|
|
pen.qCurveTo((100, 50), (50, 100), (0, 0))
|
|
|
|
pen.lineTo((0, 0)) # the duplicate point is not removed
|
|
|
|
pen.closePath()
|
|
|
|
self.assertEqual(len(pen.points), 5)
|
|
|
|
self.assertEqual(pen.points[0], (0, 0))
|
|
|
|
|
2018-03-01 18:52:55 +00:00
|
|
|
def test_within_range_component_transform(self):
|
2018-03-01 11:53:10 +00:00
|
|
|
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.addComponent(componentName, (1.5, 0, 0, 1, 0, 0))
|
|
|
|
pen.addComponent(componentName, (1, 0, 0, -1.5, 0, 0))
|
|
|
|
compositeGlyph = pen.glyph()
|
|
|
|
|
|
|
|
pen.addComponent(componentName, (1.5, 0, 0, 1, 0, 0))
|
|
|
|
pen.addComponent(componentName, (1, 0, 0, -1.5, 0, 0))
|
|
|
|
expectedGlyph = pen.glyph()
|
|
|
|
|
|
|
|
self.assertEqual(expectedGlyph, compositeGlyph)
|
|
|
|
|
2018-03-01 18:52:55 +00:00
|
|
|
def test_clamp_to_almost_2_component_transform(self):
|
2018-03-01 11:53:10 +00:00
|
|
|
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.addComponent(componentName, (1.99999, 0, 0, 1, 0, 0))
|
|
|
|
pen.addComponent(componentName, (1, 2, 0, 1, 0, 0))
|
|
|
|
pen.addComponent(componentName, (1, 0, 2, 1, 0, 0))
|
|
|
|
pen.addComponent(componentName, (1, 0, 0, 2, 0, 0))
|
|
|
|
pen.addComponent(componentName, (-2, 0, 0, -2, 0, 0))
|
|
|
|
compositeGlyph = pen.glyph()
|
|
|
|
|
2018-03-01 18:52:55 +00:00
|
|
|
almost2 = MAX_F2DOT14 # 0b1.11111111111111
|
2018-03-01 11:53:10 +00:00
|
|
|
pen.addComponent(componentName, (almost2, 0, 0, 1, 0, 0))
|
|
|
|
pen.addComponent(componentName, (1, almost2, 0, 1, 0, 0))
|
|
|
|
pen.addComponent(componentName, (1, 0, almost2, 1, 0, 0))
|
|
|
|
pen.addComponent(componentName, (1, 0, 0, almost2, 0, 0))
|
|
|
|
pen.addComponent(componentName, (-2, 0, 0, -2, 0, 0))
|
|
|
|
expectedGlyph = pen.glyph()
|
|
|
|
|
|
|
|
self.assertEqual(expectedGlyph, compositeGlyph)
|
|
|
|
|
2018-03-01 19:39:24 +00:00
|
|
|
def test_out_of_range_transform_decomposed(self):
|
2018-03-01 11:53:10 +00:00
|
|
|
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())
|
|
|
|
|
2018-03-01 17:58:44 +00:00
|
|
|
pen.addComponent(componentName, (3, 0, 0, 2, 0, 0))
|
2018-03-01 18:52:55 +00:00
|
|
|
pen.addComponent(componentName, (1, 0, 0, 1, -1, 2))
|
2018-03-01 17:58:44 +00:00
|
|
|
pen.addComponent(componentName, (2, 0, 0, -3, 0, 0))
|
2018-03-01 11:53:10 +00:00
|
|
|
compositeGlyph = pen.glyph()
|
|
|
|
|
|
|
|
pen.moveTo((0, 0))
|
2018-03-01 17:58:44 +00:00
|
|
|
pen.lineTo((0, 2))
|
2018-03-01 11:53:10 +00:00
|
|
|
pen.lineTo((3, 0))
|
|
|
|
pen.closePath()
|
2018-03-01 18:52:55 +00:00
|
|
|
pen.moveTo((-1, 2))
|
|
|
|
pen.lineTo((-1, 3))
|
|
|
|
pen.lineTo((0, 2))
|
|
|
|
pen.closePath()
|
2018-03-01 11:53:10 +00:00
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((0, -3))
|
2018-03-01 17:58:44 +00:00
|
|
|
pen.lineTo((2, 0))
|
2018-03-01 11:53:10 +00:00
|
|
|
pen.closePath()
|
|
|
|
expectedGlyph = pen.glyph()
|
|
|
|
|
|
|
|
self.assertEqual(expectedGlyph, compositeGlyph)
|
|
|
|
|
2018-03-01 20:17:48 +00:00
|
|
|
def test_no_handle_overflowing_transform(self):
|
2018-03-01 20:10:32 +00:00
|
|
|
componentName = 'a'
|
|
|
|
glyphSet = {}
|
2018-03-01 20:17:48 +00:00
|
|
|
pen = TTGlyphPen(glyphSet, handleOverflowingTransforms=False)
|
2018-03-01 20:10:32 +00:00
|
|
|
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((0, 1))
|
|
|
|
pen.lineTo((1, 0))
|
|
|
|
pen.closePath()
|
|
|
|
baseGlyph = pen.glyph()
|
|
|
|
glyphSet[componentName] = _TestGlyph(baseGlyph)
|
|
|
|
|
|
|
|
pen.addComponent(componentName, (3, 0, 0, 1, 0, 0))
|
|
|
|
compositeGlyph = pen.glyph()
|
|
|
|
|
|
|
|
self.assertEqual(compositeGlyph.components[0].transform,
|
|
|
|
((3, 0), (0, 1)))
|
|
|
|
|
|
|
|
with self.assertRaises(struct.error):
|
|
|
|
compositeGlyph.compile({'a': baseGlyph})
|
|
|
|
|
2015-11-05 15:15:33 -08:00
|
|
|
|
|
|
|
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__':
|
2017-01-11 13:05:35 +00:00
|
|
|
import sys
|
|
|
|
sys.exit(unittest.main())
|