Skip to content

Instantly share code, notes, and snippets.

@x1unix
Last active July 24, 2021 04:01
Show Gist options
  • Save x1unix/680c4821a7ed7e3be9b81d3c58fe65fb to your computer and use it in GitHub Desktop.
Save x1unix/680c4821a7ed7e3be9b81d3c58fe65fb to your computer and use it in GitHub Desktop.
Rockbox font converter
<#
.SYNOPSIS
RockBox font converter.
.DESCRIPTION
Converts a list of TTF or OTF font files to RockBox file format (FNT).
For convertion, script uses 'otf2bdf' and 'convbdf' tools.
Please ensure that mentioned tools present before using the script.
'-ToolsDir' parameter can be used to specify a custom tools directory location.
Tools download links:
* otf2bdf - https://www.rockbox.org/wiki/bin/edit/Main/OTF2BDF?topicparent=Main.RockboxFontConvertor
* convbdf - http://qupada.orcon.net.nz/convbdf.exe
.PARAMETER Files
One or multiple ttf/otf fonts to convert separated by comma.
.PARAMETER Sizes
Comma-separated list of font sizes to be generated.
.PARAMETER OutDir
Output directory for generated font files.
Default value is a current directory.
.PARAMETER Verify
Check the exit code of external font convertion tools.
.PARAMETER KeepBDF
Keep BDF font file alongside the generated FNT files.
.PARAMETER ToolsDir
Path to a directory with otf2bdf.exe and convbdf.exe tools.
Default value is a script location directory.
.EXAMPLE
makefont.ps1 -Files font.ttf -Sizes 18
.EXAMPLE
makefont.ps1 -OutDir G:\.rockbox\fonts -Files Font1.ttf,Font2.ttf -Sizes 16,24,32,48
.NOTES
Author: x1unix
#>
param(
[Parameter(Mandatory=$true)]
[string[]] $Files,
[Parameter(Mandatory=$true)]
[ValidateRange(1,128)]
[int[]] $Sizes,
[string] $OutDir=".",
[string] $ToolsDir="$PSScriptRoot",
[boolean] $Verify=$False,
[boolean] $KeepBDF=$False
)
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
$stepCount=2
$OutDir=(Get-Item "$OutDir").FullName
$chunkPerPercent=100/($Sizes.Count*$Files.Count*$stepCount)
$percentDone=0
if (-not($KeepBDF))
{
$tempDir=New-TemporaryDirectory
}
else
{
$tempDir=$OutDir
}
for ($i = 0; $i -lt $Files.Count; $i++)
{
$file=$Files[$i]
if (-not(Test-Path -Path $file -PathType Leaf))
{
Write-Error -Message "File not exists - $file" -ErrorAction Stop
}
$fileName=(Get-ChildItem "$file").Name
$fullFilePath=(Get-ChildItem "$file").FullName
$baseFileName=(Split-Path "$fileName" -Leaf).Split('.')[0]
for ($j=0; $j -lt $Sizes.Count; $j++)
{
$size = $Sizes[$j]
$bdfFile="$tempDir\$size-$baseFileName.bdf"
$destFile="$OutDir\$size-$baseFileName.fnt"
Write-Progress -Activity "[$($i+1)/$($Files.Count)] Processing '$fileName' for ${size}pt..."`
-PercentComplete $percentDone `
-CurrentOperation "${size}pt - Converting TTF/OTF to BDF..."
$p = Start-Process -NoNewWindow `
-FilePath "$ToolsDir\otf2bdf.exe" `
-ArgumentList "-p $size -o $bdfFile $fullFilePath" `
-PassThru -Wait
if ($Verify -and $p.ExitCode -ne 0)
{
Write-Error "Failed to convert $fullFilePath to BDF, otf2bdf returned error $($p.ExitCode)" `
-ErrorAction Stop
}
$percentDone+=$chunkPerPercent
Write-Progress -Activity "[$($i+1)/$($Files.Count)] Processing '$fileName' for ${size}pt..."`
-PercentComplete $percentDone `
-CurrentOperation "${size}pt - Converting BDF to ROCKbox FNT..."
$p = Start-Process -NoNewWindow `
-FilePath "$ToolsDir\convbdf.exe" `
-ArgumentList "-f -o $destFile $bdfFile" `
-PassThru -Wait
if ($Verify -and $p.ExitCode -ne 0)
{
Write-Error "Failed to convert $bdfFile to FNT, convbdf returned error $($p.ExitCode)" `
-ErrorAction Stop
}
$percentDone+=$chunkPerPercent
}
}
Write-Progress -Activity "Removing temporary files..." -PercentComplete 99 `
-CurrentOperation "Deleting '$tempDir' directory..."
Remove-Item -Recurse $tempDir
Write-Host "Generated $($Sizes.Count*$Files.Count) files to '$OutDir'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment