Edit book: Create @font-face rules when importing multiple font files into book automatically, similar to how it is done when importing a single font file.

This commit is contained in:
Kovid Goyal 2019-11-28 11:23:11 +05:30
parent c091461b99
commit 808856eabe
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 26 additions and 2 deletions

View File

@ -512,6 +512,7 @@ class Boss(QObject):
for x, folder in iteritems(folder_map)}
self.add_savepoint(_('Before Add files'))
c = current_container()
added_fonts = set()
for path in sorted(files, key=numeric_sort_key):
name = files[path]
i = 0
@ -525,11 +526,16 @@ class Boss(QObject):
except:
self.rewind_savepoint()
raise
if name.rpartition('.')[2].lower() in ('ttf', 'otf', 'woff'):
added_fonts.add(name)
self.gui.file_list.build(c)
if c.opf_name in editors:
editors[c.opf_name].replace_data(c.raw_data(c.opf_name))
self.set_modified()
completion_worker().clear_caches('names')
if added_fonts:
from calibre.gui2.tweak_book.manage_fonts import show_font_face_rule_for_font_files
show_font_face_rule_for_font_files(c, added_fonts, self.gui)
def add_cover(self):
d = AddCover(current_container(), self.gui)

View File

@ -24,9 +24,9 @@ from calibre.utils.fonts.metadata import FontMetadata, UnsupportedFont
from polyglot.builtins import iteritems, unicode_type
def show_font_face_rule_for_font_file(file_data, added_name, parent=None):
def rule_for_font(font_file, added_name):
try:
fm = FontMetadata(BytesIO(file_data)).to_dict()
fm = FontMetadata(font_file).to_dict()
except UnsupportedFont:
return
pp = _('Change this to the relative path to: %s') % added_name
@ -37,12 +37,30 @@ def show_font_face_rule_for_font_file(file_data, added_name, parent=None):
font-style: {sy};
font-stretch: {st};
}}'''.format(pp=pp, ff=fm['font-family'], w=fm['font-weight'], sy=fm['font-style'], st=fm['font-stretch'])
return rule
def show_font_face_rule_for_font_file(file_data, added_name, parent=None):
rule = rule_for_font(BytesIO(file_data), added_name)
QApplication.clipboard().setText(rule)
QMessageBox.information(parent, _('Font file added'), _(
'The font file <b>{}</b> has been added. The text for the CSS @font-face rule for this file has been copied'
' to the clipboard. You should paste it into whichever CSS file you want to add this font to.').format(added_name))
def show_font_face_rule_for_font_files(container, added_names, parent=None):
rules = []
for name in sorted(added_names):
rule = rule_for_font(container.open(name), name)
if rule:
rules.append(rule)
if rules:
QApplication.clipboard().setText('\n\n'.join(rules))
QMessageBox.information(parent, _('Font files added'), _(
'The specified font files have been added. The text for the CSS @font-face rules for these files has been copied'
' to the clipboard. You should paste it into whichever CSS file you want to add these fonts to.'))
class EmbeddingData(Dialog):
def __init__(self, family, faces, parent=None):