PDF Output: Add an option to break long words at the ends of lines.

Fixes #1773111 [add hyphenation to ePub-to-PDF conversion](https://bugs.launchpad.net/calibre/+bug/1773111)
This commit is contained in:
Kovid Goyal 2018-05-24 14:02:32 +05:30
parent c6c689f9e3
commit 4fc540bdaa
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 55 additions and 20 deletions

View File

@ -94,6 +94,8 @@ class PDFOutput(OutputFormatPlugin):
OptionRecommendation(name='pdf_mono_font_size',
recommended_value=16, help=_(
'The default font size for monospaced text')),
OptionRecommendation(name='pdf_hyphenate', recommended_value=False,
help=_('Break long words at the end of lines. This can give the text at the right margin a more even appearance.')),
OptionRecommendation(name='pdf_mark_links', recommended_value=False,
help=_('Surround all links with a red box, useful for debugging.')),
OptionRecommendation(name='uncompressed_pdf',
@ -184,8 +186,7 @@ class PDFOutput(OutputFormatPlugin):
def get_cover_data(self):
oeb = self.oeb
if (oeb.metadata.cover and
unicode(oeb.metadata.cover[0]) in oeb.manifest.ids):
if (oeb.metadata.cover and unicode(oeb.metadata.cover[0]) in oeb.manifest.ids):
cover_id = unicode(oeb.metadata.cover[0])
item = oeb.manifest.ids[cover_id]
self.cover_data = item.data

View File

@ -24,6 +24,7 @@ from calibre.ebooks.pdf.render.common import (inch, cm, mm, pica, cicero,
didot, PAPER_SIZES, current_log)
from calibre.ebooks.pdf.render.engine import PdfDevice
from calibre.ptempfile import PersistentTemporaryFile
from calibre.utils.resources import load_hyphenator_dicts
def get_page_size(opts, for_comic=False): # {{{
@ -213,6 +214,7 @@ class PDFWriter(QObject):
self.margin_top, self.margin_bottom = map(lambda x:int(floor(x)), (mt, mb))
self.painter = QPainter(self.doc)
self.book_language = pdf_metadata.mi.languages[0]
self.doc.set_metadata(title=pdf_metadata.title,
author=pdf_metadata.author,
tags=pdf_metadata.tags, mi=pdf_metadata.mi)
@ -344,6 +346,25 @@ class PDFWriter(QObject):
return sections
def hyphenate(self, evaljs):
evaljs(u'''\
Hyphenator.config(
{
'minwordlength' : 6,
// 'hyphenchar' : '|',
'displaytogglebox' : false,
'remoteloading' : false,
'doframes' : true,
'defaultlanguage' : 'en',
'storagetype' : 'session',
'onerrorhandler' : function (e) {
console.log(e);
}
});
Hyphenator.hyphenate(document.body, "%s");
''' % self.hyphenate_lang
)
def do_paged_render(self):
if self.paged_js is None:
import uuid
@ -352,6 +373,10 @@ class PDFWriter(QObject):
self.paged_js += cc('ebooks.oeb.display.indexing')
self.paged_js += cc('ebooks.oeb.display.paged')
self.paged_js += cc('ebooks.oeb.display.mathjax')
if self.opts.pdf_hyphenate:
self.paged_js += P('viewer/hyphenate/Hyphenator.js', data=True).decode('utf-8')
hjs, self.hyphenate_lang = load_hyphenator_dicts({}, self.book_language)
self.paged_js += hjs
self.hf_uuid = str(uuid.uuid4()).replace('-', '')
self.view.page().mainFrame().addToJavaScriptWindowObject("py_bridge", self)
@ -359,6 +384,8 @@ class PDFWriter(QObject):
evaljs = self.view.page().mainFrame().evaluateJavaScript
evaljs(self.paged_js)
self.load_mathjax()
if self.opts.pdf_hyphenate:
self.hyphenate(evaljs)
amap = json.loads(evaljs('''
document.body.style.backgroundColor = "white";

View File

@ -24,7 +24,7 @@ class PluginWidget(Widget, Ui_Form):
def __init__(self, parent, get_option, get_help, db=None, book_id=None):
Widget.__init__(self, parent, [
'use_profile_size', 'paper_size', 'custom_size',
'use_profile_size', 'paper_size', 'custom_size', 'pdf_hyphenate',
'preserve_cover_aspect_ratio', 'pdf_serif_family', 'unit',
'pdf_sans_family', 'pdf_mono_family', 'pdf_standard_font',
'pdf_default_font_size', 'pdf_mono_font_size', 'pdf_page_numbers',

View File

@ -91,14 +91,14 @@
</property>
</widget>
</item>
<item row="6" column="0" colspan="2">
<item row="7" column="0" colspan="2">
<widget class="QCheckBox" name="opt_pdf_add_toc">
<property name="text">
<string>Add a printable &amp;Table of Contents at the end</string>
</property>
</widget>
</item>
<item row="7" column="0">
<item row="8" column="0">
<widget class="QLabel" name="label_14">
<property name="text">
<string>&amp;Title for ToC:</string>
@ -108,10 +108,10 @@
</property>
</widget>
</item>
<item row="7" column="1">
<item row="8" column="1">
<widget class="QLineEdit" name="opt_toc_title"/>
</item>
<item row="9" column="0">
<item row="10" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Serif famil&amp;y:</string>
@ -121,10 +121,10 @@
</property>
</widget>
</item>
<item row="9" column="1">
<item row="10" column="1">
<widget class="QFontComboBox" name="opt_pdf_serif_family"/>
</item>
<item row="10" column="0">
<item row="11" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Sans fami&amp;ly:</string>
@ -134,10 +134,10 @@
</property>
</widget>
</item>
<item row="10" column="1">
<item row="11" column="1">
<widget class="QFontComboBox" name="opt_pdf_sans_family"/>
</item>
<item row="11" column="0">
<item row="12" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>&amp;Monospace family:</string>
@ -147,10 +147,10 @@
</property>
</widget>
</item>
<item row="11" column="1">
<item row="12" column="1">
<widget class="QFontComboBox" name="opt_pdf_mono_family"/>
</item>
<item row="12" column="0">
<item row="13" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>S&amp;tandard font:</string>
@ -160,10 +160,10 @@
</property>
</widget>
</item>
<item row="12" column="1">
<item row="13" column="1">
<widget class="QComboBox" name="opt_pdf_standard_font"/>
</item>
<item row="13" column="0">
<item row="14" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Default font si&amp;ze:</string>
@ -173,14 +173,14 @@
</property>
</widget>
</item>
<item row="13" column="1">
<item row="14" column="1">
<widget class="QSpinBox" name="opt_pdf_default_font_size">
<property name="suffix">
<string> px</string>
</property>
</widget>
</item>
<item row="14" column="0">
<item row="15" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Monospace &amp;font size:</string>
@ -190,21 +190,21 @@
</property>
</widget>
</item>
<item row="14" column="1">
<item row="15" column="1">
<widget class="QSpinBox" name="opt_pdf_mono_font_size">
<property name="suffix">
<string> px</string>
</property>
</widget>
</item>
<item row="15" column="0" colspan="2">
<item row="16" column="0" colspan="2">
<widget class="QGroupBox" name="page_margins_box">
<property name="title">
<string>Page margins</string>
</property>
</widget>
</item>
<item row="16" column="0" colspan="2">
<item row="17" column="0" colspan="2">
<widget class="QGroupBox" name="template_box">
<property name="title">
<string>Page headers and footers</string>
@ -252,6 +252,13 @@
</layout>
</widget>
</item>
<item row="6" column="0" colspan="2">
<widget class="QCheckBox" name="opt_pdf_hyphenate">
<property name="text">
<string>&amp;Break long words at the end of lines</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>