from __future__ import print_function, division, absolute_import
from __future__ import unicode_literals
from fontTools.misc.testTools import getXML
from fontTools.otlLib import builder
from fontTools.ttLib.tables import otTables
from itertools import chain
import unittest
class BuilderTest(unittest.TestCase):
GLYPHS = (".notdef space zero one two three four five six "
"A B C a b c grave acute cedilla f_f_i f_i c_t").split()
GLYPHMAP = {name: num for num, name in enumerate(GLYPHS)}
ANCHOR1 = builder.buildAnchor(11, -11)
ANCHOR2 = builder.buildAnchor(22, -22)
ANCHOR3 = builder.buildAnchor(33, -33)
def __init__(self, methodName):
unittest.TestCase.__init__(self, methodName)
# Python 3 renamed assertRaisesRegexp to assertRaisesRegex,
# and fires deprecation warnings if a program uses the old name.
if not hasattr(self, "assertRaisesRegex"):
self.assertRaisesRegex = self.assertRaisesRegexp
@classmethod
def setUpClass(cls):
cls.maxDiff = None
def test_buildAnchor_format1(self):
anchor = builder.buildAnchor(23, 42)
self.assertEqual(getXML(anchor.toXML),
['',
' ',
' ',
''])
def test_buildAnchor_format2(self):
anchor = builder.buildAnchor(23, 42, point=17)
self.assertEqual(getXML(anchor.toXML),
['',
' ',
' ',
' ',
''])
def test_buildAnchor_format3(self):
anchor = builder.buildAnchor(
23, 42,
deviceX=builder.buildDevice({1: 1, 0: 0}),
deviceY=builder.buildDevice({7: 7}))
self.assertEqual(getXML(anchor.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildAttachList(self):
attachList = builder.buildAttachList({
"zero": [23, 7], "one": [1],
}, self.GLYPHMAP)
self.assertEqual(getXML(attachList.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildAttachList_empty(self):
self.assertIsNone(builder.buildAttachList({}, self.GLYPHMAP))
def test_buildAttachPoint(self):
attachPoint = builder.buildAttachPoint([7, 3])
self.assertEqual(getXML(attachPoint.toXML),
['',
' ',
' ',
' ',
''])
def test_buildAttachPoint_empty(self):
self.assertIsNone(builder.buildAttachPoint([]))
def test_buildAttachPoint_duplicate(self):
attachPoint = builder.buildAttachPoint([7, 3, 7])
self.assertEqual(getXML(attachPoint.toXML),
['',
' ',
' ',
' ',
''])
def test_buildBaseArray(self):
anchor = builder.buildAnchor
baseArray = builder.buildBaseArray({
"a": {2: anchor(300, 80)},
"c": {1: anchor(300, 80), 2: anchor(300, -20)}
}, numMarkClasses=4, glyphMap=self.GLYPHMAP)
self.assertEqual(getXML(baseArray.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildBaseRecord(self):
a = builder.buildAnchor
rec = builder.buildBaseRecord([a(500, -20), None, a(300, -15)])
self.assertEqual(getXML(rec.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildCaretValueForCoord(self):
caret = builder.buildCaretValueForCoord(500)
self.assertEqual(getXML(caret.toXML),
['',
' ',
''])
def test_buildCaretValueForPoint(self):
caret = builder.buildCaretValueForPoint(23)
self.assertEqual(getXML(caret.toXML),
['',
' ',
''])
def test_buildComponentRecord(self):
a = builder.buildAnchor
rec = builder.buildComponentRecord([a(500, -20), None, a(300, -15)])
self.assertEqual(getXML(rec.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildComponentRecord_empty(self):
self.assertIsNone(builder.buildComponentRecord([]))
def test_buildComponentRecord_None(self):
self.assertIsNone(builder.buildComponentRecord(None))
def test_buildCoverage(self):
cov = builder.buildCoverage({"two", "four"}, {"two": 2, "four": 4})
self.assertEqual(getXML(cov.toXML),
['',
' ',
' ',
''])
def test_buildCursivePos(self):
pos = builder.buildCursivePosSubtable({
"two": (self.ANCHOR1, self.ANCHOR2),
"four": (self.ANCHOR3, self.ANCHOR1)
}, self.GLYPHMAP)
self.assertEqual(getXML(pos.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildDevice_format1(self):
device = builder.buildDevice({1:1, 0:0})
self.assertEqual(getXML(device.toXML),
['',
' ',
' ',
' ',
' ',
''])
def test_buildDevice_format2(self):
device = builder.buildDevice({2:2, 0:1, 1:0})
self.assertEqual(getXML(device.toXML),
['',
' ',
' ',
' ',
' ',
''])
def test_buildDevice_format3(self):
device = builder.buildDevice({5:3, 1:77})
self.assertEqual(getXML(device.toXML),
['',
' ',
' ',
' ',
' ',
''])
def test_buildLigatureArray(self):
anchor = builder.buildAnchor
ligatureArray = builder.buildLigatureArray({
"f_i": [{2: anchor(300, -20)}, {}],
"c_t": [{}, {1: anchor(500, 350), 2: anchor(1300, -20)}]
}, numMarkClasses=4, glyphMap=self.GLYPHMAP)
self.assertEqual(getXML(ligatureArray.toXML),
['',
' ',
' ', # f_i
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildLigatureAttach(self):
anchor = builder.buildAnchor
attach = builder.buildLigatureAttach([
[anchor(500, -10), None],
[None, anchor(300, -20), None]])
self.assertEqual(getXML(attach.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildLigatureAttach_emptyComponents(self):
attach = builder.buildLigatureAttach([[], None])
self.assertEqual(getXML(attach.toXML),
['',
' ',
' ',
' ',
''])
def test_buildLigatureAttach_noComponents(self):
attach = builder.buildLigatureAttach([])
self.assertEqual(getXML(attach.toXML),
['',
' ',
''])
def test_buildLigCaretList(self):
carets = builder.buildLigCaretList(
{"f_f_i": [300, 600]}, {"c_t": [42]}, self.GLYPHMAP)
self.assertEqual(getXML(carets.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildLigCaretList_bothCoordsAndPointsForSameGlyph(self):
carets = builder.buildLigCaretList(
{"f_f_i": [300]}, {"f_f_i": [7]}, self.GLYPHMAP)
self.assertEqual(getXML(carets.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildLigCaretList_empty(self):
self.assertIsNone(builder.buildLigCaretList({}, {}, self.GLYPHMAP))
def test_buildLigCaretList_None(self):
self.assertIsNone(builder.buildLigCaretList(None, None, self.GLYPHMAP))
def test_buildLigGlyph_coords(self):
lig = builder.buildLigGlyph([500, 800], None)
self.assertEqual(getXML(lig.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildLigGlyph_empty(self):
self.assertIsNone(builder.buildLigGlyph([], []))
def test_buildLigGlyph_None(self):
self.assertIsNone(builder.buildLigGlyph(None, None))
def test_buildLigGlyph_points(self):
lig = builder.buildLigGlyph(None, [2])
self.assertEqual(getXML(lig.toXML),
['',
' ',
' ',
' ',
' ',
''])
def test_buildLookup(self):
s1 = builder.buildSingleSubstSubtable({"one": "two"})
s2 = builder.buildSingleSubstSubtable({"three": "four"})
lookup = builder.buildLookup([s1, s2], flags=7)
self.assertEqual(getXML(lookup.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildLookup_badFlags(self):
s = builder.buildSingleSubstSubtable({"one": "two"})
self.assertRaisesRegex(
AssertionError, "if markFilterSet is None, "
"flags must not set LOOKUP_FLAG_USE_MARK_FILTERING_SET; "
"flags=0x0010",
builder.buildLookup, [s],
builder.LOOKUP_FLAG_USE_MARK_FILTERING_SET, None)
self.assertRaisesRegex(
AssertionError, "if markFilterSet is not None, "
"flags must set LOOKUP_FLAG_USE_MARK_FILTERING_SET; "
"flags=0x0004",
builder.buildLookup, [s],
builder.LOOKUP_FLAG_IGNORE_LIGATURES, 777)
def test_buildLookup_conflictingSubtableTypes(self):
s1 = builder.buildSingleSubstSubtable({"one": "two"})
s2 = builder.buildAlternateSubstSubtable({"one": ["two", "three"]})
self.assertRaisesRegex(
AssertionError, "all subtables must have the same LookupType",
builder.buildLookup, [s1, s2])
def test_buildLookup_noSubtables(self):
self.assertIsNone(builder.buildLookup([]))
self.assertIsNone(builder.buildLookup(None))
self.assertIsNone(builder.buildLookup([None]))
self.assertIsNone(builder.buildLookup([None, None]))
def test_buildLookup_markFilterSet(self):
s = builder.buildSingleSubstSubtable({"one": "two"})
flags = (builder.LOOKUP_FLAG_RIGHT_TO_LEFT |
builder.LOOKUP_FLAG_USE_MARK_FILTERING_SET)
lookup = builder.buildLookup([s], flags, markFilterSet=999)
self.assertEqual(getXML(lookup.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildMarkArray(self):
markArray = builder.buildMarkArray({
"acute": (7, builder.buildAnchor(300, 800)),
"grave": (2, builder.buildAnchor(10, 80))
}, self.GLYPHMAP)
self.assertLess(self.GLYPHMAP["grave"], self.GLYPHMAP["acute"])
self.assertEqual(getXML(markArray.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildMarkBasePosSubtable(self):
anchor = builder.buildAnchor
marks = {
"acute": (0, anchor(300, 700)),
"cedilla": (1, anchor(300, -100)),
"grave": (0, anchor(300, 700))
}
bases = {
# Make sure we can handle missing entries.
"A": {}, # no entry for any markClass
"B": {0: anchor(500, 900)}, # only markClass 0 specified
"C": {1: anchor(500, -10)}, # only markClass 1 specified
"a": {0: anchor(500, 400), 1: anchor(500, -20)},
"b": {0: anchor(500, 800), 1: anchor(500, -20)}
}
table = builder.buildMarkBasePosSubtable(marks, bases, self.GLYPHMAP)
self.assertEqual(getXML(table.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ', # grave
' ',
' ',
' ',
' ',
' ',
' ',
' ', # acute
' ',
' ',
' ',
' ',
' ',
' ',
' ', # cedilla
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ', # A
' ',
' ',
' ',
' ', # B
' ',
' ',
' ',
' ',
' ',
' ',
' ', # C
' ',
' ',
' ',
' ',
' ',
' ',
' ', # a
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ', # b
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildMarkGlyphSetsDef(self):
marksets = builder.buildMarkGlyphSetsDef(
[{"acute", "grave"}, {"cedilla", "grave"}], self.GLYPHMAP)
self.assertEqual(getXML(marksets.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildMarkGlyphSetsDef_empty(self):
self.assertIsNone(builder.buildMarkGlyphSetsDef([], self.GLYPHMAP))
def test_buildMarkGlyphSetsDef_None(self):
self.assertIsNone(builder.buildMarkGlyphSetsDef(None, self.GLYPHMAP))
def test_buildMarkLigPosSubtable(self):
anchor = builder.buildAnchor
marks = {
"acute": (0, anchor(300, 700)),
"cedilla": (1, anchor(300, -100)),
"grave": (0, anchor(300, 700))
}
bases = {
"f_i": [{}, {0: anchor(200, 400)}], # nothing on f; only 1 on i
"c_t": [
{0: anchor(500, 600), 1: anchor(500, -20)}, # c
{0: anchor(1300, 800), 1: anchor(1300, -20)} # t
]
}
table = builder.buildMarkLigPosSubtable(marks, bases, self.GLYPHMAP)
self.assertEqual(getXML(table.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildMarkRecord(self):
rec = builder.buildMarkRecord(17, builder.buildAnchor(500, -20))
self.assertEqual(getXML(rec.toXML),
['',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildMark2Record(self):
a = builder.buildAnchor
rec = builder.buildMark2Record([a(500, -20), None, a(300, -15)])
self.assertEqual(getXML(rec.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildPairPosClassesSubtable(self):
d20 = builder.buildValue({"XPlacement": -20})
d50 = builder.buildValue({"XPlacement": -50})
d0 = builder.buildValue({})
d8020 = builder.buildValue({"XPlacement": -80, "YPlacement": -20})
subtable = builder.buildPairPosClassesSubtable({
(tuple("A",), tuple(["zero"])): (d0, d50),
(tuple("A",), tuple(["one", "two"])): (None, d20),
(tuple(["B", "C"]), tuple(["zero"])): (d8020, d50),
}, self.GLYPHMAP)
self.assertEqual(getXML(subtable.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildPairPosGlyphs(self):
d50 = builder.buildValue({"XPlacement": -50})
d8020 = builder.buildValue({"XPlacement": -80, "YPlacement": -20})
subtables = builder.buildPairPosGlyphs({
("A", "zero"): (None, d50),
("A", "one"): (d8020, d50),
}, self.GLYPHMAP)
self.assertEqual(sum([getXML(t.toXML) for t in subtables], []),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
'',
'',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildPairPosGlyphsSubtable(self):
d20 = builder.buildValue({"XPlacement": -20})
d50 = builder.buildValue({"XPlacement": -50})
d0 = builder.buildValue({})
d8020 = builder.buildValue({"XPlacement": -80, "YPlacement": -20})
subtable = builder.buildPairPosGlyphsSubtable({
("A", "zero"): (d0, d50),
("A", "one"): (None, d20),
("B", "five"): (d8020, d50),
}, self.GLYPHMAP)
self.assertEqual(getXML(subtable.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildSinglePos(self):
subtables = builder.buildSinglePos({
"one": builder.buildValue({"XPlacement": 500}),
"two": builder.buildValue({"XPlacement": 500}),
"three": builder.buildValue({"XPlacement": 200}),
"four": builder.buildValue({"XPlacement": 400}),
"five": builder.buildValue({"XPlacement": 500}),
"six": builder.buildValue({"YPlacement": -6}),
}, self.GLYPHMAP)
self.assertEqual(sum([getXML(t.toXML) for t in subtables], []),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
'',
'',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
'',
'',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildSinglePos_ValueFormat0(self):
subtables = builder.buildSinglePos({
"zero": builder.buildValue({})
}, self.GLYPHMAP)
self.assertEqual(sum([getXML(t.toXML) for t in subtables], []),
['',
' ',
' ',
' ',
' ',
''])
def test_buildSinglePosSubtable_format1(self):
subtable = builder.buildSinglePosSubtable({
"one": builder.buildValue({"XPlacement": 777}),
"two": builder.buildValue({"XPlacement": 777}),
}, self.GLYPHMAP)
self.assertEqual(getXML(subtable.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildSinglePosSubtable_format2(self):
subtable = builder.buildSinglePosSubtable({
"one": builder.buildValue({"XPlacement": 777}),
"two": builder.buildValue({"YPlacement": -888}),
}, self.GLYPHMAP)
self.assertEqual(getXML(subtable.toXML),
['',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
''])
def test_buildValue(self):
value = builder.buildValue({"XPlacement": 7, "YPlacement": 23})
func = lambda writer, font: value.toXML(writer, font, valueName="Val")
self.assertEqual(getXML(func),
[''])
def test_getLigatureKey(self):
components = lambda s: [tuple(word) for word in s.split()]
c = components("fi fl ff ffi fff")
c.sort(key=builder._getLigatureKey)
self.assertEqual(c, components("fff ffi ff fi fl"))
def test_getSinglePosValueKey(self):
device = builder.buildDevice({10:1, 11:3})
a1 = builder.buildValue({"XPlacement": 500, "XPlaDevice": device})
a2 = builder.buildValue({"XPlacement": 500, "XPlaDevice": device})
b = builder.buildValue({"XPlacement": 500})
keyA1 = builder._getSinglePosValueKey(a1)
keyA2 = builder._getSinglePosValueKey(a1)
keyB = builder._getSinglePosValueKey(b)
self.assertEqual(keyA1, keyA2)
self.assertEqual(hash(keyA1), hash(keyA2))
self.assertNotEqual(keyA1, keyB)
self.assertNotEqual(hash(keyA1), hash(keyB))
class ClassDefBuilderTest(unittest.TestCase):
def test_build_usingClass0(self):
b = builder.ClassDefBuilder(useClass0=True)
b.add({"aa", "bb"})
b.add({"a", "b"})
b.add({"c"})
b.add({"e", "f", "g", "h"})
cdef = b.build()
self.assertIsInstance(cdef, otTables.ClassDef)
self.assertEqual(cdef.classDefs, {
"a": 2,
"b": 2,
"c": 3,
"aa": 1,
"bb": 1
})
def test_build_notUsingClass0(self):
b = builder.ClassDefBuilder(useClass0=False)
b.add({"a", "b"})
b.add({"c"})
b.add({"e", "f", "g", "h"})
cdef = b.build()
self.assertIsInstance(cdef, otTables.ClassDef)
self.assertEqual(cdef.classDefs, {
"a": 2,
"b": 2,
"c": 3,
"e": 1,
"f": 1,
"g": 1,
"h": 1
})
def test_canAdd(self):
b = builder.ClassDefBuilder(useClass0=True)
b.add({"a", "b", "c", "d"})
b.add({"e", "f"})
self.assertTrue(b.canAdd({"a", "b", "c", "d"}))
self.assertTrue(b.canAdd({"e", "f"}))
self.assertTrue(b.canAdd({"g", "h", "i"}))
self.assertFalse(b.canAdd({"b", "c", "d"}))
self.assertFalse(b.canAdd({"a", "b", "c", "d", "e", "f"}))
self.assertFalse(b.canAdd({"d", "e", "f"}))
self.assertFalse(b.canAdd({"f"}))
if __name__ == "__main__":
import sys
sys.exit(unittest.main())