generate-font.ps1 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Font file format generator script.
  2. param (
  3. [Parameter(Mandatory)][string]$inputFile
  4. )
  5. $inputFileContent = Get-Content -Path $inputFile -Raw
  6. $fontFile = New-Item -Name "$inputFile.font" -ItemType File -Force
  7. $fileStream = $fontFile.OpenWrite()
  8. try {
  9. # Font info
  10. if ($inputFileContent -match "FontInfo\s*=\s*{\s*(\d+).*\s*('.').*\s*('.').*\s*(\d+)") {
  11. $fileStream.WriteByte([Byte]$Matches[1]) # Character height
  12. $fileStream.WriteByte([Byte][Char]$Matches[2][1]) # Start character
  13. $fileStream.WriteByte([Byte][Char]$Matches[3][1]) # End character
  14. $fileStream.WriteByte([Byte]$Matches[4]) # Width, in pixels, of space character
  15. } else {
  16. throw "Unable to find FontInfo data"
  17. }
  18. # Font bitmaps
  19. if ($inputFileContent -match 'Bitmaps\[\]\s*=\s*\{([^}]+)\}') {
  20. $bitmapsRaw = $Matches[1];
  21. $bitmapsMatch = $bitmapsRaw | Select-String -Pattern '0x[0-9a-zA-Z]{2}' -AllMatches
  22. $bitmapBytes = $bitmapsMatch.Matches.Value | ForEach({ [byte]$_ })
  23. $fileStream.Write([System.BitConverter]::GetBytes([Int16]$bitmapBytes.Length), 0, 2) # Bitmap array length
  24. $fileStream.Write($bitmapBytes, 0, $bitmapBytes.Length) # Bitmap data
  25. } else {
  26. throw "Unable to find Bitmap data"
  27. }
  28. # Font descriptors
  29. if ($inputFileContent -match 'Descriptors\[\]\s*=\s*\{([\w\W]+?)\};') {
  30. $descriptorsRaw = $Matches[1];
  31. $descriptorsMatch = $descriptorsRaw | Select-String -Pattern '\{\s*(\d+),\s*(\d+)\s*\}' -AllMatches
  32. $fileStream.WriteByte([Byte]$descriptorsMatch.Matches.Length) # Descriptors array length
  33. foreach ($match in $descriptorsMatch.Matches) {
  34. $fileStream.WriteByte([Byte]$match.Groups[1].Value) # Character width
  35. $fileStream.Write([System.BitConverter]::GetBytes([Int16]$match.Groups[2].Value), 0, 2) # Character offset
  36. }
  37. } else {
  38. throw "Unable to find Descriptors data"
  39. }
  40. } finally {
  41. $fileStream.Dispose()
  42. }