From cfd76b43e8da87a7bfcb4b32f4b6b2a7e6f4cadc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 27 Dec 2012 09:39:24 +0530 Subject: [PATCH] Start work on links/outlines in the new engine --- resources/compiled_coffeescript.zip | Bin 60149 -> 62268 bytes .../ebooks/oeb/display/indexing.coffee | 25 ++++++++++++++++++ src/calibre/ebooks/oeb/display/paged.coffee | 12 +++++++++ src/calibre/ebooks/pdf/render/from_html.py | 22 +++++---------- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index 78ef638abc3a6d6b7fbe25553ed0ef0de300daa3..6aa473571b31f20ea80363634ed0238cd0a0b316 100644 GIT binary patch delta 1639 zcmZ`)UuYC(5Wifm{t2R3lNx&=Ob(mfO*fn5qEJl^nqol-8YP7*9AWuxckga-Z};5n z5mQaCr7wLd#jo|Dpwbsne2NkBU@7=mp-2Nlp%hxE#mkHJp)Y+9XTIIN%SF4ed$Tk1 z&HQHOH+%m+?*8HL?)2H&?o@AruHV1;Zf3on`t|B;B0+chN^@}a*bS8*o$Tdzm*13% zz6yp470WJH0u>yqgwMdMsjy_5K}7}Kw@Onz{9t}8MIwJVG{)V0x)J(9PB;|C7sRRf zbz)(v2gGHdDyZ0tn&CP1s;jd=rW{Ia7g^BfGz%(P)pq5gulXSs*?@OpLV=MmK#i3x zUAQYS3Jvt5Kye(q1a!3M1%X$EX+RmPP~cL814t8Db6A^edP|z^x|V-Zlpe=fiTuNc zYy%dar`EV5+s+fFFlQWz(7|X948HG3Ki_z8z>~rrNXN0`Em#uyp^ARCXc0UrX%KBs zQ9NqH$+Yz$Ry2e{WLV){lf!Ep>fFHIZ;=J}&eguW-j_&R?^_!?b#)(4&i5a19Nn?q z^NTujO}veY=kq_+^fXf;d|OnZ=$Xs-M4FT>uZm1T5~yICF;_$Y6<#?$S!!CE^q+OwXuSEjQ3E)GdE`-f}D>@cbFakuwAUSb_2)$;4a1 zHe-<$DDy(5MW~YJgRI-YKRdUF*FH$`A3s0b#rX3lhjz%6Y=Ym+OI;KE=lnYP()4U~Q&WQ-L(yscTyuQH*Z^dQA_K-!6ofl>Yf4yIddXcIoBCa(>>#qUc!A5k+A z1JlY@FcSp6U91O|ERkAjbrOJaA!)yu$ha5Rc=Mki%<0L%c>INn_w_W023? z+Qt9K?&QBc=*dc=qx21+W!$pxQPF6LqAor}Lw*4U1`w75sZrR>?Nh`*x&H(EX5mE934EvqO*Z>zJ$b<=78z81U@M`7 zxF^sQFQ%kOu!Xl0`6hkRVlqz#^VVl`P4)(IjdQ`=o=-+hkE+1TtDg**KHUa0Wj;%> JU3~#E4FIL!O(XyS diff --git a/src/calibre/ebooks/oeb/display/indexing.coffee b/src/calibre/ebooks/oeb/display/indexing.coffee index 48f0697506..cd4a5a83e5 100644 --- a/src/calibre/ebooks/oeb/display/indexing.coffee +++ b/src/calibre/ebooks/oeb/display/indexing.coffee @@ -92,6 +92,31 @@ class BookIndexing this.last_check = [body.scrollWidth, body.scrollHeight] return ans + all_links_and_anchors: () -> + body = document.body + links = [] + anchors = {} + for a in document.querySelectorAll("body a[href], body [id], body a[name]") + if window.paged_display?.in_paged_mode + geom = window.paged_display.column_location(a) + else + br = a.getBoundingClientRect() + [left, top] = viewport_to_document(br.left, br.top, a.ownerDocument) + geom = {'left':left, 'top':top, 'width':br.right-br.left, 'height':br.bottom-br.top} + + href = a.getAttribute('href') + if href + links.push([href, geom]) + id = a.getAttribute("id") + if id and id not in anchors + anchors[id] = geom + if a.tagName in ['A', "a"] + name = a.getAttribute("name") + if name and name not in anchors + anchors[name] = geom + + return {'links':links, 'anchors':anchors} + if window? window.book_indexing = new BookIndexing() diff --git a/src/calibre/ebooks/oeb/display/paged.coffee b/src/calibre/ebooks/oeb/display/paged.coffee index c5f2dbc97a..aea51b5b23 100644 --- a/src/calibre/ebooks/oeb/display/paged.coffee +++ b/src/calibre/ebooks/oeb/display/paged.coffee @@ -242,6 +242,18 @@ class PagedDisplay # Return the number of the column that contains xpos return Math.floor(xpos/this.page_width) + column_location: (elem) -> + # Return the location of elem relative to its containing column + br = elem.getBoundingClientRect() + [left, top] = calibre_utils.viewport_to_document(br.left, br.top, elem.ownerDocument) + c = this.column_at(left) + width = Math.min(br.right, (c+1)*this.page_width) - br.left + if br.bottom < br.top + br.bottom = window.innerHeight + height = Math.min(br.bottom, window.innerHeight) - br.top + left -= c*this.page_width + return {'column':c, 'left':left, 'top':top, 'width':width, 'height':height} + column_boundaries: () -> # Return the column numbers at the left edge and after the right edge # of the viewport diff --git a/src/calibre/ebooks/pdf/render/from_html.py b/src/calibre/ebooks/pdf/render/from_html.py index 46017cf913..2c3652ae11 100644 --- a/src/calibre/ebooks/pdf/render/from_html.py +++ b/src/calibre/ebooks/pdf/render/from_html.py @@ -20,7 +20,6 @@ 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.outline_writer import Outline def get_page_size(opts, for_comic=False): # {{{ use_profile = not (opts.override_profile_size or @@ -145,7 +144,6 @@ class PDFWriter(QObject): def dump(self, items, out_stream, pdf_metadata): opts = self.opts - self.outline = Outline(self.toc, items) page_size = get_page_size(self.opts) xdpi, ydpi = self.view.logicalDpiX(), self.view.logicalDpiY() ml, mr = opts.margin_left, opts.margin_right @@ -254,11 +252,14 @@ class PDFWriter(QObject): paged_display.set_geometry(1, %d, %d, %d); paged_display.layout(); paged_display.fit_images(); + py_bridge.value = book_indexing.all_links_and_anchors(); '''%(self.margin_top, self.margin_size, self.margin_bottom)) + amap = self.bridge_value + if not isinstance(amap, dict): + amap = {'links':[], 'anchors':{}} # Some javascript error occurred + mf = self.view.page().mainFrame() - start_page = self.current_page_num - dx = 0 while True: self.doc.init_page() self.painter.save() @@ -268,18 +269,7 @@ class PDFWriter(QObject): self.doc.end_page() if not nsl[1] or nsl[0] <= 0: break - dx = nsl[0] - evaljs('window.scrollTo(%d, 0)'%dx) + evaljs('window.scrollTo(%d, 0)'%nsl[0]) if self.doc.errors_occurred: break - self.bridge_value = tuple(self.outline.anchor_map[self.current_item]) - evaljs('py_bridge.value = book_indexing.anchor_positions(py_bridge.value)') - amap = self.bridge_value - if not isinstance(amap, dict): - amap = {} # Some javascript error occurred - self.outline.set_pos(self.current_item, None, start_page, 0) - for anchor, x in amap.iteritems(): - pagenum, ypos = x - self.outline.set_pos(self.current_item, anchor, start_page + pagenum, ypos) -