diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index 78ef638abc..6aa473571b 100644 Binary files a/resources/compiled_coffeescript.zip and b/resources/compiled_coffeescript.zip differ 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) -