| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from book_list.globals import get_session_data |
| 9 |
from dom import svgicon |
| 10 |
from dom import unique_id |
| 11 |
from read_book.globals import runtime |
| 12 |
from read_book.prefs.font_size import MAX_FONT_SIZE, MIN_FONT_SIZE |
| 13 |
from read_book.prefs.utils import create_button_box |
| 14 |
|
| 15 |
CONTAINER = unique_id('standalone-font-settings') |
| 16 |
DEFAULT_STANDARD_FONT = 'serif' |
| 17 |
DEFAULT_MINIMUM_FONT_SIZE = 8 |
| 18 |
DEFAULT_ZOOM_STEP_SIZE = 20 |
| 19 |
MIN_ZOOM_STEP_SIZE = 10 |
| 20 |
|
| 21 |
|
| 22 |
def current_zoom_step_size(): |
| 23 |
s = get_session_data().get('standalone_font_settings') |
| 24 |
return s.zoom_step_size or DEFAULT_ZOOM_STEP_SIZE |
| 25 |
|
| 26 |
|
| 27 |
def font_select(name, settings): |
| 28 |
ans = E.select(name=name) |
| 29 |
ans.appendChild(E.option(_('— Choose a font —'), value='')) |
| 30 |
current_val = settings[name] |
| 31 |
if not current_val: |
| 32 |
ans.lastChild.setAttribute('selected', 'selected') |
| 33 |
for family in runtime.all_font_families: |
| 34 |
if family: |
| 35 |
ans.appendChild(E.option(family)) |
| 36 |
if family is current_val: |
| 37 |
ans.lastChild.setAttribute('selected', 'selected') |
| 38 |
return E.div(ans, E.span(svgicon('angle-down')), data_component='select') |
| 39 |
|
| 40 |
|
| 41 |
def standard_font(settings): |
| 42 |
ans = E.select(name='standard_font') |
| 43 |
ans.appendChild(E.option(_('Serif'), value='serif')) |
| 44 |
ans.appendChild(E.option(_('Sans-serif'), value='sans')) |
| 45 |
ans.appendChild(E.option(_('Monospace'), value='mono')) |
| 46 |
sf = settings.standard_font or DEFAULT_STANDARD_FONT |
| 47 |
ans.querySelector(f'[value={sf}]').setAttribute('selected', 'selected') |
| 48 |
return E.div(ans, E.span(svgicon('angle-down')), data_component='select') |
| 49 |
|
| 50 |
|
| 51 |
def minimum_font_size(settings): |
| 52 |
ans = E.input(max=str(MAX_FONT_SIZE), min=str(MIN_FONT_SIZE), step='1', type='number', name='minimum_font_size') |
| 53 |
if jstype(settings.minimum_font_size) is 'number': |
| 54 |
ans.value = settings.minimum_font_size + '' |
| 55 |
else: |
| 56 |
ans.value = '' + DEFAULT_MINIMUM_FONT_SIZE |
| 57 |
return ans |
| 58 |
|
| 59 |
|
| 60 |
def zoom_step_size(settings): |
| 61 |
ans = E.input(max='100', min=MIN_ZOOM_STEP_SIZE + '', step='1', type='number', name='zoom_step_size') |
| 62 |
if jstype(settings.zoom_step_size) is 'number': |
| 63 |
ans.value = max(10, parseInt(settings.zoom_step_size)) + '' |
| 64 |
else: |
| 65 |
ans.value = '' + DEFAULT_ZOOM_STEP_SIZE |
| 66 |
return ans |
| 67 |
|
| 68 |
|
| 69 |
def restore_defaults(): |
| 70 |
container = get_container() |
| 71 |
for q in ('serif_family', 'sans_family', 'mono_family'): |
| 72 |
container.querySelector(f'[name={q}]').value = '' |
| 73 |
container.querySelector('[name=zoom_step_size]').value = DEFAULT_ZOOM_STEP_SIZE + '' |
| 74 |
container.querySelector('[name=minimum_font_size]').value = DEFAULT_MINIMUM_FONT_SIZE + '' |
| 75 |
container.querySelector('[name=standard_font]').value = DEFAULT_STANDARD_FONT |
| 76 |
|
| 77 |
|
| 78 |
def get_container(): |
| 79 |
return document.getElementById(CONTAINER) |
| 80 |
|
| 81 |
|
| 82 |
def create_fonts_panel(container, apply_func, cancel_func): |
| 83 |
container.appendChild(E.div(id=CONTAINER, style='margin: 1rem')) |
| 84 |
container = container.lastChild |
| 85 |
container.append(E.div(_('Choose fonts to use for un-styled text:'), style='margin-top: 1rem')) |
| 86 |
sd = get_session_data() |
| 87 |
settings = sd.get('standalone_font_settings') |
| 88 |
|
| 89 |
def row(label, widget): |
| 90 |
return E.tr(E.td(label + ':\xa0', style='padding-top: 1ex'), E.td(widget, style='padding-top: 1ex')) |
| 91 |
|
| 92 |
container.append(E.table(style='margin-top: 1rem', |
| 93 |
row(_('Serif family'), font_select('serif_family', settings)), |
| 94 |
row(_('Sans-serif family'), font_select('sans_family', settings)), |
| 95 |
row(_('Monospace family'), font_select('mono_family', settings)), |
| 96 |
row(_('Standard font'), standard_font(settings)), |
| 97 |
)) |
| 98 |
|
| 99 |
container.append(E.div(_('Zoom related settings'), style='margin-top: 1rem; padding-top: 1rem; width: 100%; border-top: solid 1px')) |
| 100 |
container.append(E.table( |
| 101 |
row(_('Zoom step size (%)'), zoom_step_size(settings)), |
| 102 |
row(_('Minimum font size (px)'), minimum_font_size(settings)), |
| 103 |
)) |
| 104 |
|
| 105 |
container.appendChild(create_button_box(restore_defaults, apply_func, cancel_func)) |
| 106 |
|
| 107 |
|
| 108 |
develop = create_fonts_panel |
| 109 |
|
| 110 |
|
| 111 |
def commit_fonts(onchange): |
| 112 |
sd = get_session_data() |
| 113 |
container = get_container() |
| 114 |
vals = {} |
| 115 |
zss = parseInt(container.querySelector('[name=zoom_step_size]').value) |
| 116 |
if zss is not DEFAULT_ZOOM_STEP_SIZE: |
| 117 |
vals.zoom_step_size = max(MIN_ZOOM_STEP_SIZE, zss) |
| 118 |
mfs = parseInt(container.querySelector('[name=minimum_font_size]').value) |
| 119 |
if mfs is not DEFAULT_MINIMUM_FONT_SIZE: |
| 120 |
vals.minimum_font_size = mfs |
| 121 |
sf = container.querySelector('[name=standard_font]').value |
| 122 |
if sf is not DEFAULT_STANDARD_FONT: |
| 123 |
vals.standard_font = sf |
| 124 |
for q in ('serif_family', 'sans_family', 'mono_family'): |
| 125 |
val = container.querySelector(f'[name={q}]').value |
| 126 |
if val: |
| 127 |
vals[q] = val |
| 128 |
sd.set('standalone_font_settings', vals) |