mirror of
https://github.com/penpot/penpot.git
synced 2026-11-27 21:57:52 +00:00
🐛 Show a page while its fonts load, holding back only its texts
Opening a file showed a blank page until every font its texts use had loaded, up to 3 seconds: the page reveal waited for all of them. Now the page shows at once, and only a text whose font is still loading stays off the canvas, until its own faces are stored or have failed. Past 3 seconds from the first text held, every text shows, in a fallback face if its font is still missing, and is laid out again when the font arrives. WASM keeps the flag per shape and skips drawing such a text, its shadows included, except in exports and thumbnails. Only a page load holds texts: editing, thumbnails and component renders draw them as before. The fonts of a page also start loading with each batch of shapes sent to WASM, rather than after the whole page was sent and first drawn. AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
90f857c697
commit
e3457b5a06
@ -1665,6 +1665,100 @@
|
||||
(mapcat #(get font-index % []))
|
||||
pending-faces)))
|
||||
|
||||
(defn- set-shape-awaiting-fonts
|
||||
[id awaiting?]
|
||||
(let [buffer (uuid/get-u32 id)]
|
||||
(h/call wasm/internal-module "_set_shape_awaiting_fonts"
|
||||
(aget buffer 0)
|
||||
(aget buffer 1)
|
||||
(aget buffer 2)
|
||||
(aget buffer 3)
|
||||
awaiting?)))
|
||||
|
||||
;; Longest time a text of the page being opened stays off the canvas while
|
||||
;; its font loads, as the block period of CSS `font-display: block`: past
|
||||
;; it, the text shows in a fallback face until the font arrives.
|
||||
(def ^:private ^:const FONT_HOLD_TIMEOUT_MS 3000)
|
||||
|
||||
;; Texts of the page being opened that WASM keeps off the canvas, each with
|
||||
;; the faces it still waits on.
|
||||
(defonce ^:private font-holds* (atom {}))
|
||||
|
||||
;; Releases the texts of `font-holds*` as their faces arrive or time runs out.
|
||||
(defonce ^:private font-holds-subs* (atom nil))
|
||||
|
||||
(defn- stop-font-holds
|
||||
[]
|
||||
(when-let [subs (first (reset-vals! font-holds-subs* nil))]
|
||||
(rx/dispose! subs)))
|
||||
|
||||
(defn- show-held-texts
|
||||
"Draws the held texts `ids` again, laid out anew with the faces WASM holds
|
||||
now. While shapes load, the end of loading lays them out."
|
||||
[ids]
|
||||
(when (and (seq ids) (wasm/live?))
|
||||
(doseq [id ids]
|
||||
(set-shape-awaiting-fonts id false)
|
||||
(when-not @shapes-loading?
|
||||
(f/force-update-text-layout id)))
|
||||
(request-render "held-texts-shown")))
|
||||
|
||||
(defn- release-font-holds
|
||||
"Shows the held texts that `update-holds`, a fn of `font-holds*`, drops."
|
||||
[update-holds]
|
||||
(let [[old new] (swap-vals! font-holds* update-holds)]
|
||||
(when (empty? new)
|
||||
(stop-font-holds))
|
||||
(show-held-texts (into [] (remove #(contains? new %)) (keys old)))))
|
||||
|
||||
(defn- settle-held-face
|
||||
"`holds` once `face` is stored or has failed: the texts waiting on no other
|
||||
face leave it."
|
||||
[holds face]
|
||||
(reduce-kv (fn [acc id faces]
|
||||
(let [faces (disj faces face)]
|
||||
(if (empty? faces) acc (assoc acc id faces))))
|
||||
{}
|
||||
holds))
|
||||
|
||||
(defn- hold-text-for-fonts
|
||||
"Keeps the text `id` of the page being opened off the canvas until its
|
||||
`faces` are stored or have failed, so it never shows in a fallback face
|
||||
first, the rest of the page showing meanwhile. Past
|
||||
`FONT_HOLD_TIMEOUT_MS` from the first text held, all show."
|
||||
[id faces]
|
||||
(when (seq faces)
|
||||
(set-shape-awaiting-fonts id true)
|
||||
(swap! font-holds* assoc id faces)
|
||||
(when (nil? @font-holds-subs*)
|
||||
(reset! font-holds-subs*
|
||||
(->> (rx/merge
|
||||
(->> (rx/merge f/font-stored-stream f/font-storage-failed-stream)
|
||||
(rx/map (fn [face] #(settle-held-face % face))))
|
||||
(->> (rx/timer FONT_HOLD_TIMEOUT_MS)
|
||||
(rx/map (constantly (constantly {})))))
|
||||
(rx/subs! release-font-holds))))))
|
||||
|
||||
(defn- clear-font-holds
|
||||
"Drops the holds of the page opened before: its texts are drawn as soon as
|
||||
they are on a page again."
|
||||
[]
|
||||
(stop-font-holds)
|
||||
(let [ids (keys (first (reset-vals! font-holds* {})))]
|
||||
(when (wasm/live?)
|
||||
(run! #(set-shape-awaiting-fonts % false) ids))))
|
||||
|
||||
(defn- fetch-fonts
|
||||
"Starts the font `fetches` right away, without waiting for the rest of the
|
||||
page: each face reports when it is stored or has failed, and the texts
|
||||
using it are laid out again then."
|
||||
[fetches]
|
||||
(when (seq fetches)
|
||||
(->> (rx/from fetches)
|
||||
(rx/merge-map (fn [{:keys [callback]}]
|
||||
(if (fn? callback) (callback) (rx/empty))))
|
||||
(rx/subs! noop-fn noop-fn))))
|
||||
|
||||
(defn- set-object-host-attrs
|
||||
"Host-specific attrs after structural upload (text/svg-raw/grid; optionally
|
||||
fills/strokes). When `skip-layout?` is true, flex+layout-item were already in
|
||||
@ -1673,7 +1767,10 @@
|
||||
only image fetches remain.
|
||||
|
||||
Always `use-shape` first: after a multi-shape batch the WASM current shape is
|
||||
the last record in the chunk, not this shape."
|
||||
the last record in the chunk, not this shape.
|
||||
|
||||
Returns the font fetches of a text in `:fonts`, apart from its image
|
||||
fetches, so a page load can start them first."
|
||||
[shape skip-layout? & {:keys [skip-fills-strokes?] :or {skip-fills-strokes? false}}]
|
||||
(let [id (dm/get-prop shape :id)
|
||||
type (dm/get-prop shape :type)
|
||||
@ -1709,7 +1806,6 @@
|
||||
(when is-text? (text-font-face-keys-state text-fonts))
|
||||
text-content-pending (when is-text? (upload-text-content id content text-fonts))
|
||||
pending-thumbnails (into [] (concat
|
||||
text-content-pending
|
||||
(when is-text? (set-shape-text-images id content true))
|
||||
(set-shape-fills id fills true write-fills-strokes?)
|
||||
(set-shape-strokes id strokes true write-fills-strokes?)))
|
||||
@ -1717,7 +1813,8 @@
|
||||
(when is-text? (set-shape-text-images id content false))
|
||||
(set-shape-fills id fills false write-fills-strokes?)
|
||||
(set-shape-strokes id strokes false write-fills-strokes?)))]
|
||||
{:thumbnails pending-thumbnails
|
||||
{:fonts (vec text-content-pending)
|
||||
:thumbnails pending-thumbnails
|
||||
:full pending-full
|
||||
:font-face-keys (or font-face-keys #{})
|
||||
:pending-font-face-keys (or pending-font-face-keys #{})})))
|
||||
@ -1729,9 +1826,11 @@
|
||||
(do
|
||||
(perf/begin-measure "set-object")
|
||||
(let [prepared (serialize-shape/serialize-shape shape)
|
||||
result (set-object-host-attrs prepared false)]
|
||||
{:keys [fonts] :as result} (set-object-host-attrs prepared false)]
|
||||
(perf/end-measure "set-object")
|
||||
result))))
|
||||
(-> result
|
||||
(dissoc :fonts)
|
||||
(update :thumbnails #(into fonts %)))))))
|
||||
|
||||
(defn- update-text-layouts
|
||||
"Synchronously update text layouts for all shapes and send rect updates
|
||||
@ -1882,32 +1981,6 @@
|
||||
(rx/tap (fn [_] (force-update-text-layouts text-ids)))
|
||||
(rx/ignore)))))
|
||||
|
||||
;; Longest wait for the fonts of a page before revealing it, as the block
|
||||
;; period of CSS `font-display: block`: past it, its texts show in the
|
||||
;; fallback faces until the fonts arrive.
|
||||
(def ^:private ^:const FONT_REVEAL_TIMEOUT_MS 3000)
|
||||
|
||||
(defn- when-faces-ready
|
||||
"Calls `on-ready` once the faces `text-font-state` waits on are stored or
|
||||
have failed, after relayouting and rendering the texts that use them, or
|
||||
after `FONT_REVEAL_TIMEOUT_MS`. Right away when no face is pending. A
|
||||
page transition begun meanwhile drops the call: its page is gone."
|
||||
[shapes text-font-state on-ready]
|
||||
(let [pending-faces (:pending-faces text-font-state)
|
||||
epoch @transition-epoch*]
|
||||
(if (empty? pending-faces)
|
||||
(on-ready)
|
||||
(->> (rx/race (rx/concat (await-pending-faces pending-faces) (rx/of :faces))
|
||||
(rx/map (constantly :timeout) (rx/timer FONT_REVEAL_TIMEOUT_MS)))
|
||||
(rx/take 1)
|
||||
(rx/subs!
|
||||
(fn [result]
|
||||
(when (= epoch @transition-epoch*)
|
||||
(when (and (= :faces result) (wasm/live?))
|
||||
(relayout-after-fonts! shapes text-font-state)
|
||||
(request-render "faces-ready"))
|
||||
(on-ready))))))))
|
||||
|
||||
(defn process-pending
|
||||
[shapes thumbnails full text-font-state on-complete]
|
||||
(let [pending-thumbnails
|
||||
@ -1980,10 +2053,12 @@
|
||||
"Process up to `BATCH_MAX_SHAPES` shapes starting at `start-index`.
|
||||
|
||||
Structural attrs are uploaded in one `_set_shapes_batch` FFI per chunk;
|
||||
host-specific attrs (fills/strokes/text/grid/path) stay per-shape.
|
||||
host-specific attrs (fills/strokes/text/grid/path) stay per-shape. The
|
||||
fonts of the chunk start loading at once; with `hold-fonts?`, its texts
|
||||
wait for them off the canvas (see `hold-text-for-fonts`).
|
||||
|
||||
Returns {:thumbnails [...] :full [...] :text-font-state {...} :next-index n}"
|
||||
[shapes start-index thumbnails-acc full-acc text-font-state-acc]
|
||||
[shapes start-index thumbnails-acc full-acc text-font-state-acc hold-fonts?]
|
||||
(let [total (count shapes)
|
||||
end-index (min total (+ start-index BATCH_MAX_SHAPES))
|
||||
chunk (into [] (subvec (if (vector? shapes) shapes (vec shapes))
|
||||
@ -1994,23 +2069,29 @@
|
||||
:include-fills-strokes? true})]
|
||||
|
||||
(loop [xs prepared
|
||||
fonts-acc (transient [])
|
||||
t-acc (transient thumbnails-acc)
|
||||
f-acc (transient full-acc)
|
||||
font-state-acc text-font-state-acc]
|
||||
(if-let [shape (first xs)]
|
||||
(let [{:keys [thumbnails full font-face-keys pending-font-face-keys]}
|
||||
(let [{:keys [fonts thumbnails full font-face-keys pending-font-face-keys]}
|
||||
(set-object-host-attrs shape true :skip-fills-strokes? true)]
|
||||
(when hold-fonts?
|
||||
(hold-text-for-fonts (:id shape) pending-font-face-keys))
|
||||
(recur (next xs)
|
||||
(reduce conj! fonts-acc fonts)
|
||||
(reduce conj! t-acc thumbnails)
|
||||
(reduce conj! f-acc full)
|
||||
(acc-text-font-state font-state-acc
|
||||
(:id shape)
|
||||
font-face-keys
|
||||
pending-font-face-keys)))
|
||||
{:thumbnails (persistent! t-acc)
|
||||
:full (persistent! f-acc)
|
||||
:text-font-state font-state-acc
|
||||
:next-index end-index}))))
|
||||
(do
|
||||
(fetch-fonts (persistent! fonts-acc))
|
||||
{:thumbnails (persistent! t-acc)
|
||||
:full (persistent! f-acc)
|
||||
:text-font-state font-state-acc
|
||||
:next-index end-index})))))
|
||||
|
||||
(defn- set-objects-async
|
||||
"Asynchronously process shapes in time-budgeted chunks, yielding to the
|
||||
@ -2025,7 +2106,8 @@
|
||||
;; Process one time-budgeted chunk
|
||||
(let [{:keys [thumbnails full text-font-state next-index]}
|
||||
(process-shapes-chunk shapes index
|
||||
thumbnails-acc full-acc text-font-state-acc)]
|
||||
thumbnails-acc full-acc text-font-state-acc
|
||||
(some? on-shapes-ready))]
|
||||
;; Yield to browser, then continue with next chunk
|
||||
(-> (yield-to-browser)
|
||||
(p/then (fn [_]
|
||||
@ -2034,11 +2116,8 @@
|
||||
(do
|
||||
(perf/end-measure "set-objects")
|
||||
|
||||
;; Notify that shapes are loaded and tiles rebuilt, once
|
||||
;; their fonts are: a text revealed before its font
|
||||
;; shows missing glyphs.
|
||||
(when on-shapes-ready
|
||||
(when-faces-ready shapes text-font-state-acc on-shapes-ready))
|
||||
;; Notify that shapes are loaded and tiles rebuilt
|
||||
(when on-shapes-ready (on-shapes-ready))
|
||||
(if-not (wasm/live?)
|
||||
(do
|
||||
(end-shapes-loading!)
|
||||
@ -2056,9 +2135,15 @@
|
||||
;; Text layouts must run after _end_loading (they
|
||||
;; depend on state that is only correct when loading
|
||||
;; is false). Each call touch_shape → touched_ids.
|
||||
;; A face stored while loading leaves the layout
|
||||
;; its text got in the fallback face: lay it out
|
||||
;; again, unless still held for another face.
|
||||
(let [text-ids (into [] (comp (filter cfh/text-shape?) (map :id)) shapes)]
|
||||
(when (seq text-ids)
|
||||
(update-text-layouts text-ids)
|
||||
(force-update-text-layouts
|
||||
(remove (set (keys @font-holds*))
|
||||
(shape-ids-for-pending-fonts text-font-state-acc)))
|
||||
(sync-loaded-text-selrects shapes text-font-state-acc)))
|
||||
(if render-callback
|
||||
(render-callback)
|
||||
@ -2066,23 +2151,27 @@
|
||||
(ug/dispatch! (ug/event "penpot:wasm:set-objects"))
|
||||
(resolve nil)
|
||||
|
||||
;; Kick off image fetches in the background.
|
||||
;; The promise is already resolved so these don't
|
||||
;; block the caller.
|
||||
;; Kick off image fetches in the background, and
|
||||
;; wait for them and for the faces, whose fetches
|
||||
;; started with their chunk. The promise is already
|
||||
;; resolved so these don't block the caller.
|
||||
(let [pending-thumbnails (d/index-by :key :callback thumbnails-acc)
|
||||
pending-full (d/index-by :key :callback full-acc)]
|
||||
(when (or (seq pending-thumbnails) (seq pending-full))
|
||||
(->> (rx/concat
|
||||
(->> (rx/from (vals pending-thumbnails))
|
||||
(rx/merge-map
|
||||
(fn [callback]
|
||||
(if (fn? callback) (callback) (rx/empty))))
|
||||
(rx/reduce conj []))
|
||||
(->> (rx/from (vals pending-full))
|
||||
(rx/mapcat
|
||||
(fn [callback]
|
||||
(if (fn? callback) (callback) (rx/empty))))
|
||||
(rx/reduce conj [])))
|
||||
pending-full (d/index-by :key :callback full-acc)
|
||||
pending-faces (:pending-faces text-font-state-acc)]
|
||||
(when (or (seq pending-thumbnails) (seq pending-full) (seq pending-faces))
|
||||
(->> (rx/merge
|
||||
(rx/concat
|
||||
(->> (rx/from (vals pending-thumbnails))
|
||||
(rx/merge-map
|
||||
(fn [callback]
|
||||
(if (fn? callback) (callback) (rx/empty))))
|
||||
(rx/reduce conj []))
|
||||
(->> (rx/from (vals pending-full))
|
||||
(rx/mapcat
|
||||
(fn [callback]
|
||||
(if (fn? callback) (callback) (rx/empty))))
|
||||
(rx/reduce conj [])))
|
||||
(await-pending-faces pending-faces))
|
||||
(rx/subs!
|
||||
noop-fn
|
||||
noop-fn
|
||||
@ -2145,28 +2234,33 @@
|
||||
{:include-layout? true
|
||||
:include-fills-strokes? true})
|
||||
total-shapes (count prepared)
|
||||
{:keys [thumbnails full text-font-state]}
|
||||
{:keys [fonts thumbnails full text-font-state]}
|
||||
(loop [index 0
|
||||
fonts-acc (transient [])
|
||||
thumbnails-acc (transient [])
|
||||
full-acc (transient [])
|
||||
font-state-acc empty-text-font-state]
|
||||
(if (< index total-shapes)
|
||||
(let [shape (nth prepared index)
|
||||
{:keys [thumbnails full font-face-keys pending-font-face-keys]}
|
||||
{:keys [fonts thumbnails full font-face-keys pending-font-face-keys]}
|
||||
(set-object-host-attrs shape true :skip-fills-strokes? true)]
|
||||
(when (some? on-shapes-ready)
|
||||
(hold-text-for-fonts (:id shape) pending-font-face-keys))
|
||||
(recur (inc index)
|
||||
(reduce conj! fonts-acc fonts)
|
||||
(reduce conj! thumbnails-acc thumbnails)
|
||||
(reduce conj! full-acc full)
|
||||
(acc-text-font-state font-state-acc
|
||||
(:id shape)
|
||||
font-face-keys
|
||||
pending-font-face-keys)))
|
||||
{:thumbnails (persistent! thumbnails-acc)
|
||||
{:fonts (persistent! fonts-acc)
|
||||
:thumbnails (persistent! thumbnails-acc)
|
||||
:full (persistent! full-acc)
|
||||
:text-font-state font-state-acc}))]
|
||||
(fetch-fonts fonts)
|
||||
(perf/end-measure "set-objects")
|
||||
(when on-shapes-ready
|
||||
(when-faces-ready shapes text-font-state on-shapes-ready))
|
||||
(when on-shapes-ready (on-shapes-ready))
|
||||
(when (wasm/live?)
|
||||
;; Rebuild the tile index so _render knows which shapes
|
||||
;; map to which tiles after a page switch.
|
||||
@ -2215,10 +2309,12 @@
|
||||
Shapes are processed in tree order (parents before children)
|
||||
to maintain proper shape reference consistency in WASM.
|
||||
|
||||
on-shapes-ready is an optional callback invoked once shapes are loaded
|
||||
into WASM (and tiles rebuilt for async) and the fonts of their texts are
|
||||
stored, or `FONT_REVEAL_TIMEOUT_MS` passed. It does not wait for images,
|
||||
allowing callers to reveal the page content during transitions."
|
||||
on-shapes-ready is an optional callback invoked right after shapes are
|
||||
loaded into WASM (and tiles rebuilt for async). It fires before image
|
||||
loading begins, allowing callers to reveal the page content during
|
||||
transitions. It also marks the load of a page: a text of the page whose
|
||||
font is still loading stays off the canvas until the font arrives, up to
|
||||
`FONT_HOLD_TIMEOUT_MS` (see `hold-text-for-fonts`)."
|
||||
([objects]
|
||||
(set-objects objects nil nil false))
|
||||
([objects render-callback]
|
||||
@ -2226,6 +2322,8 @@
|
||||
([objects render-callback on-shapes-ready force-sync]
|
||||
(when (wasm/live?)
|
||||
(perf/begin-measure "set-objects")
|
||||
(when (some? on-shapes-ready)
|
||||
(clear-font-holds))
|
||||
(let [shapes (shapes-in-tree-order objects)
|
||||
total-shapes (count shapes)]
|
||||
(if (or force-sync (< total-shapes ASYNC_THRESHOLD))
|
||||
|
||||
201
frontend/test/frontend_tests/render_wasm/font_holds_test.cljs
Normal file
201
frontend/test/frontend_tests/render_wasm/font_holds_test.cljs
Normal file
@ -0,0 +1,201 @@
|
||||
;; 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.font-holds-test
|
||||
"A page opens at once while the fonts of its texts load. Only the texts
|
||||
waiting for a font stay off the canvas, each until its own faces are
|
||||
stored or have failed, and the fonts start loading with the shapes that
|
||||
use them."
|
||||
(:require
|
||||
[app.common.render-wasm.serialize-shape :as serialize-shape]
|
||||
[app.common.render-wasm.wasm :as wasm]
|
||||
[app.common.uuid :as uuid]
|
||||
[app.render-wasm.api :as wasm.api]
|
||||
[app.render-wasm.api.fonts :as wasm.fonts]
|
||||
[beicon.v2.core :as rx]
|
||||
[cljs.test :as t :include-macros true]
|
||||
[frontend-tests.helpers.async :as async]
|
||||
[frontend-tests.helpers.mock :as mock]))
|
||||
|
||||
(defn- text-shape
|
||||
[]
|
||||
{:id (uuid/next) :type :text :parent-id uuid/zero :grow-type :fixed})
|
||||
|
||||
(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- quartet->uuid
|
||||
[a b c d]
|
||||
(uuid/from-unsigned-parts a b c d))
|
||||
|
||||
(defn- host-attrs-with
|
||||
"Stands in for the per-shape upload: `faces` maps a shape id to the faces
|
||||
it still waits on and `fonts` to the font fetches it starts."
|
||||
[faces fonts]
|
||||
(fn [shape & _]
|
||||
(let [id (:id shape)]
|
||||
{:fonts (get fonts id [])
|
||||
:thumbnails []
|
||||
:full []
|
||||
:font-face-keys (get faces id #{})
|
||||
:pending-font-face-keys (get faces id #{})})))
|
||||
|
||||
(defn- run-with-wasm
|
||||
"Runs `f` with the WASM boundaries mocked. `calls` collects each
|
||||
`[:awaiting id awaiting?]` and `[:relayout id]` sent to WASM, and
|
||||
`pending` each call to `process-pending`."
|
||||
[{:keys [faces fonts calls pending]} f]
|
||||
(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)
|
||||
"_force_update_shape_text_layout_for"
|
||||
(fn [a b c d]
|
||||
(swap! calls conj [:relayout (quartet->uuid a b c d)])
|
||||
nil)
|
||||
"_set_shape_awaiting_fonts"
|
||||
(fn [a b c d awaiting?]
|
||||
(swap! calls conj [:awaiting (quartet->uuid a b c d) awaiting?])
|
||||
nil)}
|
||||
serialize-shape/serialize-shapes-batch (mock/stub (fn [shapes _] shapes))
|
||||
;; Private: the per-shape upload that reports the faces of a text.
|
||||
#_{:clj-kondo/ignore [:private-call]}
|
||||
wasm.api/set-object-host-attrs (host-attrs-with faces fonts)
|
||||
wasm.api/process-pending (mock/stub (fn [& args] (swap! pending conj args) nil))
|
||||
wasm.api/request-render (mock/stub (constantly nil))
|
||||
wasm.api/get-text-dimensions (mock/stub (constantly nil))}
|
||||
(await (f))))
|
||||
|
||||
(defn- clear-holds
|
||||
[]
|
||||
(@#'wasm.api/clear-font-holds))
|
||||
|
||||
(defn- awaiting-calls
|
||||
[calls]
|
||||
(into [] (filter #(= :awaiting (first %))) @calls))
|
||||
|
||||
(t/deftest ^:async a-page-shows-at-once-while-a-text-waits-for-its-font
|
||||
(let [text (text-shape)
|
||||
calls (atom [])
|
||||
pending (atom [])
|
||||
ready (atom false)]
|
||||
(await (run-with-wasm
|
||||
{:faces {(:id text) #{"face-a"}} :calls calls :pending pending}
|
||||
#(do (wasm.api/set-objects (page-objects text) nil (fn [] (reset! ready true)) true)
|
||||
(t/is (true? @ready) "the page is revealed without waiting for the font")
|
||||
(t/is (= [[:awaiting (:id text) true]] (awaiting-calls calls))
|
||||
"the text stays off the canvas")
|
||||
(js/Promise.resolve nil))))
|
||||
(clear-holds)))
|
||||
|
||||
(t/deftest ^:async a-held-text-shows-once-all-its-faces-arrive
|
||||
(let [text (text-shape)
|
||||
calls (atom [])
|
||||
pending (atom [])]
|
||||
(await (run-with-wasm
|
||||
{:faces {(:id text) #{"face-a" "face-b"}} :calls calls :pending pending}
|
||||
(fn []
|
||||
(wasm.api/set-objects (page-objects text) nil (fn []) true)
|
||||
(rx/push! wasm.fonts/font-stored-stream "face-a")
|
||||
(t/is (= [[:awaiting (:id text) true]] (awaiting-calls calls))
|
||||
"one face is not enough")
|
||||
(rx/push! wasm.fonts/font-storage-failed-stream "face-b")
|
||||
(t/is (= [[:awaiting (:id text) true] [:awaiting (:id text) false]]
|
||||
(awaiting-calls calls))
|
||||
"a stored face and a failed one release it")
|
||||
(t/is (some #{[:relayout (:id text)]} @calls)
|
||||
"it is laid out again with the faces it got")
|
||||
(js/Promise.resolve nil))))
|
||||
(clear-holds)))
|
||||
|
||||
(t/deftest ^:async each-text-shows-when-its-own-faces-arrive
|
||||
(let [text-a (text-shape)
|
||||
text-b (text-shape)
|
||||
calls (atom [])
|
||||
pending (atom [])]
|
||||
(await (run-with-wasm
|
||||
{:faces {(:id text-a) #{"face-a"} (:id text-b) #{"face-b"}}
|
||||
:calls calls
|
||||
:pending pending}
|
||||
(fn []
|
||||
(wasm.api/set-objects (page-objects text-a text-b) nil (fn []) true)
|
||||
(rx/push! wasm.fonts/font-stored-stream "face-a")
|
||||
(t/is (some #{[:awaiting (:id text-a) false]} @calls) "the first text shows")
|
||||
(t/is (not (some #{[:awaiting (:id text-b) false]} @calls))
|
||||
"the second keeps waiting for its face")
|
||||
(js/Promise.resolve nil))))
|
||||
(clear-holds)))
|
||||
|
||||
(t/deftest ^:async texts-whose-fonts-are-loaded-are-not-held
|
||||
(let [text (text-shape)
|
||||
calls (atom [])
|
||||
pending (atom [])]
|
||||
(await (run-with-wasm
|
||||
{:faces {} :calls calls :pending pending}
|
||||
#(do (wasm.api/set-objects (page-objects text) nil (fn []) true)
|
||||
(js/Promise.resolve nil))))
|
||||
(t/is (empty? (awaiting-calls calls)))
|
||||
(clear-holds)))
|
||||
|
||||
(t/deftest ^:async loading-shapes-outside-a-page-load-holds-nothing
|
||||
"Thumbnails load a subtree without `on-shapes-ready`: their texts must
|
||||
not leave the canvas."
|
||||
(let [text (text-shape)
|
||||
calls (atom [])
|
||||
pending (atom [])]
|
||||
(await (run-with-wasm
|
||||
{:faces {(:id text) #{"face-a"}} :calls calls :pending pending}
|
||||
#(do (wasm.api/set-objects (page-objects text) nil nil true)
|
||||
(js/Promise.resolve nil))))
|
||||
(t/is (empty? (awaiting-calls calls)))
|
||||
(clear-holds)))
|
||||
|
||||
(t/deftest ^:async opening-another-page-drops-the-holds-of-the-last
|
||||
(let [text (text-shape)
|
||||
other (text-shape)
|
||||
calls (atom [])
|
||||
pending (atom [])]
|
||||
(await (run-with-wasm
|
||||
{:faces {(:id text) #{"face-a"}} :calls calls :pending pending}
|
||||
(fn []
|
||||
(wasm.api/set-objects (page-objects text) nil (fn []) true)
|
||||
(wasm.api/set-objects (page-objects other) nil (fn []) true)
|
||||
(t/is (= [[:awaiting (:id text) true] [:awaiting (:id text) false]]
|
||||
(awaiting-calls calls))
|
||||
"the text of the last page is drawn again if it comes back")
|
||||
(reset! calls [])
|
||||
(rx/push! wasm.fonts/font-stored-stream "face-a")
|
||||
(t/is (empty? @calls) "its face no longer touches it")
|
||||
(js/Promise.resolve nil))))
|
||||
(clear-holds)))
|
||||
|
||||
(t/deftest ^:async fonts-start-loading-before-images-and-the-first-render
|
||||
(let [text (text-shape)
|
||||
calls (atom [])
|
||||
pending (atom [])
|
||||
fetched (atom 0)
|
||||
fetch {:key "font-url" :callback (fn [] (swap! fetched inc) (rx/empty))}]
|
||||
(await (run-with-wasm
|
||||
{:faces {(:id text) #{"face-a"}}
|
||||
:fonts {(:id text) [fetch]}
|
||||
:calls calls
|
||||
:pending pending}
|
||||
#(do (wasm.api/set-objects (page-objects text) nil (fn []) true)
|
||||
(js/Promise.resolve nil))))
|
||||
(t/is (= 1 @fetched) "the font fetch started during the load")
|
||||
(let [[_shapes thumbnails] (first @pending)]
|
||||
(t/is (empty? thumbnails) "and is not started again with the images"))
|
||||
(await (async/settle))
|
||||
(clear-holds)))
|
||||
@ -129,34 +129,6 @@
|
||||
(rx/push! wasm.fonts/font-stored-stream face)
|
||||
(await (async/wait-for #(deref completed) "storing the face completes the wait"))))
|
||||
|
||||
(t/deftest shapes-without-pending-faces-are-ready-at-once
|
||||
(let [ready (atom false)]
|
||||
(@#'wasm.api/when-faces-ready [] {:pending-faces #{}} #(reset! ready true))
|
||||
(t/is (true? @ready))))
|
||||
|
||||
(t/deftest ^:async shapes-are-ready-once-their-pending-faces-are-stored
|
||||
"A page is revealed once the fonts of its texts are in WASM: revealed
|
||||
before, its texts show missing glyphs."
|
||||
(let [face "reveal-face-key"
|
||||
ready (atom false)]
|
||||
(@#'wasm.api/when-faces-ready [] {:font-index {face #{}} :pending-faces #{face}}
|
||||
#(reset! ready true))
|
||||
(await (async/settle))
|
||||
(t/is (not @ready) "the reveal waits for the pending face")
|
||||
(rx/push! wasm.fonts/font-stored-stream face)
|
||||
(await (async/wait-for #(deref ready) "storing the face reveals the page"))))
|
||||
|
||||
(t/deftest ^:async a-page-transition-drops-the-reveal-of-the-previous-page
|
||||
(let [face "dropped-face-key"
|
||||
ready (atom false)]
|
||||
(@#'wasm.api/when-faces-ready [] {:font-index {face #{}} :pending-faces #{face}}
|
||||
#(reset! ready true))
|
||||
(wasm.api/begin-page-transition!)
|
||||
(reset! wasm.api/page-transition? false)
|
||||
(rx/push! wasm.fonts/font-stored-stream face)
|
||||
(await (async/settle))
|
||||
(t/is (not @ready) "the faces of a page left behind reveal nothing")))
|
||||
|
||||
(t/deftest empty-grid-tracks-do-not-allocate-zero-bytes
|
||||
(let [calls (atom [])
|
||||
;; `h/call` is a macro that resolves the wasm function off the module
|
||||
|
||||
@ -122,6 +122,7 @@
|
||||
[frontend-tests.plugins.wait-for-save-test]
|
||||
[frontend-tests.render-dimensions-test]
|
||||
[frontend-tests.render-wasm.call-test]
|
||||
[frontend-tests.render-wasm.font-holds-test]
|
||||
[frontend-tests.render-wasm.fonts-test]
|
||||
[frontend-tests.render-wasm.process-objects-test]
|
||||
[frontend-tests.render-wasm.serialization-test]
|
||||
@ -352,6 +353,7 @@
|
||||
'frontend-tests.plugins.value-objects-test
|
||||
'frontend-tests.plugins.wait-for-save-test
|
||||
'frontend-tests.render-wasm.call-test
|
||||
'frontend-tests.render-wasm.font-holds-test
|
||||
'frontend-tests.render-wasm.fonts-test
|
||||
'frontend-tests.render-wasm.process-objects-test
|
||||
'frontend-tests.render-wasm.serialization-test
|
||||
|
||||
@ -1345,6 +1345,12 @@ impl RenderState {
|
||||
.any(|child_id| self.shape_visible_in_include_filter(child_id, tree))
|
||||
}
|
||||
|
||||
/// A hidden shape paints nothing, and neither does a text still waiting
|
||||
/// for its font, except in an export.
|
||||
fn skips_shape(&self, shape: &Shape) -> bool {
|
||||
shape.hidden || (shape.awaiting_fonts() && self.export_context.is_none())
|
||||
}
|
||||
|
||||
/// When an include whitelist is active, only those ids are painted.
|
||||
fn shape_should_paint_for_viewer_layer(&self, shape_id: &Uuid) -> bool {
|
||||
match &self.include_filter {
|
||||
@ -3296,7 +3302,7 @@ impl RenderState {
|
||||
let Some(shadow_shape) = tree.get(shadow_shape_id) else {
|
||||
continue;
|
||||
};
|
||||
if shadow_shape.hidden {
|
||||
if self.skips_shape(shadow_shape) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -3436,7 +3442,7 @@ impl RenderState {
|
||||
let Some(content) = pass.tree.get(&content_id) else {
|
||||
continue;
|
||||
};
|
||||
if content.hidden {
|
||||
if self.skips_shape(content) {
|
||||
continue;
|
||||
}
|
||||
self.render_black_silhouette_subtree(content, children_clip_bounds.clone(), pass)?;
|
||||
@ -3504,7 +3510,7 @@ impl RenderState {
|
||||
let Some(child) = pass.tree.get(&child_id) else {
|
||||
continue;
|
||||
};
|
||||
if child.hidden {
|
||||
if self.skips_shape(child) {
|
||||
continue;
|
||||
}
|
||||
self.render_black_silhouette_subtree(child, children_clip_bounds.clone(), pass)?;
|
||||
@ -3984,7 +3990,7 @@ impl RenderState {
|
||||
let transformed_element: Cow<Shape> = Cow::Borrowed(element);
|
||||
|
||||
// Aggressive early exit: check hidden first (fastest check)
|
||||
if transformed_element.hidden {
|
||||
if self.skips_shape(&transformed_element) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@ -217,6 +217,10 @@ pub struct Shape {
|
||||
pub svg_transform: Option<Matrix>,
|
||||
pub ignore_constraints: bool,
|
||||
deleted: bool,
|
||||
/// A text of a page being opened whose font is still loading. It is laid
|
||||
/// out but not drawn on the canvas until the frontend clears this, so it
|
||||
/// never shows in a fallback face first. Exports still draw it.
|
||||
awaiting_fonts: bool,
|
||||
/// Fills from a cold-load batch, held until text content is uploaded and laid out.
|
||||
deferred_batch_fills: Option<Vec<Fill>>,
|
||||
/// Strokes from a cold-load batch, applied together with deferred fills.
|
||||
@ -324,6 +328,7 @@ impl Shape {
|
||||
svg_transform: None,
|
||||
ignore_constraints: false,
|
||||
deleted: false,
|
||||
awaiting_fonts: false,
|
||||
deferred_batch_fills: None,
|
||||
deferred_batch_strokes: None,
|
||||
}
|
||||
@ -485,6 +490,14 @@ impl Shape {
|
||||
self.hidden = value;
|
||||
}
|
||||
|
||||
pub fn set_awaiting_fonts(&mut self, value: bool) {
|
||||
self.awaiting_fonts = value;
|
||||
}
|
||||
|
||||
pub fn awaiting_fonts(&self) -> bool {
|
||||
self.awaiting_fonts
|
||||
}
|
||||
|
||||
pub fn set_hide_fill_on_export(&mut self, value: bool) {
|
||||
self.hide_fill_on_export = value;
|
||||
}
|
||||
@ -2385,6 +2398,20 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_text_keeps_waiting_for_its_font_when_its_content_is_set_again() {
|
||||
let mut shape = any_shape();
|
||||
shape.set_shape_type(Type::Text(TextContent::default()));
|
||||
assert!(!shape.awaiting_fonts());
|
||||
|
||||
shape.set_awaiting_fonts(true);
|
||||
shape.clear_text();
|
||||
assert!(shape.awaiting_fonts());
|
||||
|
||||
shape.set_awaiting_fonts(false);
|
||||
assert!(!shape.awaiting_fonts());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_masked() {
|
||||
let mut shape = any_shape();
|
||||
|
||||
@ -487,6 +487,19 @@ pub extern "C" fn force_update_shape_text_layout_for(a: u32, b: u32, c: u32, d:
|
||||
});
|
||||
}
|
||||
|
||||
/// Keeps the text `a b c d` off the canvas while `awaiting` is true: its font
|
||||
/// is still loading. See `Shape::awaiting_fonts`.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn set_shape_awaiting_fonts(a: u32, b: u32, c: u32, d: u32, awaiting: bool) {
|
||||
with_state!(state, {
|
||||
let shape_id = uuid_from_u32_quartet(a, b, c, d);
|
||||
if let Some(shape) = state.shapes.get_mut(&shape_id) {
|
||||
shape.set_awaiting_fonts(awaiting);
|
||||
}
|
||||
state.touch_shape(shape_id);
|
||||
});
|
||||
}
|
||||
|
||||
const RAW_POSITION_DATA_SIZE: usize = size_of::<shapes::PositionData>();
|
||||
|
||||
impl From<[u8; RAW_POSITION_DATA_SIZE]> for shapes::PositionData {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user