mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add support for changing font sizes in generated EPUB files. Also make all font sizes relative, to work around Adobe DE refusing to resize text in a HTML file with absolute font sizes. Fixes #1095 (lit to epub not resizing)
This commit is contained in:
parent
ba99c66fcd
commit
4bb44bd1b6
@ -14,13 +14,15 @@ from calibre.ebooks.html import config as common_config, tostring
|
|||||||
|
|
||||||
class DefaultProfile(object):
|
class DefaultProfile(object):
|
||||||
|
|
||||||
flow_size = sys.maxint
|
flow_size = sys.maxint
|
||||||
screen_size = None
|
screen_size = None
|
||||||
|
dpi = 100
|
||||||
|
|
||||||
class PRS505(DefaultProfile):
|
class PRS505(DefaultProfile):
|
||||||
|
|
||||||
flow_size = 300000
|
flow_size = 300000
|
||||||
screen_size = (600, 775)
|
screen_size = (600, 775)
|
||||||
|
dpi = 166
|
||||||
|
|
||||||
|
|
||||||
PROFILES = {
|
PROFILES = {
|
||||||
@ -105,6 +107,8 @@ to auto-generate a Table of Contents.
|
|||||||
help=_('Set the left margin in pts. Default is %default'))
|
help=_('Set the left margin in pts. Default is %default'))
|
||||||
layout('margin_right', ['--margin-right'], default=5.0,
|
layout('margin_right', ['--margin-right'], default=5.0,
|
||||||
help=_('Set the right margin in pts. Default is %default'))
|
help=_('Set the right margin in pts. Default is %default'))
|
||||||
|
layout('base_font_size', ['--base-font-size'], default=100.0,
|
||||||
|
help=_('The base font size as a percentage. Default is %default. Changing this should allow you to control overall base font sizes, except for input HTML files that use absolute font sizes for their text tags.'))
|
||||||
|
|
||||||
c.add_opt('show_opf', ['--show-opf'], default=False, group='debug',
|
c.add_opt('show_opf', ['--show-opf'], default=False, group='debug',
|
||||||
help=_('Print generated OPF file to stdout'))
|
help=_('Print generated OPF file to stdout'))
|
||||||
|
@ -64,6 +64,7 @@ class HTMLProcessor(Processor):
|
|||||||
|
|
||||||
|
|
||||||
self.extract_css()
|
self.extract_css()
|
||||||
|
self.relativize_font_sizes()
|
||||||
if opts.verbose > 2:
|
if opts.verbose > 2:
|
||||||
self.debug_tree('nocss')
|
self.debug_tree('nocss')
|
||||||
|
|
||||||
|
@ -623,9 +623,45 @@ class Processor(Parser):
|
|||||||
self.css += '\n\n'+self.opts.override_css
|
self.css += '\n\n'+self.opts.override_css
|
||||||
self.do_layout()
|
self.do_layout()
|
||||||
# TODO: Figure out what to do about CSS imports from linked stylesheets
|
# TODO: Figure out what to do about CSS imports from linked stylesheets
|
||||||
|
|
||||||
|
def relativize_font_sizes(self, dpi=100, base=16):
|
||||||
|
'''
|
||||||
|
Convert all absolute font sizes to percentages of ``base`` using ``dpi``
|
||||||
|
to convert from screen to paper units.
|
||||||
|
:param base: Base size in pixels. Adobe DE seems to need base size to be 16
|
||||||
|
irrespective of the unit of the length being converted
|
||||||
|
:param dpi: Dots per inch used to convert pixels to absolute lengths. Since
|
||||||
|
most HTML files are created on computers with monitors of DPI ~ 100, we use
|
||||||
|
100 by default.
|
||||||
|
'''
|
||||||
|
size_value_pat = re.compile(r'(?<!/)(?P<num>[0-9.]+)(?P<unit>cm|mm|in|pt|pc|px)', re.I)
|
||||||
|
|
||||||
|
# points per unit
|
||||||
|
ptu = { # Convert to pt
|
||||||
|
'px' : 72./dpi,
|
||||||
|
'pt' : 1.0,
|
||||||
|
'pc' : 1/12.,
|
||||||
|
'in' : 72.,
|
||||||
|
'cm' : 72/2.54,
|
||||||
|
'mm' : 72/25.4,
|
||||||
|
}
|
||||||
|
|
||||||
|
def relativize(match):
|
||||||
|
val = float(match.group('num'))
|
||||||
|
unit = match.group('unit').lower()
|
||||||
|
val *= ptu[unit]
|
||||||
|
return '%.1f%%'%((val/base) * 100)
|
||||||
|
|
||||||
|
|
||||||
|
def sub(match):
|
||||||
|
rule = match.group(1)
|
||||||
|
value = size_value_pat.sub(relativize, match.group(2))
|
||||||
|
return '%s : %s'%(rule, value)
|
||||||
|
|
||||||
|
self.css = re.compile(r'(font|font-size)\s*:\s*([^;]+)', re.I).sub(sub, self.css)
|
||||||
|
|
||||||
def do_layout(self):
|
def do_layout(self):
|
||||||
self.css += '\nbody {margin-top: 0pt; margin-bottom: 0pt; margin-left: 0pt; margin-right: 0pt}\n'
|
self.css += '\nbody {margin-top: 0pt; margin-bottom: 0pt; margin-left: 0pt; margin-right: 0pt; font-size: %f%%}\n'%self.opts.base_font_size
|
||||||
self.css += '@page {margin-top: %fpt; margin-bottom: %fpt; margin-left: %fpt; margin-right: %fpt}\n'%(self.opts.margin_top, self.opts.margin_bottom, self.opts.margin_left, self.opts.margin_right)
|
self.css += '@page {margin-top: %fpt; margin-bottom: %fpt; margin-left: %fpt; margin-right: %fpt}\n'%(self.opts.margin_top, self.opts.margin_bottom, self.opts.margin_left, self.opts.margin_right)
|
||||||
|
|
||||||
def config(defaults=None, config_name='html',
|
def config(defaults=None, config_name='html',
|
||||||
|
@ -77,7 +77,7 @@
|
|||||||
<item>
|
<item>
|
||||||
<widget class="QStackedWidget" name="stack" >
|
<widget class="QStackedWidget" name="stack" >
|
||||||
<property name="currentIndex" >
|
<property name="currentIndex" >
|
||||||
<number>3</number>
|
<number>1</number>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="metadata_page" >
|
<widget class="QWidget" name="metadata_page" >
|
||||||
<layout class="QGridLayout" name="gridLayout_4" >
|
<layout class="QGridLayout" name="gridLayout_4" >
|
||||||
@ -89,6 +89,36 @@
|
|||||||
<string>Book Cover</string>
|
<string>Book Cover</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="_2" >
|
<layout class="QGridLayout" name="_2" >
|
||||||
|
<item row="0" column="0" >
|
||||||
|
<layout class="QHBoxLayout" name="_3" >
|
||||||
|
<item>
|
||||||
|
<widget class="ImageView" name="cover" >
|
||||||
|
<property name="text" >
|
||||||
|
<string/>
|
||||||
|
</property>
|
||||||
|
<property name="pixmap" >
|
||||||
|
<pixmap resource="../images.qrc" >:/images/book.svg</pixmap>
|
||||||
|
</property>
|
||||||
|
<property name="scaledContents" >
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="alignment" >
|
||||||
|
<set>Qt::AlignCenter</set>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="0" >
|
||||||
|
<widget class="QCheckBox" name="opt_prefer_metadata_cover" >
|
||||||
|
<property name="text" >
|
||||||
|
<string>Use cover from &source file</string>
|
||||||
|
</property>
|
||||||
|
<property name="checked" >
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item row="1" column="0" >
|
<item row="1" column="0" >
|
||||||
<layout class="QVBoxLayout" name="_4" >
|
<layout class="QVBoxLayout" name="_4" >
|
||||||
<property name="spacing" >
|
<property name="spacing" >
|
||||||
@ -140,36 +170,6 @@
|
|||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="0" >
|
|
||||||
<widget class="QCheckBox" name="opt_prefer_metadata_cover" >
|
|
||||||
<property name="text" >
|
|
||||||
<string>Use cover from &source file</string>
|
|
||||||
</property>
|
|
||||||
<property name="checked" >
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="0" column="0" >
|
|
||||||
<layout class="QHBoxLayout" name="_3" >
|
|
||||||
<item>
|
|
||||||
<widget class="ImageView" name="cover" >
|
|
||||||
<property name="text" >
|
|
||||||
<string/>
|
|
||||||
</property>
|
|
||||||
<property name="pixmap" >
|
|
||||||
<pixmap resource="../images.qrc" >:/images/book.svg</pixmap>
|
|
||||||
</property>
|
|
||||||
<property name="scaledContents" >
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
<property name="alignment" >
|
|
||||||
<set>Qt::AlignCenter</set>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</item>
|
|
||||||
</layout>
|
</layout>
|
||||||
<zorder>opt_prefer_metadata_cover</zorder>
|
<zorder>opt_prefer_metadata_cover</zorder>
|
||||||
<zorder></zorder>
|
<zorder></zorder>
|
||||||
@ -396,17 +396,53 @@
|
|||||||
<widget class="QWidget" name="lookandfeel_page" >
|
<widget class="QWidget" name="lookandfeel_page" >
|
||||||
<layout class="QVBoxLayout" name="verticalLayout" >
|
<layout class="QVBoxLayout" name="verticalLayout" >
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLabel" name="label_26" >
|
<layout class="QGridLayout" name="gridLayout_6" >
|
||||||
<property name="text" >
|
<item row="0" column="0" >
|
||||||
<string>Source en&coding:</string>
|
<widget class="QLabel" name="label_26" >
|
||||||
</property>
|
<property name="text" >
|
||||||
<property name="buddy" >
|
<string>Source en&coding:</string>
|
||||||
<cstring>opt_encoding</cstring>
|
</property>
|
||||||
</property>
|
<property name="buddy" >
|
||||||
</widget>
|
<cstring>opt_encoding</cstring>
|
||||||
</item>
|
</property>
|
||||||
<item>
|
</widget>
|
||||||
<widget class="QLineEdit" name="opt_encoding" />
|
</item>
|
||||||
|
<item row="0" column="1" colspan="2" >
|
||||||
|
<widget class="QLineEdit" name="opt_encoding" />
|
||||||
|
</item>
|
||||||
|
<item row="1" column="0" colspan="2" >
|
||||||
|
<widget class="QLabel" name="label_18" >
|
||||||
|
<property name="text" >
|
||||||
|
<string>Base &font size:</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy" >
|
||||||
|
<cstring>opt_base_font_size</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="2" >
|
||||||
|
<widget class="QDoubleSpinBox" name="opt_base_font_size" >
|
||||||
|
<property name="suffix" >
|
||||||
|
<string> %</string>
|
||||||
|
</property>
|
||||||
|
<property name="decimals" >
|
||||||
|
<number>0</number>
|
||||||
|
</property>
|
||||||
|
<property name="minimum" >
|
||||||
|
<double>10.000000000000000</double>
|
||||||
|
</property>
|
||||||
|
<property name="maximum" >
|
||||||
|
<double>500.000000000000000</double>
|
||||||
|
</property>
|
||||||
|
<property name="singleStep" >
|
||||||
|
<double>5.000000000000000</double>
|
||||||
|
</property>
|
||||||
|
<property name="value" >
|
||||||
|
<double>100.000000000000000</double>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QGroupBox" name="groupBox" >
|
<widget class="QGroupBox" name="groupBox" >
|
||||||
|
Loading…
x
Reference in New Issue
Block a user