@native-sdk/cli 0.0.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +50 -1
- package/app.zon +30 -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 +44 -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,1457 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
const geometry = @import("geometry");
|
|
3
|
+
const canvas = @import("root.zig");
|
|
4
|
+
const drawing_model = @import("drawing.zig");
|
|
5
|
+
const text_model = @import("text.zig");
|
|
6
|
+
const render_model = @import("render.zig");
|
|
7
|
+
const frame_model = @import("frame.zig");
|
|
8
|
+
|
|
9
|
+
const Error = canvas.Error;
|
|
10
|
+
const ImageId = canvas.ImageId;
|
|
11
|
+
const Color = drawing_model.Color;
|
|
12
|
+
const Affine = drawing_model.Affine;
|
|
13
|
+
const Radius = drawing_model.Radius;
|
|
14
|
+
const LinearGradient = drawing_model.LinearGradient;
|
|
15
|
+
const Fill = drawing_model.Fill;
|
|
16
|
+
const FillRect = drawing_model.FillRect;
|
|
17
|
+
const StrokeRect = drawing_model.StrokeRect;
|
|
18
|
+
const FillRoundedRect = drawing_model.FillRoundedRect;
|
|
19
|
+
const Line = drawing_model.Line;
|
|
20
|
+
const PathElement = drawing_model.PathElement;
|
|
21
|
+
const FillPath = drawing_model.FillPath;
|
|
22
|
+
const StrokePath = drawing_model.StrokePath;
|
|
23
|
+
const ImageFit = drawing_model.ImageFit;
|
|
24
|
+
const ImageSampling = drawing_model.ImageSampling;
|
|
25
|
+
const DrawImage = drawing_model.DrawImage;
|
|
26
|
+
const Shadow = drawing_model.Shadow;
|
|
27
|
+
const Blur = drawing_model.Blur;
|
|
28
|
+
const DrawText = text_model.DrawText;
|
|
29
|
+
const TextLayoutOptions = text_model.TextLayoutOptions;
|
|
30
|
+
const TextLine = text_model.TextLine;
|
|
31
|
+
const RenderCommand = render_model.RenderCommand;
|
|
32
|
+
const CanvasRenderPass = frame_model.CanvasRenderPass;
|
|
33
|
+
|
|
34
|
+
const layoutTextRun = text_model.layoutTextRun;
|
|
35
|
+
const textLineBounds = text_model.textLineBounds;
|
|
36
|
+
const text_ellipsis_codepoint = text_model.text_ellipsis_codepoint;
|
|
37
|
+
const estimatedGlyphAdvance = text_model.estimatedGlyphAdvance;
|
|
38
|
+
const measureTextAdvance = text_model.measureTextAdvance;
|
|
39
|
+
const nextTextOffset = text_model.nextTextOffset;
|
|
40
|
+
|
|
41
|
+
const reference_blur = @import("reference_blur.zig");
|
|
42
|
+
const reference_paths = @import("reference_paths.zig");
|
|
43
|
+
const render_fingerprints = @import("render_fingerprints.zig");
|
|
44
|
+
const vector = @import("vector.zig");
|
|
45
|
+
const font_ttf = @import("font_ttf.zig");
|
|
46
|
+
|
|
47
|
+
/// Element budget for one glyph outline: the bundled face's densest
|
|
48
|
+
/// glyphs stay well under this (maxp: 96 points per simple glyph).
|
|
49
|
+
const reference_glyph_path_capacity: usize = 256;
|
|
50
|
+
|
|
51
|
+
const referenceBlurKernel = reference_blur.referenceBlurKernel;
|
|
52
|
+
const referenceBlurSampleWithKernel = reference_blur.referenceBlurSampleWithKernel;
|
|
53
|
+
const referenceBlurSample = reference_blur.referenceBlurSample;
|
|
54
|
+
pub const ReferenceRenderMemo = @import("reference_memo.zig").ReferenceRenderMemo;
|
|
55
|
+
const referenceDistanceToSegment = reference_paths.referenceDistanceToSegment;
|
|
56
|
+
|
|
57
|
+
const max_reference_text_layout_lines: usize = 64;
|
|
58
|
+
const max_reference_blur_kernel_samples: usize = 4096;
|
|
59
|
+
|
|
60
|
+
pub const ReferenceImage = struct {
|
|
61
|
+
id: ImageId,
|
|
62
|
+
width: usize,
|
|
63
|
+
height: usize,
|
|
64
|
+
pixels: []const u8,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/// One runtime-registered font face the reference renderer resolves text
|
|
68
|
+
/// runs against: a `FontId` mapped to a parsed TrueType face. The face
|
|
69
|
+
/// (and the bytes it borrows) must outlive the surface; runtimes own
|
|
70
|
+
/// both in their registered-font pool.
|
|
71
|
+
pub const ReferenceFont = struct {
|
|
72
|
+
id: canvas.FontId,
|
|
73
|
+
face: *const font_ttf.Face,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
pub const ReferenceRenderSurface = struct {
|
|
77
|
+
width: usize,
|
|
78
|
+
height: usize,
|
|
79
|
+
pixels: []u8,
|
|
80
|
+
scratch: ?[]u8 = null,
|
|
81
|
+
images: []const ReferenceImage = &.{},
|
|
82
|
+
fonts: []const ReferenceFont = &.{},
|
|
83
|
+
/// Optional memo for large per-pixel commands (see
|
|
84
|
+
/// `ReferenceRenderMemo`): when present, a heavyweight command
|
|
85
|
+
/// (backdrop blur, drop shadow, big fills) whose inputs are
|
|
86
|
+
/// byte-identical to a previous render replays its stored output
|
|
87
|
+
/// instead of re-running its pixel loop. Same bytes either way —
|
|
88
|
+
/// this only moves time.
|
|
89
|
+
render_memo: ?*ReferenceRenderMemo = null,
|
|
90
|
+
|
|
91
|
+
pub fn init(width: usize, height: usize, pixels: []u8) Error!ReferenceRenderSurface {
|
|
92
|
+
const len = std.math.mul(usize, std.math.mul(usize, width, height) catch return error.ReferenceRenderSurfaceTooSmall, 4) catch return error.ReferenceRenderSurfaceTooSmall;
|
|
93
|
+
if (pixels.len < len) return error.ReferenceRenderSurfaceTooSmall;
|
|
94
|
+
return .{
|
|
95
|
+
.width = width,
|
|
96
|
+
.height = height,
|
|
97
|
+
.pixels = pixels[0..len],
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
pub fn initWithScratch(width: usize, height: usize, pixels: []u8, scratch: []u8) Error!ReferenceRenderSurface {
|
|
102
|
+
var surface = try init(width, height, pixels);
|
|
103
|
+
if (scratch.len < surface.pixels.len) return error.ReferenceRenderSurfaceTooSmall;
|
|
104
|
+
surface.scratch = scratch[0..surface.pixels.len];
|
|
105
|
+
return surface;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
pub fn withImages(self: ReferenceRenderSurface, images: []const ReferenceImage) ReferenceRenderSurface {
|
|
109
|
+
var next = self;
|
|
110
|
+
next.images = images;
|
|
111
|
+
return next;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/// Runtime-registered faces text runs resolve against before the
|
|
115
|
+
/// bundled fallback mapping (see `referenceFaceForFontId`).
|
|
116
|
+
pub fn withFonts(self: ReferenceRenderSurface, fonts: []const ReferenceFont) ReferenceRenderSurface {
|
|
117
|
+
var next = self;
|
|
118
|
+
next.fonts = fonts;
|
|
119
|
+
return next;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/// Attach a render memo that outlives this render pass (one per
|
|
123
|
+
/// live scene). Purely an optimization: output bytes are identical
|
|
124
|
+
/// with or without it.
|
|
125
|
+
pub fn withRenderMemo(self: ReferenceRenderSurface, memo: ?*ReferenceRenderMemo) ReferenceRenderSurface {
|
|
126
|
+
var next = self;
|
|
127
|
+
next.render_memo = memo;
|
|
128
|
+
return next;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// A memo lookup in flight for one command: the memo to store into
|
|
132
|
+
/// and the fully built key. Null everywhere a memo doesn't apply
|
|
133
|
+
/// (no memo attached, or the rect is too small to be worth a hash).
|
|
134
|
+
const MemoProbe = struct {
|
|
135
|
+
memo: *ReferenceRenderMemo,
|
|
136
|
+
key: ReferenceRenderMemo.Key,
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/// Build the memo key for a per-pixel command about to run.
|
|
140
|
+
/// `params_hash` must cover every command parameter the pixel loop
|
|
141
|
+
/// reads (see `referenceMemoParamsHash`); `apron_rows` is how far
|
|
142
|
+
/// beyond its rect the command reads destination pixels (the blur's
|
|
143
|
+
/// kernel radius; zero for single-pixel blends). Must be called
|
|
144
|
+
/// BEFORE the command writes any pixels — the key hashes the
|
|
145
|
+
/// destination bytes as the command will read them.
|
|
146
|
+
fn memoProbe(self: ReferenceRenderSurface, pixel_rect: ReferencePixelRect, apron_rows: usize, params_hash: u64) ?MemoProbe {
|
|
147
|
+
const memo = self.render_memo orelse return null;
|
|
148
|
+
if (pixel_rect.width * pixel_rect.height < memo.min_pixels) return null;
|
|
149
|
+
return .{
|
|
150
|
+
.memo = memo,
|
|
151
|
+
.key = ReferenceRenderMemo.keyFor(
|
|
152
|
+
self.pixels,
|
|
153
|
+
self.width,
|
|
154
|
+
self.height,
|
|
155
|
+
pixel_rect.x,
|
|
156
|
+
pixel_rect.y,
|
|
157
|
+
pixel_rect.width,
|
|
158
|
+
pixel_rect.height,
|
|
159
|
+
apron_rows,
|
|
160
|
+
params_hash,
|
|
161
|
+
),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// Replay a memoized command's output rows into the framebuffer.
|
|
166
|
+
/// True on a hit (the command is done); false means render normally
|
|
167
|
+
/// and offer the result to `memoStore`.
|
|
168
|
+
fn memoReplay(self: ReferenceRenderSurface, probe: ?MemoProbe, pixel_rect: ReferencePixelRect) bool {
|
|
169
|
+
const p = probe orelse return false;
|
|
170
|
+
const cached = p.memo.find(p.key) orelse return false;
|
|
171
|
+
var row: usize = 0;
|
|
172
|
+
while (row < pixel_rect.height) : (row += 1) {
|
|
173
|
+
const src_offset = row * pixel_rect.width * 4;
|
|
174
|
+
const dst_offset = ((pixel_rect.y + row) * self.width + pixel_rect.x) * 4;
|
|
175
|
+
@memcpy(
|
|
176
|
+
self.pixels[dst_offset .. dst_offset + pixel_rect.width * 4],
|
|
177
|
+
cached[src_offset .. src_offset + pixel_rect.width * 4],
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/// Remember a freshly rendered command's output rows for the next
|
|
184
|
+
/// frame. Storage failure just means the next identical run
|
|
185
|
+
/// recomputes.
|
|
186
|
+
fn memoStore(self: ReferenceRenderSurface, probe: ?MemoProbe, pixel_rect: ReferencePixelRect) void {
|
|
187
|
+
const p = probe orelse return;
|
|
188
|
+
const buffer = p.memo.store(p.key) orelse return;
|
|
189
|
+
var row: usize = 0;
|
|
190
|
+
while (row < pixel_rect.height) : (row += 1) {
|
|
191
|
+
const dst_offset = row * pixel_rect.width * 4;
|
|
192
|
+
const src_offset = ((pixel_rect.y + row) * self.width + pixel_rect.x) * 4;
|
|
193
|
+
@memcpy(
|
|
194
|
+
buffer[dst_offset .. dst_offset + pixel_rect.width * 4],
|
|
195
|
+
self.pixels[src_offset .. src_offset + pixel_rect.width * 4],
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
pub fn clear(self: ReferenceRenderSurface, color: Color) void {
|
|
201
|
+
const pixel = colorToRgba8(color);
|
|
202
|
+
var index: usize = 0;
|
|
203
|
+
while (index < self.pixels.len) : (index += 4) {
|
|
204
|
+
self.pixels[index + 0] = pixel[0];
|
|
205
|
+
self.pixels[index + 1] = pixel[1];
|
|
206
|
+
self.pixels[index + 2] = pixel[2];
|
|
207
|
+
self.pixels[index + 3] = pixel[3];
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
pub fn clearRect(self: ReferenceRenderSurface, rect: geometry.RectF, color: Color) void {
|
|
212
|
+
const pixel_rect = referencePixelRect(rect, self.width, self.height) orelse return;
|
|
213
|
+
const pixel = colorToRgba8(color);
|
|
214
|
+
var y = pixel_rect.y;
|
|
215
|
+
while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
|
|
216
|
+
var x = pixel_rect.x;
|
|
217
|
+
while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
|
|
218
|
+
const index = (y * self.width + x) * 4;
|
|
219
|
+
self.pixels[index + 0] = pixel[0];
|
|
220
|
+
self.pixels[index + 1] = pixel[1];
|
|
221
|
+
self.pixels[index + 2] = pixel[2];
|
|
222
|
+
self.pixels[index + 3] = pixel[3];
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
pub fn renderPass(self: ReferenceRenderSurface, pass: CanvasRenderPass, clear_color: Color) Error!void {
|
|
228
|
+
// One-time sRGB decode table fill (see the table's doc comment);
|
|
229
|
+
// outside the per-pixel loops so the hot path pays no checks.
|
|
230
|
+
ensureSrgbToLinearByteTable();
|
|
231
|
+
// Fresh per-pass panel-fill budget (see the memo's doc comment).
|
|
232
|
+
if (self.render_memo) |memo| memo.image_scale_fills_this_pass = 0;
|
|
233
|
+
const scale = referencePassScale(pass.scale);
|
|
234
|
+
const scissor = if (pass.scissorBounds()) |bounds| referenceScaleRect(bounds, scale) else null;
|
|
235
|
+
switch (pass.loadAction()) {
|
|
236
|
+
.skip => return,
|
|
237
|
+
.clear => self.clear(clear_color),
|
|
238
|
+
.load => if (scissor) |bounds| self.clearRect(bounds, clear_color),
|
|
239
|
+
}
|
|
240
|
+
for (pass.commands) |command| try self.renderCommand(referenceScaleCommand(command, scale), scissor);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
pub fn pixelRgba8(self: ReferenceRenderSurface, x: usize, y: usize) [4]u8 {
|
|
244
|
+
if (x >= self.width or y >= self.height) return .{ 0, 0, 0, 0 };
|
|
245
|
+
const index = (y * self.width + x) * 4;
|
|
246
|
+
return .{
|
|
247
|
+
self.pixels[index + 0],
|
|
248
|
+
self.pixels[index + 1],
|
|
249
|
+
self.pixels[index + 2],
|
|
250
|
+
self.pixels[index + 3],
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
fn renderCommand(self: ReferenceRenderSurface, command: RenderCommand, scissor: ?geometry.RectF) Error!void {
|
|
255
|
+
const draw_bounds = referenceCommandBounds(command, scissor) orelse return;
|
|
256
|
+
switch (command.command) {
|
|
257
|
+
.fill_rect => |value| try self.fillRect(command, value, draw_bounds),
|
|
258
|
+
.fill_rounded_rect => |value| try self.fillRoundedRect(command, value, draw_bounds),
|
|
259
|
+
.stroke_rect => |value| try self.strokeRect(command, value, draw_bounds),
|
|
260
|
+
.draw_line => |value| try self.drawLine(command, value, draw_bounds),
|
|
261
|
+
.fill_path => |value| try self.fillPath(command, value, draw_bounds),
|
|
262
|
+
.stroke_path => |value| try self.strokePath(command, value, draw_bounds),
|
|
263
|
+
.draw_image => |value| try self.drawImage(command, value, draw_bounds),
|
|
264
|
+
.shadow => |value| try self.drawShadow(command, value, draw_bounds),
|
|
265
|
+
.blur => |value| try self.drawBlur(command, value, draw_bounds),
|
|
266
|
+
.draw_text => |value| try self.drawText(command, value, draw_bounds),
|
|
267
|
+
else => return error.ReferenceRenderUnsupportedCommand,
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
fn fillRect(self: ReferenceRenderSurface, command: RenderCommand, value: FillRect, draw_bounds: geometry.RectF) Error!void {
|
|
272
|
+
const pixel_rect = referencePixelRect(draw_bounds, self.width, self.height) orelse return;
|
|
273
|
+
// Render memo: a large translucent fill (the modal scrim's wash
|
|
274
|
+
// covers the whole viewport) blends every pixel; it reads only
|
|
275
|
+
// the pixel it writes, so no apron rows join the key.
|
|
276
|
+
const probe = self.memoProbe(pixel_rect, 0, referenceMemoParamsHash(1, command, value));
|
|
277
|
+
if (self.memoReplay(probe, pixel_rect)) return;
|
|
278
|
+
var y = pixel_rect.y;
|
|
279
|
+
while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
|
|
280
|
+
var x = pixel_rect.x;
|
|
281
|
+
while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
|
|
282
|
+
const point = referencePixelCenter(x, y);
|
|
283
|
+
self.blendPixel(@intCast(x), @intCast(y), referenceSampleFill(value.fill, command.transform, point), command.opacity);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
self.memoStore(probe, pixel_rect);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
fn fillRoundedRect(self: ReferenceRenderSurface, command: RenderCommand, value: FillRoundedRect, draw_bounds: geometry.RectF) Error!void {
|
|
290
|
+
const rect = command.transform.transformRect(value.rect).normalized();
|
|
291
|
+
const pixel_rect = referencePixelRect(draw_bounds, self.width, self.height) orelse return;
|
|
292
|
+
const radius = referenceScaleRadius(value.radius, command.transform);
|
|
293
|
+
// Render memo: a large rounded fill (a dialog surface) tests the
|
|
294
|
+
// corner mask per pixel; it reads only the pixel it blends into,
|
|
295
|
+
// so no apron rows join the key.
|
|
296
|
+
const probe = self.memoProbe(pixel_rect, 0, referenceMemoParamsHash(2, command, value));
|
|
297
|
+
if (self.memoReplay(probe, pixel_rect)) return;
|
|
298
|
+
var y = pixel_rect.y;
|
|
299
|
+
while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
|
|
300
|
+
var x = pixel_rect.x;
|
|
301
|
+
while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
|
|
302
|
+
const point = referencePixelCenter(x, y);
|
|
303
|
+
if (referencePointInRoundedRect(point, rect, radius)) self.blendPixel(@intCast(x), @intCast(y), referenceSampleFill(value.fill, command.transform, point), command.opacity);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
self.memoStore(probe, pixel_rect);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
fn strokeRect(self: ReferenceRenderSurface, command: RenderCommand, value: StrokeRect, draw_bounds: geometry.RectF) Error!void {
|
|
310
|
+
const stroke_width = nonNegative(value.stroke.width) * referenceTransformScale(command.transform);
|
|
311
|
+
if (stroke_width <= 0) return;
|
|
312
|
+
const half_width = stroke_width * 0.5;
|
|
313
|
+
const rect = command.transform.transformRect(value.rect).normalized();
|
|
314
|
+
const outer = rect.inflate(geometry.InsetsF.all(half_width));
|
|
315
|
+
const inner = rect.deflate(geometry.InsetsF.all(@min(half_width, @min(rect.width, rect.height) * 0.5)));
|
|
316
|
+
const radius = referenceScaleRadius(value.radius, command.transform);
|
|
317
|
+
const outer_radius = referenceOutsetRadius(radius, half_width);
|
|
318
|
+
const inner_radius = referenceInsetRadius(radius, half_width);
|
|
319
|
+
const pixel_rect = referencePixelRect(draw_bounds, self.width, self.height) orelse return;
|
|
320
|
+
// Render memo: a rounded border's pixel rect spans the WHOLE
|
|
321
|
+
// stroked rect (a dialog outline covers the dialog), and every
|
|
322
|
+
// pixel tests two corner masks. It reads only the pixel it
|
|
323
|
+
// blends into, so no apron rows join the key.
|
|
324
|
+
const probe = self.memoProbe(pixel_rect, 0, referenceMemoParamsHash(5, command, value));
|
|
325
|
+
if (self.memoReplay(probe, pixel_rect)) return;
|
|
326
|
+
var y = pixel_rect.y;
|
|
327
|
+
while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
|
|
328
|
+
var x = pixel_rect.x;
|
|
329
|
+
while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
|
|
330
|
+
const point = referencePixelCenter(x, y);
|
|
331
|
+
if (referencePointInRoundedRect(point, outer, outer_radius) and !referencePointInRoundedRect(point, inner, inner_radius)) {
|
|
332
|
+
self.blendPixel(@intCast(x), @intCast(y), referenceSampleFill(value.stroke.fill, command.transform, point), command.opacity);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
self.memoStore(probe, pixel_rect);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
fn drawLine(self: ReferenceRenderSurface, command: RenderCommand, value: Line, draw_bounds: geometry.RectF) Error!void {
|
|
340
|
+
const stroke_width = nonNegative(value.stroke.width) * referenceTransformScale(command.transform);
|
|
341
|
+
if (stroke_width <= 0) return;
|
|
342
|
+
const half_width = stroke_width * 0.5;
|
|
343
|
+
const from = command.transform.transformPoint(value.from);
|
|
344
|
+
const to = command.transform.transformPoint(value.to);
|
|
345
|
+
const pixel_rect = referencePixelRect(draw_bounds, self.width, self.height) orelse return;
|
|
346
|
+
var y = pixel_rect.y;
|
|
347
|
+
while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
|
|
348
|
+
var x = pixel_rect.x;
|
|
349
|
+
while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
|
|
350
|
+
const point = referencePixelCenter(x, y);
|
|
351
|
+
if (referenceDistanceToSegment(point, from, to) <= half_width) {
|
|
352
|
+
self.blendPixel(@intCast(x), @intCast(y), referenceSampleFill(value.stroke.fill, command.transform, point), command.opacity);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
fn fillPath(self: ReferenceRenderSurface, command: RenderCommand, value: FillPath, draw_bounds: geometry.RectF) Error!void {
|
|
359
|
+
// Anti-aliased scanline fill via the shared vector core. The wire
|
|
360
|
+
// command keeps its historical even-odd interiorness; consumers
|
|
361
|
+
// wanting nonzero (glyphs, icons) call the vector core directly.
|
|
362
|
+
const pixel_rect = referencePixelRect(draw_bounds, self.width, self.height) orelse return;
|
|
363
|
+
var sink = ReferenceCoverageSink{
|
|
364
|
+
.surface = self,
|
|
365
|
+
.fill = value.fill,
|
|
366
|
+
.transform = command.transform,
|
|
367
|
+
.opacity = command.opacity,
|
|
368
|
+
};
|
|
369
|
+
vector.fillPath(
|
|
370
|
+
value.elements,
|
|
371
|
+
command.transform,
|
|
372
|
+
.even_odd,
|
|
373
|
+
vector.default_tolerance,
|
|
374
|
+
referenceVectorClip(pixel_rect),
|
|
375
|
+
&sink,
|
|
376
|
+
) catch return error.ReferenceRenderUnsupportedCommand;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
fn strokePath(self: ReferenceRenderSurface, command: RenderCommand, value: StrokePath, draw_bounds: geometry.RectF) Error!void {
|
|
380
|
+
// Anti-aliased stroke-to-outline via the shared vector core. End
|
|
381
|
+
// caps come from the command's cap channel: butt is the default
|
|
382
|
+
// (matching what hosts draw for an unadorned stroke), and
|
|
383
|
+
// emitters that want round ends — the stroke-icon dialect, the
|
|
384
|
+
// house spinner arc — opt in per command. Joins stay round: the
|
|
385
|
+
// historical distance-field semantics of this command rounded
|
|
386
|
+
// every join, and no emitter has asked for another shape.
|
|
387
|
+
const stroke_width = nonNegative(value.stroke.width) * referenceTransformScale(command.transform);
|
|
388
|
+
if (stroke_width <= 0) return;
|
|
389
|
+
const pixel_rect = referencePixelRect(draw_bounds, self.width, self.height) orelse return;
|
|
390
|
+
var sink = ReferenceCoverageSink{
|
|
391
|
+
.surface = self,
|
|
392
|
+
.fill = value.stroke.fill,
|
|
393
|
+
.transform = command.transform,
|
|
394
|
+
.opacity = command.opacity,
|
|
395
|
+
};
|
|
396
|
+
vector.strokePath(
|
|
397
|
+
value.elements,
|
|
398
|
+
command.transform,
|
|
399
|
+
.{ .width = stroke_width, .cap = value.cap, .join = .round },
|
|
400
|
+
vector.default_tolerance,
|
|
401
|
+
referenceVectorClip(pixel_rect),
|
|
402
|
+
&sink,
|
|
403
|
+
) catch return error.ReferenceRenderUnsupportedCommand;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
fn drawImage(self: ReferenceRenderSurface, command: RenderCommand, value: DrawImage, draw_bounds: geometry.RectF) Error!void {
|
|
407
|
+
// A draw whose image is absent from the resource set skips: with
|
|
408
|
+
// runtime-registered images, "not registered (yet/anymore)" is a
|
|
409
|
+
// legitimate transient state (an avatar mid-fetch, an id
|
|
410
|
+
// unregistered while a stale tree still references it), and a
|
|
411
|
+
// pure view must not be able to fail presentation with it. A
|
|
412
|
+
// PRESENT image whose pixel buffer is undersized stays a loud
|
|
413
|
+
// error — that is a corrupt resource, not a lifecycle state.
|
|
414
|
+
const image = self.findImage(value.image_id) orelse return;
|
|
415
|
+
if (referenceImagePixelLen(image.width, image.height)) |image_len| {
|
|
416
|
+
if (image.pixels.len < image_len) return error.ReferenceRenderUnsupportedCommand;
|
|
417
|
+
} else return error.ReferenceRenderUnsupportedCommand;
|
|
418
|
+
|
|
419
|
+
const src_rect = referenceImageSourceRect(image, value.src) orelse return;
|
|
420
|
+
const local_dst = referenceImageDestinationRect(value.dst, src_rect, value.fit) orelse return;
|
|
421
|
+
const dst_rect = command.transform.transformRect(local_dst).normalized();
|
|
422
|
+
// The rounded mask applies over the REQUESTED destination (the
|
|
423
|
+
// widget frame), not the fit-expanded rect a `.cover` draw fills.
|
|
424
|
+
const mask_rect = command.transform.transformRect(value.dst).normalized();
|
|
425
|
+
const mask_radius = referenceScaleRadius(value.radius, command.transform);
|
|
426
|
+
const has_mask = mask_radius.top_left > 0 or mask_radius.top_right > 0 or
|
|
427
|
+
mask_radius.bottom_right > 0 or mask_radius.bottom_left > 0;
|
|
428
|
+
const clipped = geometry.RectF.intersection(dst_rect, draw_bounds.normalized());
|
|
429
|
+
const pixel_rect = referencePixelRect(clipped, self.width, self.height) orelse return;
|
|
430
|
+
const image_opacity = std.math.clamp(value.opacity, 0, 1);
|
|
431
|
+
// Scale-once panel (memo-attached surfaces): a destination
|
|
432
|
+
// pixel's sampled color depends only on its offset inside
|
|
433
|
+
// `dst_rect` and the rect's subpixel phase. `(px + 0.5) -
|
|
434
|
+
// dst_rect.x` and `(offset + 0.5) - phase_x` name the SAME exact
|
|
435
|
+
// real number (`px + 0.5`, `offset + 0.5`, `dst_rect.x`, and its
|
|
436
|
+
// exactly-representable fractional part are all exact f32
|
|
437
|
+
// values), and one IEEE subtraction rounds one exact value one
|
|
438
|
+
// way — so a panel filled with the phase-relative formula holds
|
|
439
|
+
// bit-identical samples to direct evaluation at ANY position
|
|
440
|
+
// with the same phase. The expensive linear resampling then runs
|
|
441
|
+
// once per (image content, size, phase) and every later repaint
|
|
442
|
+
// — the cover-loading cascade, a re-opened view, a whole-pixel
|
|
443
|
+
// move — blends from the panel.
|
|
444
|
+
if (self.imageScalePanel(image, value, src_rect, dst_rect, pixel_rect)) |panel| {
|
|
445
|
+
const dst_x0: i64 = @intFromFloat(@floor(dst_rect.x));
|
|
446
|
+
const dst_y0: i64 = @intFromFloat(@floor(dst_rect.y));
|
|
447
|
+
var y = pixel_rect.y;
|
|
448
|
+
while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
|
|
449
|
+
var x = pixel_rect.x;
|
|
450
|
+
while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
|
|
451
|
+
const point = referencePixelCenter(x, y);
|
|
452
|
+
if (!dst_rect.containsPoint(point)) continue;
|
|
453
|
+
if (has_mask and !referencePointInRoundedRect(point, mask_rect, mask_radius)) continue;
|
|
454
|
+
const column: usize = @intCast(@as(i64, @intCast(x)) - dst_x0);
|
|
455
|
+
const row: usize = @intCast(@as(i64, @intCast(y)) - dst_y0);
|
|
456
|
+
const offset = (row * panel.width + column) * 4;
|
|
457
|
+
const sample = [4]u8{ panel.pixels[offset], panel.pixels[offset + 1], panel.pixels[offset + 2], panel.pixels[offset + 3] };
|
|
458
|
+
const index = (y * self.width + x) * 4;
|
|
459
|
+
const dst = [4]u8{
|
|
460
|
+
self.pixels[index + 0],
|
|
461
|
+
self.pixels[index + 1],
|
|
462
|
+
self.pixels[index + 2],
|
|
463
|
+
self.pixels[index + 3],
|
|
464
|
+
};
|
|
465
|
+
const out = blendRgba8(dst, rgba8ToColor(sample), command.opacity * image_opacity);
|
|
466
|
+
self.pixels[index + 0] = out[0];
|
|
467
|
+
self.pixels[index + 1] = out[1];
|
|
468
|
+
self.pixels[index + 2] = out[2];
|
|
469
|
+
self.pixels[index + 3] = out[3];
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
var y = pixel_rect.y;
|
|
475
|
+
while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
|
|
476
|
+
var x = pixel_rect.x;
|
|
477
|
+
while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
|
|
478
|
+
const point = referencePixelCenter(x, y);
|
|
479
|
+
if (!dst_rect.containsPoint(point)) continue;
|
|
480
|
+
if (has_mask and !referencePointInRoundedRect(point, mask_rect, mask_radius)) continue;
|
|
481
|
+
const u = std.math.clamp((point.x - dst_rect.x) / dst_rect.width, 0, 1);
|
|
482
|
+
const v = std.math.clamp((point.y - dst_rect.y) / dst_rect.height, 0, 1);
|
|
483
|
+
const sample = referenceSampleImage(image, src_rect, u, v, value.sampling);
|
|
484
|
+
const index = (y * self.width + x) * 4;
|
|
485
|
+
const dst = [4]u8{
|
|
486
|
+
self.pixels[index + 0],
|
|
487
|
+
self.pixels[index + 1],
|
|
488
|
+
self.pixels[index + 2],
|
|
489
|
+
self.pixels[index + 3],
|
|
490
|
+
};
|
|
491
|
+
const out = blendRgba8(dst, rgba8ToColor(sample), command.opacity * image_opacity);
|
|
492
|
+
self.pixels[index + 0] = out[0];
|
|
493
|
+
self.pixels[index + 1] = out[1];
|
|
494
|
+
self.pixels[index + 2] = out[2];
|
|
495
|
+
self.pixels[index + 3] = out[3];
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/// A borrowed sampled panel for this draw, or null when the draw
|
|
501
|
+
/// samples directly (no memo attached, fractional alignment, too
|
|
502
|
+
/// small to matter, over the pool bounds, or allocation failure).
|
|
503
|
+
/// See the alignment proof at the call site.
|
|
504
|
+
const ImageScalePanel = struct {
|
|
505
|
+
pixels: []const u8,
|
|
506
|
+
width: usize,
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
fn imageScalePanel(self: ReferenceRenderSurface, image: ReferenceImage, value: DrawImage, src_rect: geometry.RectF, dst_rect: geometry.RectF, pixel_rect: ReferencePixelRect) ?ImageScalePanel {
|
|
510
|
+
const memo = self.render_memo orelse return null;
|
|
511
|
+
// Exact-arithmetic bounds: pixel offsets and phases must stay in
|
|
512
|
+
// f32's exact-integer range for the phase-relative identity
|
|
513
|
+
// (2^22 leaves a full bit of headroom under 2^24).
|
|
514
|
+
const limit: f32 = 4_194_304;
|
|
515
|
+
if (!(dst_rect.width > 0 and dst_rect.height > 0)) return null;
|
|
516
|
+
if (@abs(dst_rect.x) > limit or @abs(dst_rect.y) > limit or dst_rect.width > limit or dst_rect.height > limit) return null;
|
|
517
|
+
// The panel spans the rect's full pixel coverage: columns
|
|
518
|
+
// floor(minX) .. ceil(maxX)-1, exactly the extent
|
|
519
|
+
// `referencePixelRect` can ever visit for this rect.
|
|
520
|
+
const x_floor = @floor(dst_rect.x);
|
|
521
|
+
const y_floor = @floor(dst_rect.y);
|
|
522
|
+
const phase_x = dst_rect.x - x_floor;
|
|
523
|
+
const phase_y = dst_rect.y - y_floor;
|
|
524
|
+
const x_end: i64 = referenceCeil(dst_rect.maxX());
|
|
525
|
+
const y_end: i64 = referenceCeil(dst_rect.maxY());
|
|
526
|
+
const panel_width: usize = @intCast(@max(0, x_end - @as(i64, @intFromFloat(x_floor))));
|
|
527
|
+
const panel_height: usize = @intCast(@max(0, y_end - @as(i64, @intFromFloat(y_floor))));
|
|
528
|
+
if (panel_width == 0 or panel_height == 0) return null;
|
|
529
|
+
if (panel_width * panel_height < memo.min_pixels) return null;
|
|
530
|
+
const key = ReferenceRenderMemo.ImageScaleKey{
|
|
531
|
+
.content_hash = render_fingerprints.renderImageFingerprintForResource(value.image_id, image),
|
|
532
|
+
.src_x = src_rect.x,
|
|
533
|
+
.src_y = src_rect.y,
|
|
534
|
+
.src_width = src_rect.width,
|
|
535
|
+
.src_height = src_rect.height,
|
|
536
|
+
.dst_width = dst_rect.width,
|
|
537
|
+
.dst_height = dst_rect.height,
|
|
538
|
+
.phase_x = phase_x,
|
|
539
|
+
.phase_y = phase_y,
|
|
540
|
+
.dst_width_px = panel_width,
|
|
541
|
+
.dst_height_px = panel_height,
|
|
542
|
+
.sampling = @intFromEnum(value.sampling),
|
|
543
|
+
};
|
|
544
|
+
if (memo.findImageScale(key)) |pixels| return .{ .pixels = pixels, .width = panel_width };
|
|
545
|
+
// Fill only when this draw would render the WHOLE panel anyway
|
|
546
|
+
// (fully on-surface, unclipped by the scissor) and the pass has
|
|
547
|
+
// fill budget left: a clipped or over-budget miss samples its
|
|
548
|
+
// visible pixels directly instead of paying a full-panel fill
|
|
549
|
+
// for a panel a re-phasing scroll may never reuse.
|
|
550
|
+
if (pixel_rect.width != panel_width or pixel_rect.height != panel_height) return null;
|
|
551
|
+
if (memo.image_scale_fills_this_pass >= ReferenceRenderMemo.max_image_scale_fills_per_pass) return null;
|
|
552
|
+
const buffer = memo.storeImageScale(key) orelse return null;
|
|
553
|
+
memo.image_scale_fills_this_pass += 1;
|
|
554
|
+
// Fill with the phase-relative formula — bit-identical to the
|
|
555
|
+
// absolute one (see the call site). Edge cells whose center
|
|
556
|
+
// falls outside the rect hold clamped samples the blend loop's
|
|
557
|
+
// containment check never reads.
|
|
558
|
+
var row: usize = 0;
|
|
559
|
+
while (row < panel_height) : (row += 1) {
|
|
560
|
+
const v = std.math.clamp(((@as(f32, @floatFromInt(row)) + 0.5) - phase_y) / dst_rect.height, 0, 1);
|
|
561
|
+
var column: usize = 0;
|
|
562
|
+
while (column < panel_width) : (column += 1) {
|
|
563
|
+
const u = std.math.clamp(((@as(f32, @floatFromInt(column)) + 0.5) - phase_x) / dst_rect.width, 0, 1);
|
|
564
|
+
const sample = referenceSampleImage(image, src_rect, u, v, value.sampling);
|
|
565
|
+
const offset = (row * panel_width + column) * 4;
|
|
566
|
+
buffer[offset] = sample[0];
|
|
567
|
+
buffer[offset + 1] = sample[1];
|
|
568
|
+
buffer[offset + 2] = sample[2];
|
|
569
|
+
buffer[offset + 3] = sample[3];
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
return .{ .pixels = buffer, .width = panel_width };
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
fn drawShadow(self: ReferenceRenderSurface, command: RenderCommand, value: Shadow, draw_bounds: geometry.RectF) Error!void {
|
|
576
|
+
const scale = referenceTransformScale(command.transform);
|
|
577
|
+
const blur_radius = nonNegative(value.blur) * scale;
|
|
578
|
+
const shadow_rect = command.transform.transformRect(referenceSpreadRect(value.rect.normalized().translate(value.offset), value.spread)).normalized();
|
|
579
|
+
if (shadow_rect.isEmpty()) return;
|
|
580
|
+
const shadow_radius = referenceScaleRadius(referenceSpreadRadius(value.radius, value.spread), command.transform);
|
|
581
|
+
const pixel_rect = referencePixelRect(draw_bounds, self.width, self.height) orelse return;
|
|
582
|
+
|
|
583
|
+
// Render memo: the shadow evaluates a rounded-rect distance
|
|
584
|
+
// field per pixel over its whole halo. It reads only the pixel
|
|
585
|
+
// it blends into, so no apron rows join the key.
|
|
586
|
+
const probe = self.memoProbe(pixel_rect, 0, referenceMemoParamsHash(3, command, value));
|
|
587
|
+
if (self.memoReplay(probe, pixel_rect)) return;
|
|
588
|
+
|
|
589
|
+
var y = pixel_rect.y;
|
|
590
|
+
while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
|
|
591
|
+
var x = pixel_rect.x;
|
|
592
|
+
while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
|
|
593
|
+
const point = referencePixelCenter(x, y);
|
|
594
|
+
const distance = referenceDistanceToRoundedRect(point, shadow_rect, shadow_radius);
|
|
595
|
+
const alpha = referenceShadowFalloff(distance, blur_radius);
|
|
596
|
+
if (alpha > 0) self.blendPixel(@intCast(x), @intCast(y), referenceScaleColorAlpha(value.color, alpha), command.opacity);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
self.memoStore(probe, pixel_rect);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
fn drawBlur(self: ReferenceRenderSurface, command: RenderCommand, value: Blur, draw_bounds: geometry.RectF) Error!void {
|
|
604
|
+
const scratch = self.scratch orelse return error.ReferenceRenderUnsupportedCommand;
|
|
605
|
+
const radius = nonNegative(value.radius) * referenceTransformScale(command.transform);
|
|
606
|
+
if (radius <= 0) return;
|
|
607
|
+
|
|
608
|
+
const pixel_rect = referencePixelRect(draw_bounds, self.width, self.height) orelse return;
|
|
609
|
+
const kernel_radius: i64 = @intCast(@max(1, referenceCeil(radius)));
|
|
610
|
+
|
|
611
|
+
// Render memo: the blur is the renderer's most expensive command
|
|
612
|
+
// — an O(kernel²) Gaussian gather per output pixel over what is
|
|
613
|
+
// usually the whole viewport (the modal scrim). Its inputs are
|
|
614
|
+
// the source rows within a kernel-radius apron of the rect (the
|
|
615
|
+
// mix's destination pixels lie inside that region too), so the
|
|
616
|
+
// apron rows join the key.
|
|
617
|
+
const probe = self.memoProbe(pixel_rect, @intCast(kernel_radius), referenceMemoParamsHash(4, command, value));
|
|
618
|
+
if (self.memoReplay(probe, pixel_rect)) return;
|
|
619
|
+
|
|
620
|
+
@memcpy(scratch[0..self.pixels.len], self.pixels);
|
|
621
|
+
const kernel_width: usize = @intCast(kernel_radius * 2 + 1);
|
|
622
|
+
const kernel_sample_count = kernel_width * kernel_width;
|
|
623
|
+
var kernel_storage: [max_reference_blur_kernel_samples]f32 = undefined;
|
|
624
|
+
const kernel: ?[]const f32 = if (kernel_sample_count <= kernel_storage.len)
|
|
625
|
+
referenceBlurKernel(kernel_storage[0..kernel_sample_count], kernel_radius, radius)
|
|
626
|
+
else
|
|
627
|
+
null;
|
|
628
|
+
var y = pixel_rect.y;
|
|
629
|
+
while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
|
|
630
|
+
var x = pixel_rect.x;
|
|
631
|
+
while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
|
|
632
|
+
const x_i: i64 = @intCast(x);
|
|
633
|
+
const y_i: i64 = @intCast(y);
|
|
634
|
+
const blurred = if (kernel) |weights|
|
|
635
|
+
referenceBlurSampleWithKernel(scratch, self.width, self.height, x_i, y_i, kernel_radius, weights)
|
|
636
|
+
else
|
|
637
|
+
referenceBlurSample(scratch, self.width, self.height, x_i, y_i, kernel_radius, radius);
|
|
638
|
+
const index = (y * self.width + x) * 4;
|
|
639
|
+
const dst = [4]u8{
|
|
640
|
+
self.pixels[index + 0],
|
|
641
|
+
self.pixels[index + 1],
|
|
642
|
+
self.pixels[index + 2],
|
|
643
|
+
self.pixels[index + 3],
|
|
644
|
+
};
|
|
645
|
+
const out = referenceMixRgba8(dst, blurred, command.opacity);
|
|
646
|
+
self.pixels[index + 0] = out[0];
|
|
647
|
+
self.pixels[index + 1] = out[1];
|
|
648
|
+
self.pixels[index + 2] = out[2];
|
|
649
|
+
self.pixels[index + 3] = out[3];
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
self.memoStore(probe, pixel_rect);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
fn drawText(self: ReferenceRenderSurface, command: RenderCommand, value: DrawText, draw_bounds: geometry.RectF) Error!void {
|
|
657
|
+
if (value.size <= 0) return;
|
|
658
|
+
|
|
659
|
+
if (value.text_layout) |options| {
|
|
660
|
+
if (self.drawTextLayout(command, value, draw_bounds, options)) {
|
|
661
|
+
return;
|
|
662
|
+
} else |err| switch (err) {
|
|
663
|
+
error.TextLayoutLineListFull => {},
|
|
664
|
+
else => return err,
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
const line_height = value.size * 1.25;
|
|
669
|
+
const baseline = value.origin.y;
|
|
670
|
+
try self.drawTextLine(command, value, draw_bounds, .{
|
|
671
|
+
.text_start = 0,
|
|
672
|
+
.text_len = value.text.len,
|
|
673
|
+
.glyph_start = 0,
|
|
674
|
+
.glyph_len = value.glyphs.len,
|
|
675
|
+
.bounds = textLineBounds(value, 0, value.text.len, 0, value.glyphs.len, baseline, line_height),
|
|
676
|
+
.baseline = baseline,
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
fn drawTextLayout(self: ReferenceRenderSurface, command: RenderCommand, value: DrawText, draw_bounds: geometry.RectF, options: TextLayoutOptions) Error!void {
|
|
681
|
+
var lines: [max_reference_text_layout_lines]TextLine = undefined;
|
|
682
|
+
const layout = try layoutTextRun(value, options, &lines);
|
|
683
|
+
for (layout.lines) |line| {
|
|
684
|
+
try self.drawTextLine(command, value, draw_bounds, line);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
fn drawTextLine(self: ReferenceRenderSurface, command: RenderCommand, value: DrawText, draw_bounds: geometry.RectF, line: TextLine) Error!void {
|
|
689
|
+
if (line.glyph_len > 0 and line.glyph_start < value.glyphs.len) {
|
|
690
|
+
// An elided line inks only its kept prefix, then the marker.
|
|
691
|
+
const glyph_end = @min(value.glyphs.len, line.glyph_start + line.paintedGlyphLen());
|
|
692
|
+
const raw_bounds = textLineBounds(value, line.text_start, line.paintedTextLen(), line.glyph_start, line.paintedGlyphLen(), line.baseline, line.bounds.height);
|
|
693
|
+
const first_x = if (line.glyph_start < value.glyphs.len) value.glyphs[line.glyph_start].x else 0;
|
|
694
|
+
const dx = line.bounds.x - raw_bounds.x;
|
|
695
|
+
for (value.glyphs[line.glyph_start..glyph_end]) |glyph| {
|
|
696
|
+
const width = estimatedGlyphAdvance(glyph, value.size);
|
|
697
|
+
const pen_x = value.origin.x + glyph.x - first_x + dx;
|
|
698
|
+
const baseline = line.baseline + glyph.y;
|
|
699
|
+
const glyph_rect = geometry.RectF.init(pen_x, baseline - value.size, width, value.size);
|
|
700
|
+
const codepoint = referenceGlyphCodepoint(value.text, glyph.text_start, glyph.text_len);
|
|
701
|
+
self.drawGlyphBox(command, value, draw_bounds, codepoint, pen_x, baseline, glyph_rect);
|
|
702
|
+
}
|
|
703
|
+
self.drawTextLineEllipsis(command, value, draw_bounds, line);
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
// Without shaped glyphs the pen walks the same per-cluster
|
|
708
|
+
// advances layout measured with — the injected provider when the
|
|
709
|
+
// run carries one, the deterministic estimator otherwise — so
|
|
710
|
+
// painted lines end exactly at their measured bounds. Walking the
|
|
711
|
+
// raw estimator here while a provider measured the line dropped
|
|
712
|
+
// one tail glyph per multibyte codepoint: the flat 0.65em
|
|
713
|
+
// multibyte estimate overshot the provider's real advances and
|
|
714
|
+
// pushed the tail past the measured clip bounds.
|
|
715
|
+
const measure = if (value.text_layout) |options| options.measure else null;
|
|
716
|
+
const end = @min(value.text.len, line.text_start + line.paintedTextLen());
|
|
717
|
+
var text_offset: usize = line.text_start;
|
|
718
|
+
var x = line.bounds.x;
|
|
719
|
+
while (text_offset < end) {
|
|
720
|
+
const next_offset = nextTextOffset(value.text, text_offset);
|
|
721
|
+
const advance = measureTextAdvance(measure, value.font_id, value.size, value.text, line.text_start, text_offset, next_offset);
|
|
722
|
+
defer {
|
|
723
|
+
text_offset = next_offset;
|
|
724
|
+
x += advance;
|
|
725
|
+
}
|
|
726
|
+
if (isReferenceTextSpace(value.text[text_offset])) continue;
|
|
727
|
+
const glyph_rect = geometry.RectF.init(x, line.baseline - value.size, advance, value.size);
|
|
728
|
+
const codepoint = referenceGlyphCodepoint(value.text, text_offset, next_offset - text_offset);
|
|
729
|
+
self.drawGlyphBox(command, value, draw_bounds, codepoint, x, line.baseline, glyph_rect);
|
|
730
|
+
}
|
|
731
|
+
self.drawTextLineEllipsis(command, value, draw_bounds, line);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/// The trailing ellipsis of an elided line, drawn at the painted
|
|
735
|
+
/// right edge with the advance layout reserved for it — the same
|
|
736
|
+
/// codepoint-to-outline path every glyph takes, so a face lacking
|
|
737
|
+
/// U+2026 falls back to the documented block treatment.
|
|
738
|
+
fn drawTextLineEllipsis(self: ReferenceRenderSurface, command: RenderCommand, value: DrawText, draw_bounds: geometry.RectF, line: TextLine) void {
|
|
739
|
+
if (!line.hasEllipsis()) return;
|
|
740
|
+
const pen_x = line.bounds.maxX() - line.ellipsis_advance;
|
|
741
|
+
const glyph_rect = geometry.RectF.init(pen_x, line.baseline - value.size, line.ellipsis_advance, value.size);
|
|
742
|
+
self.drawGlyphBox(command, value, draw_bounds, text_ellipsis_codepoint, pen_x, line.baseline, glyph_rect);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/// Paint one glyph: the real Geist outline through the vector core
|
|
746
|
+
/// when the codepoint resolves, the historical block rect otherwise
|
|
747
|
+
/// (unmapped codepoints, glyphs beyond the outline budgets, or draws
|
|
748
|
+
/// carrying no text bytes). Layout is untouched — the pen position
|
|
749
|
+
/// and advance still come from the deterministic estimator.
|
|
750
|
+
fn drawGlyphBox(
|
|
751
|
+
self: ReferenceRenderSurface,
|
|
752
|
+
command: RenderCommand,
|
|
753
|
+
value: DrawText,
|
|
754
|
+
draw_bounds: geometry.RectF,
|
|
755
|
+
codepoint: ?u21,
|
|
756
|
+
pen_x: f32,
|
|
757
|
+
baseline: f32,
|
|
758
|
+
block_rect: geometry.RectF,
|
|
759
|
+
) void {
|
|
760
|
+
if (codepoint) |cp| {
|
|
761
|
+
if (self.drawGlyphOutline(command, value, draw_bounds, cp, pen_x, baseline, block_rect.width)) return;
|
|
762
|
+
}
|
|
763
|
+
self.fillTextRect(command.transform.transformRect(block_rect).normalized(), draw_bounds, value.color, command.opacity);
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/// True when the glyph was handled (drawn, or intentionally empty
|
|
767
|
+
/// like a space); false requests the block fallback.
|
|
768
|
+
fn drawGlyphOutline(
|
|
769
|
+
self: ReferenceRenderSurface,
|
|
770
|
+
command: RenderCommand,
|
|
771
|
+
value: DrawText,
|
|
772
|
+
draw_bounds: geometry.RectF,
|
|
773
|
+
codepoint: u21,
|
|
774
|
+
pen_x: f32,
|
|
775
|
+
baseline: f32,
|
|
776
|
+
cell_advance: f32,
|
|
777
|
+
) bool {
|
|
778
|
+
const face = referenceFaceForFontId(self.fonts, value.font_id);
|
|
779
|
+
const glyph = face.glyphIndex(codepoint);
|
|
780
|
+
if (glyph == 0) return false;
|
|
781
|
+
|
|
782
|
+
// Center the outline's natural advance inside its layout cell when
|
|
783
|
+
// the cell is wider. With the bundled mono face this is the
|
|
784
|
+
// identity for every covered glyph (natural advance == the 0.6 em
|
|
785
|
+
// cell layout charges, so inset 0), and sans cells equal their
|
|
786
|
+
// natural advance too — the inset stays for any future face whose
|
|
787
|
+
// ink runs narrower than its layout cell.
|
|
788
|
+
const natural_advance = value.size * (face.advance(glyph) / face.units_per_em);
|
|
789
|
+
const cell_inset = @max(0, (cell_advance - natural_advance) * 0.5);
|
|
790
|
+
|
|
791
|
+
const scale = value.size / face.units_per_em;
|
|
792
|
+
// Font units are y-up; bake the flip and em scaling into the pen
|
|
793
|
+
// placement, then apply the command transform on top.
|
|
794
|
+
const local = Affine{ .a = scale, .b = 0, .c = 0, .d = -scale, .tx = pen_x + cell_inset, .ty = baseline };
|
|
795
|
+
const total = command.transform.multiply(local);
|
|
796
|
+
|
|
797
|
+
var builder = vector.PathBuilder(reference_glyph_path_capacity){};
|
|
798
|
+
face.glyphOutline(glyph, total, &builder) catch return false;
|
|
799
|
+
if (builder.slice().len == 0) return true; // Space: nothing to ink.
|
|
800
|
+
|
|
801
|
+
const pixel_rect = referencePixelRect(draw_bounds, self.width, self.height) orelse return true;
|
|
802
|
+
var sink = ReferenceCoverageSink{
|
|
803
|
+
.surface = self,
|
|
804
|
+
.fill = .{ .color = value.color },
|
|
805
|
+
.transform = command.transform,
|
|
806
|
+
.opacity = command.opacity,
|
|
807
|
+
};
|
|
808
|
+
// The outline is already in device space; TrueType interiorness
|
|
809
|
+
// is the nonzero rule.
|
|
810
|
+
vector.fillPath(
|
|
811
|
+
builder.slice(),
|
|
812
|
+
Affine.identity(),
|
|
813
|
+
.nonzero,
|
|
814
|
+
vector.default_tolerance,
|
|
815
|
+
referenceVectorClip(pixel_rect),
|
|
816
|
+
&sink,
|
|
817
|
+
) catch return false;
|
|
818
|
+
return true;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
fn fillTextRect(self: ReferenceRenderSurface, rect: geometry.RectF, draw_bounds: geometry.RectF, color: Color, opacity: f32) void {
|
|
822
|
+
const clipped = geometry.RectF.intersection(rect, draw_bounds.normalized());
|
|
823
|
+
const pixel_rect = referencePixelRect(clipped, self.width, self.height) orelse return;
|
|
824
|
+
var y = pixel_rect.y;
|
|
825
|
+
while (y < pixel_rect.y + pixel_rect.height) : (y += 1) {
|
|
826
|
+
var x = pixel_rect.x;
|
|
827
|
+
while (x < pixel_rect.x + pixel_rect.width) : (x += 1) {
|
|
828
|
+
self.blendPixel(@intCast(x), @intCast(y), color, opacity);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
fn blendPixel(self: ReferenceRenderSurface, x: usize, y: usize, color: Color, opacity: f32) void {
|
|
834
|
+
const index = (y * self.width + x) * 4;
|
|
835
|
+
const dst = [4]u8{
|
|
836
|
+
self.pixels[index + 0],
|
|
837
|
+
self.pixels[index + 1],
|
|
838
|
+
self.pixels[index + 2],
|
|
839
|
+
self.pixels[index + 3],
|
|
840
|
+
};
|
|
841
|
+
const out = blendRgba8(dst, color, opacity);
|
|
842
|
+
self.pixels[index + 0] = out[0];
|
|
843
|
+
self.pixels[index + 1] = out[1];
|
|
844
|
+
self.pixels[index + 2] = out[2];
|
|
845
|
+
self.pixels[index + 3] = out[3];
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
fn findImage(self: ReferenceRenderSurface, id: ImageId) ?ReferenceImage {
|
|
849
|
+
return findReferenceImage(self.images, id);
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
|
|
853
|
+
/// Per-pixel coverage sink for the vector core: samples the fill at the
|
|
854
|
+
/// pixel center and blends with the coverage folded into alpha.
|
|
855
|
+
const ReferenceCoverageSink = struct {
|
|
856
|
+
surface: ReferenceRenderSurface,
|
|
857
|
+
fill: Fill,
|
|
858
|
+
transform: Affine,
|
|
859
|
+
opacity: f32,
|
|
860
|
+
|
|
861
|
+
pub fn pixel(self: *ReferenceCoverageSink, x: i32, y: i32, coverage: f32) void {
|
|
862
|
+
if (x < 0 or y < 0) return;
|
|
863
|
+
const px: usize = @intCast(x);
|
|
864
|
+
const py: usize = @intCast(y);
|
|
865
|
+
if (px >= self.surface.width or py >= self.surface.height) return;
|
|
866
|
+
const point = referencePixelCenter(px, py);
|
|
867
|
+
const color = referenceSampleFill(self.fill, self.transform, point);
|
|
868
|
+
self.surface.blendPixel(px, py, referenceScaleColorAlpha(color, coverage), self.opacity);
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
fn referenceVectorClip(pixel_rect: ReferencePixelRect) vector.ClipRect {
|
|
873
|
+
return .{
|
|
874
|
+
.x0 = @intCast(pixel_rect.x),
|
|
875
|
+
.y0 = @intCast(pixel_rect.y),
|
|
876
|
+
.x1 = @intCast(pixel_rect.x + pixel_rect.width),
|
|
877
|
+
.y1 = @intCast(pixel_rect.y + pixel_rect.height),
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
fn referenceMixRgba8(a: [4]u8, b: [4]u8, t: f32) [4]u8 {
|
|
882
|
+
const value = std.math.clamp(t, 0, 1);
|
|
883
|
+
return .{
|
|
884
|
+
referenceMixByte(a[0], b[0], value),
|
|
885
|
+
referenceMixByte(a[1], b[1], value),
|
|
886
|
+
referenceMixByte(a[2], b[2], value),
|
|
887
|
+
referenceMixByte(a[3], b[3], value),
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
fn referenceMixByte(a: u8, b: u8, t: f32) u8 {
|
|
892
|
+
const start = @as(f32, @floatFromInt(a));
|
|
893
|
+
const end = @as(f32, @floatFromInt(b));
|
|
894
|
+
return @intFromFloat(@round(start + (end - start) * t));
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
const ReferencePixelRect = struct {
|
|
898
|
+
x: usize = 0,
|
|
899
|
+
y: usize = 0,
|
|
900
|
+
width: usize = 0,
|
|
901
|
+
height: usize = 0,
|
|
902
|
+
};
|
|
903
|
+
|
|
904
|
+
/// Hash a command's parameters for the render memo key: the kind tag
|
|
905
|
+
/// keeps different command types with coincidentally equal fields apart,
|
|
906
|
+
/// and `command.opacity` + `command.transform` join the value struct
|
|
907
|
+
/// because the pixel loops read all three. Clip needs no hashing — the
|
|
908
|
+
/// planner folds it into `command.bounds`, which reaches the key through
|
|
909
|
+
/// the pixel rect.
|
|
910
|
+
fn referenceMemoParamsHash(kind: u8, command: RenderCommand, value: anytype) u64 {
|
|
911
|
+
var hasher = std.hash.Wyhash.init(0x9e37_79b9);
|
|
912
|
+
referenceMemoHashValue(&hasher, kind);
|
|
913
|
+
referenceMemoHashValue(&hasher, command.opacity);
|
|
914
|
+
referenceMemoHashValue(&hasher, command.transform);
|
|
915
|
+
referenceMemoHashValue(&hasher, value);
|
|
916
|
+
return hasher.final();
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
/// Recursively hash a plain value BIT-EXACTLY: floats hash their bit
|
|
920
|
+
/// patterns (so -0.0 and 0.0, or two NaNs, are distinct keys — stricter
|
|
921
|
+
/// than `==`, which can only cause a spurious miss, never a wrong hit).
|
|
922
|
+
/// Supports exactly the shapes command values are built from; anything
|
|
923
|
+
/// else is a compile error so a new field can't silently escape the key.
|
|
924
|
+
fn referenceMemoHashValue(hasher: *std.hash.Wyhash, value: anytype) void {
|
|
925
|
+
const T = @TypeOf(value);
|
|
926
|
+
switch (@typeInfo(T)) {
|
|
927
|
+
.float => {
|
|
928
|
+
const Bits = std.meta.Int(.unsigned, @bitSizeOf(T));
|
|
929
|
+
const bits: Bits = @bitCast(value);
|
|
930
|
+
hasher.update(std.mem.asBytes(&bits));
|
|
931
|
+
},
|
|
932
|
+
.int => hasher.update(std.mem.asBytes(&value)),
|
|
933
|
+
.bool => hasher.update(&[1]u8{@intFromBool(value)}),
|
|
934
|
+
.@"enum" => referenceMemoHashValue(hasher, @intFromEnum(value)),
|
|
935
|
+
.optional => {
|
|
936
|
+
if (value) |inner| {
|
|
937
|
+
hasher.update(&[1]u8{1});
|
|
938
|
+
referenceMemoHashValue(hasher, inner);
|
|
939
|
+
} else {
|
|
940
|
+
hasher.update(&[1]u8{0});
|
|
941
|
+
}
|
|
942
|
+
},
|
|
943
|
+
.@"struct" => |info| {
|
|
944
|
+
inline for (info.fields) |field| referenceMemoHashValue(hasher, @field(value, field.name));
|
|
945
|
+
},
|
|
946
|
+
.@"union" => {
|
|
947
|
+
switch (value) {
|
|
948
|
+
inline else => |inner, tag| {
|
|
949
|
+
referenceMemoHashValue(hasher, @intFromEnum(tag));
|
|
950
|
+
referenceMemoHashValue(hasher, inner);
|
|
951
|
+
},
|
|
952
|
+
}
|
|
953
|
+
},
|
|
954
|
+
.pointer => |info| {
|
|
955
|
+
// Slices of plain items (a gradient's stops). Length first so
|
|
956
|
+
// concatenations can't collide.
|
|
957
|
+
comptime std.debug.assert(info.size == .slice);
|
|
958
|
+
referenceMemoHashValue(hasher, value.len);
|
|
959
|
+
for (value) |item| referenceMemoHashValue(hasher, item);
|
|
960
|
+
},
|
|
961
|
+
.array => {
|
|
962
|
+
for (value) |item| referenceMemoHashValue(hasher, item);
|
|
963
|
+
},
|
|
964
|
+
else => @compileError("unsupported memo param type " ++ @typeName(T)),
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
fn referenceCommandBounds(command: RenderCommand, scissor: ?geometry.RectF) ?geometry.RectF {
|
|
969
|
+
var bounds = command.bounds.normalized();
|
|
970
|
+
if (scissor) |rect| {
|
|
971
|
+
bounds = geometry.RectF.intersection(bounds, rect.normalized());
|
|
972
|
+
}
|
|
973
|
+
return if (bounds.isEmpty()) null else bounds;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
fn referencePassScale(scale: f32) f32 {
|
|
977
|
+
if (!std.math.isFinite(scale) or scale <= 0) return 1;
|
|
978
|
+
return scale;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
fn referenceScaleCommand(command: RenderCommand, scale: f32) RenderCommand {
|
|
982
|
+
if (scale == 1) return command;
|
|
983
|
+
var scaled = command;
|
|
984
|
+
const transform = Affine.scale(scale, scale);
|
|
985
|
+
scaled.transform = transform.multiply(command.transform);
|
|
986
|
+
scaled.local_bounds = referenceScaleRect(command.local_bounds, scale);
|
|
987
|
+
scaled.bounds = referenceScaleRect(command.bounds, scale);
|
|
988
|
+
if (command.clip) |clip| scaled.clip = referenceScaleRect(clip, scale);
|
|
989
|
+
return scaled;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
fn referenceScaleRect(rect: geometry.RectF, scale: f32) geometry.RectF {
|
|
993
|
+
return geometry.RectF.init(rect.x * scale, rect.y * scale, rect.width * scale, rect.height * scale);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
fn referencePixelCenter(x: usize, y: usize) geometry.PointF {
|
|
998
|
+
return geometry.PointF.init(@as(f32, @floatFromInt(x)) + 0.5, @as(f32, @floatFromInt(y)) + 0.5);
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
fn referenceSampleFill(fill: Fill, transform: Affine, point: geometry.PointF) Color {
|
|
1002
|
+
return switch (fill) {
|
|
1003
|
+
.color => |color| color,
|
|
1004
|
+
.linear_gradient => |gradient| referenceSampleLinearGradient(gradient, transform, point),
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
fn isReferenceTextSpace(byte: u8) bool {
|
|
1009
|
+
return byte == '\n' or byte == '\r' or byte == '\t' or byte == ' ';
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
/// The face a run's font id inks with: a runtime-registered face when
|
|
1013
|
+
/// the id matches one (validated at registration, so a registered id
|
|
1014
|
+
/// always resolves — no render-time fallback decision), otherwise the
|
|
1015
|
+
/// bundled mapping — the mono face for the reserved mono id, the sans
|
|
1016
|
+
/// face for everything else (the weight and italic span variants keep
|
|
1017
|
+
/// the regular outlines at their estimator advances, exactly as before).
|
|
1018
|
+
/// Glyphs missing from whichever face wins keep the per-glyph notdef
|
|
1019
|
+
/// block fallback in `drawGlyphBox`; a registered face never cascades
|
|
1020
|
+
/// into the bundled faces mid-run.
|
|
1021
|
+
fn referenceFaceForFontId(fonts: []const ReferenceFont, font_id: canvas.FontId) *const font_ttf.Face {
|
|
1022
|
+
for (fonts) |font| {
|
|
1023
|
+
if (font.id == font_id) return font.face;
|
|
1024
|
+
}
|
|
1025
|
+
if (font_id == canvas.default_mono_font_id) return &font_ttf.geist_mono;
|
|
1026
|
+
return &font_ttf.geist_regular;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
/// Decode the first codepoint of the cluster `text[start..start+len]`;
|
|
1030
|
+
/// null when the draw carries no text bytes for the glyph (pure glyph-id
|
|
1031
|
+
/// draws keep their historical block rendering).
|
|
1032
|
+
fn referenceGlyphCodepoint(text: []const u8, start: usize, len: usize) ?u21 {
|
|
1033
|
+
if (len == 0 or start >= text.len) return null;
|
|
1034
|
+
const end = @min(text.len, start + len);
|
|
1035
|
+
const cluster = text[start..end];
|
|
1036
|
+
const seq_len = std.unicode.utf8ByteSequenceLength(cluster[0]) catch return null;
|
|
1037
|
+
if (seq_len > cluster.len) return null;
|
|
1038
|
+
return std.unicode.utf8Decode(cluster[0..seq_len]) catch null;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
fn referenceSampleLinearGradient(gradient: LinearGradient, transform: Affine, point: geometry.PointF) Color {
|
|
1042
|
+
if (gradient.stops.len == 0) return Color.rgba8(0, 0, 0, 0);
|
|
1043
|
+
if (gradient.stops.len == 1) return gradient.stops[0].color;
|
|
1044
|
+
|
|
1045
|
+
const start = transform.transformPoint(gradient.start);
|
|
1046
|
+
const end = transform.transformPoint(gradient.end);
|
|
1047
|
+
const dx = end.x - start.x;
|
|
1048
|
+
const dy = end.y - start.y;
|
|
1049
|
+
const length_sq = dx * dx + dy * dy;
|
|
1050
|
+
const t = if (length_sq <= 0.000001) 0 else ((point.x - start.x) * dx + (point.y - start.y) * dy) / length_sq;
|
|
1051
|
+
|
|
1052
|
+
var previous = gradient.stops[0];
|
|
1053
|
+
if (t <= previous.offset) return previous.color;
|
|
1054
|
+
for (gradient.stops[1..]) |stop| {
|
|
1055
|
+
if (t <= stop.offset) {
|
|
1056
|
+
const span = stop.offset - previous.offset;
|
|
1057
|
+
const local_t = if (@abs(span) <= 0.000001) 1 else std.math.clamp((t - previous.offset) / span, 0, 1);
|
|
1058
|
+
return referenceMixColor(previous.color, stop.color, local_t);
|
|
1059
|
+
}
|
|
1060
|
+
previous = stop;
|
|
1061
|
+
}
|
|
1062
|
+
return previous.color;
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
fn referenceMixColor(a: Color, b: Color, t: f32) Color {
|
|
1066
|
+
const value = std.math.clamp(t, 0, 1);
|
|
1067
|
+
return .{
|
|
1068
|
+
.r = referenceMixSrgb(a.r, b.r, value),
|
|
1069
|
+
.g = referenceMixSrgb(a.g, b.g, value),
|
|
1070
|
+
.b = referenceMixSrgb(a.b, b.b, value),
|
|
1071
|
+
.a = a.a + (b.a - a.a) * value,
|
|
1072
|
+
};
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
fn referenceMixSrgb(a: f32, b: f32, t: f32) f32 {
|
|
1076
|
+
const start = referenceSrgbToLinear(a);
|
|
1077
|
+
const end = referenceSrgbToLinear(b);
|
|
1078
|
+
return referenceLinearToSrgb(start + (end - start) * std.math.clamp(t, 0, 1));
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
fn referenceSrgbToLinear(value: f32) f32 {
|
|
1082
|
+
const channel = std.math.clamp(value, 0, 1);
|
|
1083
|
+
if (channel <= 0.04045) return channel / 12.92;
|
|
1084
|
+
return std.math.pow(f32, (channel + 0.055) / 1.055, 2.4);
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
fn referenceLinearToSrgb(value: f32) f32 {
|
|
1088
|
+
const channel = std.math.clamp(value, 0, 1);
|
|
1089
|
+
if (channel <= 0.0031308) return channel * 12.92;
|
|
1090
|
+
return 1.055 * std.math.pow(f32, channel, 1.0 / 2.4) - 0.055;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
fn referenceScaleColorAlpha(color: Color, alpha: f32) Color {
|
|
1094
|
+
return .{
|
|
1095
|
+
.r = color.r,
|
|
1096
|
+
.g = color.g,
|
|
1097
|
+
.b = color.b,
|
|
1098
|
+
.a = color.a * std.math.clamp(alpha, 0, 1),
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
fn referenceShadowFalloff(distance: f32, blur_radius: f32) f32 {
|
|
1103
|
+
if (blur_radius <= 0) return if (distance <= 0) 1 else 0;
|
|
1104
|
+
const t = std.math.clamp(1 - distance / blur_radius, 0, 1);
|
|
1105
|
+
return t * t * (3 - 2 * t);
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
fn referenceSpreadRect(rect: geometry.RectF, spread: f32) geometry.RectF {
|
|
1109
|
+
const normalized = rect.normalized();
|
|
1110
|
+
if (spread >= 0) return normalized.inflate(geometry.InsetsF.all(spread));
|
|
1111
|
+
return normalized.deflate(geometry.InsetsF.all(-spread));
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
fn referenceSpreadRadius(radius: Radius, spread: f32) Radius {
|
|
1115
|
+
return if (spread >= 0) referenceOutsetRadius(radius, spread) else referenceInsetRadius(radius, -spread);
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
fn referenceDistanceToRoundedRect(point: geometry.PointF, rect: geometry.RectF, radius: Radius) f32 {
|
|
1119
|
+
if (referencePointInRoundedRect(point, rect, radius)) return 0;
|
|
1120
|
+
|
|
1121
|
+
const normalized = rect.normalized();
|
|
1122
|
+
if (normalized.isEmpty()) return 0;
|
|
1123
|
+
|
|
1124
|
+
const max_radius = @min(normalized.width, normalized.height) * 0.5;
|
|
1125
|
+
const top_left = std.math.clamp(nonNegative(radius.top_left), 0, max_radius);
|
|
1126
|
+
const top_right = std.math.clamp(nonNegative(radius.top_right), 0, max_radius);
|
|
1127
|
+
const bottom_right = std.math.clamp(nonNegative(radius.bottom_right), 0, max_radius);
|
|
1128
|
+
const bottom_left = std.math.clamp(nonNegative(radius.bottom_left), 0, max_radius);
|
|
1129
|
+
|
|
1130
|
+
if (point.x < normalized.x + top_left and point.y < normalized.y + top_left) {
|
|
1131
|
+
return referenceDistanceToCircle(point, geometry.PointF.init(normalized.x + top_left, normalized.y + top_left), top_left);
|
|
1132
|
+
}
|
|
1133
|
+
if (point.x >= normalized.maxX() - top_right and point.y < normalized.y + top_right) {
|
|
1134
|
+
return referenceDistanceToCircle(point, geometry.PointF.init(normalized.maxX() - top_right, normalized.y + top_right), top_right);
|
|
1135
|
+
}
|
|
1136
|
+
if (point.x >= normalized.maxX() - bottom_right and point.y >= normalized.maxY() - bottom_right) {
|
|
1137
|
+
return referenceDistanceToCircle(point, geometry.PointF.init(normalized.maxX() - bottom_right, normalized.maxY() - bottom_right), bottom_right);
|
|
1138
|
+
}
|
|
1139
|
+
if (point.x < normalized.x + bottom_left and point.y >= normalized.maxY() - bottom_left) {
|
|
1140
|
+
return referenceDistanceToCircle(point, geometry.PointF.init(normalized.x + bottom_left, normalized.maxY() - bottom_left), bottom_left);
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
const dx = @max(@max(normalized.x - point.x, 0), point.x - normalized.maxX());
|
|
1144
|
+
const dy = @max(@max(normalized.y - point.y, 0), point.y - normalized.maxY());
|
|
1145
|
+
return @sqrt(dx * dx + dy * dy);
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
fn referenceDistanceToCircle(point: geometry.PointF, center: geometry.PointF, radius: f32) f32 {
|
|
1149
|
+
const dx = point.x - center.x;
|
|
1150
|
+
const dy = point.y - center.y;
|
|
1151
|
+
return @max(0, @sqrt(dx * dx + dy * dy) - radius);
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
fn referencePixelRect(rect: geometry.RectF, width: usize, height: usize) ?ReferencePixelRect {
|
|
1155
|
+
const normalized = rect.normalized();
|
|
1156
|
+
if (normalized.isEmpty() or width == 0 or height == 0) return null;
|
|
1157
|
+
const x0 = clampI32(referenceFloor(normalized.minX()), 0, @intCast(width));
|
|
1158
|
+
const y0 = clampI32(referenceFloor(normalized.minY()), 0, @intCast(height));
|
|
1159
|
+
const x1 = clampI32(referenceCeil(normalized.maxX()), 0, @intCast(width));
|
|
1160
|
+
const y1 = clampI32(referenceCeil(normalized.maxY()), 0, @intCast(height));
|
|
1161
|
+
if (x1 <= x0 or y1 <= y0) return null;
|
|
1162
|
+
return .{
|
|
1163
|
+
.x = @intCast(x0),
|
|
1164
|
+
.y = @intCast(y0),
|
|
1165
|
+
.width = @intCast(x1 - x0),
|
|
1166
|
+
.height = @intCast(y1 - y0),
|
|
1167
|
+
};
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
fn referenceImagePixelLen(width: usize, height: usize) ?usize {
|
|
1171
|
+
const pixel_count = std.math.mul(usize, width, height) catch return null;
|
|
1172
|
+
return std.math.mul(usize, pixel_count, 4) catch return null;
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
fn findReferenceImage(images: []const ReferenceImage, id: ImageId) ?ReferenceImage {
|
|
1176
|
+
for (images) |image| {
|
|
1177
|
+
if (image.id == id) return image;
|
|
1178
|
+
}
|
|
1179
|
+
return null;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
fn referenceImageSourceRect(image: ReferenceImage, src: ?geometry.RectF) ?geometry.RectF {
|
|
1183
|
+
const full = geometry.RectF.init(0, 0, @floatFromInt(image.width), @floatFromInt(image.height));
|
|
1184
|
+
const requested = if (src) |rect| rect.normalized() else full;
|
|
1185
|
+
const clipped = geometry.RectF.intersection(requested, full);
|
|
1186
|
+
return if (clipped.isEmpty()) null else clipped;
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
fn referenceImageDestinationRect(dst: geometry.RectF, src: geometry.RectF, fit: ImageFit) ?geometry.RectF {
|
|
1190
|
+
const normalized = dst.normalized();
|
|
1191
|
+
if (normalized.isEmpty() or src.width <= 0 or src.height <= 0) return null;
|
|
1192
|
+
if (fit == .stretch) return normalized;
|
|
1193
|
+
|
|
1194
|
+
const src_aspect = src.width / src.height;
|
|
1195
|
+
const dst_aspect = normalized.width / normalized.height;
|
|
1196
|
+
var width = normalized.width;
|
|
1197
|
+
var height = normalized.height;
|
|
1198
|
+
switch (fit) {
|
|
1199
|
+
.stretch => unreachable,
|
|
1200
|
+
.contain => {
|
|
1201
|
+
if (dst_aspect > src_aspect) {
|
|
1202
|
+
height = normalized.height;
|
|
1203
|
+
width = height * src_aspect;
|
|
1204
|
+
} else {
|
|
1205
|
+
width = normalized.width;
|
|
1206
|
+
height = width / src_aspect;
|
|
1207
|
+
}
|
|
1208
|
+
},
|
|
1209
|
+
.cover => {
|
|
1210
|
+
if (dst_aspect > src_aspect) {
|
|
1211
|
+
width = normalized.width;
|
|
1212
|
+
height = width / src_aspect;
|
|
1213
|
+
} else {
|
|
1214
|
+
height = normalized.height;
|
|
1215
|
+
width = height * src_aspect;
|
|
1216
|
+
}
|
|
1217
|
+
},
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
return geometry.RectF.init(
|
|
1221
|
+
normalized.x + (normalized.width - width) * 0.5,
|
|
1222
|
+
normalized.y + (normalized.height - height) * 0.5,
|
|
1223
|
+
width,
|
|
1224
|
+
height,
|
|
1225
|
+
);
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
const ReferencePremultipliedLinearColor = struct {
|
|
1229
|
+
r: f32 = 0,
|
|
1230
|
+
g: f32 = 0,
|
|
1231
|
+
b: f32 = 0,
|
|
1232
|
+
a: f32 = 0,
|
|
1233
|
+
};
|
|
1234
|
+
|
|
1235
|
+
fn referenceSampleImage(image: ReferenceImage, src: geometry.RectF, u: f32, v: f32, sampling: ImageSampling) [4]u8 {
|
|
1236
|
+
return switch (sampling) {
|
|
1237
|
+
.nearest => referenceSampleImageNearest(image, src, u, v),
|
|
1238
|
+
.linear => referenceSampleImageLinear(image, src, u, v),
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
fn referenceSampleImageNearest(image: ReferenceImage, src: geometry.RectF, u: f32, v: f32) [4]u8 {
|
|
1243
|
+
const sample_x_f = src.x + std.math.clamp(u, 0, 1) * src.width;
|
|
1244
|
+
const sample_y_f = src.y + std.math.clamp(v, 0, 1) * src.height;
|
|
1245
|
+
const x = clampI32(referenceFloor(sample_x_f), 0, @intCast(image.width - 1));
|
|
1246
|
+
const y = clampI32(referenceFloor(sample_y_f), 0, @intCast(image.height - 1));
|
|
1247
|
+
return referenceImagePixel(image, x, y);
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
fn referenceSampleImageLinear(image: ReferenceImage, src: geometry.RectF, u: f32, v: f32) [4]u8 {
|
|
1251
|
+
// Belt over the renderPass-level fill: direct sampler callers (unit
|
|
1252
|
+
// tests, future paths) stay correct. One predictable branch per
|
|
1253
|
+
// output pixel — noise next to the twelve pows the table replaces.
|
|
1254
|
+
ensureSrgbToLinearByteTable();
|
|
1255
|
+
const sample_x_f = src.x + std.math.clamp(u, 0, 1) * src.width - 0.5;
|
|
1256
|
+
const sample_y_f = src.y + std.math.clamp(v, 0, 1) * src.height - 0.5;
|
|
1257
|
+
const x_floor = referenceFloor(sample_x_f);
|
|
1258
|
+
const y_floor = referenceFloor(sample_y_f);
|
|
1259
|
+
const x0 = clampI32(x_floor, 0, @intCast(image.width - 1));
|
|
1260
|
+
const y0 = clampI32(y_floor, 0, @intCast(image.height - 1));
|
|
1261
|
+
const x1 = clampI32(x_floor + 1, 0, @intCast(image.width - 1));
|
|
1262
|
+
const y1 = clampI32(y_floor + 1, 0, @intCast(image.height - 1));
|
|
1263
|
+
const tx = std.math.clamp(sample_x_f - @as(f32, @floatFromInt(x_floor)), 0, 1);
|
|
1264
|
+
const ty = std.math.clamp(sample_y_f - @as(f32, @floatFromInt(y_floor)), 0, 1);
|
|
1265
|
+
|
|
1266
|
+
const top_left = referenceImagePixel(image, x0, y0);
|
|
1267
|
+
const top_right = referenceImagePixel(image, x1, y0);
|
|
1268
|
+
const bottom_left = referenceImagePixel(image, x0, y1);
|
|
1269
|
+
const bottom_right = referenceImagePixel(image, x1, y1);
|
|
1270
|
+
|
|
1271
|
+
const sample = referenceBilinearPremultipliedLinearColor(top_left, top_right, bottom_left, bottom_right, tx, ty);
|
|
1272
|
+
if (sample.a <= 0.000001) return .{ 0, 0, 0, 0 };
|
|
1273
|
+
|
|
1274
|
+
const inverse_alpha = 1 / sample.a;
|
|
1275
|
+
return .{
|
|
1276
|
+
colorChannelToByte(referenceLinearToSrgb(sample.r * inverse_alpha)),
|
|
1277
|
+
colorChannelToByte(referenceLinearToSrgb(sample.g * inverse_alpha)),
|
|
1278
|
+
colorChannelToByte(referenceLinearToSrgb(sample.b * inverse_alpha)),
|
|
1279
|
+
colorChannelToByte(sample.a),
|
|
1280
|
+
};
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
fn referenceImagePixel(image: ReferenceImage, x: i32, y: i32) [4]u8 {
|
|
1284
|
+
const index = (@as(usize, @intCast(y)) * image.width + @as(usize, @intCast(x))) * 4;
|
|
1285
|
+
return .{
|
|
1286
|
+
image.pixels[index + 0],
|
|
1287
|
+
image.pixels[index + 1],
|
|
1288
|
+
image.pixels[index + 2],
|
|
1289
|
+
image.pixels[index + 3],
|
|
1290
|
+
};
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
fn referenceBilinearPremultipliedLinearColor(top_left: [4]u8, top_right: [4]u8, bottom_left: [4]u8, bottom_right: [4]u8, tx: f32, ty: f32) ReferencePremultipliedLinearColor {
|
|
1294
|
+
const top = referenceMixPremultipliedLinearColor(referencePremultiplySrgba8(top_left), referencePremultiplySrgba8(top_right), tx);
|
|
1295
|
+
const bottom = referenceMixPremultipliedLinearColor(referencePremultiplySrgba8(bottom_left), referencePremultiplySrgba8(bottom_right), tx);
|
|
1296
|
+
return referenceMixPremultipliedLinearColor(top, bottom, ty);
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
/// Precomputed `referenceSrgbToLinear(byte / 255.0)` for every byte
|
|
1300
|
+
/// value: the linear image sampler decodes twelve sRGB channels per
|
|
1301
|
+
/// output pixel, and each decode costs a `pow` — the single hottest
|
|
1302
|
+
/// operation in a full-surface raster with photographic covers. Filled
|
|
1303
|
+
/// lazily by evaluating the function itself over its exact 256 possible
|
|
1304
|
+
/// inputs, so lookups are bit-identical to direct evaluation — this
|
|
1305
|
+
/// only moves time, never pixels. The benign-race fill (any two fills
|
|
1306
|
+
/// write identical bytes) keeps the hot path free of atomics.
|
|
1307
|
+
var srgb_to_linear_byte_table: [256]f32 = undefined;
|
|
1308
|
+
var srgb_to_linear_byte_table_ready: bool = false;
|
|
1309
|
+
|
|
1310
|
+
fn ensureSrgbToLinearByteTable() void {
|
|
1311
|
+
if (srgb_to_linear_byte_table_ready) return;
|
|
1312
|
+
for (&srgb_to_linear_byte_table, 0..) |*value, index| {
|
|
1313
|
+
value.* = referenceSrgbToLinear(@as(f32, @floatFromInt(index)) / 255.0);
|
|
1314
|
+
}
|
|
1315
|
+
srgb_to_linear_byte_table_ready = true;
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
fn referencePremultiplySrgba8(pixel: [4]u8) ReferencePremultipliedLinearColor {
|
|
1319
|
+
const alpha = @as(f32, @floatFromInt(pixel[3])) / 255.0;
|
|
1320
|
+
return .{
|
|
1321
|
+
.r = srgb_to_linear_byte_table[pixel[0]] * alpha,
|
|
1322
|
+
.g = srgb_to_linear_byte_table[pixel[1]] * alpha,
|
|
1323
|
+
.b = srgb_to_linear_byte_table[pixel[2]] * alpha,
|
|
1324
|
+
.a = alpha,
|
|
1325
|
+
};
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
fn referenceMixPremultipliedLinearColor(a: ReferencePremultipliedLinearColor, b: ReferencePremultipliedLinearColor, t: f32) ReferencePremultipliedLinearColor {
|
|
1329
|
+
const value = std.math.clamp(t, 0, 1);
|
|
1330
|
+
return .{
|
|
1331
|
+
.r = a.r + (b.r - a.r) * value,
|
|
1332
|
+
.g = a.g + (b.g - a.g) * value,
|
|
1333
|
+
.b = a.b + (b.b - a.b) * value,
|
|
1334
|
+
.a = a.a + (b.a - a.a) * value,
|
|
1335
|
+
};
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
fn referencePointInRoundedRect(point: geometry.PointF, rect: geometry.RectF, radius: Radius) bool {
|
|
1339
|
+
const normalized = rect.normalized();
|
|
1340
|
+
if (!normalized.containsPoint(point)) return false;
|
|
1341
|
+
const max_radius = @min(normalized.width, normalized.height) * 0.5;
|
|
1342
|
+
const top_left = std.math.clamp(nonNegative(radius.top_left), 0, max_radius);
|
|
1343
|
+
const top_right = std.math.clamp(nonNegative(radius.top_right), 0, max_radius);
|
|
1344
|
+
const bottom_right = std.math.clamp(nonNegative(radius.bottom_right), 0, max_radius);
|
|
1345
|
+
const bottom_left = std.math.clamp(nonNegative(radius.bottom_left), 0, max_radius);
|
|
1346
|
+
|
|
1347
|
+
if (point.x < normalized.x + top_left and point.y < normalized.y + top_left) {
|
|
1348
|
+
return referencePointInCorner(point, geometry.PointF.init(normalized.x + top_left, normalized.y + top_left), top_left);
|
|
1349
|
+
}
|
|
1350
|
+
if (point.x >= normalized.maxX() - top_right and point.y < normalized.y + top_right) {
|
|
1351
|
+
return referencePointInCorner(point, geometry.PointF.init(normalized.maxX() - top_right, normalized.y + top_right), top_right);
|
|
1352
|
+
}
|
|
1353
|
+
if (point.x >= normalized.maxX() - bottom_right and point.y >= normalized.maxY() - bottom_right) {
|
|
1354
|
+
return referencePointInCorner(point, geometry.PointF.init(normalized.maxX() - bottom_right, normalized.maxY() - bottom_right), bottom_right);
|
|
1355
|
+
}
|
|
1356
|
+
if (point.x < normalized.x + bottom_left and point.y >= normalized.maxY() - bottom_left) {
|
|
1357
|
+
return referencePointInCorner(point, geometry.PointF.init(normalized.x + bottom_left, normalized.maxY() - bottom_left), bottom_left);
|
|
1358
|
+
}
|
|
1359
|
+
return true;
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
fn referencePointInCorner(point: geometry.PointF, center: geometry.PointF, radius: f32) bool {
|
|
1363
|
+
if (radius <= 0) return false;
|
|
1364
|
+
const dx = point.x - center.x;
|
|
1365
|
+
const dy = point.y - center.y;
|
|
1366
|
+
return dx * dx + dy * dy <= radius * radius;
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
fn referenceScaleRadius(radius: Radius, transform: Affine) Radius {
|
|
1370
|
+
const scale = referenceTransformScale(transform);
|
|
1371
|
+
return .{
|
|
1372
|
+
.top_left = radius.top_left * scale,
|
|
1373
|
+
.top_right = radius.top_right * scale,
|
|
1374
|
+
.bottom_right = radius.bottom_right * scale,
|
|
1375
|
+
.bottom_left = radius.bottom_left * scale,
|
|
1376
|
+
};
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
fn referenceInsetRadius(radius: Radius, inset: f32) Radius {
|
|
1380
|
+
return .{
|
|
1381
|
+
.top_left = @max(0, radius.top_left - inset),
|
|
1382
|
+
.top_right = @max(0, radius.top_right - inset),
|
|
1383
|
+
.bottom_right = @max(0, radius.bottom_right - inset),
|
|
1384
|
+
.bottom_left = @max(0, radius.bottom_left - inset),
|
|
1385
|
+
};
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
fn referenceOutsetRadius(radius: Radius, outset: f32) Radius {
|
|
1389
|
+
return .{
|
|
1390
|
+
.top_left = @max(0, radius.top_left + outset),
|
|
1391
|
+
.top_right = @max(0, radius.top_right + outset),
|
|
1392
|
+
.bottom_right = @max(0, radius.bottom_right + outset),
|
|
1393
|
+
.bottom_left = @max(0, radius.bottom_left + outset),
|
|
1394
|
+
};
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
fn referenceTransformScale(transform: Affine) f32 {
|
|
1398
|
+
const x_scale = @sqrt(transform.a * transform.a + transform.b * transform.b);
|
|
1399
|
+
const y_scale = @sqrt(transform.c * transform.c + transform.d * transform.d);
|
|
1400
|
+
return @max(0.0001, @max(x_scale, y_scale));
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
fn referenceFloor(value: f32) i32 {
|
|
1404
|
+
if (!std.math.isFinite(value)) return 0;
|
|
1405
|
+
return @intFromFloat(@floor(value));
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
fn referenceCeil(value: f32) i32 {
|
|
1409
|
+
if (!std.math.isFinite(value)) return 0;
|
|
1410
|
+
return @intFromFloat(@ceil(value));
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
fn clampI32(value: i32, min_value: i32, max_value: i32) i32 {
|
|
1414
|
+
return @min(@max(value, min_value), max_value);
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
fn colorToRgba8(color: Color) [4]u8 {
|
|
1418
|
+
return .{
|
|
1419
|
+
colorChannelToByte(color.r),
|
|
1420
|
+
colorChannelToByte(color.g),
|
|
1421
|
+
colorChannelToByte(color.b),
|
|
1422
|
+
colorChannelToByte(color.a),
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
fn rgba8ToColor(pixel: [4]u8) Color {
|
|
1427
|
+
return Color.rgba(
|
|
1428
|
+
@as(f32, @floatFromInt(pixel[0])) / 255.0,
|
|
1429
|
+
@as(f32, @floatFromInt(pixel[1])) / 255.0,
|
|
1430
|
+
@as(f32, @floatFromInt(pixel[2])) / 255.0,
|
|
1431
|
+
@as(f32, @floatFromInt(pixel[3])) / 255.0,
|
|
1432
|
+
);
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
fn blendRgba8(dst: [4]u8, src: Color, opacity: f32) [4]u8 {
|
|
1436
|
+
const src_a = std.math.clamp(src.a * std.math.clamp(opacity, 0, 1), 0, 1);
|
|
1437
|
+
const dst_a = @as(f32, @floatFromInt(dst[3])) / 255.0;
|
|
1438
|
+
const out_a = src_a + dst_a * (1 - src_a);
|
|
1439
|
+
if (out_a <= 0) return .{ 0, 0, 0, 0 };
|
|
1440
|
+
|
|
1441
|
+
const dst_r = @as(f32, @floatFromInt(dst[0])) / 255.0;
|
|
1442
|
+
const dst_g = @as(f32, @floatFromInt(dst[1])) / 255.0;
|
|
1443
|
+
const dst_b = @as(f32, @floatFromInt(dst[2])) / 255.0;
|
|
1444
|
+
return .{
|
|
1445
|
+
colorChannelToByte((std.math.clamp(src.r, 0, 1) * src_a + dst_r * dst_a * (1 - src_a)) / out_a),
|
|
1446
|
+
colorChannelToByte((std.math.clamp(src.g, 0, 1) * src_a + dst_g * dst_a * (1 - src_a)) / out_a),
|
|
1447
|
+
colorChannelToByte((std.math.clamp(src.b, 0, 1) * src_a + dst_b * dst_a * (1 - src_a)) / out_a),
|
|
1448
|
+
colorChannelToByte(out_a),
|
|
1449
|
+
};
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
fn colorChannelToByte(value: f32) u8 {
|
|
1453
|
+
return @intFromFloat(@round(std.math.clamp(value, 0, 1) * 255.0));
|
|
1454
|
+
}
|
|
1455
|
+
fn nonNegative(value: f32) f32 {
|
|
1456
|
+
return @max(0, value);
|
|
1457
|
+
}
|