Opened 6 weeks ago
Last modified 3 weeks ago
#66198 new enhancement
Typography: Add font-stretch and font-variation-settings support to Theme JSON styles
| Reported by: | kimjiwoon | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Editor | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests needs-testing |
| Cc: | Focuses: | css, rest-api |
Description (last modified by )
Related: Gutenberg 83148 is the design discussion, and 83159 is the editor prototype. This ticket is not a proposal to move that discussion here: it covers only the Core-side contract two typography style properties need, so that the editor work has somewhere to land. Which controls appear, and what a font's faces say they can do, stay in Gutenberg.
What this adds
Two style properties, each stored under styles.typography and written by the style engine:
| Stored as | Written as |
fontStretch | font-stretch
|
fontVariationSettings | font-variation-settings
|
With them:
settings.typography.fontStretch, alongside the existingfontStyleandfontWeight, and on by default inwp-includes/theme.json.settings.typography.fontVariations, a boolean saying whether a site offers editing of a font's custom axes at all.axeson a font face, so a face can say which OpenType axes it has and over what range.font-variation-settingsin the listsafecss_filter_attr()allows.font-stretchis already there.fontStretchandfontVariationSettingsas typography block supports.- Tests for the block support, the style engine and
WP_Theme_JSON.
Why a width needs a property of its own
font-stretch is the only axis of the four that map to a CSS property which a browser will not stand in for: there is no font-synthesis-width to go with font-synthesis-weight and font-synthesis-style. A variable font that declares a width range has nothing in Core today that can carry a chosen width, so the value has to be written by hand.
The name is the older one deliberately. CSS Fonts 4 renames the property font-width and keeps font-stretch as its legacy alias; no browser implements font-width yet, and the alias drives the wdth axis identically.
Why the variation settings value is an object
font-variation-settings replaces the whole list rather than merging, so a string value cannot be inherited through the theme.json origins the way other styles are: setting one axis on a block would mean repeating every axis the root set. Keyed by tag, origins merge per axis, and the style engine assembles the string once at the boundary.
The four axes that have a property of their own — wght, wdth, slnt, ital — are refused there. The property is applied after the properties that map to the same axis, so a coordinate written here takes the axis away from the control that owns it: font-weight: 700 with "wght" 400 renders at 400, and a <strong> inside an element carrying "wght" 300 does not get bolder. opsz is allowed, because font-optical-sizing can only switch the browser's own tracking on or off and has no way to carry a chosen size.
A tag must be four letters or digits, which is what OpenType allows and also keeps quotation marks out of the serialized value, and a value must be a number rather than a numeric string. Those rules live in one place, WP_Style_Engine, and WP_Theme_JSON reads them through it when it sanitizes rather than repeating them.
What is not here
- The editor controls, and the reading of a family's faces that decides what they offer. That is Gutenberg's, and #83148 has the model.
- Which blocks opt in. The supports exist; no block in this patch turns them on.
- Reading
axesfrom a font file'sfvartable on upload. The contract is here so that a theme can declare it; the Font Library reading it is separate work.
Change History (10)
#1
@
6 weeks ago
- Summary Typography: Accept a font width and font variation settings as styles → Typography: Add font-stretch and font-variation-settings support to Theme JSON styles
This ticket was mentioned in PR #13789 on WordPress/wordpress-develop by @kimjiwoon.
6 weeks ago
#2
- Keywords has-patch has-unit-tests added
@therssoftware commented on PR #13789:
6 weeks ago
#4
Tested PR #13789 / Trac #66198 on PHP 8.3 + macOS.
Test Results
- PHPUnit: Ran the filtered typography suite (
Typography|Theme_JSON|Style_Engine|StyleEngine|ThemeJson|font_stretch), passing all 516 tests (933 assertions). - PHPCS: 0 errors, 0 warnings across all 7 modified files.
- Theme JSON generation: Verified that
styles.typography.fontStretchproducesfont-stretchandfontVariationSettingscompiles tofont-variation-settings. Property-owned axes (wght,wdth,slnt,ital) are properly excluded fromfont-variation-settings. - Sanitization: Verified
WP_Theme_JSON::remove_insecure_properties()strips invalid/injected values while preserving valid numeric axis declarations. - KSES: Verified
safecss_filter_attr()allowsfont-variation-settings.
Minor Observation
In WP_Style_Engine::is_font_variation_axis( $tag, $value ):
`php
return is_string( $tag )
&& 1 === preg_match( '/[A-Za-z0-9]{4}$/', $tag )
| is_int( $tag ) ) or (string) $tag would allow 4-digit tags to pass as intended. |
Overall, the implementation and tests are very solid and ready for Core.
AI Disclosure
Assisted by generative AI tools to run automated checks (PHPUnit and PHPCS), execute edge-case scripts, and format the testing report.
@kimjiwoon commented on PR #13789:
6 weeks ago
#5
Thank you for running this, and for the tag observation — it found a real defect, though it turns out to point the other way.
You are right that PHP stores a decimal string array key as an integer, so '1000' => 20 arrives as 1000 => 20 and is_string( $tag ) refuses it. What I got wrong is treating that as a tag worth keeping. The OpenType Design-Variation Axis Tag Registry is explicit:
Axis tags must begin with a letter (0x41 to 0x5A, 0x61 to 0x7A) and must use only letters, digits (0x30 to 0x39) or space (0x20). Space characters must only occur as trailing characters in tags that have fewer than four letters or digits.
So 1000 is not an axis tag at all, and ^[A-Za-z0-9]{4}$ was not too strict but too loose: it accepted 1234 and 1ABC, which no font can declare. Allowing the integer key through would have stored what the registry forbids.
I have turned the check the other way, into a validator that asks whether the tag begins with a letter and is four letters or digits, rather than counting four of anything. That covers the registered tags, such as opsz, and the foundry-defined ones, which the registry narrows further to uppercase letters and digits, such as GRAD.
The casting question then answers itself, and the code says so where the next reader will need it: a tag cannot begin with a digit, so a key made only of digits was never a tag this could accept, and nothing is lost to PHP having turned it into an integer.
One more thing came out of writing the tests, which is why the validator is not the full grammar. The registry also allows a tag of fewer than four letters or digits to be padded with trailing spaces, so I accepted 'abc ' and 'a ' at first. The test then failed with the declaration reading "a " 12 and the compiled CSS reading "a " 12: filter_declaration() passes every value through wp_strip_all_tags( $value, true ), whose second argument collapses a run of whitespace into a single space. That is the path every declaration takes and not something to change from here, so a padded tag is refused rather than written as a different tag than the one asked for. The reason is in the docblock, next to the rule it explains.
The serializer's fixture carries both sides. Refused: 1234, 1ABC, ' abc', 'ab c', 'abc', 'abcde', 'abc ', 'a ', alongside the value and property-owned cases that were already there. Accepted: opsz, GRAD, A123.
One mismatch is left over, and it is on the other side of the fence: the axes[].tag pattern in Gutenberg's theme.json schema is still ^[A-Za-z0-9]{4}$. Core is now the narrower of the two rather than the wider, so nothing unsafe is stored, but the contract is written twice and I will bring the schema to the same grammar in the Gutenberg pull request.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Reading the registry's syntax requirements against the implementation, writing the validator and the test cases, and running the suite. I reviewed the reasoning and the results.
@kimjiwoon commented on PR #13789:
6 weeks ago
#6
Filed the whitespace behaviour as its own ticket: #66199. It is not specific to this pull request — a font family named "My Font" is stored with both spaces and serialized with one — so the refusal here stays a limit rather than a fix.
@therssoftware commented on PR #13789:
6 weeks ago
#7
Thanks for digging into the OpenType axis registry spec @Jiwoon-Kim! That makes total sense regarding tags having to begin with a letter, and creating is_font_variation_axis_tag() with the updated regex /^[A-Za-z][A-Za-z0-9]{3}$/ handles this cleanly.
Re-tested with 8c63a53cb4 on PHP 8.3 and all 517 tests pass.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Why
Two typography style properties have no way to reach CSS today, and a variable font needs both.
font-stretchis the one axis with a CSS property that a browser will not synthesize — there is nofont-synthesis-width— so a width a font declares cannot be used at all without it.font-variation-settingsis what the axes with no property of their own have, and there is no style property for it.This is the Core half of editor work happening in Gutenberg. It carries the contract only: what may be stored, what is written, and what is refused.
What
styles.typographyfontStretchfont-stretchfontVariationSettings, an object keyed by axis tagfont-variation-settingsAlso:
settings.typography.fontStretchbeside the existingfontStyleandfontWeight, on by default;settings.typography.fontVariations, a boolean for whether a site offers custom-axis editing;axeson a font face, for the axes a file has and their ranges;font-variation-settingsadded to whatsafecss_filter_attr()allows,font-stretchbeing there already; and the two block supports.How
WP_Style_Engineholds the rule for what may be written tofont-variation-settings: a tag of four letters or digits, a numeric value, and not one of the four axes a CSS property owns (wght,wdth,slnt,ital), because the property is applied after those and would take the axis from them.opszis allowed, sincefont-optical-sizingonly switches the browser's tracking rather than taking a coordinate.WP_Theme_JSONreads that rule through the style engine in both places it needs it — serializing a stylesheet, and sanitizing styles for a user withoutunfiltered_html— rather than holding a second copy. A second copy is what this whole line of work started from: two helpers reading the same font faces by different rules offered a static family nine widths it had no files for.The block support's two lists of variables are regenerated rather than patched, because the longest name sets the column the coding standard aligns the assignments to. No line in them changes meaning.
Testing Instructions
npm run test:php -- --filter 'Typography|Theme_JSON|Style_Engine|StyleEngine|ThemeJson|font_stretch'. 516 tests pass here.theme.json, setstyles.typography.fontStretchto"75%"andstyles.typography.fontVariationSettingsto{ "GRAD": 50, "wght": 700 }. The stylesheet carriesfont-stretch: 75%andfont-variation-settings: "GRAD" 50; the weight is left tofont-weight, so a<strong>inside still renders bolder.unfiltered_html, save Global Styles withfontVariationSettingsset to{ "GRAD": 20, "wght": 700, "XTRA": "500; color: red" }. OnlyGRADis stored.Not included
The editor controls and the reading of a family's faces that decides what they offer, which stay in Gutenberg; which blocks opt in, since the supports here are off everywhere; and reading
axesfrom a font file'sfvartable when it is uploaded.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Porting the implementation and tests from the Gutenberg prototype, and running the test suites. I reviewed the changes and the results, and one wrong assumption the port made was found by the tests rather than by reading: Core keys the font families setting by origin, as the plugin does.