Skip to content

Commit

Permalink
font-patcher: Allow to rehint some Cascadia glyphs
Browse files Browse the repository at this point in the history
[why]
Some Caskaydia Cove glyphs that are used in ligatures (to create endless
arrows for example) show uneven line thickness on some platforms.

The reason is the not-matching hinting of glyphs that are places next
to each other and so minuscule differences are quite visible.

Note that the 'original' hinting is generated by VTT on the static
Cascadia Code instances, which upstream just have ttfautohint hints that
are different from the hints in the variable fonts and people complained
that the look is different.

[how]
Add a new field to the config.json file that holds regexes of glyph
names for glyphs that should be re-hinted by fontforge on patching time.

In principle we could also implement that as an additional pre-step that
needs to be manually done after running VTT on the static font files.

I'm not sure which is better.

Note that fontforge generates a lot of debug output because the hinting
is not ideal - the global tables are kept and probably less compatible
to fontforge's own hinting... But the results are good.

Fixes: #1291
Fixes: #1609

Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
  • Loading branch information
Finii committed Apr 18, 2024
1 parent c46764c commit 0c6e419
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
38 changes: 33 additions & 5 deletions font-patcher
Original file line number Diff line number Diff line change
Expand Up @@ -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.13.1"
script_version = "4.14.0"

version = "3.2.1"
projectName = "Nerd Fonts"
Expand Down Expand Up @@ -339,7 +339,9 @@ class font_patcher:
self.sourceFont = font
self.setup_version()
self.assert_monospace()
self.load_config()
self.remove_ligatures()
self.manipulate_hints()
self.get_essential_references()
self.get_sourcefont_dimensions()
self.setup_patch_set()
Expand Down Expand Up @@ -769,11 +771,18 @@ class font_patcher:
# print("Version now is {}".format(sourceFont.version))


def load_config(self):
""" Try to read the config file (if specified) """
if self.args.configfile:
if not self.config.read(self.args.configfile):
logger.error("Unable to read configfile")


def remove_ligatures(self):
# let's deal with ligatures (mostly for monospaced fonts)
# Usually removes 'fi' ligs that end up being only one cell wide, and 'ldot'
if self.args.configfile and self.config.read(self.args.configfile):
if self.args.removeligatures:
if self.args.removeligatures:
if self.args.configfile:
logger.info("Removing ligatures from configfile `Subtables` section")
ligature_subtables = json.loads(self.config.get("Subtables", "ligatures"))
for subtable in ligature_subtables:
Expand All @@ -783,9 +792,28 @@ class font_patcher:
logger.debug("Successfully removed subtable: %s", subtable)
except Exception:
logger.error("Failed to remove subtable: %s", subtable)
elif self.args.removeligatures:
logger.error("Unable to read configfile, unable to remove ligatures")
else:
logger.error("No configfile, unable to remove ligatures")


def manipulate_hints(self):
""" Redo the hinting on some problematic glyphs """
if not self.args.configfile:
return
redo = json.loads(self.config.get("Hinting", "re_hint"))
if not len(redo):
return
logger.debug("Working on {} rehinting rules".format(len(redo)))
count = 0
for gname in self.sourceFont:
for regex in redo:
if re.fullmatch(regex, gname):
glyph = self.sourceFont[gname]
glyph.autoHint()
glyph.autoInstr()
count += 1
break
logger.info("Rehinted {} glyphs".format(count))

def assert_monospace(self):
# Check if the sourcefont is monospaced
Expand Down
5 changes: 5 additions & 0 deletions src/unpatched-fonts/CascadiaCode/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Hinting]
re_hint: [
".*hyphen.*\\.(liga|seq)",
".*equal.*\\.(liga|seq)",
".*numbersign.*\\.(liga|seq)" ]
5 changes: 5 additions & 0 deletions src/unpatched-fonts/CascadiaMono/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Hinting]
re_hint: [
".*hyphen.*\\.(liga|seq)",
".*equal.*\\.(liga|seq)",
".*numbersign.*\\.(liga|seq)" ]

0 comments on commit 0c6e419

Please sign in to comment.