From 29e2645f4e21b02a7ddf111cd32c55eeeb1aa610 Mon Sep 17 00:00:00 2001 From: Julien Van Roy Date: Thu, 13 Apr 2023 11:16:48 +0000 Subject: [PATCH] [FIX] tools: support recent versions of fonttools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Support recent version of fonttools. In particular, class `_TTGlyphSet` was refactored between 4.37.1 and 4.37.2 and no longer has an `_htmx` attribute. Instead, the new attribute `hMetrics` can be used to get the metrics from htmx. closes odoo/odoo#118571 See: https://github.com/fonttools/fonttools/commit/b818e1494ff2bfb7f0cd71d827ba97578c919303 Signed-off-by: William André (wan) --- odoo/tools/pdf.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/odoo/tools/pdf.py b/odoo/tools/pdf.py index cdd2e359af7..cef9e95de7e 100644 --- a/odoo/tools/pdf.py +++ b/odoo/tools/pdf.py @@ -12,6 +12,7 @@ from reportlab.lib import colors from reportlab.lib.units import cm from reportlab.lib.utils import ImageReader from reportlab.pdfgen import canvas +from odoo.tools.parse_version import parse_version try: # class were renamed in PyPDF2 > 2.0 @@ -36,6 +37,7 @@ except ImportError: from PyPDF2.generic import DictionaryObject, NameObject, ArrayObject, DecodedStreamObject, NumberObject, createStringObject, ByteStringObject try: + import fontTools from fontTools.ttLib import TTFont except ImportError: TTFont = None @@ -369,7 +371,10 @@ class OdooPdfFileWriter(PdfFileWriter): stream = io.BytesIO(decompress(font_file._data)) ttfont = TTFont(stream) font_upm = ttfont['head'].unitsPerEm - glyphs = ttfont.getGlyphSet()._hmtx.metrics + if parse_version(fontTools.__version__) < parse_version('4.37.2'): + glyphs = ttfont.getGlyphSet()._hmtx.metrics + else: + glyphs = ttfont.getGlyphSet().hMetrics glyph_widths = [] for key, values in glyphs.items(): if key[:5] == 'glyph':