mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Start work on fonts manager
This commit is contained in:
parent
97c59cc1ec
commit
7fa847f6e8
58
src/calibre/ebooks/oeb/polish/fonts.py
Normal file
58
src/calibre/ebooks/oeb/polish/fonts.py
Normal file
@ -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 <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
|
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
|
45
src/calibre/gui2/tweak_book/manage_fonts.py
Normal file
45
src/calibre/gui2/tweak_book/manage_fonts.py
Normal file
@ -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 <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
|
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)
|
Loading…
x
Reference in New Issue
Block a user