Skip to content

feat(css,paint,layout): implement font-variant-numeric and font-feature-settings - #460

Open
tannevaled wants to merge 3 commits into
mainfrom
feat/font-variant-numeric
Open

tannevaled wants to merge 3 commits into
mainfrom
feat/font-variant-numeric

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Summary

font-variant-numeric and font-feature-settings were completely unimplemented: css/parse.go's apply() switch had no case for either property, so a declaration such as font-variant-numeric: lining-nums was silently dropped — found on a real page that set lining-nums on an EB Garamond heading and saw old-style figures instead. The OpenType shaper (go-opentype/opentype's Face.Shape(text, features...)) is fully generic and already supports arbitrary GSUB feature tags; the engine just never asked it for any beyond the hardcoded required-ligature feature.

  • css/value.go: Style.FontVariantNumeric, Style.FontFeatureSettings, and Style.ActiveFontFeatures(), which combines both per CSS Fonts 4 §7.2 (font-feature-settings applies last and wins on conflict).
  • css/parse.go: the two apply() cases (standard unset-inherits pattern), their inheritProperty cases, and parseFontVariantNumeric/parseFontFeatureSettings.
  • Every text-shaping/measuring call site in paint/ and layout/ now passes ActiveFontFeatures() alongside the existing required-ligature feature, so layout and paint shape with the same features — a feature can change glyph and advance width, confirmed on EB Garamond's own "pnum" (proportional-nums), which measures narrower than the font's default digits.
  • paint.Fonts.Measure/measure gained a trailing features ...string parameter; layout.Measurer's interface (and its two test fakes) follow suit.
  • css.Advance (used only to resolve the ch unit before a style's font-variant/feature-settings are known) intentionally keeps its old signature; devicescale.go wraps Fonts.Measure to drop the extra parameter there, preserving the pre-existing ch-unit behavior exactly.

Test plan

  • css/parse_test.go: parsing, inheritance (unset), and the feature-settings-wins-on-conflict rule.
  • paint/fontfeature_test.go: a real-font fixture (EB Garamond regular TTF, SIL OFL 1.1 — the exact file behind the original report) proves Measure's output changes for pnum/tnum. This file's GSUB table has no lnum/onum (lining-nums/oldstyle-nums, the tags in the original report), but the CSS parsing and Shape plumbing are generic OpenType tag handling with no per-tag special-casing, so lining-nums/oldstyle-nums will work identically once a font defining those two tags is used.
  • go build ./..., go vet ./..., gofmt -l clean on every touched file.
  • go test ./... — all packages green.
  • go test ./... -race on every touched package (css, paint, paint/bundledfonts, layout, root engine) — clean. (A pre-existing, unrelated data race in js/worker_test.go's goja-worker console.log path, outside this change's files, is not addressed here.)
  • govulncheck ./... — no vulnerabilities reachable from this code.

🤖 Generated with Claude Code

tannevaled and others added 3 commits November 29, 2026 18:58
…re-settings

Both properties were completely unimplemented: css/parse.go's apply() switch
had no case for either, so a declaration like font-variant-numeric:
lining-nums was silently dropped, same as an unknown property — found on a
real page setting lining-nums on an EB Garamond heading and seeing no
effect at all, even though the OpenType shaper (go-opentype/opentype) is
fully generic and already supports arbitrary GSUB feature tags via
Face.Shape(text, features...).

css/value.go adds Style.FontVariantNumeric, Style.FontFeatureSettings and
Style.ActiveFontFeatures(), which combines both per CSS Fonts 4 §7.2
(font-feature-settings applies last and wins on conflict). css/parse.go
adds the two apply() cases (with the standard unset-inherits pattern),
their inheritProperty cases, and parseFontVariantNumeric/
parseFontFeatureSettings.

Every paint/layout call site that shapes or measures text now passes the
style's ActiveFontFeatures() alongside the existing required-ligature
feature, so layout and paint agree on glyph widths: a feature can change
both glyph choice and advance width, confirmed on EB Garamond's own
"pnum" (proportional-nums), which measures narrower than the font's
default digits. paint.Fonts.Measure/measure gained a trailing features
...string parameter (and measureKey a joined-string field, since []string
cannot be a map key); layout.Measurer's interface and both of its test
fakes follow suit. css.Advance (used only to resolve the `ch` unit before
a style's font is fully known) is intentionally left without a features
parameter; devicescale.go wraps Fonts.Measure to drop them there, matching
pre-existing ch-unit behaviour exactly.

Tests: css/parse_test.go covers parsing, inheritance and the
feature-settings-wins-on-conflict rule. paint/fontfeature_test.go adds a
real-font fixture (EB Garamond regular TTF, SIL OFL 1.1, the exact file
behind the original report) proving Measure's output actually changes
for "pnum"/"tnum" — this file's GSUB table has no lnum/onum (lining-nums/
oldstyle-nums, the original report's own tags), but the parsing and Shape
plumbing are generic OpenType tag plumbing with no special-casing per
tag, so lining-nums/oldstyle-nums work identically once a font defining
those two tags is used.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…ttings

CI's coverage gate (scripts/coverage-gate.sh) requires the css package to
stay at or above a 99.5% floor; the new code in the previous commit
(900e88f) landed it at 99.3%, failing PR #460's CI.

go tool cover -func pinpointed two under-tested functions:

- parseFontFeatureSettings (css/parse.go:2241) was missing its malformed-
  entry recovery paths: an empty entry from a stray comma, an unquoted
  tag, an unterminated quote, a quoted tag whose length isn't 4, the
  spelled-out "on"/"off" keywords, and an unparseable numeric value
  (CSS Syntax 3 error recovery drops only that one entry).
- Style.ActiveFontFeatures (css/value.go:1560) never exercised its early
  return for the common case where neither font-variant-numeric nor
  font-feature-settings is set, nor the two no-op branches of its
  conflict switch (re-requesting an already-active tag, turning off a
  tag nothing requested).

Added test cases for each in css/parse_test.go; css package coverage is
now 99.5%, meeting the floor. No production code changed.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The css mutation-test gate (mutsweep) flagged three refusals as SURVIVED
on the previous commit: deleting any of them changes nothing a test can
observe.

- parseFontVariantNumeric's `if lv == "" || lv == "normal" { return nil }`
  (css/parse.go): fontVariantNumericTags has no entry for "normal" or "",
  so the loop below already returns nil for both without the guard.
- parseFontFeatureSettings's `if v == "" || strings.EqualFold(v, "normal")
  { return nil }` (css/parse.go): an empty value produces one empty,
  unquoted part, and "normal" in any case is itself an unquoted part, so
  both already fall into the "not a quoted tag" skip and leave out nil.
- Style.ActiveFontFeatures's `if len(s.FontVariantNumeric) == 0 &&
  len(s.FontFeatureSettings) == 0 { return nil }` (css/value.go): with
  both empty, appending zero elements to a nil slice is a no-op and the
  loop runs zero times, so it already returns nil.

All three were genuinely redundant fast paths, not defects, confirmed by
tracing every input through the fallback path by hand; no test can tell
the two versions apart because there is no input on which they differ.
Removing them (rather than carrying a mutsweep exception) is the
simpler fix. Behavior is unchanged: gotest ./css/... stays green at
99.5% coverage.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant