font-patcher: Keep overlap in proportional font

[why]
The previous commit is somewhat incomplete in some cases and plain wrong
in others (with proportional fonts). Examples:

The Heard 0x2665 gets a positive right side bearing, which is
unexpected. The commits prevents negative right side bearings but not
positive ones.

The glyphs with overlap (which are the Powerline ones) like 0xE0B0 and
0xE0B2 end up in wrong sizes.

This can especially be seen with the Symbols-Only (non-Mono) font,
because that is (secretly) a 'Nerd Font Propo' (--variable-width-glyphs)
font.

[how]
This is kind of a design choice: As with the other patched font variants
we ignore existing borders (positive bearings) around the glyph. The
previous commit tried to keep them, which seems to be impossible and
is inconsistent). Also negative bearings would be ignored (but there are
none).

The only place where bearings come into play is now when we have
overlap. All non-overlap glyphs render without any bearing.

If we have overlap we need to
a) reduce the width by the overlap
b) introduce a negative bearing on the appropriate side

First we remove any left side bearing by transforming the glyph to the
side, such that the bearing becomes zero.

For left-side overlap we additionally transform the glyph by the overlap
amount to the left (as usual). This creates the neg. left bearing.

For right-side overlap we keep the left bearing to be zero.

After correcting the left-side bearing (by transforming) we set the
corrected width. That is the width subtracted by the overlap.
In the left-aligned case this makes the right-side bearing zero.
In the right-aligned case this results in a negative right-side bearing.

Note how fontforge handles size and bearing changes:
  Fontforge handles the width change like this:
  - Keep existing left_side_bearing
  - Set width
  - Calculate and set new right_side_bearing

Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
This commit is contained in:
Fini Jastrow 2023-01-02 16:17:22 +01:00
parent 946c1d09dd
commit 5b36e8ec16

View file

@ -1086,7 +1086,8 @@ class font_patcher:
# Handle glyph l/r/c alignment
x_align_distance = 0
if self.args.nonmono:
pass
# Remove left side bearing
x_align_distance = -self.sourceFont[currentSourceFontGlyph].left_side_bearing
elif sym_attr['align']:
# First find the baseline x-alignment (left alignment amount)
x_align_distance = self.font_dim['xmin'] - sym_dim['xmin']
@ -1101,7 +1102,8 @@ class font_patcher:
overlap_width = self.font_dim['width'] * overlap
if sym_attr['align'] == 'l':
x_align_distance -= overlap_width
if sym_attr['align'] == 'r':
if sym_attr['align'] == 'r' and not self.args.nonmono:
# Nonmono is 'left aligned' per definition, translation does not help here
x_align_distance += overlap_width
align_matrix = psMat.translate(x_align_distance, y_align_distance)
@ -1117,15 +1119,20 @@ class font_patcher:
# same width for all character glyphs. This needs to be done for all glyphs,
# even the ones that are empty and didn't go through the scaling operations.
# It should come after setting the glyph bearings
self.set_glyph_width_mono(self.sourceFont[currentSourceFontGlyph])
# Target font with variable advance width get the icons with their native advance
# or at least width
if self.args.nonmono:
if sym_dim['advance']:
self.sourceFont[currentSourceFontGlyph].width = int(sym_dim['advance'])
else:
self.sourceFont[currentSourceFontGlyph].width = int(sym_dim['width'])
if not self.args.nonmono:
self.set_glyph_width_mono(self.sourceFont[currentSourceFontGlyph])
else:
# Target font with variable advance width get the icons with their native widths
# and keeping possible (negative) bearings in effect
width = sym_dim['width']
# If we have overlap we need to subtract that to keep/get negative bearings
if overlap and (sym_attr['align'] == 'l' or sym_attr['align'] == 'r'):
width -= overlap_width
# Fontforge handles the width change like this:
# - Keep existing left_side_bearing
# - Set width
# - Calculate and set new right_side_bearing
self.sourceFont[currentSourceFontGlyph].width = int(width)
# Check if the inserted glyph is scaled correctly for monospace
if self.args.single: