@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,279 @@
|
|
|
1
|
+
//! Batched text-measurement advances: one provider call per text RUN.
|
|
2
|
+
//!
|
|
3
|
+
//! Line breaking used to measure the growing line prefix once per UTF-8
|
|
4
|
+
//! cluster through `TextMeasureProvider.measureWidth` — O(L²) bytes
|
|
5
|
+
//! measured per line, one host round-trip per cluster on platforms whose
|
|
6
|
+
//! provider crosses an FFI boundary. This module turns that into ONE
|
|
7
|
+
//! batched provider call per run: the host returns per-cluster advances
|
|
8
|
+
//! for the whole run, and every consumer breaks lines (or sums slice
|
|
9
|
+
//! widths) from cumulative advances exactly the way the deterministic
|
|
10
|
+
//! estimator path always has.
|
|
11
|
+
//!
|
|
12
|
+
//! Advance layout contract (shared with `PlatformServices.
|
|
13
|
+
//! measure_text_advances_fn` and `TextMeasureProvider.measure_advances_fn`):
|
|
14
|
+
//! the output array is per BYTE, `text.len` entries. The advance of the
|
|
15
|
+
//! UTF-8 cluster starting at byte `i` is stored at index `i`; the
|
|
16
|
+
//! cluster's continuation bytes hold exactly 0. A slice width is
|
|
17
|
+
//! therefore the plain sum of its byte range (adding the interleaved
|
|
18
|
+
//! zeros is an exact f32 identity), and a cluster walk reads one entry
|
|
19
|
+
//! per lead byte — no cluster-index bookkeeping on either side.
|
|
20
|
+
//!
|
|
21
|
+
//! Caching: fetched runs are retained in a bounded, least-recently-used
|
|
22
|
+
//! keyed store so steady-state rebuilds of unchanged text skip the
|
|
23
|
+
//! provider entirely. Keys cover the text BYTES (content hash, not
|
|
24
|
+
//! pointer identity — retained text storage rewrites bytes in place),
|
|
25
|
+
//! the font id, the size, the provider identity (context + function),
|
|
26
|
+
//! and the global measure generation below. Runs longer than a cache
|
|
27
|
+
//! slot use a single uncached scratch slot (still one batched call per
|
|
28
|
+
//! fetch); runs longer than the scratch fall back to the caller's
|
|
29
|
+
//! unbatched path.
|
|
30
|
+
//!
|
|
31
|
+
//! Generation: `bumpTextMeasureGeneration` invalidates every cached
|
|
32
|
+
//! advance (and the retained wrap results keyed on the same generation
|
|
33
|
+
//! in text_spans.zig). The runtime bumps it when a font registers (the
|
|
34
|
+
//! host just learned a new face for an id), when the platform appearance
|
|
35
|
+
//! flips (theme-derived typography may re-resolve host fonts), and when
|
|
36
|
+
//! a runtime constructs (so a recycled provider context pointer from a
|
|
37
|
+
//! previous runtime in the same process can never serve stale advances).
|
|
38
|
+
//!
|
|
39
|
+
//! Thread model: storage is threadlocal, matching the runtime's
|
|
40
|
+
//! single-threaded frame path and the other threadlocal planner scratch
|
|
41
|
+
//! in this codebase (`text_layout_cache.zig`'s probe tables, the canvas
|
|
42
|
+
//! packet baseline scratch). Two runtimes on two threads simply keep
|
|
43
|
+
//! independent caches. The generation counter is a process-wide atomic
|
|
44
|
+
//! so an invalidation on the runtime loop thread is seen everywhere.
|
|
45
|
+
//!
|
|
46
|
+
//! Lifetime rule for returned slices: a returned advances slice aliases
|
|
47
|
+
//! cache (or scratch) storage and is only valid until the NEXT
|
|
48
|
+
//! `textRunAdvances` call on the same thread — consume it immediately;
|
|
49
|
+
//! never hold it across another fetch.
|
|
50
|
+
|
|
51
|
+
const std = @import("std");
|
|
52
|
+
const builtin = @import("builtin");
|
|
53
|
+
const canvas = @import("root.zig");
|
|
54
|
+
const text_metrics = @import("text_metrics.zig");
|
|
55
|
+
|
|
56
|
+
const FontId = canvas.FontId;
|
|
57
|
+
const TextMeasureProvider = text_metrics.TextMeasureProvider;
|
|
58
|
+
|
|
59
|
+
/// Per-entry advance capacity in text bytes. Sized for real paragraph
|
|
60
|
+
/// runs (chat messages, markdown blocks); longer runs use the oversize
|
|
61
|
+
/// scratch slot below.
|
|
62
|
+
pub const max_cached_advance_run_bytes: usize = 2048;
|
|
63
|
+
|
|
64
|
+
/// Retained entries. 256 slots of 2048 f32 advances is 2 MiB of
|
|
65
|
+
/// threadlocal storage — the same order as the runtime's other
|
|
66
|
+
/// fixed-capacity pools. Capacity must exceed the hot set of runs that
|
|
67
|
+
/// FETCH per rebuild (wrapping paragraphs and elided labels on the
|
|
68
|
+
/// mounted screen; per-frame bounds only peek, see
|
|
69
|
+
/// `cachedTextRunAdvances`), because least-recently-used eviction
|
|
70
|
+
/// degrades to a 100% miss rate the moment the steadily revisited set
|
|
71
|
+
/// outgrows the slots — measured live as a plan-stage regression before
|
|
72
|
+
/// the peek/fetch split.
|
|
73
|
+
pub const advance_cache_capacity: usize = 256;
|
|
74
|
+
|
|
75
|
+
/// Oversize scratch: one slot covering runs up to the per-view text
|
|
76
|
+
/// budget (`canvas_limits.max_canvas_text_bytes_per_view`). A run this
|
|
77
|
+
/// long still gets ONE batched call per fetch; it just is not retained
|
|
78
|
+
/// across other fetches.
|
|
79
|
+
pub const max_batched_advance_run_bytes: usize = 32768;
|
|
80
|
+
|
|
81
|
+
/// Process-wide invalidation stamp for everything keyed on measured
|
|
82
|
+
/// text: cached advances here and retained wrap results downstream.
|
|
83
|
+
/// Starts at 1 so a zero-initialized key can never match.
|
|
84
|
+
///
|
|
85
|
+
/// The stamp is atomic so a bump on the runtime loop thread is seen by
|
|
86
|
+
/// every thread's threadlocal cache — except on wasm32, which has no
|
|
87
|
+
/// 64-bit atomics to compile and no second thread to synchronize with
|
|
88
|
+
/// (the docs live preview runs single-threaded by construction), so it
|
|
89
|
+
/// uses a plain counter behind the same accessors.
|
|
90
|
+
const generation_needs_atomic = !builtin.target.cpu.arch.isWasm();
|
|
91
|
+
var text_measure_generation: if (generation_needs_atomic) std.atomic.Value(u64) else u64 =
|
|
92
|
+
if (generation_needs_atomic) std.atomic.Value(u64).init(1) else 1;
|
|
93
|
+
|
|
94
|
+
pub fn textMeasureGeneration() u64 {
|
|
95
|
+
if (generation_needs_atomic) return text_measure_generation.load(.monotonic);
|
|
96
|
+
return text_measure_generation;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
pub fn bumpTextMeasureGeneration() void {
|
|
100
|
+
if (generation_needs_atomic) {
|
|
101
|
+
_ = text_measure_generation.fetchAdd(1, .monotonic);
|
|
102
|
+
} else {
|
|
103
|
+
text_measure_generation += 1;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// Cache key. `hash` covers the text bytes; the rest pins the exact
|
|
108
|
+
/// measurement context. Float size is compared by bit pattern (sizes are
|
|
109
|
+
/// finite positive reals here; bitwise equality is the honest identity).
|
|
110
|
+
const AdvanceKey = struct {
|
|
111
|
+
hash: u64 = 0,
|
|
112
|
+
text_len: usize = 0,
|
|
113
|
+
font_id: FontId = 0,
|
|
114
|
+
size_bits: u32 = 0,
|
|
115
|
+
provider_context: usize = 0,
|
|
116
|
+
provider_fn: usize = 0,
|
|
117
|
+
generation: u64 = 0,
|
|
118
|
+
used: bool = false,
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const AdvanceEntry = struct {
|
|
122
|
+
key: AdvanceKey = .{},
|
|
123
|
+
last_used: u64 = 0,
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
threadlocal var advance_entries: [advance_cache_capacity]AdvanceEntry = @splat(.{});
|
|
127
|
+
threadlocal var advance_storage: [advance_cache_capacity][max_cached_advance_run_bytes]f32 = undefined;
|
|
128
|
+
threadlocal var oversize_key: AdvanceKey = .{};
|
|
129
|
+
threadlocal var oversize_storage: [max_batched_advance_run_bytes]f32 = undefined;
|
|
130
|
+
/// Monotonic use tick driving least-recently-used eviction.
|
|
131
|
+
threadlocal var advance_use_tick: u64 = 0;
|
|
132
|
+
/// Fetch statistics for tests and the render benchmark: how many
|
|
133
|
+
/// batched provider calls actually happened vs how many lookups were
|
|
134
|
+
/// answered from retained entries.
|
|
135
|
+
threadlocal var advance_fetch_count: u64 = 0;
|
|
136
|
+
threadlocal var advance_hit_count: u64 = 0;
|
|
137
|
+
|
|
138
|
+
pub fn textAdvanceFetchCount() u64 {
|
|
139
|
+
return advance_fetch_count;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
pub fn textAdvanceHitCount() u64 {
|
|
143
|
+
return advance_hit_count;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
fn advanceKeyFor(provider: *const TextMeasureProvider, font_id: FontId, size: f32, text: []const u8) AdvanceKey {
|
|
147
|
+
return .{
|
|
148
|
+
.hash = std.hash.Wyhash.hash(0x746578746164, text),
|
|
149
|
+
.text_len = text.len,
|
|
150
|
+
.font_id = font_id,
|
|
151
|
+
.size_bits = @bitCast(size),
|
|
152
|
+
.provider_context = @intFromPtr(provider.context),
|
|
153
|
+
.provider_fn = if (provider.measure_advances_fn) |advances_fn| @intFromPtr(advances_fn) else 0,
|
|
154
|
+
.generation = textMeasureGeneration(),
|
|
155
|
+
.used = true,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
fn advanceKeysEqual(a: AdvanceKey, b: AdvanceKey) bool {
|
|
160
|
+
return a.used and b.used and
|
|
161
|
+
a.hash == b.hash and
|
|
162
|
+
a.text_len == b.text_len and
|
|
163
|
+
a.font_id == b.font_id and
|
|
164
|
+
a.size_bits == b.size_bits and
|
|
165
|
+
a.provider_context == b.provider_context and
|
|
166
|
+
a.provider_fn == b.provider_fn and
|
|
167
|
+
a.generation == b.generation;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/// A fetched advances array is only trustworthy if every entry is a
|
|
171
|
+
/// finite, non-negative number: layout accumulates them into widths, so
|
|
172
|
+
/// a single NaN would poison every break decision after it. Reject the
|
|
173
|
+
/// whole batch and let the caller fall back to the unbatched seam (which
|
|
174
|
+
/// carries its own per-call fallback to the estimator).
|
|
175
|
+
fn advancesValid(advances: []const f32) bool {
|
|
176
|
+
for (advances) |advance| {
|
|
177
|
+
if (!(advance >= 0) or !std.math.isFinite(advance)) return false;
|
|
178
|
+
}
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/// Per-byte cluster advances for the whole run `text`, from the retained
|
|
183
|
+
/// cache or a single batched provider call. Null when the provider has
|
|
184
|
+
/// no batched entry, the run exceeds the scratch bound, or the host
|
|
185
|
+
/// declined (invalid UTF-8, unresolvable font) — callers keep their
|
|
186
|
+
/// existing per-prefix measurement path for those.
|
|
187
|
+
///
|
|
188
|
+
/// The returned slice aliases threadlocal storage: consume it before the
|
|
189
|
+
/// next call to this function.
|
|
190
|
+
pub fn textRunAdvances(provider: *const TextMeasureProvider, font_id: FontId, size: f32, text: []const u8) ?[]const f32 {
|
|
191
|
+
if (provider.measure_advances_fn == null) return null;
|
|
192
|
+
if (text.len == 0) return &.{};
|
|
193
|
+
if (text.len > max_batched_advance_run_bytes) return null;
|
|
194
|
+
|
|
195
|
+
const key = advanceKeyFor(provider, font_id, size, text);
|
|
196
|
+
advance_use_tick += 1;
|
|
197
|
+
|
|
198
|
+
if (text.len > max_cached_advance_run_bytes) {
|
|
199
|
+
// Oversize runs: one uncached scratch slot, memoized against
|
|
200
|
+
// itself so repeated fetches of the same long run (the line
|
|
201
|
+
// breaker, then elision, then bounds) still pay one call.
|
|
202
|
+
if (advanceKeysEqual(oversize_key, key)) {
|
|
203
|
+
advance_hit_count += 1;
|
|
204
|
+
return oversize_storage[0..text.len];
|
|
205
|
+
}
|
|
206
|
+
oversize_key = .{};
|
|
207
|
+
advance_fetch_count += 1;
|
|
208
|
+
if (!provider.measureAdvances(font_id, size, text, oversize_storage[0..text.len])) return null;
|
|
209
|
+
if (!advancesValid(oversize_storage[0..text.len])) return null;
|
|
210
|
+
oversize_key = key;
|
|
211
|
+
return oversize_storage[0..text.len];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
var victim: usize = 0;
|
|
215
|
+
var victim_tick: u64 = std.math.maxInt(u64);
|
|
216
|
+
for (&advance_entries, 0..) |*entry, index| {
|
|
217
|
+
if (advanceKeysEqual(entry.key, key)) {
|
|
218
|
+
entry.last_used = advance_use_tick;
|
|
219
|
+
advance_hit_count += 1;
|
|
220
|
+
return advance_storage[index][0..text.len];
|
|
221
|
+
}
|
|
222
|
+
// Unused slots evict first (tick 0), then the least recently
|
|
223
|
+
// used entry — honest bounded retention, no clock heuristics.
|
|
224
|
+
const tick = if (entry.key.used) entry.last_used else 0;
|
|
225
|
+
if (tick < victim_tick) {
|
|
226
|
+
victim_tick = tick;
|
|
227
|
+
victim = index;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
advance_entries[victim].key = .{};
|
|
232
|
+
advance_fetch_count += 1;
|
|
233
|
+
if (!provider.measureAdvances(font_id, size, text, advance_storage[victim][0..text.len])) return null;
|
|
234
|
+
if (!advancesValid(advance_storage[victim][0..text.len])) return null;
|
|
235
|
+
advance_entries[victim] = .{ .key = key, .last_used = advance_use_tick };
|
|
236
|
+
return advance_storage[victim][0..text.len];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/// Peek: the run's advances IF already retained (or sitting in the
|
|
240
|
+
/// oversize slot), never fetching. Per-frame consumers that would
|
|
241
|
+
/// otherwise fetch hundreds of retained single-line runs (line-bounds
|
|
242
|
+
/// measurement in the frame planner) use this so a run only ever costs
|
|
243
|
+
/// a batched host call when something that actually breaks lines
|
|
244
|
+
/// fetched it — measured live, fetching from the bounds path turned one
|
|
245
|
+
/// memoized host width per run into one full host shape per run per
|
|
246
|
+
/// frame once the retained set outgrew the cache.
|
|
247
|
+
pub fn cachedTextRunAdvances(provider: *const TextMeasureProvider, font_id: FontId, size: f32, text: []const u8) ?[]const f32 {
|
|
248
|
+
if (provider.measure_advances_fn == null) return null;
|
|
249
|
+
if (text.len == 0) return &.{};
|
|
250
|
+
if (text.len > max_batched_advance_run_bytes) return null;
|
|
251
|
+
const key = advanceKeyFor(provider, font_id, size, text);
|
|
252
|
+
if (text.len > max_cached_advance_run_bytes) {
|
|
253
|
+
if (advanceKeysEqual(oversize_key, key)) {
|
|
254
|
+
advance_hit_count += 1;
|
|
255
|
+
return oversize_storage[0..text.len];
|
|
256
|
+
}
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
for (&advance_entries, 0..) |*entry, index| {
|
|
260
|
+
if (advanceKeysEqual(entry.key, key)) {
|
|
261
|
+
advance_use_tick += 1;
|
|
262
|
+
entry.last_used = advance_use_tick;
|
|
263
|
+
advance_hit_count += 1;
|
|
264
|
+
return advance_storage[index][0..text.len];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/// Width of `advances[start..end)` — the batched twin of measuring the
|
|
271
|
+
/// slice through the provider. Summing in byte order visits the same
|
|
272
|
+
/// cluster advances in the same order a per-cluster accumulation would,
|
|
273
|
+
/// and the interleaved continuation zeros are exact f32 identities, so
|
|
274
|
+
/// the result is bit-identical to the cluster-order sum.
|
|
275
|
+
pub fn advanceSliceWidth(advances: []const f32, start: usize, end: usize) f32 {
|
|
276
|
+
var width: f32 = 0;
|
|
277
|
+
for (advances[start..end]) |advance| width += advance;
|
|
278
|
+
return width;
|
|
279
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
const canvas = @import("root.zig");
|
|
3
|
+
const font_ttf = @import("font_ttf.zig");
|
|
4
|
+
const text_atlas = @import("text_atlas.zig");
|
|
5
|
+
const text_interaction = @import("text_interaction.zig");
|
|
6
|
+
|
|
7
|
+
const FontId = canvas.FontId;
|
|
8
|
+
pub const Glyph = text_atlas.Glyph;
|
|
9
|
+
const default_sans_font_id = canvas.default_sans_font_id;
|
|
10
|
+
const default_mono_font_id = canvas.default_mono_font_id;
|
|
11
|
+
const utf8SequenceLength = text_interaction.utf8SequenceLength;
|
|
12
|
+
|
|
13
|
+
/// Injected text measurement. The engine stays pure: platforms provide a
|
|
14
|
+
/// context pointer plus a function that returns the width of a single-line
|
|
15
|
+
/// run of `text` at `size` for `font_id`, shaped with the same font
|
|
16
|
+
/// resolution the presentation layer draws with. When no provider is
|
|
17
|
+
/// installed every consumer falls back to the deterministic estimator
|
|
18
|
+
/// below, keeping the reference renderer and golden tests bit-identical.
|
|
19
|
+
pub const TextMeasureProvider = struct {
|
|
20
|
+
context: ?*anyopaque = null,
|
|
21
|
+
measure_fn: *const fn (context: ?*anyopaque, font_id: FontId, size: f32, text: []const u8) f32,
|
|
22
|
+
/// Optional batched measurement: fill `advances` (per byte, `text.len`
|
|
23
|
+
/// entries) with per-cluster advances — the advance of the UTF-8
|
|
24
|
+
/// cluster starting at byte `i` lands at index `i`, continuation
|
|
25
|
+
/// bytes hold 0 — and return true. Returning false (or leaving this
|
|
26
|
+
/// null) keeps callers on the per-call `measureWidth` seam. This is
|
|
27
|
+
/// the O(L) escape from measuring a growing line prefix once per
|
|
28
|
+
/// cluster: one provider round-trip per RUN instead of per cluster,
|
|
29
|
+
/// with line breaks then derived from cumulative advances exactly as
|
|
30
|
+
/// the estimator path derives them.
|
|
31
|
+
measure_advances_fn: ?*const fn (context: ?*anyopaque, font_id: FontId, size: f32, text: []const u8, advances: []f32) bool = null,
|
|
32
|
+
|
|
33
|
+
pub fn measureWidth(self: TextMeasureProvider, font_id: FontId, size: f32, text: []const u8) f32 {
|
|
34
|
+
if (text.len == 0) return 0;
|
|
35
|
+
const width = self.measure_fn(self.context, font_id, size, text);
|
|
36
|
+
if (!(width >= 0) or !isFiniteF32(width)) return estimateTextWidthForFont(font_id, text, size);
|
|
37
|
+
return width;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// Batched per-cluster advances for `text` into `advances[0..text.len]`.
|
|
41
|
+
/// False means "no batched answer" — the provider has no batched
|
|
42
|
+
/// entry or the host declined (invalid UTF-8, unresolvable font);
|
|
43
|
+
/// callers fall back to `measureWidth` prefix measurement, which
|
|
44
|
+
/// carries its own per-call estimator fallback.
|
|
45
|
+
pub fn measureAdvances(self: TextMeasureProvider, font_id: FontId, size: f32, text: []const u8, advances: []f32) bool {
|
|
46
|
+
const advances_fn = self.measure_advances_fn orelse return false;
|
|
47
|
+
if (text.len == 0) return true;
|
|
48
|
+
if (advances.len < text.len) return false;
|
|
49
|
+
return advances_fn(self.context, font_id, size, text, advances[0..text.len]);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/// Width of a text run: the provider when installed, the deterministic
|
|
54
|
+
/// estimator otherwise. This is the single measurement seam every layout
|
|
55
|
+
/// consumer (intrinsic widget sizing, line breaking, caret and selection
|
|
56
|
+
/// geometry) goes through. The provider is carried as a pointer so the
|
|
57
|
+
/// seam costs one word inside retained command storage; the pointee must
|
|
58
|
+
/// outlive layout state that carries it (runtimes own one for their whole
|
|
59
|
+
/// lifetime).
|
|
60
|
+
pub fn measureTextWidthForFont(provider: ?*const TextMeasureProvider, font_id: FontId, text: []const u8, size: f32) f32 {
|
|
61
|
+
if (provider) |value| return value.measureWidth(font_id, size, text);
|
|
62
|
+
return estimateTextWidthForFont(font_id, text, size);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// Advance of the cluster `text[cluster_start..cluster_end]` within a line
|
|
66
|
+
/// starting at `line_start`. With a provider the advance is the difference
|
|
67
|
+
/// of two prefix widths so kerning against the preceding text is honored;
|
|
68
|
+
/// without one it is the estimator's per-cluster advance (bit-identical to
|
|
69
|
+
/// the historical behavior).
|
|
70
|
+
pub fn measureTextAdvance(
|
|
71
|
+
provider: ?*const TextMeasureProvider,
|
|
72
|
+
font_id: FontId,
|
|
73
|
+
size: f32,
|
|
74
|
+
text: []const u8,
|
|
75
|
+
line_start: usize,
|
|
76
|
+
cluster_start: usize,
|
|
77
|
+
cluster_end: usize,
|
|
78
|
+
) f32 {
|
|
79
|
+
if (provider) |value| {
|
|
80
|
+
const with_cluster = value.measureWidth(font_id, size, text[line_start..cluster_end]);
|
|
81
|
+
const without_cluster = value.measureWidth(font_id, size, text[line_start..cluster_start]);
|
|
82
|
+
return @max(0, with_cluster - without_cluster);
|
|
83
|
+
}
|
|
84
|
+
return estimateTextAdvanceForBytes(font_id, text[cluster_start..cluster_end], size);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
fn isFiniteF32(value: f32) bool {
|
|
88
|
+
return value - value == 0;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
pub fn estimateTextWidth(text: []const u8, size: f32) f32 {
|
|
92
|
+
return estimateTextWidthForFont(default_sans_font_id, text, size);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
pub fn estimateTextWidthForFont(font_id: FontId, text: []const u8, size: f32) f32 {
|
|
96
|
+
var width: f32 = 0;
|
|
97
|
+
var index: usize = 0;
|
|
98
|
+
while (index < text.len) {
|
|
99
|
+
const next = @min(text.len, index + utf8SequenceLength(text[index]));
|
|
100
|
+
width += estimateTextAdvanceForBytes(font_id, text[index..next], size);
|
|
101
|
+
index = next;
|
|
102
|
+
}
|
|
103
|
+
return width;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// Design pitch for the reserved mono id: 0.6 em, Geist Mono's advance
|
|
107
|
+
/// (600/1000 units in every published weight), so the estimator, the
|
|
108
|
+
/// bundled mono outlines, the live Geist Mono provider path, and
|
|
109
|
+
/// SF Mono (0.6 em) all agree. The comptime check below holds the
|
|
110
|
+
/// bundled mono face to the constant — a bundle swap that changes the
|
|
111
|
+
/// pitch is a compile error, not a silent layout/ink drift. Public
|
|
112
|
+
/// (re-exported as `canvas.mono_advance_em`) so apps that pitch-snap
|
|
113
|
+
/// their mono sizes to whole pixels derive from the same constant the
|
|
114
|
+
/// engine measures and inks with.
|
|
115
|
+
pub const mono_advance_em: f32 = 0.6;
|
|
116
|
+
|
|
117
|
+
comptime {
|
|
118
|
+
const face = &font_ttf.geist_mono;
|
|
119
|
+
const zero_advance = face.advance(face.glyphIndex('0')) / face.units_per_em;
|
|
120
|
+
if (zero_advance != mono_advance_em)
|
|
121
|
+
@compileError("bundled mono face pitch drifted from the estimator's mono advance");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/// The bundled face's `.notdef` advance in em units (0.6 em in Geist).
|
|
125
|
+
/// This is the width the face itself declares for "I do not have this
|
|
126
|
+
/// glyph", so it is what layout charges for codepoints outside the
|
|
127
|
+
/// face's coverage (and for control or malformed bytes, which paint as
|
|
128
|
+
/// the same block fallback).
|
|
129
|
+
const notdef_advance_em: f32 = faceAdvanceEm(&font_ttf.geist_regular, 0);
|
|
130
|
+
|
|
131
|
+
/// Em-unit advances for the printable ASCII range, read from the
|
|
132
|
+
/// bundled face's `cmap`/`hmtx` at comptime. Layout therefore measures
|
|
133
|
+
/// with exactly the advances the reference renderer inks; a bundle
|
|
134
|
+
/// swap that drops ASCII coverage is a compile error.
|
|
135
|
+
const ascii_advance_em: [0x7F - 0x20]f32 = blk: {
|
|
136
|
+
@setEvalBranchQuota(400_000);
|
|
137
|
+
var table: [0x7F - 0x20]f32 = undefined;
|
|
138
|
+
for (&table, 0x20..) |*slot, codepoint| {
|
|
139
|
+
const glyph = font_ttf.geist_regular.glyphIndex(codepoint);
|
|
140
|
+
if (glyph == 0) @compileError("bundled Geist face is missing printable ASCII coverage");
|
|
141
|
+
slot.* = faceAdvanceEm(&font_ttf.geist_regular, glyph);
|
|
142
|
+
}
|
|
143
|
+
break :blk table;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
fn faceAdvanceEm(face: *const font_ttf.Face, glyph: u16) f32 {
|
|
147
|
+
return face.advance(glyph) / face.units_per_em;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
pub fn estimateTextAdvanceForBytes(font_id: FontId, bytes: []const u8, size: f32) f32 {
|
|
151
|
+
if (bytes.len == 0) return 0;
|
|
152
|
+
if (font_id == default_mono_font_id) return size * mono_advance_em;
|
|
153
|
+
const weight = sansVariantWidthFactor(font_id);
|
|
154
|
+
return size * clusterAdvanceEm(bytes) * weight;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/// The bundled face's U+2026 HORIZONTAL ELLIPSIS advance in em units,
|
|
158
|
+
/// read at comptime like the ASCII table. Elision consults the marker's
|
|
159
|
+
/// advance once per overflowing line every frame, so the estimator path
|
|
160
|
+
/// answers from this constant instead of re-walking the `cmap`.
|
|
161
|
+
const ellipsis_advance_em: f32 = blk: {
|
|
162
|
+
@setEvalBranchQuota(400_000);
|
|
163
|
+
const glyph = font_ttf.geist_regular.glyphIndex(0x2026);
|
|
164
|
+
if (glyph == 0) @compileError("bundled Geist face is missing U+2026 (the elision marker)");
|
|
165
|
+
break :blk faceAdvanceEm(&font_ttf.geist_regular, glyph);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/// Estimator advance of the elision marker ("…"): bit-identical to
|
|
169
|
+
/// `estimateTextWidthForFont(font_id, "…", size)`, without the runtime
|
|
170
|
+
/// codepoint lookup.
|
|
171
|
+
pub fn estimatedTextEllipsisAdvance(font_id: FontId, size: f32) f32 {
|
|
172
|
+
if (font_id == default_mono_font_id) return size * mono_advance_em;
|
|
173
|
+
return size * ellipsis_advance_em * sansVariantWidthFactor(font_id);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/// Deterministic width of `text` measured against an arbitrary parsed
|
|
177
|
+
/// face — the registered-font counterpart of `estimateTextWidthForFont`.
|
|
178
|
+
/// Same contract as the bundled-face estimator: covered codepoints charge
|
|
179
|
+
/// the face's own `hmtx` advance (so layout measures exactly what the
|
|
180
|
+
/// reference renderer inks), uncovered codepoints take the same three
|
|
181
|
+
/// documented fallback classes (East Asian wide 1.0 em, uncovered
|
|
182
|
+
/// symbol/pictograph blocks 0.8 em, everything else that face's own
|
|
183
|
+
/// `.notdef` advance).
|
|
184
|
+
pub fn estimateTextWidthForFace(face: *const font_ttf.Face, text: []const u8, size: f32) f32 {
|
|
185
|
+
var width: f32 = 0;
|
|
186
|
+
var index: usize = 0;
|
|
187
|
+
while (index < text.len) {
|
|
188
|
+
const next = @min(text.len, index + utf8SequenceLength(text[index]));
|
|
189
|
+
width += size * clusterAdvanceEmForFace(face, text[index..next]);
|
|
190
|
+
index = next;
|
|
191
|
+
}
|
|
192
|
+
return width;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/// Em-unit advance of one UTF-8 cluster against an arbitrary face: the
|
|
196
|
+
/// runtime mirror of `clusterAdvanceEm`, with the face's own `.notdef`
|
|
197
|
+
/// advance standing in for the bundled face's.
|
|
198
|
+
fn clusterAdvanceEmForFace(face: *const font_ttf.Face, bytes: []const u8) f32 {
|
|
199
|
+
if (bytes.len == 0) return 0;
|
|
200
|
+
const face_notdef_em = faceAdvanceEm(face, 0);
|
|
201
|
+
if (bytes.len == 1) {
|
|
202
|
+
const byte = bytes[0];
|
|
203
|
+
if (byte >= 0x20 and byte < 0x7F) {
|
|
204
|
+
const glyph = face.glyphIndex(byte);
|
|
205
|
+
if (glyph != 0) return faceAdvanceEm(face, glyph);
|
|
206
|
+
}
|
|
207
|
+
return face_notdef_em;
|
|
208
|
+
}
|
|
209
|
+
// Truncated clusters at the end of a run take the notdef fallback
|
|
210
|
+
// explicitly, mirroring the bundled-face path (`utf8Decode` asserts
|
|
211
|
+
// on length mismatch rather than erroring).
|
|
212
|
+
if (bytes.len != utf8SequenceLength(bytes[0])) return face_notdef_em;
|
|
213
|
+
const codepoint = std.unicode.utf8Decode(bytes) catch return face_notdef_em;
|
|
214
|
+
const glyph = face.glyphIndex(codepoint);
|
|
215
|
+
if (glyph != 0) return faceAdvanceEm(face, glyph);
|
|
216
|
+
if (isEastAsianWideCodepoint(codepoint)) return 1.0;
|
|
217
|
+
if (isSymbolPictographCodepoint(codepoint)) return 0.8;
|
|
218
|
+
return face_notdef_em;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/// Em-unit advance of one UTF-8 cluster, derived from the bundled face:
|
|
222
|
+
/// a comptime `hmtx` table for printable ASCII, a runtime lookup against
|
|
223
|
+
/// the embedded face for every other covered codepoint (so the estimate
|
|
224
|
+
/// is definitionally in lockstep with the bytes the renderer inks), and
|
|
225
|
+
/// three documented fallbacks for what the face cannot answer — East
|
|
226
|
+
/// Asian wide/fullwidth codepoints charge a full em (the advance CJK
|
|
227
|
+
/// fallback fonts such as PingFang use, and the cell a fullwidth block
|
|
228
|
+
/// glyph should occupy), uncovered symbol/pictograph blocks charge
|
|
229
|
+
/// 0.8 em (the AppleSymbols cascade class real macOS text falls back
|
|
230
|
+
/// to), everything else charges the face's own `.notdef` advance.
|
|
231
|
+
fn clusterAdvanceEm(bytes: []const u8) f32 {
|
|
232
|
+
if (bytes.len == 1) {
|
|
233
|
+
const byte = bytes[0];
|
|
234
|
+
if (byte >= 0x20 and byte < 0x7F) return ascii_advance_em[byte - 0x20];
|
|
235
|
+
return notdef_advance_em;
|
|
236
|
+
}
|
|
237
|
+
// `utf8Decode` asserts (rather than errors) when the slice length
|
|
238
|
+
// does not match the lead byte, so truncated clusters — possible at
|
|
239
|
+
// the end of a run — take the notdef fallback explicitly.
|
|
240
|
+
if (bytes.len != utf8SequenceLength(bytes[0])) return notdef_advance_em;
|
|
241
|
+
const codepoint = std.unicode.utf8Decode(bytes) catch return notdef_advance_em;
|
|
242
|
+
const face = &font_ttf.geist_regular;
|
|
243
|
+
const glyph = face.glyphIndex(codepoint);
|
|
244
|
+
if (glyph != 0) return faceAdvanceEm(face, glyph);
|
|
245
|
+
if (isEastAsianWideCodepoint(codepoint)) return 1.0;
|
|
246
|
+
if (isSymbolPictographCodepoint(codepoint)) return 0.8;
|
|
247
|
+
return notdef_advance_em;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/// Symbol and pictograph blocks the bundled face does not cover but that
|
|
251
|
+
/// real dynamic content carries constantly (GitHub markdown ballot boxes
|
|
252
|
+
/// U+2610/U+2611, technical symbols, geometric shapes). Live macOS text
|
|
253
|
+
/// draws these through CoreText's cascade — AppleSymbols answers the
|
|
254
|
+
/// ballot boxes at 0.83 em behind the bundled sans — so charging the
|
|
255
|
+
/// face's 0.6 em `.notdef` advance under-reserved the cell every time the
|
|
256
|
+
/// deterministic estimator measured one. 0.8 em is the documented
|
|
257
|
+
/// approximation of that cascade class (measured: U+2610/U+2611 0.83 em
|
|
258
|
+
/// via AppleSymbols; only codepoints the face itself cannot answer reach
|
|
259
|
+
/// this class, so covered symbols like → keep their exact face advance).
|
|
260
|
+
fn isSymbolPictographCodepoint(codepoint: u21) bool {
|
|
261
|
+
return switch (codepoint) {
|
|
262
|
+
// Arrows through Misc Symbols and Arrows: arrows, math operators,
|
|
263
|
+
// misc technical, control pictures, enclosed alphanumerics, box
|
|
264
|
+
// drawing/blocks, geometric shapes, misc symbols, dingbats.
|
|
265
|
+
0x2190...0x2BFF => true,
|
|
266
|
+
else => false,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/// East Asian wide / fullwidth blocks (Unicode EAW `W`/`F`, plus the
|
|
271
|
+
/// emoji planes, which render fullwidth everywhere). The bundled face
|
|
272
|
+
/// covers none of these, so they take the 1.0 em fallback instead of
|
|
273
|
+
/// the `.notdef` advance.
|
|
274
|
+
fn isEastAsianWideCodepoint(codepoint: u21) bool {
|
|
275
|
+
return switch (codepoint) {
|
|
276
|
+
0x1100...0x115F, // Hangul jamo
|
|
277
|
+
0x2E80...0x303E, // CJK radicals, Kangxi, CJK symbols and punctuation
|
|
278
|
+
0x3041...0x33FF, // Kana, CJK compatibility
|
|
279
|
+
0x3400...0x4DBF, // CJK extension A
|
|
280
|
+
0x4E00...0x9FFF, // CJK unified ideographs
|
|
281
|
+
0xA000...0xA4CF, // Yi
|
|
282
|
+
0xA960...0xA97F, // Hangul jamo extended-A
|
|
283
|
+
0xAC00...0xD7A3, // Hangul syllables
|
|
284
|
+
0xF900...0xFAFF, // CJK compatibility ideographs
|
|
285
|
+
0xFE10...0xFE19, // Vertical forms
|
|
286
|
+
0xFE30...0xFE6F, // CJK compatibility forms, small form variants
|
|
287
|
+
0xFF00...0xFF60, // Fullwidth forms
|
|
288
|
+
0xFFE0...0xFFE6, // Fullwidth signs
|
|
289
|
+
0x1F300...0x1FAFF, // Emoji and symbol planes
|
|
290
|
+
0x20000...0x3FFFD, // CJK extensions B and beyond
|
|
291
|
+
=> true,
|
|
292
|
+
else => false,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/// Width factor for the reserved sans variant ids (span weight/slant).
|
|
297
|
+
/// The regular id keeps factor 1 exactly, so existing single-style text
|
|
298
|
+
/// measurements — and every golden derived from them — are unchanged.
|
|
299
|
+
fn sansVariantWidthFactor(font_id: FontId) f32 {
|
|
300
|
+
if (font_id == canvas.default_sans_medium_font_id) return 1.02;
|
|
301
|
+
if (font_id == canvas.default_sans_bold_font_id) return 1.04;
|
|
302
|
+
if (font_id == canvas.default_sans_bold_italic_font_id) return 1.04;
|
|
303
|
+
return 1;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
pub fn estimatedGlyphAdvance(glyph: Glyph, size: f32) f32 {
|
|
307
|
+
return @max(size * 0.25, glyph.advance);
|
|
308
|
+
}
|
|
309
|
+
|