Add DOM-free font offset fallback#18463
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.
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. |
| const ascent = Number(metrics.fontBoundingBoxAscent ?? metrics.actualBoundingBoxAscent); | ||
| const descent = Number(metrics.fontBoundingBoxDescent ?? metrics.actualBoundingBoxDescent); |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
|
Snapshot stored with reference name: Test environment: To test a playground add it to the URL, for example: https://snapshots-cvgtc2eugrd3cgfd.z01.azurefd.net/refs/pull/18463/merge/index.html#WGZLGJ#4600 Links to test your changes to core in the published versions of the Babylon tools (does not contain changes you made to the tools themselves): https://playground.babylonjs.com/?snapshot=refs/pull/18463/merge To test the snapshot in the playground with a playground ID add it after the snapshot query string: https://playground.babylonjs.com/?snapshot=refs/pull/18463/merge#BCU1XR#0 If you made changes to the sandbox or playground in this PR, additional comments will be generated soon containing links to the dev versions of those tools. |
🟢 Memory Leak Test Results13 passed, 0 leaked out of 13 scenarios 🟢 All memory leak tests passed — no leaks detected. Passed Scenarios (13)
|
|
WebGL2 visualization test reporter: |
|
Visualization tests for WebGPU |
⚡ Performance Test Results🟢 All performance tests passed — no regressions detected. |
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
|
Moving all active PRs to draft for the next 24 to 48 hours. |
|
Please make sure to merge from master |
|
Reviewer - this PR has made changes to one or more package.json files. |
|
Reviewer - this PR has made changes to the build configuration file. This build will release a new package on npm If that was unintentional please make sure to revert those changes or close this PR. |
🟢 Memory Leak Test Results13 passed, 0 leaked out of 13 scenarios 🟢 All memory leak tests passed — no leaks detected. Passed Scenarios (13)
|
|
WebGL2 visualization test reporter: |
⚡ Performance Test Results🟢 All performance tests passed — no regressions detected. |
|
Visualization tests for WebGPU |
|
Merged latest master (9.9.1) into the branch. The earlier package/build-configuration bot comments are stale after the rebase/merge: the current PR diff is only Local validation with the reconciled 18460 + 18463 patches:
Fresh PR checks currently show GitGuardian passing, with Azure/Mergify entries neutral/skipping rather than failing. |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
🟢 Memory Leak Test Results13 passed, 0 leaked out of 13 scenarios 🟢 All memory leak tests passed — no leaks detected. Passed Scenarios (13)
|
|
WebGL2 visualization test reporter: |
|
Visualization tests for WebGPU |
|
one last thing - the copilot comment seems logical to me, does it make sense? feel free to resolve or commit these changes, and I will merge it right after |
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