ufoLib groupnames conversion 2 → 3 (#1762)
* If a group name is the same as a glyph name, it cannot be a kerning group name * Add test
This commit is contained in:
parent
9d3b14dd5f
commit
6c3edaded2
@ -339,7 +339,8 @@ class UFOReader(_UFOBaseIO):
|
|||||||
# convert kerning and groups
|
# convert kerning and groups
|
||||||
kerning, groups, conversionMaps = convertUFO1OrUFO2KerningToUFO3Kerning(
|
kerning, groups, conversionMaps = convertUFO1OrUFO2KerningToUFO3Kerning(
|
||||||
self._upConvertedKerningData["originalKerning"],
|
self._upConvertedKerningData["originalKerning"],
|
||||||
deepcopy(self._upConvertedKerningData["originalGroups"])
|
deepcopy(self._upConvertedKerningData["originalGroups"]),
|
||||||
|
self.getGlyphSet()
|
||||||
)
|
)
|
||||||
# store
|
# store
|
||||||
self._upConvertedKerningData["kerning"] = kerning
|
self._upConvertedKerningData["kerning"] = kerning
|
||||||
@ -637,7 +638,7 @@ class UFOReader(_UFOBaseIO):
|
|||||||
|
|
||||||
``validateRead`` will validate the read data, by default it is set to the
|
``validateRead`` will validate the read data, by default it is set to the
|
||||||
class's validate value, can be overridden.
|
class's validate value, can be overridden.
|
||||||
``validateWrte`` will validate the written data, by default it is set to the
|
``validateWrite`` will validate the written data, by default it is set to the
|
||||||
class's validate value, can be overridden.
|
class's validate value, can be overridden.
|
||||||
"""
|
"""
|
||||||
from fontTools.ufoLib.glifLib import GlyphSet
|
from fontTools.ufoLib.glifLib import GlyphSet
|
||||||
|
@ -6,16 +6,16 @@ Conversion functions.
|
|||||||
|
|
||||||
# adapted from the UFO spec
|
# adapted from the UFO spec
|
||||||
|
|
||||||
def convertUFO1OrUFO2KerningToUFO3Kerning(kerning, groups):
|
def convertUFO1OrUFO2KerningToUFO3Kerning(kerning, groups, glyphSet=()):
|
||||||
# gather known kerning groups based on the prefixes
|
# gather known kerning groups based on the prefixes
|
||||||
firstReferencedGroups, secondReferencedGroups = findKnownKerningGroups(groups)
|
firstReferencedGroups, secondReferencedGroups = findKnownKerningGroups(groups)
|
||||||
# Make lists of groups referenced in kerning pairs.
|
# Make lists of groups referenced in kerning pairs.
|
||||||
for first, seconds in list(kerning.items()):
|
for first, seconds in list(kerning.items()):
|
||||||
if first in groups:
|
if first in groups and first not in glyphSet:
|
||||||
if not first.startswith("public.kern1."):
|
if not first.startswith("public.kern1."):
|
||||||
firstReferencedGroups.add(first)
|
firstReferencedGroups.add(first)
|
||||||
for second in list(seconds.keys()):
|
for second in list(seconds.keys()):
|
||||||
if second in groups:
|
if second in groups and second not in glyphSet:
|
||||||
if not second.startswith("public.kern2."):
|
if not second.startswith("public.kern2."):
|
||||||
secondReferencedGroups.add(second)
|
secondReferencedGroups.add(second)
|
||||||
# Create new names for these groups.
|
# Create new names for these groups.
|
||||||
@ -154,7 +154,7 @@ def test():
|
|||||||
... "DGroup" : ["D"],
|
... "DGroup" : ["D"],
|
||||||
... }
|
... }
|
||||||
>>> kerning, groups, maps = convertUFO1OrUFO2KerningToUFO3Kerning(
|
>>> kerning, groups, maps = convertUFO1OrUFO2KerningToUFO3Kerning(
|
||||||
... testKerning, testGroups)
|
... testKerning, testGroups, [])
|
||||||
>>> expected = {
|
>>> expected = {
|
||||||
... "A" : {
|
... "A" : {
|
||||||
... "A": 1,
|
... "A": 1,
|
||||||
@ -220,7 +220,7 @@ def test():
|
|||||||
... "@MMK_R_XGroup" : ["X"],
|
... "@MMK_R_XGroup" : ["X"],
|
||||||
... }
|
... }
|
||||||
>>> kerning, groups, maps = convertUFO1OrUFO2KerningToUFO3Kerning(
|
>>> kerning, groups, maps = convertUFO1OrUFO2KerningToUFO3Kerning(
|
||||||
... testKerning, testGroups)
|
... testKerning, testGroups, [])
|
||||||
>>> expected = {
|
>>> expected = {
|
||||||
... "A" : {
|
... "A" : {
|
||||||
... "A": 1,
|
... "A": 1,
|
||||||
@ -293,7 +293,7 @@ def test():
|
|||||||
... "DGroup" : ["D"],
|
... "DGroup" : ["D"],
|
||||||
... }
|
... }
|
||||||
>>> kerning, groups, maps = convertUFO1OrUFO2KerningToUFO3Kerning(
|
>>> kerning, groups, maps = convertUFO1OrUFO2KerningToUFO3Kerning(
|
||||||
... testKerning, testGroups)
|
... testKerning, testGroups, [])
|
||||||
>>> expected = {
|
>>> expected = {
|
||||||
... "A" : {
|
... "A" : {
|
||||||
... "A": 1,
|
... "A": 1,
|
||||||
|
@ -883,7 +883,7 @@ def groupsValidator(value):
|
|||||||
return False, "A group has an empty name."
|
return False, "A group has an empty name."
|
||||||
if groupName.startswith("public."):
|
if groupName.startswith("public."):
|
||||||
if not groupName.startswith("public.kern1.") and not groupName.startswith("public.kern2."):
|
if not groupName.startswith("public.kern1.") and not groupName.startswith("public.kern2."):
|
||||||
# unknown pubic.* name. silently skip.
|
# unknown public.* name. silently skip.
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
if len("public.kernN.") == len(groupName):
|
if len("public.kernN.") == len(groupName):
|
||||||
|
@ -137,7 +137,9 @@ class KerningUpConversionTestCase(unittest.TestCase):
|
|||||||
("A", "public.kern2.CGroup"): 3,
|
("A", "public.kern2.CGroup"): 3,
|
||||||
("A", "public.kern2.DGroup"): 4,
|
("A", "public.kern2.DGroup"): 4,
|
||||||
("A", "A"): 1,
|
("A", "A"): 1,
|
||||||
("A", "B"): 2
|
("A", "B"): 2,
|
||||||
|
("X", "A"): 13,
|
||||||
|
("X", "public.kern2.CGroup"): 14
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedGroups = {
|
expectedGroups = {
|
||||||
@ -148,7 +150,8 @@ class KerningUpConversionTestCase(unittest.TestCase):
|
|||||||
"public.kern1.CGroup": ["C", "Ccedilla"],
|
"public.kern1.CGroup": ["C", "Ccedilla"],
|
||||||
"public.kern2.CGroup": ["C", "Ccedilla"],
|
"public.kern2.CGroup": ["C", "Ccedilla"],
|
||||||
"public.kern2.DGroup": ["D"],
|
"public.kern2.DGroup": ["D"],
|
||||||
"Not A Kerning Group" : ["A"]
|
"Not A Kerning Group" : ["A"],
|
||||||
|
"X": ["X", "X.sc"]
|
||||||
}
|
}
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -163,6 +166,20 @@ class KerningUpConversionTestCase(unittest.TestCase):
|
|||||||
self.clearUFO()
|
self.clearUFO()
|
||||||
if not os.path.exists(self.ufoPath):
|
if not os.path.exists(self.ufoPath):
|
||||||
os.mkdir(self.ufoPath)
|
os.mkdir(self.ufoPath)
|
||||||
|
|
||||||
|
# glyphs
|
||||||
|
glyphsPath = os.path.join(self.ufoPath, "glyphs")
|
||||||
|
if not os.path.exists(glyphsPath):
|
||||||
|
os.mkdir(glyphsPath)
|
||||||
|
glyphFile = "X_.glif"
|
||||||
|
glyphsContents = dict(X=glyphFile)
|
||||||
|
path = os.path.join(glyphsPath, "contents.plist")
|
||||||
|
with open(path, "wb") as f:
|
||||||
|
plistlib.dump(glyphsContents, f)
|
||||||
|
path = os.path.join(glyphsPath, glyphFile)
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
|
||||||
|
|
||||||
# metainfo.plist
|
# metainfo.plist
|
||||||
metaInfo = dict(creator="test", formatVersion=formatVersion)
|
metaInfo = dict(creator="test", formatVersion=formatVersion)
|
||||||
path = os.path.join(self.ufoPath, "metainfo.plist")
|
path = os.path.join(self.ufoPath, "metainfo.plist")
|
||||||
@ -187,6 +204,10 @@ class KerningUpConversionTestCase(unittest.TestCase):
|
|||||||
"B" : 10,
|
"B" : 10,
|
||||||
"CGroup" : 11,
|
"CGroup" : 11,
|
||||||
"DGroup" : 12
|
"DGroup" : 12
|
||||||
|
},
|
||||||
|
"X": {
|
||||||
|
"A" : 13,
|
||||||
|
"CGroup" : 14
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
path = os.path.join(self.ufoPath, "kerning.plist")
|
path = os.path.join(self.ufoPath, "kerning.plist")
|
||||||
@ -197,7 +218,8 @@ class KerningUpConversionTestCase(unittest.TestCase):
|
|||||||
"BGroup" : ["B"],
|
"BGroup" : ["B"],
|
||||||
"CGroup" : ["C", "Ccedilla"],
|
"CGroup" : ["C", "Ccedilla"],
|
||||||
"DGroup" : ["D"],
|
"DGroup" : ["D"],
|
||||||
"Not A Kerning Group" : ["A"]
|
"Not A Kerning Group" : ["A"],
|
||||||
|
"X" : ["X", "X.sc"] # a group with a name that is also a glyph name
|
||||||
}
|
}
|
||||||
path = os.path.join(self.ufoPath, "groups.plist")
|
path = os.path.join(self.ufoPath, "groups.plist")
|
||||||
with open(path, "wb") as f:
|
with open(path, "wb") as f:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user