-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgenerate-font.ps1
64 lines (56 loc) · 2.44 KB
/
generate-font.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
# Font file format generator script.
param (
[Parameter(Mandatory)][string]$inputFile,
[Parameter(Mandatory)][string]$outputFile
)
function Get-UInt16AsBytes() {
[OutputType([Array])]
param (
[Parameter(Mandatory)][uint16]$Value
)
$hi = ($Value -shr 8) -band 0xFF
$lo = $Value -band 0xFF
return [Array]@($lo, $hi)
}
$inputFileContent = Get-Content -Path $inputFile -Raw
$fontFile = New-Item -Name $outputFile -ItemType File -Force
$fileStream = $fontFile.OpenWrite()
try {
# Font info
if ($inputFileContent -match "FontInfo\s*=\s*{\s*`"(.+)`",\s*(\d+).*\s*('.').*\s*('.').*\s*(\d+)") {
$fileStream.WriteByte([Byte]$Matches[1].Length) # Font name length
$fileStream.Write([System.Text.Encoding]::ASCII.GetBytes($Matches[1]), 0, $Matches[1].Length) # Font name
$fileStream.WriteByte([Byte]$Matches[2]) # Character height
$fileStream.WriteByte([Byte][Char]$Matches[3][1]) # Start character
$fileStream.WriteByte([Byte][Char]$Matches[4][1]) # End character
$fileStream.WriteByte([Byte]$Matches[5]) # Width, in pixels, of space character
} else {
throw "Unable to find FontInfo data"
}
# Font bitmaps
if ($inputFileContent -match 'Bitmaps\[\]\s*=\s*\{([^}]+)\}') {
$bitmapsRaw = $Matches[1];
$bitmapsMatch = $bitmapsRaw | Select-String -Pattern '0x[0-9a-zA-Z]{2}' -AllMatches
$bitmapBytes = $bitmapsMatch.Matches.Value | ForEach({ [byte]$_ })
$length = [uint16]$bitmapBytes.Length
$fileStream.Write((Get-UInt16AsBytes -Value $length), 0, 2) # Bitmap array length
$fileStream.Write($bitmapBytes, 0, $length) # Bitmap data
} else {
throw "Unable to find Bitmap data"
}
# Font descriptors
if ($inputFileContent -match 'Descriptors\[\]\s*=\s*\{([\w\W]+?)\};') {
$descriptorsRaw = $Matches[1];
$descriptorsMatch = $descriptorsRaw | Select-String -Pattern '\{\s*(\d+),\s*(\d+)\s*\}' -AllMatches
$fileStream.WriteByte([Byte]$descriptorsMatch.Matches.Length) # Descriptors array length
foreach ($match in $descriptorsMatch.Matches) {
$fileStream.WriteByte([Byte]$match.Groups[1].Value) # Character width
$offset = [uint16]$match.Groups[2].Value
$fileStream.Write((Get-UInt16AsBytes -Value $offset), 0, 2) # Character offset
}
} else {
throw "Unable to find Descriptors data"
}
} finally {
$fileStream.Dispose()
}