diff --git a/src/calibre/ebooks/epub/__init__.py b/src/calibre/ebooks/epub/__init__.py index 1a48cddef7..bf151d840c 100644 --- a/src/calibre/ebooks/epub/__init__.py +++ b/src/calibre/ebooks/epub/__init__.py @@ -14,13 +14,15 @@ from calibre.ebooks.html import config as common_config, tostring class DefaultProfile(object): - flow_size = sys.maxint + flow_size = sys.maxint screen_size = None + dpi = 100 class PRS505(DefaultProfile): flow_size = 300000 screen_size = (600, 775) + dpi = 166 PROFILES = { @@ -105,6 +107,8 @@ to auto-generate a Table of Contents. help=_('Set the left margin in pts. Default is %default')) layout('margin_right', ['--margin-right'], default=5.0, 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', help=_('Print generated OPF file to stdout')) diff --git a/src/calibre/ebooks/epub/from_html.py b/src/calibre/ebooks/epub/from_html.py index 92fc8382ff..8e9c0dd1ec 100644 --- a/src/calibre/ebooks/epub/from_html.py +++ b/src/calibre/ebooks/epub/from_html.py @@ -64,6 +64,7 @@ class HTMLProcessor(Processor): self.extract_css() + self.relativize_font_sizes() if opts.verbose > 2: self.debug_tree('nocss') diff --git a/src/calibre/ebooks/html.py b/src/calibre/ebooks/html.py index 7525c4a07a..e22f2bbc0e 100644 --- a/src/calibre/ebooks/html.py +++ b/src/calibre/ebooks/html.py @@ -623,9 +623,45 @@ class Processor(Parser): self.css += '\n\n'+self.opts.override_css self.do_layout() # 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'(?[0-9.]+)(?Pcm|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): - 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) def config(defaults=None, config_name='html', diff --git a/src/calibre/gui2/dialogs/epub.ui b/src/calibre/gui2/dialogs/epub.ui index 4e748e5af9..fe4ccdef5d 100644 --- a/src/calibre/gui2/dialogs/epub.ui +++ b/src/calibre/gui2/dialogs/epub.ui @@ -77,7 +77,7 @@ - 3 + 1 @@ -89,6 +89,36 @@ Book Cover + + + + + + + + + :/images/book.svg + + + true + + + Qt::AlignCenter + + + + + + + + + Use cover from &source file + + + true + + + @@ -140,36 +170,6 @@ - - - - Use cover from &source file - - - true - - - - - - - - - - - - :/images/book.svg - - - true - - - Qt::AlignCenter - - - - - opt_prefer_metadata_cover @@ -396,17 +396,53 @@ - - - Source en&coding: - - - opt_encoding - - - - - + + + + + Source en&coding: + + + opt_encoding + + + + + + + + + + Base &font size: + + + opt_base_font_size + + + + + + + % + + + 0 + + + 10.000000000000000 + + + 500.000000000000000 + + + 5.000000000000000 + + + 100.000000000000000 + + + +