From 6846c27bd680ba37d2c2e71c43edab2499d31d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Corr=C3=AAa=20da=20Silva=20Sanches?= Date: Fri, 29 Nov 2024 19:23:25 -0300 Subject: [PATCH] Fix ERROR on hinting_impact check When running from a directory where the user lacks write permissions we were getting an ERROR. To fix the problem, we now use a better location for creating a temp-dir (and a temp-file) for this check. hinting_impact On the Universal profile (issue #4917) --- CHANGELOG.md | 1 + Lib/fontbakery/checks/hinting_impact.py | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 241c4c232d..2c54770e09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ A more detailed list of changes is available in the corresponding milestones for ### Changes to existing checks ### On the Universal profile + - **[[hinting_impact]]:** Fix ERROR when running from a directory where the user lacks write permissions. Use a better location for creating a temp-dir (and a temp-file) for this check. (issue #4917) - **[family/control_chars]:** renamed to **control_chars** as it is not realy a family-wide check. (issue #4896) - **[glyf_nested_components]** renamed to **nested_components**. diff --git a/Lib/fontbakery/checks/hinting_impact.py b/Lib/fontbakery/checks/hinting_impact.py index 1d9351d081..1d7f1e149b 100644 --- a/Lib/fontbakery/checks/hinting_impact.py +++ b/Lib/fontbakery/checks/hinting_impact.py @@ -1,19 +1,20 @@ +import os +from io import BytesIO +import tempfile + +from dehinter.font import dehint +from fontTools.subset import main as pyftsubset +from fontTools.ttLib import TTFont + from fontbakery.prelude import check, Message, INFO from fontbakery.testable import Font from fontbakery.utils import filesize_formatting def hinting_stats(font: Font): + """Return file size differences for a hinted font compared + to an dehinted version of same file. """ - Return file size differences for a hinted font compared to an dehinted version - of same file - """ - import os - from io import BytesIO - from dehinter.font import dehint - from fontTools.subset import main as pyftsubset - from fontTools.ttLib import TTFont - hinted_size = os.stat(font.file).st_size ttFont = TTFont(font.file) # Use our own copy since we will dehint it @@ -25,7 +26,9 @@ def hinting_stats(font: Font): dehinted_size = len(dehinted_buffer.read()) elif font.is_cff or font.is_cff2: ext = os.path.splitext(font.file)[1] - tmp = font.file.replace(ext, "-tmp-dehinted%s" % ext) + tmp_dir = tempfile.mkdtemp() + tmp = os.path.join(tmp_dir, "dehinted%s" % ext) + args = [ font.file, "--no-hinting", @@ -45,7 +48,7 @@ def hinting_stats(font: Font): dehinted_size = os.stat(tmp).st_size os.remove(tmp) - + os.rmdir(tmp_dir) else: return None