Skip to content

Commit

Permalink
partial fix for issue #24
Browse files Browse the repository at this point in the history
  • Loading branch information
arrowtype committed Jun 22, 2023
1 parent 9823deb commit 2b9c2c5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Fonts:
# Font Features

# Freeze in code ligatures? True or False
# Note: must be set to False if you want to both have a proportional font (MONO < 1) and bake in code ligatures under the "calt" feature
Code Ligatures: True

# Include font features to freeze in stylistic options. Copy them below to use.
Expand Down
37 changes: 21 additions & 16 deletions scripts/dlig2calt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def dlig2calt(fontPath, inplace=False):

font = ttLib.TTFont(fontPath)

unitWidth = font['hmtx']['space'][0] # 600 for most monospace fonts w/ UPM=1000
# set unit width / tabular width ... assumes the "=" symbol will have a tabular width for any code font
# 600 for most monospace fonts w/ UPM=1000
unitWidth = font['hmtx']['equal'][0]

# make "LIG" glyph
font['glyf'].__setitem__('LIG', font['glyf']['space'])
Expand All @@ -28,25 +30,28 @@ def dlig2calt(fontPath, inplace=False):
for glyphName in font.getGlyphNames():
if font['hmtx'][glyphName][0] > unitWidth:

# set width to space (e.g. 600), then offset left side to be negative
oldWidth = font['hmtx'][glyphName][0]
oldLSB = font['hmtx'][glyphName][1]
widthDiff = oldWidth - unitWidth
newLSB = oldLSB - widthDiff
font['hmtx'].__setitem__(glyphName, (unitWidth, newLSB))
# only apply this to code ligatures... leave other glyphs as-is, in case they are intentionally proportional (i.e. MONO != 1)
if ".code" in glyphName:

# Adjust coordinates in glyf table
coords = font['glyf']._getCoordinatesAndControls(glyphName, font['hmtx'].metrics)[0]
phantoms = font['glyf']._getPhantomPoints(glyphName, font['hmtx'].metrics)
# set width to equal sign (e.g. 600), then offset left side to be negative
oldWidth = font['hmtx'][glyphName][0]
oldLSB = font['hmtx'][glyphName][1]
widthDiff = oldWidth - unitWidth
newLSB = oldLSB - widthDiff
font['hmtx'].__setitem__(glyphName, (unitWidth, newLSB))

# take off last four items of coords to allow adjusted phantoms to be handled separately, then combined
coords = coords[:len(coords)-4]
# Adjust coordinates in glyf table
coords = font['glyf']._getCoordinatesAndControls(glyphName, font['hmtx'].metrics)[0]
phantoms = font['glyf']._getPhantomPoints(glyphName, font['hmtx'].metrics)

adjustedCoords = [(x-widthDiff, y) for x, y in coords]
adjustedPhantoms = [(0,0), (600,0), phantoms[-2], phantoms[-1]]
# take off last four items of coords to allow adjusted phantoms to be handled separately, then combined
coords = coords[:len(coords)-4]

newCoords = adjustedCoords+adjustedPhantoms
font['glyf']._setCoordinates(glyphName, newCoords, font['hmtx'].metrics)
adjustedCoords = [(x-widthDiff, y) for x, y in coords]
adjustedPhantoms = [(0,0), (600,0), phantoms[-2], phantoms[-1]]

newCoords = adjustedCoords+adjustedPhantoms
font['glyf']._setCoordinates(glyphName, newCoords, font['hmtx'].metrics)


# add new feature code, using calt rather than dlig
Expand Down
22 changes: 14 additions & 8 deletions scripts/instantiate-code-fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ def splitFont(
# -------------------------------------------------------
# Code font special stuff in post processing

# freeze in rvrn & stylistic set features with pyftfeatfreeze
pyftfeatfreeze.main([f"--features=rvrn,{','.join(fontOptions['Features'])}", outputPath, outputPath])
if fontOptions["Fonts"][instance]["MONO"] == 1:
# freeze in rvrn & stylistic set features with pyftfeatfreeze
pyftfeatfreeze.main([f"--features=rvrn,{','.join(fontOptions['Features'])}", outputPath, outputPath])
else:
# if font is proportional, also keep the kern feature for kerning
pyftfeatfreeze.main([f"--features=rvrn,{','.join(fontOptions['Features'])},kern", outputPath, outputPath])

if fontOptions['Code Ligatures']:
# swap dlig2calt to make code ligatures work in old code editor apps
Expand All @@ -181,14 +185,16 @@ def splitFont(
except KeyError:
print("Font has no STAT table.")

# In the post table, isFixedPitched flag must be set in the code fonts
monoFont['post'].isFixedPitch = 1
if fontOptions["Fonts"][instance]["MONO"] == 1:

# In the post table, isFixedPitched flag must be set in the code fonts
monoFont['post'].isFixedPitch = 1

# In the OS/2 table Panose bProportion must be set to 9
monoFont["OS/2"].panose.bProportion = 9
# In the OS/2 table Panose bProportion must be set to 9
monoFont["OS/2"].panose.bProportion = 9

# Also in the OS/2 table, xAvgCharWidth should be set to 600 rather than 612 (612 is an average of glyphs in the "Mono" files which include wide ligatures).
monoFont["OS/2"].xAvgCharWidth = 600
# Also in the OS/2 table, xAvgCharWidth should be set to 600 rather than 612 (612 is an average of glyphs in the "Mono" files which include wide ligatures).
monoFont["OS/2"].xAvgCharWidth = 600

# Code to fix fsSelection adapted from:
# https://github.com/googlefonts/gftools/blob/a0b516d71f9e7988dfa45af2d0822ec3b6972be4/Lib/gftools/fix.py#L764
Expand Down

0 comments on commit 2b9c2c5

Please sign in to comment.