Skip to content

Commit 7c3b85a

Browse files
authored
Merge pull request #38 from moyogo/extract-uvs
Extract Unicode Variation Sequences
2 parents 86145b7 + 71d1f6a commit 7c3b85a

File tree

7 files changed

+80
-9
lines changed

7 files changed

+80
-9
lines changed

Lib/extractor/formats/opentype.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def extractFontFromOpenType(
4141
extractOpenTypeInfo(source, destination)
4242
if doGlyphs:
4343
extractOpenTypeGlyphs(source, destination)
44+
extractUnicodeVariationSequences(source, destination)
4445
if doGlyphOrder:
4546
extractGlyphOrder(source, destination)
4647
if doKerning:
@@ -61,6 +62,33 @@ def extractGlyphOrder(source, destination):
6162
destination.lib["public.glyphOrder"] = glyphOrder
6263

6364

65+
# ---------------------------
66+
# Unicode Variation Sequences
67+
# ---------------------------
68+
69+
70+
def extractUnicodeVariationSequences(source, destination):
71+
"""
72+
Extract the Unicode Variation Sequences
73+
"""
74+
cmap = source.get("cmap")
75+
mapping = cmap.getBestCmap()
76+
for subtable in cmap.tables:
77+
if subtable.format == 14:
78+
destination.lib["public.unicodeVariationSequences"] = {
79+
"%04X" % variationSelector: {
80+
"%04X" % charValue: glyphName if glyphName else mapping.get(charValue)
81+
for (charValue, glyphName) in uvsList
82+
}
83+
for variationSelector, uvsList in subtable.uvsDict.items()
84+
}
85+
86+
87+
# ------------
88+
# Instructions
89+
# ------------
90+
91+
6492
def extractInstructions(source, destination):
6593
if "glyf" not in source:
6694
return
@@ -500,7 +528,6 @@ def extractOpenTypeGlyphs(source, destination):
500528
# grab the cmap
501529
vmtx = source.get("vmtx")
502530
vorg = source.get("VORG")
503-
cmap = source.getBestCmap()
504531
is_ttf = "glyf" in source
505532
reversedMapping = source.get("cmap").buildReversed()
506533
# grab the glyphs

dev-requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest
2+
ufo2ft

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
fonttools
1+
fonttools
2+
defcon
3+
ufoLib2

tests/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
4+
@pytest.fixture(scope="session", params=["defcon", "ufoLib2"])
5+
def ufo_module(request):
6+
return pytest.importorskip(request.param)
7+
8+
9+
@pytest.fixture(scope="session")
10+
def FontClass(ufo_module):
11+
if hasattr(ufo_module.Font, "open"):
12+
13+
def ctor(path=None):
14+
if path is None:
15+
return ufo_module.Font()
16+
else:
17+
return ufo_module.Font.open(path)
18+
19+
return ctor
20+
return ufo_module.Font

tests/data/UVSTest.ttf

1.18 KB
Binary file not shown.

tests/extractor_test.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
import extractor
2-
import unittest
2+
import os
3+
import pytest
34

45

5-
class ExtractUfoTest(unittest.TestCase):
6+
def getpath(filename):
7+
dirname = os.path.dirname(__file__)
8+
return os.path.join(dirname, "data", filename)
69

7-
# TODO: replace this with real test cases
8-
def test_extractUFO_dummy_test(self):
9-
self.assertTrue(True)
10+
11+
class ExtractUfoTest:
12+
13+
def test_extract_cmap_with_UVS(self, FontClass):
14+
ufo = FontClass()
15+
extractor.extractUFO(getpath("UVSTest.ttf"), ufo)
16+
17+
assert {glyph.name: set(glyph.unicodes) for glyph in ufo if glyph.unicodes} == {
18+
"zero": {0x030},
19+
"Anegativesquared": {0x1F170},
20+
}
21+
22+
assert ufo.lib.get("public.unicodeVariationSequences") == {
23+
"FE00": {"0030": "zero.slash"},
24+
"FE0E": {"1F170": "Anegativesquared.text"},
25+
"FE0F": {"1F170": "Anegativesquared"},
26+
}
1027

1128

1229
if __name__ == "__main__":
13-
unittest.main()
30+
import sys
31+
32+
sys.exit(pytest.main(sys.argv))

tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ envlist = py36, py38
44
[testenv]
55
deps =
66
pytest
7-
-rrequirements.txt
7+
-r requirements.txt
8+
-r dev-requirements.txt
89
commands =
910
pytest {posargs}

0 commit comments

Comments
 (0)