From 33d12e9406617490cedaa27bd6d0413d3c05dad7 Mon Sep 17 00:00:00 2001 From: Miguel Sousa Date: Fri, 3 Mar 2017 14:49:11 -0800 Subject: [PATCH 1/7] =?UTF-8?q?[varLib.mutator]=20Change=20=E2=80=98GX?= =?UTF-8?q?=E2=80=99=20to=20=E2=80=98variable=E2=80=99.=20Include=20HVAR?= =?UTF-8?q?=20in=20the=20list=20of=20tables=20to=20remove.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/fontTools/varLib/mutator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/fontTools/varLib/mutator.py b/Lib/fontTools/varLib/mutator.py index 82da45d21..638a2ffaf 100644 --- a/Lib/fontTools/varLib/mutator.py +++ b/Lib/fontTools/varLib/mutator.py @@ -1,7 +1,7 @@ """ Instantiate a variation font. Run, eg: -$ python mutator.py ./NotoSansArabic-GX.ttf wght=140 wdth=85 +$ python mutator.py ./NotoSansArabic-VF.ttf wght=140 wdth=85 """ from __future__ import print_function, division, absolute_import from fontTools.misc.py23 import * @@ -28,7 +28,7 @@ def main(args=None): loc[tag.ljust(4)] = float(val) print("Location:", loc) - print("Loading GX font") + print("Loading variable font") varfont = TTFont(varfilename) fvar = varfont['fvar'] @@ -48,8 +48,8 @@ def main(args=None): coordinates += GlyphCoordinates(var.coordinates) * scalar _SetCoordinates(varfont, glyphname, coordinates) - print("Removing GX tables") - for tag in ('fvar','avar','gvar'): + print("Removing variable tables") + for tag in ('fvar','avar','gvar','HVAR'): if tag in varfont: del varfont[tag] From e01c52244d18181a82795a322dc2596978427d20 Mon Sep 17 00:00:00 2001 From: Miguel Sousa Date: Fri, 3 Mar 2017 14:54:40 -0800 Subject: [PATCH 2/7] [varLib.mutator] Test case and test result --- Tests/varLib/data/MutatorTest.ttx | 668 ++++++++++++++++++++++++++++++ Tests/varLib/mutator_test.py | 113 +++++ 2 files changed, 781 insertions(+) create mode 100644 Tests/varLib/data/MutatorTest.ttx create mode 100644 Tests/varLib/mutator_test.py diff --git a/Tests/varLib/data/MutatorTest.ttx b/Tests/varLib/data/MutatorTest.ttx new file mode 100644 index 000000000..e5786f50b --- /dev/null +++ b/Tests/varLib/data/MutatorTest.ttx @@ -0,0 +1,668 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Weight + + + Contrast + + + ExtraLight + + + TestFamily-ExtraLight + + + Light + + + TestFamily-Light + + + Regular + + + TestFamily-Regular + + + Semibold + + + TestFamily-Semibold + + + Bold + + + TestFamily-Bold + + + Black + + + TestFamily-Black + + + Black Medium Contrast + + + TestFamily-BlackMediumContrast + + + Black High Contrast + + + TestFamily-BlackHighContrast + + + Test Family + + + Regular + + + Version 1.001;ADBO;Test Family Regular + + + Test Family + + + Version 1.001 + + + TestFamily-Master1 + + + Frank Grießhammer + + + Master 1 + + + Weight + + + Contrast + + + ExtraLight + + + TestFamily-ExtraLight + + + Light + + + TestFamily-Light + + + Regular + + + TestFamily-Regular + + + Semibold + + + TestFamily-Semibold + + + Bold + + + TestFamily-Bold + + + Black + + + TestFamily-Black + + + Black Medium Contrast + + + TestFamily-BlackMediumContrast + + + Black High Contrast + + + TestFamily-BlackHighContrast + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/varLib/mutator_test.py b/Tests/varLib/mutator_test.py new file mode 100644 index 000000000..3da5d1a03 --- /dev/null +++ b/Tests/varLib/mutator_test.py @@ -0,0 +1,113 @@ +from __future__ import print_function, division, absolute_import +from fontTools.misc.py23 import * +from fontTools.ttLib import TTFont +from fontTools.varLib import build +from fontTools.varLib.mutator import main as mutator +import difflib +import os +import shutil +import sys +import tempfile +import unittest + + +class MutatorTest(unittest.TestCase): + def __init__(self, methodName): + unittest.TestCase.__init__(self, methodName) + # Python 3 renamed assertRaisesRegexp to assertRaisesRegex, + # and fires deprecation warnings if a program uses the old name. + if not hasattr(self, "assertRaisesRegex"): + self.assertRaisesRegex = self.assertRaisesRegexp + + def setUp(self): + self.tempdir = None + self.num_tempfiles = 0 + + def tearDown(self): + if self.tempdir: + shutil.rmtree(self.tempdir) + + @staticmethod + def get_test_input(test_file_or_folder): + path, _ = os.path.split(__file__) + return os.path.join(path, "data", test_file_or_folder) + + @staticmethod + def get_file_list(folder, suffix): + all_files = os.listdir(folder) + return [os.path.abspath(os.path.join(folder, p)) for p in all_files + if p.endswith(suffix)] + + def temp_path(self, suffix): + self.temp_dir() + self.num_tempfiles += 1 + return os.path.join(self.tempdir, + "tmp%d%s" % (self.num_tempfiles, suffix)) + + def temp_dir(self): + if not self.tempdir: + self.tempdir = tempfile.mkdtemp() + + def read_ttx(self, path): + lines = [] + with open(path, "r", encoding="utf-8") as ttx: + for line in ttx.readlines(): + # Elide ttFont attributes because ttLibVersion may change, + # and use os-native line separators so we can run difflib. + if line.startswith("" + os.linesep) + else: + lines.append(line.rstrip() + os.linesep) + return lines + + def expect_ttx(self, font, expected_ttx, tables): + path = self.temp_path(suffix=".ttx") + font.saveXML(path, tables=tables) + actual = self.read_ttx(path) + expected = self.read_ttx(expected_ttx) + if actual != expected: + for line in difflib.unified_diff( + expected, actual, fromfile=expected_ttx, tofile=path): + sys.stdout.write(line) + self.fail("TTX output is different from expected") + + def compile_font(self, path, suffix, temp_dir): + ttx_filename = os.path.basename(path) + savepath = os.path.join(temp_dir, ttx_filename.replace('.ttx', suffix)) + font = TTFont(recalcBBoxes=False, recalcTimestamp=False) + font.importXML(path) + font.save(savepath, reorderTables=None) + return font, savepath + +# ----- +# Tests +# ----- + + def test_varlib_mutator_ttf(self): + suffix = '.ttf' + ds_path = self.get_test_input('BuildTest.designspace') + ufo_dir = self.get_test_input('master_ufo') + ttx_dir = self.get_test_input('master_ttx_interpolatable_ttf') + + ttx_paths = self.get_file_list(ttx_dir, '.ttx') + self.temp_dir() + for path in ttx_paths: + self.compile_font(path, suffix, self.tempdir) + + finder = lambda s: s.replace(ufo_dir, self.tempdir).replace('.ufo', suffix) + varfont, _, _ = build(ds_path, finder) + varfont_path = os.path.join(self.tempdir, 'MutatorTest' + suffix) + varfont.save(varfont_path) + + args = [varfont_path, 'wght=500', 'cntr=50'] + mutator(args) + + instfont_path = os.path.splitext(varfont_path)[0] + '-instance' + suffix + instfont = TTFont(instfont_path) + tables = [table_tag for table_tag in instfont.keys() if table_tag != 'head'] + expected_ttx = self.get_test_input('MutatorTest.ttx') + self.expect_ttx(instfont, expected_ttx, tables) + + +if __name__ == "__main__": + sys.exit(unittest.main()) From 7bf853ed2e2309213ddf87ae91bc5c4b757aa4c6 Mon Sep 17 00:00:00 2001 From: Miguel Sousa Date: Sat, 4 Mar 2017 12:54:20 -0800 Subject: [PATCH 3/7] [varLib] Round advance width and LSB to integers --- Lib/fontTools/varLib/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/fontTools/varLib/__init__.py b/Lib/fontTools/varLib/__init__.py index 8f2d7fc24..eead87f65 100644 --- a/Lib/fontTools/varLib/__init__.py +++ b/Lib/fontTools/varLib/__init__.py @@ -154,8 +154,8 @@ def _SetCoordinates(font, glyphName, coord): glyph.recalcBounds(glyf) - horizontalAdvanceWidth = rightSideX - leftSideX - leftSideBearing = glyph.xMin - leftSideX + horizontalAdvanceWidth = round(rightSideX - leftSideX) + leftSideBearing = round(glyph.xMin - leftSideX) # XXX Handle vertical font["hmtx"].metrics[glyphName] = horizontalAdvanceWidth, leftSideBearing From de6b436c1424f9ed7345943b05f24c7cff556cde Mon Sep 17 00:00:00 2001 From: Miguel Sousa Date: Sat, 4 Mar 2017 12:55:23 -0800 Subject: [PATCH 4/7] [varLib mutator] Fix test result --- Tests/varLib/data/MutatorTest.ttx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/varLib/data/MutatorTest.ttx b/Tests/varLib/data/MutatorTest.ttx index e5786f50b..2467eb33f 100644 --- a/Tests/varLib/data/MutatorTest.ttx +++ b/Tests/varLib/data/MutatorTest.ttx @@ -16,9 +16,9 @@ - + - + From 9e4e1ffc4432d521908f1ee0bb51484e403db9ce Mon Sep 17 00:00:00 2001 From: Miguel Sousa Date: Sat, 4 Mar 2017 12:58:00 -0800 Subject: [PATCH 5/7] [varLib.mutator] Revert Include HVAR in the list of tables to remove. --- Lib/fontTools/varLib/mutator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/fontTools/varLib/mutator.py b/Lib/fontTools/varLib/mutator.py index 638a2ffaf..f665df6ad 100644 --- a/Lib/fontTools/varLib/mutator.py +++ b/Lib/fontTools/varLib/mutator.py @@ -48,8 +48,8 @@ def main(args=None): coordinates += GlyphCoordinates(var.coordinates) * scalar _SetCoordinates(varfont, glyphname, coordinates) - print("Removing variable tables") - for tag in ('fvar','avar','gvar','HVAR'): + print("Removing GX tables") + for tag in ('fvar','avar','gvar'): if tag in varfont: del varfont[tag] From 7f792cbaf84a07a1b3f096b1e13783670e0f45a5 Mon Sep 17 00:00:00 2001 From: Miguel Sousa Date: Sat, 4 Mar 2017 13:02:52 -0800 Subject: [PATCH 6/7] [varLib.mutator] Add HVAR table to test result --- Tests/varLib/data/MutatorTest.ttx | 88 +++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/Tests/varLib/data/MutatorTest.ttx b/Tests/varLib/data/MutatorTest.ttx index 2467eb33f..260a8c184 100644 --- a/Tests/varLib/data/MutatorTest.ttx +++ b/Tests/varLib/data/MutatorTest.ttx @@ -665,4 +665,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From af3df891b298b7fc3156e1a5aa433d8194b67452 Mon Sep 17 00:00:00 2001 From: Miguel Sousa Date: Sat, 4 Mar 2017 23:30:37 -0800 Subject: [PATCH 7/7] [varLib.mutator] Drop all variable tables --- Lib/fontTools/varLib/mutator.py | 4 +- Tests/varLib/data/MutatorTest.ttx | 88 ------------------------------- 2 files changed, 2 insertions(+), 90 deletions(-) diff --git a/Lib/fontTools/varLib/mutator.py b/Lib/fontTools/varLib/mutator.py index f665df6ad..a7097954d 100644 --- a/Lib/fontTools/varLib/mutator.py +++ b/Lib/fontTools/varLib/mutator.py @@ -48,8 +48,8 @@ def main(args=None): coordinates += GlyphCoordinates(var.coordinates) * scalar _SetCoordinates(varfont, glyphname, coordinates) - print("Removing GX tables") - for tag in ('fvar','avar','gvar'): + print("Removing variable tables") + for tag in ('avar','cvar','fvar','gvar','HVAR','MVAR','VVAR','STAT'): if tag in varfont: del varfont[tag] diff --git a/Tests/varLib/data/MutatorTest.ttx b/Tests/varLib/data/MutatorTest.ttx index 260a8c184..2467eb33f 100644 --- a/Tests/varLib/data/MutatorTest.ttx +++ b/Tests/varLib/data/MutatorTest.ttx @@ -665,92 +665,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -