@native-sdk/cli 0.9.4 → 0.10.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 (97) hide show
  1. package/README.md +3 -3
  2. package/build/app.zig +75 -57
  3. package/build/ts_run.mjs +1 -1
  4. package/build.zig +20 -2
  5. package/package.json +11 -10
  6. package/packages/core/package-lock.json +12 -11
  7. package/packages/core/package.json +2 -2
  8. package/packages/core/scripts/compiler_command.mjs +1 -1
  9. package/packages/core/scripts/run_external_core_compiler.mjs +19 -9
  10. package/packages/core/scripts/run_external_service_compiler.mjs +17 -8
  11. package/packages/core/scripts/vendor_service_packages.mjs +121 -8
  12. package/packages/core/sdk/events.d.ts +33 -4
  13. package/packages/core/sdk/events.ts +45 -6
  14. package/packages/core/src/checker.ts +103 -8
  15. package/packages/core/src/modules.ts +6 -2
  16. package/packages/core/src/sqlite_cli.ts +22 -2
  17. package/packages/core/src/typed_ast.ts +4 -2
  18. package/packages/core/src/types.ts +8 -8
  19. package/schemas/app.schema.json +446 -0
  20. package/skill-data/core/SKILL.md +53 -15
  21. package/skill-data/core/references/project-anatomy.md +6 -6
  22. package/skill-data/native-ui/SKILL.md +26 -5
  23. package/skill-data/ts-core/SKILL.md +6 -6
  24. package/skill-data/ts-services/references/service-surface.md +1 -1
  25. package/skills/native-sdk/SKILL.md +2 -2
  26. package/src/app_runner/root.zig +25 -0
  27. package/src/automation/snapshot.zig +91 -3
  28. package/src/platform/macos/appkit_host.h +66 -2
  29. package/src/platform/macos/appkit_host.m +735 -24
  30. package/src/platform/macos/cef_host.mm +208 -9
  31. package/src/platform/macos/root.zig +205 -3
  32. package/src/platform/null_platform.zig +50 -1
  33. package/src/platform/null_platform_tests.zig +53 -0
  34. package/src/platform/root.zig +12 -0
  35. package/src/platform/types.zig +73 -0
  36. package/src/platform/windows/root.zig +88 -15
  37. package/src/primitives/app_manifest/tests.zig +19 -0
  38. package/src/primitives/app_manifest/validation.zig +14 -2
  39. package/src/primitives/canvas/font_ttf.zig +24 -0
  40. package/src/primitives/canvas/gpu.zig +5 -0
  41. package/src/primitives/canvas/reference.zig +41 -16
  42. package/src/primitives/canvas/reference_tests.zig +63 -0
  43. package/src/primitives/canvas/render_tests.zig +1 -1
  44. package/src/primitives/canvas/root.zig +1 -0
  45. package/src/primitives/canvas/serialization.zig +1 -0
  46. package/src/primitives/canvas/test_support.zig +1 -0
  47. package/src/primitives/canvas/text.zig +1 -0
  48. package/src/primitives/canvas/text_layout.zig +84 -15
  49. package/src/primitives/canvas/text_layout_hash.zig +3 -1
  50. package/src/primitives/canvas/text_layout_types.zig +3 -0
  51. package/src/primitives/canvas/text_metrics.zig +28 -1
  52. package/src/primitives/canvas/text_tests.zig +61 -6
  53. package/src/primitives/canvas/ui.zig +9 -0
  54. package/src/primitives/canvas/ui_markup.zig +29 -0
  55. package/src/primitives/canvas/ui_markup_compiled.zig +32 -0
  56. package/src/primitives/canvas/ui_markup_compiled_tests.zig +4 -2
  57. package/src/primitives/canvas/ui_markup_contract.zig +7 -0
  58. package/src/primitives/canvas/ui_markup_contract_tests.zig +21 -0
  59. package/src/primitives/canvas/ui_markup_tests.zig +6 -0
  60. package/src/primitives/canvas/ui_markup_view.zig +28 -0
  61. package/src/primitives/canvas/ui_markup_view_tests.zig +32 -3
  62. package/src/primitives/canvas/ui_schema.zig +9 -0
  63. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  64. package/src/primitives/canvas/ui_tests.zig +4 -2
  65. package/src/primitives/canvas/widget_render.zig +13 -2
  66. package/src/primitives/canvas/widget_render_controls.zig +1 -0
  67. package/src/primitives/canvas/widget_semantics_tests.zig +3 -1
  68. package/src/root.zig +6 -0
  69. package/src/runtime/automation_snapshot.zig +3 -0
  70. package/src/runtime/canvas_fonts.zig +87 -0
  71. package/src/runtime/core.zig +1 -0
  72. package/src/runtime/flow.zig +8 -1
  73. package/src/runtime/native_command_event_tests.zig +58 -0
  74. package/src/runtime/state.zig +14 -0
  75. package/src/runtime/system_services.zig +62 -0
  76. package/src/runtime/ts_ui_app.zig +161 -33
  77. package/src/runtime/ui_app.zig +33 -1
  78. package/src/runtime/ui_app_tests.zig +63 -0
  79. package/src/runtime/validation.zig +87 -1
  80. package/src/tooling/android.zig +5 -4
  81. package/src/tooling/buildgraph.zig +24 -8
  82. package/src/tooling/db.zig +1 -1
  83. package/src/tooling/doctor.zig +1 -1
  84. package/src/tooling/ios.zig +5 -4
  85. package/src/tooling/json_to_zon.zig +136 -0
  86. package/src/tooling/manifest.zig +199 -35
  87. package/src/tooling/package.zig +33 -0
  88. package/src/tooling/raw_manifest.zig +11 -0
  89. package/src/tooling/root.zig +2 -0
  90. package/src/tooling/templates.zig +210 -116
  91. package/src/tooling/ts_core.zig +11 -7
  92. package/src/tooling/update.zig +576 -0
  93. package/src/tooling/verbs.zig +7 -6
  94. package/src/tooling/web_engine.zig +17 -1
  95. package/src/updater/c_api.zig +123 -0
  96. package/src/updater/feed.zig +215 -0
  97. package/src/updater/root.zig +10 -0
@@ -486,6 +486,7 @@ pub const ReferenceRenderSurface = struct {
486
486
  } else return error.ReferenceRenderUnsupportedCommand;
487
487
 
488
488
  const src_rect = referenceImageSourceRect(image, value.src) orelse return;
489
+ const sample_bounds = referenceImageSampleBounds(image, src_rect);
489
490
  const local_dst = referenceImageDestinationRect(value.dst, src_rect, value.fit) orelse return;
490
491
  const dst_rect = command.transform.transformRect(local_dst).normalized();
491
492
  // The rounded mask applies over the REQUESTED destination (the
@@ -510,7 +511,7 @@ pub const ReferenceRenderSurface = struct {
510
511
  // once per (image content, size, phase) and every later repaint
511
512
  // — the cover-loading cascade, a re-opened view, a whole-pixel
512
513
  // move — blends from the panel.
513
- if (self.imageScalePanel(image, value, src_rect, dst_rect, pixel_rect)) |panel| {
514
+ if (self.imageScalePanel(image, value, src_rect, sample_bounds, dst_rect, pixel_rect)) |panel| {
514
515
  const dst_x0: i64 = @intFromFloat(@floor(dst_rect.x));
515
516
  const dst_y0: i64 = @intFromFloat(@floor(dst_rect.y));
516
517
  var y = pixel_rect.y;
@@ -549,7 +550,7 @@ pub const ReferenceRenderSurface = struct {
549
550
  if (has_mask and !referencePointInRoundedRect(point, mask_rect, mask_radius)) continue;
550
551
  const u = std.math.clamp((point.x - dst_rect.x) / dst_rect.width, 0, 1);
551
552
  const v = std.math.clamp((point.y - dst_rect.y) / dst_rect.height, 0, 1);
552
- const sample = referenceSampleImage(image, src_rect, u, v, value.sampling);
553
+ const sample = referenceSampleImage(image, src_rect, sample_bounds, u, v, value.sampling);
553
554
  const index = (y * self.width + x) * 4;
554
555
  const dst = [4]u8{
555
556
  self.pixels[index + 0],
@@ -575,7 +576,7 @@ pub const ReferenceRenderSurface = struct {
575
576
  width: usize,
576
577
  };
577
578
 
578
- fn imageScalePanel(self: ReferenceRenderSurface, image: ReferenceImage, value: DrawImage, src_rect: geometry.RectF, dst_rect: geometry.RectF, pixel_rect: ReferencePixelRect) ?ImageScalePanel {
579
+ fn imageScalePanel(self: ReferenceRenderSurface, image: ReferenceImage, value: DrawImage, src_rect: geometry.RectF, sample_bounds: ReferenceImageSampleBounds, dst_rect: geometry.RectF, pixel_rect: ReferencePixelRect) ?ImageScalePanel {
579
580
  const memo = self.render_memo orelse return null;
580
581
  // Exact-arithmetic bounds: pixel offsets and phases must stay in
581
582
  // f32's exact-integer range for the phase-relative identity
@@ -630,7 +631,7 @@ pub const ReferenceRenderSurface = struct {
630
631
  var column: usize = 0;
631
632
  while (column < panel_width) : (column += 1) {
632
633
  const u = std.math.clamp(((@as(f32, @floatFromInt(column)) + 0.5) - phase_x) / dst_rect.width, 0, 1);
633
- const sample = referenceSampleImage(image, src_rect, u, v, value.sampling);
634
+ const sample = referenceSampleImage(image, src_rect, sample_bounds, u, v, value.sampling);
634
635
  const offset = (row * panel_width + column) * 4;
635
636
  buffer[offset] = sample[0];
636
637
  buffer[offset + 1] = sample[1];
@@ -781,7 +782,10 @@ pub const ReferenceRenderSurface = struct {
781
782
  // one tail glyph per multibyte codepoint: the flat 0.65em
782
783
  // multibyte estimate overshot the provider's real advances and
783
784
  // pushed the tail past the measured clip bounds.
784
- const measure = if (value.text_layout) |options| options.measure else null;
785
+ const measure = if (value.text_layout) |options|
786
+ options.measure orelse value.measure
787
+ else
788
+ value.measure;
785
789
  const end = @min(value.text.len, line.text_start + line.paintedTextLen());
786
790
  var text_offset: usize = line.text_start;
787
791
  var x = line.bounds.x;
@@ -1372,22 +1376,43 @@ const ReferencePremultipliedLinearColor = struct {
1372
1376
  a: f32 = 0,
1373
1377
  };
1374
1378
 
1375
- fn referenceSampleImage(image: ReferenceImage, src: geometry.RectF, u: f32, v: f32, sampling: ImageSampling) [4]u8 {
1379
+ fn referenceSampleImage(image: ReferenceImage, src: geometry.RectF, bounds: ReferenceImageSampleBounds, u: f32, v: f32, sampling: ImageSampling) [4]u8 {
1376
1380
  return switch (sampling) {
1377
- .nearest => referenceSampleImageNearest(image, src, u, v),
1378
- .linear => referenceSampleImageLinear(image, src, u, v),
1381
+ .nearest => referenceSampleImageNearest(image, src, bounds, u, v),
1382
+ .linear => referenceSampleImageLinear(image, src, bounds, u, v),
1383
+ };
1384
+ }
1385
+
1386
+ const ReferenceImageSampleBounds = struct {
1387
+ min_x: i32,
1388
+ min_y: i32,
1389
+ max_x: i32,
1390
+ max_y: i32,
1391
+ };
1392
+
1393
+ /// Inclusive texel bounds touched by a clipped source rectangle. Native
1394
+ /// image APIs constrain filtering to their source portion; mirror that
1395
+ /// here so scaling a texture-atlas tile never samples an adjacent tile.
1396
+ fn referenceImageSampleBounds(image: ReferenceImage, src: geometry.RectF) ReferenceImageSampleBounds {
1397
+ const image_max_x: i32 = @intCast(image.width - 1);
1398
+ const image_max_y: i32 = @intCast(image.height - 1);
1399
+ return .{
1400
+ .min_x = clampI32(referenceFloor(src.minX()), 0, image_max_x),
1401
+ .min_y = clampI32(referenceFloor(src.minY()), 0, image_max_y),
1402
+ .max_x = clampI32(referenceCeil(src.maxX()) - 1, 0, image_max_x),
1403
+ .max_y = clampI32(referenceCeil(src.maxY()) - 1, 0, image_max_y),
1379
1404
  };
1380
1405
  }
1381
1406
 
1382
- fn referenceSampleImageNearest(image: ReferenceImage, src: geometry.RectF, u: f32, v: f32) [4]u8 {
1407
+ fn referenceSampleImageNearest(image: ReferenceImage, src: geometry.RectF, bounds: ReferenceImageSampleBounds, u: f32, v: f32) [4]u8 {
1383
1408
  const sample_x_f = src.x + std.math.clamp(u, 0, 1) * src.width;
1384
1409
  const sample_y_f = src.y + std.math.clamp(v, 0, 1) * src.height;
1385
- const x = clampI32(referenceFloor(sample_x_f), 0, @intCast(image.width - 1));
1386
- const y = clampI32(referenceFloor(sample_y_f), 0, @intCast(image.height - 1));
1410
+ const x = clampI32(referenceFloor(sample_x_f), bounds.min_x, bounds.max_x);
1411
+ const y = clampI32(referenceFloor(sample_y_f), bounds.min_y, bounds.max_y);
1387
1412
  return referenceImagePixel(image, x, y);
1388
1413
  }
1389
1414
 
1390
- fn referenceSampleImageLinear(image: ReferenceImage, src: geometry.RectF, u: f32, v: f32) [4]u8 {
1415
+ fn referenceSampleImageLinear(image: ReferenceImage, src: geometry.RectF, bounds: ReferenceImageSampleBounds, u: f32, v: f32) [4]u8 {
1391
1416
  // Belt over the renderPass-level fill: direct sampler callers (unit
1392
1417
  // tests, future paths) stay correct. One predictable branch per
1393
1418
  // output pixel — noise next to the twelve pows the table replaces.
@@ -1396,10 +1421,10 @@ fn referenceSampleImageLinear(image: ReferenceImage, src: geometry.RectF, u: f32
1396
1421
  const sample_y_f = src.y + std.math.clamp(v, 0, 1) * src.height - 0.5;
1397
1422
  const x_floor = referenceFloor(sample_x_f);
1398
1423
  const y_floor = referenceFloor(sample_y_f);
1399
- const x0 = clampI32(x_floor, 0, @intCast(image.width - 1));
1400
- const y0 = clampI32(y_floor, 0, @intCast(image.height - 1));
1401
- const x1 = clampI32(x_floor + 1, 0, @intCast(image.width - 1));
1402
- const y1 = clampI32(y_floor + 1, 0, @intCast(image.height - 1));
1424
+ const x0 = clampI32(x_floor, bounds.min_x, bounds.max_x);
1425
+ const y0 = clampI32(y_floor, bounds.min_y, bounds.max_y);
1426
+ const x1 = clampI32(x_floor + 1, bounds.min_x, bounds.max_x);
1427
+ const y1 = clampI32(y_floor + 1, bounds.min_y, bounds.max_y);
1403
1428
  const tx = std.math.clamp(sample_x_f - @as(f32, @floatFromInt(x_floor)), 0, 1);
1404
1429
  const ty = std.math.clamp(sample_y_f - @as(f32, @floatFromInt(y_floor)), 0, 1);
1405
1430
 
@@ -1731,6 +1731,69 @@ test "reference renderer bilinear-filters scaled images" {
1731
1731
  try expectPixelRgba8(.{ 255, 255, 255, 255 }, surface, 3, 3);
1732
1732
  }
1733
1733
 
1734
+ test "reference renderer keeps linear atlas crops inside their source rectangle" {
1735
+ const commands = [_]CanvasCommand{.{ .draw_image = .{
1736
+ .id = 1,
1737
+ .image_id = 42,
1738
+ .src = geometry.RectF.init(1, 1, 2, 2),
1739
+ .dst = geometry.RectF.init(0, 0, 4, 4),
1740
+ } }};
1741
+
1742
+ // A green 2x2 tile surrounded by red atlas neighbors. Scaling the
1743
+ // crop 2x must stay green through its edge pixels; full-image clamp
1744
+ // would blend red into every side through the bilinear taps.
1745
+ var image_pixels: [4 * 4 * 4]u8 = undefined;
1746
+ for (0..16) |index| {
1747
+ image_pixels[index * 4 + 0] = 255;
1748
+ image_pixels[index * 4 + 1] = 0;
1749
+ image_pixels[index * 4 + 2] = 0;
1750
+ image_pixels[index * 4 + 3] = 255;
1751
+ }
1752
+ for (1..3) |y| {
1753
+ for (1..3) |x| {
1754
+ const index = (y * 4 + x) * 4;
1755
+ image_pixels[index + 0] = 0;
1756
+ image_pixels[index + 1] = 255;
1757
+ image_pixels[index + 2] = 0;
1758
+ }
1759
+ }
1760
+ const images = [_]ReferenceImage{.{
1761
+ .id = 42,
1762
+ .width = 4,
1763
+ .height = 4,
1764
+ .pixels = &image_pixels,
1765
+ }};
1766
+
1767
+ var render_commands: [1]RenderCommand = undefined;
1768
+ var render_batches: [1]RenderBatch = undefined;
1769
+ var resources: [1]RenderResource = undefined;
1770
+ var resource_cache_entries: [1]RenderResourceCacheEntry = undefined;
1771
+ var resource_cache_actions: [1]RenderResourceCacheAction = undefined;
1772
+ var glyphs: [0]GlyphAtlasEntry = .{};
1773
+ var changes: [0]DiffChange = .{};
1774
+ const frame = try (DisplayList{ .commands = &commands }).framePlan(null, .{
1775
+ .surface_size = geometry.SizeF.init(4, 4),
1776
+ }, .{
1777
+ .render_commands = &render_commands,
1778
+ .render_batches = &render_batches,
1779
+ .resources = &resources,
1780
+ .resource_cache_entries = &resource_cache_entries,
1781
+ .resource_cache_actions = &resource_cache_actions,
1782
+ .glyph_atlas_entries = &glyphs,
1783
+ .changes = &changes,
1784
+ });
1785
+
1786
+ var pixels: [4 * 4 * 4]u8 = undefined;
1787
+ const surface = (try ReferenceRenderSurface.init(4, 4, &pixels)).withImages(&images);
1788
+ try surface.renderPass(frame.renderPass(), Color.rgb8(0, 0, 0));
1789
+
1790
+ for (0..4) |y| {
1791
+ for (0..4) |x| {
1792
+ try expectPixelRgba8(.{ 0, 255, 0, 255 }, surface, x, y);
1793
+ }
1794
+ }
1795
+ }
1796
+
1734
1797
  test "reference renderer nearest-filters scaled images" {
1735
1798
  const commands = [_]CanvasCommand{.{ .draw_image = .{
1736
1799
  .id = 1,
@@ -612,7 +612,7 @@ test "render batch plan groups adjacent commands by pipeline and state" {
612
612
  try std.testing.expectEqual(@as(usize, 2), batch_plan.batches[1].command_start);
613
613
  try std.testing.expectEqual(RenderPipelineKind.glyph_run, batch_plan.batches[2].pipeline);
614
614
  try std.testing.expectEqual(@as(usize, 3), batch_plan.batches[2].command_start);
615
- // Text command bounds carry the ink allowance (right 0.35em,
615
+ // Text command bounds carry the ink allowance (top/right 0.35em,
616
616
  // bottom/left 0.1em) past the metric box.
617
617
  try expectRectApprox(geometry.RectF.init(0, 0, 87.684, 22.2), batch_plan.bounds);
618
618
  }
@@ -165,6 +165,7 @@ pub const textEllipsisAdvance = text_model.textEllipsisAdvance;
165
165
  pub const textLayoutKeysEqual = text_model.textLayoutKeysEqual;
166
166
  pub const TextLayoutOptions = text_model.TextLayoutOptions;
167
167
  pub const TextMeasureProvider = text_model.TextMeasureProvider;
168
+ pub const TextInkMetrics = text_model.TextInkMetrics;
168
169
  /// Batched-measurement invalidation seam (see text_measure_cache.zig):
169
170
  /// bump when anything that could change a provider's answers changes —
170
171
  /// font registration, appearance flips, runtime construction. Cached
@@ -512,6 +512,7 @@ fn packetDrawText(value: CanvasGpuText) text_model.DrawText {
512
512
  .color = value.color,
513
513
  .text = value.text,
514
514
  .glyphs = value.glyphs,
515
+ .measure = value.measure,
515
516
  .text_layout = value.text_layout,
516
517
  };
517
518
  }
@@ -336,6 +336,7 @@ pub const defaultFocusable = event_model.defaultFocusable;
336
336
  pub const textLineBounds = text_model.textLineBounds;
337
337
  pub const textBounds = text_model.textBounds;
338
338
  pub const TextMeasureProvider = text_model.TextMeasureProvider;
339
+ pub const TextInkMetrics = text_model.TextInkMetrics;
339
340
  pub const measureTextWidthForFont = text_model.measureTextWidthForFont;
340
341
  pub const measureTextAdvance = text_model.measureTextAdvance;
341
342
  pub const estimateTextWidth = text_model.estimateTextWidth;
@@ -59,6 +59,7 @@ pub const textLineRange = text_layout.textLineRange;
59
59
  pub const textLineCaretX = text_layout.textLineCaretX;
60
60
  pub const textLineBounds = text_layout.textLineBounds;
61
61
  pub const TextMeasureProvider = text_layout.TextMeasureProvider;
62
+ pub const TextInkMetrics = text_layout.TextInkMetrics;
62
63
  pub const measureTextWidthForFont = text_layout.measureTextWidthForFont;
63
64
  pub const measureTextAdvance = text_layout.measureTextAdvance;
64
65
  pub const estimateTextWidth = text_layout.estimateTextWidth;
@@ -36,14 +36,17 @@ pub const estimateTextWidthForFace = text_metrics.estimateTextWidthForFace;
36
36
  pub const estimateTextAdvanceForBytes = text_metrics.estimateTextAdvanceForBytes;
37
37
  pub const estimatedGlyphAdvance = text_metrics.estimatedGlyphAdvance;
38
38
  pub const TextMeasureProvider = text_metrics.TextMeasureProvider;
39
+ pub const TextInkMetrics = text_metrics.TextInkMetrics;
39
40
  pub const measureTextWidthForFont = text_metrics.measureTextWidthForFont;
40
41
  pub const measureTextAdvance = text_metrics.measureTextAdvance;
41
42
 
42
43
  /// The measurement provider a DrawText carries via its layout options, if
43
44
  /// any. Runs without layout options always measure with the estimator.
44
45
  fn drawTextMeasure(text: DrawText) ?*const text_metrics.TextMeasureProvider {
45
- const options = text.text_layout orelse return null;
46
- return options.measure;
46
+ if (text.text_layout) |options| {
47
+ if (options.measure) |measure| return measure;
48
+ }
49
+ return text.measure;
47
50
  }
48
51
 
49
52
  const textLayoutOptionsForDrawText = text_layout_hash.textLayoutOptionsForDrawText;
@@ -284,7 +287,7 @@ pub const TextLayoutPlanner = struct {
284
287
 
285
288
  fn consumeText(self: *TextLayoutPlanner, text: DrawText, options: TextLayoutOptions) Error!void {
286
289
  if (self.plan_len >= self.plans.len) return error.TextLayoutPlanListFull;
287
- const plan = try layoutTextRunPlan(text, textLayoutOptionsForDrawText(options, text), self.lines[self.line_len..]);
290
+ const plan = try layoutTextRunPlan(text, options, self.lines[self.line_len..]);
288
291
  self.plans[self.plan_len] = plan;
289
292
  self.plan_len += 1;
290
293
  self.line_len += plan.lineCount();
@@ -318,9 +321,10 @@ pub fn layoutTextRun(text: DrawText, options: TextLayoutOptions, output: []TextL
318
321
  }
319
322
 
320
323
  pub fn layoutTextRunPlan(text: DrawText, options: TextLayoutOptions, output: []TextLine) Error!TextLayoutPlan {
324
+ const effective_options = textLayoutOptionsForDrawText(options, text);
321
325
  var len: usize = 0;
322
326
  var bounds: ?geometry.RectF = null;
323
- var lines = TextLineIterator.init(text, options);
327
+ var lines = TextLineIterator.init(text, effective_options);
324
328
  while (lines.next()) |line| {
325
329
  if (len >= output.len) return error.TextLayoutLineListFull;
326
330
  output[len] = line;
@@ -328,7 +332,7 @@ pub fn layoutTextRunPlan(text: DrawText, options: TextLayoutOptions, output: []T
328
332
  bounds = unionOptionalBounds(bounds, line.bounds);
329
333
  }
330
334
  return .{
331
- .key = textLayoutKey(text, options),
335
+ .key = textLayoutKey(text, effective_options),
332
336
  .layout = .{ .lines = output[0..len], .bounds = bounds },
333
337
  };
334
338
  }
@@ -413,31 +417,96 @@ pub const TextLineIterator = struct {
413
417
  /// Conservative ink allowance around a text run's metric box. Command
414
418
  /// bounds must cover everything a command may ink (stroke bounds inflate
415
419
  /// by stroke width, blur bounds by the radius); text metrics only cover
416
- /// advances, and real glyph outlines overhang them: a wide glyph behind
420
+ /// advances, and real glyph outlines overhang them: fallback glyphs can
421
+ /// extend above the bundled face's cap-height anchor, a wide glyph behind
417
422
  /// the flat 0.65em multibyte estimate (arrows, dingbats) pokes up to
418
423
  /// ~0.3em past the last advance, descenders plus anti-aliasing spill a
419
424
  /// hair below the 0.25em descent box, and italic or negative-LSB glyphs
420
425
  /// lean slightly left of the pen. Renderers clip to command bounds, so a
421
- /// metric-tight box visibly shaved tail glyphs off reference screenshots.
426
+ /// metric-tight box visibly shaved fallback and tail glyphs off screenshots.
422
427
  fn textInkInsets(size: f32) geometry.InsetsF {
423
428
  const em = @max(0, size);
424
- return geometry.InsetsF.init(0, em * 0.35, em * 0.1, em * 0.1);
429
+ return geometry.InsetsF.init(em * 0.35, em * 0.35, em * 0.1, em * 0.1);
425
430
  }
426
431
 
427
432
  pub fn textBounds(value: DrawText) ?geometry.RectF {
428
- const metric = metricTextBounds(value) orelse return null;
429
- return metric.inflate(textInkInsets(value.size));
430
- }
431
-
432
- fn metricTextBounds(value: DrawText) ?geometry.RectF {
433
- if (value.glyphs.len == 0 and value.text.len == 0) return null;
434
433
  if (value.text_layout) |options| {
435
434
  var lines: [max_text_bounds_layout_lines]TextLine = undefined;
436
435
  if (layoutTextRun(value, options, &lines)) |layout| {
437
- if (layout.bounds) |bounds| return bounds;
436
+ return textBoundsForLines(value, layout.lines);
438
437
  } else |_| {}
438
+
439
+ // Bounds must remain truthful even when the fixed layout scratch
440
+ // cannot materialize every line. Stream the same iterator used by
441
+ // caret and selection queries; this keeps long wrapped paragraphs
442
+ // from falling back to a metric box that can clip host-shaped ink.
443
+ const effective_options = textLayoutOptionsForDrawText(options, value);
444
+ var iterator = TextLineIterator.init(value, effective_options);
445
+ var bounds: ?geometry.RectF = null;
446
+ while (iterator.next()) |line| {
447
+ bounds = unionOptionalBounds(bounds, textLineInkBounds(value, line));
448
+ }
449
+ if (bounds) |value_bounds| return value_bounds.inflate(textInkInsets(value.size));
439
450
  }
440
451
 
452
+ const metric = metricTextBounds(value) orelse return null;
453
+ var bounds = metric.inflate(textInkInsets(value.size));
454
+ if (value.text_layout == null and value.glyphs.len == 0) {
455
+ if (drawTextMeasure(value)) |provider| {
456
+ if (provider.measureInk(value.font_id, value.size, value.text)) |metrics| {
457
+ bounds = bounds.unionWith(geometry.RectF.init(
458
+ value.origin.x + metrics.min_x,
459
+ value.origin.y - metrics.max_y,
460
+ metrics.max_x - metrics.min_x,
461
+ metrics.max_y - metrics.min_y,
462
+ ));
463
+ }
464
+ }
465
+ }
466
+ return bounds;
467
+ }
468
+
469
+ fn textBoundsForLines(value: DrawText, lines: []const TextLine) ?geometry.RectF {
470
+ var bounds: ?geometry.RectF = null;
471
+ for (lines) |line| {
472
+ bounds = unionOptionalBounds(bounds, textLineInkBounds(value, line));
473
+ }
474
+ return if (bounds) |value_bounds| value_bounds.inflate(textInkInsets(value.size)) else null;
475
+ }
476
+
477
+ fn textLineInkBounds(value: DrawText, line: TextLine) geometry.RectF {
478
+ var bounds = line.bounds;
479
+ if (value.glyphs.len > 0) return bounds;
480
+ if (drawTextMeasure(value)) |provider| {
481
+ const start = @min(line.text_start, value.text.len);
482
+ const end = @min(value.text.len, start + line.paintedTextLen());
483
+ if (end > start) {
484
+ if (provider.measureInk(value.font_id, value.size, value.text[start..end])) |line_metrics| {
485
+ bounds = bounds.unionWith(geometry.RectF.init(
486
+ line.bounds.x + line_metrics.min_x,
487
+ line.baseline - line_metrics.max_y,
488
+ line_metrics.max_x - line_metrics.min_x,
489
+ line_metrics.max_y - line_metrics.min_y,
490
+ ));
491
+ }
492
+ }
493
+ if (line.hasEllipsis()) {
494
+ if (provider.measureInk(value.font_id, value.size, text_ellipsis)) |ellipsis_metrics| {
495
+ bounds = bounds.unionWith(geometry.RectF.init(
496
+ line.bounds.maxX() - line.ellipsis_advance + ellipsis_metrics.min_x,
497
+ line.baseline - ellipsis_metrics.max_y,
498
+ ellipsis_metrics.max_x - ellipsis_metrics.min_x,
499
+ ellipsis_metrics.max_y - ellipsis_metrics.min_y,
500
+ ));
501
+ }
502
+ }
503
+ }
504
+ return bounds;
505
+ }
506
+
507
+ fn metricTextBounds(value: DrawText) ?geometry.RectF {
508
+ if (value.glyphs.len == 0 and value.text.len == 0) return null;
509
+
441
510
  var min_x = value.origin.x;
442
511
  var min_y = value.origin.y - value.size;
443
512
  var max_x = value.origin.x;
@@ -21,7 +21,9 @@ const resourceHashF32 = hash_model.resourceHashF32;
21
21
  const resourceHashPoint = hash_model.resourceHashPoint;
22
22
 
23
23
  pub fn textLayoutOptionsForDrawText(frame_options: TextLayoutOptions, text: DrawText) TextLayoutOptions {
24
- return text.text_layout orelse frame_options;
24
+ var options = text.text_layout orelse frame_options;
25
+ if (options.measure == null) options.measure = text.measure;
26
+ return options;
25
27
  }
26
28
 
27
29
  pub fn textLayoutKey(text: DrawText, options: TextLayoutOptions) TextLayoutKey {
@@ -17,6 +17,9 @@ pub const DrawText = struct {
17
17
  color: Color,
18
18
  text: []const u8 = "",
19
19
  glyphs: []const Glyph = &.{},
20
+ /// Process-local measurement context used for host-aware ink bounds.
21
+ /// Excluded from equality, hashing, and serialization.
22
+ measure: ?*const text_metrics.TextMeasureProvider = null,
20
23
  text_layout: ?TextLayoutOptions = null,
21
24
  };
22
25
 
@@ -10,6 +10,23 @@ const default_sans_font_id = canvas.default_sans_font_id;
10
10
  const default_mono_font_id = canvas.default_mono_font_id;
11
11
  const utf8SequenceLength = text_interaction.utf8SequenceLength;
12
12
 
13
+ /// Ink bounds for one shaped line, relative to its baseline.
14
+ pub const TextInkMetrics = struct {
15
+ min_x: f32 = 0,
16
+ max_x: f32 = 0,
17
+ min_y: f32 = 0,
18
+ max_y: f32 = 0,
19
+
20
+ pub fn valid(self: TextInkMetrics) bool {
21
+ return std.math.isFinite(self.min_x) and
22
+ std.math.isFinite(self.max_x) and
23
+ std.math.isFinite(self.min_y) and
24
+ std.math.isFinite(self.max_y) and
25
+ self.max_x >= self.min_x and
26
+ self.max_y >= self.min_y;
27
+ }
28
+ };
29
+
13
30
  /// Injected text measurement. The engine stays pure: platforms provide a
14
31
  /// context pointer plus a function that returns the width of a single-line
15
32
  /// run of `text` at `size` for `font_id`, shaped with the same font
@@ -29,6 +46,9 @@ pub const TextMeasureProvider = struct {
29
46
  /// with line breaks then derived from cumulative advances exactly as
30
47
  /// the estimator path derives them.
31
48
  measure_advances_fn: ?*const fn (context: ?*anyopaque, font_id: FontId, size: f32, text: []const u8, advances: []f32) bool = null,
49
+ /// Optional shaped ink bounds for one line, relative to its baseline.
50
+ /// Returning false keeps callers on conservative engine bounds.
51
+ measure_ink_fn: ?*const fn (context: ?*anyopaque, font_id: FontId, size: f32, text: []const u8, metrics: *TextInkMetrics) bool = null,
32
52
 
33
53
  pub fn measureWidth(self: TextMeasureProvider, font_id: FontId, size: f32, text: []const u8) f32 {
34
54
  if (text.len == 0) return 0;
@@ -48,6 +68,14 @@ pub const TextMeasureProvider = struct {
48
68
  if (advances.len < text.len) return false;
49
69
  return advances_fn(self.context, font_id, size, text, advances[0..text.len]);
50
70
  }
71
+
72
+ pub fn measureInk(self: TextMeasureProvider, font_id: FontId, size: f32, text: []const u8) ?TextInkMetrics {
73
+ const measure_ink_fn = self.measure_ink_fn orelse return null;
74
+ if (text.len == 0) return null;
75
+ var metrics: TextInkMetrics = .{};
76
+ if (!measure_ink_fn(self.context, font_id, size, text, &metrics)) return null;
77
+ return if (metrics.valid()) metrics else null;
78
+ }
51
79
  };
52
80
 
53
81
  /// Width of a text run: the provider when installed, the deterministic
@@ -306,4 +334,3 @@ fn sansVariantWidthFactor(font_id: FontId) f32 {
306
334
  pub fn estimatedGlyphAdvance(glyph: Glyph, size: f32) f32 {
307
335
  return @max(size * 0.25, glyph.advance);
308
336
  }
309
-
@@ -837,9 +837,10 @@ test "ime composition owns exact bytes when its preview completes CRLF" {
837
837
  }
838
838
 
839
839
  test "text bounds follow utf8 scalar fallback and shaped y offsets" {
840
- // Metric boxes inflate by the ink allowance (left/bottom 0.1em,
841
- // right 0.35em) so real glyph outlines never clip at the bounds.
842
- try expectRectApprox(geometry.RectF.init(1, 8, 19.47, 13.5), textBounds(.{
840
+ // Metric boxes inflate by the ink allowance (top 0.35em,
841
+ // left/bottom 0.1em, right 0.35em) so real and fallback glyph
842
+ // outlines never clip at the bounds.
843
+ try expectRectApprox(geometry.RectF.init(1, 4.5, 19.47, 17), textBounds(.{
843
844
  .font_id = 1,
844
845
  .size = 10,
845
846
  .origin = geometry.PointF.init(2, 18),
@@ -851,7 +852,7 @@ test "text bounds follow utf8 scalar fallback and shaped y offsets" {
851
852
  .{ .id = 1, .x = 0, .y = -2, .advance = 6 },
852
853
  .{ .id = 2, .x = 8, .y = 3, .advance = 5 },
853
854
  };
854
- try expectRect(geometry.RectF.init(3, 8, 17.5, 18.5), textBounds(.{
855
+ try expectRect(geometry.RectF.init(3, 4.5, 17.5, 22), textBounds(.{
855
856
  .font_id = 1,
856
857
  .size = 10,
857
858
  .origin = geometry.PointF.init(4, 20),
@@ -860,6 +861,60 @@ test "text bounds follow utf8 scalar fallback and shaped y offsets" {
860
861
  }));
861
862
  }
862
863
 
864
+ test "text bounds reserve top ink headroom for fallback glyphs" {
865
+ const text = DrawText{
866
+ .font_id = 1,
867
+ .size = 16,
868
+ .origin = geometry.PointF.init(4, 20),
869
+ .color = Color.rgb8(255, 255, 255),
870
+ .text = "😀",
871
+ };
872
+ const bounds = textBounds(text).?;
873
+ try std.testing.expect(bounds.y < text.origin.y - text.size);
874
+ try std.testing.expectEqual(@as(f32, 5.6), text.origin.y - text.size - bounds.y);
875
+ }
876
+
877
+ fn providerInkMetricsForTests(
878
+ context: ?*anyopaque,
879
+ font_id: FontId,
880
+ size: f32,
881
+ text: []const u8,
882
+ metrics: *support.TextInkMetrics,
883
+ ) bool {
884
+ _ = context;
885
+ _ = font_id;
886
+ _ = size;
887
+ _ = text;
888
+ metrics.* = .{
889
+ .min_x = -2,
890
+ .max_x = 12,
891
+ .min_y = -4,
892
+ .max_y = 18,
893
+ };
894
+ return true;
895
+ }
896
+
897
+ const provider_ink_measure = support.TextMeasureProvider{
898
+ .measure_fn = coretextLikeMeasureForTests,
899
+ .measure_ink_fn = providerInkMetricsForTests,
900
+ };
901
+
902
+ test "text bounds include provider ink metrics for standalone runs" {
903
+ const text = DrawText{
904
+ .font_id = 1,
905
+ .size = 10,
906
+ .origin = geometry.PointF.init(4, 20),
907
+ .color = Color.rgb8(255, 255, 255),
908
+ .text = "A",
909
+ .measure = &provider_ink_measure,
910
+ };
911
+ const bounds = textBounds(text).?;
912
+ try std.testing.expect(bounds.x <= 2);
913
+ try std.testing.expect(bounds.y <= 2);
914
+ try std.testing.expect(bounds.maxX() >= 16);
915
+ try std.testing.expect(bounds.maxY() >= 24);
916
+ }
917
+
863
918
  test "text bounds and reference renderer honor per-run wrapping" {
864
919
  const text = DrawText{
865
920
  .font_id = 1,
@@ -870,13 +925,13 @@ test "text bounds and reference renderer honor per-run wrapping" {
870
925
  .text_layout = .{ .max_width = 10, .line_height = 12, .wrap = .character },
871
926
  };
872
927
  // Metric box (0, 0, 7.11, 48) plus the ink allowance.
873
- try expectRectApprox(geometry.RectF.init(-1, 0, 11.61, 49), textBounds(text));
928
+ try expectRectApprox(geometry.RectF.init(-1, -3.5, 11.61, 52.5), textBounds(text));
874
929
 
875
930
  const commands = [_]CanvasCommand{.{ .draw_text = text }};
876
931
  var render_commands: [1]RenderCommand = undefined;
877
932
  const render_plan = try (DisplayList{ .commands = &commands }).renderPlan(&render_commands);
878
933
  try std.testing.expectEqual(@as(usize, 1), render_plan.commandCount());
879
- try expectRectApprox(geometry.RectF.init(-1, 0, 11.61, 49), render_plan.commands[0].bounds);
934
+ try expectRectApprox(geometry.RectF.init(-1, -3.5, 11.61, 52.5), render_plan.commands[0].bounds);
880
935
 
881
936
  var pixels: [16 * 32 * 4]u8 = [_]u8{0} ** (16 * 32 * 4);
882
937
  const surface = try ReferenceRenderSurface.init(16, 32, &pixels);
@@ -579,6 +579,14 @@ pub fn Ui(comptime Msg: type) type {
579
579
  /// shape, one bit of the id space apart (see
580
580
  /// `canvas.media_surface_image_id_bit`).
581
581
  image: canvas.ImageId = 0,
582
+ /// Optional source rectangle in image pixel coordinates for
583
+ /// image-bearing widgets. Null draws the whole registered
584
+ /// image; a rectangle draws only that sub-region, clipped to
585
+ /// the registered image bounds — the texture-atlas path.
586
+ /// Crops use nearest sampling so filtering cannot bleed an
587
+ /// adjacent atlas region. The destination remains the
588
+ /// widget's resolved frame.
589
+ image_src: ?geometry.RectF = null,
582
590
  /// Vector icon name drawn inside icon-bearing controls
583
591
  /// (`button`, `toggle_button`, `icon_button`, `list_item`,
584
592
  /// `menu_item`): a built-in registry name
@@ -3759,6 +3767,7 @@ pub fn Ui(comptime Msg: type) type {
3759
3767
  .autofocus = options.autofocus,
3760
3768
  .submit_on_enter = options.submit_on_enter,
3761
3769
  .image_id = options.image,
3770
+ .image_src = options.image_src,
3762
3771
  .value = options.value,
3763
3772
  .value_x = options.value_x,
3764
3773
  .tree_level = options.tree_level,
@@ -1269,6 +1269,14 @@ pub const tooltip_delay_dependent_attr_message = "tooltip-delay needs anchor on
1269
1269
 
1270
1270
  pub const image_binding_message = "image takes one {binding} to a u64 ImageId the app registered at runtime (Cmd.imageLoad, fx.loadImage, fx.registerImageBytes) - runtime image ids are model data, not markup literals; 0 renders nothing (an avatar falls back to its initials)";
1271
1271
  pub const image_binding_element_message = "image is only supported on avatar and image - the remaining image-bearing widget (icon-button) stays a Zig view (ElementOptions.image)";
1272
+ pub const image_source_element_message = "source-x, source-y, source-width, and source-height are only supported on avatar and image - they crop a runtime-registered image in decoded-image pixel coordinates";
1273
+ pub const image_source_binding_message = "source-x, source-y, source-width, and source-height require the element's image binding - without a registered image the source rectangle is inert";
1274
+ pub const image_source_complete_message = "source-x, source-y, source-width, and source-height must be declared together - they form one source rectangle in decoded-image pixel coordinates";
1275
+ pub const image_source_attr_names = [_][]const u8{ "source-x", "source-y", "source-width", "source-height" };
1276
+
1277
+ pub fn imageSourceAttrName(name: []const u8) bool {
1278
+ return nameInList(name, &image_source_attr_names);
1279
+ }
1272
1280
  pub const image_missing_image_message = "image requires image={binding} naming the u64 ImageId the app registered at runtime - without one the leaf can never draw anything (dead markup, same policy as icon without name)";
1273
1281
  pub const image_children_message = "image is a leaf - it takes no children";
1274
1282
 
@@ -3353,6 +3361,27 @@ fn validateNode(document: MarkupDocument, node: MarkupNode, parent_element: ?[]c
3353
3361
  }
3354
3362
  continue;
3355
3363
  }
3364
+ if (imageSourceAttrName(attribute.name)) {
3365
+ // A registered-image crop is one atomic rectangle in
3366
+ // decoded-image pixel coordinates. Partial declarations
3367
+ // and crops without an image would otherwise become
3368
+ // silently inert data.
3369
+ if (!std.mem.eql(u8, node.name, "avatar") and !std.mem.eql(u8, node.name, "image")) {
3370
+ return attrError(node, attribute, image_source_element_message);
3371
+ }
3372
+ if (node.attr("image") == null) {
3373
+ return attrError(node, attribute, image_source_binding_message);
3374
+ }
3375
+ for (image_source_attr_names) |name| {
3376
+ if (node.attr(name) == null) {
3377
+ return attrError(node, attribute, image_source_complete_message);
3378
+ }
3379
+ }
3380
+ if (attrExpressionError(attribute.value, invalid_expression_message)) |message| {
3381
+ return attrError(node, attribute, message);
3382
+ }
3383
+ continue;
3384
+ }
3356
3385
  if (std.mem.eql(u8, attribute.name, "surface")) {
3357
3386
  // The media-surface producer rendezvous, media-surface
3358
3387
  // scoped: surface ids are model data a producer