From 9fd9c7a51fb37c0ebf5d5f0f69ca58832c775ec0 Mon Sep 17 00:00:00 2001 From: Tucker Barbour Date: Fri, 2 Mar 2018 11:13:47 +0000 Subject: [PATCH] Scale BoundingBox and Text elements to account for additional space. Here we are manually scaling the pt width used for the BoundingBox and the Text element when manually adding whitespace to account for limitations of the PDF.js viewer. This fixes an initial regression noticed when selecting text elements in Chrome and PDFium. The width of the Text element and BoundBox had not been adjusted for the additional whitespace so the highlighting was offset slightly. --- ocrmypdf/hocrtransform.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ocrmypdf/hocrtransform.py b/ocrmypdf/hocrtransform.py index 244eec0e..dd772391 100755 --- a/ocrmypdf/hocrtransform.py +++ b/ocrmypdf/hocrtransform.py @@ -196,14 +196,16 @@ class HocrTransform(): if len(elemtxt) == 0: continue + pxl_coords = self.element_coordinates(elem) + pt = self.pt_from_pixel(pxl_coords) + # if the advanced option `--interword-spaces` is true, append a space # to the end of each text element to allow simpler PDF viewers such # as PDF.js to better recognize words in search and copy and paste if interwordSpaces: elemtxt += ' ' - - pxl_coords = self.element_coordinates(elem) - pt = self.pt_from_pixel(pxl_coords) + pt = Rect._make((pt.x1, pt.y1, + pt.x2 + pdf.stringWidth(' ', fontname, pt.y2 - pt.y1), pt.y2)) # draw the bbox border if showBoundingboxes: