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.

This commit is contained in:
Kovid Goyal 2016-11-14 10:59:35 +05:30
parent 933f0f55d2
commit 9d57aa4e33
2 changed files with 28 additions and 3 deletions

View File

@ -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.')):

View File

@ -7,11 +7,12 @@ __license__ = 'GPL v3'
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
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 <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))
class EmbeddingData(Dialog):