From 808856eabe7c495bba512470bca02663d398957a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 28 Nov 2019 11:23:11 +0530 Subject: [PATCH] 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. --- src/calibre/gui2/tweak_book/boss.py | 6 ++++++ src/calibre/gui2/tweak_book/manage_fonts.py | 22 +++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index e7d9d470ea..036e9e3e25 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -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) diff --git a/src/calibre/gui2/tweak_book/manage_fonts.py b/src/calibre/gui2/tweak_book/manage_fonts.py index 236ca44edb..49246b1462 100644 --- a/src/calibre/gui2/tweak_book/manage_fonts.py +++ b/src/calibre/gui2/tweak_book/manage_fonts.py @@ -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 {} 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):