Skip to content

Commit 3ccffaa

Browse files
committed
be more flexible when reading uni-names from UFO
1 parent 9767549 commit 3ccffaa

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

goadbWriter.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def get_glyph_order(f, include_template_glyphs=False):
238238
return order
239239

240240

241-
def get_uni_name(cp):
241+
def make_uni_name(cp):
242242
'''
243243
convert codepoint to uniXXXX (or uXXXXX) glyph name
244244
'''
@@ -249,12 +249,12 @@ def get_uni_name(cp):
249249
return uni_name
250250

251251

252-
def get_uni_override(cp_list):
252+
def make_uni_override(cp_list):
253253
'''
254254
comma-separated Unicode override string
255255
(or a single string if len(cp_list) == 1)
256256
'''
257-
unicode_override = ','.join([get_uni_name(cp) for cp in cp_list])
257+
unicode_override = ','.join([make_uni_name(cp) for cp in cp_list])
258258
return unicode_override
259259

260260

@@ -359,7 +359,11 @@ def __init__(self, gn_friendly, g=None, gn_final=None, cp_override=None):
359359

360360
def assign_final_and_cp_override(self):
361361
is_agd_name = self.gn_friendly in AGD_DICT.keys()
362-
rx_uni_name = r'(?:u|uni)?([0-9A-F]{4,5}).*' # ?: is non-capturing
362+
# The uni name is something like `uni0020`. In theory, the zero-padding
363+
# could be omitted (`uni20` -- although I have not seen that yet).
364+
# The last Unicode Plane (16) ends at 10FFFF, so allowing code points
365+
# up to FFFFFF should be enough.
366+
rx_uni_name = r'^(?:u|uni)?([0-9A-F]{1,6})$'
363367
uni_name_match = re.match(rx_uni_name, self.gn_friendly)
364368

365369
# glyph name is in AGD
@@ -374,7 +378,7 @@ def assign_final_and_cp_override(self):
374378
# glyph name is in AGD, but multiple code points attached;
375379
# override is needed
376380
self.gn_final = self.gn_friendly
377-
self.cp_override = get_uni_override(self.glyph.unicodes)
381+
self.cp_override = make_uni_override(self.glyph.unicodes)
378382
else:
379383
# just one codepoint
380384
expected_codepoint = agd_cp
@@ -384,34 +388,41 @@ def assign_final_and_cp_override(self):
384388
self.gn_final = agd_final
385389
else:
386390
# codepoint is different from what we expect
387-
self.gn_final = get_uni_name(self.glyph.unicode)
391+
self.gn_final = make_uni_name(self.glyph.unicode)
388392

389393
# glyph name implies Unicode value (uniXXXX or uXXXXX)
390394
elif uni_name_match:
391395
cp_hex = uni_name_match.group(1)
396+
cp_int = int(cp_hex, 16)
397+
392398
if self.glyph.unicodes == []:
393399
# no codepoint assigned to glyph, codepoint will be assigned
394400
# through the glyph name only
395-
self.gn_final = self.gn_friendly
401+
# The glyph name could be uniFFFFF, which is not a legal final
402+
# name, so we are sending it through make_uni_name.
403+
self.gn_final = make_uni_name(cp_int)
396404
elif len(self.glyph.unicodes) > 1:
397405
# glyph name implies one code point, but multiple code points
398406
# are attached -- override needed.
399407
# Overriding a uniXXXX name used to be a makeotf problem, but
400408
# this has been solved here:
401409
# https://github.com/adobe-type-tools/afdko/pull/1615
410+
411+
# The final name does not matter, because it is
412+
# overridden anyway.
402413
self.gn_final = self.gn_friendly
403-
self.cp_override = get_uni_override(self.glyph.unicodes)
414+
self.cp_override = make_uni_override(self.glyph.unicodes)
404415
else:
405416
# just one codepoint
406-
expected_codepoint = int(cp_hex, 16)
407417
actual_codepoint = self.glyph.unicode
408-
if expected_codepoint == actual_codepoint:
409-
# codepoint is the expected one
410-
self.gn_final = self.gn_friendly
418+
if cp_int == actual_codepoint:
419+
# codepoint is the expected one. Name could be uniFFFFF,
420+
# which makeotf only understands as uFFFFF.
421+
self.gn_final = make_uni_name(cp_int)
411422
else:
412423
# codepoint is different from what the name implies
413424
# (weird flex but OK)
414-
self.gn_final = get_uni_name(self.glyph.unicode)
425+
self.gn_final = make_uni_name(self.glyph.unicode)
415426

416427
# custom glyph name
417428
else:
@@ -421,10 +432,10 @@ def assign_final_and_cp_override(self):
421432
elif len(self.glyph.unicodes) > 1:
422433
# multiple code points are attached -- override needed
423434
self.gn_final = self.gn_friendly
424-
self.cp_override = get_uni_override(self.glyph.unicodes)
435+
self.cp_override = make_uni_override(self.glyph.unicodes)
425436
else:
426437
# just one codepoint, the final name will tell makeotf about it
427-
self.gn_final = get_uni_name(self.glyph.unicode)
438+
self.gn_final = make_uni_name(self.glyph.unicode)
428439

429440

430441
def fill_gn_dict(gb, glyph_name_dict):
@@ -506,7 +517,7 @@ def make_glyph_name_dict(f, glyph_order):
506517

507518
if g.unicodes:
508519
# the alt glyph itself may have a codepoint
509-
gb.cp_override = get_uni_override(g.unicodes)
520+
gb.cp_override = make_uni_override(g.unicodes)
510521

511522
glyph_name_dict = fill_gn_dict(gb, glyph_name_dict)
512523

@@ -527,7 +538,7 @@ def make_glyph_name_dict(f, glyph_order):
527538

528539
if g.unicodes:
529540
# some ligatures have codepoints
530-
gb.cp_override = get_uni_override(g.unicodes)
541+
gb.cp_override = make_uni_override(g.unicodes)
531542

532543
glyph_name_dict = fill_gn_dict(gb, glyph_name_dict)
533544

@@ -554,7 +565,7 @@ def main(test_args=None):
554565

555566
if args.output:
556567
with open(args.output, 'w') as blob:
557-
blob.write(goadb)
568+
blob.write(goadb + '\n')
558569
else:
559570
print(goadb)
560571

0 commit comments

Comments
 (0)