From 2ba5dec3630311a9cb5072bd2e2474b0110c4cc0 Mon Sep 17 00:00:00 2001 From: Fini Jastrow Date: Tue, 3 Jan 2023 11:32:38 +0100 Subject: [PATCH] font-patcher: Simplify bounding box scaling [why] The code looks so compliacted while in fact it is not (so much). Rounding sometimes and sometimes not is hard to reaon about. The un-rounded values should in principle be better, but there is some rounding hidden in the font that we can not really simulate, so simulate what we can. [how] Always scale (even if factor is one) and round to integer the BB. [note] Also use 'is not None' ideom. Signed-off-by: Fini Jastrow --- font-patcher | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/font-patcher b/font-patcher index 867c3041a..16dfac1e9 100755 --- a/font-patcher +++ b/font-patcher @@ -1072,11 +1072,7 @@ class font_patcher: sym_dim = get_glyph_dimensions(self.sourceFont[currentSourceFontGlyph]) # Use combined bounding box? if scale_glyph_data is not None and scale_glyph_data[1] is not None: - if scale_ratio_x != 1 or scale_ratio_y != 1: - # Simulate scaling on combined bounding box - scaleglyph_dim = scale_bounding_box(scale_glyph_data[1], scale_ratio_x, scale_ratio_y) - else: - scaleglyph_dim = scale_glyph_data[1] + scaleglyph_dim = scale_bounding_box(scale_glyph_data[1], scale_ratio_x, scale_ratio_y) if scaleglyph_dim['advance'] is None: # On monospaced symbol collections use their advance with, otherwise align horizontally individually scaleglyph_dim['xmin'] = sym_dim['xmin'] @@ -1341,12 +1337,13 @@ def get_glyph_dimensions(glyph): def scale_bounding_box(bbox, scale_x, scale_y): """ Return a scaled version of a glyph dimensions dict """ + # Simulate scaling on combined bounding box, round values for better simulation new_dim = { 'xmin' : int(bbox['xmin'] * scale_x), 'ymin' : int(bbox['ymin'] * scale_y), 'xmax' : int(bbox['xmax'] * scale_x), 'ymax' : int(bbox['ymax'] * scale_y), - 'advance': int(bbox['advance'] * scale_x) if bbox['advance'] else None, + 'advance': int(bbox['advance'] * scale_x) if bbox['advance'] is not None else None, } new_dim['width'] = new_dim['xmax'] + (-new_dim['xmin']) new_dim['height'] = new_dim['ymax'] + (-new_dim['ymin'])