font-patcher: Add option to select metrics source

[why]
If the font has contradicting baseline to baseline metrics the patcher
tries to find a sane value and use that. That automatism gets it right
in most cases, but there might be fonts where the user wants a different
metric to be used.

At the moment the use would need to use `font-line` to adjust the
metrics, which is not very convenient.

[how]
Add option to select one of the metricses.
Use that metrics when setting up the patched font.

Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
This commit is contained in:
Fini Jastrow 2023-10-13 11:42:25 +02:00
parent 62f48f3109
commit 72fb7fb814

View file

@ -6,7 +6,7 @@
from __future__ import absolute_import, print_function, unicode_literals
# Change the script version when you edit this script:
script_version = "4.5.3"
script_version = "4.6.0"
version = "3.0.2"
projectName = "Nerd Fonts"
@ -284,6 +284,10 @@ def get_btb_metrics(font):
win_btb = win_height + win_gap
return (hhea_btb, typo_btb, win_btb, win_gap)
def get_metrics_names():
""" Helper to get the line metrics names consistent """
return ['HHEA','TYPO','WIN']
def get_old_average_x_width(font):
""" Determine xAvgCharWidth of the OS/2 table """
# Fontforge can not create fonts with old (i.e. prior to OS/2 version 3)
@ -1139,15 +1143,16 @@ class font_patcher:
(hhea_btb, typo_btb, win_btb, win_gap) = get_btb_metrics(self.sourceFont)
use_typo = self.sourceFont.os2_use_typo_metrics != 0
Metric = Enum('Metric', ['HHEA', 'TYPO', 'WIN'])
Metric = Enum('Metric', get_metrics_names())
if not self.args.metrics:
# We use either TYPO (1) or WIN (2) and compare with HHEA
# and use HHEA (0) if the fonts seems broken - no WIN, see #1056
our_btb = typo_btb if use_typo else win_btb
if our_btb == hhea_btb:
metrics = Metric.TYPO if use_typo else Metric.WIN # conforming font
elif abs(our_btb - hhea_btb) / our_btb < 0.03:
logger.info("Font vertical metrics slightly off (%.1f)", (our_btb - hhea_btb) / our_btb * 100.0)
logger.info("Font vertical metrics slightly off (%.1f%)", (our_btb - hhea_btb) / our_btb * 100.0)
metrics = Metric.TYPO if use_typo else Metric.WIN
else:
# Try the other metric
@ -1162,6 +1167,16 @@ class font_patcher:
logger.warning("Font vertical metrics inconsistent (HHEA %d / TYPO %d / WIN %d), using WIN", hhea_btb, typo_btb, win_btb)
our_btb = win_btb
metrics = Metric.WIN
else:
metrics = Metric[self.args.metrics]
logger.debug("Metrics in the font: HHEA %d / TYPO %d / WIN %d", hhea_btb, typo_btb, win_btb)
if metrics == Metric.HHEA:
our_btb = hhea_btb
elif metrics == Metric.TYPO:
our_btb = typo_btb
else:
our_btb = win_btb
logger.info("Manually selected metrics: %s (%d)", self.args.metrics, our_btb)
# print("FINI hhea {} typo {} win {} use {} {} {}".format(hhea_btb, typo_btb, win_btb, use_typo, our_btb != hhea_btb, self.sourceFont.fontname))
@ -1177,6 +1192,7 @@ class font_patcher:
self.font_dim['ymin'] = -self.sourceFont.os2_windescent - half_gap(win_gap, False)
self.font_dim['ymax'] = self.sourceFont.os2_winascent + half_gap(win_gap, True)
else:
logger.debug("Metrics is strange")
pass # Will fail the metrics check some line later
# Calculate font height
@ -1880,6 +1896,7 @@ def setup_arguments():
# <none> - copy from sourcefont (default)
# 0 - calculate from font according to OS/2-version-2
# 500 - set to 500
parser.add_argument('--metrics', dest='metrics', default=None, choices=get_metrics_names(), help='Select vertical metrics source (for problematic cases)')
parser.add_argument('--name', dest='force_name', default=None, type=str, help='Specify naming source (\'full\', \'postscript\', or concrete free name-string)')
# symbol fonts to include arguments