-
Notifications
You must be signed in to change notification settings - Fork 1
/
FTAutofix.psm1
334 lines (290 loc) · 12.9 KB
/
FTAutofix.psm1
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<#
.SYNOPSIS
Fixes fonts with broken name tables.
.DESCRIPTION
FTAutofix accepts a comma-delimited list of TrueType/OpenType fonts and/or folders and attempts to automatically fix broken name tables.
It requires one name field to hold the full font name and guesses its way from there.
.PARAMETER Inputs
Comma-delimited list of files and/or Directories to process (can take mixed).
Alias: -i
.PARAMETER Recurse
Recurse subdirectories.
Alias: -r
.EXAMPLE
FTAutofix X:\font.ttf
.EXAMPLE
FTAutofix X:\Fonts -r
.LINK
https://github.com/line0/FontToys
#>
#requires -version 3
function FTAutofix {
[CmdletBinding()]
param
(
[Parameter(Position = 0, Mandatory = $true, HelpMessage = 'Comma-delimited list of files and/or Directories to process (can take mixed).')]
[alias("i")]
[string[]]$Inputs,
[Parameter(Mandatory = $false, HelpMessage = 'Recurse subdirectories.')]
[alias("r")]
[switch]$Recurse = $false,
[Parameter(Mandatory = $false, HelpMessage = 'Name table entries searched for the full font name.')]
[alias("nt")]
[object[]]$NameTableEntries = (@(4, 1, 0, 0), @(4, 3, 0, 0)),
[Parameter(Mandatory = $false, HelpMessage = 'Retrieve the font name from the file name instead of a name table entry.')]
[alias("f")]
[switch]$UseFilename = $false,
[Parameter(Mandatory = $false, HelpMessage = 'Regex pattern to be used for matching family and style name parts.')]
[alias("p")]
[regex]$MatchPattern,
[Parameter(Mandatory = $false, HelpMessage = 'A single family name that overrides all detected family names.')]
[alias("fo")]
[string]$FamilyOverride
)
# if there are no other properties, $_.#text is resolved into a string, which is inconvenient so we undo it
$fontWordsXml = [xml](Get-Content (Join-Path (Split-Path -parent $PSCommandPath) "StyleWords.xml"))
$knownStyleWords = @($fontWordsXml.fontWords.styleWords.word | Where-Object {$_ -is [System.Xml.XmlElement]}) + @(
$fontWordsXml.fontWords.styleWords.word | Where-Object {$_ -is [string]} | ForEach-Object {@{"#text" = $_}})
$knownFamilyWords = @($fontWordsXml.fontWords.familyWords.word | Where-Object {$_ -is [System.Xml.XmlElement]}) + @(
$fontWordsXml.fontWords.familyWords.word | Where-Object {$_ -is [string]} | ForEach-Object {@{"#text" = $_}})
try {
$fonts = Get-Files $Inputs -match '.[o|t]tf$' -matchDesc 'OpenType/TrueType Font' -acceptFolders -recurse:$Recurse
} catch {
if($_.Exception.WasThrownFromThrowStatement -eq $true) {
Write-Host $_.Exception.Message -ForegroundColor Red
break
} else {
throw $_.Exception
}
}
$overallActivity = "FontToys Autofix"
Write-Progress -Activity $overallActivity -Id 0 -PercentComplete 0 -Status "Step 1/2: Reading and fixing fonts"
$readActivity = "Reading $($fonts.Count) fonts.."
Write-Progress -Activity $readActivity -Id 1 -PercentComplete 0 -Status "Font 1/$($fonts.Count)"
$tableView = @(
@{label = "File"; Expression = { $_.Path.Name}; width = 50},
@{label = "Family Name"; Expression = { $_._FamilyName}; width = 50},
@{label = "Style Name"; Expression = { $_._StyleName}; width = 32}
@{label = "Selection Flags"; Expression = { $_.GetFsFlags()}; width = 32}
@{label = "Weight"; Expression = { $_.GetWeight()}; width = 12}
@{label = "Width"; Expression = { $_.GetWidth()}; width = 16}
)
$fonts | FixFont -NameTableEntries $NameTableEntries -useFilename $UseFilename -matchPattern $MatchPattern -familyOverride $FamilyOverride -OutVariable fontList |
Format-Table -Property $tableView | ForEach-Object {
$_
$fntReadCnt = $fontList.Count / $fonts.Count
Write-Progress -Activity $readActivity -Id 1 -PercentComplete (100 * $fntReadCnt) -Status "File $($fontList.Count+1)/$($fonts.Count): $($fontList[-1].Path.Name)"
Write-Progress -Activity $overallActivity -Id 0 -PercentComplete (50 * $fntReadCnt) -Status "Step 1/2: Reading and fixing fonts"
}
$familyList = $fontList | Group-Object -Property _FamilyName
$familyActivity = "Writing $($familyList.Count) families.."
Write-Progress -Activity $overallActivity -Id 0 -PercentComplete 50 -Status "Step 2/2: Writing fixed fonts"
$doneCnt = 0
foreach($group in $familyList) {
Write-Progress -Activity $familyActivity -Id 1 -PercentComplete (100 * $doneCnt / $familyList.Count) -Status $group.Name
$familyPath = New-Item (Join-Path $group.Group[0].Path.DirectoryName $group.Name) -ItemType Directory -Force
$styleActivity = "Writing $($group.Group.Count) styles.."
$group.Group | ForEach-Object {$styleDoneCnt = 0} {
Write-Progress -Activity $styleActivity -Id 2 -PercentComplete (100 * $styleDoneCnt / $group.Group.Count) -Status $_.GetNames(2, 1, 0, 0).Name
$percentOverall = 50 + 50 * (($doneCnt / $familyList.Count) + 1 * $styleDoneCnt / ($familyList.Count * $group.Group.Count))
Write-Progress -Activity $overallActivity -Id 0 -PercentComplete $percentOverall -Status "Step 2/2: Writing fixed fonts"
Export-Font $_ -OutPath $familyPath.FullName -Format ttf
$styleDoneCnt++
}
$doneCnt++
}
}
filter FixFont {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True, ValueFromPipeline = $true)]
[System.IO.FileSystemInfo]$_,
[object[]]$NameTableEntries = (@(4, 1, 0, 0), @(4, 3, 0, 0)),
[bool]$useFilename = $false,
[regex]$matchPattern,
[string]$familyOverride
)
$fontData = Import-Font -InFile $_.FullName
$fontName = if($useFilename) {
$fontData.Path.BaseName
} else {
($NameTableEntries | ForEach-Object {$fontData.GetNames($_[0], $_[1], $_[2], $_[3]).Name} | Where-Object {$_ -ne $null} |
Sort-Object -Property Length -Descending | Select-Object -First 1 # longest font name is probably of the highest quality
) -replace ':.*','' # sometimes the full font name is suffixed by version like "Aarcover Plain:001.001"
}
$styleWords = @()
if($matchPattern) {
$matches = Select-String -InputObject $fontName -pattern $matchPattern | Select-Object -ExpandProperty Matches
if ($matches.Groups.Count -lt 3) {
throw "Your match pattern '$($matchPattern)' didn't produce at least 2 groups."
} else {
$familyName = $matches.Groups[1].Value
$styleName = ReplaceStyleWords $matches.Groups[1].Value -StyleWords $knownStyleWords
$styleWords = @(($styleName -replace "_", " " -split "\s+") | Where-Object {$_})
}
} else {
$fontName = ReplaceStyleWords $fontName -StyleWords ($knownStyleWords + $knownFamilyWords) -ProtectBeginning $true
# split font name into words at boundaries denoted by underscores, dashes and spaces
$fontWords = @(($fontName.Trim() -replace "-", " " -replace "_", " " -split "\s+") | Where-Object {$_ -match '[^\s]'})
Write-Debug "Font Words: $($fontWords -join ', ')"
$familyWords = @()
# filter out any style words that just perform replacements
$matchingknownStyleWords = $knownStyleWords | Where-Object {$_."#text" -or ($_.match -and -not $_.replace)}
# determine the boundary between family name and style name using incredibly crude heuristics
$firstStyleWordIndex = -1
for ($($i = 1; $previousWordIsStyleWord = $false); $i -lt $fontWords.Count; $i++) {
# filter out any style word not meant to go into this position in the style word order
$applicableknownStyleWords = $matchingknownStyleWords | Where-Object {
(!$_.onlyLast -or ($fontWords.Count - $i) -le ($_.onlyLast)) -and (!$_.notFirst -or $i -gt $_.notFirst)
}
$currentWordIsStyleWord = IsStyleWord $fontWords[$i] -StyleWords $applicableknownStyleWords
if (!$previousWordIsStyleWord -and $currentWordIsStyleWord) {
$firstStyleWordIndex = $i
} elseif (!$currentWordIsStyleWord) {
if ($previousWordIsStyleWord -and ($fontWords[$i] -in $knownFamilyWords."#text")) {
$familyWords += $fontWords[$i]
$fontWords[$i] = [string]::Empty
} else {
$firstStyleWordIndex = -1
}
}
$previousWordIsStyleWord = $currentWordIsStyleWord
}
$fontWords[0] = UpperFirst $fontWords[0] # start font with an uppercase character
# generate the family name, decamelize it and remove it from the style words list
$familyWords = if ($firstStyleWordIndex -eq -1) {
$fontWords + $familyWords
} else {
$fontWords[0..($firstStyleWordIndex - 1)] + $familyWords
}
if ($firstStyleWordIndex -gt -1) {
# Collect and TitleCase all style words
$styleWords = @($fontWords[$firstStyleWordIndex..($fontWords.Count - 1)] | Where-Object {$_} | UpperFirst -FixCase)
}
$familyName = @($familyWords | ForEach-Object {UpperFirst $_}) -join " " -creplace '(\p{Ll}+)([\p{Lu}\p{Lt}])', '$1 $2'
Write-Debug "Family Name: $familyName"
$fontData | Add-Member -Type NoteProperty -Name _FontWords -Value $fontWords # only for debugging purposes
}
if ($styleWords.Length -gt 0) {
$knownStyleWords | Where-Object {$_.'#text'} | ForEach-Object {
# conform case of all known style words
$isMatch = if ($_.match -and $styleWords -cmatch "^$($_.match)`$") {
$styleWords = $styleWords -creplace "^$($_.match)`$", $_.'#text' # a bit wasteful doing the matching twice, but style words of this configuration are rare at the time of writing
$true
} elseif (!$_.match -and 0 -le ($i = [Array]::FindIndex($styleWords, [Predicate[string]] {param($word) $word -eq $_.'#text'}))) {
$styleWords[$i] = $_.'#text'
$true
}
if ($isMatch) {
ImportMetrics -Font $fontData -StyleWord $_
}
}
}
$fontData | Add-Member -Type NoteProperty -Name _StyleName -Value ($styleWords -join " ") -PassThru |
Add-Member -Type NoteProperty -Name _FamilyName -Value $(if ($familyOverride) {
$familyOverride
} else {
$familyName
})
$fontData.SetFamily($fontData._FamilyName, $fontData._StyleName)
return $fontData
}
function UpperFirst(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[string]$str,
[switch]$FixCase = $false
) {
Process {
if($FixCase -and ($str -ceq $str.ToLower() -or $str -ceq $str.ToUpper())) {
return (Get-Culture).TextInfo.ToTitleCase($str)
} else {
return $str.Substring(0, 1).ToUpper() + $str.Substring(1)
}
}
}
function ReplaceStyleWords(
[Parameter(Position = 0, Mandatory = $true)]
[string]$FontName,
[Parameter(Mandatory = $true)]
[System.Object[]]$StyleWords,
[Parameter(Mandatory = $false)]
[bool]$ProtectBeginning = $false
) {
$protectBeginningLookahead = if($ProtectBeginning) {
"(?<=.)"
}
$StyleWords | Where-Object {$_.match -and $_.replace} | ForEach-Object {
$prevFontName = $FontName
# Protect the very beginning of the font name if configured and don't match partial words
# Add separation markers for every match to denote word boundaries
$FontName = $FontName -creplace "$($protectBeginningLookahead)($($_.match))(?!\p{Ll})", "_$($_.replace)_"
if ($prevFontName -ne $FontName) {
Write-Debug "Replaced: $($_.match) -> $($_.replace)"
}
}
$StyleWords | Where-Object {$_.separate -eq 1 -and -not ($_.match -and $_.replace)} | ForEach-Object {
$styleWord = $_
# match using "match" tag attribute if specified, otherwise match the element content
$match = if($styleWord.match) {
$styleWord.match
} else {
[Regex]::Escape($styleWord."#text")
}
if (!$match) {
return;
}
# Add separation markers for every match to denote word boundaries
$regexOptions = if (!$styleWord.caseSensitive -or $styleWord.caseSensitive -eq 0) {
[Text.RegularExpressions.RegexOptions]::IgnoreCase
} else {
[Text.RegularExpressions.RegexOptions]::None
}
[regex]::Matches($FontName, $match, $regexOptions) | ForEach-Object {
if (($ProtectBeginning -and $_.Index -eq 0) -or $FontName[$_.Index + $_.Length] -cmatch "\p{Ll}") {
# Protect the very beginning of the font name if configured and don't match partial words
return;
}
$term = $FontName.Substring($_.Index, $_.Length)
$FontName = $FontName.Substring(0, $_.Index) + '_' + $term + '_' + $FontName.Substring($_.Index + $_.Length)
Write-Debug "Separated: $FontName"
}
}
return $FontName
}
function IsStyleWord(
[Parameter(Position = 0, Mandatory = $true)]
[string]$Word,
[Parameter(Mandatory = $true)]
[System.Object[]]$StyleWords
) {
foreach ($styleWord in $StyleWords) {
if ($styleWord.match) {
if ($Word -cmatch "^$($styleWord.match)`$") {
return $true
}
} elseif ($styleWord.'#text') {
$match = [Regex]::Escape($styleWord.'#text')
if ($Word -match "^$match`$") {
return $true
}
}
}
return $false
}
function ImportMetrics(
[Parameter(Mandatory = $true)]
[object]$Font,
[Parameter(Mandatory = $true)]
[object]$StyleWord
) {
if($StyleWord.weight) {
$Font.SetWeight([int]$StyleWord.weight)
}
if($StyleWord.width) {
$Font.SetWidth([int]$StyleWord.width)
}
if($StyleWord.fsSelection) {
$Font.AddFsFlags([int]$StyleWord.fsSelection)
}
}
Export-ModuleMember FTAutofix