Skip to content

Commit

Permalink
Merge pull request #495 from moyogo/uvs-lib-key
Browse files Browse the repository at this point in the history
UVS lib key for Unicode Variation Sequences
  • Loading branch information
moyogo authored Apr 13, 2021
2 parents a5267d1 + 3ce39df commit 9315872
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Lib/ufo2ft/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
COLOR_LAYER_MAPPING_KEY = UFO2FT_PREFIX + "colorLayerMapping"

OPENTYPE_CATEGORIES_KEY = "public.openTypeCategories"

UNICODE_VARIATION_SEQUENCES_KEY = "public.unicodeVariationSequences"
26 changes: 25 additions & 1 deletion Lib/ufo2ft/outlineCompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
from fontTools.ttLib.tables._h_e_a_d import mac_epoch_diff
from fontTools.ttLib.tables.O_S_2f_2 import Panose

from ufo2ft.constants import COLOR_LAYERS_KEY, COLOR_PALETTES_KEY
from ufo2ft.constants import (
COLOR_LAYERS_KEY,
COLOR_PALETTES_KEY,
UNICODE_VARIATION_SEQUENCES_KEY,
)
from ufo2ft.errors import InvalidFontData
from ufo2ft.fontInfoData import (
dateStringForNow,
Expand Down Expand Up @@ -504,6 +508,26 @@ def setupTable_cmap(self):
cmap12_3_10.cmap = nonBMP
# update tables registry
cmap.tables = [cmap4_0_3, cmap4_3_1, cmap12_0_4, cmap12_3_10]
# unicode variation sequences
uvsMapping = self.ufo.lib.get(UNICODE_VARIATION_SEQUENCES_KEY)
if uvsMapping:
from fontTools.ttLib.tables._c_m_a_p import cmap_format_14

cmap14_0_5 = cmap_format_14(14)
cmap14_0_5.platformID = 0
cmap14_0_5.platEncID = 5
cmap14_0_5.language = 0
if nonBMP:
mapping = nonBMP
cmap14_0_5.uvsDict = {
uvs: {
uv: None if glyphName == mapping[uv] else glyphName
for (uv, glyphName) in glyphMapping.items()
}
for (uvs, glyphMapping) in uvsMapping.items()
}
# update tables registry
cmap.tables.append(cmap14_0_5)

def setupTable_OS2(self):
"""
Expand Down
74 changes: 74 additions & 0 deletions tests/outlineCompiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,80 @@ def test_colr_cpal_raw(self, FontClass):
assert layers == {"a": [("a.color1", 0), ("a.color2", 1)]}


class CmapTest:
def test_cmap_BMP(self, testufo):
compiler = OutlineOTFCompiler(testufo)
otf = compiler.otf = TTFont(sfntVersion="OTTO")

compiler.setupTable_cmap()

assert "cmap" in otf
cmap = otf["cmap"]
assert len(cmap.tables) == 2
cmap4_0_3 = cmap.tables[0]
cmap4_3_1 = cmap.tables[1]

assert (cmap4_0_3.platformID, cmap4_0_3.platEncID) == (0, 3)
assert (cmap4_3_1.platformID, cmap4_3_1.platEncID) == (3, 1)
assert cmap4_0_3.language == cmap4_3_1.language
assert cmap4_0_3.language == 0
mapping = {c: chr(c) for c in range(0x61, 0x6D)}
mapping[0x20] = "space"
assert cmap4_0_3.cmap == cmap4_3_1.cmap
assert cmap4_0_3.cmap == mapping

def test_cmap_nonBMP_with_UVS(self, testufo):
u1F170 = testufo.newGlyph("u1F170")
u1F170.unicode = 0x1F170
testufo.newGlyph("u1F170.text")
testufo.lib["public.unicodeVariationSequences"] = {
0xFE0E: {
0x1F170: "u1F170.text",
},
0xFE0F: {
0x1F170: "u1F170",
},
}

compiler = OutlineOTFCompiler(testufo)
otf = compiler.otf = TTFont(sfntVersion="OTTO")

compiler.setupTable_cmap()

assert "cmap" in otf
cmap = otf["cmap"]
assert len(cmap.tables) == 5
cmap4_0_3 = cmap.tables[0]
cmap4_3_1 = cmap.tables[1]
cmap12_0_4 = cmap.tables[2]
cmap12_3_10 = cmap.tables[3]
cmap14_0_5 = cmap.tables[4]

assert (cmap4_0_3.platformID, cmap4_0_3.platEncID) == (0, 3)
assert (cmap4_3_1.platformID, cmap4_3_1.platEncID) == (3, 1)
assert cmap4_0_3.language == cmap4_3_1.language
assert cmap4_0_3.language == 0
mapping = {c: chr(c) for c in range(0x61, 0x6D)}
mapping[0x20] = "space"
assert cmap4_0_3.cmap == cmap4_3_1.cmap
assert cmap4_0_3.cmap == mapping

assert (cmap12_0_4.platformID, cmap12_0_4.platEncID) == (0, 4)
assert (cmap12_3_10.platformID, cmap12_3_10.platEncID) == (3, 10)
assert cmap12_0_4.language == cmap12_3_10.language
assert cmap12_0_4.language == 0
mapping[0x1F170] = "u1F170"
assert cmap12_0_4.cmap == cmap12_3_10.cmap
assert cmap12_0_4.cmap == mapping

assert (cmap14_0_5.platformID, cmap14_0_5.platEncID) == (0, 5)
assert cmap14_0_5.language == 0
assert cmap14_0_5.uvsDict == {
0xFE0E: {0x1F170: "u1F170.text"},
0xFE0F: {0x1F170: None},
}


ASCII = [chr(c) for c in range(0x20, 0x7E)]


Expand Down

0 comments on commit 9315872

Please sign in to comment.