Skip to content

Commit

Permalink
font-patcher: Issue warning if symbol scaling fails monospace goal
Browse files Browse the repository at this point in the history
[why]
Sometimes fonts patched with --mono are not recognized as monospaced
fonts.

One reason can be that the inserted glyphs are too wide. This will show
in the end in the font's advanceWidthMax property which is not congruent
to the normal font width.

[how]
After all the scaling and jiggling we double check if the new glyph
already in the to-be-patched is not wider than our design goal for the
width. Normally one would expect that this always holds.

An exemption could be if we insert ligatures, that are two spaces wide.
But at the moment we can not anyhow (because there is no way to add
information to the ligature tables right now).

If a glyph is wider a warning is issued.

No warning is issued if the glyph shall have some overlap. That overlap
is taken into account of this check.

Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
  • Loading branch information
Finii committed Jan 1, 2022
1 parent de13c66 commit cfcd15a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions font-patcher
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,15 @@ class font_patcher:
# Currently stretching vertically for both monospace and double-width
scale_ratio_y = self.font_dim['height'] / sym_dim['height']

if 'overlap' in sym_attr['params']:
overlap = sym_attr['params']['overlap']
else:
overlap = 0

if scale_ratio_x != 1 or scale_ratio_y != 1:
if 'overlap' in sym_attr['params']:
scale_ratio_x *= 1 + sym_attr['params']['overlap']
scale_ratio_y *= 1 + sym_attr['params']['overlap']
if overlap != 0:
scale_ratio_x *= 1 + overlap
scale_ratio_y *= 1 + overlap
self.sourceFont[currentSourceFontGlyph].transform(psMat.scale(scale_ratio_x, scale_ratio_y))

# Use the dimensions from the newly pasted and stretched glyph
Expand All @@ -797,8 +802,8 @@ class font_patcher:
# Right align
x_align_distance += self.font_dim['width'] - sym_dim['width']

if 'overlap' in sym_attr['params']:
overlap_width = self.font_dim['width'] * sym_attr['params']['overlap']
if overlap != 0:
overlap_width = self.font_dim['width'] * overlap
if sym_attr['align'] == 'l':
x_align_distance -= overlap_width
if sym_attr['align'] == 'r':
Expand All @@ -817,6 +822,13 @@ class font_patcher:
# does not overlap the bearings (edges)
self.remove_glyph_neg_bearings(self.sourceFont[currentSourceFontGlyph])

# Check if the inserted glyph is scaled correctly for monospace
if self.args.single:
(xmin, _, xmax, _) = self.sourceFont[currentSourceFontGlyph].boundingBox()
if int(xmax - xmin) > self.font_dim['width'] * (1 + overlap):
print("\n Warning: Scaled glyph U+{:X} wider than one monospace width ({} / {} (overlap {}))".format(
currentSourceFontGlyph, int(xmax - xmin), self.font_dim['width'], overlap))

# end for

if self.args.quiet is False or self.args.progressbars:
Expand Down

0 comments on commit cfcd15a

Please sign in to comment.