Dont bother merging identical TrueType fonts, simply pick one and discard the rest

This commit is contained in:
Kovid Goyal 2019-07-28 14:32:11 +05:30
parent bf602f48ed
commit 7b6dc6eed9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -774,6 +774,14 @@ def merge_cmaps(cmaps):
return ans.serialize() return ans.serialize()
def fonts_are_identical(fonts):
for key in ('ToUnicode', 'Data'):
all_values = {f[key] for f in fonts}
if len(all_values) > 1:
return False
return True
def merge_font(fonts): def merge_font(fonts):
# choose the largest font as the base font # choose the largest font as the base font
fonts.sort(key=lambda f: len(f['Data'] or b''), reverse=True) fonts.sort(key=lambda f: len(f['Data'] or b''), reverse=True)
@ -781,6 +789,9 @@ def merge_font(fonts):
t0_font = next(f for f in fonts if f['DescendantFont'] == base_font['Reference']) t0_font = next(f for f in fonts if f['DescendantFont'] == base_font['Reference'])
descendant_fonts = [f for f in fonts if f['Subtype'] != 'Type0'] descendant_fonts = [f for f in fonts if f['Subtype'] != 'Type0']
t0_fonts = [f for f in fonts if f['Subtype'] == 'Type0'] t0_fonts = [f for f in fonts if f['Subtype'] == 'Type0']
references_to_drop = tuple(f['Reference'] for f in fonts if f is not base_font and f is not t0_font)
if fonts_are_identical(descendant_fonts):
return t0_font, base_font, references_to_drop
cmaps = list(filter(None, (f['ToUnicode'] for f in t0_fonts))) cmaps = list(filter(None, (f['ToUnicode'] for f in t0_fonts)))
if cmaps: if cmaps:
t0_font['ToUnicode'] = as_bytes(merge_cmaps(cmaps)) t0_font['ToUnicode'] = as_bytes(merge_cmaps(cmaps))
@ -788,7 +799,6 @@ def merge_font(fonts):
arrays = tuple(filter(None, (f[key] for f in descendant_fonts))) arrays = tuple(filter(None, (f[key] for f in descendant_fonts)))
base_font[key] = merge_w_arrays(arrays) base_font[key] = merge_w_arrays(arrays)
base_font['sfnt'] = merge_truetype_fonts_for_pdf(*(f['sfnt'] for f in descendant_fonts)) base_font['sfnt'] = merge_truetype_fonts_for_pdf(*(f['sfnt'] for f in descendant_fonts))
references_to_drop = tuple(f['Reference'] for f in fonts if f is not base_font and f is not t0_font)
return t0_font, base_font, references_to_drop return t0_font, base_font, references_to_drop