@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
@@ -119,6 +119,108 @@ pub fn widgetKeyboardNewlineTextEditEvent(kind: WidgetKind, event: WidgetKeyboar
119
119
  return .{ .insert_text = "\n" };
120
120
  }
121
121
 
122
+ /// The single-line text-entry kinds: their value can never hold a line
123
+ /// break (Enter submits instead of editing — see
124
+ /// `widgetKeyboardNewlineTextEditEvent`), so text inserted into them
125
+ /// sanitizes through `sanitizedSingleLineTextInputEvent`. The textarea is
126
+ /// the one genuinely multi-line editable kind and stays out.
127
+ pub fn widgetKindSingleLineTextEntry(kind: WidgetKind) bool {
128
+ return kind == .input or kind == .text_field or kind == .search_field or kind == .combobox;
129
+ }
130
+
131
+ /// Sanitized-edit scratch: the rewritten insert bytes live here until the
132
+ /// next edit that needs rewriting. Sound for the same reason the runtime's
133
+ /// paste buffer is: the event loop is single-threaded, at most one
134
+ /// insert-bearing edit is derived per dispatched input, and every consumer
135
+ /// (retained editor apply, the app's `on_input` Msg, model mirrors) reads
136
+ /// the stamped bytes synchronously within that dispatch. Sized to the
137
+ /// runtime's per-view widget-text budget
138
+ /// (`max_canvas_widget_text_bytes_per_view`), the largest insert the
139
+ /// editor could accept anyway.
140
+ const max_sanitized_text_edit_bytes: usize = 65536;
141
+ const SanitizedTextEditScratch = struct {
142
+ bytes: [max_sanitized_text_edit_bytes]u8,
143
+ };
144
+ const sanitized_text_edit_scratch = @import("lazy_tls.zig").LazyTls(SanitizedTextEditScratch);
145
+
146
+ fn textContainsLineBreakByte(text: []const u8) bool {
147
+ // Raw byte scan is UTF-8 safe: 0x0A/0x0D never appear inside a
148
+ // multibyte sequence.
149
+ return std.mem.indexOfAny(u8, text, "\r\n") != null;
150
+ }
151
+
152
+ /// The ONE sanitization rule for text entering a single-line field, at
153
+ /// the edit-derivation seam every insertion source flows through
154
+ /// (clipboard paste — shortcut and context menu —, typed/automation
155
+ /// `text_input`, IME composition, and the app-side fallback derivation):
156
+ ///
157
+ /// line breaks are STRIPPED from inserted text — U+000A and U+000D
158
+ /// removed outright, lines joined with nothing between them.
159
+ ///
160
+ /// This is the HTML value sanitization algorithm for single-line inputs
161
+ /// ("Strip newlines from the value",
162
+ /// https://html.spec.whatwg.org/multipage/input.html), which is also what
163
+ /// Chromium does when pasting multi-line text into an `<input>` — the
164
+ /// dominant convention. (WebKit historically substituted spaces; there is
165
+ /// no spec for the paste path itself, so the value-sanitization rule
166
+ /// wins.)
167
+ ///
168
+ /// Contracts, in declaration order:
169
+ /// - multi-line kinds (textarea) and non-insert edits pass through
170
+ /// untouched;
171
+ /// - an insert that strips to NOTHING is suppressed (null): pasting
172
+ /// bare newlines inserts nothing and never eats a live selection,
173
+ /// and an Enter whose host stuffed "\r"/"\n" into the key event
174
+ /// stays not-an-insert;
175
+ /// - a composition update strips the same way but an EMPTY result is
176
+ /// kept (an empty preview is meaningful — it clears the previous
177
+ /// one), with the preview cursor shifted left past the removed
178
+ /// bytes, so the IME COMMIT (which lands whatever the preview
179
+ /// holds) can never commit a line break into a single-line field;
180
+ /// - an insert too large for the scratch passes through untouched —
181
+ /// the editor apply rejects over-budget inserts loudly anyway.
182
+ ///
183
+ /// Deterministic derivation: the session journal records the RAW
184
+ /// platform event; replaying it re-derives the identical sanitized edit
185
+ /// here, so recorded multi-line pastes replay byte-identically.
186
+ pub fn sanitizedSingleLineTextInputEvent(kind: WidgetKind, event: TextInputEvent) ?TextInputEvent {
187
+ if (!widgetKindSingleLineTextEntry(kind)) return event;
188
+ switch (event) {
189
+ .insert_text => |text| {
190
+ if (!textContainsLineBreakByte(text)) return event;
191
+ if (text.len > max_sanitized_text_edit_bytes) return event;
192
+ const stripped = stripLineBreakBytes(text, &sanitized_text_edit_scratch.get().bytes);
193
+ if (stripped.len == 0) return null;
194
+ return .{ .insert_text = stripped };
195
+ },
196
+ .set_composition => |composition| {
197
+ if (!textContainsLineBreakByte(composition.text)) return event;
198
+ if (composition.text.len > max_sanitized_text_edit_bytes) return event;
199
+ const cursor = @min(composition.cursor orelse composition.text.len, composition.text.len);
200
+ var stripped_cursor: usize = cursor;
201
+ for (composition.text[0..cursor]) |byte| {
202
+ if (byte == '\n' or byte == '\r') stripped_cursor -= 1;
203
+ }
204
+ const stripped = stripLineBreakBytes(composition.text, &sanitized_text_edit_scratch.get().bytes);
205
+ return .{ .set_composition = .{
206
+ .text = stripped,
207
+ .cursor = if (composition.cursor == null and stripped_cursor == stripped.len) null else stripped_cursor,
208
+ } };
209
+ },
210
+ else => return event,
211
+ }
212
+ }
213
+
214
+ fn stripLineBreakBytes(text: []const u8, buffer: []u8) []const u8 {
215
+ var len: usize = 0;
216
+ for (text) |byte| {
217
+ if (byte == '\n' or byte == '\r') continue;
218
+ buffer[len] = byte;
219
+ len += 1;
220
+ }
221
+ return buffer[0..len];
222
+ }
223
+
122
224
  /// The clipboard intent of a key event: cmd+C/X/V on macOS, ctrl+C/X/V
123
225
  /// elsewhere (`hasCommandModifier` covers both). Shift/alt variants are
124
226
  /// deliberately excluded so shift+ctrl+V-style paste-special chords stay
@@ -694,3 +796,62 @@ pub fn defaultFocusable(widget: Widget) bool {
694
796
  else => false,
695
797
  };
696
798
  }
799
+
800
+ test "sanitizedSingleLineTextInputEvent strips line breaks per the HTML value-sanitization rule" {
801
+ const testing = std.testing;
802
+ // Interior LF, CR, and CRLF all strip outright — lines join with
803
+ // nothing between them (the Chromium <input> paste behavior).
804
+ const pasted = sanitizedSingleLineTextInputEvent(.input, .{ .insert_text = "alpha\nbeta\r\ngamma\r" }).?;
805
+ try testing.expectEqualStrings("alphabetagamma", pasted.insert_text);
806
+
807
+ // Every single-line kind sanitizes; the textarea keeps its breaks.
808
+ for ([_]WidgetKind{ .input, .text_field, .search_field, .combobox }) |kind| {
809
+ const stripped = sanitizedSingleLineTextInputEvent(kind, .{ .insert_text = "a\nb" }).?;
810
+ try testing.expectEqualStrings("ab", stripped.insert_text);
811
+ }
812
+ const textarea = sanitizedSingleLineTextInputEvent(.textarea, .{ .insert_text = "a\nb" }).?;
813
+ try testing.expectEqualStrings("a\nb", textarea.insert_text);
814
+
815
+ // Break-free inserts pass through as the SAME slice (zero copy), and
816
+ // the deliberately-empty insert (cut's delete-selection) survives.
817
+ const clean: TextInputEvent = .{ .insert_text = "plain" };
818
+ try testing.expectEqual(clean.insert_text.ptr, sanitizedSingleLineTextInputEvent(.input, clean).?.insert_text.ptr);
819
+ const cut = sanitizedSingleLineTextInputEvent(.input, .{ .insert_text = "" }).?;
820
+ try testing.expectEqualStrings("", cut.insert_text);
821
+
822
+ // An insert that is ONLY line breaks suppresses: pasting bare
823
+ // newlines inserts nothing, and an Enter whose host stuffed "\r"
824
+ // into the key event stays not-an-insert.
825
+ try testing.expect(sanitizedSingleLineTextInputEvent(.input, .{ .insert_text = "\r\n\n" }) == null);
826
+
827
+ // Non-insert edits pass through untouched.
828
+ const moved = sanitizedSingleLineTextInputEvent(.input, .{ .move_caret = .{ .direction = .end } }).?;
829
+ try testing.expect(moved.move_caret.direction == .end);
830
+ }
831
+
832
+ test "sanitizedSingleLineTextInputEvent strips composition text and shifts the preview cursor" {
833
+ const testing = std.testing;
834
+ // "ab\ncd" with the cursor after "cd" (offset 5): the stripped
835
+ // preview is "abcd" with the cursor at 4.
836
+ const preview = sanitizedSingleLineTextInputEvent(.combobox, .{ .set_composition = .{ .text = "ab\ncd", .cursor = 5 } }).?;
837
+ try testing.expectEqualStrings("abcd", preview.set_composition.text);
838
+ try testing.expectEqual(@as(usize, 4), preview.set_composition.cursor.?);
839
+
840
+ // A cursor BEFORE the break does not shift.
841
+ const early = sanitizedSingleLineTextInputEvent(.search_field, .{ .set_composition = .{ .text = "ab\ncd", .cursor = 2 } }).?;
842
+ try testing.expectEqual(@as(usize, 2), early.set_composition.cursor.?);
843
+
844
+ // A null cursor (end-of-preview) stays null.
845
+ const tail = sanitizedSingleLineTextInputEvent(.input, .{ .set_composition = .{ .text = "a\r\nb" } }).?;
846
+ try testing.expectEqualStrings("ab", tail.set_composition.text);
847
+ try testing.expect(tail.set_composition.cursor == null);
848
+
849
+ // An all-breaks preview is KEPT as the empty preview (it clears the
850
+ // previous one) rather than suppressed.
851
+ const cleared = sanitizedSingleLineTextInputEvent(.input, .{ .set_composition = .{ .text = "\n" } }).?;
852
+ try testing.expectEqualStrings("", cleared.set_composition.text);
853
+
854
+ // A textarea preview keeps its newline.
855
+ const multi = sanitizedSingleLineTextInputEvent(.textarea, .{ .set_composition = .{ .text = "a\nb" } }).?;
856
+ try testing.expectEqualStrings("a\nb", multi.set_composition.text);
857
+ }
@@ -9,10 +9,17 @@
9
9
  //! shaping, no CFF. The deterministic estimator (`text_metrics.zig`)
10
10
  //! derives its advance table from this face's `cmap`/`hmtx`, so layout
11
11
  //! measures with exactly the advances these outlines are inked at.
12
- //! All parsing is bounds-checked against fixed budgets sized from
13
- //! the bundled face's `maxp` (96 points / 16 contours per simple glyph,
14
- //! composites well under both), and any glyph beyond the budget fails
15
- //! with a recoverable error so callers can fall back to block glyphs.
12
+ //! All parsing is bounds-checked against fixed budgets sized from real
13
+ //! production faces' `maxp` tables — CJK included, dense kanji are the
14
+ //! high-water mark (see the budget constants below for the measured
15
+ //! values). `Face.parse` rejects any face whose `maxp` declares maxima
16
+ //! beyond the budgets, so validation stays loud and registration-time:
17
+ //! a face that parses renders every glyph it maps, never a block
18
+ //! fallback. The per-glyph budget checks in the outline walk remain as
19
+ //! defensive backstops for faces whose `maxp` under-declares (hostile
20
+ //! or malformed data), plus one placement form `maxp` cannot describe:
21
+ //! composites positioned by point matching (no real face measured uses
22
+ //! it) still fail per-glyph with a recoverable error.
16
23
  //!
17
24
  //! The bundled face is embedded at comptime and its table directory is
18
25
  //! validated at comptime: a corrupt bundle is a compile error, and the
@@ -32,8 +39,46 @@ pub const Error = error{
32
39
  FontGlyphTooComplex,
33
40
  } || @import("vector.zig").Error;
34
41
 
35
- pub const max_glyph_points: usize = 128;
36
- pub const max_glyph_contours: usize = 24;
42
+ /// Outline budgets, sized from measured `maxp` maxima of real production
43
+ /// faces (Google Fonts TrueType builds, July 2026) so dense CJK renders
44
+ /// as outlines, never block fallbacks:
45
+ ///
46
+ /// face maxPoints maxContours compPts compCtrs compDepth compElems
47
+ /// Geist Regular (bundled) 96 16 89 5 3 4
48
+ /// Geist Mono (bundled) 116 16 104 10 1 3
49
+ /// Noto Sans JP 520 84 0 0 0 0
50
+ /// Noto Sans SC 584 84 0 0 0 0
51
+ /// Noto Sans TC 685 87 0 0 0 0
52
+ /// Noto Sans KR 479 61 0 0 0 0
53
+ /// Noto Serif JP 465 40 0 0 0 0
54
+ /// Yuji Mai (brush kanji) 738 22 198 5 1 3
55
+ ///
56
+ /// 1024 points / 128 contours cover the densest measured glyph (Yuji
57
+ /// Mai's 738-point brush kanji; Noto Sans TC's 685) with ~1.4x headroom;
58
+ /// the depth/element budgets carry 1.3-2x over the deepest measured
59
+ /// use (the bundled Geist's own accent stacking).
60
+ ///
61
+ /// The composite budgets bound a composite glyph's FLATTENED outline
62
+ /// (`maxp.maxCompositePoints`/`maxCompositeContours`: totals across the
63
+ /// whole component tree). They equal the simple budgets deliberately: a
64
+ /// composite flattens through the same path sink and rides the same
65
+ /// per-glyph path capacity as a simple glyph (the reference renderer
66
+ /// sizes its builder as max over both forms), so "densest renderable
67
+ /// glyph" is one answer, not two. Nested components could otherwise
68
+ /// multiply — depth 4 of 8 elements is 8^4 leaves, far past any
69
+ /// capacity — which is why these are gated at parse like the simple
70
+ /// maxima, not left to render-time refusal. Measured worst composite:
71
+ /// 198 points (Yuji Mai), 10 contours (Geist Mono) — 5-12x headroom.
72
+ ///
73
+ /// Stack shape: the simple-glyph parse buffers
74
+ /// (`flags`/`xs`/`ys`/`end_points`) total ~9.5 KiB and live in exactly
75
+ /// ONE frame at a time — simple glyphs are leaves, so composite
76
+ /// recursion stacks only the small component-walk frames (depth <= 4),
77
+ /// never these arrays.
78
+ pub const max_glyph_points: usize = 1024;
79
+ pub const max_glyph_contours: usize = 128;
80
+ pub const max_composite_points: usize = max_glyph_points;
81
+ pub const max_composite_contours: usize = max_glyph_contours;
37
82
  pub const max_composite_depth: usize = 4;
38
83
  pub const max_composite_components: usize = 8;
39
84
 
@@ -116,7 +161,27 @@ pub const Face = struct {
116
161
  const units_per_em = try readU16(bytes, head_r.offset + 18);
117
162
  if (units_per_em == 0) return error.FontParseFailed;
118
163
  const index_to_loc = try readI16(bytes, head_r.offset + 50);
164
+
165
+ // `glyf` outlines require the full version-1.0 `maxp` (the 0.5
166
+ // form carries no outline maxima and belongs to CFF fonts).
167
+ if (try readU32(bytes, maxp_r.offset) != 0x00010000) return error.FontParseFailed;
119
168
  const num_glyphs = try readU16(bytes, maxp_r.offset + 4);
169
+ // The registration-time glyph-budget gate: a face whose declared
170
+ // maxima exceed the outline budgets is rejected WHOLE, here,
171
+ // instead of degrading past-budget glyphs to block fallbacks at
172
+ // render time (registration validation is loud; render never
173
+ // is). Parsed faces therefore always outline every glyph they
174
+ // map — the per-glyph checks below only fire for faces whose
175
+ // `maxp` under-declares.
176
+ if (try readU16(bytes, maxp_r.offset + 6) > max_glyph_points or
177
+ try readU16(bytes, maxp_r.offset + 8) > max_glyph_contours or
178
+ try readU16(bytes, maxp_r.offset + 10) > max_composite_points or
179
+ try readU16(bytes, maxp_r.offset + 12) > max_composite_contours or
180
+ try readU16(bytes, maxp_r.offset + 28) > max_composite_components or
181
+ try readU16(bytes, maxp_r.offset + 30) > max_composite_depth)
182
+ {
183
+ return error.FontGlyphTooComplex;
184
+ }
120
185
  const ascender = try readI16(bytes, hhea_r.offset + 4);
121
186
  const descender = try readI16(bytes, hhea_r.offset + 6);
122
187
  const num_h_metrics = try readU16(bytes, hhea_r.offset + 34);
@@ -204,6 +269,9 @@ pub const Face = struct {
204
269
  }
205
270
 
206
271
  fn glyphOutlineInner(self: *const Face, glyph: u16, transform: Affine, sink: anytype, depth: usize) Error!void {
272
+ // Backstop only: `parse` already gated the face's declared
273
+ // `maxp.maxComponentDepth` against this budget, so for a parsed
274
+ // face this fires only when the font under-declares its depth.
207
275
  if (depth > max_composite_depth) return error.FontGlyphTooComplex;
208
276
  if (glyph >= self.num_glyphs) return error.FontParseFailed;
209
277
  const range = try self.glyphRange(glyph);
@@ -232,6 +300,10 @@ pub const Face = struct {
232
300
  }
233
301
 
234
302
  fn simpleOutline(self: *const Face, glyph_offset: usize, contour_count: usize, transform: Affine, sink: anytype) Error!void {
303
+ // Backstop only (here and the point-count check below): `parse`
304
+ // gated the face's declared `maxp` maxima against these budgets,
305
+ // so for a parsed face they fire only when the font
306
+ // under-declares — the buffers below stay safe either way.
235
307
  if (contour_count > max_glyph_contours) return error.FontGlyphTooComplex;
236
308
  var end_points: [max_glyph_contours]u16 = undefined;
237
309
  var cursor = glyph_offset + 10;
@@ -320,8 +392,12 @@ pub const Face = struct {
320
392
  const child: u16 = try readU16(self.bytes, cursor + 2);
321
393
  cursor += 4;
322
394
 
323
- // Only XY-offset placement is supported (point matching does
324
- // not occur in the bundled face).
395
+ // Only XY-offset placement is supported. Point matching is
396
+ // the one per-glyph refusal `maxp` cannot gate at
397
+ // registration time (no declared field describes it); no
398
+ // measured production face uses it — the bundled Geist
399
+ // faces and every CJK face measured for the budgets above
400
+ // place composites by XY offsets.
325
401
  if (flags & 0x0002 == 0) return error.FontGlyphTooComplex;
326
402
  var dx: f32 = 0;
327
403
  var dy: f32 = 0;
@@ -360,6 +436,8 @@ pub const Face = struct {
360
436
 
361
437
  if (flags & 0x0020 == 0) return;
362
438
  }
439
+ // Backstop only: `parse` gated `maxp.maxComponentElements`, so
440
+ // this fires only when the font under-declares.
363
441
  return error.FontGlyphTooComplex;
364
442
  }
365
443
  };
@@ -421,6 +499,60 @@ fn emitContour(transform: Affine, sink: anytype, flags: []const u8, xs: []const
421
499
  try sink.close();
422
500
  }
423
501
 
502
+ /// A face's `maxp`-declared outline maxima — what the glyph-budget gate
503
+ /// in `Face.parse` compares against the budgets above. Registration
504
+ /// teaching uses this to name a refused face's declared numbers next to
505
+ /// the budgets it exceeds.
506
+ pub const GlyphMaxima = struct {
507
+ points: u16,
508
+ contours: u16,
509
+ /// Flattened totals across a composite glyph's whole component tree
510
+ /// (`maxp.maxCompositePoints`/`maxCompositeContours`) — the maxima
511
+ /// the path builder actually receives when a composite renders, so
512
+ /// the gate must read them alongside the simple maxima or a
513
+ /// composite-heavy face would pass registration and overflow the
514
+ /// builder at render time.
515
+ composite_points: u16,
516
+ composite_contours: u16,
517
+ component_elements: u16,
518
+ component_depth: u16,
519
+
520
+ pub fn withinBudgets(self: GlyphMaxima) bool {
521
+ return self.points <= max_glyph_points and
522
+ self.contours <= max_glyph_contours and
523
+ self.composite_points <= max_composite_points and
524
+ self.composite_contours <= max_composite_contours and
525
+ self.component_elements <= max_composite_components and
526
+ self.component_depth <= max_composite_depth;
527
+ }
528
+ };
529
+
530
+ /// The declared outline maxima of `bytes`, or null when the file carries
531
+ /// no readable version-1.0 `maxp` table (truncated, CFF-flavored, or not
532
+ /// a font at all). Reads only the table directory and `maxp`, so it
533
+ /// answers for byte blobs `Face.parse` rejects.
534
+ pub fn declaredGlyphMaxima(bytes: []const u8) ?GlyphMaxima {
535
+ const table_count = readU16(bytes, 4) catch return null;
536
+ var index: usize = 0;
537
+ while (index < table_count) : (index += 1) {
538
+ const record = 12 + index * 16;
539
+ const tag = readBytes(bytes, record, 4) catch return null;
540
+ if (!std.mem.eql(u8, tag, "maxp")) continue;
541
+ const offset = readU32(bytes, record + 8) catch return null;
542
+ const version = readU32(bytes, offset) catch return null;
543
+ if (version != 0x00010000) return null;
544
+ return .{
545
+ .points = readU16(bytes, offset + 6) catch return null,
546
+ .contours = readU16(bytes, offset + 8) catch return null,
547
+ .composite_points = readU16(bytes, offset + 10) catch return null,
548
+ .composite_contours = readU16(bytes, offset + 12) catch return null,
549
+ .component_elements = readU16(bytes, offset + 28) catch return null,
550
+ .component_depth = readU16(bytes, offset + 30) catch return null,
551
+ };
552
+ }
553
+ return null;
554
+ }
555
+
424
556
  /// Why `Face.parse` rejects `bytes`, as a self-contained teaching
425
557
  /// sentence for registration-time diagnostics, or null when the bytes
426
558
  /// parse cleanly (`parse` is the single authority; this never disagrees
@@ -466,6 +598,21 @@ fn diagnoseParseFailure(bytes: []const u8) []const u8 {
466
598
  for (required, 0..) |entry, slot| {
467
599
  if (!found[slot]) return entry.teach;
468
600
  }
601
+ // Declared glyph complexity beyond the outline budgets: the whole
602
+ // face is refused at parse (registration) time rather than degrading
603
+ // past-budget glyphs to block fallbacks at render time. Callers that
604
+ // can format (UiApp's `fonts` teaching) name the face's declared
605
+ // numbers via `declaredGlyphMaxima`.
606
+ if (declaredGlyphMaxima(bytes)) |maxima| {
607
+ if (!maxima.withinBudgets()) {
608
+ return std.fmt.comptimePrint(
609
+ "font declares glyph outlines denser than the renderer's budgets ({d} points / {d} contours per simple glyph, {d} points / {d} contours per flattened composite, composites {d} deep of {d} components); past-budget glyphs would render as block fallbacks, so registration refuses the face",
610
+ .{ max_glyph_points, max_glyph_contours, max_composite_points, max_composite_contours, max_composite_depth, max_composite_components },
611
+ );
612
+ }
613
+ } else {
614
+ return "'maxp' table is not the version-1.0 form TrueType 'glyf' outlines require (or is truncated), so the font's glyph maxima cannot be validated";
615
+ }
469
616
  // Structure looks complete, so the failure is field-level.
470
617
  return "font tables are present but malformed: needs a nonzero units-per-em, at least one horizontal metric, and a Unicode format-4 'cmap' subtable";
471
618
  }