Skip to content

Commit 7999191

Browse files
committed
make ufoLib2 not required, make instantiator work with defcon as well
Remove the unconditional ufoLib2 import and make instantiator work with name record dicts like defcon has Fixes #956
1 parent b73b77d commit 7999191

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

Lib/ufo2ft/instantiator.py

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
from collections.abc import Iterable, KeysView
6767

6868
from ufoLib2.objects import Font, Glyph, Info
69-
from ufoLib2.objects.info import NameRecord as ufoNameRecord
69+
70+
# Type alias for name records - can be dict (defcon) or NameRecord object (ufoLib2)
71+
NameRecordDict = Dict[str, Any]
7072

7173
logger = logging.getLogger(__name__)
7274

@@ -204,50 +206,53 @@ def process_rules_swaps(rules, location, glyphNames):
204206

205207

206208
def _override_name_record(
207-
openTypeNameRecords: list[ufoNameRecord],
208-
new_value: ufoNameRecord,
209-
) -> None:
210-
"""Override an existing name record in the list of OpenType name records."""
209+
openTypeNameRecords: list,
210+
new_value: NameRecordDict,
211+
) -> bool:
212+
"""Override an existing name record in the list of OpenType name records.
213+
214+
Records can be dicts (defcon) or NameRecord objects (ufoLib2). Both support
215+
dict-style read access via []. For writing, dicts use [] while NameRecord
216+
uses setattr since it's read-only for the Mapping interface.
217+
"""
211218
for record in openTypeNameRecords:
212219
if (
213-
record.platformID == new_value.platformID
214-
and record.encodingID == new_value.encodingID
215-
and record.languageID == new_value.languageID
216-
and record.nameID == new_value.nameID
220+
record["platformID"] == new_value["platformID"]
221+
and record["encodingID"] == new_value["encodingID"]
222+
and record["languageID"] == new_value["languageID"]
223+
and record["nameID"] == new_value["nameID"]
217224
):
218225
# Override existing name record
219-
record.string = new_value.string
226+
if isinstance(record, dict):
227+
record["string"] = new_value["string"]
228+
else:
229+
record.string = new_value["string"]
220230
return True
221231
return False
222232

223233

224-
def _make_ufo_name_record(
234+
def _make_name_record_dict(
225235
name_id: int,
226236
value: str,
227237
language_tag: str = "en",
228-
) -> ufoNameRecord:
229-
"""Add a name to the list of OpenType name records, or override an existing one."""
230-
from ufoLib2.objects.info import NameRecord
231-
238+
) -> NameRecordDict:
239+
"""Create a name record dict from name ID, value, and language tag."""
232240
temp_name_record: ftNameRecord = _makeWindowsName(value, name_id, language_tag)
233-
new_name_record: ufoNameRecord = NameRecord(
234-
platformID=temp_name_record.platformID,
235-
encodingID=temp_name_record.platEncID,
236-
languageID=temp_name_record.langID,
237-
nameID=temp_name_record.nameID,
238-
string=value.strip(),
239-
)
240-
return new_name_record
241+
return {
242+
"platformID": temp_name_record.platformID,
243+
"encodingID": temp_name_record.platEncID,
244+
"languageID": temp_name_record.langID,
245+
"nameID": temp_name_record.nameID,
246+
"string": value.strip(),
247+
}
241248

242249

243250
def merge_public_font_info(
244251
font: Font,
245252
override_public_font_info: Dict[str, Any],
246253
instance_location: Location,
247254
) -> None:
248-
"""Merge the public.fontInfo dict into the ufoLib2.Font's info object (fontinfo.plist)."""
249-
from ufoLib2.objects.info import NameRecord
250-
255+
"""Merge the public.fontInfo dict into the font's info object (fontinfo.plist)."""
251256
for key, value in override_public_font_info.items():
252257
try:
253258
if key == "openTypeNameRecords":
@@ -264,17 +269,17 @@ def merge_public_font_info(
264269

265270
# merge the existing openTypeNameRecords with the new ones
266271
for dict_name_record in value:
267-
ufo_name_record = NameRecord(
268-
platformID=dict_name_record["platformID"],
269-
encodingID=dict_name_record["encodingID"],
270-
languageID=dict_name_record["languageID"],
271-
nameID=dict_name_record["nameID"],
272-
string=dict_name_record["string"].strip(),
273-
)
272+
name_record = {
273+
"platformID": dict_name_record["platformID"],
274+
"encodingID": dict_name_record["encodingID"],
275+
"languageID": dict_name_record["languageID"],
276+
"nameID": dict_name_record["nameID"],
277+
"string": dict_name_record["string"].strip(),
278+
}
274279
if not _override_name_record(
275-
font.info.openTypeNameRecords, ufo_name_record
280+
font.info.openTypeNameRecords, name_record
276281
):
277-
font.info.openTypeNameRecords.append(ufo_name_record)
282+
font.info.openTypeNameRecords.append(name_record)
278283
else:
279284
setattr(font.info, key, copy.deepcopy(value))
280285
except AttributeError:
@@ -804,7 +809,7 @@ def _generate_instance_info(
804809
if language_tag in instance.localisedFamilyName:
805810
# assume is Preferred Family Name (ID=16)
806811
mulitlingual_opentype_font_records.append(
807-
_make_ufo_name_record(
812+
_make_name_record_dict(
808813
OT_TYPOGRAPHIC_FAMILY_NAME_ID,
809814
instance.localisedFamilyName[language_tag],
810815
language_tag,
@@ -813,7 +818,7 @@ def _generate_instance_info(
813818
if language_tag in instance.localisedStyleName:
814819
# assume is Preferred Subfamily Name (ID=17)
815820
mulitlingual_opentype_font_records.append(
816-
_make_ufo_name_record(
821+
_make_name_record_dict(
817822
OT_TYPOGRAPHIC_SUBFAMILY_NAME_ID,
818823
instance.localisedStyleName[language_tag],
819824
language_tag,
@@ -822,7 +827,7 @@ def _generate_instance_info(
822827
if language_tag in instance.localisedStyleMapFamilyName:
823828
# Style Map Family Name (ID=1)
824829
mulitlingual_opentype_font_records.append(
825-
_make_ufo_name_record(
830+
_make_name_record_dict(
826831
OT_STYLE_MAP_FAMILY_NAME_ID,
827832
instance.localisedStyleMapFamilyName[language_tag],
828833
language_tag,
@@ -845,7 +850,7 @@ def _generate_instance_info(
845850
# add style name to ID=1
846851
full_family_name += " " + font.info.styleName
847852
mulitlingual_opentype_font_records.append(
848-
_make_ufo_name_record(
853+
_make_name_record_dict(
849854
OT_STYLE_MAP_FAMILY_NAME_ID,
850855
full_family_name,
851856
language_tag,
@@ -854,7 +859,7 @@ def _generate_instance_info(
854859
if language_tag in instance.localisedStyleMapStyleName:
855860
# Style Map Subfamily Name (ID=2)
856861
mulitlingual_opentype_font_records.append(
857-
_make_ufo_name_record(
862+
_make_name_record_dict(
858863
OT_STYLE_MAP_STYLE_NAME_ID,
859864
instance.localisedStyleMapStyleName[language_tag],
860865
language_tag,

0 commit comments

Comments
 (0)