OpenTPW

COMPAT-002: Verify sign width rounding#

Status and classification#

Current findings (source-verified)#

Fractional advances at 100 percent#

SignTextLayout.Place accumulates the raw hmtx advance in font units while widthPercent is 100, and Measure multiplies only once by ScaleForEmHeight(emPixels) * horizontalScale. The result therefore stays fractional; there is no per-glyph or per-line integer rounding. Example from the characterization tests: a 601-unit advance at upem 1000 and a 100 px em measures 60.1 px; "AB" with advances 601 and 603 measures 120.4 px, and 180.6 px at horizontalScale = 1.5.

Percent truncation is per character, in pixel space#

For any widthPercent other than 100, Place passes the already scaled pixel advance (advance * pixelsPerUnit) to ScaledAdvance. ScaledAdvance computes advancePixels * widthPercent / 100f and applies MathF.Truncate to that one character. The truncated pixel value is divided back by pixelsPerUnit for accumulation and multiplied out again by Measure. Consequently the line width is the sum of per-glyph integer pixel advances, not truncate(line total * percent / 100). Example: two 612-unit glyphs at a 100 px em are 122.4 px at 100 percent; at 50 percent each 61.2 px advance becomes 30 px, for 60 px, whereas truncating the line total would give 61 px.

Horizontal scale acts before percent truncation#

Both the explicit horizontalScale argument and the value returned by LogFontHorizontalScale enter pixelsPerUnit before ScaledAdvance. At 100 percent they remain continuous. At another percent they change the pre-truncation pixel value, so truncation can discard a different fraction at different scales. Example: advances 601 and 603 at horizontalScale = 1.5 give 90.15 and 90.45 px; at 90 percent those become 81 and 81 px, for 162 px, while a continuous 90 percent of the 180.6 px line would be 162.54 px.

LOGFONT width scaling is a continuous average-width ratio#

LogFontHorizontalScale returns logFontWidth / (AverageCharWidth * ScaleForEmHeight(emPixels)) when logFontWidth > 0 and AverageCharWidth > 0, otherwise 1. It is continuous and performs no rounding. It is a different mechanism from the slot percent: the percent truncates each already-scaled glyph advance to a whole pixel, while the LOGFONT width ratio does not truncate at all. Example with synthetic xAvgCharWidth = 600, upem 1000 and a 100 px em: the average character is 60 px, lfWidth = 90 selects scale 1.5, and a 601-unit glyph measures 90.15 px at 100 percent; the same glyph at 90 percent measures the truncated 81 px.

Synthetic builder and the OS/2 dependency#

TrueTypeFontTests.FontBuilder writes head, hhea, maxp, hmtx, loca, glyf, cmap, optional name and optional kern, but no OS/2 table. Such a font therefore has AverageCharWidth == 0, so LogFontHorizontalScale is neutral for it. That is characterized directly, and the new test file adds a minimal 78-byte OS/2 table in a test-only wrapper so the existing average-width path can be exercised with synthetic metrics. The base glyph metrics still come from FontBuilder.

Long synthetic names#

A 32-character synthetic name with every glyph 601 units wide, em 144, upem 1000, and synthetic xAvgCharWidth = 600 measures 2769.408 px at 100 percent. At 90 percent the 86.544 px advance truncates to 77 px per character, for 2464 px, a loss that accumulates over the name. FitLogFontWidth(..., logFontWidth: 58, canvasWidth: 512) narrows the name to width 15 at 100 percent because average-width scaling makes the extent depend on lfWidth; at 90 percent a 513 px canvas selects width 18. The one-pixel margin avoids treating float reconstruction of 32 truncated 16 px advances as an exact 512 px comparison.

Source sites#

Characterization test contract (revised, exact-head CI pending)#

These fixtures call only the managed TrueTypeFont and SignTextLayout APIs. They make no comparison with native output and no parity claim.

Distinguishing the two mechanisms#

MechanismWhere appliedRoundingObservable result
Slot width percentPer character, after em scale and horizontal scaleMathF.Truncate to a whole pixel per glyphQuantized line width; fraction lost per glyph; not a continuous percentage of the line
LOGFONT width (lfWidth)As a continuous horizontal-scale ratio derived from OS/2 xAvgCharWidthNone in managed codeFractional extent proportional to lfWidth; drives FitLogFontWidth

The two can be combined: the LOGFONT ratio changes the pre-truncation pixel advance, and the percent then truncates each character. Treating percent truncation as LOGFONT width scaling, or the reverse, is therefore incorrect.

What remains unproven#

Exact evidence still needed#

Historical context (superseded plan)#

The first draft, based on 5ccddcab, was plan-only and stated that LogFontHorizontalScale uses average-width scaling and retains fractional advances, while Mac GDI width rounding is unverified. Its proposed steps are historical, not executed:

  1. Confirm ownership and rebase against latest main.

  2. Compare line extents at fit-loop boundaries before proposing platform-specific rounding.

  3. Turn each verified distinction into a deterministic regression fixture.

  4. Keep unknown behavior unresolved; an evidence-only outcome is valid.

  5. Submit any later implementation with exact evidence, changed scope and test output.

Its acceptance list (long names just below/at/above canvas width, width 0, narrow and wide runs) is now partially realized as the characterization fixtures above. The original caution still applies: synthetic consistency alone is not original-fidelity proof, and no screenshots or runtime checks were performed.

Ownership and overlap#

Completion boundary#

Source: docs/proposals/compat-002-verify-sign-width-rounding.md in the project repository.