@native-sdk/cli 0.0.0 → 0.4.1
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.
- package/LICENSE +176 -0
- package/README.md +50 -1
- package/app.zon +30 -0
- package/assets/icon.icns +0 -0
- package/assets/icon.ico +0 -0
- package/assets/icon.png +0 -0
- package/assets/icon.svg +15 -0
- package/assets/native-sdk.entitlements +10 -0
- package/assets/native-sdk.manifest +12 -0
- package/bin/native.js +156 -0
- package/build/app.zig +716 -0
- package/build.zig +2593 -0
- package/build.zig.zon +24 -0
- package/native-sdk.d.ts +615 -0
- package/package.json +45 -8
- package/skill-data/automation/SKILL.md +248 -0
- package/skill-data/core/SKILL.md +249 -0
- package/skill-data/core/references/app-model-runtime.md +141 -0
- package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
- package/skill-data/core/references/frontend-assets.md +124 -0
- package/skill-data/core/references/project-anatomy.md +108 -0
- package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
- package/skill-data/native-ui/SKILL.md +1080 -0
- package/skills/native-sdk/SKILL.md +33 -0
- package/src/app_runner/root.zig +740 -0
- package/src/assets/root.zig +20 -0
- package/src/automation/protocol.zig +285 -0
- package/src/automation/root.zig +13 -0
- package/src/automation/server.zig +506 -0
- package/src/automation/snapshot.zig +1202 -0
- package/src/automation/watcher.zig +165 -0
- package/src/bridge/root.zig +507 -0
- package/src/debug/root.zig +246 -0
- package/src/embed/app_exports.zig +11 -0
- package/src/embed/c_api.zig +793 -0
- package/src/embed/c_exports.zig +10 -0
- package/src/embed/chrome.zig +168 -0
- package/src/embed/conversions.zig +359 -0
- package/src/embed/host.zig +960 -0
- package/src/embed/root.zig +81 -0
- package/src/embed/tests.zig +2552 -0
- package/src/embed/types.zig +382 -0
- package/src/embed/ui_host.zig +289 -0
- package/src/extensions/root.zig +197 -0
- package/src/frontend/root.zig +48 -0
- package/src/js/root.zig +65 -0
- package/src/platform/android/NativeSdkActivity.java +1370 -0
- package/src/platform/android/android_host.c +696 -0
- package/src/platform/android/files.zig +9 -0
- package/src/platform/android/native_sdk_app.h +237 -0
- package/src/platform/ios/files.zig +8 -0
- package/src/platform/ios/native_sdk_app.h +289 -0
- package/src/platform/ios/uikit_host.m +2739 -0
- package/src/platform/linux/cef_host.cpp +599 -0
- package/src/platform/linux/gtk_host.c +5362 -0
- package/src/platform/linux/gtk_host.h +265 -0
- package/src/platform/linux/root.zig +1519 -0
- package/src/platform/macos/appkit_host.h +604 -0
- package/src/platform/macos/appkit_host.m +10392 -0
- package/src/platform/macos/automation_host.h +14 -0
- package/src/platform/macos/automation_host.m +12 -0
- package/src/platform/macos/cef_host.mm +3028 -0
- package/src/platform/macos/root.zig +2260 -0
- package/src/platform/null_platform.zig +2232 -0
- package/src/platform/null_platform_tests.zig +670 -0
- package/src/platform/policy_values.zig +14 -0
- package/src/platform/root.zig +181 -0
- package/src/platform/types.zig +2681 -0
- package/src/platform/windows/cef_host.cpp +3 -0
- package/src/platform/windows/root.zig +1521 -0
- package/src/platform/windows/webview2_host.cpp +6230 -0
- package/src/primitives/app_dirs/root.zig +440 -0
- package/src/primitives/app_manifest/root.zig +109 -0
- package/src/primitives/app_manifest/tests.zig +758 -0
- package/src/primitives/app_manifest/types.zig +587 -0
- package/src/primitives/app_manifest/validation.zig +828 -0
- package/src/primitives/assets/root.zig +520 -0
- package/src/primitives/canvas/a11y_audit.zig +390 -0
- package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
- package/src/primitives/canvas/app_icon.zig +1590 -0
- package/src/primitives/canvas/chart.zig +392 -0
- package/src/primitives/canvas/chart_tests.zig +751 -0
- package/src/primitives/canvas/commands.zig +446 -0
- package/src/primitives/canvas/drawing.zig +309 -0
- package/src/primitives/canvas/equality.zig +316 -0
- package/src/primitives/canvas/events.zig +696 -0
- package/src/primitives/canvas/font_coverage.zig +118 -0
- package/src/primitives/canvas/font_ttf.zig +510 -0
- package/src/primitives/canvas/font_ttf_tests.zig +280 -0
- package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
- package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
- package/src/primitives/canvas/frame.zig +679 -0
- package/src/primitives/canvas/frame_metrics.zig +462 -0
- package/src/primitives/canvas/gpu.zig +639 -0
- package/src/primitives/canvas/hash.zig +112 -0
- package/src/primitives/canvas/icons/alert.svg +1 -0
- package/src/primitives/canvas/icons/archive.svg +1 -0
- package/src/primitives/canvas/icons/arrow-down.svg +1 -0
- package/src/primitives/canvas/icons/arrow-right.svg +1 -0
- package/src/primitives/canvas/icons/arrow-up.svg +1 -0
- package/src/primitives/canvas/icons/check-circle.svg +1 -0
- package/src/primitives/canvas/icons/check.svg +1 -0
- package/src/primitives/canvas/icons/chevron-down.svg +1 -0
- package/src/primitives/canvas/icons/chevron-left.svg +1 -0
- package/src/primitives/canvas/icons/chevron-right.svg +1 -0
- package/src/primitives/canvas/icons/chevron-up.svg +1 -0
- package/src/primitives/canvas/icons/circle-dot.svg +1 -0
- package/src/primitives/canvas/icons/clock.svg +1 -0
- package/src/primitives/canvas/icons/copy.svg +1 -0
- package/src/primitives/canvas/icons/download.svg +1 -0
- package/src/primitives/canvas/icons/edit.svg +1 -0
- package/src/primitives/canvas/icons/ellipsis.svg +1 -0
- package/src/primitives/canvas/icons/external-link.svg +1 -0
- package/src/primitives/canvas/icons/eye.svg +1 -0
- package/src/primitives/canvas/icons/file-text.svg +1 -0
- package/src/primitives/canvas/icons/folder-open.svg +1 -0
- package/src/primitives/canvas/icons/folder.svg +1 -0
- package/src/primitives/canvas/icons/git-branch.svg +1 -0
- package/src/primitives/canvas/icons/git-merge.svg +1 -0
- package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
- package/src/primitives/canvas/icons/info.svg +1 -0
- package/src/primitives/canvas/icons/menu.svg +1 -0
- package/src/primitives/canvas/icons/missing.svg +1 -0
- package/src/primitives/canvas/icons/moon.svg +1 -0
- package/src/primitives/canvas/icons/music.svg +1 -0
- package/src/primitives/canvas/icons/panel-left.svg +1 -0
- package/src/primitives/canvas/icons/panel-right.svg +1 -0
- package/src/primitives/canvas/icons/pause.svg +1 -0
- package/src/primitives/canvas/icons/play.svg +1 -0
- package/src/primitives/canvas/icons/plus.svg +1 -0
- package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
- package/src/primitives/canvas/icons/repeat.svg +1 -0
- package/src/primitives/canvas/icons/save.svg +1 -0
- package/src/primitives/canvas/icons/search.svg +1 -0
- package/src/primitives/canvas/icons/send.svg +1 -0
- package/src/primitives/canvas/icons/settings.svg +1 -0
- package/src/primitives/canvas/icons/shuffle.svg +1 -0
- package/src/primitives/canvas/icons/skip-back.svg +1 -0
- package/src/primitives/canvas/icons/skip-forward.svg +1 -0
- package/src/primitives/canvas/icons/sun.svg +1 -0
- package/src/primitives/canvas/icons/terminal.svg +1 -0
- package/src/primitives/canvas/icons/trash.svg +1 -0
- package/src/primitives/canvas/icons/volume.svg +1 -0
- package/src/primitives/canvas/icons/wrench.svg +1 -0
- package/src/primitives/canvas/icons/x-circle.svg +1 -0
- package/src/primitives/canvas/icons/x.svg +1 -0
- package/src/primitives/canvas/icons.zig +249 -0
- package/src/primitives/canvas/layout_audit.zig +744 -0
- package/src/primitives/canvas/layout_audit_tests.zig +426 -0
- package/src/primitives/canvas/markdown.zig +1031 -0
- package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
- package/src/primitives/canvas/markdown_tests.zig +538 -0
- package/src/primitives/canvas/plan_key_index.zig +120 -0
- package/src/primitives/canvas/png.zig +355 -0
- package/src/primitives/canvas/reference.zig +1457 -0
- package/src/primitives/canvas/reference_blur.zig +104 -0
- package/src/primitives/canvas/reference_memo.zig +322 -0
- package/src/primitives/canvas/reference_paths.zig +184 -0
- package/src/primitives/canvas/reference_tests.zig +1650 -0
- package/src/primitives/canvas/render.zig +747 -0
- package/src/primitives/canvas/render_effects.zig +255 -0
- package/src/primitives/canvas/render_fingerprints.zig +256 -0
- package/src/primitives/canvas/render_generic_resources.zig +376 -0
- package/src/primitives/canvas/render_images.zig +247 -0
- package/src/primitives/canvas/render_layers.zig +260 -0
- package/src/primitives/canvas/render_paths.zig +347 -0
- package/src/primitives/canvas/render_resources.zig +51 -0
- package/src/primitives/canvas/render_tests.zig +3186 -0
- package/src/primitives/canvas/root.zig +744 -0
- package/src/primitives/canvas/serialization.zig +1528 -0
- package/src/primitives/canvas/svg_icon.zig +636 -0
- package/src/primitives/canvas/svg_icon_tests.zig +312 -0
- package/src/primitives/canvas/test_support.zig +422 -0
- package/src/primitives/canvas/testdata/markdown_document.md +83 -0
- package/src/primitives/canvas/tests.zig +30 -0
- package/src/primitives/canvas/text.zig +82 -0
- package/src/primitives/canvas/text_atlas.zig +408 -0
- package/src/primitives/canvas/text_batch_tests.zig +431 -0
- package/src/primitives/canvas/text_interaction.zig +615 -0
- package/src/primitives/canvas/text_layout.zig +1069 -0
- package/src/primitives/canvas/text_layout_cache.zig +281 -0
- package/src/primitives/canvas/text_layout_hash.zig +113 -0
- package/src/primitives/canvas/text_layout_types.zig +128 -0
- package/src/primitives/canvas/text_measure_cache.zig +279 -0
- package/src/primitives/canvas/text_metrics.zig +309 -0
- package/src/primitives/canvas/text_metrics_tests.zig +262 -0
- package/src/primitives/canvas/text_span_tests.zig +608 -0
- package/src/primitives/canvas/text_spans.zig +1010 -0
- package/src/primitives/canvas/text_tests.zig +1949 -0
- package/src/primitives/canvas/themes/geist.zig +470 -0
- package/src/primitives/canvas/tokens.zig +1522 -0
- package/src/primitives/canvas/ui.zig +2686 -0
- package/src/primitives/canvas/ui_markup.zig +3791 -0
- package/src/primitives/canvas/ui_markup_binary.zig +522 -0
- package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
- package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
- package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
- package/src/primitives/canvas/ui_markup_edit.zig +416 -0
- package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
- package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
- package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
- package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
- package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
- package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
- package/src/primitives/canvas/ui_markup_view.zig +2352 -0
- package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
- package/src/primitives/canvas/ui_provenance.zig +96 -0
- package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
- package/src/primitives/canvas/ui_schema.zig +825 -0
- package/src/primitives/canvas/ui_schema_tests.zig +128 -0
- package/src/primitives/canvas/ui_tests.zig +1432 -0
- package/src/primitives/canvas/vector.zig +919 -0
- package/src/primitives/canvas/vector_tests.zig +547 -0
- package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
- package/src/primitives/canvas/virtual_extents.zig +540 -0
- package/src/primitives/canvas/widget_access.zig +208 -0
- package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
- package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
- package/src/primitives/canvas/widget_invalidation.zig +748 -0
- package/src/primitives/canvas/widget_layout.zig +2055 -0
- package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
- package/src/primitives/canvas/widget_metrics.zig +228 -0
- package/src/primitives/canvas/widget_render.zig +2534 -0
- package/src/primitives/canvas/widget_render_controls.zig +1563 -0
- package/src/primitives/canvas/widget_render_scroll.zig +136 -0
- package/src/primitives/canvas/widget_render_style.zig +626 -0
- package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
- package/src/primitives/canvas/widget_routing.zig +475 -0
- package/src/primitives/canvas/widget_runtime.zig +307 -0
- package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
- package/src/primitives/canvas/widget_semantics.zig +480 -0
- package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
- package/src/primitives/canvas/widget_tests.zig +8 -0
- package/src/primitives/canvas/widget_text_input.zig +351 -0
- package/src/primitives/canvas/widget_text_select.zig +147 -0
- package/src/primitives/canvas/widget_tree.zig +232 -0
- package/src/primitives/canvas/widgets.zig +1319 -0
- package/src/primitives/diagnostics/root.zig +445 -0
- package/src/primitives/geometry/root.zig +889 -0
- package/src/primitives/json/root.zig +272 -0
- package/src/primitives/platform_info/root.zig +450 -0
- package/src/primitives/trace/root.zig +569 -0
- package/src/root.zig +165 -0
- package/src/runtime/api.zig +431 -0
- package/src/runtime/async_bridge.zig +49 -0
- package/src/runtime/automation_commands.zig +686 -0
- package/src/runtime/automation_liveness_tests.zig +140 -0
- package/src/runtime/automation_snapshot.zig +246 -0
- package/src/runtime/automation_widget_dispatch.zig +617 -0
- package/src/runtime/bridge_dispatch_tests.zig +202 -0
- package/src/runtime/bridge_payload.zig +194 -0
- package/src/runtime/bridge_permission_tests.zig +551 -0
- package/src/runtime/bridge_responses.zig +414 -0
- package/src/runtime/builtin_bridge.zig +554 -0
- package/src/runtime/canvas_budget_edge_tests.zig +415 -0
- package/src/runtime/canvas_font_tests.zig +410 -0
- package/src/runtime/canvas_fonts.zig +229 -0
- package/src/runtime/canvas_frame.zig +1734 -0
- package/src/runtime/canvas_frame_animation_tests.zig +609 -0
- package/src/runtime/canvas_frame_cache_tests.zig +333 -0
- package/src/runtime/canvas_frame_helpers.zig +414 -0
- package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
- package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
- package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
- package/src/runtime/canvas_frame_retained_tests.zig +753 -0
- package/src/runtime/canvas_frame_tests.zig +9 -0
- package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
- package/src/runtime/canvas_image_tests.zig +362 -0
- package/src/runtime/canvas_images.zig +224 -0
- package/src/runtime/canvas_limits.zig +317 -0
- package/src/runtime/canvas_screenshot_tests.zig +289 -0
- package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
- package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
- package/src/runtime/canvas_widget_chart_tests.zig +249 -0
- package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
- package/src/runtime/canvas_widget_context_menu.zig +318 -0
- package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
- package/src/runtime/canvas_widget_control_tests.zig +2164 -0
- package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
- package/src/runtime/canvas_widget_display.zig +677 -0
- package/src/runtime/canvas_widget_event_tests.zig +1779 -0
- package/src/runtime/canvas_widget_events.zig +1098 -0
- package/src/runtime/canvas_widget_floating_tests.zig +695 -0
- package/src/runtime/canvas_widget_layout_tests.zig +26 -0
- package/src/runtime/canvas_widget_runtime.zig +1335 -0
- package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
- package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
- package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
- package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
- package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
- package/src/runtime/canvas_widget_state.zig +798 -0
- package/src/runtime/canvas_widget_text_tests.zig +2104 -0
- package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
- package/src/runtime/clock.zig +231 -0
- package/src/runtime/core.zig +927 -0
- package/src/runtime/effects.zig +4197 -0
- package/src/runtime/effects_audio_tests.zig +690 -0
- package/src/runtime/effects_clipboard_tests.zig +317 -0
- package/src/runtime/effects_fetch_tests.zig +1031 -0
- package/src/runtime/effects_file_tests.zig +407 -0
- package/src/runtime/effects_tests.zig +1251 -0
- package/src/runtime/flow.zig +1175 -0
- package/src/runtime/frame_profile.zig +221 -0
- package/src/runtime/gpu_surface_events.zig +639 -0
- package/src/runtime/launch_timing.zig +59 -0
- package/src/runtime/markdown_app_tests.zig +224 -0
- package/src/runtime/native_command_event_tests.zig +431 -0
- package/src/runtime/platform_bridge_tests.zig +8 -0
- package/src/runtime/platform_config_tests.zig +392 -0
- package/src/runtime/root.zig +150 -0
- package/src/runtime/runtime_event_tests.zig +1079 -0
- package/src/runtime/session_journal.zig +1522 -0
- package/src/runtime/session_record.zig +332 -0
- package/src/runtime/session_replay.zig +274 -0
- package/src/runtime/session_state.zig +30 -0
- package/src/runtime/session_tests.zig +399 -0
- package/src/runtime/shell_layout.zig +472 -0
- package/src/runtime/shell_layout_tests.zig +1246 -0
- package/src/runtime/shell_view_tests.zig +604 -0
- package/src/runtime/state.zig +110 -0
- package/src/runtime/system_services.zig +392 -0
- package/src/runtime/test_support.zig +79 -0
- package/src/runtime/tests.zig +30 -0
- package/src/runtime/ui_app.zig +3290 -0
- package/src/runtime/ui_app_huge_model_tests.zig +351 -0
- package/src/runtime/ui_app_provenance.zig +370 -0
- package/src/runtime/ui_app_tests.zig +3254 -0
- package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
- package/src/runtime/ui_app_window_tests.zig +442 -0
- package/src/runtime/validation.zig +206 -0
- package/src/runtime/view.zig +908 -0
- package/src/runtime/view_canvas.zig +850 -0
- package/src/runtime/view_widget_control.zig +433 -0
- package/src/runtime/view_widget_scroll.zig +360 -0
- package/src/runtime/view_widget_text.zig +275 -0
- package/src/runtime/view_widget_tree.zig +843 -0
- package/src/runtime/webview_view_bridge_tests.zig +664 -0
- package/src/runtime/widget_bridge.zig +231 -0
- package/src/runtime/window_command_bridge_tests.zig +403 -0
- package/src/runtime/window_storage.zig +255 -0
- package/src/runtime/window_view_runtime.zig +727 -0
- package/src/runtime/window_views.zig +334 -0
- package/src/security/root.zig +118 -0
- package/src/tooling/android.zig +1172 -0
- package/src/tooling/assets.zig +128 -0
- package/src/tooling/buildgraph.zig +379 -0
- package/src/tooling/cef.zig +671 -0
- package/src/tooling/codesign.zig +172 -0
- package/src/tooling/components/identity_tests.zig +185 -0
- package/src/tooling/components/stepper.zig +113 -0
- package/src/tooling/components/timeline.native +27 -0
- package/src/tooling/components/timeline_item.zig +131 -0
- package/src/tooling/default_icon.icns +0 -0
- package/src/tooling/default_icon.png +0 -0
- package/src/tooling/dev.zig +172 -0
- package/src/tooling/doctor.zig +349 -0
- package/src/tooling/eject_components.zig +170 -0
- package/src/tooling/embedlib.zig +290 -0
- package/src/tooling/ios.zig +528 -0
- package/src/tooling/manifest.zig +1939 -0
- package/src/tooling/package.zig +2388 -0
- package/src/tooling/process_tree.zig +114 -0
- package/src/tooling/raw_manifest.zig +193 -0
- package/src/tooling/root.zig +22 -0
- package/src/tooling/templates.zig +3191 -0
- package/src/tooling/toolchain.zig +371 -0
- package/src/tooling/verbs.zig +204 -0
- package/src/tooling/web_engine.zig +135 -0
- package/src/tooling/xcodeproj.zig +462 -0
- package/src/window_state/root.zig +507 -0
- package/index.js +0 -2
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
//! Runtime canvas font registry tests: registration validation and
|
|
2
|
+
//! capacity bounds (loud, one-past-budget), hostile/truncated font
|
|
3
|
+
//! files, the font-aware measure provider, pixel parity between the
|
|
4
|
+
//! present path and the reference screenshot path for a registered
|
|
5
|
+
//! face, and the UiApp `Options.fonts` startup seam.
|
|
6
|
+
//!
|
|
7
|
+
//! The registered fixture is the ALREADY-BUNDLED Geist Mono bytes
|
|
8
|
+
//! (`canvas.font_ttf.geist_mono_bytes`) under a registered id: the
|
|
9
|
+
//! bundled face doubles as a known-good registered face, so parity can
|
|
10
|
+
//! assert byte-identical pixels against the built-in mono id without
|
|
11
|
+
//! shipping any new font binary.
|
|
12
|
+
|
|
13
|
+
const std = @import("std");
|
|
14
|
+
const geometry = @import("geometry");
|
|
15
|
+
const canvas = @import("canvas");
|
|
16
|
+
const app_manifest = @import("app_manifest");
|
|
17
|
+
const core = @import("core.zig");
|
|
18
|
+
const canvas_limits = @import("canvas_limits.zig");
|
|
19
|
+
const ui_app_model = @import("ui_app.zig");
|
|
20
|
+
const support = @import("test_support.zig");
|
|
21
|
+
|
|
22
|
+
const platform = support.platform;
|
|
23
|
+
const App = support.App;
|
|
24
|
+
const TestHarness = support.TestHarness;
|
|
25
|
+
|
|
26
|
+
const registered_font_id: canvas.FontId = canvas.min_registered_font_id;
|
|
27
|
+
const mono_bytes = canvas.font_ttf.geist_mono_bytes;
|
|
28
|
+
|
|
29
|
+
fn startedGpuHarness(allocator: std.mem.Allocator) !*TestHarness() {
|
|
30
|
+
const harness = try TestHarness().create(allocator, .{ .size = geometry.SizeF.init(240, 140) });
|
|
31
|
+
errdefer harness.destroy(allocator);
|
|
32
|
+
harness.null_platform.gpu_surfaces = true;
|
|
33
|
+
return harness;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const RegistryApp = struct {
|
|
37
|
+
fn app(self: *@This()) App {
|
|
38
|
+
return .{ .context = self, .name = "canvas-font-registry", .source = platform.WebViewSource.html("<h1>Fonts</h1>") };
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
test "canvas font registry validates ids, bytes, and capacity" {
|
|
43
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
44
|
+
defer harness.destroy(std.testing.allocator);
|
|
45
|
+
var app_state: RegistryApp = .{};
|
|
46
|
+
try harness.start(app_state.app());
|
|
47
|
+
|
|
48
|
+
// Id validation: 0 is the "inherit run font" sentinel, everything
|
|
49
|
+
// below the registered floor belongs to built-in faces.
|
|
50
|
+
try std.testing.expectError(error.InvalidFontId, harness.runtime.registerCanvasFont(0, mono_bytes));
|
|
51
|
+
try std.testing.expectError(error.ReservedFontId, harness.runtime.registerCanvasFont(1, mono_bytes));
|
|
52
|
+
try std.testing.expectError(error.ReservedFontId, harness.runtime.registerCanvasFont(canvas.min_registered_font_id - 1, mono_bytes));
|
|
53
|
+
try std.testing.expectEqual(@as(usize, 0), harness.runtime.registeredCanvasFontCount());
|
|
54
|
+
|
|
55
|
+
// Over the per-font budget: loud, one past the documented constant,
|
|
56
|
+
// registers nothing.
|
|
57
|
+
const oversized = try std.testing.allocator.alloc(u8, canvas_limits.max_registered_canvas_font_bytes + 1);
|
|
58
|
+
defer std.testing.allocator.free(oversized);
|
|
59
|
+
@memset(oversized, 0);
|
|
60
|
+
try std.testing.expectError(error.FontTooLarge, harness.runtime.registerCanvasFont(registered_font_id, oversized));
|
|
61
|
+
try std.testing.expectEqual(@as(usize, 0), harness.runtime.registeredCanvasFontCount());
|
|
62
|
+
|
|
63
|
+
// A good registration resolves through the registry.
|
|
64
|
+
try harness.runtime.registerCanvasFont(registered_font_id, mono_bytes);
|
|
65
|
+
try std.testing.expectEqual(@as(usize, 1), harness.runtime.registeredCanvasFontCount());
|
|
66
|
+
const face = harness.runtime.registeredCanvasFontFace(registered_font_id).?;
|
|
67
|
+
try std.testing.expect(face.glyphIndex('A') != 0);
|
|
68
|
+
try std.testing.expect(harness.runtime.registeredCanvasFontFace(registered_font_id + 1) == null);
|
|
69
|
+
|
|
70
|
+
// Registered ids are permanent: re-use fails loudly (atlas caches key
|
|
71
|
+
// glyphs by font id with no content fingerprint).
|
|
72
|
+
try std.testing.expectError(error.FontIdInUse, harness.runtime.registerCanvasFont(registered_font_id, mono_bytes));
|
|
73
|
+
try std.testing.expectEqual(@as(usize, 1), harness.runtime.registeredCanvasFontCount());
|
|
74
|
+
|
|
75
|
+
// Fill every slot; the one-past registration overflows loudly.
|
|
76
|
+
var id: canvas.FontId = registered_font_id + 1;
|
|
77
|
+
while (harness.runtime.registeredCanvasFontCount() < canvas_limits.max_registered_canvas_fonts) : (id += 1) {
|
|
78
|
+
try harness.runtime.registerCanvasFont(id, mono_bytes);
|
|
79
|
+
}
|
|
80
|
+
try std.testing.expectError(error.FontRegistryFull, harness.runtime.registerCanvasFont(id, mono_bytes));
|
|
81
|
+
try std.testing.expectEqual(canvas_limits.max_registered_canvas_fonts, harness.runtime.registeredCanvasFontCount());
|
|
82
|
+
|
|
83
|
+
// The registered resource set hands both renderers one entry per id.
|
|
84
|
+
const resources = harness.runtime.registeredCanvasFonts();
|
|
85
|
+
try std.testing.expectEqual(canvas_limits.max_registered_canvas_fonts, resources.len);
|
|
86
|
+
try std.testing.expectEqual(registered_font_id, resources[0].id);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
test "hostile and truncated font files fail loud at registration and never corrupt the registry" {
|
|
90
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
91
|
+
defer harness.destroy(std.testing.allocator);
|
|
92
|
+
var app_state: RegistryApp = .{};
|
|
93
|
+
try harness.start(app_state.app());
|
|
94
|
+
|
|
95
|
+
// Garbage bytes.
|
|
96
|
+
try std.testing.expectError(error.FontParseFailed, harness.runtime.registerCanvasFont(registered_font_id, "definitely not a font"));
|
|
97
|
+
|
|
98
|
+
// Truncations of a real face at coarse strides: registration must
|
|
99
|
+
// reject every prefix (the table directory always points past a
|
|
100
|
+
// truncated file) with a recoverable error — never a crash, never a
|
|
101
|
+
// partial slot.
|
|
102
|
+
var len: usize = 0;
|
|
103
|
+
while (len < mono_bytes.len) : (len += if (len < 64) 7 else 4099) {
|
|
104
|
+
try std.testing.expectError(error.FontParseFailed, harness.runtime.registerCanvasFont(registered_font_id, mono_bytes[0..len]));
|
|
105
|
+
try std.testing.expectEqual(@as(usize, 0), harness.runtime.registeredCanvasFontCount());
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Bit-flipped table directory: parse rejects or (for flips in table
|
|
109
|
+
// payloads the directory does not bound) glyph reads stay
|
|
110
|
+
// bounds-checked. Either way registration state stays consistent.
|
|
111
|
+
var corrupted = try std.testing.allocator.dupe(u8, mono_bytes);
|
|
112
|
+
defer std.testing.allocator.free(corrupted);
|
|
113
|
+
var offset: usize = 4;
|
|
114
|
+
var flip_id: canvas.FontId = registered_font_id;
|
|
115
|
+
while (offset < 256) : (offset += 13) {
|
|
116
|
+
// Leave one slot free so the good-face registration below always
|
|
117
|
+
// has room (some flips — table tags the parser does not require,
|
|
118
|
+
// checksum bytes — are legitimately tolerated and take a slot).
|
|
119
|
+
if (harness.runtime.registeredCanvasFontCount() >= canvas_limits.max_registered_canvas_fonts - 1) break;
|
|
120
|
+
corrupted[offset] ^= 0xFF;
|
|
121
|
+
defer corrupted[offset] ^= 0xFF;
|
|
122
|
+
const before = harness.runtime.registeredCanvasFontCount();
|
|
123
|
+
if (harness.runtime.registerCanvasFont(flip_id, corrupted)) |_| {
|
|
124
|
+
// A flip the parser legitimately tolerates: the slot is
|
|
125
|
+
// committed and the face answers lookups without crashing.
|
|
126
|
+
try std.testing.expectEqual(before + 1, harness.runtime.registeredCanvasFontCount());
|
|
127
|
+
_ = harness.runtime.registeredCanvasFontFace(flip_id).?.glyphIndex('A');
|
|
128
|
+
flip_id += 1;
|
|
129
|
+
} else |_| {
|
|
130
|
+
try std.testing.expectEqual(before, harness.runtime.registeredCanvasFontCount());
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Every teaching diagnostic is available for the failures above.
|
|
135
|
+
try std.testing.expect(canvas.font_ttf.parseFailureReason("definitely not a font") != null);
|
|
136
|
+
try std.testing.expect(canvas.font_ttf.parseFailureReason(mono_bytes[0 .. mono_bytes.len / 2]) != null);
|
|
137
|
+
|
|
138
|
+
// The registry still accepts a good face after the hostile parade.
|
|
139
|
+
try harness.runtime.registerCanvasFont(flip_id, mono_bytes);
|
|
140
|
+
try std.testing.expect(harness.runtime.registeredCanvasFontFace(flip_id) != null);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
test "registered faces measure with their own advances through the runtime provider" {
|
|
144
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
145
|
+
defer harness.destroy(std.testing.allocator);
|
|
146
|
+
var app_state: RegistryApp = .{};
|
|
147
|
+
try harness.start(app_state.app());
|
|
148
|
+
|
|
149
|
+
// The null platform has no host measurement: no provider until a
|
|
150
|
+
// font registers (layout stays on the estimator, byte-identical to
|
|
151
|
+
// before this seam existed).
|
|
152
|
+
try std.testing.expect(harness.runtime.textMeasureProvider() == null);
|
|
153
|
+
|
|
154
|
+
try harness.runtime.registerCanvasFont(registered_font_id, mono_bytes);
|
|
155
|
+
const provider = harness.runtime.textMeasureProvider().?;
|
|
156
|
+
|
|
157
|
+
// Registered id: the parsed face's advances — the mono pitch, not
|
|
158
|
+
// the sans advances the id-keyed estimator would guess.
|
|
159
|
+
const registered_width = provider.measureWidth(registered_font_id, 10.0, "Hello");
|
|
160
|
+
try std.testing.expectApproxEqAbs(@as(f32, 5 * 10.0 * canvas.mono_advance_em), registered_width, 0.001);
|
|
161
|
+
|
|
162
|
+
// Built-in ids keep the deterministic estimator exactly.
|
|
163
|
+
const sans_width = provider.measureWidth(canvas.default_sans_font_id, 10.0, "Hello");
|
|
164
|
+
try std.testing.expectApproxEqAbs(canvas.estimateTextWidthForFont(canvas.default_sans_font_id, "Hello", 10.0), sans_width, 0.0001);
|
|
165
|
+
|
|
166
|
+
// Tokens stamped through the runtime carry the provider, and the
|
|
167
|
+
// pointer is stable frame to frame.
|
|
168
|
+
const tokens = harness.runtime.tokensWithTextMeasure(.{});
|
|
169
|
+
try std.testing.expect(tokens.text_measure == provider);
|
|
170
|
+
try std.testing.expect(harness.runtime.tokensWithTextMeasure(.{}).text_measure == provider);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
test "registered faces answer the batched advances seam identically to per-prefix widths" {
|
|
174
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
175
|
+
defer harness.destroy(std.testing.allocator);
|
|
176
|
+
var app_state: RegistryApp = .{};
|
|
177
|
+
try harness.start(app_state.app());
|
|
178
|
+
|
|
179
|
+
const generation_before = canvas.textMeasureGeneration();
|
|
180
|
+
try harness.runtime.registerCanvasFont(registered_font_id, mono_bytes);
|
|
181
|
+
// Registration changes what the seam answers for the id: cached
|
|
182
|
+
// advances and retained wrap results must miss.
|
|
183
|
+
try std.testing.expect(canvas.textMeasureGeneration() > generation_before);
|
|
184
|
+
|
|
185
|
+
const provider = harness.runtime.textMeasureProvider().?;
|
|
186
|
+
try std.testing.expect(provider.measure_advances_fn != null);
|
|
187
|
+
|
|
188
|
+
// Batched advances sum to exactly the per-prefix width for both a
|
|
189
|
+
// registered id (face advances) and a built-in id (estimator
|
|
190
|
+
// advances) — the additive property the parity law rides on.
|
|
191
|
+
const text = "Hello 123 \xc3\xa9";
|
|
192
|
+
const ids = [_]canvas.FontId{ registered_font_id, canvas.default_sans_font_id };
|
|
193
|
+
for (ids) |font_id| {
|
|
194
|
+
var advances: [text.len]f32 = undefined;
|
|
195
|
+
try std.testing.expect(provider.measureAdvances(font_id, 10.0, text, &advances));
|
|
196
|
+
var sum: f32 = 0;
|
|
197
|
+
for (advances) |advance| sum += advance;
|
|
198
|
+
try std.testing.expectEqual(provider.measureWidth(font_id, 10.0, text), sum);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
fn installFontFixtureWidgets(harness: anytype) !void {
|
|
203
|
+
_ = try harness.runtime.createView(.{
|
|
204
|
+
.window_id = 1,
|
|
205
|
+
.label = "canvas",
|
|
206
|
+
.kind = .gpu_surface,
|
|
207
|
+
.frame = geometry.RectF.init(0, 0, 240, 140),
|
|
208
|
+
});
|
|
209
|
+
const controls = [_]canvas.Widget{.{
|
|
210
|
+
.id = 2,
|
|
211
|
+
.kind = .text,
|
|
212
|
+
.frame = geometry.RectF.init(10, 10, 220, 40),
|
|
213
|
+
.text = "Hello 123",
|
|
214
|
+
}};
|
|
215
|
+
var nodes: [2]canvas.WidgetLayoutNode = undefined;
|
|
216
|
+
const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &controls }, geometry.RectF.init(0, 0, 240, 140), &nodes);
|
|
217
|
+
_ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
fn fontFixtureTokens(runtime: *core.Runtime, font_id: canvas.FontId) canvas.DesignTokens {
|
|
221
|
+
var tokens = canvas.DesignTokens{};
|
|
222
|
+
tokens.typography.font_id = font_id;
|
|
223
|
+
return runtime.tokensWithTextMeasure(tokens);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
fn fontFixtureScreenshot(harness: anytype, allocator: std.mem.Allocator, font_id: canvas.FontId) ![]u8 {
|
|
227
|
+
_ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", fontFixtureTokens(&harness.runtime, font_id));
|
|
228
|
+
const pixel_size = try harness.runtime.canvasScreenshotPixelSize(1, "canvas", null);
|
|
229
|
+
const pixels = try allocator.alloc(u8, pixel_size.byte_len);
|
|
230
|
+
defer allocator.free(pixels);
|
|
231
|
+
const scratch = try allocator.alloc(u8, pixel_size.byte_len);
|
|
232
|
+
defer allocator.free(scratch);
|
|
233
|
+
const screenshot = try harness.runtime.renderCanvasScreenshot(1, "canvas", null, pixels, scratch);
|
|
234
|
+
return allocator.dupe(u8, screenshot.rgba8);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
test "a registered face renders pixel-identically on the present path and the reference path" {
|
|
238
|
+
const harness = try startedGpuHarness(std.testing.allocator);
|
|
239
|
+
defer harness.destroy(std.testing.allocator);
|
|
240
|
+
var app_state: RegistryApp = .{};
|
|
241
|
+
try harness.start(app_state.app());
|
|
242
|
+
try installFontFixtureWidgets(harness);
|
|
243
|
+
|
|
244
|
+
try harness.runtime.registerCanvasFont(registered_font_id, mono_bytes);
|
|
245
|
+
|
|
246
|
+
// Present path: the software pixel present (the packet fallback and
|
|
247
|
+
// GTK-class platforms' real path) planned through the same
|
|
248
|
+
// font-resource threading every present uses.
|
|
249
|
+
_ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", fontFixtureTokens(&harness.runtime, registered_font_id));
|
|
250
|
+
const pixel_size = try harness.runtime.canvasScreenshotPixelSize(1, "canvas", null);
|
|
251
|
+
const present_pixels = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
|
|
252
|
+
defer std.testing.allocator.free(present_pixels);
|
|
253
|
+
const present_scratch = try std.testing.allocator.alloc(u8, pixel_size.byte_len);
|
|
254
|
+
defer std.testing.allocator.free(present_scratch);
|
|
255
|
+
const clear_color = (canvas.DesignTokens{}).colors.background;
|
|
256
|
+
_ = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{ .full_repaint = true }, harness.runtime.canvasFrameScratchStorage(), present_pixels, present_scratch, clear_color);
|
|
257
|
+
|
|
258
|
+
// Reference path: the screenshot renderer, planned independently.
|
|
259
|
+
const reference = try fontFixtureScreenshot(harness, std.testing.allocator, registered_font_id);
|
|
260
|
+
defer std.testing.allocator.free(reference);
|
|
261
|
+
try std.testing.expectEqualSlices(u8, present_pixels[0..reference.len], reference);
|
|
262
|
+
|
|
263
|
+
// Cross-check against the face itself: the registered id carries the
|
|
264
|
+
// bundled mono bytes, so its pixels must be byte-identical to the
|
|
265
|
+
// built-in mono id — layout (advances) AND ink (outlines) both
|
|
266
|
+
// honored the registered face.
|
|
267
|
+
const builtin_mono = try fontFixtureScreenshot(harness, std.testing.allocator, canvas.default_mono_font_id);
|
|
268
|
+
defer std.testing.allocator.free(builtin_mono);
|
|
269
|
+
try std.testing.expectEqualSlices(u8, builtin_mono, reference);
|
|
270
|
+
|
|
271
|
+
// And it is genuinely a different face than the default sans — the
|
|
272
|
+
// registered id changed the pixels, not just the fingerprints.
|
|
273
|
+
const sans = try fontFixtureScreenshot(harness, std.testing.allocator, canvas.default_sans_font_id);
|
|
274
|
+
defer std.testing.allocator.free(sans);
|
|
275
|
+
try std.testing.expect(!std.mem.eql(u8, sans, reference));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// ------------------------------------------------ UiApp Options.fonts
|
|
279
|
+
|
|
280
|
+
const FontAppModel = struct { presses: u32 = 0 };
|
|
281
|
+
const FontAppMsg = union(enum) { press: void };
|
|
282
|
+
const FontApp = ui_app_model.UiApp(FontAppModel, FontAppMsg);
|
|
283
|
+
|
|
284
|
+
const font_app_canvas_label = "canvas";
|
|
285
|
+
|
|
286
|
+
fn fontAppUpdate(model: *FontAppModel, msg: FontAppMsg) void {
|
|
287
|
+
switch (msg) {
|
|
288
|
+
.press => model.presses += 1,
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
fn fontAppView(ui: *FontApp.Ui, model: *const FontAppModel) FontApp.Ui.Node {
|
|
293
|
+
_ = model;
|
|
294
|
+
return ui.column(.{ .gap = 8, .padding = 12 }, .{
|
|
295
|
+
ui.text(.{}, "Registered face body text"),
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
fn fontAppTokens(model: *const FontAppModel) canvas.DesignTokens {
|
|
300
|
+
_ = model;
|
|
301
|
+
var tokens = canvas.DesignTokens{};
|
|
302
|
+
tokens.typography.font_id = registered_font_id;
|
|
303
|
+
return tokens;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const font_app_views = [_]app_manifest.ShellView{
|
|
307
|
+
.{ .label = font_app_canvas_label, .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
|
|
308
|
+
};
|
|
309
|
+
const font_app_windows = [_]app_manifest.ShellWindow{.{
|
|
310
|
+
.label = "main",
|
|
311
|
+
.title = "Fonts",
|
|
312
|
+
.width = 240,
|
|
313
|
+
.height = 200,
|
|
314
|
+
.views = &font_app_views,
|
|
315
|
+
}};
|
|
316
|
+
const font_app_scene: app_manifest.ShellConfig = .{ .windows = &font_app_windows };
|
|
317
|
+
|
|
318
|
+
fn fontAppOptions(fonts: []const FontApp.FontRegistration) FontApp.Options {
|
|
319
|
+
return .{
|
|
320
|
+
.name = "ui-app-fonts",
|
|
321
|
+
.scene = font_app_scene,
|
|
322
|
+
.canvas_label = font_app_canvas_label,
|
|
323
|
+
.tokens_fn = fontAppTokens,
|
|
324
|
+
.update = fontAppUpdate,
|
|
325
|
+
.view = fontAppView,
|
|
326
|
+
.fonts = fonts,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
test "ui app registers declared fonts before the first view build" {
|
|
331
|
+
const harness = try TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(240, 200) });
|
|
332
|
+
defer harness.destroy(std.testing.allocator);
|
|
333
|
+
harness.null_platform.gpu_surfaces = true;
|
|
334
|
+
|
|
335
|
+
const fonts = [_]FontApp.FontRegistration{.{
|
|
336
|
+
.id = registered_font_id,
|
|
337
|
+
.name = "GeistMono-Regular.ttf",
|
|
338
|
+
.ttf = mono_bytes,
|
|
339
|
+
}};
|
|
340
|
+
const app_state = try std.testing.allocator.create(FontApp);
|
|
341
|
+
defer std.testing.allocator.destroy(app_state);
|
|
342
|
+
app_state.* = FontApp.init(std.testing.allocator, .{}, fontAppOptions(&fonts));
|
|
343
|
+
defer app_state.deinit();
|
|
344
|
+
const app = app_state.app();
|
|
345
|
+
try harness.start(app);
|
|
346
|
+
|
|
347
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
348
|
+
.label = font_app_canvas_label,
|
|
349
|
+
.size = geometry.SizeF.init(240, 200),
|
|
350
|
+
.scale_factor = 1,
|
|
351
|
+
.frame_index = 1,
|
|
352
|
+
.timestamp_ns = 1_000_000,
|
|
353
|
+
.nonblank = true,
|
|
354
|
+
} });
|
|
355
|
+
try std.testing.expect(app_state.installed);
|
|
356
|
+
try std.testing.expectEqual(@as(usize, 1), harness.runtime.registeredCanvasFontCount());
|
|
357
|
+
try std.testing.expect(harness.runtime.registeredCanvasFontFace(registered_font_id) != null);
|
|
358
|
+
try std.testing.expectEqual(@as(usize, 0), harness.runtime.dispatchErrors().len);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
test "ui app declared font failures are teaching errors, not crashes or silent fallbacks" {
|
|
362
|
+
const harness = try TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(240, 200) });
|
|
363
|
+
defer harness.destroy(std.testing.allocator);
|
|
364
|
+
harness.null_platform.gpu_surfaces = true;
|
|
365
|
+
|
|
366
|
+
const fonts = [_]FontApp.FontRegistration{.{
|
|
367
|
+
.id = registered_font_id,
|
|
368
|
+
.name = "Broken.ttf",
|
|
369
|
+
.ttf = mono_bytes[0..512],
|
|
370
|
+
}};
|
|
371
|
+
const app_state = try std.testing.allocator.create(FontApp);
|
|
372
|
+
defer std.testing.allocator.destroy(app_state);
|
|
373
|
+
app_state.* = FontApp.init(std.testing.allocator, .{}, fontAppOptions(&fonts));
|
|
374
|
+
defer app_state.deinit();
|
|
375
|
+
const app = app_state.app();
|
|
376
|
+
try harness.start(app);
|
|
377
|
+
|
|
378
|
+
// Production loops degrade dispatch errors into the loud error
|
|
379
|
+
// channel (error event + teaching log) instead of dying; mirror that
|
|
380
|
+
// policy here (the harness default propagates so capacity bugs fail
|
|
381
|
+
// tests).
|
|
382
|
+
harness.runtime.dispatch_error_policy = .degrade;
|
|
383
|
+
|
|
384
|
+
// The installing frame surfaces the failure through the dispatch
|
|
385
|
+
// error channel and the app stays alive.
|
|
386
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
387
|
+
.label = font_app_canvas_label,
|
|
388
|
+
.size = geometry.SizeF.init(240, 200),
|
|
389
|
+
.scale_factor = 1,
|
|
390
|
+
.frame_index = 1,
|
|
391
|
+
.timestamp_ns = 1_000_000,
|
|
392
|
+
.nonblank = true,
|
|
393
|
+
} });
|
|
394
|
+
try std.testing.expectEqual(@as(usize, 0), harness.runtime.registeredCanvasFontCount());
|
|
395
|
+
const first_errors = harness.runtime.dispatchErrorTotal();
|
|
396
|
+
try std.testing.expect(first_errors > 0);
|
|
397
|
+
|
|
398
|
+
// Registration does not retry every frame: a second frame installs
|
|
399
|
+
// the app without re-raising a registration error per frame.
|
|
400
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
|
|
401
|
+
.label = font_app_canvas_label,
|
|
402
|
+
.size = geometry.SizeF.init(240, 200),
|
|
403
|
+
.scale_factor = 1,
|
|
404
|
+
.frame_index = 2,
|
|
405
|
+
.timestamp_ns = 2_000_000,
|
|
406
|
+
.nonblank = true,
|
|
407
|
+
} });
|
|
408
|
+
try std.testing.expect(app_state.installed);
|
|
409
|
+
try std.testing.expectEqual(first_errors, harness.runtime.dispatchErrorTotal());
|
|
410
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
//! Runtime canvas font registry: TrueType faces apps register at startup
|
|
2
|
+
//! under a caller-chosen `FontId` (the image-registry spirit — store the
|
|
3
|
+
//! id in the model/tokens, no handles to leak) and reference from
|
|
4
|
+
//! everywhere a font id rides: `TypographyTokenOverrides.font_id` /
|
|
5
|
+
//! `mono_font_id`, draw commands, glyph atlas keys, and render
|
|
6
|
+
//! fingerprints.
|
|
7
|
+
//!
|
|
8
|
+
//! Validation is loud and registration-time only: the bytes must parse as
|
|
9
|
+
//! a TrueType face (`canvas.font_ttf.Face.parse`) under the registry
|
|
10
|
+
//! bounds, or registration fails with a recoverable error — a registered
|
|
11
|
+
//! id therefore ALWAYS resolves at render time, and the only per-glyph
|
|
12
|
+
//! fallback is the same notdef block built-in faces use for codepoints a
|
|
13
|
+
//! face does not cover. `canvas.font_ttf.parseFailureReason` turns a
|
|
14
|
+
//! rejected file into a teaching sentence for callers that know the
|
|
15
|
+
//! file's name (UiApp's `fonts` option does this).
|
|
16
|
+
//!
|
|
17
|
+
//! Both renderers resolve registered ids exactly like built-ins:
|
|
18
|
+
//! - The frame planner threads the registered set into
|
|
19
|
+
//! `CanvasFrameOptions.font_resources` for every view, so the CPU
|
|
20
|
+
//! reference paths (presentation, screenshots, goldens) ink the
|
|
21
|
+
//! registered outlines.
|
|
22
|
+
//! - Platforms that measure and draw text host-side (`measure_text_fn`
|
|
23
|
+
//! present — macOS) receive the raw bytes through
|
|
24
|
+
//! `registerGpuSurfaceFont` at registration time, before any layout can
|
|
25
|
+
//! measure the id, so host measurement and packet text drawing resolve
|
|
26
|
+
//! the same face. A host that measures text but cannot learn a
|
|
27
|
+
//! registered face fails the registration loudly
|
|
28
|
+
//! (`error.FontHostRegistrationUnsupported`) instead of silently
|
|
29
|
+
//! substituting the default family.
|
|
30
|
+
//! - Platforms without host-side text measure through the runtime's
|
|
31
|
+
//! font-aware provider: registered ids charge the parsed face's own
|
|
32
|
+
//! `cmap`/`hmtx` advances (`canvas.estimateTextWidthForFace`), built-in
|
|
33
|
+
//! ids keep the deterministic estimator — measured layout and reference
|
|
34
|
+
//! ink stay in lockstep.
|
|
35
|
+
//!
|
|
36
|
+
//! Registration is permanent for the runtime's lifetime: glyph atlas and
|
|
37
|
+
//! text-layout caches key glyphs by (font id, glyph id) with no content
|
|
38
|
+
//! fingerprint, so replacing an id's bytes would serve stale glyphs from
|
|
39
|
+
//! retained caches. Re-using a registered id fails with
|
|
40
|
+
//! `error.FontIdInUse`; there is deliberately no unregister.
|
|
41
|
+
//!
|
|
42
|
+
//! Capacities follow `canvas_limits`: `max_registered_canvas_fonts` slots
|
|
43
|
+
//! of `max_registered_canvas_font_bytes` each, overflow is
|
|
44
|
+
//! `error.FontRegistryFull` / `error.FontTooLarge` — never silent.
|
|
45
|
+
|
|
46
|
+
const std = @import("std");
|
|
47
|
+
const canvas = @import("canvas");
|
|
48
|
+
const canvas_frame_module = @import("canvas_frame.zig");
|
|
49
|
+
const canvas_limits = @import("canvas_limits.zig");
|
|
50
|
+
|
|
51
|
+
pub const max_registered_canvas_fonts = canvas_limits.max_registered_canvas_fonts;
|
|
52
|
+
pub const max_registered_canvas_font_bytes = canvas_limits.max_registered_canvas_font_bytes;
|
|
53
|
+
|
|
54
|
+
/// One registered font's metadata; the bytes live in the runtime's slot
|
|
55
|
+
/// pool and the parsed face view in the parallel face array at the same
|
|
56
|
+
/// index.
|
|
57
|
+
pub const CanvasFontEntry = struct {
|
|
58
|
+
id: canvas.FontId = 0,
|
|
59
|
+
byte_len: usize = 0,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/// Placeholder measure fn for the runtime's font-aware provider field
|
|
63
|
+
/// before any font registers (the provider is never handed out in that
|
|
64
|
+
/// state — `textMeasureProvider` returns it only when fonts exist — but
|
|
65
|
+
/// the field needs a well-defined default for in-place construction).
|
|
66
|
+
pub fn unboundCanvasFontMeasure(context: ?*anyopaque, font_id: canvas.FontId, size: f32, text: []const u8) f32 {
|
|
67
|
+
_ = context;
|
|
68
|
+
return canvas.estimateTextWidthForFont(font_id, text, size);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
pub fn RuntimeCanvasFonts(comptime Runtime: type) type {
|
|
72
|
+
return struct {
|
|
73
|
+
/// Register the TrueType face in `ttf` under `id` (an app-chosen
|
|
74
|
+
/// id at or above `canvas.min_registered_font_id`). The runtime
|
|
75
|
+
/// copies the bytes, so the caller's buffer is free when this
|
|
76
|
+
/// returns; the id resolves everywhere a `FontId` rides, for the
|
|
77
|
+
/// rest of the runtime's lifetime. Startup-shaped: register before
|
|
78
|
+
/// (or on) the installing frame so the first layout already
|
|
79
|
+
/// measures with the face.
|
|
80
|
+
///
|
|
81
|
+
/// Errors — all at registration, never at render time:
|
|
82
|
+
/// `error.InvalidFontId` (id 0 is the "inherit run font"
|
|
83
|
+
/// sentinel), `error.ReservedFontId` (below
|
|
84
|
+
/// `canvas.min_registered_font_id` — reserved for built-in
|
|
85
|
+
/// faces), `error.FontTooLarge` (over the per-slot bound
|
|
86
|
+
/// `canvas_limits.max_registered_canvas_font_bytes`),
|
|
87
|
+
/// `error.FontIdInUse` (ids are permanent; see the module doc),
|
|
88
|
+
/// `error.FontRegistryFull` (all
|
|
89
|
+
/// `canvas_limits.max_registered_canvas_fonts` slots hold other
|
|
90
|
+
/// ids), `error.FontParseFailed` (not a parseable TrueType face —
|
|
91
|
+
/// `canvas.font_ttf.parseFailureReason(ttf)` names what is wrong),
|
|
92
|
+
/// `error.FontHostRegistrationUnsupported` (the platform measures
|
|
93
|
+
/// and draws text host-side but has no font registration seam, so
|
|
94
|
+
/// the face could not be honored pixel-honestly).
|
|
95
|
+
pub fn registerCanvasFont(self: *Runtime, id: canvas.FontId, ttf: []const u8) anyerror!void {
|
|
96
|
+
if (id == 0) return error.InvalidFontId;
|
|
97
|
+
if (id < canvas.min_registered_font_id) return error.ReservedFontId;
|
|
98
|
+
if (ttf.len > max_registered_canvas_font_bytes) return error.FontTooLarge;
|
|
99
|
+
if (findCanvasFontIndex(self, id) != null) return error.FontIdInUse;
|
|
100
|
+
if (self.canvas_font_count >= max_registered_canvas_fonts) return error.FontRegistryFull;
|
|
101
|
+
|
|
102
|
+
const index = self.canvas_font_count;
|
|
103
|
+
const pooled = self.canvas_font_bytes[index][0..ttf.len];
|
|
104
|
+
@memcpy(pooled, ttf);
|
|
105
|
+
const face = canvas.font_ttf.Face.parse(pooled) catch return error.FontParseFailed;
|
|
106
|
+
|
|
107
|
+
// Host sync BEFORE committing the slot: platforms with
|
|
108
|
+
// host-side text (measure_text_fn) must learn the face or the
|
|
109
|
+
// whole registration fails — a committed id the host cannot
|
|
110
|
+
// resolve would measure and draw as the default family, the
|
|
111
|
+
// exact silent fallback this seam forbids. Platforms without
|
|
112
|
+
// host-side text may lack the seam (`UnsupportedService`):
|
|
113
|
+
// the engine measures with the parsed face and inks it
|
|
114
|
+
// through the reference renderer, so nothing is lost.
|
|
115
|
+
self.options.platform.services.registerGpuSurfaceFont(.{ .id = id, .ttf = pooled }) catch |err| switch (err) {
|
|
116
|
+
error.UnsupportedService => {
|
|
117
|
+
if (self.options.platform.services.measure_text_fn != null) return error.FontHostRegistrationUnsupported;
|
|
118
|
+
},
|
|
119
|
+
else => return err,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
self.canvas_font_faces[index] = face;
|
|
123
|
+
self.canvas_font_entries[index] = .{ .id = id, .byte_len = ttf.len };
|
|
124
|
+
self.canvas_font_count = index + 1;
|
|
125
|
+
// Bind the font-aware measure provider on first registration.
|
|
126
|
+
// The runtime address is stable from here on (registration
|
|
127
|
+
// happens through a settled *Runtime), so the provider
|
|
128
|
+
// pointer stamped into tokens stays valid for the runtime's
|
|
129
|
+
// lifetime, matching the platform provider's contract.
|
|
130
|
+
if (index == 0) {
|
|
131
|
+
self.canvas_font_measure_provider = .{
|
|
132
|
+
.context = self,
|
|
133
|
+
.measure_fn = canvasFontMeasure,
|
|
134
|
+
.measure_advances_fn = canvasFontMeasureAdvances,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
noteCanvasFontsChanged(self);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/// The registered set as the `ReferenceFont` slice both renderers
|
|
141
|
+
/// consume, rebuilt into runtime scratch (faces are borrowed from
|
|
142
|
+
/// the slot pool; with no unregister they stay valid for the
|
|
143
|
+
/// runtime's lifetime).
|
|
144
|
+
pub fn registeredCanvasFonts(self: *Runtime) []const canvas.ReferenceFont {
|
|
145
|
+
for (self.canvas_font_entries[0..self.canvas_font_count], 0..) |entry, index| {
|
|
146
|
+
self.canvas_font_resources_scratch[index] = .{
|
|
147
|
+
.id = entry.id,
|
|
148
|
+
.face = &self.canvas_font_faces[index],
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
return self.canvas_font_resources_scratch[0..self.canvas_font_count];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/// The parsed face registered under `id`, or null when `id` is
|
|
155
|
+
/// not registered.
|
|
156
|
+
pub fn registeredCanvasFontFace(self: *const Runtime, id: canvas.FontId) ?*const canvas.font_ttf.Face {
|
|
157
|
+
const index = findCanvasFontIndex(self, id) orelse return null;
|
|
158
|
+
return &self.canvas_font_faces[index];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
pub fn registeredCanvasFontCount(self: *const Runtime) usize {
|
|
162
|
+
return self.canvas_font_count;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// Measure fn for the runtime's font-aware provider (installed on
|
|
166
|
+
/// platforms without host-side text measurement): registered ids
|
|
167
|
+
/// charge the parsed face's own advances so measured layout
|
|
168
|
+
/// matches the outlines the reference renderer inks; every other
|
|
169
|
+
/// id keeps the deterministic estimator, bit-identical to the
|
|
170
|
+
/// provider-less path.
|
|
171
|
+
fn canvasFontMeasure(context: ?*anyopaque, font_id: canvas.FontId, size: f32, text: []const u8) f32 {
|
|
172
|
+
const runtime: *Runtime = @ptrCast(@alignCast(context));
|
|
173
|
+
if (findCanvasFontIndex(runtime, font_id)) |index| {
|
|
174
|
+
return canvas.estimateTextWidthForFace(&runtime.canvas_font_faces[index], text, size);
|
|
175
|
+
}
|
|
176
|
+
return canvas.estimateTextWidthForFont(font_id, text, size);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/// Batched twin of `canvasFontMeasure`: per-cluster advances from
|
|
180
|
+
/// the same face (or estimator) tables, cluster advance at the
|
|
181
|
+
/// lead byte and 0 at continuation bytes. Both underlying width
|
|
182
|
+
/// functions are plain per-cluster sums, so a slice's width is
|
|
183
|
+
/// exactly the sum of these advances — line breaks from the
|
|
184
|
+
/// batched path are bit-identical to the per-prefix path, and the
|
|
185
|
+
/// registered-face provider drops from O(L²) cluster walks per
|
|
186
|
+
/// line to O(L) per run like the host providers.
|
|
187
|
+
fn canvasFontMeasureAdvances(context: ?*anyopaque, font_id: canvas.FontId, size: f32, text: []const u8, advances: []f32) bool {
|
|
188
|
+
const runtime: *Runtime = @ptrCast(@alignCast(context));
|
|
189
|
+
const face = if (findCanvasFontIndex(runtime, font_id)) |index| &runtime.canvas_font_faces[index] else null;
|
|
190
|
+
var index: usize = 0;
|
|
191
|
+
while (index < text.len) {
|
|
192
|
+
const next = @min(text.len, index + canvas.utf8SequenceLength(text[index]));
|
|
193
|
+
advances[index] = if (face) |value|
|
|
194
|
+
canvas.estimateTextWidthForFace(value, text[index..next], size)
|
|
195
|
+
else
|
|
196
|
+
canvas.estimateTextAdvanceForBytes(font_id, text[index..next], size);
|
|
197
|
+
@memset(advances[index + 1 .. next], 0);
|
|
198
|
+
index = next;
|
|
199
|
+
}
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
fn findCanvasFontIndex(self: *const Runtime, id: canvas.FontId) ?usize {
|
|
204
|
+
for (self.canvas_font_entries[0..self.canvas_font_count], 0..) |entry, index| {
|
|
205
|
+
if (entry.id == id) return index;
|
|
206
|
+
}
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// A face joined the registry: force every gpu_surface view to
|
|
211
|
+
/// re-render its next frame (text referencing the id may already
|
|
212
|
+
/// be retained) and request frames so the repaint is not gated on
|
|
213
|
+
/// other input — the image-registry choreography.
|
|
214
|
+
fn noteCanvasFontsChanged(self: *Runtime) void {
|
|
215
|
+
// A new face changes what the measurement seam answers for
|
|
216
|
+
// its id (host providers just learned the face; the engine
|
|
217
|
+
// provider now charges its real advances): invalidate every
|
|
218
|
+
// cached advance batch and retained wrap result.
|
|
219
|
+
canvas.bumpTextMeasureGeneration();
|
|
220
|
+
const frame_methods = canvas_frame_module.RuntimeCanvasFrames(Runtime);
|
|
221
|
+
for (self.views[0..self.view_count], 0..) |*view, index| {
|
|
222
|
+
if (!view.open or view.kind != .gpu_surface) continue;
|
|
223
|
+
view.presented_canvas_valid = false;
|
|
224
|
+
self.invalidateFor(.state, view.frame);
|
|
225
|
+
frame_methods.requestCanvasFrameForView(self, index) catch {};
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
}
|