summaryrefslogtreecommitdiffstats
path: root/contrib/python/fonttools/fontTools/varLib/instancer/__init__.py
diff options
context:
space:
mode:
authorrobot-contrib <[email protected]>2023-11-30 09:51:41 +0300
committerrobot-contrib <[email protected]>2023-11-30 10:16:12 +0300
commitd3b4d830e105623b4cc96655c4dbb2d73f825989 (patch)
treedf2a42a8615d7c1bc9d7fd682c8c8f992a17b681 /contrib/python/fonttools/fontTools/varLib/instancer/__init__.py
parent468826fff1465595d80da778532b253674e1e1c3 (diff)
Update contrib/python/fonttools to 4.44.3
Diffstat (limited to 'contrib/python/fonttools/fontTools/varLib/instancer/__init__.py')
-rw-r--r--contrib/python/fonttools/fontTools/varLib/instancer/__init__.py42
1 files changed, 41 insertions, 1 deletions
diff --git a/contrib/python/fonttools/fontTools/varLib/instancer/__init__.py b/contrib/python/fonttools/fontTools/varLib/instancer/__init__.py
index cde1d39f6fc..a887e5d38f9 100644
--- a/contrib/python/fonttools/fontTools/varLib/instancer/__init__.py
+++ b/contrib/python/fonttools/fontTools/varLib/instancer/__init__.py
@@ -105,6 +105,7 @@ from fontTools.misc.cliTools import makeOutputFileName
from fontTools.varLib.instancer import solver
import collections
import dataclasses
+from contextlib import contextmanager
from copy import deepcopy
from enum import IntEnum
import logging
@@ -694,6 +695,43 @@ def setMvarDeltas(varfont, deltas):
)
+@contextmanager
+def verticalMetricsKeptInSync(varfont):
+ """Ensure hhea vertical metrics stay in sync with OS/2 ones after instancing.
+
+ When applying MVAR deltas to the OS/2 table, if the ascender, descender and
+ line gap change but they were the same as the respective hhea metrics in the
+ original font, this context manager ensures that hhea metrcs also get updated
+ accordingly.
+ The MVAR spec only has tags for the OS/2 metrics, but it is common in fonts
+ to have the hhea metrics be equal to those for compat reasons.
+
+ https://learn.microsoft.com/en-us/typography/opentype/spec/mvar
+ https://googlefonts.github.io/gf-guide/metrics.html#7-hhea-and-typo-metrics-should-be-equal
+ https://github.com/fonttools/fonttools/issues/3297
+ """
+ current_os2_vmetrics = [
+ getattr(varfont["OS/2"], attr)
+ for attr in ("sTypoAscender", "sTypoDescender", "sTypoLineGap")
+ ]
+ metrics_are_synced = current_os2_vmetrics == [
+ getattr(varfont["hhea"], attr) for attr in ("ascender", "descender", "lineGap")
+ ]
+
+ yield metrics_are_synced
+
+ if metrics_are_synced:
+ new_os2_vmetrics = [
+ getattr(varfont["OS/2"], attr)
+ for attr in ("sTypoAscender", "sTypoDescender", "sTypoLineGap")
+ ]
+ if current_os2_vmetrics != new_os2_vmetrics:
+ for attr, value in zip(
+ ("ascender", "descender", "lineGap"), new_os2_vmetrics
+ ):
+ setattr(varfont["hhea"], attr, value)
+
+
def instantiateMVAR(varfont, axisLimits):
log.info("Instantiating MVAR table")
@@ -701,7 +739,9 @@ def instantiateMVAR(varfont, axisLimits):
fvarAxes = varfont["fvar"].axes
varStore = mvar.VarStore
defaultDeltas = instantiateItemVariationStore(varStore, fvarAxes, axisLimits)
- setMvarDeltas(varfont, defaultDeltas)
+
+ with verticalMetricsKeptInSync(varfont):
+ setMvarDeltas(varfont, defaultDeltas)
if varStore.VarRegionList.Region:
varIndexMapping = varStore.optimize()