Add DOM-free font offset fallback#18463
Open
matthargett wants to merge 1 commit into
Open
Conversation
Make GetFontOffset robust when DOM layout is unavailable or reports zero metrics by falling back to canvas text metrics, then to a CSS font-size estimate. This lets native runtimes use GUI resizeToFit without carrying validation-only font metric shims.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves GetFontOffset so it can produce usable font metrics in non-DOM environments by falling back from DOM layout measurement to canvas text metrics, with a final CSS pixel-size estimate fallback, and adds a unit test covering the “DOM layout returns zero bounds” path.
Changes:
- Refactors
GetFontOffsetinto DOM-based, canvas-based, and estimated fallback strategies. - Adds a CSS
pxfont-size parser to support the final fallback when no measurement APIs are available. - Adds a focused jsdom unit test that forces the zero-layout DOM case and validates the canvas-metrics fallback behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/dev/core/src/Engines/engine.common.ts | Implements multi-stage DOM/canvas/estimated fallbacks for font metric extraction. |
| packages/dev/core/test/unit/Engines/engine.common.test.ts | Adds unit coverage for the “DOM returns zero bounds, use canvas metrics” fallback. |
Comment on lines
+213
to
+214
| const ascent = Number(metrics.fontBoundingBoxAscent ?? metrics.actualBoundingBoxAscent); | ||
| const descent = Number(metrics.fontBoundingBoxDescent ?? metrics.actualBoundingBoxDescent); |
| } | ||
|
|
||
| function GetCssPixelFontSize(font: string): number { | ||
| const match = /(?:^|\s)([0-9]+(?:\.[0-9]+)?)px(?:\s|$)/.exec(String(font || "")); |
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.
What
Why
Native runtimes need GUI resize-to-fit code to work without carrying validation-only font metric shims.
Validation
npx vitest run --project=unit packages/dev/core/test/unit/Engines/engine.common.test.ts