| 1 |
#!/usr/bin/env python |
| 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 |
# The base class that all tools must inherit from |
| 17 |
from calibre.gui2.tweak_book.plugin import Tool |
| 18 |
|
| 19 |
|
| 20 |
class DemoTool(Tool): |
| 21 |
#: Set this to a unique name it will be used as a key |
| 22 |
name = 'demo-tool' |
| 23 |
|
| 24 |
#: If True the user can choose to place this tool in the plugins toolbar |
| 25 |
allowed_in_toolbar = True |
| 26 |
|
| 27 |
#: If True the user can choose to place this tool in the plugins menu |
| 28 |
allowed_in_menu = True |
| 29 |
|
| 30 |
def create_action(self, for_toolbar=True): |
| 31 |
# Create an action, this will be added to the plugins toolbar and |
| 32 |
# the plugins menu |
| 33 |
ac = QAction(get_icons('images/icon.png'), 'Magnify fonts', self.gui) # noqa: F821 |
| 34 |
if not for_toolbar: |
| 35 |
# Register a keyboard shortcut for this toolbar action. We only |
| 36 |
# register it for the action created for the menu, not the toolbar, |
| 37 |
# to avoid a double trigger |
| 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 |
# Ask the user for a factor by which to multiply all font sizes |
| 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 |
# Ensure any in progress editing the user is doing is present in the container |
| 49 |
self.boss.commit_all_editors_to_container() |
| 50 |
try: |
| 51 |
self.magnify_fonts(factor) |
| 52 |
except Exception: |
| 53 |
# Something bad happened report the error to the user |
| 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 |
# Revert to the saved restore point |
| 64 |
self.boss.revert_requested(self.boss.global_undo.previous_container) |
| 65 |
else: |
| 66 |
# Show the user what changes we have made, allowing her to |
| 67 |
# revert them if necessary |
| 68 |
self.boss.show_current_diff() |
| 69 |
# Update the editor UI to take into account all the changes we |
| 70 |
# have made |
| 71 |
self.boss.apply_container_update_to_gui() |
| 72 |
|
| 73 |
def magnify_fonts(self, factor): |
| 74 |
# Magnify all font sizes defined in the book by the specified factor |
| 75 |
# First we create a restore point so that the user can undo all changes |
| 76 |
# we make. |
| 77 |
self.boss.add_savepoint('Before: Magnify fonts') |
| 78 |
|
| 79 |
container = self.current_container # The book being edited as a container object |
| 80 |
|
| 81 |
# Iterate over all style declarations in the book, this means css |
| 82 |
# stylesheets, <style> tags and style="" attributes |
| 83 |
for name, media_type in container.mime_map.items(): |
| 84 |
if media_type in OEB_STYLES: |
| 85 |
# A stylesheet. Parsed stylesheets are css_parser CSSStylesheet |
| 86 |
# objects. |
| 87 |
self.magnify_stylesheet(container.parsed(name), factor) |
| 88 |
container.dirty(name) # Tell the container that we have changed the stylesheet |
| 89 |
elif media_type in OEB_DOCS: |
| 90 |
# A HTML file. Parsed HTML files are lxml elements |
| 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 |
# We have an inline CSS <style> tag, parse it into a |
| 95 |
# stylesheet object |
| 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) # Tell the container that we have changed the stylesheet |
| 100 |
for elem in container.parsed(name).xpath('//*[@style]'): |
| 101 |
# Process inline style attributes |
| 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 |
# Magnify all fonts in the specified stylesheet by the specified |
| 108 |
# factor. |
| 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 |
# Magnify all fonts in the specified style declaration by the specified |
| 114 |
# factor |
| 115 |
val = style.getPropertyValue('font-size') |
| 116 |
if not val: |
| 117 |
return |
| 118 |
# see if the font-size contains a number |
| 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 |
# We should also be dealing with the font shorthand property and |
| 125 |
# font sizes specified as non numbers, but those are left as exercises |
| 126 |
# for the reader |