diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 612f9756fc..57416237cf 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -1663,13 +1663,13 @@ (defn- text-selrect-stale? "Check if the WASM-measured dimensions of an auto-grow text shape differ - from its stored selrect (same 0.1px tolerance as the classic renderer)." - [{:keys [id selrect grow-type]}] + from its stored selrect by more than `tolerance`." + [{:keys [id selrect grow-type]} tolerance] (when-let [{:keys [width height]} (get-text-dimensions id)] (case grow-type - :auto-width (or (not (mth/close? width (:width selrect) 0.1)) - (not (mth/close? height (:height selrect) 0.1))) - :auto-height (not (mth/close? height (:height selrect) 0.1)) + :auto-width (or (not (mth/close? width (:width selrect) tolerance)) + (not (mth/close? height (:height selrect) tolerance))) + :auto-height (not (mth/close? height (:height selrect) tolerance)) false))) (defonce ^:private pending-stale-selrect-ids (atom #{})) @@ -1699,16 +1699,32 @@ "Emit the ids of auto-grow text shapes whose selrect no longer matches the measured layout, so the workspace resizes them (data-event instead of a direct call to avoid a circular dependency; see the watcher in - `app.main.data.workspace/initialize-workspace`)." - [shapes] - (let [stale-ids (into [] - (comp (filter cfh/text-shape?) - (filter (comp #{:auto-width :auto-height} :grow-type)) - (filter text-selrect-stale?) - (map :id)) - shapes)] - (when (seq stale-ids) - (schedule-stale-selrect-sync! stale-ids)))) + `app.main.data.workspace/initialize-workspace`). The default tolerance is + 0.1px, as in the classic renderer." + ([shapes] + (sync-stale-text-selrects! shapes 0.1)) + ([shapes tolerance] + (let [stale-ids (into [] + (comp (filter cfh/text-shape?) + (filter (comp #{:auto-width :auto-height} :grow-type)) + (filter #(text-selrect-stale? % tolerance)) + (map :id)) + shapes)] + (when (seq stale-ids) + (schedule-stale-selrect-sync! stale-ids))))) + +(defn- sync-loaded-text-selrects + "Check the auto-grow texts just loaded into WASM, so a text whose stored + size came from elsewhere (e.g. a component sync while its page was closed) + is resized to its layout. Texts still waiting for a font are left to + `relayout-after-fonts!`, which checks them once the font arrives, with + its own 0.1px tolerance. This check runs on every page load, so it uses + 1px: the classic renderer stores whole-pixel sizes and WASM fractional + ones, and a finer tolerance would rewrite the same texts each time a page + opens." + [shapes text-font-state] + (let [pending-ids (or (shape-ids-for-pending-fonts text-font-state) #{})] + (sync-stale-text-selrects! (into [] (remove (comp pending-ids :id)) shapes) 1))) (defn- relayout-after-fonts! "Relayout text shapes once their pending fonts have resolved. Font fetches @@ -1868,7 +1884,8 @@ ;; is false). Each call touch_shape → touched_ids. (let [text-ids (into [] (comp (filter cfh/text-shape?) (map :id)) shapes)] (when (seq text-ids) - (update-text-layouts text-ids))) + (update-text-layouts text-ids) + (sync-loaded-text-selrects shapes text-font-state-acc))) (if render-callback (render-callback) (request-render "set-objects-complete")) @@ -1986,7 +2003,8 @@ (if render-callback (render-callback) (request-render "set-objects-sync-complete")) - (ug/dispatch! (ug/event "penpot:wasm:set-objects"))))))) + (ug/dispatch! (ug/event "penpot:wasm:set-objects")))) + (sync-loaded-text-selrects shapes text-font-state)))) (defn- shapes-in-tree-order "Returns shapes sorted in tree order (parents before children). diff --git a/frontend/test/frontend_tests/render_wasm/stale_text_selrects_test.cljs b/frontend/test/frontend_tests/render_wasm/stale_text_selrects_test.cljs new file mode 100644 index 0000000000..55b17b0046 --- /dev/null +++ b/frontend/test/frontend_tests/render_wasm/stale_text_selrects_test.cljs @@ -0,0 +1,160 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.render-wasm.stale-text-selrects-test + "A page load must check every auto-grow text whose stored size no longer + matches its WASM layout, not only the texts that waited for a font. A copy + synced while its page was closed keeps the main's size otherwise." + (:require + [app.common.render-wasm.serialize-shape :as serialize-shape] + [app.common.render-wasm.wasm :as wasm] + [app.common.uuid :as uuid] + [app.main.store :as st] + [app.render-wasm.api :as wasm.api] + [app.util.timers :as timers] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.async :as async] + [frontend-tests.helpers.mock :as mock] + [potok.v2.core :as ptk] + [promesa.core :as p])) + +(defn- text-shape + [grow-type width height] + {:id (uuid/next) + :type :text + :parent-id uuid/zero + :grow-type grow-type + :selrect {:x 0 :y 0 :width width :height height}}) + +(defn- rect-shape + [] + {:id (uuid/next) :type :rect :parent-id uuid/zero}) + +(defn- page-objects + [& shapes] + (into {uuid/zero {:id uuid/zero + :type :frame + :parent-id uuid/zero + :shapes (mapv :id shapes)}} + (map (juxt :id identity)) + shapes)) + +(defn- stale-selrect-ids + "Ids carried by the stale-selrect events among `events`." + [events] + (into #{} + (comp (filter #(= :app.render-wasm.api/stale-text-selrects (ptk/type %))) + (mapcat (comp :ids deref))) + events)) + +(defn- no-pending-fonts + "Host attrs of a shape whose fonts are all loaded." + [& _] + {:thumbnails [] :full [] :font-face-keys #{} :pending-font-face-keys #{}}) + +(defn- run-with-wasm + "Run `f` with the WASM boundaries mocked and resolve to the events emitted + to the store. `dims` maps shape id to its measured layout size, + `host-attrs` stands in for the per-shape upload and `process-pending` + replaces the pending fetches (a no-op unless given). `f` may return a + promise, which is awaited." + [{:keys [dims host-attrs process-pending]} f] + (let [emitted (atom []) + host-attrs (or host-attrs no-pending-fonts) + process-pending (or process-pending (mock/stub (constantly nil)))] + (->> (mock/with-mocks* + {wasm/live? (mock/stub (constantly true)) + wasm/internal-module #js {"_set_view_end" (fn [& _] nil) + "_begin_loading" (fn [& _] nil) + "_end_loading" (fn [& _] nil) + "_update_shape_text_layout_for" (fn [& _] nil)} + serialize-shape/serialize-shapes-batch (mock/stub (fn [shapes _] shapes)) + ;; Private: the only seam that feeds the font state of the load. + #_{:clj-kondo/ignore [:private-call]} + wasm.api/set-object-host-attrs host-attrs + wasm.api/process-pending process-pending + wasm.api/request-render (mock/stub (constantly nil)) + wasm.api/get-text-dimensions (mock/stub (fn [id] (get dims id))) + wasm.api/listen-tiles-render-complete-once! (mock/stub (fn [f] (f))) + timers/schedule-on-idle mock/schedule-on-idle-mock + st/emit! (mock/stub (fn [& events] (swap! emitted into events) nil))} + (await (f)) + (await (async/settle))) + (p/fmap (fn [_] @emitted))))) + +(defn- load-page + "Load `objects` through `wasm.api/set-objects`, forcing the sync path used + by small pages." + ([objects dims] + (load-page objects dims nil)) + ([objects dims host-attrs] + (run-with-wasm {:dims dims :host-attrs host-attrs} + #(wasm.api/set-objects objects nil nil true)))) + +(defn- load-large-page + "Load `objects` through `wasm.api/set-objects` without forcing, so a page + with enough shapes takes the chunked async path. Resolves once the load + has rendered." + [objects dims] + (run-with-wasm {:dims dims} + #(let [rendered (p/deferred)] + (wasm.api/set-objects objects (fn [] (p/resolve rendered nil)) nil false) + rendered))) + +(t/deftest ^:async page-load-requests-resize-for-stale-auto-height-text + (let [text (text-shape :auto-height 100 17) + events (await (load-page (page-objects text) + {(:id text) {:width 100 :height 34}}))] + (t/is (= #{(:id text)} (stale-selrect-ids events)) + "the text stored at the main's height is resized on page load"))) + +(t/deftest ^:async large-page-load-requests-resize-for-stale-text + (let [text (text-shape :auto-height 100 17) + shapes (conj (vec (repeatedly (dec wasm.api/ASYNC_THRESHOLD) rect-shape)) text) + events (await (load-large-page (apply page-objects shapes) + {(:id text) {:width 100 :height 34}}))] + (t/is (= #{(:id text)} (stale-selrect-ids events)) + "a page loaded in chunks checks its texts too"))) + +(t/deftest ^:async page-load-ignores-sub-pixel-gaps + (let [text (text-shape :auto-width 100 17) + events (await (load-page (page-objects text) + {(:id text) {:width 100.5 :height 17.5}}))] + (t/is (empty? (stale-selrect-ids events)) + "rounding differences between renderers do not trigger a resize"))) + +(t/deftest ^:async page-load-skips-fixed-text + (let [text (text-shape :fixed 100 17) + events (await (load-page (page-objects text) + {(:id text) {:width 100 :height 34}}))] + (t/is (empty? (stale-selrect-ids events)) + "fixed text boxes are never resized to their layout"))) + +(t/deftest ^:async page-load-skips-texts-waiting-for-a-font + (let [waiting (text-shape :auto-height 100 17) + ready (text-shape :auto-height 100 17) + events (await (load-page + (page-objects waiting ready) + {(:id waiting) {:width 100 :height 34} + (:id ready) {:width 100 :height 34}} + ;; One of the two stale texts uses a face still loading. + (fn [shape & _] + (if (= (:id shape) (:id waiting)) + {:thumbnails [] :full [] + :font-face-keys #{"font-key"} + :pending-font-face-keys #{"font-key"}} + (no-pending-fonts)))))] + (t/is (= #{(:id ready)} (stale-selrect-ids events)) + "a text waiting for a font is not checked when the page loads"))) + +(t/deftest ^:async pending-shape-updates-do-not-check-text-sizes + (let [text (text-shape :auto-height 100 17) + events (await (run-with-wasm + {:dims {(:id text) {:width 100 :height 34}} + :process-pending wasm.api/process-pending} + #(wasm.api/process-pending [text] [] [] {} nil)))] + (t/is (empty? (stale-selrect-ids events)) + "shape updates while editing do not resize texts to their layout"))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 599274f3a8..38f8716aa2 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -119,6 +119,7 @@ [frontend-tests.render-wasm.process-objects-test] [frontend-tests.render-wasm.serialization-test] [frontend-tests.render-wasm.shape-proxy-test] + [frontend-tests.render-wasm.stale-text-selrects-test] [frontend-tests.render-wasm.text-editor-apply-styles-test] [frontend-tests.render-wasm.text-editor-caret-color-test] [frontend-tests.render-wasm.text-editor-content-range-test] @@ -330,6 +331,7 @@ 'frontend-tests.render-wasm.process-objects-test 'frontend-tests.render-wasm.serialization-test 'frontend-tests.render-wasm.shape-proxy-test + 'frontend-tests.render-wasm.stale-text-selrects-test 'frontend-tests.render-wasm.text-editor-apply-styles-test 'frontend-tests.render-wasm.text-editor-caret-color-test 'frontend-tests.render-wasm.text-editor-content-range-test