Skip to content

Add DOM-free font offset fallback#18463

Open
matthargett wants to merge 1 commit into
BabylonJS:masterfrom
rebeckerspecialties:codex/native-font-offset
Open

Add DOM-free font offset fallback#18463
matthargett wants to merge 1 commit into
BabylonJS:masterfrom
rebeckerspecialties:codex/native-font-offset

Conversation

@matthargett
Copy link
Copy Markdown
Contributor

What

  • Falls back from DOM layout font metrics to canvas text metrics when DOM layout is unavailable or returns zero-sized bounds.
  • Uses a CSS pixel-size estimate as the final fallback.
  • Adds focused unit coverage for the zero-layout path.

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

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.
Copilot AI review requested due to automatic review settings May 17, 2026 02:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 GetFontOffset into DOM-based, canvas-based, and estimated fallback strategies.
  • Adds a CSS px font-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 || ""));
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.

2 participants