From 2c89d50fb8b71a7d0f6d2365c5408325b7ad2a86 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 7 Jun 2023 19:52:04 +0530 Subject: [PATCH] PDF Output: Fix error when input document contains multiple instances of a font some with vertical metric and some without. Fixes #2023041 [Private bug](https://bugs.launchpad.net/calibre/+bug/2023041) --- src/calibre/utils/fonts/sfnt/merge.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/calibre/utils/fonts/sfnt/merge.py b/src/calibre/utils/fonts/sfnt/merge.py index 8c24d6b5e9..72c21ab0d8 100644 --- a/src/calibre/utils/fonts/sfnt/merge.py +++ b/src/calibre/utils/fonts/sfnt/merge.py @@ -50,11 +50,17 @@ def merge_truetype_fonts_for_pdf(fonts, log=None): log(f'Size mismatch for glyph id: {glyph_id} prev_sz: {len(prev_glyph_data)} sz: {sz}') if hhea is not None: m = hhea.metrics_for(glyph_id) - if m != hmetrics_map[glyph_id]: + old_val = hmetrics_map.get(glyph_id) + if old_val is None: + hmetrics_map[glyph_id] = m + elif m != old_val: log(f'Metrics mismatch for glyph id: {glyph_id} prev: {hmetrics_map[glyph_id]} cur: {m}') if vhea is not None: m = vhea.metrics_for(glyph_id) - if m != vmetrics_map[glyph_id]: + old_val = vmetrics_map.get(glyph_id) + if old_val is None: + vmetrics_map[glyph_id] = m + elif m != vmetrics_map[glyph_id]: log(f'Metrics mismatch for glyph id: {glyph_id} prev: {vmetrics_map[glyph_id]} cur: {m}') glyf = ans[b'glyf'] @@ -71,9 +77,9 @@ def merge_truetype_fonts_for_pdf(fonts, log=None): head.update() maxp.num_glyphs = len(loca.offset_map) - 1 maxp.update() - if hmetrics_map: + if hmetrics_map and b'hhea' in ans: ans[b'hhea'].update(hmetrics_map, ans[b'hmtx']) - if vmetrics_map: + if vmetrics_map and b'vhea' in ans: ans[b'vhea'].update(vmetrics_map, ans[b'vmtx']) for name in 'hdmx GPOS GSUB'.split():