Repository navigation
feat(css,paint,layout): implement font-variant-numeric and font-feature-settings - #460
Open
tannevaled wants to merge 3 commits into
Open
tannevaled wants to merge 3 commits into
tannevaled wants to merge 3 commits into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
font-variant-numericandfont-feature-settingswere completely unimplemented:css/parse.go'sapply()switch had no case for either property, so a declaration such asfont-variant-numeric: lining-numswas silently dropped — found on a real page that setlining-numson an EB Garamond heading and saw old-style figures instead. The OpenType shaper (go-opentype/opentype'sFace.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, andStyle.ActiveFontFeatures(), which combines both per CSS Fonts 4 §7.2 (font-feature-settings applies last and wins on conflict).css/parse.go: the twoapply()cases (standard unset-inherits pattern), theirinheritPropertycases, andparseFontVariantNumeric/parseFontFeatureSettings.paint/andlayout/now passesActiveFontFeatures()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/measuregained a trailingfeatures ...stringparameter;layout.Measurer's interface (and its two test fakes) follow suit.css.Advance(used only to resolve thechunit before a style's font-variant/feature-settings are known) intentionally keeps its old signature;devicescale.gowrapsFonts.Measureto drop the extra parameter there, preserving the pre-existingch-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) provesMeasure's output changes forpnum/tnum. This file's GSUB table has nolnum/onum(lining-nums/oldstyle-nums, the tags in the original report), but the CSS parsing andShapeplumbing are generic OpenType tag handling with no per-tag special-casing, solining-nums/oldstyle-numswill work identically once a font defining those two tags is used.go build ./...,go vet ./...,gofmt -lclean on every touched file.go test ./...— all packages green.go test ./... -raceon every touched package (css,paint,paint/bundledfonts,layout, rootengine) — clean. (A pre-existing, unrelated data race injs/worker_test.go's goja-workerconsole.logpath, outside this change's files, is not addressed here.)govulncheck ./...— no vulnerabilities reachable from this code.🤖 Generated with Claude Code