-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathreorder_glyphs_test.py
169 lines (140 loc) · 5.33 KB
/
reorder_glyphs_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from fontTools import ttLib
from functools import reduce
from nanoemoji.reorder_glyphs import (
_REORDER_RULES,
ReorderCoverage,
ReorderList,
_sort_by_gid,
reorder_glyphs,
)
from nanoemoji.util import load_fully
from nanoemoji import write_font
from nanoemoji.util import only
import os
from pathlib import Path
import pytest
import tempfile
import test_helper
def _dotted_converter(item, dotted_attr):
attr_names = dotted_attr.split(".")
assert attr_names
while attr_names:
attr_name = attr_names.pop(0)
item = item.getConverterByName(attr_name)
# Do we have to descend?
if attr_names:
item = item.tableClass()
return item
def test_metadata_is_valid():
for (clazz, fmt), reorders in _REORDER_RULES.items():
instance = clazz()
instance.Format = fmt
assert (
instance.getConverters()
), f"Lack of converters suggests {clazz} dislikes Format {fmt}"
for reorder in reorders:
if isinstance(reorder, ReorderCoverage):
assert (
_dotted_converter(instance, reorder.coverage_attr) is not None
), f"No {clazz} {fmt} {reorder.coverage_attr}"
if reorder.parallel_list_attr:
assert (
_dotted_converter(instance, reorder.parallel_list_attr)
is not None
), f"No {clazz} {fmt} {reorder.parallel_list_attr}"
elif isinstance(reorder, ReorderList):
assert (
_dotted_converter(instance, reorder.list_attr) is not None
), f"No {clazz} {fmt} {reorder.list_attr}"
else:
raise NotImplementedError(type(reorder))
def test_sort_just_glyphs():
glyphs = ["a", "b", "c", "d"]
gids = [42, 0, 4, 1]
_sort_by_gid(lambda gn: gids[glyphs.index(gn)], glyphs, None)
assert glyphs == ["b", "d", "c", "a"]
def test_sort_parallel_list():
glyphs = ["a", "b", "c", "d"]
parallel = ["aa", "bb", "cc", "dd"]
gids = [0, 42, 16, 2]
_sort_by_gid(lambda gn: gids[glyphs.index(gn)], glyphs, parallel)
assert glyphs == ["a", "d", "c", "b"]
assert parallel == ["aa", "dd", "cc", "bb"]
def test_reorder_actual_font():
def _pair_pos(font):
# Initial state should be we have a GPOS with PairPos lookup for ab, ac
pair_pos = only(
reduce(
lambda a, c: a + c.SubTable, font["GPOS"].table.LookupList.Lookup, []
)
)
return tuple(
(
pair_pos.Coverage.glyphs[i],
[
(pvr.SecondGlyph, pvr.Value1.XAdvance)
for pvr in pair_pos.PairSet[i].PairValueRecord
],
)
for i, pair_set in enumerate(pair_pos.PairSet)
)
# tell the boxes to get closer together when ordered ab
# use GPOS, PairPos Format1 which has Coverage and a Value, a parallel sorted List[ValueRecord]
fea = r"""
languagesystem DFLT dflt;
languagesystem latn dflt;
feature kern {
position a b -12;
position b b -14;
position b c -16;
} kern;
"""
with tempfile.TemporaryDirectory() as temp_dir:
fea_file = Path(temp_dir) / "fea.fea"
fea_file.write_text(fea)
# upem 24, input svgs are on a 0 0 24 24 viewBox
svgs = tuple(
test_helper.locate_test_file(f"narrow_rects/{c}.svg") for c in "abc"
)
config, glyph_inputs = test_helper.color_font_config(
{
"upem": 24,
"fea_file": fea_file,
},
svgs,
tmp_dir=Path(temp_dir),
codepoint_fn=lambda svg_file, _: (ord(svg_file.stem),),
)
_, font = write_font._generate_color_font(config, glyph_inputs)
# Initial state
assert _pair_pos(font) == (("a", [("b", -12)]), ("b", [("b", -14), ("c", -16)]))
# reverse the glyph order from a, b, c to c, b, a
new_glyph_order = list(reversed(font.getGlyphOrder()))
reorder_glyphs(font, new_glyph_order)
# Confirm swap applied to Coverage and pair pos parallel array
assert _pair_pos(font) == (("b", [("c", -16), ("b", -14)]), ("a", [("b", -12)]))
def test_invalid_new_glyph_order():
font = ttLib.TTFont()
font.setGlyphOrder([".notdef", "a", "b", "c"])
with pytest.raises(
ValueError, match="New glyph order contains 3 glyphs, but font has 4 glyphs"
):
reorder_glyphs(font, new_glyph_order=[".notdef", "a", "b"])
with pytest.raises(
ValueError,
match="New glyph order does not contain the same set of glyphs as the font:",
):
reorder_glyphs(font, new_glyph_order=[".notdef", "a", "b", "d"])