mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Conversion: Do not embed a second copy of a font when using the option to embed font family if the font is already embedded in the book. Fixes #2074002 [Private bug](https://bugs.launchpad.net/calibre/+bug/2074002)
This commit is contained in:
parent
72ce908b73
commit
edeb5cb1eb
@ -242,19 +242,29 @@ class CSSFlattener:
|
|||||||
self.oeb.log.warn(msg)
|
self.oeb.log.warn(msg)
|
||||||
return body_font_family, efi
|
return body_font_family, efi
|
||||||
|
|
||||||
|
from calibre.ebooks.oeb.polish.utils import OEB_FONTS
|
||||||
for i, font in enumerate(faces):
|
for i, font in enumerate(faces):
|
||||||
ext = 'otf' if font['is_otf'] else 'ttf'
|
ext = 'otf' if font['is_otf'] else 'ttf'
|
||||||
fid, href = self.oeb.manifest.generate(id='font',
|
font_data = font_scanner.get_font_data(font)
|
||||||
href='fonts/%s.%s'%(ascii_filename(font['full_name']).replace(' ', '-'), ext))
|
for x in self.oeb.manifest:
|
||||||
item = self.oeb.manifest.add(fid, href,
|
if x.media_type in OEB_FONTS:
|
||||||
guess_type('dummy.'+ext)[0],
|
matches = x.data == font_data
|
||||||
data=font_scanner.get_font_data(font))
|
x.unload_data_from_memory()
|
||||||
item.unload_data_from_memory()
|
if matches:
|
||||||
|
item = x
|
||||||
|
href = item.href
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
fid, href = self.oeb.manifest.generate(id='font',
|
||||||
|
href='fonts/%s.%s'%(ascii_filename(font['full_name']).replace(' ', '-'), ext))
|
||||||
|
item = self.oeb.manifest.add(fid, href,
|
||||||
|
guess_type('dummy.'+ext)[0],
|
||||||
|
data=font_data)
|
||||||
|
item.unload_data_from_memory()
|
||||||
|
|
||||||
cfont = {
|
cfont = {
|
||||||
'font-family': '"%s"'%font['font-family'],
|
'font-family': '"%s"'%font['font-family'],
|
||||||
'panose-1': ' '.join(map(str, font['panose'])),
|
'src': 'url(%s)'%item.href,
|
||||||
'src': 'url(%s)'%item.href,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if i == 0:
|
if i == 0:
|
||||||
@ -620,6 +630,25 @@ class CSSFlattener:
|
|||||||
return href
|
return href
|
||||||
|
|
||||||
def collect_global_css(self):
|
def collect_global_css(self):
|
||||||
|
def rules_in(sheets):
|
||||||
|
for s in sheets:
|
||||||
|
yield from s.cssRules
|
||||||
|
def unique_font_face_rules(*rules):
|
||||||
|
seen = set()
|
||||||
|
for rule in rules:
|
||||||
|
try:
|
||||||
|
ff = rule.style.getPropertyValue('font-family')
|
||||||
|
src = rule.style.getPropertyValue('src')
|
||||||
|
w = rule.style.getPropertyValue('font-weight')
|
||||||
|
s = rule.style.getPropertyValue('font-style')
|
||||||
|
except Exception:
|
||||||
|
yield rule
|
||||||
|
else:
|
||||||
|
key = ff, src, w, s
|
||||||
|
if key not in seen:
|
||||||
|
seen.add(key)
|
||||||
|
yield rule
|
||||||
|
|
||||||
global_css = defaultdict(list)
|
global_css = defaultdict(list)
|
||||||
for item in self.items:
|
for item in self.items:
|
||||||
stylizer = self.stylizers[item]
|
stylizer = self.stylizers[item]
|
||||||
@ -632,7 +661,7 @@ class CSSFlattener:
|
|||||||
items = sorted(stylizer.page_rule.items())
|
items = sorted(stylizer.page_rule.items())
|
||||||
css = ';\n'.join(f"{key}: {val}" for key, val in items)
|
css = ';\n'.join(f"{key}: {val}" for key, val in items)
|
||||||
css = ('@page {\n%s\n}\n'%css) if items else ''
|
css = ('@page {\n%s\n}\n'%css) if items else ''
|
||||||
rules = [css_text(r) for r in stylizer.font_face_rules + self.embed_font_rules]
|
rules = [css_text(r) for r in unique_font_face_rules(*stylizer.font_face_rules, *rules_in(self.embed_font_rules))]
|
||||||
raw = '\n\n'.join(rules)
|
raw = '\n\n'.join(rules)
|
||||||
css += '\n\n' + raw
|
css += '\n\n' + raw
|
||||||
global_css[css].append(item)
|
global_css[css].append(item)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user