Add a wizard to make customizing the font rescaling algorithm easier

This commit is contained in:
Kovid Goyal 2009-10-25 18:59:05 -06:00
parent dc9aa69508
commit d70d42f900
4 changed files with 483 additions and 119 deletions

View File

@ -0,0 +1,104 @@
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from PyQt4.Qt import QDialog, SIGNAL
from calibre.gui2.convert.font_key_ui import Ui_Dialog
class FontKeyChooser(QDialog, Ui_Dialog):
def __init__(self, parent=None, base_font_size=0.0, font_key=None):
QDialog.__init__(self, parent)
self.setupUi(self)
self.default_font_key = font_key
self.default_base_font_size = base_font_size
self.connect(self.buttonBox, SIGNAL('clicked(QAbstractButton*)'),
self.button_clicked)
self.connect(self.button_use_default, SIGNAL('clicked()'),
self.use_default)
for x in ('input_base_font_size', 'input_font_size',
'output_base_font_size'):
self.connect(getattr(self, x), SIGNAL('valueChanged(double)'),
self.calculate)
self.connect(self.font_size_key, SIGNAL('textChanged(QString)'),
self.calculate)
self.initialize()
def initialize(self):
self.input_base_font_size.setValue(12.0)
self.input_font_size.setValue(12.0)
self.input_mapped_font_size.setText('0.0 pt')
self.output_base_font_size.setValue(self.default_base_font_size)
if self.default_font_key:
self.font_size_key.setText(self.default_font_key)
else:
self.font_size_key.setText('')
self.calculate()
def button_clicked(self, button):
if button is self.buttonBox.button(self.buttonBox.RestoreDefaults):
self.output_base_font_size.setValue(0.0)
self.font_size_key.setText('')
self.calculate()
def get_profile_values(self):
from calibre.ebooks.conversion.config import load_defaults
recs = load_defaults('page_setup')
pfname = recs.get('output_profile', 'default')
from calibre.customize.ui import output_profiles
for profile in output_profiles():
if profile.short_name == pfname:
break
dbase = profile.fbase
fsizes = profile.fkey
return dbase, fsizes
@property
def fsizes(self):
key = unicode(self.font_size_key.text()).strip()
return [float(x.strip()) for x in key.split(',') if x.strip()]
@property
def dbase(self):
return self.output_base_font_size.value()
def calculate(self, *args):
sbase = self.input_base_font_size.value()
dbase = self.dbase
fsize = self.input_font_size.value()
try:
fsizes = self.fsizes
except:
return
if dbase == 0.0 or not fsizes:
pd, pfs = self.get_profile_values()
if dbase == 0.0:
dbase = pd
if not fsizes:
fsizes = pfs
from calibre.ebooks.oeb.transforms.flatcss import KeyMapper
mapper = KeyMapper(sbase, dbase, fsizes)
msize = mapper[fsize]
self.input_mapped_font_size.setText('%.1f pt'%msize)
def use_default(self):
dbase, fsizes = self.get_profile_values()
self.output_base_font_size.setValue(dbase)
self.font_size_key.setText(', '.join(['%.1f'%x for x in fsizes]))
if __name__ == '__main__':
from PyQt4.Qt import QApplication
QApplication([])
d = FontKeyChooser()
d.exec_()

View File

@ -0,0 +1,226 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>536</width>
<height>554</height>
</rect>
</property>
<property name="windowTitle">
<string>Font rescaling wizard</string>
</property>
<property name="windowIcon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/wizard.svg</normaloff>:/images/wizard.svg</iconset>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="5" column="1" colspan="3">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::RestoreDefaults</set>
</property>
</widget>
</item>
<item row="0" column="0" colspan="4">
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;p&gt;This wizard will help you choose an appropriate font size key for your needs. Just enter the base font size of the input document and then enter an input font size. The wizard will display what font size it will be mapped to, by the font rescaling algorithm. You can adjust the algorithm by adjusting the output base font size and font key below. When you find values suitable for you, click OK.&lt;/p&gt;
&lt;p&gt;By default, if the output base font size is zero and/or no font size key is specified, calibre will use the values from the current Output Profile. &lt;/p&gt;
&lt;p&gt;See the &lt;a href=&quot;http://calibre.kovidgoyal.net/user_manual/conversion.html#font-size-rescaling&quot;&gt;User Manual&lt;/a&gt; for a discussion of how font size rescaling works.&lt;/p&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0" colspan="4">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>&amp;Output document</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>&amp;Base font size:</string>
</property>
<property name="buddy">
<cstring>output_base_font_size</cstring>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Font size &amp;key:</string>
</property>
<property name="buddy">
<cstring>font_size_key</cstring>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLineEdit" name="font_size_key"/>
</item>
<item row="0" column="2">
<widget class="QDoubleSpinBox" name="output_base_font_size">
<property name="suffix">
<string> pt</string>
</property>
<property name="decimals">
<number>1</number>
</property>
</widget>
</item>
<item row="2" column="0" colspan="3">
<widget class="QPushButton" name="button_use_default">
<property name="text">
<string>Use &amp;default values</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0" colspan="4">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>&amp;Input document</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>&amp;Base font size:</string>
</property>
<property name="buddy">
<cstring>input_base_font_size</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="input_base_font_size">
<property name="suffix">
<string> pt</string>
</property>
<property name="decimals">
<number>1</number>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>&amp;Font size: </string>
</property>
<property name="buddy">
<cstring>input_font_size</cstring>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="input_font_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>130</width>
<height>16777215</height>
</size>
</property>
<property name="suffix">
<string> pt</string>
</property>
<property name="decimals">
<number>1</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string> will map to size: </string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="input_mapped_font_size">
<property name="text">
<string>0.0 pt</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../../../../resources/images.qrc"/>
</resources>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -6,6 +6,7 @@ __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from PyQt4.Qt import SIGNAL
from calibre.gui2.convert.look_and_feel_ui import Ui_Form
from calibre.gui2.convert import Widget
@ -29,4 +30,16 @@ class LookAndFeelWidget(Widget, Ui_Form):
self.initialize_options(get_option, get_help, db, book_id)
self.opt_disable_font_rescaling.toggle()
self.opt_disable_font_rescaling.toggle()
self.connect(self.button_font_key, SIGNAL('clicked()'),
self.font_key_wizard)
def font_key_wizard(self):
from calibre.gui2.convert.font_key import FontKeyChooser
d = FontKeyChooser(self, self.opt_base_font_size.value(),
unicode(self.opt_font_size_mapping.text()).strip())
if d.exec_() == d.Accepted:
self.opt_font_size_mapping.setText(', '.join(['%.1f'%x for x in
d.fsizes]))
self.opt_base_font_size.setValue(d.dbase)

View File

@ -13,132 +13,150 @@
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_6">
<item row="1" column="0" colspan="2">
<widget class="QLabel" name="label_18">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="opt_disable_font_rescaling">
<property name="text">
<string>&amp;Disable font size rescaling</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_18">
<property name="text">
<string>Base &amp;font size:</string>
</property>
<property name="buddy">
<cstring>opt_base_font_size</cstring>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QDoubleSpinBox" name="opt_base_font_size">
<property name="suffix">
<string> pt</string>
</property>
<property name="decimals">
<number>1</number>
</property>
<property name="minimum">
<double>0.000000000000000</double>
</property>
<property name="maximum">
<double>30.000000000000000</double>
</property>
<property name="singleStep">
<double>1.000000000000000</double>
</property>
<property name="value">
<double>15.000000000000000</double>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Font size &amp;key:</string>
</property>
<property name="buddy">
<cstring>opt_font_size_mapping</cstring>
</property>
</widget>
</item>
<item row="2" column="2" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="opt_font_size_mapping">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="button_font_key">
<property name="toolTip">
<string>Wizard to help you choose an appropriate font size key</string>
</property>
<property name="text">
<string>Base &amp;font size:</string>
<string>...</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> pt</string>
</property>
<property name="decimals">
<number>1</number>
</property>
<property name="minimum">
<double>0.000000000000000</double>
</property>
<property name="maximum">
<double>30.000000000000000</double>
</property>
<property name="singleStep">
<double>1.000000000000000</double>
</property>
<property name="value">
<double>15.000000000000000</double>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QLabel" name="label">
<property name="text">
<string>Line &amp;height:</string>
</property>
<property name="buddy">
<cstring>opt_line_height</cstring>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QDoubleSpinBox" name="opt_line_height">
<property name="suffix">
<string> pt</string>
</property>
<property name="decimals">
<number>1</number>
</property>
</widget>
</item>
<item row="5" column="0" colspan="3">
<widget class="QCheckBox" name="opt_remove_paragraph_spacing">
<property name="text">
<string>Remove &amp;spacing between paragraphs</string>
</property>
</widget>
</item>
<item row="7" column="0" colspan="3">
<widget class="QCheckBox" name="opt_dont_justify">
<property name="text">
<string>No text &amp;justification</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="3">
<widget class="QCheckBox" name="opt_linearize_tables">
<property name="text">
<string>&amp;Linearize tables</string>
</property>
</widget>
</item>
<item row="9" column="0" colspan="3">
<widget class="QCheckBox" name="opt_asciiize">
<property name="text">
<string>&amp;Transliterate unicode characters to ASCII.</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Font size &amp;key:</string>
</property>
<property name="buddy">
<cstring>opt_font_size_mapping</cstring>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<widget class="QLineEdit" name="opt_font_size_mapping"/>
</item>
<item row="4" column="0" colspan="2">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Input character &amp;encoding</string>
</property>
<property name="buddy">
<cstring>opt_input_encoding</cstring>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QLineEdit" name="opt_input_encoding"/>
</item>
<item row="0" column="0" colspan="3">
<widget class="QCheckBox" name="opt_disable_font_rescaling">
<property name="text">
<string>&amp;Disable font size rescaling</string>
</property>
</widget>
</item>
<item row="6" column="0" colspan="3">
<widget class="QCheckBox" name="opt_insert_blank_line">
<property name="text">
<string>Insert &amp;blank line</string>
<property name="icon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/wizard.svg</normaloff>:/images/wizard.svg</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item>
<item row="3" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Line &amp;height:</string>
</property>
<property name="buddy">
<cstring>opt_line_height</cstring>
</property>
</widget>
</item>
<item row="3" column="3">
<widget class="QDoubleSpinBox" name="opt_line_height">
<property name="suffix">
<string> pt</string>
</property>
<property name="decimals">
<number>1</number>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Input character &amp;encoding:</string>
</property>
<property name="buddy">
<cstring>opt_input_encoding</cstring>
</property>
</widget>
</item>
<item row="5" column="0" colspan="3">
<widget class="QCheckBox" name="opt_remove_paragraph_spacing">
<property name="text">
<string>Remove &amp;spacing between paragraphs</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QCheckBox" name="opt_insert_blank_line">
<property name="text">
<string>Insert &amp;blank line</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QCheckBox" name="opt_dont_justify">
<property name="text">
<string>No text &amp;justification</string>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QCheckBox" name="opt_linearize_tables">
<property name="text">
<string>&amp;Linearize tables</string>
</property>
</widget>
</item>
<item row="9" column="0" colspan="3">
<widget class="QCheckBox" name="opt_asciiize">
<property name="text">
<string>&amp;Transliterate unicode characters to ASCII.</string>
</property>
</widget>
</item>
<item row="10" column="0" colspan="4">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Extra &amp;CSS</string>
@ -150,6 +168,9 @@
</layout>
</widget>
</item>
<item row="4" column="2" colspan="2">
<widget class="QLineEdit" name="opt_input_encoding"/>
</item>
</layout>
</widget>
<resources>