font-patcher: Resolve rounding error when rescaling

[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>
This commit is contained in:
Fini Jastrow 2021-12-22 19:57:51 +01:00
parent d887391ca0
commit e805b87997

View file

@ -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:
@ -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.