From 0c5959f2983cd863ee87dd956b4881c1ba077003 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 18 Jul 2013 10:03:30 +0530 Subject: [PATCH] PDF Output: Fix anchors sometimes located incorrectly PDF Output: Workaround bug in WebKit's getBoundingClientRect() method that could cause links to occasionally point to incorrect locations. Fixes #1202390 [Private bug](https://bugs.launchpad.net/calibre/+bug/1202390) --- resources/compiled_coffeescript.zip | Bin 71983 -> 72211 bytes src/calibre/ebooks/oeb/display/paged.coffee | 16 ++++++++++++++-- src/calibre/ebooks/pdf/render/from_html.py | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index 3c5f0e80c6f57fd0b309fa8ea039fcfcd3f1e310..ddf0db08e77d28f55c7eb8344a7899e938a18244 100644 GIT binary patch delta 260 zcmZ3#iDmK@7U=+QW)=|!5XkcSSSJZ^~onK1zF>ZQqv44_v`CVw#}3V zit=(AMjOQHD3s(EOpbXVy}8f&j~w$#*H4>!qkin>L$!DMWHCl-kmU4>VvN#EDbbTR zR*6n$7H2eNT2csR`-?MLFjZCq+49p@h%;(2m9~P#Uy3stF{wc9=AsQIgS} psdXk;eY_;20n^l9(#B%>@_r5Gd79stY?N45X} diff --git a/src/calibre/ebooks/oeb/display/paged.coffee b/src/calibre/ebooks/oeb/display/paged.coffee index f97f1b3cf8..39c4b12fe9 100644 --- a/src/calibre/ebooks/oeb/display/paged.coffee +++ b/src/calibre/ebooks/oeb/display/paged.coffee @@ -294,9 +294,21 @@ class PagedDisplay return Math.floor(xpos/this.page_width) column_location: (elem) -> - # Return the location of elem relative to its containing column + # Return the location of elem relative to its containing column. + # WARNING: This method may cause the viewport to scroll (to workaround + # a bug in WebKit). br = elem.getBoundingClientRect() - [left, top] = calibre_utils.viewport_to_document(br.left, br.top, elem.ownerDocument) + # Because of a bug in WebKit's getBoundingClientRect() in column + # mode, this position can be inaccurate, see + # https://bugs.launchpad.net/calibre/+bug/1202390 for a test case. + # The usual symptom of the inaccuracy is br.top is highly negative. + if br.top < -100 + # We have to actually scroll the element into view to get its + # position + elem.scrollIntoView() + [left, top] = calibre_utils.viewport_to_document(elem.scrollLeft, elem.scrollTop, elem.ownerDocument) + else + [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 diff --git a/src/calibre/ebooks/pdf/render/from_html.py b/src/calibre/ebooks/pdf/render/from_html.py index 8ea1d8203e..771cc33ede 100644 --- a/src/calibre/ebooks/pdf/render/from_html.py +++ b/src/calibre/ebooks/pdf/render/from_html.py @@ -353,6 +353,7 @@ class PDFWriter(QObject): paged_display.layout(); paged_display.fit_images(); py_bridge.value = book_indexing.all_links_and_anchors(); + window.scrollTo(0, 0); // This is needed as getting anchor positions could have caused the viewport to scroll '''%(self.margin_top, 0, self.margin_bottom)) amap = self.bridge_value