Skip to content

Commit

Permalink
font-patcher: Resolve rounding error when rescaling
Browse files Browse the repository at this point in the history
[why]
The symbol glyphs are rescaled (when --mono is specified) so that they
have a predefined width after insertion in the source font (to be
patched font, called 'target font' below).

Sometimes the width of the glyph after insertion is off a bit, like
0.2% or so. This seems strange, as we calculate the target width
exactly.

[how]
As expected this are rounding errors. In the old code we take the
original width of the glyph when it is in the symbol font and
rescale it when it is in the target font. The width of the glyph
should be the same in the source and the target font, right?

It fact it is not, because the coordinate systems of the two fonts can
(and usually are) different. fontforge's magic scales the glyph
into the new coordinate system on insertion, such that it is approx
the same as before. But when the coordinate system is integer based
we get some small rounding errors just from copy and paste.

To solve this, we
- first copy the glyph from the source into the target font
- then determine the glyphs width
- then rescale the glyph to the target width

Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
  • Loading branch information
Finii committed Jan 1, 2022
1 parent 5e3beaf commit 363c7d4
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions font-patcher
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,6 @@ class font_patcher:
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 careful or 'careful' in sym_attr['params']:
if currentSourceFontGlyph in self.sourceFont:
Expand All @@ -761,6 +758,9 @@ class font_patcher:
scale_ratio_x = 1
scale_ratio_y = 1

# Prepare symbol glyph dimensions
sym_dim = get_glyph_dimensions(self.sourceFont[currentSourceFontGlyph])

# Now that we have copy/pasted the glyph, if we are creating a monospace
# font we need to scale and move the glyphs. It is possible to have
# empty glyphs, so we need to skip those.
Expand Down

0 comments on commit 363c7d4

Please sign in to comment.