Files
cleonos/scripts/gen-tty-psf.ps1

215 lines
6.1 KiB
PowerShell
Raw Permalink Normal View History

2026-04-11 16:46:10 +08:00
param(
2026-04-11 22:27:54 +08:00
[string]$OutputPath = "ramdisk/system/tty.psf",
[int]$Width = 12,
[int]$Height = 24,
[int]$GlyphCount = 256,
[int]$FontSize = 20,
[int]$OffsetX = 0,
[int]$OffsetY = 0,
[int]$AutoCenter = 1
2026-04-11 16:46:10 +08:00
)
Add-Type -AssemblyName System.Drawing
2026-04-11 22:27:54 +08:00
if ($Width -le 0 -or $Height -le 0) {
throw "Width and Height must be positive"
}
if ($GlyphCount -le 0) {
throw "GlyphCount must be positive"
}
2026-04-11 16:46:10 +08:00
2026-04-11 22:27:54 +08:00
$fontCandidates = @("Cascadia Mono", "Consolas", "Lucida Console")
2026-04-11 16:46:10 +08:00
$font = $null
2026-04-11 22:27:54 +08:00
2026-04-11 16:46:10 +08:00
foreach ($name in $fontCandidates) {
try {
2026-04-11 22:27:54 +08:00
$candidate = New-Object System.Drawing.Font($name, $FontSize, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Pixel)
if ($candidate.Name -eq $name) {
$font = $candidate
2026-04-11 16:46:10 +08:00
break
}
2026-04-11 22:27:54 +08:00
$candidate.Dispose()
2026-04-11 16:46:10 +08:00
} catch {
$font = $null
}
}
if ($null -eq $font) {
throw "No usable monospace font found for PSF generation"
}
2026-04-11 22:27:54 +08:00
function Set-U32LE {
param(
[byte[]]$Buffer,
[int]$Offset,
[uint32]$Value
)
$Buffer[$Offset + 0] = [byte]($Value -band 0xFF)
$Buffer[$Offset + 1] = [byte](($Value -shr 8) -band 0xFF)
$Buffer[$Offset + 2] = [byte](($Value -shr 16) -band 0xFF)
$Buffer[$Offset + 3] = [byte](($Value -shr 24) -band 0xFF)
}
function Pixel-On {
param([System.Drawing.Color]$Pixel)
return (($Pixel.R + $Pixel.G + $Pixel.B) -ge 384)
}
2026-04-12 17:47:16 +08:00
function Get-BBox {
param([System.Drawing.Bitmap]$Bmp)
2026-04-11 22:27:54 +08:00
2026-04-12 17:47:16 +08:00
$w = $Bmp.Width
$h = $Bmp.Height
$minX = $w
$minY = $h
2026-04-11 22:27:54 +08:00
$maxX = -1
$maxY = -1
2026-04-12 17:47:16 +08:00
for ($y = 0; $y -lt $h; $y++) {
for ($x = 0; $x -lt $w; $x++) {
if (Pixel-On ($Bmp.GetPixel($x, $y))) {
2026-04-11 22:27:54 +08:00
if ($x -lt $minX) { $minX = $x }
if ($y -lt $minY) { $minY = $y }
if ($x -gt $maxX) { $maxX = $x }
if ($y -gt $maxY) { $maxY = $y }
}
}
}
2026-04-12 17:47:16 +08:00
if ($maxX -lt 0 -or $maxY -lt 0) {
return @{ Empty = $true; MinX = 0; MinY = 0; MaxX = -1; MaxY = -1 }
2026-04-11 22:27:54 +08:00
}
2026-04-12 17:47:16 +08:00
return @{ Empty = $false; MinX = $minX; MinY = $minY; MaxX = $maxX; MaxY = $maxY }
2026-04-11 22:27:54 +08:00
}
$headerSize = 32
$bytesPerRow = (($Width + 7) -shr 3)
$bytesPerGlyph = $bytesPerRow * $Height
$totalSize = $headerSize + ($GlyphCount * $bytesPerGlyph)
2026-04-11 16:46:10 +08:00
$bytes = New-Object byte[] $totalSize
2026-04-11 22:27:54 +08:00
# PSF2 header
Set-U32LE -Buffer $bytes -Offset 0 -Value ([Convert]::ToUInt32("864AB572", 16))
Set-U32LE -Buffer $bytes -Offset 4 -Value 0
Set-U32LE -Buffer $bytes -Offset 8 -Value ([uint32]$headerSize)
Set-U32LE -Buffer $bytes -Offset 12 -Value 0
Set-U32LE -Buffer $bytes -Offset 16 -Value ([uint32]$GlyphCount)
Set-U32LE -Buffer $bytes -Offset 20 -Value ([uint32]$bytesPerGlyph)
Set-U32LE -Buffer $bytes -Offset 24 -Value ([uint32]$Height)
Set-U32LE -Buffer $bytes -Offset 28 -Value ([uint32]$Width)
2026-04-11 16:46:10 +08:00
for ($i = $headerSize; $i -lt $totalSize; $i++) {
$bytes[$i] = 0x00
}
2026-04-12 17:47:16 +08:00
$workW = [Math]::Max($Width * 4, 96)
$workH = [Math]::Max($Height * 4, 96)
$drawX = [Math]::Max(16, [Math]::Floor($workW / 4))
$drawY = [Math]::Max(16, [Math]::Floor($workH / 4))
$fmt = New-Object System.Drawing.StringFormat
$fmt.FormatFlags = [System.Drawing.StringFormatFlags]::NoClip
$fmt.Alignment = [System.Drawing.StringAlignment]::Near
$fmt.LineAlignment = [System.Drawing.StringAlignment]::Near
$rendered = @{}
$globalMinX = $workW
$globalMinY = $workH
$globalMaxX = -1
$globalMaxY = -1
2026-04-11 22:27:54 +08:00
for ($code = 32; $code -le [Math]::Min(126, $GlyphCount - 1); $code++) {
2026-04-11 16:46:10 +08:00
$ch = [char]$code
2026-04-12 17:47:16 +08:00
$bmp = New-Object System.Drawing.Bitmap $workW, $workH
$g = [System.Drawing.Graphics]::FromImage($bmp)
$g.Clear([System.Drawing.Color]::Black)
$g.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::SingleBitPerPixelGridFit
$g.DrawString([string]$ch, $font, [System.Drawing.Brushes]::White, $drawX, $drawY, $fmt)
$g.Dispose()
$bbox = Get-BBox -Bmp $bmp
if ($bbox.Empty -eq $false) {
if ($bbox.MinX -lt $globalMinX) { $globalMinX = $bbox.MinX }
if ($bbox.MinY -lt $globalMinY) { $globalMinY = $bbox.MinY }
if ($bbox.MaxX -gt $globalMaxX) { $globalMaxX = $bbox.MaxX }
if ($bbox.MaxY -gt $globalMaxY) { $globalMaxY = $bbox.MaxY }
}
$rendered[$code] = $bmp
}
if ($globalMaxX -lt $globalMinX -or $globalMaxY -lt $globalMinY) {
$fmt.Dispose()
$font.Dispose()
throw "No visible glyph pixels found during render"
}
$globalW = $globalMaxX - $globalMinX + 1
$globalH = $globalMaxY - $globalMinY + 1
$cropX = $globalMinX
$cropY = $globalMinY
if ($AutoCenter -ne 0 -and $globalW -lt $Width) {
$cropX = $cropX - [Math]::Floor(($Width - $globalW) / 2)
}
# Keep top anchored to preserve english punctuation/baseline relationships.
$cropX = $cropX + $OffsetX
$cropY = $cropY + $OffsetY
for ($code = 32; $code -le [Math]::Min(126, $GlyphCount - 1); $code++) {
$src = $rendered[$code]
2026-04-11 16:46:10 +08:00
$glyphOffset = $headerSize + ($code * $bytesPerGlyph)
2026-04-11 22:27:54 +08:00
for ($y = 0; $y -lt $Height; $y++) {
2026-04-12 17:47:16 +08:00
$srcY = $cropY + $y
if ($srcY -lt 0 -or $srcY -ge $workH) {
continue
}
2026-04-11 22:27:54 +08:00
$rowOffset = $glyphOffset + ($y * $bytesPerRow)
2026-04-11 16:46:10 +08:00
2026-04-11 22:27:54 +08:00
for ($x = 0; $x -lt $Width; $x++) {
2026-04-12 17:47:16 +08:00
$srcX = $cropX + $x
if ($srcX -lt 0 -or $srcX -ge $workW) {
continue
}
if (Pixel-On ($src.GetPixel($srcX, $srcY))) {
2026-04-11 22:27:54 +08:00
$byteIndex = ($x -shr 3)
$bitIndex = 7 - ($x -band 7)
$target = $rowOffset + $byteIndex
$bytes[$target] = [byte]($bytes[$target] -bor (1 -shl $bitIndex))
2026-04-11 16:46:10 +08:00
}
}
}
2026-04-12 17:47:16 +08:00
}
2026-04-11 16:46:10 +08:00
2026-04-12 17:47:16 +08:00
foreach ($bmp in $rendered.Values) {
$bmp.Dispose()
2026-04-11 16:46:10 +08:00
}
2026-04-12 17:47:16 +08:00
$fmt.Dispose()
2026-04-11 16:46:10 +08:00
$font.Dispose()
2026-04-11 22:27:54 +08:00
if ([System.IO.Path]::IsPathRooted($OutputPath)) {
$fullOutput = $OutputPath
} else {
$fullOutput = Join-Path (Resolve-Path '.') $OutputPath
}
$dir = Split-Path -Parent $fullOutput
2026-04-11 16:46:10 +08:00
if (-not [string]::IsNullOrWhiteSpace($dir)) {
New-Item -ItemType Directory -Force -Path $dir | Out-Null
}
[System.IO.File]::WriteAllBytes($fullOutput, $bytes)
2026-04-12 17:47:16 +08:00
Write-Output "Generated PSF2: $OutputPath (w=$Width, h=$Height, glyphs=$GlyphCount, bytes=$($bytes.Length), global_bbox=${globalW}x${globalH})"