Skip to content

Text: keep colour-only spans out of the shaping key and resolve their ink at glyph emission - #1116

Merged
Hona merged 2 commits into
devfrom
search-color-spans-3
Nov 18, 2026
Merged

Hona merged 2 commits into
devfrom
search-color-spans-3

Conversation

@Hona

@Hona Hona commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Colour-only Spans (color with no weight, mono, size, italic, bg or underline) do not change glyph shaping, advances, line breaking or painted decorations: Parley already shapes across Brush boundaries (parley::shape::shape_text splits only on font, size, locale, variations, features and spacing). Keeping non-empty layout-affecting spans alone in text_hash, ParagraphLayout's paragraph keys and Parley's Brush runs lets highlighted text reuse plain and previously highlighted layouts (and cached Elision truncations) without reshaping.

flowchart LR
  S["TextSpec (content + spans)"] --> K["layout_spans (start < end && affects_layout)<br/>shape_key / text_hash / Brush ordinals"]
  K -->|hit| L["Shared Rc&lt;Layout&lt;Ink&gt;&gt;<br/>(+ CachedElision)"]
  S --> I["set_glyph_inks(color, drawn_spans)<br/>sorted disjoint (start, end, ink) intervals"]
  L --> E["line_glyph_bytes + glyph_emit / GlyphCache"]
  I --> E
  E --> M["ink resolved by forward cursor / binary search<br/>at cluster start byte before dilation & gamma"]
Loading
  • Shared non-empty layout-span filter (text/span.rs, text/hash.rs, text/paragraphs.rs, text.rs, text/decorations.rs, text/synthetic_bold.rs): span::layout_spans (s.start < s.end && s.affects_layout()) is shared across text_hash, per-paragraph hashes, Parley Brush(Ink(ordinal)) pushes, decorations_into (collected once per call) and synthetic_bold::inks, so a span gets a brush ordinal if and only if it entered the shaping key (including after head or middle truncation cuts an earlier layout span to an empty range).
  • Disjoint ink intervals & O(1) cursor lookup (text.rs, text/glyph_emit.rs, text/glyph_cache.rs, text/windfoil.rs): TextSystem::set_glyph_inks resolves coloured spans once (set_ink_spans) into sorted, non-overlapping (start, end, ink) intervals where later spans win, alongside the smoothing/gamma buckets. line_glyph_bytes walks each run's clusters in visual order and gives every glyph in a cluster that cluster's first logical byte (cl.text_range().start, covering ligatures, combining sequences and RTL runs). ink_at steps a forward cursor in O(1) (falling back to partition_point binary search on jumps) in both glyphs_in_band and GlyphCache::replay, resolving ink before apple.radius / windows.bucket / GlyphKey lookup. Shared-run cells (glyphs_in_scaled / advance_in) look up cell byte 0, matching their cell-relative ink_spans.
  • Paint caches (runtime/paint/content.rs): passes elision-remapped spans to set_glyph_inks and hashes (sp.start, sp.end, sp.color) in text_quads so a shared LayoutId repaints when its highlight range or colour changes.
  • Scope limit (flatten_spans): rich(), El::spans() (with_pill_colors) and text_input decorations flatten overlapping spans. When a colour range overlaps a layout-affecting span (weight, mono, size, italic, bg or underline), flattening splits that layout span at the colour's edges so those split points still enter the shaping key (for example a match inside inline code or inside a bold label still reshapes when its highlight moves). Unstyled text with search highlights and SyntaxTheme::spans code blocks (which emit colour-only spans, plus weight on Markdown headings) keep a stable key.
  • Rune before vs after pixels (9f465b74 vs b12a7972): 0 differing pixels between Rune before and Rune after across 12 (query, scale, scheme) captures at 1x and 1.5x (light and dark, covering "fi", "ff" and "Te"), and across the driven review scenes (trunc-ab, trunc-ba, shared-colors, copy-repaint steps 0–2, edges-all ligature/combining/RTL/bidi/synthetic-bold rows at 1x and 1.5x, and app palette, diff and live light→dark theme-switch captures; note this 0-pixel figure compares Rune before against Rune after, not against Chromium).

Before / after (opencode-desktop, headless 1x, release-quick --features frame-profile, exclusive quiet windows)

Surface Step Shaped (9f465b74 → b12a7972) Frame ms (9f465b74 → be26ed6c → b12a7972) Notes
Palette ("Target", first open) Open / Keys 1–3 25, 42, 30, 4 → 25, 41, 29, 3 6.16, 8.41, 4.24, 2.76 → 5.62, 6.96, 3.59, 2.32 (at be26ed6c) Update key 1: 4.58 → 3.43 ms
Palette ("Target", first open) Keys 4–6 (Targ..Target) 2, 2, 2 → 1, 1, 1 2.17, 2.31, 1.99 → 2.23, 1.79, 1.98 Only the search input shapes
Palette ("Toggle", 6 rows kept) Keys 1–6 (T..Toggle) 42, 31, 8, 8, 8, 8 → 41, 15, 1, 1, 1, 1 — Kept rows reshape 0 times as highlight grows
2,000-line Rust code block (~12k colour spans) Streaming commit frames (p50) — 5.86 → 7.28 → 4.75 text_cpu: 3.18 → 2.21 → 2.16 ms
2,000-line Rust code block (~12k colour spans) Streaming non-commit frames (p50) — 1.21 → 3.81 → 1.27 bytes = true per-glyph walk
2,000-line Rust code block (~12k colour spans) Settled wheel scroll (p50) — 3.45 → 34.50 → 3.35 GlyphCache::replay
2,000-line Rust code block (~12k colour spans) Settled still (p50) — 3.23 → 37.23 → 4.14 text_quads copy path

… ink at glyph emission

A span that sets only `color` (search highlights, syntax tokens) does not change glyph shaping, advances, line breaking or painted decorations: Parley already shapes across brush edges. Keeping colour-only spans out of `text_hash`, `ParagraphLayout`'s paragraph keys and Parley's `Brush` runs lets highlighted text reuse plain and earlier-highlighted layouts (and cached truncations) without reshaping.

`TextSystem::set_glyph_inks` records coloured span byte ranges alongside the smoothing and gamma buckets. Glyph emission and `GlyphCache` map each glyph to its cluster's first logical byte (`cl.glyphs()` on `is_atom_start()`) and resolve its `ink` before the atlas mask / Windfoil lookup, so per-ink dilation, DirectWrite gamma and synthetic bold keep their exact behaviour, and `text_quads` hashes span ranges with their colours.
Copilot AI balanced review requested due to automatic review settings November 17, 2026 07:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@Hona

Hona commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

Windows whole suite (-p rune -p rune-kit) on adais, judged against dev's own failures at 32eb7811 (this lane, whole suite) and eb5ae6c9 (FUNCTION's run, rune only) and 3468f3a3 and cca5df6f: #1116 be26ed6c (18 Nov 02:42): 2839 passed, 18 failed in 170 binaries; also on dev: alloc::disabled_trace_hooks_neither_format_nor_allocate, canvas_input::the_middle_button_drags_only_elements_that_take_it, rune::edit::tests::hidden_multiline_pristine_replace_and_delete, rune::edit::tests::pill_edge_cases_words_lines_bidi_and_direct_text_mutation, rune::edit::tests::the_caret_steps_over_a_pill_in_one_move, rune::input::keycaps::tests::repeated_chords_count_and_named_keys_use_glyphs, rune::native_surface::gpu_tests::mirrored_video_is_exact_cropped_centred_and_toggles_without_a_new_frame, rune::native_surface::gpu_tests::video_gpu_colour_sharpness_geometry_and_lifetime, rune::render::breaker::tests::a_refused_blur_pipeline_draws_unblurred, rune::render::layers::cache_tests::live_scale_zoom_sweep_buckets_during_motion_and_settles_crisp, rune::render::layers::rebind_tests::a_texture_switch_binds_no_parked_target_until_one_returns, rune::render::layers::tests::clipped_single_draw_fades_do_not_allocate_targets, rune::runtime::tests::layout_rows::send_flight_follows_the_measured_bubble_while_earlier_rows_land, rune::runtime::tests::picture_scale::a_changing_static_scale_keeps_the_glyph_atlas_bounded, rune::runtime::tests::picture_scale::text_inside_a_scale_draws_at_that_size, rune::runtime::tests::scroll_children::scroll_xy_with_direct_children_scrolls_both_axes_and_warns_on_trailing_growth_sibling, rune::text::macos::smoothing::tests::pipelines_never_share_masks, selectable_text::copy_select_all_and_the_context_menu; none new. Base 9f465b74. Proof f1109486.

…th a cursor

Addresses the #1116 review findings:
- Share `span::layout_spans` (`start < end && affects_layout()`) across `text_hash`, `key_paragraphs`, Parley `Brush` ordinals, `decorations_into` and `synthetic_bold::inks`, so empty spans created by truncation never shift brush ordinals on a shared layout.
- Resolve coloured spans once in `set_glyph_inks` into sorted, non-overlapping `(start, end, ink)` intervals and walk them with a forward cursor (falling back to binary search on jumps) instead of scanning every coloured span per glyph; collect `layout_spans` once per `decorations_into` call.
- Look up byte 0 in `glyphs_in_scaled`, since `ink_spans` holds the cell's own spans rather than the shared run's.
@Hona

Hona commented Nov 18, 2026

Copy link
Copy Markdown
Member Author

Pushed b12a79729f1b2629a1a4d05bddc880b1d57bc091 addressing the review findings (0 test attributes or test files added):

  1. Must-fix 1 — Shared non-empty layout-span numbering (span.rs:106, hash.rs:40, paragraphs.rs:810, text.rs:857, decorations.rs:81, synthetic_bold.rs:39):

    • span::layout_spans(spans) (s.start < s.end && s.affects_layout()) is now the single filter used by text_hash, key_paragraphs, Parley Brush(Ink(layout_span)) ordinals, decorations_into and synthetic_bold::inks.
    • Driven with the review's exact head-truncation pair ("README.md → src/main.rs" [0..9 bold, 12..23 underline(blue)] and "notes → src/main.rs" [8..19 underline(red)] at w = 112.0) in both orders (trunc-ab, trunc-ba in proof/search/revcases-11180435/report.json): be26ed6c lost the second underline in both orders (86 differing pixels: [red=0, blue=86] on AB, [red=86, blue=0] on BA), while b12a7972 draws both ([red=86, blue=86], 0 differing pixels vs 9f465b74).
  2. Should-fix 3 — Sorted disjoint ink intervals + O(1) cursor lookup (glyph_emit.rs:17-44, 150-185, glyph_cache.rs:260-303, decorations.rs:81):

    • set_glyph_inks calls set_ink_spans once per text to record sorted, non-overlapping (start, end, ink) intervals (fast linear pass with zero allocation when spans are already sorted and disjoint, as flatten_spans emits; sweep-line merge when overlapping). ink_at(byte, &mut cursor) steps forward in O(1) and falls back to partition_point binary search on backward/distant jumps in both glyphs_in_band and GlyphCache::replay. decorations_into collects layout_spans once per call.
    • Measured on a 2,000-line syntax-highlighted Rust code block (~12,000 colour-only SyntaxTheme spans, proof/search/code2k-11180352/summary.json, p50 frame µs across 9f465b74 → be26ed6c → b12a7972):
      • Streaming commit frames (n = 142): 5,861 → 7,284 → 4,748 µs (text_cpu: 3,177 → 2,209 → 2,156 µs)
      • Streaming non-commit frames (n = 100): 1,210 → 3,810 → 1,271 µs
      • Settled wheel scroll (GlyphCache::replay, n = 60): 3,454 → 34,495 → 3,345 µs
      • Settled still (text_quads, n = 48): 3,231 → 37,228 → 4,142 µs
  3. Should-fix 4 — Shared-run cell byte (glyph_emit.rs:124):

    • glyphs_in_scaled now looks up self.ink_at(0, &mut cursor) because ink_spans holds the cell's own spans rather than the shared Run's.
    • Driven on "hello" advance_in cells each with Span::new(0, 1).color(red): be26ed6c coloured only 'h' ([84, 0, 0, 0, 0] red px), while b12a7972 colours all five cells ([84, 88, 51, 51, 86] red px).
  4. Should-fix 5 — PR body & changelog (changelog/unreleased/2026-11-17-color-only-spans.md):

    • Updated both to drop is_atom_start(), state that the 0 differing pixels figure is Rune before vs Rune after (9f465b74 vs b12a7972) rather than vs Chromium, and document the flatten_spans limit when a colour range overlaps a layout-affecting span.
  5. Driven review scenes & open questions (proof/search/revcases-11180435/report.json):

    • Same-text differently coloured nodes on screen at once (shared-colors palette + diff lines, and app app-pal-shared + app-diff): 0 differing pixels vs 9f465b74.
    • Moving highlight on text_quads + hover colour + live light→dark theme switch (copy-repaint steps 0/1/2 and app app-theme-switch): 0 differing pixels vs 9f465b74 at every step.
    • Ligature "o" + red "ffi" + "ce", combining "e" + red "\u{0301}" + "clair", RTL "س" + red "ل" + "ام", mixed Arabic+English bidi line, and SimSun weight-700 synthetic bold (edges-all at 1x and 1.5x): 0 differing pixels vs 9f465b74 ("ffi" draws red at 183/390 red px; "e\u{0301}" keeps its base 'e' ink; mixed-direction line.runs() and line.items() stay aligned with debug_assert! clean). Official Chromium 1x/1.5x comparison captures (official-edges-{1x,1.5x}.png) are queued for adais's capture lane now that blade servers/browsers are stopped.
    • Does OpenCode's syntax highlighting emit colour-only spans, and does any caller bypass flatten_spans? Yes and no: SyntaxTheme::spans (crates/rune-kit/src/syntax.rs:470-482) sets color only (mono: false, weight: None except Syntax::Heading at 700.0 in session-view/src/palette.rs:268), and code_view (apps/desktop/src/code_view/mod.rs:417-421) sets color only (bg only on search/diff word highlights). Every El entry point (rich(), El::spans() via with_pill_colors in el/pills.rs:122-129, and sync_input_rich) flattens through flatten_spans.

@Hona
Hona merged commit f0d3238 into dev Nov 18, 2026
1 check passed
overp0werj0int added a commit to overp0werj0int/rune that referenced this pull request Nov 18, 2026
This reverts commit 2bd88e5 and keeps its test. Upstream's reworked
paragraph painting (Piece and TextFlow::Paragraphs, anomalyco#1133, with
colour-only spans resolved at glyph emission, anomalyco#1116) cuts highlights to
each paragraph itself: tests/highlight_pieces.rs passes on upstream's dev
without this commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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