From 9d57aa4e331861274540c23c552455e10058a845 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 14 Nov 2016 10:59:35 +0530 Subject: [PATCH] Edit Book: When adding a font file via File->New automatically generate the appropriate @font-face rule and copy it to the clipboard so that it can be easily inserted into the appropriate CSS files. --- src/calibre/gui2/tweak_book/boss.py | 8 +++++-- src/calibre/gui2/tweak_book/manage_fonts.py | 23 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 8f55948936..90078b2220 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -435,14 +435,17 @@ class Boss(QObject): d = NewFileDialog(self.gui) if d.exec_() != d.Accepted: return - self.do_add_file(d.file_name, d.file_data, using_template=d.using_template, edit_file=True) + added_name = self.do_add_file(d.file_name, d.file_data, using_template=d.using_template, edit_file=True) + if d.file_name.rpartition('.')[2].lower() in ('ttf', 'otf', 'woff'): + from calibre.gui2.tweak_book.manage_fonts import show_font_face_rule_for_font_file + show_font_face_rule_for_font_file(d.file_data, added_name, self.gui) def do_add_file(self, file_name, data, using_template=False, edit_file=False): self.add_savepoint(_('Before: Add file %s') % self.gui.elided_text(file_name)) c = current_container() adata = data.replace(b'%CURSOR%', b'') if using_template else data try: - c.add_file(file_name, adata) + added_name = c.add_file(file_name, adata) except: self.rewind_savepoint() raise @@ -459,6 +462,7 @@ class Boss(QObject): self.edit_file(file_name, syntax) self.set_modified() completion_worker().clear_caches('names') + return added_name def add_files(self): if not self.ensure_book(_('You must first open a book to tweak, before trying to create new files in it.')): diff --git a/src/calibre/gui2/tweak_book/manage_fonts.py b/src/calibre/gui2/tweak_book/manage_fonts.py index d4ee672c0c..e7ec2abb40 100644 --- a/src/calibre/gui2/tweak_book/manage_fonts.py +++ b/src/calibre/gui2/tweak_book/manage_fonts.py @@ -7,11 +7,12 @@ __license__ = 'GPL v3' __copyright__ = '2014, Kovid Goyal ' import sys, textwrap +from io import BytesIO from PyQt5.Qt import ( QSplitter, QVBoxLayout, QTableView, QWidget, QLabel, QAbstractTableModel, Qt, QTimer, QPushButton, pyqtSignal, QFormLayout, QLineEdit, QIcon, QSize, - QHBoxLayout, QTextEdit) + QHBoxLayout, QTextEdit, QApplication, QMessageBox) from calibre.ebooks.oeb.polish.container import get_container from calibre.ebooks.oeb.polish.fonts import font_family_data, change_font @@ -20,6 +21,26 @@ from calibre.gui2.tweak_book import current_container, set_current_container from calibre.gui2.tweak_book.widgets import Dialog, BusyCursor from calibre.utils.icu import primary_sort_key as sort_key from calibre.utils.fonts.scanner import font_scanner, NoFonts +from calibre.utils.fonts.metadata import FontMetadata, UnsupportedFont + + +def show_font_face_rule_for_font_file(file_data, added_name, parent=None): + try: + fm = FontMetadata(BytesIO(file_data)).to_dict() + except UnsupportedFont: + return + pp = _('Change this to the relative path to: %s') % added_name + rule = '''@font-face {{ + src: url({pp}); + font-family: "{ff}"; + font-weight: {w}; + 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']) + 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)) class EmbeddingData(Dialog):