-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
subset-fonts.ps1
executable file
·130 lines (117 loc) · 4.76 KB
/
subset-fonts.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/env pwsh
Set-StrictMode -Version 3.0
$ErrorActionPreference = "Stop"
# Features included by default by pyftsubset are:
# ['BUZZ', 'Buzz', 'HARF', 'Harf', 'abvf', 'abvm',
# 'abvs', 'akhn', 'blwf', 'blwm', 'blws', 'calt',
# 'ccmp', 'cfar', 'chws', 'cjct', 'clig', 'cswh',
# 'curs', 'dist', 'dnom', 'fin2', 'fin3', 'fina',
# 'frac', 'half', 'haln', 'halt', 'init', 'isol',
# 'jalt', 'kern', 'liga', 'ljmo', 'locl', 'ltra',
# 'ltrm', 'mark', 'med2', 'medi', 'mkmk', 'mset',
# 'nukt', 'numr', 'pref', 'pres', 'pstf', 'psts',
# 'rand', 'rclt', 'rkrf', 'rlig', 'rphf', 'rtla',
# 'rtlm', 'rvrn', 'stch', 'tjmo', 'valt', 'vatu',
# 'vchw', 'vert', 'vhal', 'vjmo', 'vkrn', 'vpal',
# 'vrt2']
$fonts = [ordered]@{
"charis" = @{
"Dir" = "Charis_SIL"
"Family" = "Charis SIL"
"Subsets" = [ordered]@{
# This operates as a fallback for missing characters in SS4.
# e.g. COMBINING VERTICAL LINE ABOVE
# both the base character and the combining one are required
"LatinFallback" = @{
"Blocks" = @("BasicLatin", "CombiningDiacriticalMarks")
}
# Mediaeval & Egyptological
"Historical" = @{
"Blocks" = @("LatinExtendedD")
}
}
}
"sourceserif4" = @{
"Family" = "Source Serif 4"
"Dir" = "SourceSerif4"
"Subsets" = [ordered]@{
"Latin" = @{
"Features" = "ordn,smcp,c2sc,subs,sups,case,kern,onum,tnum"
# diacritical marks must be included here or they won't combine
"Blocks" = @("BasicLatin", "GeneralPunctuation", "Latin1Supplement", "CombiningDiacriticalMarks")
}
# Don't need smcp/c2sc for these
"LatinExt" = @{
"Blocks" = @("LatinExtendedA", "SpacingModifierLetters", "IpaExtensions")
}
# Chinese (Pinyin & Jyutping) transliteration
"ChineseTranslit" = @{
"Blocks" = @( "LatinExtendedB", "SuperscriptsandSubscripts")
}
# Vietnamese and Indic transliteration
"VietIndic" = @{
"Blocks" = @("LatinExtendedAdditional")
}
"Cyrillic" = @{
"Blocks" = @("Cyrillic")
}
"Greek" = @{
"Blocks" = @("GreekandCoptic")
}
}
}
}
foreach ($font in $fonts.GetEnumerator()) {
$fontName = $font.Key
$dir = $font.Value.Dir
$fontFamily = $font.Value.Family
$subsets = $font.Value.Subsets
pushd "input-fonts/$dir"
mkdir -p "../../fonts/$fontName"
$css = "../../fonts/$fontName.css"
echo "" > $css # blank it
function whitelistFromBlock([string]$block) {
$range = [System.Text.Unicode.UnicodeRanges]::$block
if ($range -eq $null) {
throw "no such block $block"
}
return "U+$($range.FirstCodePoint.ToString("x4"))-$(($range.FirstCodePoint + $range.Length - 1).ToString("x4"))"
}
foreach ($subset in $subsets.GetEnumerator()) {
$name = $subset.Key
echo "$name - $($subset.Value.Blocks)"
$whitelist = ($subset.Value.Blocks | %{ whitelistFromBlock($_) }) -join ','
if ($subset.Value.ContainsKey('Additional')) {
$whitelist += ",$($subset.Value.Additional)"
}
$features = @()
if ($subset.Value.ContainsKey('Features')) {
$features = @("--layout-features+=$($subset.Value.Features)")
echo $features
}
foreach ($file in Get-ChildItem -Filter '*.woff2' -File) {
$targetFile = Join-Path "../../fonts/$fontName" ($file.Name -replace "-", "-$name-")
pyftsubset $file --unicodes=$whitelist --flavor=woff2 --harfbuzz-repacker --output-file=$targetFile `
@features `
--drop-tables+=Silt # --drop-tables+=name --drop-tables+=post
echo "/* $name ($($subset.Value.Blocks -join ', ')) */" >> $css
echo "@font-face {" >> $css
echo " font-family: '$fontFamily';" >> $css
echo " src: local('$fontFamily'), url('/fonts/$fontName/$(Split-Path -Leaf $targetFile)') format('woff2');" >> $css
if ($subset.Value.ContainsKey("Display")) {
echo " font-display: $($subset.Value.Display);" >> $css
} else {
echo " font-display: swap;" >> $css
}
if ($file.Name -like "*Italic*") {
echo " font-style: italic;" >> $css
}
if ($file.Name -like "*Bold*") {
echo " font-weight: bold;" >> $css
}
echo " unicode-range: $whitelist;" >> $css
echo "}" >> $css
}
}
popd
}