diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 546f4c4bdc..3cb9938df9 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -1712,31 +1712,46 @@ (when (seq stale-ids) (schedule-stale-selrect-sync! stale-ids))))) +(defn- shape-ids-for-failed-fonts + "Ids of the texts that use a face that failed to load. WASM lays them out + with a fallback font, so their measured size must not be saved: it would + be wrong and the heal records no undo step." + [{:keys [font-index]}] + (into #{} + (comp (filter f/font-failed?) (mapcat font-index)) + (keys font-index))) + (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 + its own 0.1px tolerance. Texts using a face that failed to load are + skipped: their layout comes from a fallback font and would save a wrong + size with no undo step. 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))) + (let [skip-ids (into (or (shape-ids-for-pending-fonts text-font-state) #{}) + (shape-ids-for-failed-fonts text-font-state))] + (sync-stale-text-selrects! (into [] (remove (comp skip-ids :id)) shapes) 1))) (defn- relayout-after-fonts! "Relayout text shapes once their pending fonts have resolved. Font fetches are deduped per URL, so only shapes that use a not-yet-ready face at upload time need a forced relayout; then re-sync selrects that drifted for those - shapes only." + shapes only, leaving out the texts whose face failed to load." [shapes text-font-state] (let [text-ids (into [] (comp (filter cfh/text-shape?) (map :id)) shapes) affected-ids (or (shape-ids-for-pending-fonts text-font-state) #{}) shapes-by-id (d/index-by :id shapes)] (when (seq text-ids) (if (seq affected-ids) - (let [affected-shapes (into [] (keep shapes-by-id) affected-ids)] + (let [failed-ids (shape-ids-for-failed-fonts text-font-state) + affected-shapes (into [] + (comp (remove failed-ids) (keep shapes-by-id)) + affected-ids)] (force-update-text-layouts affected-ids) (sync-stale-text-selrects! affected-shapes)) (update-text-layouts text-ids))))) diff --git a/frontend/src/app/render_wasm/api/fonts.cljs b/frontend/src/app/render_wasm/api/fonts.cljs index 0b438c5e20..0575c549bb 100644 --- a/frontend/src/app/render_wasm/api/fonts.cljs +++ b/frontend/src/app/render_wasm/api/fonts.cljs @@ -276,6 +276,12 @@ (:style font-data) emoji?)))) +(defn font-failed? + "Returns true when the face behind `font-key` failed to load, so WASM lays + it out with a fallback." + [font-key] + (contains? @failed-font-data-keys font-key)) + (defn font-ready? "Returns true when WASM can lay out with the requested face or its fallback." [font-data] diff --git a/frontend/test/frontend_tests/data/workspace_sync_text_wasm_test.cljs b/frontend/test/frontend_tests/data/workspace_sync_text_wasm_test.cljs index a476ac085b..dcb5a60c60 100644 --- a/frontend/test/frontend_tests/data/workspace_sync_text_wasm_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_sync_text_wasm_test.cljs @@ -22,6 +22,7 @@ [app.common.uuid :as uuid] [app.main.data.changes :as dch] [app.main.data.workspace.libraries :as dwl] + [app.main.data.workspace.thumbnails :as dwt] [app.main.data.workspace.undo :as dwu] [app.main.data.workspace.wasm-text :as dwwt] [beicon.v2.core :as rx] @@ -31,8 +32,14 @@ (def ^:private orig-resize-wasm-text-all dwwt/resize-wasm-text-all) +(def ^:private orig-clear-thumbnail dwt/clear-thumbnail) + (def ^:private resize-calls (atom [])) +(defn- mock-clear-thumbnail + ([_ _] nil) + ([_ _ _ _] nil)) + (defn- mock-resize-wasm-text-all ([ids] (mock-resize-wasm-text-all ids nil)) ([ids opts] @@ -43,9 +50,13 @@ {:before (fn [] (cthi/reset-idmap!) (reset! resize-calls []) - (set! dwwt/resize-wasm-text-all mock-resize-wasm-text-all)) + (set! dwwt/resize-wasm-text-all mock-resize-wasm-text-all) + ;; sync-file clears frame thumbnails on a 200ms timer that + ;; outlives the test and calls rp/cmd! inside later tests. + (set! dwt/clear-thumbnail mock-clear-thumbnail)) :after (fn [] - (set! dwwt/resize-wasm-text-all orig-resize-wasm-text-all))}) + (set! dwwt/resize-wasm-text-all orig-resize-wasm-text-all) + (set! dwt/clear-thumbnail orig-clear-thumbnail))}) (defn- setup-file [& {:keys [grow-type] :or {grow-type :auto-width}}] 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 index 7e42a152e0..2ab20becea 100644 --- a/frontend/test/frontend_tests/render_wasm/stale_text_selrects_test.cljs +++ b/frontend/test/frontend_tests/render_wasm/stale_text_selrects_test.cljs @@ -14,7 +14,9 @@ [app.common.uuid :as uuid] [app.main.store :as st] [app.render-wasm.api :as wasm.api] + [app.render-wasm.api.fonts :as wasm.fonts] [app.util.timers :as timers] + [beicon.v2.core :as rx] [cljs.test :as t :include-macros true] [frontend-tests.helpers.async :as async] [frontend-tests.helpers.mock :as mock] @@ -63,19 +65,21 @@ (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] + `host-attrs` stands in for the per-shape upload, `failed-faces` holds the + font faces that failed to load and `process-pending` replaces the pending + fetches (a no-op unless given). `f` may return a promise, which is + awaited." + [{:keys [dims host-attrs failed-faces 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)} + wasm/internal-module #js {"_set_view_end" (fn [& _] nil) + "_begin_loading" (fn [& _] nil) + "_end_loading" (fn [& _] nil) + "_update_shape_text_layout_for" (fn [& _] nil) + "_force_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]} @@ -83,6 +87,7 @@ 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.fonts/font-failed? (mock/stub #(contains? (or failed-faces #{}) %)) 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))} @@ -96,7 +101,9 @@ ([objects dims] (load-page objects dims nil)) ([objects dims host-attrs] - (run-with-wasm {:dims dims :host-attrs host-attrs} + (load-page objects dims host-attrs #{})) + ([objects dims host-attrs failed-faces] + (run-with-wasm {:dims dims :host-attrs host-attrs :failed-faces failed-faces} #(wasm.api/set-objects objects nil nil true)))) (defn- load-large-page @@ -171,6 +178,25 @@ (t/is (= #{(:id ready)} (stale-selrect-ids events)) "a text waiting for a font is not checked when the page loads"))) +(t/deftest ^:async page-load-skips-texts-using-a-failed-font + (let [failed (text-shape :auto-height 100 17) + ready (text-shape :auto-height 100 17) + events (await (load-page + (page-objects failed ready) + {(:id failed) {:width 100 :height 34} + (:id ready) {:width 100 :height 34}} + ;; WASM counts the failed face as ready and lays the + ;; text out with a fallback font. + (fn [shape & _] + (if (= (:id shape) (:id failed)) + {:thumbnails [] :full [] + :font-face-keys #{"failed-key"} + :pending-font-face-keys #{}} + (no-pending-fonts))) + #{"failed-key"}))] + (t/is (= #{(:id ready)} (stale-selrect-ids events)) + "a text measured with a fallback font keeps its stored size"))) + (t/deftest ^:async pending-shape-updates-do-not-check-text-sizes (let [text (text-shape :auto-height 100 17) events (await (run-with-wasm @@ -179,3 +205,20 @@ #(wasm.api/process-pending [text] [] [] {} nil)))] (t/is (empty? (stale-selrect-ids events)) "shape updates while editing do not resize texts to their layout"))) + +(t/deftest ^:async font-load-skips-texts-whose-font-failed + (let [failed (text-shape :auto-height 100 17) + loaded (text-shape :auto-height 100 17) + font-state {:font-index {"failed-key" #{(:id failed)} + "loaded-key" #{(:id loaded)}} + :pending-faces #{"failed-key" "loaded-key"}} + ;; A pending fetch, so the font relayout runs once it completes. + full [{:key "font" :callback #(rx/of nil)}] + events (await (run-with-wasm + {:dims {(:id failed) {:width 100 :height 34} + (:id loaded) {:width 100 :height 34}} + :failed-faces #{"failed-key"} + :process-pending wasm.api/process-pending} + #(wasm.api/process-pending [failed loaded] [] full font-state nil)))] + (t/is (= #{(:id loaded)} (stale-selrect-ids events)) + "a text laid out with a fallback font after its face failed keeps its stored size")))