Files
jobtrackingapp/docs/i18n/00/R02_font_probe.ps1
T

107 lines
5.3 KiB
PowerShell

# Offline I18N-00 research. Emit CSV to stdout, or compare with the saved observations.
[CmdletBinding()]
param([switch]$Check, [switch]$Exemplars)
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName PresentationCore
$root = (Resolve-Path (Join-Path $PSScriptRoot '../../..')).Path
$fontDir = Join-Path $root 'JobTrackerApi/DocumentFonts'
$probePath = Join-Path $PSScriptRoot 'evidence/runtime-capabilities-r01/node-results.json'
$utf8 = [System.Text.UTF8Encoding]::new($false, $true)
$probeText = [IO.File]::ReadAllText($probePath, $utf8).Replace("`r`n", "`n")
$hash = [Security.Cryptography.SHA256]::Create()
try {
$digest = [BitConverter]::ToString($hash.ComputeHash($utf8.GetBytes($probeText))).Replace('-', '').ToLowerInvariant()
} finally {
$hash.Dispose()
}
if ($digest -ne '41159ff2ed98e28d23b418abc46570b087767624f498ec2857e46aa96c7825c9') {
throw 'R01 locale sample source changed'
}
$probe = $probeText | ConvertFrom-Json
$fontFiles = (Get-Content -Raw -Encoding utf8 (Join-Path $fontDir 'provenance.json') | ConvertFrom-Json).files |
Where-Object { $_.file.EndsWith('.ttf') } | Sort-Object file
$declared = [IO.File]::ReadAllText((Join-Path $root 'JobTrackerApi/Services/CvDocumentFonts.cs'), $utf8)
$embeddedNames = [regex]::Matches($declared, '"([^"]+\.ttf)"') | ForEach-Object { $_.Groups[1].Value }
if (@(Compare-Object @($fontFiles.file) @($embeddedNames)).Count -or $fontFiles.Count -ne 31) {
throw 'Font manifest differs from the registered document faces'
}
$faces = [ordered]@{}
foreach ($file in $fontFiles) {
$path = Join-Path $fontDir $file.file
if ((Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash -ne $file.sha256) {
throw "Font digest mismatch for $($file.file)"
}
$faces[$file.file] = [Windows.Media.GlyphTypeface]::new([uri]$path).CharacterToGlyphMap
}
$outputName = 'FONT_PROBE_R02.csv'
$expectedTested = 56
$sources = @{}
if ($Exemplars) {
$raw = & python -X utf8 (Join-Path $PSScriptRoot 'R03_exemplars.py')
if ($LASTEXITCODE -ne 0) { throw 'ICU exemplar extraction failed' }
$corpus = $raw | ConvertFrom-Json
if ($corpus.icu -ne '72.1.0.4') { throw "Requalify changed Windows ICU version $($corpus.icu) against R03" }
foreach ($row in $corpus.results) { $sources[$row.tag] = $row }
if ($sources.Count -ne $probe.results.Count) { throw 'Exemplar tag count differs from R01' }
$outputName = 'FONT_EXEMPLARS_R03.csv'
$excluded = @($corpus.results | Where-Object reason | ForEach-Object tag | Sort-Object)
if (($excluded -join ',') -ne 'cnr-ME,fkv-NO,nb-NO,nn-NO') { throw 'Unexpected exemplar fallback identities' }
}
$rows = foreach ($locale in $probe.results) {
$tag = $locale.tag
$reason = ''
if ($Exemplars) {
if (!$sources.ContainsKey($tag)) { throw "Missing exemplar disposition for $tag" }
$reason = $sources[$tag].reason
$sample = $sources[$tag].sample
} elseif ($locale.canonicalChanged -or !$locale.supported.DisplayNames.Count -or
!$locale.supported.NumberFormat.Count -or !$locale.supported.DateTimeFormat.Count) {
$reason = if ($locale.canonicalChanged) { 'different-language-identity' } else { 'missing-locale-data' }
} else {
$sample = "$($locale.nativeName)$($locale.fixedUtcInstantInParis)$($locale.fixedEurAmount)"
}
if ($reason) {
[pscustomobject]@{ tag=$tag; observation=$reason; points=0; missing=''; faceCount=0; faces='' }
continue
}
$points = [Collections.Generic.SortedSet[int]]::new()
for ($i = 0; $i -lt $sample.Length; $i++) {
$point = [char]::ConvertToUtf32($sample, $i)
$category = [Globalization.CharUnicodeInfo]::GetUnicodeCategory($sample, $i)
if ($category -notin 'Control', 'Format') { [void]$points.Add($point) }
if ($point -gt 0xffff) { $i++ }
}
if (!$points.Count) { throw "Empty locale sample: $tag" }
$groups = @{}
foreach ($name in $faces.Keys) {
$missing = @(
foreach ($point in $points) {
if (!$faces[$name].ContainsKey($point) -or $faces[$name][$point] -eq 0) {
'U+{0:X4}' -f $point
}
}
) -join ' '
if (!$groups.ContainsKey($missing)) { $groups[$missing] = @() }
$groups[$missing] += $name
}
foreach ($missing in @($groups.Keys | Sort-Object)) {
[pscustomobject]@{ tag=$tag; observation='sample-tested'; points=$points.Count; missing=$missing;
faceCount=$groups[$missing].Count; faces=$groups[$missing] -join ' ' }
}
}
if ($Exemplars) {
foreach ($row in $rows) { $row | Add-Member -NotePropertyName exemplarSource -NotePropertyValue $sources[$row.tag].source }
}
$csv = @($rows | ConvertTo-Csv -NoTypeInformation)
if ($Check) {
$saved = [IO.File]::ReadAllLines((Join-Path $PSScriptRoot $outputName), $utf8)
if (($saved -join "`n") -cne ($csv -join "`n")) { throw 'Font observations changed' }
$tested = @($rows | Where-Object observation -eq 'sample-tested')
$pairs = ($tested | Measure-Object faceCount -Sum).Sum
if ($probe.results.Count -ne 60 -or $pairs -ne ($expectedTested * $faces.Count)) { throw 'Unexpected sample coverage' }
"Verified $($faces.Count) bound font faces, $pairs face/sample observations and $($probe.results.Count - $expectedTested) explicitly untested candidate tags."
} else {
$csv
}