From 8b61d2d5214ca236ea4bef182901f7147456b626 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Tue, 30 Oct 2018 14:40:53 -0700 Subject: [PATCH] pdfminer: If font descent claims to be positive, treat it as negative --- src/ocrmypdf/pdfinfo/layout.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ocrmypdf/pdfinfo/layout.py b/src/ocrmypdf/pdfinfo/layout.py index edf48935..4a393004 100644 --- a/src/ocrmypdf/pdfinfo/layout.py +++ b/src/ocrmypdf/pdfinfo/layout.py @@ -28,7 +28,7 @@ from pdfminer.layout import ( LTChar, LTContainer, LTLayoutContainer, LTPage, LTTextLine, LAParams, LTTextBox ) -from pdfminer.pdffont import PDFUnicodeNotDefined, PDFType3Font +from pdfminer.pdffont import PDFUnicodeNotDefined, PDFType3Font, PDFFont, PDFCIDFont from pdfminer.pdfpage import PDFPage from pdfminer.utils import matrix2str, bbox2str, fsplit @@ -59,6 +59,22 @@ PDFType3Font.get_height = PDFType3Font__get_height PDFType3Font.get_ascent = PDFType3Font__get_ascent PDFType3Font.get_descent = PDFType3Font__get_descent + +original_PDFFont_init = PDFFont.__init__ +def PDFFont__init__(self, descriptor, widths, default_width=None): + original_PDFFont_init(self, descriptor, widths, default_width) + # PDF spec says descent should be negative + # A font with a positive descent implies it floats entirely above the + # baseline, i.e. it's not really a baseline anymore. I have fonts that + # claim a positive descent, but treating descent as positive always seems + # to misposition text. + if self.descent > 0: + self.descent = -self.descent + +PDFFont.__init__ = PDFFont__init__ + + + class LTStateAwareChar(LTChar): """A subclass of LTChar that tracks text render mode at time of drawing"""