mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #282. Implement the --base-font-size option
This commit is contained in:
parent
41b7f7445f
commit
29ecca2c11
@ -114,11 +114,8 @@ def option_parser(usage):
|
|||||||
parser.add_option('--ignore-tables', action='store_true', default=False, dest='ignore_tables',
|
parser.add_option('--ignore-tables', action='store_true', default=False, dest='ignore_tables',
|
||||||
help=_('Render HTML tables as blocks of text instead of actual tables. This is neccessary if the HTML contains very large or complex tables.'))
|
help=_('Render HTML tables as blocks of text instead of actual tables. This is neccessary if the HTML contains very large or complex tables.'))
|
||||||
laf = parser.add_option_group('LOOK AND FEEL')
|
laf = parser.add_option_group('LOOK AND FEEL')
|
||||||
laf.add_option('--font-delta', action='store', type='float', default=0., \
|
laf.add_option('--base-font-size', action='store', type='float', default=10.,
|
||||||
help=_("""Increase the font size by 2 * FONT_DELTA pts and """
|
help=_('''Specify the base font size in pts. All fonts are rescaled accordingly. This option obsoletes the --font-delta option and takes precendence over it. To use --font-delta, set this to 0.'''))
|
||||||
'''the line spacing by FONT_DELTA pts. FONT_DELTA can be a fraction.'''
|
|
||||||
"""If FONT_DELTA is negative, the font size is decreased."""),
|
|
||||||
dest='font_delta')
|
|
||||||
laf.add_option('--enable-autorotation', action='store_true', default=False,
|
laf.add_option('--enable-autorotation', action='store_true', default=False,
|
||||||
help=_('Enable autorotation of images that are wider than the screen width.'),
|
help=_('Enable autorotation of images that are wider than the screen width.'),
|
||||||
dest='autorotation')
|
dest='autorotation')
|
||||||
@ -134,6 +131,12 @@ def option_parser(usage):
|
|||||||
help=_('Override the CSS. Can be either a path to a CSS stylesheet or a string. If it is a string it is interpreted as CSS.'))
|
help=_('Override the CSS. Can be either a path to a CSS stylesheet or a string. If it is a string it is interpreted as CSS.'))
|
||||||
laf.add_option('--use-spine', default=False, dest='use_spine', action='store_true',
|
laf.add_option('--use-spine', default=False, dest='use_spine', action='store_true',
|
||||||
help=_('Use the <spine> element from the OPF file to determine the order in which the HTML files are appended to the LRF. The .opf file must be in the same directory as the base HTML file.'))
|
help=_('Use the <spine> element from the OPF file to determine the order in which the HTML files are appended to the LRF. The .opf file must be in the same directory as the base HTML file.'))
|
||||||
|
laf.add_option('--font-delta', action='store', type='float', default=0., \
|
||||||
|
help=_("""Increase the font size by 2 * FONT_DELTA pts and """
|
||||||
|
'''the line spacing by FONT_DELTA pts. FONT_DELTA can be a fraction.'''
|
||||||
|
"""If FONT_DELTA is negative, the font size is decreased."""),
|
||||||
|
dest='font_delta')
|
||||||
|
|
||||||
|
|
||||||
page = parser.add_option_group('PAGE OPTIONS')
|
page = parser.add_option_group('PAGE OPTIONS')
|
||||||
profiles = profile_map.keys()
|
profiles = profile_map.keys()
|
||||||
|
@ -264,6 +264,10 @@ class HTMLConverter(object):
|
|||||||
ascii_text = text.encode('ascii', 'ignore')
|
ascii_text = text.encode('ascii', 'ignore')
|
||||||
self.book.addTocEntry(ascii_text, tb)
|
self.book.addTocEntry(ascii_text, tb)
|
||||||
|
|
||||||
|
if self.base_font_size > 0:
|
||||||
|
self.logger.info('\tRationalizing font sizes...')
|
||||||
|
self.book.rationalize_font_sizes(self.base_font_size)
|
||||||
|
|
||||||
def is_baen(self, soup):
|
def is_baen(self, soup):
|
||||||
return bool(soup.find('meta', attrs={'name':'Publisher',
|
return bool(soup.find('meta', attrs={'name':'Publisher',
|
||||||
'content':re.compile('Baen', re.IGNORECASE)}))
|
'content':re.compile('Baen', re.IGNORECASE)}))
|
||||||
|
@ -36,7 +36,7 @@
|
|||||||
# Plot, Image (outside of ImageBlock),
|
# Plot, Image (outside of ImageBlock),
|
||||||
# EmpLine, EmpDots
|
# EmpLine, EmpDots
|
||||||
|
|
||||||
import os, re, codecs
|
import os, re, codecs, operator
|
||||||
from datetime import date
|
from datetime import date
|
||||||
try:
|
try:
|
||||||
from elementtree.ElementTree import (Element, SubElement)
|
from elementtree.ElementTree import (Element, SubElement)
|
||||||
@ -311,6 +311,14 @@ class LrsContainer(object):
|
|||||||
self.contents.append(content)
|
self.contents.append(content)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def get_all(self, predicate=lambda x: x):
|
||||||
|
for child in self.contents:
|
||||||
|
if predicate(child):
|
||||||
|
yield child
|
||||||
|
if hasattr(child, 'get_all'):
|
||||||
|
for grandchild in child.get_all(predicate):
|
||||||
|
yield grandchild
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class LrsObject(object):
|
class LrsObject(object):
|
||||||
@ -533,6 +541,43 @@ class Book(Delegator):
|
|||||||
method(content)
|
method(content)
|
||||||
|
|
||||||
|
|
||||||
|
def rationalize_font_sizes(self, base_font_size=10):
|
||||||
|
base_font_size *= 10.
|
||||||
|
main = None
|
||||||
|
for obj in self.delegates:
|
||||||
|
if isinstance(obj, Main):
|
||||||
|
main = obj
|
||||||
|
break
|
||||||
|
pages = [obj for obj in main.contents if isinstance(obj, Page)]
|
||||||
|
text_blocks = []
|
||||||
|
for p in pages:
|
||||||
|
for obj in p.contents:
|
||||||
|
if isinstance(obj, TextBlock):
|
||||||
|
text_blocks.append(obj)
|
||||||
|
|
||||||
|
text_styles = set([t.textStyle for t in text_blocks])
|
||||||
|
fonts = {}
|
||||||
|
for ts in text_styles:
|
||||||
|
fs = int(ts.attrs['fontsize'])
|
||||||
|
if fonts.has_key(fs):
|
||||||
|
fonts[fs] += 1
|
||||||
|
else:
|
||||||
|
fonts[fs] = 1
|
||||||
|
|
||||||
|
old_base_font_size = float(max(zip(fonts.keys(), fonts.values()), key=operator.itemgetter(1))[0])
|
||||||
|
|
||||||
|
def rescale(old):
|
||||||
|
return str(int(int(old) * (base_font_size/old_base_font_size)))
|
||||||
|
|
||||||
|
for ts in text_styles:
|
||||||
|
ts.attrs['fontsize'] = rescale(ts.attrs['fontsize'])
|
||||||
|
|
||||||
|
for tb in text_blocks:
|
||||||
|
if tb.textSettings.has_key('fontsize'):
|
||||||
|
tb.textSettings['fontsize'] = rescale(tb.textSettings['fontsize'])
|
||||||
|
for span in tb.get_all(lambda x: isinstance(x, Span) and x.attrs.has_key('fontsize')):
|
||||||
|
span.attrs['fontsize'] = rescale(span.attrs['fontsize'])
|
||||||
|
|
||||||
def renderLrs(self, lrsFile):
|
def renderLrs(self, lrsFile):
|
||||||
if isinstance(lrsFile, basestring):
|
if isinstance(lrsFile, basestring):
|
||||||
lrsFile = codecs.open(lrsFile, "wb", encoding="utf-16")
|
lrsFile = codecs.open(lrsFile, "wb", encoding="utf-16")
|
||||||
|
@ -472,15 +472,15 @@
|
|||||||
<item row="0" column="0" colspan="3" >
|
<item row="0" column="0" colspan="3" >
|
||||||
<widget class="QLabel" name="label_8" >
|
<widget class="QLabel" name="label_8" >
|
||||||
<property name="text" >
|
<property name="text" >
|
||||||
<string>&Font delta:</string>
|
<string>Base &font size:</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="buddy" >
|
<property name="buddy" >
|
||||||
<cstring>gui_font_delta</cstring>
|
<cstring>gui_base_font_size</cstring>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="0" column="2" >
|
<item row="0" column="2" >
|
||||||
<widget class="QDoubleSpinBox" name="gui_font_delta" >
|
<widget class="QDoubleSpinBox" name="gui_base_font_size" >
|
||||||
<property name="buttonSymbols" >
|
<property name="buttonSymbols" >
|
||||||
<enum>QAbstractSpinBox::PlusMinus</enum>
|
<enum>QAbstractSpinBox::PlusMinus</enum>
|
||||||
</property>
|
</property>
|
||||||
@ -491,14 +491,17 @@
|
|||||||
<number>1</number>
|
<number>1</number>
|
||||||
</property>
|
</property>
|
||||||
<property name="minimum" >
|
<property name="minimum" >
|
||||||
<double>-5.000000000000000</double>
|
<double>2.000000000000000</double>
|
||||||
</property>
|
</property>
|
||||||
<property name="maximum" >
|
<property name="maximum" >
|
||||||
<double>5.000000000000000</double>
|
<double>20.000000000000000</double>
|
||||||
</property>
|
</property>
|
||||||
<property name="singleStep" >
|
<property name="singleStep" >
|
||||||
<double>0.100000000000000</double>
|
<double>0.100000000000000</double>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="value" >
|
||||||
|
<double>10.000000000000000</double>
|
||||||
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="0" colspan="3" >
|
<item row="1" column="0" colspan="3" >
|
||||||
|
@ -166,10 +166,10 @@ class Main(MainWindow, Ui_MainWindow):
|
|||||||
if not self.renderer.aborted and self.renderer.lrf is not None:
|
if not self.renderer.aborted and self.renderer.lrf is not None:
|
||||||
width, height = self.renderer.lrf.device_info.width, \
|
width, height = self.renderer.lrf.device_info.width, \
|
||||||
self.renderer.lrf.device_info.height
|
self.renderer.lrf.device_info.height
|
||||||
self.graphics_view.resize_for(width, height)
|
self.graphics_view.resize_for(width+5, height+5)
|
||||||
desktop = QCoreApplication.instance().desktop()
|
desktop = QCoreApplication.instance().desktop()
|
||||||
screen_height = desktop.availableGeometry().height()
|
screen_height = desktop.availableGeometry().height()
|
||||||
height = min(screen_height, height+50)
|
height = min(screen_height, height+55)
|
||||||
self.resize(self.size().width(), height)
|
self.resize(self.size().width(), height)
|
||||||
self.setWindowTitle(self.renderer.lrf.metadata.title + ' - ' + __appname__)
|
self.setWindowTitle(self.renderer.lrf.metadata.title + ' - ' + __appname__)
|
||||||
self.document_title = self.renderer.lrf.metadata.title
|
self.document_title = self.renderer.lrf.metadata.title
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>614</width>
|
<width>615</width>
|
||||||
<height>702</height>
|
<height>702</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user