Start work on links in the new pdf output engine

This commit is contained in:
Kovid Goyal 2012-12-28 00:01:49 +05:30
parent 0ad7ce0b27
commit dd73b97b6c
2 changed files with 38 additions and 1 deletions

View File

@ -17,9 +17,11 @@ from PyQt4.QtWebKit import QWebView, QWebPage, QWebSettings
from calibre import fit_image
from calibre.ebooks.oeb.display.webview import load_html
from calibre.ebooks.pdf.render.engine import PdfDevice
from calibre.ebooks.pdf.render.common import (inch, cm, mm, pica, cicero,
didot, PAPER_SIZES)
from calibre.ebooks.pdf.render.engine import PdfDevice
from calibre.ebooks.pdf.render.links import Links
def get_page_size(opts, for_comic=False): # {{{
use_profile = not (opts.override_profile_size or
@ -141,6 +143,7 @@ class PDFWriter(QObject):
self.view.page().mainFrame().setScrollBarPolicy(x,
Qt.ScrollBarAlwaysOff)
self.report_progress = lambda x, y: x
self.links = Links()
def dump(self, items, out_stream, pdf_metadata):
opts = self.opts
@ -258,6 +261,8 @@ class PDFWriter(QObject):
amap = self.bridge_value
if not isinstance(amap, dict):
amap = {'links':[], 'anchors':{}} # Some javascript error occurred
self.links.add(self.current_item, self.current_page_num, amap['links'],
amap['anchors'])
mf = self.view.page().mainFrame()
while True:

View File

@ -0,0 +1,32 @@
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:fdm=marker:ai
from __future__ import (unicode_literals, division, absolute_import,
print_function)
__license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import os
from calibre.ebooks.pdf.render.common import Array, Name
class Destination(Array):
def __init__(self, start_page, pos):
super(Destination, self).__init__(
[start_page + pos['column'], Name('FitH'), pos['y']])
class Links(object):
def __init__(self):
self.anchors = {}
def add(self, base_path, start_page, links, anchors):
path = os.path.normcase(os.path.abspath(base_path))
self.anchors[path] = a = {}
a[None] = Destination(start_page, {'y':0, 'column':0})
for anchor, pos in anchors.iteritems():
a[anchor] = Destination(start_page, pos)