Skip to content

Commit

Permalink
font-patcher: Simplify output code
Browse files Browse the repository at this point in the history
[why]
The code around `currentSourceFontGlyph` and `copiedToSlot` is
needlessly complex and checks for conditions that are impossible to
occur (possibly because the algorithm was different in the past).

It becomes rather hard to follow which variable holds what kind of value
and when.

[how]
Drop all the string-that-contain-hex-numbers stuff and use a regular
integer list/variable.

Format the string when it is output.

Drop obsolete checks.

Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
  • Loading branch information
Finii committed Dec 22, 2021
1 parent 7e92302 commit c728079
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions font-patcher
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,8 @@ class font_patcher:
careful = True

if exactEncoding is False:
sourceFontList = []
sourceFontList = list(range(sourceFontStart, sourceFontEnd + 1))
sourceFontCounter = 0
for i in range(sourceFontStart, sourceFontEnd + 1):
sourceFontList.append(format(i, 'X'))

scale_factor = 0
if scaleGlyph:
Expand Down Expand Up @@ -685,46 +683,34 @@ class font_patcher:
if exactEncoding:
# use the exact same hex values for the source font as for the symbol font
currentSourceFontGlyph = sym_glyph.encoding

# Save as a hex string without the '0x' prefix
copiedToSlot = format(sym_glyph.unicode, 'X')
else:
# use source font defined hex values based on passed in start and end
# convince that this string really is a hex:
currentSourceFontGlyph = int("0x" + sourceFontList[sourceFontCounter], 16)
copiedToSlot = sourceFontList[sourceFontCounter]
currentSourceFontGlyph = sourceFontList[sourceFontCounter]
sourceFontCounter += 1

if int(copiedToSlot, 16) < 0:
print("Found invalid glyph slot number. Skipping.")
continue

if self.args.quiet is False:
if self.args.progressbars:
update_progress(round(float(index + 1) / glyphSetLength, 2))
else:
progressText = "\nUpdating glyph: " + str(sym_glyph) + " " + str(sym_glyph.glyphname) + " putting at: " + copiedToSlot
progressText = "\nUpdating glyph: {} {} putting at: {:X}".format(sym_glyph, sym_glyph.glyphname, currentSourceFontGlyph)
sys.stdout.write(progressText)
sys.stdout.flush()

# Prepare symbol glyph dimensions
sym_dim = get_glyph_dimensions(sym_glyph)

# check if a glyph already exists in this location
if copiedToSlot.startswith("uni"):
copiedToSlot = copiedToSlot[3:]
codepoint = int("0x" + copiedToSlot, 16)
if careful or 'careful' in sym_attr['params']:
if codepoint in self.sourceFont:
if currentSourceFontGlyph in self.sourceFont:
if self.args.quiet is False:
print(" Found existing Glyph at {}. Skipping...".format(copiedToSlot))
print(" Found existing Glyph at {:X}. Skipping...".format(currentSourceFontGlyph))
# We don't want to touch anything so move to next Glyph
continue
else:
# If we overwrite an existing glyph all subtable entries regarding it will be wrong
# (Probably; at least if we add a symbol and do not substitude a ligature or such)
if codepoint in self.sourceFont:
self.sourceFont[codepoint].removePosSub("*")
if currentSourceFontGlyph in self.sourceFont:
self.sourceFont[currentSourceFontGlyph].removePosSub("*")

# Select and copy symbol from its encoding point
# We need to do this select after the careful check, this way we don't
Expand Down

0 comments on commit c728079

Please sign in to comment.