diff --git a/src/calibre/ebooks/oeb/polish/fonts.py b/src/calibre/ebooks/oeb/polish/fonts.py new file mode 100644 index 0000000000..cd9857d60e --- /dev/null +++ b/src/calibre/ebooks/oeb/polish/fonts.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +from __future__ import (unicode_literals, division, absolute_import, + print_function) + +__license__ = 'GPL v3' +__copyright__ = '2014, Kovid Goyal ' + +from calibre.ebooks.oeb.polish.container import OEB_STYLES, OEB_DOCS +from calibre.ebooks.oeb.normalize_css import normalize_font + +def unquote(x): + if x and len(x) > 1 and x[0] == x[-1] and x[0] in ('"', "'"): + x = x[1:-1] + return x + +def font_family_data_from_declaration(style, families): + font_families = [] + f = style.getProperty('font') + if f is not None: + f = normalize_font(f.cssValue, font_family_as_list=True).get('font-family', None) + if f is not None: + font_families = f + f = style.getProperty('font-family') + if f is not None: + font_families = [x.cssText for x in f] + + for f in font_families: + f = unquote(f) + families[f] = families.get(f, False) + +def font_family_data_from_sheet(sheet, families): + for rule in sheet.cssRules: + if rule.type == rule.STYLE_RULE: + font_family_data_from_declaration(rule.style, families) + elif rule.type == rule.FONT_FACE_RULE: + ff = rule.style.getProperty('font-family') + if ff is not None: + for f in ff: + families[unquote(f)] = True + +def font_family_data(container): + families = {} + for name, mt in container.mime_map.iteritems(): + if mt in OEB_STYLES: + sheet = container.parsed(name) + font_family_data_from_sheet(sheet, families) + elif mt in OEB_DOCS: + root = container.parsed(name) + for style in root.xpath('//*[local-name() = "style"]'): + if style.text and style.get('type', 'text/css').lower() == 'text/css': + sheet = container.parse_css(style.text) + font_family_data_from_sheet(sheet, families) + for style in root.xpath('//*/@style'): + if style: + style = container.parse_css(style, is_declaration=True) + font_family_data_from_declaration(style, families) + return families diff --git a/src/calibre/gui2/tweak_book/manage_fonts.py b/src/calibre/gui2/tweak_book/manage_fonts.py new file mode 100644 index 0000000000..3173ec4cfd --- /dev/null +++ b/src/calibre/gui2/tweak_book/manage_fonts.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +from __future__ import (unicode_literals, division, absolute_import, + print_function) + +__license__ = 'GPL v3' +__copyright__ = '2014, Kovid Goyal ' + +from PyQt4.Qt import ( + QSplitter, QVBoxLayout, QTableView, QWidget, QSize, QTableModel) + +from calibre.gui2.tweak_book.widgets import Dialog + +class AllFonts(QTableModel): + + def __init__(self, parent=None): + QTableModel.__init__(self, parent) + self.items = [] + self.font_data = {} + + def build(self): + self.beginResetModel() + +class ManageFonts(Dialog): + + def __init__(self, parent=None): + Dialog.__init__(self, _('Manage Fonts'), 'manage-fonts', parent=parent) + + def setup_ui(self): + self.l = l = QVBoxLayout(self) + self.setLayout(l) + + self.bb.clear() + self.bb.addButton(self.bb.Close) + self.splitter = s = QSplitter(self) + l.addWidget(s), l.addWidget(self.bb) + + self.fonts_view = fv = QTableView(self) + self.container = c = QWidget() + l = c.l = QVBoxLayout(c) + c.setLayout(l) + s.addWidget(fv), s.addWidget(l) + + def sizeHint(self): + return QSize(900, 600)