Skip to content

Commit

Permalink
🎉 (playfair) support super/subscript unicode literals
Browse files Browse the repository at this point in the history
  • Loading branch information
samizdatco committed Oct 18, 2023
1 parent 50b5e3e commit 254a6e2
Show file tree
Hide file tree
Showing 27 changed files with 78 additions and 2 deletions.
6 changes: 4 additions & 2 deletions devTools/fonts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ VENV := .env
PYTHON := $(VENV)/bin/python3
PIP := $(VENV)/bin/pip3
SUBSET := $(VENV)/bin/pyftsubset
PRETTIER := node ../../node_modules/prettier/bin-prettier.js
PRETTIER := node ../../node_modules/.bin/prettier
.PHONY: all install report test clean

default: all
Expand Down Expand Up @@ -41,7 +41,9 @@ $(PLAYFAIR).tsv:
$(PLAYFAIR): $(PLAYFAIR).tsv
mkdir -p $@
@cat $< | while IFS=$$'\t' read -r url filename; do \
curl "$$url" -o "$@/$$(basename $$filename)"; \
FONT="$@/$$(basename $$filename)"; \
curl -# "$$url" -o "$$FONT"; \
$(PYTHON) fix-numerals.py "$$FONT"; \
done
PlayfairDisplay-%.woff2: PlayfairDisplay-%.ttf
$(SUBSET) $< --unicodes="*" --flavor=woff2 --layout-features+=$(OT_FEATURES) --name-IDs="*" --output-file="$@"
Expand Down
72 changes: 72 additions & 0 deletions devTools/fonts/fix-numerals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!./env/bin/python3
"""
This script performs an in-place modification of the specified font file to insert missing
character-map entries for superscript and subscript numerals. It's intended to be used on the
PlayfairDisplay TTFs downloaded from google fonts since they lack those mappings. If invoked with
the filename "-" it will read from stdin and write the modified font to stdout.
"""
from subprocess import run
from os.path import exists, dirname, abspath
import sys
import re

TTX = f'{dirname(abspath(__file__))}/.env/bin/ttx'

# unicode codepoint -> glyph name mappings
scripts = {
# subscripts
0x2080: "zero.subs",
0x2081: "one.subs",
0x2082: "two.subs",
0x2083: "three.subs",
0x2084: "four.subs",
0x2085: "five.subs",
0x2086: "six.subs",
0x2087: "seven.subs",
0x2088: "eight.subs",
0x2089: "nine.subs",

# superscripts
0x2070: "zero.sups",
0x00B9: "uni00B9", # one
0x00B2: "uni00B2", # two
0x00B3: "uni00B3", # three
0x2074: "four.sups",
0x2075: "five.sups",
0x2076: "six.sups",
0x2077: "seven.sups",
0x2078: "eight.sups",
0x2079: "nine.sups",
}

def update_cmap(path):
pipe_input = sys.stdin.buffer.read() if path=="-" else None
if not exists(path) and not pipe_input:
print("No such file:", path, file=sys.stderr)
sys.exit(1)

# decompile the TTF
print(f"Updating character tables in {path}...", end=' ', flush=True, file=sys.stderr)
ttx_orig = run([TTX, '-o', '-', path], capture_output=True, input=pipe_input).stdout.decode('utf-8')

# bail out if this font has already been modified
for m in re.findall(r'<cmap_format_\d.*?</cmap_format_\d>', ttx_orig, re.DOTALL):
if 'zero.subs' in m:
print("(already contains super/subscript definitions)", file=sys.stderr)
sys.exit(0)

# add missing definitions to all cmap tables in the font
cmap_additions = "".join([
f'<map code="{uni:#x}" name="{name}"/>' for uni, name in scripts.items()
])
ttx_modified = re.sub(r'(</cmap_format_\d>)', cmap_additions + r'\1', ttx_orig).encode('utf-8')

# compile the updated font and overwrite the existing file
run([TTX, '-q', '-o', path, '-'], input=ttx_modified)
print("(done)", file=sys.stderr)

if __name__ == "__main__":
try:
update_cmap(*sys.argv[1:2])
except TypeError:
print("Usage: fix-numerals.py <path-to-font>")
2 changes: 2 additions & 0 deletions devTools/fonts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const FAMILIES = {
}

const SAMPLE_TEXT =
// `x<sup>0</sup>x<sup>1</sup>x<sup>2</sup>x<sup>3</sup>x<sup>4</sup>x<sup>5</sup>x<sup>6</sup>x<sup>7</sup>x<sup>8</sup>x<sup>9</sup>x<sub>0</sub>x<sub>1</sub>x<sub>2</sub>x<sub>3</sub>x<sub>4</sub>x<sub>5</sub>x<sub>6</sub>x<sub>7</sub>x<sub>8</sub>x<sub>9</sub>`
// `x⁰x¹x²x³x⁴x⁵x⁶x⁷x⁸x⁹x₀x₁x₂x₃x₄x₅x₆x₇x₈x₉`
"hamburgefonstiv 0123<sub>456</sub><sup>789</sup> ←↑→↓↔↕↖↗↘↙"

function pangram(family, postscriptNames = false) {
Expand Down
Binary file modified public/fonts/PlayfairDisplay-Black.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-BlackItalic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-Bold.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-BoldItalic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-ExtraBold.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-ExtraBoldItalic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-Italic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-Medium.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-MediumItalic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-Regular.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-SemiBold.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplay-SemiBoldItalic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-Black.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-BlackItalic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-Bold.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-BoldItalic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-ExtraBold.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-ExtraBoldItalic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-Italic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-Medium.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-MediumItalic.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-Regular.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-SemiBold.woff2
Binary file not shown.
Binary file modified public/fonts/PlayfairDisplayLatin-SemiBoldItalic.woff2
Binary file not shown.

0 comments on commit 254a6e2

Please sign in to comment.