@native-sdk/cli 0.5.2 → 0.5.3

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 (82) hide show
  1. package/build.zig +41 -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/events.ts +39 -2
  6. package/packages/core/src/checker.ts +1 -1
  7. package/packages/core/src/emitter.ts +39 -3
  8. package/packages/core/src/infer.ts +53 -7
  9. package/skill-data/automation/SKILL.md +7 -4
  10. package/skill-data/core/references/app-model-runtime.md +3 -0
  11. package/skill-data/native-ui/SKILL.md +1 -0
  12. package/src/app_runner/root.zig +25 -0
  13. package/src/automation/protocol.zig +20 -1
  14. package/src/embed/host.zig +27 -0
  15. package/src/embed/tests.zig +69 -0
  16. package/src/embed/ui_host.zig +4 -0
  17. package/src/platform/macos/appkit_host.h +11 -0
  18. package/src/platform/macos/appkit_host.m +130 -0
  19. package/src/platform/macos/root.zig +34 -0
  20. package/src/platform/null_platform_tests.zig +39 -0
  21. package/src/platform/root.zig +3 -0
  22. package/src/platform/types.zig +109 -4
  23. package/src/primitives/canvas/chart.zig +7 -7
  24. package/src/primitives/canvas/chart_tests.zig +66 -0
  25. package/src/primitives/canvas/commands.zig +48 -0
  26. package/src/primitives/canvas/events.zig +161 -0
  27. package/src/primitives/canvas/font_ttf.zig +155 -8
  28. package/src/primitives/canvas/font_ttf_tests.zig +686 -0
  29. package/src/primitives/canvas/reference.zig +67 -8
  30. package/src/primitives/canvas/render_fingerprints.zig +8 -0
  31. package/src/primitives/canvas/root.zig +25 -0
  32. package/src/primitives/canvas/ui.zig +35 -2
  33. package/src/primitives/canvas/ui_markup.zig +24 -0
  34. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  35. package/src/primitives/canvas/ui_markup_compiled.zig +40 -3
  36. package/src/primitives/canvas/ui_markup_compiled_tests.zig +33 -0
  37. package/src/primitives/canvas/ui_markup_contract.zig +9 -0
  38. package/src/primitives/canvas/ui_markup_contract_tests.zig +24 -0
  39. package/src/primitives/canvas/ui_markup_tests.zig +26 -0
  40. package/src/primitives/canvas/ui_markup_view.zig +53 -4
  41. package/src/primitives/canvas/ui_markup_view_tests.zig +73 -2
  42. package/src/primitives/canvas/ui_schema.zig +16 -0
  43. package/src/primitives/canvas/ui_schema_tests.zig +10 -11
  44. package/src/primitives/canvas/ui_tests.zig +31 -0
  45. package/src/primitives/canvas/vector.zig +258 -141
  46. package/src/primitives/canvas/widget_access.zig +1 -1
  47. package/src/primitives/canvas/widget_builtin_tests.zig +129 -0
  48. package/src/primitives/canvas/widget_layout.zig +9 -4
  49. package/src/primitives/canvas/widget_render.zig +78 -40
  50. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  51. package/src/primitives/canvas/widget_semantics.zig +6 -1
  52. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  53. package/src/primitives/canvas/widget_text_input.zig +118 -10
  54. package/src/primitives/canvas/widgets.zig +16 -0
  55. package/src/root.zig +5 -0
  56. package/src/runtime/api.zig +13 -0
  57. package/src/runtime/automation_commands.zig +33 -0
  58. package/src/runtime/automation_widget_dispatch.zig +64 -0
  59. package/src/runtime/canvas_font_tests.zig +272 -3
  60. package/src/runtime/canvas_fonts.zig +60 -20
  61. package/src/runtime/canvas_frame_helpers.zig +15 -0
  62. package/src/runtime/canvas_image_tests.zig +26 -0
  63. package/src/runtime/canvas_images.zig +33 -7
  64. package/src/runtime/canvas_limits.zig +44 -8
  65. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  66. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  67. package/src/runtime/canvas_widget_events.zig +24 -4
  68. package/src/runtime/canvas_widget_runtime.zig +24 -7
  69. package/src/runtime/canvas_widget_state.zig +6 -1
  70. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  71. package/src/runtime/core.zig +98 -6
  72. package/src/runtime/flow.zig +27 -0
  73. package/src/runtime/gpu_surface_events.zig +7 -0
  74. package/src/runtime/media_surface.zig +726 -0
  75. package/src/runtime/media_surface_tests.zig +1476 -0
  76. package/src/runtime/root.zig +9 -0
  77. package/src/runtime/session_journal.zig +30 -3
  78. package/src/runtime/session_tests.zig +126 -4
  79. package/src/runtime/tests.zig +1 -0
  80. package/src/runtime/ts_ui_app.zig +56 -0
  81. package/src/runtime/ui_app.zig +58 -0
  82. package/src/runtime/ui_app_tests.zig +334 -0
@@ -2,6 +2,11 @@
2
2
  //! bundled Geist Regular face: table parsing, cmap lookup, advances,
3
3
  //! and glyph outlines (simple and composite) rasterized through the
4
4
  //! vector core with pixel goldens in the reference conventions.
5
+ //!
6
+ //! The synthetic-font fixtures at the bottom build TrueType bytes
7
+ //! in-test (dense CJK-shaped glyphs, composite chains, over- and
8
+ //! under-declaring `maxp` tables) to prove both sides of the
9
+ //! registration-time glyph-budget gate without shipping font binaries.
5
10
 
6
11
  const std = @import("std");
7
12
  const geometry = @import("geometry");
@@ -266,6 +271,687 @@ test "out of range glyph ids error instead of reading wild" {
266
271
  try std.testing.expectError(error.FontParseFailed, face.glyphOutline(60000, Affine.identity(), &builder));
267
272
  }
268
273
 
274
+ // --------------------------------------------------------------------
275
+ // Synthetic fonts: in-test TrueType bytes with caller-controlled `maxp`
276
+ // declarations and glyph payloads. Just enough of the format for the
277
+ // parser's seven required tables; hostile fidelity (checksums, search
278
+ // ranges) is deliberately absent because the parser never reads it.
279
+
280
+ /// Fixed-buffer big-endian byte builder for synthetic tables.
281
+ const ByteBuilder = struct {
282
+ bytes: [32768]u8 = undefined,
283
+ len: usize = 0,
284
+
285
+ fn appendU8(self: *ByteBuilder, value: u8) void {
286
+ self.bytes[self.len] = value;
287
+ self.len += 1;
288
+ }
289
+
290
+ fn appendU16(self: *ByteBuilder, value: u16) void {
291
+ self.appendU8(@intCast(value >> 8));
292
+ self.appendU8(@intCast(value & 0xFF));
293
+ }
294
+
295
+ fn appendI16(self: *ByteBuilder, value: i16) void {
296
+ self.appendU16(@bitCast(value));
297
+ }
298
+
299
+ fn appendU32(self: *ByteBuilder, value: u32) void {
300
+ self.appendU16(@intCast(value >> 16));
301
+ self.appendU16(@intCast(value & 0xFFFF));
302
+ }
303
+
304
+ fn appendZeros(self: *ByteBuilder, count: usize) void {
305
+ @memset(self.bytes[self.len .. self.len + count], 0);
306
+ self.len += count;
307
+ }
308
+
309
+ fn slice(self: *const ByteBuilder) []const u8 {
310
+ return self.bytes[0..self.len];
311
+ }
312
+ };
313
+
314
+ /// Append a simple glyph of `contour_count` closed square rings, each
315
+ /// with `points_per_contour` on-curve points (must be divisible by 4)
316
+ /// walking the ring's perimeter, rings spread along x. All flags are
317
+ /// plain on-curve with 16-bit deltas, so the outline emitted for ring
318
+ /// points p0..pn is exactly moveTo(p0), lineTo(p1..pn), close.
319
+ fn appendSimpleGlyph(glyf: *ByteBuilder, contour_count: u16, points_per_contour: u16) void {
320
+ std.debug.assert(points_per_contour % 4 == 0);
321
+ const side = points_per_contour / 4;
322
+ glyf.appendI16(@intCast(contour_count));
323
+ glyf.appendZeros(8); // bbox: unread by the parser
324
+ var contour: u16 = 0;
325
+ while (contour < contour_count) : (contour += 1) {
326
+ glyf.appendU16((contour + 1) * points_per_contour - 1);
327
+ }
328
+ glyf.appendU16(0); // no instructions
329
+ const total = @as(usize, contour_count) * points_per_contour;
330
+ var index: usize = 0;
331
+ while (index < total) : (index += 1) glyf.appendU8(0x01); // on-curve, long deltas
332
+ // Coordinates: point k of contour c walks a `side`-unit square at
333
+ // x offset c * (side + 4).
334
+ const point = struct {
335
+ fn at(c: u16, k: u16, s: u16) [2]i16 {
336
+ const quarter = k / s;
337
+ const step: i16 = @intCast(k % s);
338
+ const size: i16 = @intCast(s);
339
+ const base: i16 = @intCast(@as(u32, c) * (@as(u32, s) + 4));
340
+ return switch (quarter) {
341
+ 0 => .{ base + step, 0 },
342
+ 1 => .{ base + size, step },
343
+ 2 => .{ base + size - step, size },
344
+ else => .{ base, size - step },
345
+ };
346
+ }
347
+ }.at;
348
+ // X deltas then Y deltas, accumulated across the whole glyph.
349
+ var previous: i16 = 0;
350
+ contour = 0;
351
+ while (contour < contour_count) : (contour += 1) {
352
+ var k: u16 = 0;
353
+ while (k < points_per_contour) : (k += 1) {
354
+ const p = point(contour, k, side);
355
+ glyf.appendI16(p[0] - previous);
356
+ previous = p[0];
357
+ }
358
+ }
359
+ previous = 0;
360
+ contour = 0;
361
+ while (contour < contour_count) : (contour += 1) {
362
+ var k: u16 = 0;
363
+ while (k < points_per_contour) : (k += 1) {
364
+ const p = point(contour, k, side);
365
+ glyf.appendI16(p[1] - previous);
366
+ previous = p[1];
367
+ }
368
+ }
369
+ }
370
+
371
+ const Component = struct { glyph: u16, dx: i16, dy: i16 };
372
+
373
+ /// Append a composite glyph placing each component by XY offset.
374
+ fn appendCompositeGlyph(glyf: *ByteBuilder, components: []const Component) void {
375
+ glyf.appendI16(-1);
376
+ glyf.appendZeros(8); // bbox: unread by the parser
377
+ for (components, 0..) |component, index| {
378
+ const more: u16 = if (index + 1 < components.len) 0x0020 else 0;
379
+ glyf.appendU16(0x0001 | 0x0002 | more); // words + xy offsets
380
+ glyf.appendU16(component.glyph);
381
+ glyf.appendI16(component.dx);
382
+ glyf.appendI16(component.dy);
383
+ }
384
+ }
385
+
386
+ /// Append a composite glyph placed by POINT MATCHING (the placement
387
+ /// form the renderer refuses per glyph and `maxp` cannot describe).
388
+ fn appendPointMatchedComposite(glyf: *ByteBuilder, child: u16) void {
389
+ glyf.appendI16(-1);
390
+ glyf.appendZeros(8);
391
+ glyf.appendU16(0x0001); // words, but NOT xy offsets: point numbers
392
+ glyf.appendU16(child);
393
+ glyf.appendI16(0);
394
+ glyf.appendI16(0);
395
+ }
396
+
397
+ const DeclaredMaxima = struct {
398
+ points: u16,
399
+ contours: u16,
400
+ composite_points: u16 = 0,
401
+ composite_contours: u16 = 0,
402
+ component_elements: u16 = 0,
403
+ component_depth: u16 = 0,
404
+ maxp_version: u32 = 0x00010000,
405
+ };
406
+
407
+ /// Assemble a whole font: the seven required tables around `glyf_bytes`
408
+ /// whose per-glyph offsets are `loca_offsets` (one per glyph plus the
409
+ /// final end; glyph 0 stays empty when its two offsets are equal). The
410
+ /// cmap maps 'A' + n to glyph 1 + n.
411
+ fn buildSyntheticFont(declared: DeclaredMaxima, glyf_bytes: []const u8, loca_offsets: []const u32) ByteBuilder {
412
+ const num_glyphs: u16 = @intCast(loca_offsets.len - 1);
413
+ var font = ByteBuilder{};
414
+ font.appendU32(0x00010000);
415
+ font.appendU16(7);
416
+ font.appendZeros(6); // searchRange/entrySelector/rangeShift: unread
417
+ const off_head: u32 = 12 + 7 * 16;
418
+ const off_maxp = off_head + 54;
419
+ const off_hhea = off_maxp + 32;
420
+ const off_hmtx = off_hhea + 36;
421
+ const off_cmap = off_hmtx + 4 * @as(u32, num_glyphs);
422
+ const off_loca = off_cmap + 44;
423
+ const off_glyf = off_loca + 4 * (@as(u32, num_glyphs) + 1);
424
+ const record = struct {
425
+ fn append(builder: *ByteBuilder, tag: *const [4]u8, offset: u32, length: u32) void {
426
+ for (tag) |byte| builder.appendU8(byte);
427
+ builder.appendU32(0); // checksum: unread
428
+ builder.appendU32(offset);
429
+ builder.appendU32(length);
430
+ }
431
+ }.append;
432
+ record(&font, "head", off_head, 54);
433
+ record(&font, "maxp", off_maxp, 32);
434
+ record(&font, "hhea", off_hhea, 36);
435
+ record(&font, "hmtx", off_hmtx, 4 * @as(u32, num_glyphs));
436
+ record(&font, "cmap", off_cmap, 44);
437
+ record(&font, "loca", off_loca, 4 * (@as(u32, num_glyphs) + 1));
438
+ record(&font, "glyf", off_glyf, @intCast(glyf_bytes.len));
439
+
440
+ // head: unitsPerEm at +18, indexToLocFormat at +50 (long loca).
441
+ std.debug.assert(font.len == off_head);
442
+ font.appendZeros(18);
443
+ font.appendU16(1000);
444
+ font.appendZeros(30);
445
+ font.appendI16(1);
446
+ font.appendI16(0);
447
+
448
+ // maxp v1.0 with the declared maxima under test.
449
+ std.debug.assert(font.len == off_maxp);
450
+ font.appendU32(declared.maxp_version);
451
+ font.appendU16(num_glyphs);
452
+ font.appendU16(declared.points);
453
+ font.appendU16(declared.contours);
454
+ font.appendU16(declared.composite_points);
455
+ font.appendU16(declared.composite_contours);
456
+ font.appendZeros(14); // zones..instruction sizes
457
+ font.appendU16(declared.component_elements);
458
+ font.appendU16(declared.component_depth);
459
+
460
+ // hhea: ascender +4, descender +6, numberOfHMetrics +34.
461
+ std.debug.assert(font.len == off_hhea);
462
+ font.appendZeros(4);
463
+ font.appendI16(800);
464
+ font.appendI16(-200);
465
+ font.appendZeros(26);
466
+ font.appendU16(num_glyphs);
467
+
468
+ // hmtx: one long metric per glyph.
469
+ std.debug.assert(font.len == off_hmtx);
470
+ var glyph: u16 = 0;
471
+ while (glyph < num_glyphs) : (glyph += 1) {
472
+ font.appendU16(500);
473
+ font.appendI16(0);
474
+ }
475
+
476
+ // cmap: one format-4 subtable, 'A' + n -> glyph 1 + n.
477
+ std.debug.assert(font.len == off_cmap);
478
+ font.appendU16(0);
479
+ font.appendU16(1);
480
+ font.appendU16(3);
481
+ font.appendU16(1);
482
+ font.appendU32(12);
483
+ font.appendU16(4); // format
484
+ font.appendU16(32); // subtable length
485
+ font.appendU16(0); // language
486
+ font.appendU16(4); // segCountX2
487
+ font.appendZeros(6); // searchRange/entrySelector/rangeShift: unread
488
+ font.appendU16(0x41 + num_glyphs - 2); // endCode[0]
489
+ font.appendU16(0xFFFF); // endCode[1]
490
+ font.appendU16(0); // reservedPad
491
+ font.appendU16(0x41); // startCode[0]
492
+ font.appendU16(0xFFFF); // startCode[1]
493
+ font.appendU16(@as(u16, 1) -% @as(u16, 0x41)); // idDelta[0]: 'A' -> glyph 1
494
+ font.appendU16(1); // idDelta[1]: 0xFFFF -> glyph 0
495
+ font.appendU16(0); // idRangeOffset[0]
496
+ font.appendU16(0); // idRangeOffset[1]
497
+
498
+ // loca (long form).
499
+ std.debug.assert(font.len == off_loca);
500
+ for (loca_offsets) |offset| font.appendU32(offset);
501
+
502
+ std.debug.assert(font.len == off_glyf);
503
+ @memcpy(font.bytes[font.len .. font.len + glyf_bytes.len], glyf_bytes);
504
+ font.len += glyf_bytes.len;
505
+ return font;
506
+ }
507
+
508
+ test "synthetic dense glyphs beyond the old Latin-sized budgets parse and outline correctly" {
509
+ // CJK-shaped density, truthfully declared: 84 rings x 12 points
510
+ // (1008 points — the measured Noto Sans JP contour high-water is 84,
511
+ // and dense kanji far exceed the old 128-point budget) plus a
512
+ // single-contour glyph at exactly the point budget.
513
+ var glyf = ByteBuilder{};
514
+ var loca: [4]u32 = undefined;
515
+ loca[0] = 0;
516
+ loca[1] = 0; // glyph 0: empty
517
+ appendSimpleGlyph(&glyf, 84, 12);
518
+ loca[2] = @intCast(glyf.len);
519
+ appendSimpleGlyph(&glyf, 1, @intCast(font_ttf.max_glyph_points));
520
+ loca[3] = @intCast(glyf.len);
521
+ const font = buildSyntheticFont(
522
+ .{ .points = font_ttf.max_glyph_points, .contours = 84 },
523
+ glyf.slice(),
524
+ &loca,
525
+ );
526
+
527
+ const face = try font_ttf.Face.parse(font.slice());
528
+ try std.testing.expectEqual(@as(u16, 3), face.num_glyphs);
529
+ try std.testing.expectEqual(@as(u16, 1), face.glyphIndex('A'));
530
+ try std.testing.expectEqual(@as(u16, 2), face.glyphIndex('B'));
531
+
532
+ // The dense glyph outlines completely: every on-curve ring is
533
+ // moveTo + 12 lineTo (the walk returns to the start point) + close,
534
+ // so 84 * 14 elements, starting at the first ring's origin.
535
+ var builder = vector.PathBuilder(2048){};
536
+ try face.glyphOutline(1, Affine.identity(), &builder);
537
+ try std.testing.expectEqual(@as(usize, 84 * 14), builder.slice().len);
538
+ const first = builder.slice()[0];
539
+ try std.testing.expectEqual(drawing.PathVerb.move_to, first.verb);
540
+ try std.testing.expectEqual(@as(f32, 0), first.points[0].x);
541
+ try std.testing.expectEqual(@as(f32, 0), first.points[0].y);
542
+
543
+ // A glyph at exactly the point budget parses and outlines too.
544
+ builder.reset();
545
+ try face.glyphOutline(2, Affine.identity(), &builder);
546
+ try std.testing.expectEqual(@as(usize, font_ttf.max_glyph_points + 2), builder.slice().len);
547
+
548
+ // And the dense outline rasterizes through the vector core within
549
+ // the edge budget.
550
+ var grid = Grid{};
551
+ const scale: f32 = 16.0 / face.units_per_em;
552
+ const transform = Affine{ .a = scale, .b = 0, .c = 0, .d = -scale, .tx = 2, .ty = 20 };
553
+ builder.reset();
554
+ try face.glyphOutline(1, transform, &builder);
555
+ try vector.fillPath(builder.slice(), Affine.identity(), .nonzero, vector.default_tolerance, fullClip(), &grid);
556
+ }
557
+
558
+ test "synthetic composites at the depth and component budgets parse and outline" {
559
+ // Glyph 1: a simple ring. Glyphs 2..5: a composite chain nested to
560
+ // exactly `max_composite_depth`. Glyph 6: one composite carrying
561
+ // exactly `max_composite_components` offset copies of the ring.
562
+ var glyf = ByteBuilder{};
563
+ var loca: [8]u32 = undefined;
564
+ loca[0] = 0;
565
+ loca[1] = 0;
566
+ appendSimpleGlyph(&glyf, 1, 4);
567
+ loca[2] = @intCast(glyf.len);
568
+ appendCompositeGlyph(&glyf, &.{.{ .glyph = 1, .dx = 10, .dy = 0 }});
569
+ loca[3] = @intCast(glyf.len);
570
+ appendCompositeGlyph(&glyf, &.{.{ .glyph = 2, .dx = 10, .dy = 0 }});
571
+ loca[4] = @intCast(glyf.len);
572
+ appendCompositeGlyph(&glyf, &.{.{ .glyph = 3, .dx = 10, .dy = 0 }});
573
+ loca[5] = @intCast(glyf.len);
574
+ appendCompositeGlyph(&glyf, &.{.{ .glyph = 4, .dx = 10, .dy = 0 }});
575
+ loca[6] = @intCast(glyf.len);
576
+ var components: [font_ttf.max_composite_components]Component = undefined;
577
+ for (&components, 0..) |*component, index| {
578
+ component.* = .{ .glyph = 1, .dx = @intCast(index * 8), .dy = 0 };
579
+ }
580
+ appendCompositeGlyph(&glyf, &components);
581
+ loca[7] = @intCast(glyf.len);
582
+ const font = buildSyntheticFont(.{
583
+ .points = 4,
584
+ .contours = 1,
585
+ .composite_points = 8 * 4, // the component fan, flattened
586
+ .composite_contours = 8,
587
+ .component_elements = font_ttf.max_composite_components,
588
+ .component_depth = font_ttf.max_composite_depth,
589
+ }, glyf.slice(), &loca);
590
+
591
+ const face = try font_ttf.Face.parse(font.slice());
592
+
593
+ // The chain bottoms out at the simple ring (moveTo + 4 lineTo +
594
+ // close), its XY offsets summed.
595
+ var builder = vector.PathBuilder(64){};
596
+ try face.glyphOutline(5, Affine.identity(), &builder);
597
+ try std.testing.expectEqual(@as(usize, 6), builder.slice().len);
598
+ try std.testing.expectEqual(@as(f32, 40), builder.slice()[0].points[0].x);
599
+
600
+ // The full component fan emits one ring per component.
601
+ builder.reset();
602
+ try face.glyphOutline(6, Affine.identity(), &builder);
603
+ try std.testing.expectEqual(@as(usize, 6 * font_ttf.max_composite_components), builder.slice().len);
604
+ }
605
+
606
+ test "a composite flattening to more points than any simple glyph renders within the derived path capacity" {
607
+ // The failure the composite gate exists to prevent: a face whose
608
+ // simple glyphs are modest but whose composites flatten far denser.
609
+ // Glyph 1: a 128-point ring (the face's densest simple glyph).
610
+ // Glyph 2: a composite fanning 8 offset copies of it — flattened
611
+ // 1024 points / 8 contours, exactly `max_composite_points`, eight
612
+ // times denser than any simple glyph in the face.
613
+ var glyf = ByteBuilder{};
614
+ var loca: [4]u32 = undefined;
615
+ loca[0] = 0;
616
+ loca[1] = 0;
617
+ appendSimpleGlyph(&glyf, 1, 128);
618
+ loca[2] = @intCast(glyf.len);
619
+ var components: [font_ttf.max_composite_components]Component = undefined;
620
+ for (&components, 0..) |*component, index| {
621
+ component.* = .{ .glyph = 1, .dx = @intCast(index * 40), .dy = 0 };
622
+ }
623
+ appendCompositeGlyph(&glyf, &components);
624
+ loca[3] = @intCast(glyf.len);
625
+ const font = buildSyntheticFont(.{
626
+ .points = 128,
627
+ .contours = 1,
628
+ .composite_points = @intCast(font_ttf.max_composite_points),
629
+ .composite_contours = 8,
630
+ .component_elements = font_ttf.max_composite_components,
631
+ .component_depth = 1,
632
+ }, glyf.slice(), &loca);
633
+
634
+ // Truthful at-budget composite maxima pass the gate...
635
+ const face = try font_ttf.Face.parse(font.slice());
636
+
637
+ // ...and the flattened outline fits a builder sized exactly like
638
+ // the reference renderer's glyph path capacity: points + 3*contours
639
+ // taken over max(simple budgets, composite budgets). Each ring
640
+ // emits moveTo + 128 lineTo + close, so the fan is 8 * 130 = 1040
641
+ // elements — past what the face's simple maxima alone could emit
642
+ // (130 + 3), inside the composite-aware bound.
643
+ const capacity = @max(
644
+ font_ttf.max_glyph_points + 3 * font_ttf.max_glyph_contours,
645
+ font_ttf.max_composite_points + 3 * font_ttf.max_composite_contours,
646
+ );
647
+ var builder = vector.PathBuilder(capacity){};
648
+ try face.glyphOutline(2, Affine.identity(), &builder);
649
+ try std.testing.expectEqual(@as(usize, 8 * 130), builder.slice().len);
650
+ try std.testing.expect(builder.slice().len > 128 + 3 * 1);
651
+
652
+ // The same fan rasterizes through the vector core.
653
+ var grid = Grid{};
654
+ const scale: f32 = 16.0 / face.units_per_em;
655
+ var raster_builder = vector.PathBuilder(capacity){};
656
+ try face.glyphOutline(2, .{ .a = scale, .b = 0, .c = 0, .d = -scale, .tx = 2, .ty = 20 }, &raster_builder);
657
+ try vector.fillPath(raster_builder.slice(), Affine.identity(), .nonzero, vector.default_tolerance, fullClip(), &grid);
658
+ try std.testing.expect(grid.inkCount() > 0);
659
+ }
660
+
661
+ test "a face declaring maxima beyond the budgets is refused at parse with a teaching" {
662
+ var glyf = ByteBuilder{};
663
+ var loca: [3]u32 = undefined;
664
+ loca[0] = 0;
665
+ loca[1] = 0;
666
+ appendSimpleGlyph(&glyf, 1, 4);
667
+ loca[2] = @intCast(glyf.len);
668
+
669
+ const over_budget = [_]DeclaredMaxima{
670
+ .{ .points = font_ttf.max_glyph_points + 1, .contours = 1 },
671
+ .{ .points = 4, .contours = font_ttf.max_glyph_contours + 1 },
672
+ .{ .points = 4, .contours = 1, .composite_points = font_ttf.max_composite_points + 1 },
673
+ .{ .points = 4, .contours = 1, .composite_contours = font_ttf.max_composite_contours + 1 },
674
+ .{ .points = 4, .contours = 1, .component_elements = font_ttf.max_composite_components + 1 },
675
+ .{ .points = 4, .contours = 1, .component_depth = font_ttf.max_composite_depth + 1 },
676
+ };
677
+ for (over_budget) |declared| {
678
+ const font = buildSyntheticFont(declared, glyf.slice(), &loca);
679
+ try std.testing.expectError(error.FontGlyphTooComplex, font_ttf.Face.parse(font.slice()));
680
+ // The teaching machinery names the refusal — simple AND
681
+ // flattened-composite budgets by name — and the maxima stay
682
+ // readable for callers that format the face's numbers.
683
+ const reason = font_ttf.parseFailureReason(font.slice()).?;
684
+ try std.testing.expect(std.mem.indexOf(u8, reason, "budgets") != null);
685
+ try std.testing.expect(std.mem.indexOf(u8, reason, "flattened composite") != null);
686
+ const maxima = font_ttf.declaredGlyphMaxima(font.slice()).?;
687
+ try std.testing.expect(!maxima.withinBudgets());
688
+ try std.testing.expectEqual(declared.composite_points, maxima.composite_points);
689
+ try std.testing.expectEqual(declared.composite_contours, maxima.composite_contours);
690
+ }
691
+
692
+ // The same font declared truthfully parses: the gate reads maxima,
693
+ // not vibes.
694
+ const honest = buildSyntheticFont(.{ .points = 4, .contours = 1 }, glyf.slice(), &loca);
695
+ _ = try font_ttf.Face.parse(honest.slice());
696
+ try std.testing.expectEqual(@as(?[]const u8, null), font_ttf.parseFailureReason(honest.slice()));
697
+
698
+ // A maxp that is not the version-1.0 glyf form is a parse failure
699
+ // with its own teaching.
700
+ const wrong_version = buildSyntheticFont(.{ .points = 4, .contours = 1, .maxp_version = 0x00005000 }, glyf.slice(), &loca);
701
+ try std.testing.expectError(error.FontParseFailed, font_ttf.Face.parse(wrong_version.slice()));
702
+ const version_reason = font_ttf.parseFailureReason(wrong_version.slice()).?;
703
+ try std.testing.expect(std.mem.indexOf(u8, version_reason, "maxp") != null);
704
+ }
705
+
706
+ test "a face that under-declares its maxp hits the per-glyph backstops, never wild reads" {
707
+ // Glyph 1 really carries more contours than the budget; glyph 2 is
708
+ // a composite chain one level past the depth budget; glyph 3 places
709
+ // its component by point matching. maxp declares none of it.
710
+ var glyf = ByteBuilder{};
711
+ var loca: [10]u32 = undefined;
712
+ loca[0] = 0;
713
+ loca[1] = 0;
714
+ appendSimpleGlyph(&glyf, @intCast(font_ttf.max_glyph_contours + 1), 4);
715
+ loca[2] = @intCast(glyf.len);
716
+ appendSimpleGlyph(&glyf, 1, 4);
717
+ loca[3] = @intCast(glyf.len);
718
+ // Five nested composites: the deepest simple child sits one level
719
+ // past `max_composite_depth`.
720
+ appendCompositeGlyph(&glyf, &.{.{ .glyph = 2, .dx = 0, .dy = 0 }});
721
+ loca[4] = @intCast(glyf.len);
722
+ appendCompositeGlyph(&glyf, &.{.{ .glyph = 3, .dx = 0, .dy = 0 }});
723
+ loca[5] = @intCast(glyf.len);
724
+ appendCompositeGlyph(&glyf, &.{.{ .glyph = 4, .dx = 0, .dy = 0 }});
725
+ loca[6] = @intCast(glyf.len);
726
+ appendCompositeGlyph(&glyf, &.{.{ .glyph = 5, .dx = 0, .dy = 0 }});
727
+ loca[7] = @intCast(glyf.len);
728
+ appendCompositeGlyph(&glyf, &.{.{ .glyph = 6, .dx = 0, .dy = 0 }});
729
+ loca[8] = @intCast(glyf.len);
730
+ appendPointMatchedComposite(&glyf, 2);
731
+ loca[9] = @intCast(glyf.len);
732
+ const font = buildSyntheticFont(.{ .points = 4, .contours = 1, .component_depth = 1, .component_elements = 1 }, glyf.slice(), &loca);
733
+
734
+ // The lie passes the gate (declared maxima are tiny)...
735
+ const face = try font_ttf.Face.parse(font.slice());
736
+
737
+ // ...and the per-glyph backstops catch the reality, recoverably.
738
+ var builder = vector.PathBuilder(2048){};
739
+ try std.testing.expectError(error.FontGlyphTooComplex, face.glyphOutline(1, Affine.identity(), &builder));
740
+ builder.reset();
741
+ try std.testing.expectError(error.FontGlyphTooComplex, face.glyphOutline(7, Affine.identity(), &builder));
742
+ builder.reset();
743
+ try std.testing.expectError(error.FontGlyphTooComplex, face.glyphOutline(8, Affine.identity(), &builder));
744
+ }
745
+
746
+ test "declaredGlyphMaxima reads the bundled faces' true maxima" {
747
+ // Pins the measured ground truth the budget constants document.
748
+ const regular = font_ttf.declaredGlyphMaxima(font_ttf.geist_regular_bytes).?;
749
+ try std.testing.expectEqual(@as(u16, 96), regular.points);
750
+ try std.testing.expectEqual(@as(u16, 16), regular.contours);
751
+ try std.testing.expectEqual(@as(u16, 89), regular.composite_points);
752
+ try std.testing.expectEqual(@as(u16, 5), regular.composite_contours);
753
+ try std.testing.expectEqual(@as(u16, 4), regular.component_elements);
754
+ try std.testing.expectEqual(@as(u16, 3), regular.component_depth);
755
+ try std.testing.expect(regular.withinBudgets());
756
+
757
+ const mono = font_ttf.declaredGlyphMaxima(font_ttf.geist_mono_bytes).?;
758
+ try std.testing.expectEqual(@as(u16, 116), mono.points);
759
+ try std.testing.expectEqual(@as(u16, 16), mono.contours);
760
+ try std.testing.expectEqual(@as(u16, 104), mono.composite_points);
761
+ try std.testing.expectEqual(@as(u16, 10), mono.composite_contours);
762
+ try std.testing.expectEqual(@as(u16, 3), mono.component_elements);
763
+ try std.testing.expectEqual(@as(u16, 1), mono.component_depth);
764
+ try std.testing.expect(mono.withinBudgets());
765
+
766
+ // Not-a-font blobs answer null instead of erroring.
767
+ try std.testing.expectEqual(@as(?font_ttf.GlyphMaxima, null), font_ttf.declaredGlyphMaxima("not a font"));
768
+ }
769
+
770
+ // --------------------------------------------------------------------
771
+ // Glyph raster budgets: the registration gate admits faces up to the
772
+ // outline budgets, and the vector core's glyph-fill budgets are DERIVED
773
+ // from them (vector.zig's glyph budget block), so a gate-admitted glyph
774
+ // must rasterize — never `VectorPathTooComplex`, never a block fallback.
775
+ // The zigzag fixtures below are the truthful adversarial maxima.
776
+
777
+ /// Append a single-contour zigzag glyph: `point_count` points marching x
778
+ /// one font unit per point, y alternating 0 and `height`. With
779
+ /// `on_curve` every point is an on-curve corner — ~one scanline crossing
780
+ /// per point, the crossing-budget worst case. Without it every point is
781
+ /// an off-curve control (TrueType implies on-curve midpoints), so the
782
+ /// contour is one full-height quad per point — the flattening
783
+ /// worst case: at large sizes every quad clamps at
784
+ /// `vector.max_glyph_curve_segments`, which is the edge-budget maximum.
785
+ fn appendZigzagGlyph(glyf: *ByteBuilder, point_count: u16, height: i16, on_curve: bool) void {
786
+ glyf.appendI16(1); // one contour
787
+ glyf.appendZeros(8); // bbox: unread by the parser
788
+ glyf.appendU16(point_count - 1);
789
+ glyf.appendU16(0); // no instructions
790
+ var index: usize = 0;
791
+ while (index < point_count) : (index += 1) glyf.appendU8(if (on_curve) 0x01 else 0x00);
792
+ // X deltas: start at 0, then +1 unit per point.
793
+ glyf.appendI16(0);
794
+ index = 1;
795
+ while (index < point_count) : (index += 1) glyf.appendI16(1);
796
+ // Y deltas: alternate 0 -> height -> 0 -> ...
797
+ glyf.appendI16(0);
798
+ index = 1;
799
+ while (index < point_count) : (index += 1) glyf.appendI16(if (index % 2 == 1) height else -height);
800
+ }
801
+
802
+ /// The truthful budget-maximal zigzag face: glyph 1 ('A') the on-curve
803
+ /// line zigzag, glyph 2 ('B') the all-off-curve quad zigzag, both at
804
+ /// exactly `max_glyph_points` in one contour, `maxp` declaring the
805
+ /// truth — the registration gate admits this face.
806
+ fn buildZigzagFont() ByteBuilder {
807
+ var glyf = ByteBuilder{};
808
+ var loca: [4]u32 = undefined;
809
+ loca[0] = 0;
810
+ loca[1] = 0; // glyph 0: empty
811
+ appendZigzagGlyph(&glyf, @intCast(font_ttf.max_glyph_points), 1000, true);
812
+ loca[2] = @intCast(glyf.len);
813
+ appendZigzagGlyph(&glyf, @intCast(font_ttf.max_glyph_points), 1000, false);
814
+ loca[3] = @intCast(glyf.len);
815
+ return buildSyntheticFont(
816
+ .{ .points = @intCast(font_ttf.max_glyph_points), .contours = 1 },
817
+ glyf.slice(),
818
+ &loca,
819
+ );
820
+ }
821
+
822
+ test "glyph raster budgets stay lockstep with the outline budgets they derive from" {
823
+ // The derivation chain in vector.zig, pinned against the source
824
+ // constants (vector.zig cannot import font_ttf.zig — the import
825
+ // runs the other way): quads <= points + contours over both glyph
826
+ // forms the gate admits, edges <= quads * clamp + close edges,
827
+ // crossings <= edges (an edge crosses a scanline at most once).
828
+ const quads = @max(
829
+ font_ttf.max_glyph_points + font_ttf.max_glyph_contours,
830
+ font_ttf.max_composite_points + font_ttf.max_composite_contours,
831
+ );
832
+ const closes = @max(font_ttf.max_glyph_contours, font_ttf.max_composite_contours);
833
+ try std.testing.expectEqual(quads * vector.max_glyph_curve_segments + closes, vector.max_glyph_fill_edges);
834
+ try std.testing.expectEqual(vector.max_glyph_fill_edges, vector.max_glyph_scanline_crossings);
835
+ }
836
+
837
+ test "truthful budget-maximal zigzag glyphs pass the gate and rasterize at the max raster size" {
838
+ const font = buildZigzagFont();
839
+ // The registration gate (`registerCanvasFont` routes every face
840
+ // through this parse) ADMITS the truthful declaration...
841
+ const face = try font_ttf.Face.parse(font.slice());
842
+
843
+ // ...so both zigzags must rasterize at the largest sweep the core
844
+ // serves (`max_raster_width` = an 8192-px em; the clamp makes the
845
+ // budgets size-independent above it). The band clip sits at ~30% of
846
+ // the em, where the line zigzag crosses one edge per point (~1024
847
+ // crossings — the old shared 256-crossing cap refused this) and the
848
+ // quad zigzag's ~1025 full-height quads each flatten to the 16-
849
+ // segment clamp (~16,400 edges — the old shared 4096-edge cap
850
+ // refused that).
851
+ const InkCounter = struct {
852
+ covered: usize = 0,
853
+ pub fn pixel(self: *@This(), x: i32, y: i32, coverage: f32) void {
854
+ _ = x;
855
+ _ = y;
856
+ if (coverage > 0) self.covered += 1;
857
+ }
858
+ };
859
+ const size: f32 = @floatFromInt(vector.max_raster_width);
860
+ const scale = size / face.units_per_em;
861
+ const raster = try std.testing.allocator.create(vector.GlyphRasterizer);
862
+ defer std.testing.allocator.destroy(raster);
863
+ const band_y0: i32 = @intFromFloat(size - 700 * scale);
864
+ for ([_]u21{ 'A', 'B' }) |codepoint| {
865
+ const glyph = face.glyphIndex(codepoint);
866
+ try std.testing.expect(glyph != 0);
867
+ var builder = vector.PathBuilder(font_ttf.max_glyph_points + 3 * font_ttf.max_glyph_contours){};
868
+ try face.glyphOutline(glyph, .{ .a = scale, .b = 0, .c = 0, .d = -scale, .tx = 0, .ty = size }, &builder);
869
+ var counter = InkCounter{};
870
+ try vector.fillGlyphPath(
871
+ raster,
872
+ builder.slice(),
873
+ Affine.identity(),
874
+ .nonzero,
875
+ vector.default_tolerance,
876
+ .{ .x0 = 0, .y0 = band_y0, .x1 = @intCast(vector.max_raster_width), .y1 = band_y0 + 32 },
877
+ &counter,
878
+ );
879
+ try std.testing.expect(counter.covered > 0);
880
+ }
881
+ }
882
+
883
+ test "a gate-admitted zigzag face inks outlines on both reference text paths, never the block fallback" {
884
+ // End to end through the reference renderer at the max raster size,
885
+ // on BOTH `drawTextLine` branches: the per-cluster pen walk (plain
886
+ // text) and the shaped-glyph branch (the glyph array the atlas plan
887
+ // keys glyphs by). The block fallback is a solid cell; the zigzag
888
+ // outline alternates inked slivers with background every few
889
+ // pixels, so a mid-glyph row must show many ink transitions —
890
+ // a solid run (block) shows at most two.
891
+ const reference = @import("reference.zig");
892
+ const render_model = @import("render.zig");
893
+ const frame_model = @import("frame.zig");
894
+ const text_model = @import("text.zig");
895
+
896
+ const font = buildZigzagFont();
897
+ const face = try font_ttf.Face.parse(font.slice());
898
+
899
+ const width: usize = vector.max_raster_width;
900
+ const height: usize = 32;
901
+ const size: f32 = @floatFromInt(vector.max_raster_width);
902
+ // Put the surface band at ~30% of the em: baseline sits below the
903
+ // surface, the zigzag's strokes cross every visible row.
904
+ const baseline: f32 = @as(f32, @floatFromInt(height / 2)) + 0.3 * size;
905
+
906
+ const pixels = try std.testing.allocator.alloc(u8, width * height * 4);
907
+ defer std.testing.allocator.free(pixels);
908
+
909
+ const fonts = [_]reference.ReferenceFont{.{ .id = 64, .face = &face }};
910
+ const shaped = [_]text_model.Glyph{.{ .id = 1, .x = 0, .y = 0, .text_start = 0, .text_len = 1 }};
911
+ const bounds = geometry.RectF.init(0, 0, @floatFromInt(width), @floatFromInt(height));
912
+
913
+ for ([_]bool{ false, true }) |use_shaped_glyphs| {
914
+ const glyph_slice: []const text_model.Glyph = if (use_shaped_glyphs) &shaped else &.{};
915
+ const commands = [_]render_model.RenderCommand{.{
916
+ .command = .{ .draw_text = .{
917
+ .id = 1,
918
+ .font_id = 64,
919
+ .size = size,
920
+ .origin = geometry.PointF.init(0, baseline),
921
+ .color = drawing.Color.rgb8(255, 255, 255),
922
+ .text = "A",
923
+ .glyphs = glyph_slice,
924
+ } },
925
+ .local_bounds = bounds,
926
+ .bounds = bounds,
927
+ }};
928
+ const surface = (try reference.ReferenceRenderSurface.init(width, height, pixels)).withFonts(&fonts);
929
+ try surface.renderPass(frame_model.CanvasRenderPass{
930
+ .surface_size = geometry.SizeF.init(@floatFromInt(width), @floatFromInt(height)),
931
+ .full_repaint = true,
932
+ .commands = &commands,
933
+ }, drawing.Color.rgba8(0, 0, 0, 0));
934
+
935
+ // Mid-band row: count inked pixels and ink<->background
936
+ // transitions. The outline's ~8-px slivers produce hundreds of
937
+ // transitions; the block fallback would be one solid run.
938
+ const row = height / 2;
939
+ var inked: usize = 0;
940
+ var transitions: usize = 0;
941
+ var previous_inked = false;
942
+ var x: usize = 0;
943
+ while (x < width) : (x += 1) {
944
+ const alpha = pixels[(row * width + x) * 4 + 3];
945
+ const is_inked = alpha > 0;
946
+ if (is_inked) inked += 1;
947
+ if (is_inked != previous_inked) transitions += 1;
948
+ previous_inked = is_inked;
949
+ }
950
+ try std.testing.expect(inked > 0);
951
+ try std.testing.expect(transitions > 100);
952
+ }
953
+ }
954
+
269
955
  test "font_coverage answers identically to the face's cmap" {
270
956
  // The std-only coverage module (markup tofu guard) re-reads the
271
957
  // same embedded bytes with its own minimal cmap walk; it must never