| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
__license__ = 'GPL v3' |
| 5 |
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>' |
| 6 |
|
| 7 |
import re |
| 8 |
|
| 9 |
from css_parser.css import CSSRule |
| 10 |
from qt.core import QAction, QInputDialog |
| 11 |
|
| 12 |
from calibre import force_unicode |
| 13 |
from calibre.ebooks.oeb.polish.container import OEB_DOCS, OEB_STYLES, serialize |
| 14 |
from calibre.gui2 import error_dialog |
| 15 |
|
| 16 |
|
| 17 |
from calibre.gui2.tweak_book.plugin import Tool |
| 18 |
|
| 19 |
|
| 20 |
class DemoTool(Tool): |
| 21 |
|
| 22 |
name = 'demo-tool' |
| 23 |
|
| 24 |
|
| 25 |
allowed_in_toolbar = True |
| 26 |
|
| 27 |
|
| 28 |
allowed_in_menu = True |
| 29 |
|
| 30 |
def create_action(self, for_toolbar=True): |
| 31 |
|
| 32 |
|
| 33 |
ac = QAction(get_icons('images/icon.png'), 'Magnify fonts', self.gui) |
| 34 |
if not for_toolbar: |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
self.register_shortcut(ac, 'magnify-fonts-tool', default_keys=('Ctrl+Shift+Alt+D',)) |
| 39 |
ac.triggered.connect(self.ask_user) |
| 40 |
return ac |
| 41 |
|
| 42 |
def ask_user(self): |
| 43 |
|
| 44 |
factor, ok = QInputDialog.getDouble( |
| 45 |
self.gui, 'Enter a magnification factor', 'Allow font sizes in the book will be multiplied by the specified factor', value=2, min=0.1, max=4 |
| 46 |
) |
| 47 |
if ok: |
| 48 |
|
| 49 |
self.boss.commit_all_editors_to_container() |
| 50 |
try: |
| 51 |
self.magnify_fonts(factor) |
| 52 |
except Exception: |
| 53 |
|
| 54 |
import traceback |
| 55 |
|
| 56 |
error_dialog( |
| 57 |
self.gui, |
| 58 |
_('Failed to magnify fonts'), |
| 59 |
_('Failed to magnify fonts, click "Show details" for more info'), |
| 60 |
det_msg=traceback.format_exc(), |
| 61 |
show=True, |
| 62 |
) |
| 63 |
|
| 64 |
self.boss.revert_requested(self.boss.global_undo.previous_container) |
| 65 |
else: |
| 66 |
|
| 67 |
|
| 68 |
self.boss.show_current_diff() |
| 69 |
|
| 70 |
|
| 71 |
self.boss.apply_container_update_to_gui() |
| 72 |
|
| 73 |
def magnify_fonts(self, factor): |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
self.boss.add_savepoint('Before: Magnify fonts') |
| 78 |
|
| 79 |
container = self.current_container |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
for name, media_type in container.mime_map.items(): |
| 84 |
if media_type in OEB_STYLES: |
| 85 |
|
| 86 |
|
| 87 |
self.magnify_stylesheet(container.parsed(name), factor) |
| 88 |
container.dirty(name) |
| 89 |
elif media_type in OEB_DOCS: |
| 90 |
|
| 91 |
|
| 92 |
for style_tag in container.parsed(name).xpath('//*[local-name="style"]'): |
| 93 |
if style_tag.text and style_tag.get('type', None) in {None, 'text/css'}: |
| 94 |
|
| 95 |
|
| 96 |
sheet = container.parse_css(style_tag.text) |
| 97 |
self.magnify_stylesheet(sheet, factor) |
| 98 |
style_tag.text = serialize(sheet, 'text/css', pretty_print=True) |
| 99 |
container.dirty(name) |
| 100 |
for elem in container.parsed(name).xpath('//*[@style]'): |
| 101 |
|
| 102 |
block = container.parse_css(elem.get('style'), is_declaration=True) |
| 103 |
self.magnify_declaration(block, factor) |
| 104 |
elem.set('style', force_unicode(block.getCssText(separator=' '), 'utf-8')) |
| 105 |
|
| 106 |
def magnify_stylesheet(self, sheet, factor): |
| 107 |
|
| 108 |
|
| 109 |
for rule in sheet.cssRules.rulesOfType(CSSRule.STYLE_RULE): |
| 110 |
self.magnify_declaration(rule.style, factor) |
| 111 |
|
| 112 |
def magnify_declaration(self, style, factor): |
| 113 |
|
| 114 |
|
| 115 |
val = style.getPropertyValue('font-size') |
| 116 |
if not val: |
| 117 |
return |
| 118 |
|
| 119 |
num = re.search(r'[0-9.]+', val) |
| 120 |
if num is not None: |
| 121 |
num = num.group() |
| 122 |
val = val.replace(num, f'{float(num) * factor:f}') |
| 123 |
style.setProperty('font-size', val) |
| 124 |
|
| 125 |
|
| 126 |
|