| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
|
| 7 |
from read_book.globals import dark_link_color, runtime |
| 8 |
from session import session_defaults |
| 9 |
|
| 10 |
opts = {} |
| 11 |
|
| 12 |
|
| 13 |
def update_settings(settings): |
| 14 |
settings = Object.assign({}, session_defaults(), settings) |
| 15 |
opts.base_font_size = max(8, min(settings.base_font_size, 64)) |
| 16 |
opts.color_scheme = settings.color_scheme |
| 17 |
opts.columns_per_screen = settings.columns_per_screen |
| 18 |
opts.cover_preserve_aspect_ratio = v'!!settings.cover_preserve_aspect_ratio' |
| 19 |
opts.hide_tooltips = settings.hide_tooltips |
| 20 |
opts.is_dark_theme = v'!!settings.is_dark_theme' |
| 21 |
opts.lines_per_sec_auto = settings.lines_per_sec_auto |
| 22 |
opts.lines_per_sec_smooth = settings.lines_per_sec_smooth |
| 23 |
opts.margin_left = max(0, settings.margin_left) |
| 24 |
opts.margin_right = max(0, settings.margin_right) |
| 25 |
opts.margin_top = max(0, settings.margin_top) |
| 26 |
opts.margin_bottom = max(0, settings.margin_bottom) |
| 27 |
opts.override_book_colors = settings.override_book_colors |
| 28 |
opts.paged_wheel_scrolls_by_screen = v'!!settings.paged_wheel_scrolls_by_screen' |
| 29 |
opts.paged_wheel_section_jumps = v'!!settings.paged_wheel_section_jumps' |
| 30 |
opts.paged_pixel_scroll_threshold = settings.paged_pixel_scroll_threshold |
| 31 |
opts.scroll_auto_boundary_delay = settings.scroll_auto_boundary_delay |
| 32 |
opts.scroll_mouse_boundary_delay = settings.scroll_mouse_boundary_delay |
| 33 |
opts.scroll_stop_boundaries = v'!!settings.scroll_stop_boundaries' |
| 34 |
opts.reverse_page_turn_zones = v'!!settings.reverse_page_turn_zones' |
| 35 |
opts.user_stylesheet = settings.user_stylesheet |
| 36 |
opts.gesture_overrides = settings.gesture_overrides |
| 37 |
|
| 38 |
update_settings() |
| 39 |
|
| 40 |
|
| 41 |
def apply_font_size(): |
| 42 |
if not runtime.is_standalone_viewer: |
| 43 |
document.documentElement.style.fontSize = '{}px'.format(opts.base_font_size) |
| 44 |
|
| 45 |
|
| 46 |
def default_selection_colors(): |
| 47 |
if opts.is_dark_theme: |
| 48 |
return dark_link_color, '#111' |
| 49 |
return '#3297FD', '#fff' |
| 50 |
|
| 51 |
|
| 52 |
def make_selection_background_opaque(selbg): |
| 53 |
# see https://stackoverflow.com/questions/7224445/css3-selection-behaves-differently-in-ff-chrome |
| 54 |
if selbg and selbg.startsWith('#') and len(selbg) is 7: |
| 55 |
selbg += 'fe' |
| 56 |
return selbg |
| 57 |
|
| 58 |
|
| 59 |
styles_id = 'calibre-color-scheme-style-overrides' |
| 60 |
|
| 61 |
|
| 62 |
def apply_colors(is_content_popup): |
| 63 |
des = document.documentElement.style |
| 64 |
des.setProperty('--calibre-viewer-background-color', opts.color_scheme.background) |
| 65 |
des.setProperty('--calibre-viewer-foreground-color', opts.color_scheme.foreground) |
| 66 |
des.colorScheme = 'dark' if opts.is_dark_theme else 'light' |
| 67 |
if opts.color_scheme.link: |
| 68 |
des.setProperty('--calibre-viewer-link-color', opts.color_scheme.link) |
| 69 |
for elem in (document.documentElement, document.body): |
| 70 |
elem.style.color = opts.color_scheme.foreground |
| 71 |
# set background color to transparent so that the users background |
| 72 |
# color which is set on the iframe is used instead |
| 73 |
elem.style.backgroundColor = 'transparent' |
| 74 |
des.backgroundColor = 'transparent' |
| 75 |
ss = document.getElementById('calibre-color-scheme-style-overrides') |
| 76 |
if not ss: |
| 77 |
ss = E.style(id=styles_id, type='text/css') |
| 78 |
document.documentElement.appendChild(ss) |
| 79 |
text = '' |
| 80 |
if runtime.is_standalone_viewer and 'macos' in window.navigator.userAgent: |
| 81 |
# Change the hyphenate character to a plain ASCII minus (U+002d) the default |
| 82 |
# is U+2010 but that does not render with the default Times font on macOS as of Monterey |
| 83 |
# and Qt 15.5 See https://bugs.launchpad.net/bugs/1951467 and can be easily reproduced |
| 84 |
# by converting a plain text file with the --pdf-hyphenate option |
| 85 |
# https://bugs.chromium.org/p/chromium/issues/detail?id=1267606 (fix released Feb 1 2022 v98) |
| 86 |
# See also pdf-preprint.js |
| 87 |
text += '\n* { -webkit-hyphenate-character: "-" !important }\n' |
| 88 |
if opts.override_book_colors is not 'never': |
| 89 |
text = 'body' |
| 90 |
if opts.override_book_colors is 'dark': |
| 91 |
text += '.calibre-viewer-dark-colors' |
| 92 |
text += f''' |
| 93 |
* {{ |
| 94 |
color: {opts.color_scheme.foreground} !important; /**/ |
| 95 |
background-color: {opts.color_scheme.background} !important; /**/ |
| 96 |
border-color: {opts.color_scheme.foreground} !important; /**/ |
| 97 |
}}''' |
| 98 |
if opts.color_scheme.link: |
| 99 |
c = opts.color_scheme.link |
| 100 |
# we use the html > body form so that these selectors have higher |
| 101 |
# priority than the override all selectors above |
| 102 |
text += f'\nhtml > body :link, html > body :link * {{ color: {c} !important }} html > body :visited, html > body :visited * {{ color: {c} !important }}' |
| 103 |
|
| 104 |
selbg, selfg = default_selection_colors() |
| 105 |
selbg = make_selection_background_opaque(selbg) |
| 106 |
text += f'\n::selection {{ background-color: {selbg}; color: {selfg} }}' |
| 107 |
text += f'\n::selection:window-inactive {{ background-color: {selbg}; color: {selfg} }}' |
| 108 |
if not is_content_popup: |
| 109 |
# In Chrome when content overflows in RTL and vertical layouts on the left side, |
| 110 |
# it is not displayed properly when scrolling unless overflow:visible is set, |
| 111 |
# but this causes scrollbars to appear. |
| 112 |
# Force disable scrollbars in Chrome, Safari, and Firefox to address this side effect. |
| 113 |
text += '\nhtml::-webkit-scrollbar, body::-webkit-scrollbar { display: none !important }' |
| 114 |
# for firefox: https://developer.mozilla.org/en-US/docs/Web/CSS/scrollbar-width |
| 115 |
text += '\nhtml, body { scrollbar-width: none !important }' |
| 116 |
|
| 117 |
# show a dot after highlights that have notes |
| 118 |
text += '''\n\n |
| 119 |
.crw-has-dot::after { |
| 120 |
content: "";\ |
| 121 |
vertical-align: text-top;\ |
| 122 |
background-color: currentColor !important;\ |
| 123 |
text-decoration: none !important;\ |
| 124 |
display: inline-block;\ |
| 125 |
height: 0.7ex;\ |
| 126 |
width: 0.7ex;\ |
| 127 |
border-radius: 50%;\ |
| 128 |
} |
| 129 |
''' |
| 130 |
hints_box_css = f''' |
| 131 |
display: inline-block !important;\ |
| 132 |
position: absolute !important;\ |
| 133 |
text-indent: 0 !important;\ |
| 134 |
text-decoration: none !important;\ |
| 135 |
font-weight: bold !important;\ |
| 136 |
color: {selfg} !important;\ |
| 137 |
background: {selbg} !important;\ |
| 138 |
cursor: default !important;\ |
| 139 |
padding: 1px !important;\ |
| 140 |
border: solid 1px {opts.color_scheme.foreground} !important;\ |
| 141 |
''' |
| 142 |
|
| 143 |
# reference mode hint box |
| 144 |
text += f'''\n\n |
| 145 |
.calibre-reference-mode [data-calibre-ref-num]::before {{ |
| 146 |
content: attr(data-calibre-ref-num) !important;\ |
| 147 |
{hints_box_css} |
| 148 |
}} |
| 149 |
''' |
| 150 |
|
| 151 |
# follow links hint box |
| 152 |
text += f'''\n\n |
| 153 |
.calibre-hint-visible::before {{ |
| 154 |
content: attr(data-calibre-hint-render) !important;\ |
| 155 |
{hints_box_css} |
| 156 |
}} |
| 157 |
|
| 158 |
.calibre-hint-enter::before {{ |
| 159 |
background: #FF5733 !important; \ |
| 160 |
}} |
| 161 |
|
| 162 |
.calibre-animated-hint {{ |
| 163 |
animation-name: calibre-animate-hint; \ |
| 164 |
animation-duration: 0.3s; \ |
| 165 |
animation-timing-function: ease-out; \ |
| 166 |
display: inline-block !important; \ |
| 167 |
text-indent: 0 !important; \ |
| 168 |
}} |
| 169 |
@keyframes calibre-animate-hint {{ |
| 170 |
from {{ transform: scale(1); }} |
| 171 |
to {{ transform: scale(2); }} |
| 172 |
}} |
| 173 |
''' |
| 174 |
ss.textContent = text |
| 175 |
|
| 176 |
|
| 177 |
def set_selection_style(style): |
| 178 |
if not style: |
| 179 |
selbg, selfg = default_selection_colors() |
| 180 |
style = {'color': selfg, 'background-color': selbg} |
| 181 |
sheet = document.getElementById(styles_id) |
| 182 |
if not sheet: |
| 183 |
return |
| 184 |
css_text = '' |
| 185 |
if style.selbg: |
| 186 |
style.selbg = make_selection_background_opaque(style.selbg) |
| 187 |
for prop in Object.keys(style): |
| 188 |
css_text += f'{prop}: {style[prop]}; ' |
| 189 |
for rule in sheet.sheet.cssRules: |
| 190 |
if rule.type is rule.STYLE_RULE and rule.selectorText.indexOf('selection') > -1: |
| 191 |
rule.style.cssText = css_text |
| 192 |
|
| 193 |
|
| 194 |
def set_color_scheme_class(): |
| 195 |
document.documentElement.classList.add('is-calibre-viewer') |
| 196 |
if opts.is_dark_theme: |
| 197 |
document.body.classList.add('calibre-viewer-dark-colors') |
| 198 |
document.body.classList.remove('calibre-viewer-light-colors') |
| 199 |
else: |
| 200 |
document.body.classList.add('calibre-viewer-light-colors') |
| 201 |
document.body.classList.remove('calibre-viewer-dark-colors') |
| 202 |
|
| 203 |
|
| 204 |
def apply_stylesheet(): |
| 205 |
sid = 'calibre-browser-viewer-user-stylesheet' |
| 206 |
style = document.getElementById(sid) |
| 207 |
if not style: |
| 208 |
if not opts.user_stylesheet: |
| 209 |
return |
| 210 |
style = E.style(type='text/css', id=sid) |
| 211 |
document.documentElement.appendChild(style) |
| 212 |
style.textContent = opts.user_stylesheet |
| 213 |
|
| 214 |
|
| 215 |
def apply_settings(is_content_popup): |
| 216 |
apply_font_size() |
| 217 |
apply_colors(is_content_popup) |
| 218 |
apply_stylesheet() |