Add option to render text as invisible OCR text
Prior to this change, hocrtransform would render printable text (black on white) and then a fully opaque image on top of the text. According to the PDF spec, text that is the output of OCR should be marked invisible, so that PDF viewers /know/ it's OCR output in a document that might mix OCR and text overlays. Another benefit is that PDF viewers would know to skip rendering text if they are not smart enough to figure out the image will completely overwrite it. However, for debug, visible text is nice, so retain it as an option.
This commit is contained in:
@@ -50,8 +50,9 @@ class HocrTransform():
|
||||
for div in self.hocr.findall(
|
||||
".//%sdiv[@class='ocr_page']" % (self.xmlns)):
|
||||
coords = self.element_coordinates(div)
|
||||
self.width = self.px2pt(coords[2] - coords[0])
|
||||
self.height = self.px2pt(coords[3] - coords[1])
|
||||
pt_coords = self.pt_from_pixel(coords)
|
||||
self.width = pt_coords.x2 - pt_coords.x1
|
||||
self.height = pt_coords.y2 - pt_coords.y1
|
||||
# there shouldn't be more than one, and if there is, we don't want
|
||||
# it
|
||||
break
|
||||
@@ -116,7 +117,7 @@ class HocrTransform():
|
||||
return s
|
||||
|
||||
def to_pdf(self, outFileName, imageFileName=None, showBoundingboxes=False,
|
||||
fontname="Helvetica"):
|
||||
fontname="Helvetica", invisibleText=False):
|
||||
"""
|
||||
Creates a PDF file with an image superimposed on top of the text.
|
||||
Text is positioned according to the bounding box of the lines in
|
||||
@@ -187,6 +188,8 @@ class HocrTransform():
|
||||
text = pdf.beginText()
|
||||
fontsize = pt.y2 - pt.y1
|
||||
text.setFont(fontname, fontsize)
|
||||
if invisibleText:
|
||||
text.setTextRenderMode(3) # Invisible (indicates OCR text)
|
||||
|
||||
# set cursor to bottom left corner of bbox (adjust for dpi)
|
||||
text.setTextOrigin(pt.x1, self.height - pt.y2)
|
||||
|
||||
+2
-2
@@ -577,7 +577,7 @@ def render_page(infiles, output_file):
|
||||
|
||||
hocrtransform = HocrTransform(hocr, dpi)
|
||||
hocrtransform.to_pdf(output_file, imageFileName=image,
|
||||
showBoundingboxes=False)
|
||||
showBoundingboxes=False, invisibleText=True)
|
||||
|
||||
|
||||
@active_if(ocr_required and options.pdf_noimg)
|
||||
@@ -587,7 +587,7 @@ def render_text_output_page(input_file, output_file):
|
||||
|
||||
hocrtransform = HocrTransform(input_file, dpi)
|
||||
hocrtransform.to_pdf(output_file, imageFileName=None,
|
||||
showBoundingboxes=True)
|
||||
showBoundingboxes=True, invisibleText=False)
|
||||
|
||||
|
||||
@merge([render_page, skip_ocr],
|
||||
|
||||
Reference in New Issue
Block a user