876 lines
27 KiB
TypeScript
876 lines
27 KiB
TypeScript
/**
|
||
* R4.1 freeze O4 (plan 2026-10-17 §B.5.1): the text and number rules the
|
||
* Rust writer reproduces, computed with the TypeScript engine and pdf-lib
|
||
* 1.17.1. `afm.json`: the metrics and the WinAnsi table pdf-lib measures
|
||
* with (`@pdf-lib/standard-fonts`). `text.json`: `measure` over a corpus ×
|
||
* the three fonts × eight sizes, `wrapLines`, `safeText` over every code
|
||
* point, `toUpperCase`, JS `\s` and `trim`, `Math.round`, pdf-lib's
|
||
* `numberToString` and `rasterSizeOf`. `MAKE_R4_ORACLES=1` writes
|
||
* `crates/core/tests/oracles/r4/{afm,text}.json`.
|
||
*/
|
||
import { readFileSync } from 'node:fs'
|
||
import { resolve } from 'node:path'
|
||
import { beforeAll, describe, expect, it } from 'vitest'
|
||
import { Encodings, Font, FontNames } from '@pdf-lib/standard-fonts'
|
||
import {
|
||
numberToString,
|
||
PageSizes,
|
||
PDFDocument,
|
||
StandardFontEmbedder,
|
||
type PDFFont,
|
||
} from 'pdf-lib'
|
||
import type { Observation } from '@faunosteo/domain'
|
||
import { defaultPdfLabels } from '../src'
|
||
import { embedFonts } from '../src/render/fonts'
|
||
import { measure, wrapLines } from '../src/render/text'
|
||
import { rasterSizeOf, renderHighlightedView } from '../src/svg/view'
|
||
import { safeText } from '../src/text'
|
||
import { DRAFT_DIR, loadDraftSheet } from './helpers/draft'
|
||
import {
|
||
charOf,
|
||
classRanges,
|
||
codePointRanges,
|
||
environment,
|
||
freezeJson,
|
||
isSurrogate,
|
||
jsNumber,
|
||
MAX_CODE_POINT,
|
||
mulberry32,
|
||
} from './helpers/r4-oracle-io'
|
||
import {
|
||
loadGoldenReports,
|
||
REPORT_NAMES,
|
||
type GoldenReports,
|
||
} from './helpers/reports'
|
||
|
||
/** The fonts `embedFonts` embeds, in its order: bodyRegular, bodyBold, headingRegular. */
|
||
const FONT_NAMES = [
|
||
FontNames.Helvetica,
|
||
FontNames.HelveticaBold,
|
||
FontNames.TimesRoman,
|
||
]
|
||
const SIZES = [8, 9, 10, 11, 14, 22, 9.5, 13.333333333333334]
|
||
const SEED = 0x52344f34
|
||
const RANDOM_WINANSI = 500
|
||
const RANDOM_UNICODE = 200
|
||
|
||
let fonts: PDFFont[]
|
||
let golden: GoldenReports
|
||
beforeAll(async () => {
|
||
const embedded = await embedFonts(await PDFDocument.create())
|
||
fonts = [embedded.bodyRegular, embedded.bodyBold, embedded.headingRegular]
|
||
golden = loadGoldenReports()
|
||
})
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// afm.json
|
||
|
||
/**
|
||
* StandardFontEmbedder of pdf-lib 1.17.1 (es build; the cjs build vitest loads
|
||
* differs only by the `utils_1.` prefix), quoted for the port.
|
||
*/
|
||
const EMBEDDER_SOURCE = [
|
||
'StandardFontEmbedder.prototype.widthOfTextAtSize = function (text, size) {',
|
||
' var glyphs = this.encodeTextAsGlyphs(text);',
|
||
' var totalWidth = 0;',
|
||
' for (var idx = 0, len = glyphs.length; idx < len; idx++) {',
|
||
' var left = glyphs[idx].name;',
|
||
' var right = (glyphs[idx + 1] || {}).name;',
|
||
' var kernAmount = this.font.getXAxisKerningForPair(left, right) || 0;',
|
||
' totalWidth += this.widthOfGlyph(left) + kernAmount;',
|
||
' }',
|
||
' var scale = size / 1000;',
|
||
' return totalWidth * scale;',
|
||
'};',
|
||
'StandardFontEmbedder.prototype.widthOfGlyph = function (glyphName) {',
|
||
" // Default to 250 if font doesn't specify a width",
|
||
' return this.font.getWidthOfGlyph(glyphName) || 250;',
|
||
'};',
|
||
'StandardFontEmbedder.prototype.encodeTextAsGlyphs = function (text) {',
|
||
' var codePoints = Array.from(text);',
|
||
' var glyphs = new Array(codePoints.length);',
|
||
' for (var idx = 0, len = codePoints.length; idx < len; idx++) {',
|
||
' var codePoint = toCodePoint(codePoints[idx]);',
|
||
' glyphs[idx] = this.encoding.encodeUnicodeCodePoint(codePoint);',
|
||
' }',
|
||
' return glyphs;',
|
||
'};',
|
||
]
|
||
|
||
/** @pdf-lib/standard-fonts 1.0.0 Font.js, the two lookups the embedder calls. */
|
||
const FONT_SOURCE = [
|
||
'this.getWidthOfGlyph = function (glyphName) {',
|
||
' return _this.CharWidths[glyphName];',
|
||
'};',
|
||
'this.getXAxisKerningForPair = function (leftGlyphName, rightGlyphName) {',
|
||
' return (_this.KernPairXAmounts[leftGlyphName] || {})[rightGlyphName];',
|
||
'};',
|
||
'// CharWidths: CharMetrics reduced name -> WX; KernPairXAmounts: KernPairs reduced',
|
||
'// left -> right -> amount (a later duplicate would win; the three fonts have none).',
|
||
]
|
||
|
||
const normalised = (code: string): string =>
|
||
code.replace(/\b\w+_\d+\./g, '').replace(/\s+/g, ' ')
|
||
|
||
/** Every quoted body line, whitespace-normalised, is in the installed code. */
|
||
function assertQuoted(quote: string[], installed: string): void {
|
||
const code = normalised(installed)
|
||
for (const line of quote.filter(
|
||
(l) => l.startsWith(' ') && !l.includes('//'),
|
||
)) {
|
||
expect(code, line).toContain(normalised(line.trim()))
|
||
}
|
||
}
|
||
|
||
function assertQuoteMatchesInstalled(): void {
|
||
const proto = StandardFontEmbedder.prototype as unknown as Record<
|
||
string,
|
||
() => void
|
||
>
|
||
const methods = ['widthOfTextAtSize', 'widthOfGlyph', 'encodeTextAsGlyphs']
|
||
assertQuoted(
|
||
EMBEDDER_SOURCE,
|
||
methods.map((name) => proto[name]!.toString()).join('\n'),
|
||
)
|
||
const font = Font.load(FontNames.Helvetica)
|
||
const lookups = [font.getWidthOfGlyph, font.getXAxisKerningForPair]
|
||
assertQuoted(FONT_SOURCE, lookups.map((fn) => fn.toString()).join('\n'))
|
||
}
|
||
|
||
type WinAnsiRow = [number, number, string]
|
||
|
||
/** `Encodings.WinAnsi`: code point → [byte code, glyph name], code point order. */
|
||
function winAnsiTable(): WinAnsiRow[] {
|
||
const encoding = Encodings.WinAnsi
|
||
return encoding.supportedCodePoints.map((cp) => {
|
||
const { code, name } = encoding.encodeUnicodeCodePoint(cp)
|
||
return [cp, code, name]
|
||
})
|
||
}
|
||
|
||
function fontMetrics(fontName: FontNames, table: WinAnsiRow[]) {
|
||
const font = Font.load(fontName)
|
||
const glyphs: Record<string, [number, number[]]> = {}
|
||
for (const { N, WX } of font.CharMetrics) {
|
||
const codes = table
|
||
.filter(([, , name]) => name === N)
|
||
.map(([, code]) => code)
|
||
glyphs[N] = [WX, [...new Set(codes)].sort((a, b) => a - b)]
|
||
}
|
||
const kerning: Record<string, Record<string, number>> = {}
|
||
for (const [left, right, amount] of font.KernPairs) {
|
||
kerning[left] = { ...kerning[left], [right]: amount }
|
||
}
|
||
/** The AFM header fields: every own value that is neither a table nor a method. */
|
||
const scalars = Object.fromEntries(
|
||
Object.entries(font).filter(
|
||
([, value]) => typeof value !== 'function' && typeof value !== 'object',
|
||
),
|
||
)
|
||
return {
|
||
...scalars,
|
||
FontBBox: font.FontBBox,
|
||
charMetricsCount: font.CharMetrics.length,
|
||
kernPairsCount: font.KernPairs.length,
|
||
winAnsiGlyphsWithoutWidth: [
|
||
...new Set(table.map(([, , name]) => name)),
|
||
].filter((name) => font.getWidthOfGlyph(name) === undefined),
|
||
glyphs,
|
||
kerning,
|
||
}
|
||
}
|
||
|
||
function buildAfm() {
|
||
assertQuoteMatchesInstalled()
|
||
const table = winAnsiTable()
|
||
return {
|
||
comment: [
|
||
'O4 metrics: what pdf-lib 1.17.1 measures with (@pdf-lib/standard-fonts 1.0.0, Font.load).',
|
||
'glyphs: name -> [WX, WinAnsi byte codes that print this glyph]; no AFM C field is kept by',
|
||
'the package. kerning: left -> right -> KPX amount, every pair of the font. winAnsi: code',
|
||
'point -> [byte code, glyph name] for every code point Encodings.WinAnsi can encode (note',
|
||
'376 U+0178 -> [159, "ydieresis"], 160 -> "space", 173 -> "hyphen", as pdf-lib has them).',
|
||
'Width of a string: sum over its glyphs of (WX || 250) + (KPX(left, next) || 0), times',
|
||
'size / 1000 (measuredWith below). The fonts carry no APAFML notice in this package:',
|
||
'Notice/Comment are the AFM header fields as pdf-lib stores them.',
|
||
],
|
||
measuredWith: [...EMBEDDER_SOURCE, ...FONT_SOURCE],
|
||
environment: environment(),
|
||
fonts: Object.fromEntries(
|
||
FONT_NAMES.map((name) => [name, fontMetrics(name, table)]),
|
||
),
|
||
winAnsi: table,
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// text.json: measure
|
||
|
||
/** Every `{ fr, en }` label found under a `name` key of the draft document. */
|
||
function sheetNames(): string[] {
|
||
const out: string[] = []
|
||
const walk = (value: unknown, key: string): void => {
|
||
if (Array.isArray(value)) return value.forEach((item) => walk(item, key))
|
||
if (!value || typeof value !== 'object') return
|
||
const record = value as Record<string, unknown>
|
||
if (
|
||
key === 'name' &&
|
||
typeof record.fr === 'string' &&
|
||
typeof record.en === 'string'
|
||
) {
|
||
out.push(record.fr, record.en)
|
||
return
|
||
}
|
||
for (const [k, v] of Object.entries(record)) walk(v, k)
|
||
}
|
||
walk(loadDraftSheet().document, '')
|
||
return out
|
||
}
|
||
|
||
function stringLeaves(value: unknown): string[] {
|
||
if (typeof value === 'string') return [value]
|
||
if (Array.isArray(value)) return value.flatMap(stringLeaves)
|
||
if (value && typeof value === 'object')
|
||
return Object.values(value).flatMap(stringLeaves)
|
||
return []
|
||
}
|
||
|
||
const pdfLabelTexts = (): string[] => [
|
||
...Object.values(defaultPdfLabels.fr),
|
||
...Object.values(defaultPdfLabels.en),
|
||
]
|
||
|
||
function randomText(
|
||
rng: () => number,
|
||
alphabet: number[],
|
||
maxLength: number,
|
||
): string {
|
||
const length = 1 + Math.floor(rng() * maxLength)
|
||
return Array.from({ length }, () =>
|
||
String.fromCodePoint(alphabet[Math.floor(rng() * alphabet.length)]!),
|
||
).join('')
|
||
}
|
||
|
||
/** A scalar value (never a surrogate) from one of four bands: ASCII, 2-byte, BMP, astral. */
|
||
function randomScalar(rng: () => number): number {
|
||
const bands: Array<[number, number]> = [
|
||
[0, 0x7f],
|
||
[0x80, 0x7ff],
|
||
[0x800, 0xffff],
|
||
[0x10000, MAX_CODE_POINT],
|
||
]
|
||
const [low, high] = bands[Math.floor(rng() * bands.length)]!
|
||
for (;;) {
|
||
const cp = low + Math.floor(rng() * (high - low + 1))
|
||
if (!isSurrogate(cp)) return cp
|
||
}
|
||
}
|
||
|
||
function randomUnicodeText(rng: () => number): string {
|
||
const length = 1 + Math.floor(rng() * 16)
|
||
return Array.from({ length }, () =>
|
||
String.fromCodePoint(randomScalar(rng)),
|
||
).join('')
|
||
}
|
||
|
||
interface CorpusGroups {
|
||
sheetNames: string[]
|
||
pdfLabels: string[]
|
||
reports: string[]
|
||
randomWinAnsi: string[]
|
||
randomUnicode: string[]
|
||
}
|
||
|
||
/** Each fixed text and its upper-case form; a text already seen in an earlier group is not repeated. */
|
||
function corpus(): CorpusGroups {
|
||
const seen = new Set<string>()
|
||
const fresh = (texts: string[]) =>
|
||
texts.filter((text) => !seen.has(text) && seen.add(text))
|
||
const withUpper = (texts: string[]) =>
|
||
fresh(texts.flatMap((text) => [text, text.toUpperCase()]))
|
||
const rng = mulberry32(SEED)
|
||
const winAnsi = Encodings.WinAnsi.supportedCodePoints
|
||
return {
|
||
sheetNames: withUpper(sheetNames()),
|
||
pdfLabels: withUpper(pdfLabelTexts()),
|
||
reports: withUpper(
|
||
REPORT_NAMES.flatMap((name) => stringLeaves(golden.reports[name])),
|
||
),
|
||
randomWinAnsi: Array.from({ length: RANDOM_WINANSI }, () =>
|
||
randomText(rng, winAnsi, 40),
|
||
),
|
||
randomUnicode: Array.from({ length: RANDOM_UNICODE }, () =>
|
||
randomUnicodeText(rng),
|
||
),
|
||
}
|
||
}
|
||
|
||
/** The width rule of StandardFontEmbedder, as the Rust port must write it. */
|
||
const WIDTH_FORMULA = 'units * (size / 1000)'
|
||
const SAMPLE_HEAD = 100
|
||
const SAMPLE_DRAWN = 100
|
||
const SAMPLE_SEED = SEED + 2
|
||
|
||
/** Flat row indices that keep their widths: the first 100, then 100 distinct others drawn by the PRNG. */
|
||
function widthSample(total: number): number[] {
|
||
const rng = mulberry32(SAMPLE_SEED)
|
||
const head = Math.min(SAMPLE_HEAD, total)
|
||
const picked = new Set(Array.from({ length: head }, (_, i) => i))
|
||
const target = Math.min(total, SAMPLE_HEAD + SAMPLE_DRAWN)
|
||
while (picked.size < target) {
|
||
picked.add(SAMPLE_HEAD + Math.floor(rng() * (total - SAMPLE_HEAD)))
|
||
}
|
||
return [...picked].sort((a, b) => a - b)
|
||
}
|
||
|
||
interface MeasureRow {
|
||
text: string
|
||
safe: string
|
||
units: number[]
|
||
widths?: number[][]
|
||
}
|
||
|
||
/**
|
||
* One text: its safeText form and the raw unit sum per font (size 1000).
|
||
* Every font × size is checked against the formula; the widths are kept
|
||
* for sampled rows only.
|
||
*/
|
||
function measureRow(text: string, keepWidths: boolean): MeasureRow {
|
||
const safe = safeText(text)
|
||
const units = fonts.map((font) => font.widthOfTextAtSize(safe, 1000))
|
||
const widths = fonts.map((font, f) =>
|
||
SIZES.map((size) => {
|
||
const width = measure(font, text, size)
|
||
expect(width).toBe(units[f]! * (size / 1000))
|
||
expect(width).toBe(font.widthOfTextAtSize(safe, size))
|
||
return width
|
||
}),
|
||
)
|
||
return keepWidths ? { text, safe, units, widths } : { text, safe, units }
|
||
}
|
||
|
||
function measureGroups(groups: CorpusGroups, sample: Set<number>) {
|
||
const rows: Record<string, MeasureRow[]> = {}
|
||
let offset = 0
|
||
for (const [name, texts] of Object.entries(groups) as Array<
|
||
[string, string[]]
|
||
>) {
|
||
rows[name] = texts.map((text, i) =>
|
||
measureRow(text, sample.has(offset + i)),
|
||
)
|
||
offset += texts.length
|
||
}
|
||
return rows
|
||
}
|
||
|
||
function buildMeasure() {
|
||
const groups = corpus()
|
||
const total = Object.values(groups).reduce((n, texts) => n + texts.length, 0)
|
||
const sample = widthSample(total)
|
||
return {
|
||
comment: [
|
||
'measure(font, text, size) = font.widthOfTextAtSize(safeText(text), size) (render/text.ts).',
|
||
'units[f] = widthOfTextAtSize(safe, 1000): the raw sum of glyph widths and kerning for',
|
||
'fonts[f]. The generator asserts, for every row, font and size, measure === widthFormula',
|
||
'evaluated in JavaScript (f64, this exact expression and order); widths[f][s] (sizes[s]) is',
|
||
'kept only on the widthSample rows. Fixed groups hold each text and its toUpperCase(); a',
|
||
'text is listed once, in the first group that has it. Random groups use mulberry32(seed):',
|
||
'randomWinAnsi draws 1-40 code points of Encodings.WinAnsi, randomUnicode 1-16 scalar',
|
||
'values from four bands (ASCII, 2-byte, rest of the BMP, astral), no surrogate.',
|
||
],
|
||
fonts: fonts.map((font) => font.name),
|
||
sizes: SIZES,
|
||
widthFormula: WIDTH_FORMULA,
|
||
seed: SEED,
|
||
counts: Object.fromEntries(
|
||
Object.entries(groups).map(([k, v]) => [k, (v as string[]).length]),
|
||
),
|
||
widthSample: {
|
||
rule: [
|
||
'Rows indexed in file order across the groups (sheetNames, pdfLabels, reports,',
|
||
`randomWinAnsi, randomUnicode), from 0. Kept: indices 0..${SAMPLE_HEAD - 1}, then`,
|
||
`${SAMPLE_DRAWN} distinct indices drawn as ${SAMPLE_HEAD} + floor(rng() * (total - ${SAMPLE_HEAD}))`,
|
||
'with rng = mulberry32(seed), a repeat drawn again; listed ascending.',
|
||
],
|
||
seed: SAMPLE_SEED,
|
||
total,
|
||
indices: sample,
|
||
},
|
||
groups: measureGroups(groups, new Set(sample)),
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// text.json: wrapLines
|
||
|
||
/** `width - MARGIN_X * 2` on A4, the only maxWidth the observation and conclusion pages pass. */
|
||
const LAYOUT_MAX_WIDTH = PageSizes.A4[0] - 36 * 2
|
||
const WRAP_WIDTHS = [LAYOUT_MAX_WIDTH, 0, 1, 50, 100, 200, 300, 1000]
|
||
/** wrapLines is only called with bodyRegular: size 10 (observation sections), 11 (conclusion). */
|
||
const WRAP_SIZES = [10, 11]
|
||
|
||
function reportLongTexts(): string[] {
|
||
return REPORT_NAMES.flatMap((name) => {
|
||
const report = golden.reports[name]
|
||
const next = report.nextAppointment
|
||
return [
|
||
report.conclusion,
|
||
report.recommendations,
|
||
next && next.kind === 'period' ? next.text : '',
|
||
...report.observations.flatMap((o) => [
|
||
o.comment,
|
||
o.comment.trim(),
|
||
...o.freeTextFindings,
|
||
]),
|
||
...report.observations.map((o) => o.freeTextFindings.join(' · ')),
|
||
]
|
||
})
|
||
}
|
||
|
||
function edgeWrapTexts(): string[] {
|
||
const document = loadDraftSheet().document
|
||
const vocabulary = sheetNames()
|
||
.filter((_, i) => i % 2 === 0)
|
||
.slice(0, 60)
|
||
return [
|
||
document.scope_disclaimer.fr ?? '',
|
||
document.scope_disclaimer.en ?? '',
|
||
vocabulary.join(' '),
|
||
'',
|
||
' ',
|
||
'\t\n',
|
||
' leading and trailing ',
|
||
'a\u00a0b\u2028c\ufeffd\u3000e\u1680f\u205fg',
|
||
'word\u200bjoined by a zero width space',
|
||
'x\u0007y z',
|
||
'\u0007 \u0007 word \u0007',
|
||
'Supercalifragilisticexpialidocious'.repeat(3),
|
||
'short Supercalifragilisticexpialidociouscalifragilistic tail',
|
||
'emoji 😀 and CJK 漢字 and ☃ and → arrows',
|
||
'Œuvre « guillemets » – tiret — long … points € ‰ ™',
|
||
]
|
||
}
|
||
|
||
function buildWrap() {
|
||
const seen = new Set<string>()
|
||
const texts = [
|
||
...reportLongTexts(),
|
||
...pdfLabelTexts(),
|
||
...edgeWrapTexts(),
|
||
].filter((text) => !seen.has(text) && seen.add(text))
|
||
const font = fonts[0]!
|
||
return {
|
||
comment: [
|
||
'wrapLines(text, font, size, maxWidth) (render/text.ts): split on JS /\\s+/, safeText per',
|
||
'word, greedy join with one space. lines[s][w] for sizes[s] and maxWidths[w]; maxWidths[0]',
|
||
'is the layout value A4 width - 36 * 2 as JavaScript computes it.',
|
||
],
|
||
font: font.name,
|
||
sizes: WRAP_SIZES,
|
||
maxWidths: WRAP_WIDTHS,
|
||
cases: texts.map((text) => ({
|
||
text,
|
||
lines: WRAP_SIZES.map((size) =>
|
||
WRAP_WIDTHS.map((max) => wrapLines(text, font, size, max)),
|
||
),
|
||
})),
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// text.json: code point tables
|
||
|
||
type SafeClass = 'kept' | 'dropped' | 'question'
|
||
|
||
function safeClassOf(cp: number): SafeClass {
|
||
const char = charOf(cp)
|
||
const out = safeText(char)
|
||
if (out === char) return 'kept'
|
||
if (out === '') return 'dropped'
|
||
if (out === '?') return 'question'
|
||
throw new Error(`safeText(${cp}) gave ${JSON.stringify(out)}`)
|
||
}
|
||
|
||
type UpperRun = [number, number, number, number]
|
||
|
||
/** Single-code-point mappings as runs [start, end, step, delta]: start, start + step, … end map to cp + delta. */
|
||
function upperRuns(single: Array<[number, number]>): UpperRun[] {
|
||
const runs: UpperRun[] = []
|
||
for (const [cp, mapped] of single) {
|
||
const delta = mapped - cp
|
||
const last = runs.at(-1)
|
||
const step = last ? cp - last[1] : 0
|
||
const joins =
|
||
last &&
|
||
last[3] === delta &&
|
||
(last[0] === last[1] ? step <= 2 : step === last[2])
|
||
if (last && joins) {
|
||
last[2] = step
|
||
last[1] = cp
|
||
} else runs.push([cp, cp, 1, delta])
|
||
}
|
||
return runs
|
||
}
|
||
|
||
function expandRuns(runs: UpperRun[]): Array<[number, number]> {
|
||
return runs.flatMap(([start, end, step, delta]) => {
|
||
const out: Array<[number, number]> = []
|
||
for (let cp = start; cp <= end; cp += step) out.push([cp, cp + delta])
|
||
return out
|
||
})
|
||
}
|
||
|
||
function buildUpper() {
|
||
const single: Array<[number, number]> = []
|
||
const special: Array<[number, number[]]> = []
|
||
for (let cp = 0; cp <= MAX_CODE_POINT; cp += 1) {
|
||
if (isSurrogate(cp)) continue
|
||
const char = String.fromCodePoint(cp)
|
||
const upper = char.toUpperCase()
|
||
if (upper === char) continue
|
||
const mapped = Array.from(upper, (c) => c.codePointAt(0)!)
|
||
if (mapped.length === 1) single.push([cp, mapped[0]!])
|
||
else special.push([cp, mapped])
|
||
}
|
||
const runs = upperRuns(single)
|
||
expect(expandRuns(runs)).toEqual(single)
|
||
return {
|
||
comment: [
|
||
'String.fromCodePoint(cp).toUpperCase() for every non-surrogate code point where it differs.',
|
||
'runs: [start, end, step, delta]: start, start + step, ... end each map to cp + delta (one',
|
||
'code point). special: [cp, result code points] when the result is not one code point.',
|
||
'Any other code point maps to itself.',
|
||
],
|
||
singleCount: single.length,
|
||
runs,
|
||
special,
|
||
}
|
||
}
|
||
|
||
const isJsWhitespace = (cp: number): boolean =>
|
||
!isSurrogate(cp) && /\s/.test(String.fromCodePoint(cp))
|
||
const isJsTrimmed = (cp: number): boolean =>
|
||
!isSurrogate(cp) && `${String.fromCodePoint(cp)}a`.trim() === 'a'
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// text.json: numbers
|
||
|
||
const HALF_TIES = [
|
||
0.5, -0.5, 1.5, -1.5, 2.5, -2.5, 3.5, -3.5, 4.5, 99.5, 1199.5, 2399.5,
|
||
]
|
||
const ROUND_EDGES = [
|
||
0.49999999999999994,
|
||
-0.49999999999999994,
|
||
0.5000000000000001,
|
||
1.4999999999999998,
|
||
2 ** 52 + 0.5,
|
||
2 ** 52 - 0.5,
|
||
-(2 ** 52) + 0.5,
|
||
2 ** 53,
|
||
2 ** 53 + 2,
|
||
1e21,
|
||
1.7976931348623157e308,
|
||
5e-324,
|
||
-5e-324,
|
||
1e-300,
|
||
0,
|
||
-0,
|
||
0.4,
|
||
-0.4,
|
||
0.6,
|
||
-0.6,
|
||
100.49999999999999,
|
||
Infinity,
|
||
-Infinity,
|
||
NaN,
|
||
1200 * 0.2,
|
||
1200 * 0.6,
|
||
1200 * 1.2345,
|
||
1200 * (943 / 1200),
|
||
1200 * (942.5 / 1200),
|
||
]
|
||
|
||
function mathRoundInputs(rng: () => number): number[] {
|
||
const random = Array.from({ length: 50 }, () => (rng() - 0.5) * 6000)
|
||
const halves = Array.from(
|
||
{ length: 50 },
|
||
() => Math.floor((rng() - 0.5) * 2e6) + 0.5,
|
||
)
|
||
const raster = Array.from({ length: 30 }, () => 1200 * (0.2 + rng() * 1.8))
|
||
return [...HALF_TIES, ...ROUND_EDGES, ...random, ...halves, ...raster]
|
||
}
|
||
|
||
const LAYOUT_SUMS = [
|
||
36 + 523.28 / 2,
|
||
595.28 - 36,
|
||
841.89 - 95,
|
||
841.89 - 122,
|
||
841.89 - 122 - 12,
|
||
(595.28 - 72 - 16) / 2,
|
||
(595.28 - 72 - 16) / 2 / 2,
|
||
595.28 - 36 - 50.5,
|
||
800 - 12 * 3,
|
||
(14 - 10) * 0.3,
|
||
1 - 0.9,
|
||
0.1 + 0.2,
|
||
0.1 * 3,
|
||
523.28 * 0.6,
|
||
253.64 * 0.7876,
|
||
841.89 - 48,
|
||
36 + 253.64 + 16,
|
||
595.28 / 2 - 50,
|
||
]
|
||
const COLOUR_COMPONENTS = [
|
||
0.12, 0.11, 0.09, 0.42, 0.39, 0.35, 0.72, 0.25, 0.55, 0.65, 0.45, 0.85, 0.18,
|
||
0.78, 0.2,
|
||
]
|
||
const INTEGERS = [
|
||
0,
|
||
1,
|
||
2,
|
||
36,
|
||
72,
|
||
595,
|
||
842,
|
||
1000,
|
||
2 ** 31,
|
||
2 ** 32,
|
||
2 ** 53,
|
||
1e15,
|
||
1e20,
|
||
123456789,
|
||
-1,
|
||
-842,
|
||
]
|
||
const TINY_RESIDUES = [
|
||
0.1 + 0.2 - 0.3,
|
||
1.0000000000001 - 1,
|
||
3 * 0.1 - 0.3,
|
||
523.28 - 523.2799999999999,
|
||
1e-13,
|
||
1.5e-13,
|
||
9.999999999999999e-14,
|
||
0.30000000000000004 - 0.3,
|
||
0.000001,
|
||
0.0000009999999,
|
||
5e-324,
|
||
1e-300,
|
||
2.2250738585072014e-308,
|
||
-1e-7,
|
||
-1.5e-13,
|
||
-(0.1 + 0.2 - 0.3),
|
||
]
|
||
const LARGE = [
|
||
1e21,
|
||
1.5e21,
|
||
9.999999999999999e21,
|
||
1e22,
|
||
1.2345678901234567e25,
|
||
2 ** 70,
|
||
2 ** 100,
|
||
1e100,
|
||
1.7976931348623157e308,
|
||
-1e21,
|
||
-3.3e25,
|
||
999999999999999900000,
|
||
1e21 - 65536,
|
||
]
|
||
const SPECIAL_NUMBERS = [NaN, Infinity, -Infinity, -0]
|
||
|
||
function numberInputs(rng: () => number): number[] {
|
||
const n = (count: number, make: () => number) =>
|
||
Array.from({ length: count }, make)
|
||
const widths = fonts.map((font) =>
|
||
measure(font, 'Restriction en flexion', 10),
|
||
)
|
||
return [
|
||
...n(300, () => rng() * 842),
|
||
...n(150, () => rng()),
|
||
...n(50, () => -rng() * 842),
|
||
...n(50, () => -rng()),
|
||
...INTEGERS,
|
||
...LAYOUT_SUMS,
|
||
...COLOUR_COMPONENTS,
|
||
...widths.flatMap((w) => [595.28 - 36 - w, (595.28 - w) / 2, w + 10]),
|
||
...n(14, () => 0).map((_, i) => 10 ** -(i + 7)),
|
||
...n(28, () => (1 + rng() * 9) * 10 ** -(7 + Math.floor(rng() * 14))),
|
||
...TINY_RESIDUES,
|
||
...LARGE,
|
||
...n(20, () => (1 + rng() * 9) * 10 ** (21 + Math.floor(rng() * 280))),
|
||
...SPECIAL_NUMBERS,
|
||
]
|
||
}
|
||
|
||
function buildNumbers() {
|
||
const rng = mulberry32(SEED + 1)
|
||
return {
|
||
mathRound: {
|
||
comment:
|
||
'Math.round(input): [input, output] as JavaScript prints them, "-0" kept, NaN/Infinity by name.',
|
||
seed: SEED + 1,
|
||
cases: mathRoundInputs(rng).map((x) => [
|
||
jsNumber(x),
|
||
jsNumber(Math.round(x)),
|
||
]),
|
||
},
|
||
numberToString: {
|
||
comment: [
|
||
'pdf-lib 1.17.1 numberToString (utils/numbers.js, as loaded), the operand printer of every',
|
||
'content stream operator and PDFNumber: [String(input) ("-0" for -0), output]. Source below.',
|
||
],
|
||
source: numberToString.toString().split('\n'),
|
||
seed: SEED + 1,
|
||
cases: numberInputs(rng).map((x) => [jsNumber(x), numberToString(x)]),
|
||
},
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// text.json: rasterSizeOf
|
||
|
||
const VIEW_BOXES: Array<string | null> = [
|
||
null,
|
||
'',
|
||
'0 0 100 50',
|
||
'0 0 1200 943',
|
||
'0,0,100,50',
|
||
'0, 0, 100, 50',
|
||
' 0 0 100 50 ',
|
||
'\t0\n0\r100\u00a050\ufeff',
|
||
'0\u20280\u3000100\u205f50',
|
||
'0 0 0x10 0x20',
|
||
'0 0 Infinity 50',
|
||
'0 0 100 Infinity',
|
||
'0 0 -Infinity 50',
|
||
'0 0 NaN 50',
|
||
'0 0 -100 50',
|
||
'0 0 100 -50',
|
||
'0 0 0 50',
|
||
'0 0 100 0',
|
||
'0 0 -0 50',
|
||
'0 0 100',
|
||
'0 0',
|
||
'0',
|
||
'0 0 100 50 7',
|
||
'0 0 100 1000',
|
||
'0 0 1000 1',
|
||
'0 0 1e2 5e1',
|
||
'0 0 1E2 5E1',
|
||
'0 0 1e999 50',
|
||
'0 0 100 1e-400',
|
||
'0 0 5e-324 1',
|
||
'0 0 .5 .25',
|
||
'0 0 5. 2.',
|
||
'0 0 +100 +50',
|
||
'0 0 0b11 0o7',
|
||
'0 0 1_000 500',
|
||
'0 0 100px 50px',
|
||
',0 0 100 50',
|
||
'0 0 100 50,',
|
||
'0 0 100,,50',
|
||
'a b c d',
|
||
'-50 -50 100 50',
|
||
'0 0 100 50.5',
|
||
'0 0 1200 942.5',
|
||
'0 0 3 2',
|
||
'0 0 7 3',
|
||
'0 0 2400 1885',
|
||
'0 0 100 20',
|
||
'0 0 100 200',
|
||
'0 0 100 19.999',
|
||
'0 0 100 200.001',
|
||
'0 0 1 0.6000000000000001',
|
||
'0 0 100 0x',
|
||
'0 0 ١٠٠ ٥٠',
|
||
'0 0 100 50',
|
||
]
|
||
|
||
const VIEW_BOX_ATTRIBUTE = /<svg\b[^>]*\sviewBox="([^"]*)"/
|
||
|
||
/** The viewBox of every drawing the draft sheet bundles, as its asset text declares it. */
|
||
function draftViewBoxes(): string[] {
|
||
const assets = JSON.parse(
|
||
readFileSync(resolve(DRAFT_DIR, 'assets.json'), 'utf8'),
|
||
) as Array<{ file: string }>
|
||
return assets.flatMap(({ file }) => {
|
||
const match = VIEW_BOX_ATTRIBUTE.exec(
|
||
readFileSync(resolve(DRAFT_DIR, 'assets', file), 'utf8'),
|
||
)
|
||
return match ? [match[1]!] : []
|
||
})
|
||
}
|
||
|
||
/** The viewBox `rasterSizeOf` reads for each view of the draft: the composed root's, view framing applied. */
|
||
function composedViewBoxes(): string[] {
|
||
const sheet = golden.snapshots.demo
|
||
const none = {
|
||
id: 'none',
|
||
zones: [],
|
||
findings: [],
|
||
freeTextFindings: [],
|
||
comment: '',
|
||
} as unknown as Observation
|
||
return sheet.document.views.map((view) => {
|
||
const { svg } = renderHighlightedView(sheet, view.id, none)
|
||
return VIEW_BOX_ATTRIBUTE.exec(svg)?.[1] ?? ''
|
||
})
|
||
}
|
||
|
||
function buildRasterSizes() {
|
||
return {
|
||
comment: [
|
||
'rasterSizeOf(viewBox) (svg/view.ts): [input (null = attribute absent), widthPx, heightPx].',
|
||
'The last rows are the draft drawing viewBoxes, then the composed view roots (main, head).',
|
||
],
|
||
cases: [...VIEW_BOXES, ...draftViewBoxes(), ...composedViewBoxes()].map(
|
||
(viewBox) => {
|
||
const { widthPx, heightPx } = rasterSizeOf(viewBox)
|
||
return [viewBox, widthPx, heightPx]
|
||
},
|
||
),
|
||
}
|
||
}
|
||
|
||
function buildText() {
|
||
return {
|
||
comment: [
|
||
'O4 text and numbers, frozen from the TypeScript engine (packages/pdf) and pdf-lib 1.17.1.',
|
||
'Code point tables depend on the V8 / ICU / Unicode versions in environment.',
|
||
],
|
||
environment: environment(),
|
||
measure: buildMeasure(),
|
||
wrapLines: buildWrap(),
|
||
safeText: {
|
||
comment: [
|
||
'safeText(one code point) over 0..0x10FFFF, surrogates as lone single-unit strings:',
|
||
'[start, end, class]; kept = unchanged, dropped = removed (\\p{Cc}, \\p{Cf}, \\p{Zl}, \\p{Zp}),',
|
||
'question = replaced by "?" (not WinAnsi-encodable). Ranges are contiguous and cover all.',
|
||
],
|
||
ranges: classRanges(safeClassOf),
|
||
},
|
||
upper: buildUpper(),
|
||
jsWhitespace: {
|
||
comment:
|
||
'Code points JS /\\s/ matches (non-surrogates), [start, end] ranges.',
|
||
ranges: codePointRanges(isJsWhitespace),
|
||
},
|
||
jsTrim: {
|
||
comment:
|
||
'Code points String.prototype.trim removes (non-surrogates), [start, end] ranges.',
|
||
ranges: codePointRanges(isJsTrimmed),
|
||
},
|
||
...buildNumbers(),
|
||
rasterSizeOf: buildRasterSizes(),
|
||
}
|
||
}
|
||
|
||
describe('R4 text oracles (TypeScript freeze)', () => {
|
||
it('matches the frozen AFM metrics and WinAnsi table', () => {
|
||
freezeJson('afm.json', buildAfm())
|
||
})
|
||
|
||
it('matches the frozen text and number rules', () => {
|
||
freezeJson('text.json', buildText())
|
||
}, 120_000)
|
||
})
|