@native-sdk/cli 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/build.zig +8 -0
  2. package/package.json +9 -9
  3. package/packages/core/package-lock.json +2 -2
  4. package/packages/core/package.json +1 -1
  5. package/packages/core/sdk/text.ts +62 -21
  6. package/skill-data/native-ui/SKILL.md +17 -2
  7. package/src/platform/macos/appkit_host.h +2 -0
  8. package/src/platform/macos/appkit_host.m +36 -10
  9. package/src/platform/macos/root.zig +7 -1
  10. package/src/platform/types.zig +2 -0
  11. package/src/primitives/canvas/code.zig +590 -0
  12. package/src/primitives/canvas/code_tests.zig +1062 -0
  13. package/src/primitives/canvas/equality.zig +3 -1
  14. package/src/primitives/canvas/events.zig +4 -0
  15. package/src/primitives/canvas/layout_audit.zig +3 -1
  16. package/src/primitives/canvas/markdown.zig +13 -11
  17. package/src/primitives/canvas/markdown_tests.zig +167 -4
  18. package/src/primitives/canvas/root.zig +15 -2
  19. package/src/primitives/canvas/tests.zig +1 -0
  20. package/src/primitives/canvas/text.zig +10 -0
  21. package/src/primitives/canvas/text_interaction.zig +164 -22
  22. package/src/primitives/canvas/text_layout.zig +107 -21
  23. package/src/primitives/canvas/text_measure_cache.zig +4 -4
  24. package/src/primitives/canvas/text_span_tests.zig +254 -0
  25. package/src/primitives/canvas/text_spans.zig +556 -32
  26. package/src/primitives/canvas/text_tests.zig +86 -0
  27. package/src/primitives/canvas/themes/geist.zig +29 -0
  28. package/src/primitives/canvas/tokens.zig +72 -2
  29. package/src/primitives/canvas/ui.zig +325 -21
  30. package/src/primitives/canvas/ui_markup.zig +59 -2
  31. package/src/primitives/canvas/ui_markup_compiled.zig +76 -0
  32. package/src/primitives/canvas/ui_markup_compiled_tests.zig +33 -0
  33. package/src/primitives/canvas/ui_markup_contract.zig +34 -0
  34. package/src/primitives/canvas/ui_markup_reflect.zig +20 -3
  35. package/src/primitives/canvas/ui_markup_view.zig +73 -0
  36. package/src/primitives/canvas/ui_markup_view_tests.zig +101 -0
  37. package/src/primitives/canvas/ui_schema.zig +17 -6
  38. package/src/primitives/canvas/ui_schema_tests.zig +7 -6
  39. package/src/primitives/canvas/ui_tests.zig +6 -0
  40. package/src/primitives/canvas/widget_invalidation.zig +3 -0
  41. package/src/primitives/canvas/widget_layout.zig +57 -8
  42. package/src/primitives/canvas/widget_layout_tests.zig +35 -0
  43. package/src/primitives/canvas/widget_metrics.zig +30 -1
  44. package/src/primitives/canvas/widget_render.zig +454 -9
  45. package/src/primitives/canvas/widget_runtime.zig +1 -0
  46. package/src/primitives/canvas/widget_text_input.zig +23 -11
  47. package/src/primitives/canvas/widget_text_select.zig +2 -2
  48. package/src/primitives/canvas/widgets.zig +13 -0
  49. package/src/root.zig +1 -0
  50. package/src/runtime/api.zig +9 -0
  51. package/src/runtime/canvas_frame_helpers.zig +1 -0
  52. package/src/runtime/canvas_limits.zig +7 -0
  53. package/src/runtime/canvas_widget_accessibility_tests.zig +27 -1
  54. package/src/runtime/canvas_widget_clipboard_tests.zig +146 -0
  55. package/src/runtime/canvas_widget_context_menu.zig +12 -2
  56. package/src/runtime/canvas_widget_display.zig +15 -1
  57. package/src/runtime/canvas_widget_events.zig +50 -20
  58. package/src/runtime/canvas_widget_runtime.zig +74 -12
  59. package/src/runtime/canvas_widget_state.zig +1 -0
  60. package/src/runtime/canvas_widget_text_tests.zig +1935 -70
  61. package/src/runtime/gpu_surface_events.zig +38 -0
  62. package/src/runtime/view.zig +37 -3
  63. package/src/runtime/view_widget_text.zig +1242 -14
  64. package/src/runtime/view_widget_tree.zig +2 -0
@@ -41,8 +41,11 @@ const TextAlign = @import("text_layout_types.zig").TextAlign;
41
41
  /// `max_text_span_lines_per_paragraph` lines. Overflow truncates
42
42
  /// deterministically instead of failing.
43
43
  pub const max_text_spans_per_paragraph: usize = 32;
44
- pub const max_text_span_runs_per_paragraph: usize = 128;
45
- pub const max_text_span_lines_per_paragraph: usize = 64;
44
+ pub const max_text_span_lines_per_paragraph: usize = 128;
45
+ // A maximally split highlighted paragraph can add one run at every span
46
+ // boundary in addition to one run per visual line.
47
+ pub const max_text_span_runs_per_paragraph: usize =
48
+ max_text_span_lines_per_paragraph + max_text_spans_per_paragraph;
46
49
 
47
50
  pub const TextSpanWeight = enum {
48
51
  regular,
@@ -143,9 +146,7 @@ pub fn textSpanFontId(span: TextSpan, typography: token_model.TypographyTokens)
143
146
  }
144
147
 
145
148
  pub fn textSpanColorValue(colors: token_model.ColorTokens, ref: TextSpanColor) Color {
146
- return switch (ref) {
147
- inline else => |tag| @field(colors, @tagName(tag)),
148
- };
149
+ return token_model.colorTokenValue(colors, ref);
149
150
  }
150
151
 
151
152
  pub fn textSpanScale(span: TextSpan) f32 {
@@ -170,24 +171,34 @@ pub fn textSpanLineHeight(spans: []const TextSpan, options: TextSpanLayoutOption
170
171
  return options.size * textSpansMaxScale(spans) * 1.25;
171
172
  }
172
173
 
173
- /// Single-line (unwrapped) advance of the whole paragraph: the intrinsic
174
- /// width seam for widget sizing. Measures per-span with the span's font.
174
+ /// Widest logical-line advance of an unwrapped paragraph: the intrinsic
175
+ /// width seam for widget sizing. Measures per-span with the span's font
176
+ /// while carrying each line across span boundaries.
175
177
  pub fn textSpansIntrinsicWidth(spans: []const TextSpan, options: TextSpanLayoutOptions) f32 {
176
178
  var width: f32 = 0;
179
+ var max_width: f32 = 0;
177
180
  for (spans, 0..) |span, index| {
178
181
  if (index >= max_text_spans_per_paragraph) break;
179
182
  var start: usize = 0;
180
183
  var cursor: usize = 0;
181
184
  while (cursor < span.text.len) {
182
- if (span.text[cursor] == '\n') {
185
+ if (span.text[cursor] == '\r') {
186
+ // CR is presentation-free. In CRLF source the following
187
+ // LF owns the hard break; a bare CR remains selectable and
188
+ // copyable without painting a fallback control glyph.
189
+ width += measureSpanSlice(span, span.text[start..cursor], options);
190
+ start = cursor + 1;
191
+ } else if (span.text[cursor] == '\n') {
183
192
  width += measureSpanSlice(span, span.text[start..cursor], options);
193
+ max_width = @max(max_width, width);
194
+ width = 0;
184
195
  start = cursor + 1;
185
196
  }
186
197
  cursor += 1;
187
198
  }
188
199
  width += measureSpanSlice(span, span.text[start..], options);
189
200
  }
190
- return width;
201
+ return @max(max_width, width);
191
202
  }
192
203
 
193
204
  /// Wrapped paragraph height at `max_width`: the vertical-extent seam the
@@ -199,6 +210,22 @@ pub fn textSpansWrappedHeight(spans: []const TextSpan, options: TextSpanLayoutOp
199
210
  }
200
211
 
201
212
  fn measureSpanSlice(span: TextSpan, slice: []const u8, options: TextSpanLayoutOptions) f32 {
213
+ if (slice.len == 0) return 0;
214
+ const first_cr = std.mem.indexOfScalar(u8, slice, '\r') orelse
215
+ return measureSpanSliceExact(span, slice, options);
216
+ var width: f32 = 0;
217
+ var start: usize = 0;
218
+ var cr: ?usize = first_cr;
219
+ while (cr) |index| {
220
+ width += measureSpanSliceExact(span, slice[start..index], options);
221
+ start = index + 1;
222
+ cr = std.mem.indexOfScalarPos(u8, slice, start, '\r');
223
+ }
224
+ width += measureSpanSliceExact(span, slice[start..], options);
225
+ return width;
226
+ }
227
+
228
+ fn measureSpanSliceExact(span: TextSpan, slice: []const u8, options: TextSpanLayoutOptions) f32 {
202
229
  if (slice.len == 0) return 0;
203
230
  const font_id = textSpanFontId(span, options.typography);
204
231
  const size = textSpanSize(span, options.size);
@@ -236,10 +263,211 @@ fn spanSliceAdvances(span: TextSpan, slice: []const u8, options: TextSpanLayoutO
236
263
  return advances[offset..][0..slice.len];
237
264
  }
238
265
 
266
+ pub const TextSpanRunVisibleSlice = struct {
267
+ text: []const u8,
268
+ /// Horizontal offset from the original run origin.
269
+ x: f32 = 0,
270
+ };
271
+
272
+ fn textSpanRunPrefixWidth(
273
+ span: TextSpan,
274
+ run: TextSpanRun,
275
+ options: TextSpanLayoutOptions,
276
+ end: usize,
277
+ ) f32 {
278
+ return measureSpanSlice(span, run.text[0..@min(end, run.text.len)], options);
279
+ }
280
+
281
+ /// First scalar boundary whose measured prefix reaches `target_x`.
282
+ /// The logarithmic search keeps long runs outside the batched-advance
283
+ /// scratch bound from turning viewport cropping into a quadratic prefix
284
+ /// walk.
285
+ fn textSpanRunBoundaryForX(
286
+ span: TextSpan,
287
+ run: TextSpanRun,
288
+ options: TextSpanLayoutOptions,
289
+ target_x: f32,
290
+ ) usize {
291
+ if (target_x <= 0) return 0;
292
+ if (target_x >= run.width) return run.text.len;
293
+
294
+ var low: usize = 0;
295
+ var high = run.text.len;
296
+ while (text_interaction.nextTextOffset(run.text, low) < high) {
297
+ var middle = text_interaction.snapTextOffset(
298
+ run.text,
299
+ low + (high - low) / 2,
300
+ );
301
+ if (middle <= low) middle = text_interaction.nextTextOffset(run.text, low);
302
+ if (middle >= high) middle = text_interaction.previousTextOffset(run.text, high);
303
+ if (middle <= low or middle >= high) break;
304
+ if (textSpanRunPrefixWidth(span, run, options, middle) < target_x) {
305
+ low = middle;
306
+ } else {
307
+ high = middle;
308
+ }
309
+ }
310
+ return high;
311
+ }
312
+
313
+ fn textSpanRunScalarAdvance(
314
+ span: TextSpan,
315
+ run: TextSpanRun,
316
+ options: TextSpanLayoutOptions,
317
+ start: usize,
318
+ ) f32 {
319
+ const end = text_interaction.nextTextOffset(run.text, start);
320
+ return @max(
321
+ 0,
322
+ textSpanRunPrefixWidth(span, run, options, end) -
323
+ textSpanRunPrefixWidth(span, run, options, start),
324
+ );
325
+ }
326
+
327
+ fn textSpanRunPreviousClusterStart(
328
+ span: TextSpan,
329
+ run: TextSpanRun,
330
+ options: TextSpanLayoutOptions,
331
+ start: usize,
332
+ ) usize {
333
+ var cursor = start;
334
+ while (cursor > 0) {
335
+ const previous = text_interaction.previousTextOffset(run.text, cursor);
336
+ if (textSpanRunScalarAdvance(span, run, options, previous) > 0) return previous;
337
+ cursor = previous;
338
+ }
339
+ return start;
340
+ }
341
+
342
+ fn textSpanRunGuardEnd(
343
+ span: TextSpan,
344
+ run: TextSpanRun,
345
+ options: TextSpanLayoutOptions,
346
+ start: usize,
347
+ ) usize {
348
+ var cursor = start;
349
+ var found_guard = false;
350
+ while (cursor < run.text.len) {
351
+ if (textSpanRunScalarAdvance(span, run, options, cursor) > 0) {
352
+ if (found_guard) return cursor;
353
+ found_guard = true;
354
+ }
355
+ cursor = text_interaction.nextTextOffset(run.text, cursor);
356
+ }
357
+ return run.text.len;
358
+ }
359
+
360
+ fn unbatchedTextSpanRunVisibleSlice(
361
+ span: TextSpan,
362
+ run: TextSpanRun,
363
+ options: TextSpanLayoutOptions,
364
+ min_x: f32,
365
+ max_x: f32,
366
+ ) ?TextSpanRunVisibleSlice {
367
+ const first_boundary = textSpanRunBoundaryForX(span, run, options, @max(0, min_x));
368
+ if (first_boundary == run.text.len and min_x >= run.width) return null;
369
+ const visible_start = text_interaction.previousTextOffset(run.text, first_boundary);
370
+ const first = textSpanRunPreviousClusterStart(span, run, options, visible_start);
371
+ const last_boundary = textSpanRunBoundaryForX(span, run, options, max_x);
372
+ const last = textSpanRunGuardEnd(span, run, options, last_boundary);
373
+ if (first >= last) return null;
374
+ return .{
375
+ .text = run.text[first..last],
376
+ .x = textSpanRunPrefixWidth(span, run, options, first),
377
+ };
378
+ }
379
+
380
+ /// Return the measured portion of `run` needed to cover the horizontal
381
+ /// interval `[min_x, max_x]`, plus one shaped cluster of guard ink on each
382
+ /// side. Batched providers supply their exact per-cluster advances; the
383
+ /// unbatched seam uses a logarithmic contextual-prefix search. A provider
384
+ /// may represent a multi-codepoint cluster by placing its advance on the
385
+ /// first scalar and zero on the rest, so cut points are chosen only at the
386
+ /// next positive-advance cluster start.
387
+ pub fn textSpanRunVisibleSlice(
388
+ span: TextSpan,
389
+ run: TextSpanRun,
390
+ options: TextSpanLayoutOptions,
391
+ min_x: f32,
392
+ max_x: f32,
393
+ ) ?TextSpanRunVisibleSlice {
394
+ if (run.text.len == 0) return null;
395
+ if (!std.math.isFinite(min_x) or
396
+ !std.math.isFinite(max_x) or
397
+ min_x <= 0 and max_x >= run.width)
398
+ {
399
+ return .{ .text = run.text };
400
+ }
401
+
402
+ const measured_advances = spanSliceAdvances(span, run.text, options, run.font_id, run.size);
403
+ if (measured_advances == null) {
404
+ return unbatchedTextSpanRunVisibleSlice(span, run, options, min_x, max_x);
405
+ }
406
+ var cursor: usize = 0;
407
+ var x: f32 = 0;
408
+ var previous_cluster_start: ?usize = null;
409
+ var previous_cluster_x: f32 = 0;
410
+ var first: usize = 0;
411
+ var first_x: f32 = 0;
412
+ var found_first = false;
413
+ var right_guard_seen = false;
414
+
415
+ while (cursor < run.text.len) {
416
+ const next = text_interaction.nextTextOffset(run.text, cursor);
417
+ var advance: f32 = 0;
418
+ for (measured_advances.?[cursor..next]) |value| advance += value;
419
+ advance = @max(0, advance);
420
+
421
+ if (std.math.isFinite(advance) and advance > 0) {
422
+ if (!found_first and x + advance > @max(0, min_x)) {
423
+ if (previous_cluster_start) |start| {
424
+ first = start;
425
+ first_x = previous_cluster_x;
426
+ } else {
427
+ first = cursor;
428
+ first_x = x;
429
+ }
430
+ found_first = true;
431
+ }
432
+
433
+ if (found_first and x >= max_x) {
434
+ if (right_guard_seen) {
435
+ if (first >= cursor) return null;
436
+ return .{
437
+ .text = run.text[first..cursor],
438
+ .x = first_x,
439
+ };
440
+ }
441
+ right_guard_seen = true;
442
+ }
443
+
444
+ previous_cluster_start = cursor;
445
+ previous_cluster_x = x;
446
+ }
447
+ x += advance;
448
+ cursor = next;
449
+ }
450
+
451
+ if (!found_first) {
452
+ // A zero-width run cannot be cropped meaningfully; retain it so a
453
+ // platform shaper still receives the original cluster sequence.
454
+ if (x <= 0) return .{ .text = run.text };
455
+ return null;
456
+ }
457
+ return .{
458
+ .text = run.text[first..],
459
+ .x = first_x,
460
+ };
461
+ }
462
+
239
463
  const LayoutState = struct {
240
464
  spans: []const TextSpan,
241
465
  options: TextSpanLayoutOptions,
242
466
  runs: []TextSpanRun,
467
+ /// First absolute visual line retained in `runs`. Earlier lines are
468
+ /// still measured so the returned extent and absolute baselines stay
469
+ /// identical to a full paragraph layout.
470
+ run_line_start: usize = 0,
243
471
  run_len: usize = 0,
244
472
  line_index: usize = 0,
245
473
  line_run_start: usize = 0,
@@ -291,6 +519,7 @@ const LayoutState = struct {
291
519
  self.max_line_width = @max(self.max_line_width, self.pen_x);
292
520
  self.line_has_content = true;
293
521
  }
522
+ if (self.line_index < self.run_line_start) return;
294
523
  if (self.run_len > self.line_run_start) {
295
524
  const previous = &self.runs[self.run_len - 1];
296
525
  if (previous.span_index == span_index and
@@ -302,7 +531,9 @@ const LayoutState = struct {
302
531
  return;
303
532
  }
304
533
  }
305
- if (self.run_len >= self.runs.len or self.line_index >= max_text_span_lines_per_paragraph) {
534
+ if (self.run_len >= self.runs.len or
535
+ self.line_index >= self.run_line_start +| max_text_span_lines_per_paragraph)
536
+ {
306
537
  self.truncated = true;
307
538
  return;
308
539
  }
@@ -359,18 +590,38 @@ pub fn layoutTextSpans(spans: []const TextSpan, options: TextSpanLayoutOptions,
359
590
  if (findSpanWrapEntry(cache, key)) |entry_index| {
360
591
  if (rebaseSpanWrapEntry(cache, entry_index, spans, options, runs_storage)) |layout| return layout;
361
592
  }
362
- const layout = layoutTextSpansUncached(spans, options, runs_storage);
593
+ const layout = layoutTextSpansUncached(spans, options, 0, runs_storage);
363
594
  storeSpanWrapEntry(cache, key, spans, layout);
364
595
  return layout;
365
596
  }
366
- return layoutTextSpansUncached(spans, options, runs_storage);
597
+ return layoutTextSpansUncached(spans, options, 0, runs_storage);
598
+ }
599
+
600
+ /// Lay out the full paragraph while retaining only the bounded visual-line
601
+ /// page beginning at `first_line`. Runs keep absolute line indexes and
602
+ /// baselines, so a viewport can paint later pages without changing the
603
+ /// paragraph's measured size or allocating source-sized storage.
604
+ pub fn layoutTextSpansFromLine(
605
+ spans: []const TextSpan,
606
+ options: TextSpanLayoutOptions,
607
+ first_line: usize,
608
+ runs_storage: []TextSpanRun,
609
+ ) TextSpanLayout {
610
+ if (first_line == 0) return layoutTextSpans(spans, options, runs_storage);
611
+ return layoutTextSpansUncached(spans, options, first_line, runs_storage);
367
612
  }
368
613
 
369
- fn layoutTextSpansUncached(spans: []const TextSpan, options: TextSpanLayoutOptions, runs_storage: []TextSpanRun) TextSpanLayout {
614
+ fn layoutTextSpansUncached(
615
+ spans: []const TextSpan,
616
+ options: TextSpanLayoutOptions,
617
+ first_line: usize,
618
+ runs_storage: []TextSpanRun,
619
+ ) TextSpanLayout {
370
620
  var state = LayoutState{
371
621
  .spans = spans,
372
622
  .options = options,
373
623
  .runs = runs_storage,
624
+ .run_line_start = first_line,
374
625
  .line_height = textSpanLineHeight(spans, options),
375
626
  .baseline_offset = options.size * textSpansMaxScale(spans),
376
627
  .max_width = if (options.wrap != .none and options.max_width > 0 and std.math.isFinite(options.max_width))
@@ -396,11 +647,21 @@ fn layoutTextSpansUncached(spans: []const TextSpan, options: TextSpanLayoutOptio
396
647
  offset += 1;
397
648
  continue;
398
649
  }
650
+ if (byte == '\r') {
651
+ // Preserve the byte in paragraph offsets/clipboard data but do
652
+ // not place it in a glyph run. LF remains the single hard-line
653
+ // delimiter for both Unix and Windows source.
654
+ offset += 1;
655
+ continue;
656
+ }
399
657
  if (isSpanBreakByte(byte)) {
400
658
  const end = spanWhitespaceEnd(text, offset);
401
- // Whitespace at a fresh line start is consumed by the wrap;
402
- // mid-line whitespace is held back until the next word lands.
403
- if (state.line_has_content) {
659
+ // Prose consumes whitespace at a fresh line start, but a
660
+ // monospace span is preformatted content (markdown fences are
661
+ // assembled from these) and must keep its source indentation.
662
+ // In both cases whitespace is held until the next word lands,
663
+ // so trailing spaces still never widen a rendered line.
664
+ if (state.line_has_content or spans[span_index].monospace) {
404
665
  const slice = text[offset..end];
405
666
  state.recordPendingWhitespace(span_index, slice, measureSpanSlice(spans[span_index], slice, options));
406
667
  }
@@ -793,7 +1054,7 @@ fn spanWhitespaceEnd(text: []const u8, start: usize) usize {
793
1054
 
794
1055
  fn spanWordEnd(text: []const u8, start: usize) usize {
795
1056
  var end = start;
796
- while (end < text.len and text[end] != '\n' and !isSpanBreakByte(text[end])) end += 1;
1057
+ while (end < text.len and text[end] != '\n' and text[end] != '\r' and !isSpanBreakByte(text[end])) end += 1;
797
1058
  return end;
798
1059
  }
799
1060
 
@@ -846,8 +1107,8 @@ pub fn textSpanOffsetForPoint(
846
1107
  point: geometry.PointF,
847
1108
  ) ?usize {
848
1109
  var runs: [max_text_span_runs_per_paragraph]TextSpanRun = undefined;
849
- const layout = layoutTextSpans(spans, options, &runs);
850
- if (layout.runs.len == 0 or layout.line_count == 0 or layout.line_height <= 0) return null;
1110
+ var layout = layoutTextSpans(spans, options, &runs);
1111
+ if (layout.line_count == 0 or layout.line_height <= 0) return null;
851
1112
 
852
1113
  const raw_line = point.y / layout.line_height;
853
1114
  const max_line = layout.line_count - 1;
@@ -856,6 +1117,50 @@ pub fn textSpanOffsetForPoint(
856
1117
  else
857
1118
  @min(max_line, @as(usize, @intFromFloat(@floor(raw_line))));
858
1119
 
1120
+ // Page the same bounded paragraph layout the viewport painter uses.
1121
+ // Page zero deliberately retains only 128 visual lines; without this
1122
+ // handoff every point below that page collapsed onto its last source
1123
+ // offset even though later runs were visibly painted.
1124
+ if (line_index >= max_text_span_lines_per_paragraph) {
1125
+ layout = layoutTextSpansFromLine(spans, options, line_index, &runs);
1126
+ }
1127
+ if (layout.runs.len == 0) {
1128
+ // Preformatted whitespace is valid selectable source even though
1129
+ // the layout deliberately keeps it out of painted runs. Preserve
1130
+ // both edges so a drag can select/copy an all-whitespace block.
1131
+ if (paragraphOnlyUnpaintedWhitespace(paragraph)) {
1132
+ const source_line_count = 1 + std.mem.count(u8, paragraph, "\n") -
1133
+ @intFromBool(paragraph[paragraph.len - 1] == '\n');
1134
+ const source_line: usize = if (raw_line < 0)
1135
+ 0
1136
+ else
1137
+ @min(source_line_count - 1, @as(usize, @intFromFloat(@floor(raw_line))));
1138
+ const edge = unpaintedWhitespaceLineRange(paragraph, source_line);
1139
+ const width = textSpanParagraphRangeWidth(
1140
+ paragraph,
1141
+ spans,
1142
+ options,
1143
+ text_interaction.TextRange.init(
1144
+ edge.start,
1145
+ if (edge.end > edge.start and paragraph[edge.end - 1] == '\n')
1146
+ edge.end - 1
1147
+ else
1148
+ edge.end,
1149
+ ),
1150
+ ) orelse 0;
1151
+ const midpoint = if (width > 0)
1152
+ width * 0.5
1153
+ else if (options.max_width > 0 and std.math.isFinite(options.max_width))
1154
+ options.max_width * 0.5
1155
+ else
1156
+ 0;
1157
+ return if (point.x < midpoint) edge.start else edge.end;
1158
+ }
1159
+ // A later page made only of trailing blank logical lines has no
1160
+ // glyph run to map. Its nearest source edge is the paragraph end.
1161
+ return if (paragraph.len > 0) paragraph.len else null;
1162
+ }
1163
+
859
1164
  var result: ?usize = null;
860
1165
  var first_range: ?text_interaction.TextRange = null;
861
1166
  var last_range: ?text_interaction.TextRange = null;
@@ -878,11 +1183,73 @@ pub fn textSpanOffsetForPoint(
878
1183
  const first = first_range orelse return lineFallbackOffset(paragraph, layout, line_index);
879
1184
  if (point.x < first_x) return first.start;
880
1185
  if (last_range) |last| {
881
- if (point.x >= last_end_x) return last.end;
1186
+ if (point.x >= last_end_x) return textSpanLineSourceEnd(paragraph, last.end);
882
1187
  }
883
1188
  return first.start;
884
1189
  }
885
1190
 
1191
+ fn paragraphOnlyUnpaintedWhitespace(paragraph: []const u8) bool {
1192
+ if (paragraph.len == 0) return false;
1193
+ for (paragraph) |byte| {
1194
+ if (byte != '\n' and byte != '\r' and !isSpanBreakByte(byte)) return false;
1195
+ }
1196
+ return true;
1197
+ }
1198
+
1199
+ fn unpaintedWhitespaceLineRange(paragraph: []const u8, line_index: usize) text_interaction.TextRange {
1200
+ var start: usize = 0;
1201
+ var line: usize = 0;
1202
+ while (line < line_index) : (line += 1) {
1203
+ const newline = std.mem.indexOfScalarPos(u8, paragraph, start, '\n') orelse
1204
+ return text_interaction.TextRange.init(paragraph.len, paragraph.len);
1205
+ start = newline + 1;
1206
+ }
1207
+ const newline = std.mem.indexOfScalarPos(u8, paragraph, start, '\n');
1208
+ return text_interaction.TextRange.init(start, if (newline) |index| index + 1 else paragraph.len);
1209
+ }
1210
+
1211
+ fn textSpanParagraphRangeWidth(
1212
+ paragraph: []const u8,
1213
+ spans: []const TextSpan,
1214
+ options: TextSpanLayoutOptions,
1215
+ range: text_interaction.TextRange,
1216
+ ) ?f32 {
1217
+ if (range.start >= range.end) return 0;
1218
+ const paragraph_base = @intFromPtr(paragraph.ptr);
1219
+ var covered = range.start;
1220
+ var width: f32 = 0;
1221
+ for (spans) |span| {
1222
+ const span_start_ptr = @intFromPtr(span.text.ptr);
1223
+ if (span_start_ptr < paragraph_base) return null;
1224
+ const span_start = span_start_ptr - paragraph_base;
1225
+ const span_end = span_start + span.text.len;
1226
+ if (span_end > paragraph.len) return null;
1227
+ const start = @max(range.start, span_start);
1228
+ const end = @min(range.end, span_end);
1229
+ if (start >= end) continue;
1230
+ if (start > covered) return null;
1231
+ width += measureSpanSlice(
1232
+ span,
1233
+ span.text[start - span_start .. end - span_start],
1234
+ options,
1235
+ );
1236
+ covered = @max(covered, end);
1237
+ if (covered >= range.end) return width;
1238
+ }
1239
+ return null;
1240
+ }
1241
+
1242
+ /// Extend a painted run edge through source bytes that deliberately carry
1243
+ /// no ink at the end of its visual line: spaces/tabs and one explicit
1244
+ /// newline. A wrapped word begins the next line without being consumed.
1245
+ fn textSpanLineSourceEnd(paragraph: []const u8, painted_end: usize) usize {
1246
+ var end = @min(painted_end, paragraph.len);
1247
+ while (end < paragraph.len and isSpanBreakByte(paragraph[end])) end += 1;
1248
+ if (end < paragraph.len and paragraph[end] == '\r') end += 1;
1249
+ if (end < paragraph.len and paragraph[end] == '\n') end += 1;
1250
+ return end;
1251
+ }
1252
+
886
1253
  /// Offset within `run.text` for a run-relative x, midpoint rule per
887
1254
  /// codepoint, mirroring the plain-text `textLineOffsetForX`.
888
1255
  fn spanRunOffsetForX(span: TextSpan, run: TextSpanRun, options: TextSpanLayoutOptions, x: f32) usize {
@@ -918,8 +1285,9 @@ fn lineFallbackOffset(paragraph: []const u8, layout: TextSpanLayout, line_index:
918
1285
 
919
1286
  /// Selection highlight rects (relative to the paragraph origin) for a
920
1287
  /// paragraph byte range: one rect per line, spanning the selected extent
921
- /// across that line's runs. Returns the rects that fit in `output`;
922
- /// overflow truncates deterministically like span layout itself.
1288
+ /// across that line's runs. A range spanning more lines than `output`
1289
+ /// holds folds the overflow into the last rectangle, matching plain text
1290
+ /// selection so long selections stay truthfully highlighted.
923
1291
  pub fn textSpanSelectionRects(
924
1292
  paragraph: []const u8,
925
1293
  spans: []const TextSpan,
@@ -929,18 +1297,169 @@ pub fn textSpanSelectionRects(
929
1297
  ) []const text_interaction.TextSelectionRect {
930
1298
  const normalized = text_interaction.snapTextRange(paragraph, range);
931
1299
  if (normalized.isCollapsed(paragraph.len)) return output[0..0];
1300
+ if (output.len == 0) return output[0..0];
932
1301
 
933
1302
  var runs: [max_text_span_runs_per_paragraph]TextSpanRun = undefined;
934
- const layout = layoutTextSpans(spans, options, &runs);
935
- if (layout.line_height <= 0) return output[0..0];
936
-
1303
+ const first_layout = layoutTextSpans(spans, options, &runs);
1304
+ if (first_layout.line_height <= 0 or first_layout.line_count == 0) return output[0..0];
1305
+
1306
+ const page_count = (first_layout.line_count +
1307
+ max_text_span_lines_per_paragraph - 1) /
1308
+ max_text_span_lines_per_paragraph;
1309
+ const first_page = textSpanSelectionFirstPage(
1310
+ paragraph,
1311
+ spans,
1312
+ options,
1313
+ normalized.start,
1314
+ page_count,
1315
+ &runs,
1316
+ ) orelse return output[0..0];
937
1317
  var len: usize = 0;
1318
+ var page_index = first_page;
1319
+ while (page_index < page_count) : (page_index += 1) {
1320
+ const first_line = page_index * max_text_span_lines_per_paragraph;
1321
+ const layout = if (page_index == 0)
1322
+ first_layout
1323
+ else
1324
+ layoutTextSpansFromLine(spans, options, first_line, &runs);
1325
+ const page_end = appendTextSpanSelectionPage(
1326
+ paragraph,
1327
+ spans,
1328
+ options,
1329
+ normalized,
1330
+ layout,
1331
+ output,
1332
+ &len,
1333
+ );
1334
+ if (page_end >= normalized.end) break;
1335
+ }
1336
+ return output[0..len];
1337
+ }
1338
+
1339
+ /// Earliest visual-line page whose retained source runs can intersect a
1340
+ /// selection beginning at `offset`. Non-empty pages stay logarithmic; when
1341
+ /// explicit newlines create an empty page, inspect the surrounding gap
1342
+ /// because emptiness alone does not order that page against the offset.
1343
+ fn textSpanSelectionFirstPage(
1344
+ paragraph: []const u8,
1345
+ spans: []const TextSpan,
1346
+ options: TextSpanLayoutOptions,
1347
+ offset: usize,
1348
+ page_count: usize,
1349
+ runs: []TextSpanRun,
1350
+ ) ?usize {
1351
+ var low: usize = 0;
1352
+ var high = page_count;
1353
+ var candidate: ?usize = null;
1354
+ while (low < high) {
1355
+ const middle = low + (high - low) / 2;
1356
+ const page_end = textSpanSelectionPageEnd(
1357
+ paragraph,
1358
+ spans,
1359
+ options,
1360
+ middle,
1361
+ runs,
1362
+ );
1363
+ if (page_end) |end| {
1364
+ if (end <= offset) {
1365
+ low = middle + 1;
1366
+ } else {
1367
+ candidate = middle;
1368
+ high = middle;
1369
+ }
1370
+ continue;
1371
+ }
1372
+
1373
+ // A runless page may sit between source before and after `offset`.
1374
+ // Find its nearest retained neighbor on each side before deciding
1375
+ // which half can be discarded.
1376
+ var left = middle;
1377
+ var left_end: ?usize = null;
1378
+ while (left > low) {
1379
+ left -= 1;
1380
+ left_end = textSpanSelectionPageEnd(
1381
+ paragraph,
1382
+ spans,
1383
+ options,
1384
+ left,
1385
+ runs,
1386
+ );
1387
+ if (left_end != null) break;
1388
+ }
1389
+ if (left_end) |end| {
1390
+ if (end > offset) {
1391
+ candidate = left;
1392
+ high = left;
1393
+ continue;
1394
+ }
1395
+ }
1396
+
1397
+ var right = middle + 1;
1398
+ var right_end: ?usize = null;
1399
+ while (right < high) : (right += 1) {
1400
+ right_end = textSpanSelectionPageEnd(
1401
+ paragraph,
1402
+ spans,
1403
+ options,
1404
+ right,
1405
+ runs,
1406
+ );
1407
+ if (right_end != null) break;
1408
+ }
1409
+ if (right_end) |end| {
1410
+ if (end > offset) return right;
1411
+ low = right + 1;
1412
+ continue;
1413
+ }
1414
+
1415
+ // No retained run in this interval precedes the best page already
1416
+ // found by the binary search.
1417
+ return candidate;
1418
+ }
1419
+ return candidate;
1420
+ }
1421
+
1422
+ fn textSpanSelectionPageEnd(
1423
+ paragraph: []const u8,
1424
+ spans: []const TextSpan,
1425
+ options: TextSpanLayoutOptions,
1426
+ page_index: usize,
1427
+ runs: []TextSpanRun,
1428
+ ) ?usize {
1429
+ const layout = layoutTextSpansFromLine(
1430
+ spans,
1431
+ options,
1432
+ page_index * max_text_span_lines_per_paragraph,
1433
+ runs,
1434
+ );
1435
+ var page_end: ?usize = null;
1436
+ for (layout.runs) |run| {
1437
+ const run_range = textSpanRunParagraphRange(paragraph, run) orelse continue;
1438
+ page_end = @max(page_end orelse 0, run_range.end);
1439
+ }
1440
+ return page_end;
1441
+ }
1442
+
1443
+ /// Append selection geometry from one absolute visual-line page. Returns
1444
+ /// the furthest paragraph byte represented by that page so the caller can
1445
+ /// stop once the selected tail has been covered.
1446
+ fn appendTextSpanSelectionPage(
1447
+ paragraph: []const u8,
1448
+ spans: []const TextSpan,
1449
+ options: TextSpanLayoutOptions,
1450
+ normalized: text_interaction.TextRange,
1451
+ layout: TextSpanLayout,
1452
+ output: []text_interaction.TextSelectionRect,
1453
+ len: *usize,
1454
+ ) usize {
1455
+ var page_end: usize = 0;
938
1456
  var current_line: ?usize = null;
939
1457
  var line_left: f32 = 0;
940
1458
  var line_right: f32 = 0;
941
1459
  var line_range = text_interaction.TextRange.init(0, 0);
942
1460
  for (layout.runs) |run| {
943
1461
  const run_range = textSpanRunParagraphRange(paragraph, run) orelse continue;
1462
+ page_end = @max(page_end, run_range.end);
944
1463
  const start = @max(normalized.start, run_range.start);
945
1464
  const end = @min(normalized.end, run_range.end);
946
1465
  if (start >= end) continue;
@@ -955,7 +1474,7 @@ pub fn textSpanSelectionRects(
955
1474
  line_range = text_interaction.TextRange.init(@min(line_range.start, start), @max(line_range.end, end));
956
1475
  continue;
957
1476
  }
958
- if (!flushSpanSelectionLine(layout, line, line_left, line_right, line_range, output, &len)) return output[0..len];
1477
+ flushSpanSelectionLine(layout, line, line_left, line_right, line_range, output, len);
959
1478
  }
960
1479
  current_line = run.line_index;
961
1480
  line_left = @min(x0, x1);
@@ -963,9 +1482,9 @@ pub fn textSpanSelectionRects(
963
1482
  line_range = text_interaction.TextRange.init(start, end);
964
1483
  }
965
1484
  if (current_line) |line| {
966
- if (!flushSpanSelectionLine(layout, line, line_left, line_right, line_range, output, &len)) return output[0..len];
1485
+ flushSpanSelectionLine(layout, line, line_left, line_right, line_range, output, len);
967
1486
  }
968
- return output[0..len];
1487
+ return page_end;
969
1488
  }
970
1489
 
971
1490
  fn flushSpanSelectionLine(
@@ -976,15 +1495,20 @@ fn flushSpanSelectionLine(
976
1495
  range: text_interaction.TextRange,
977
1496
  output: []text_interaction.TextSelectionRect,
978
1497
  len: *usize,
979
- ) bool {
980
- if (len.* >= output.len) return false;
1498
+ ) void {
981
1499
  const top = @as(f32, @floatFromInt(line_index)) * layout.line_height;
982
- output[len.*] = .{
1500
+ const selection = text_interaction.TextSelectionRect{
983
1501
  .range = range,
984
1502
  .rect = geometry.RectF.init(left, top, @max(1, right - left), @max(1, layout.line_height)),
985
1503
  };
1504
+ if (len.* >= output.len) {
1505
+ const last = &output[output.len - 1];
1506
+ last.range = text_interaction.TextRange.init(last.range.start, range.end);
1507
+ last.rect = last.rect.unionWith(selection.rect);
1508
+ return;
1509
+ }
1510
+ output[len.*] = selection;
986
1511
  len.* += 1;
987
- return true;
988
1512
  }
989
1513
 
990
1514
  /// Deep equality for widget invalidation: styles, text bytes, and link