Skip to content

Headless measures text at the display's pixels per point (#273) - #275

Merged
samitbasu merged 3 commits into
mainfrom
headless-273
Nov 16, 2026
Merged

samitbasu merged 3 commits into
mainfrom
headless-273

Conversation

@samitbasu

@samitbasu samitbasu commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Fixes #273

Cause. Headless always ran its passes at egui's default of 1 pixel per point. egui snaps glyphs to the physical pixel grid, so text measured at 1.0 comes out a few points wider or narrower than the same text drawn at 1.5 or 2.0. The text leaf's rect, and where its lines break, then differ between the headless layout and the drawn frame. With the scale, base text, preset and theme all matching, the two agree exactly.

Fix. Headless now carries a testing::PixelsPerPoint (a newtype that must be finite and positive, 1.0 by default) and passes it to every pass as the root viewport's native_pixels_per_point, the same way kittest and eframe do. A test sets it with Headless::pixels_per_point to match the harness or display it compares against.

Test. headless::headless_text_wraps_as_the_drawn_frame_does lays out a shadcn-styled card (a semi-bold title over a wrapping paragraph, in the preset's body text, light theme) two ways: headless, and through the kittest support harness. It does this at 1.0, 1.5 and 2.0 pixels per point and asserts every text leaf's rect is equal within 0.5 pt. It also asserts its preconditions: the body wraps, and it is sized to its lines rather than stretched. Without the fix it fails at 1.5 (body right edge 220.0 headless, 216.8 drawn). With the fix restored it passes.

Note on the consumer's report. The consumer's test helper builds its Headless without .base_text(..), so the root text falls back to egui's Body, while the frame sets the theme's base text. On top of that, kittest's build_ui_state insets the root Ui by an 8 pt outer margin. Either one alone shifts a wrapping card by tens of points at 1.0 pixels per point. Those differences are in the consumer's setup, not in Headless; this PR fixes the gap that remains once the setup matches.

Follow-up: the default base text (b5678c3). A layout given no base text used to fall back to egui's Body, even where an eggshell style was installed. That is the trap the consumer fell into. The default now follows the installed style: the installed Style's text, in egui's text colour, else egui's Body as before. It is one fallback (TextStyle::default_for, used by LayoutState::build), shared by Headless and every drawn layout, so the two still agree. Changing Headless alone would have split them for an app that sets no base text.

  • Tests: headless::without_a_base_text_a_layout_reads_the_installed_style uses a style whose text is unlike Body; it failed before the change. headless::without_a_base_text_or_a_style_a_layout_reads_egui_body checks the unstyled case is unchanged.

Note for the testing guide (#236, the user's prose). kittest's build_ui_state insets the root by an 8 pt margin, so a harness's layout area is 16 pt narrower than the size it's given. A Headless meant to match a kittest frame needs the inner size. This PR leaves it to the guide.

Behaviour changes.

  • In a context with an eggshell style installed, a layout given no base text now sets its root text in that style's text instead of egui's Body. Under the shadcn preset the two are the same, so no golden moved.
  • None from the pixels-per-point fix at the default scale.

No-panic rule (#279). PixelsPerPoint::new asserted that the scale was finite and positive. It now falls back to 1.0 for any other scale, with a // TODO(#279): report. A test, headless::a_scale_that_is_not_finite_and_positive_falls_back_to_one, failed before the change.

New public API: testing::PixelsPerPoint and Headless::pixels_per_point.

Goldens changed. None.

CI. cargo xtask ci passed (exit 0, fuzz included).

🤖 Generated with Claude Code

https://claude.ai/code/session_01LesfK54UWq1haPSEr9X22o

samitbasu and others added 2 commits November 15, 2026 23:02
A Headless pass ran with egui's default scale of one pixel per point,
whatever display the frame it stands in for is drawn on. egui lays
glyphs out on the physical pixel grid, so a galley measured at 1.0 is a
few points wider or narrower than the same text at 1.5 or 2.0: a text
leaf's rect, and where its lines break, differed between Headless and
the drawn frame.

Headless now carries a PixelsPerPoint (finite and positive, 1.0 by
default) and hands it to every pass as the root viewport's
native_pixels_per_point, as kittest and eframe do. A test sets it with
Headless::pixels_per_point to match the harness or display it compares
against.

The test lays a shadcn-styled card with a semi-bold title and a wrapping
paragraph out headless and through a kittest harness at 1.0, 1.5 and
2.0 pixels per point, and holds every text leaf's rect within 0.5 pt.
Without the fix it fails at 1.5 (the body 216.8 drawn, 220.0 headless).

Behaviour changes: none at the default scale; new public API
testing::PixelsPerPoint and Headless::pixels_per_point.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LesfK54UWq1haPSEr9X22o
A layout given no base text measured in egui's Body even where an eggshell
style was installed, so a test that left the base text out of `Headless`
laid text out in another size than the app it tested. The default now
follows the style an app installs: the installed `Style`'s text, in egui's
text colour, else egui's Body as before. It is one fallback, shared by
`Headless` and every drawn `LayoutState`, so the two still agree.

Behaviour change: in a context with an eggshell style installed, a layout
with no base text sets its root text in that style's `text` instead of
egui's Body. Under the shadcn preset the two are the same; no golden moved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LesfK54UWq1haPSEr9X22o
@samitbasu

Copy link
Copy Markdown
Owner Author

Gate (my run on b5678c3, merged with main): cargo xtask ci (with fuzz) exits 0. Testing infrastructure plus a default-text fallback (listed); no look change under shadcn. Ready to merge.

A scale that is not finite and positive gives the default, marked
TODO(#279) for the library's error channel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LesfK54UWq1haPSEr9X22o
@samitbasu
samitbasu merged commit ef5347e into main Nov 16, 2026
4 checks passed
samitbasu added a commit that referenced this pull request Nov 16, 2026
No textual conflicts. The new table (src/owned/table.rs) called the old
`div_id_with(id, key, ..)`; it now passes a Slot (the row's own, and
`slot_of` for each cell's id), so a repeated key there gets its own id
like everywhere else. The merged code adds no debug-misuse panics in
WP1's scope (the markdown builder's `shell()` is the builder's, which
cannot panic).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKe8zfqGfVMCSC5L3up
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.

Headless layout and the drawn frame wrap text at different widths (rects about 40 px off)

1 participant