From 1b7b980f1f49c251a4f1e82ad8589421f4b7a85e Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 24 Nov 2026 17:50:05 +0100 Subject: [PATCH] :bug: Outline a text only once its font is loaded Converting a text to a path right after changing its font traced the fallback face: the outline came from WASM while the new font was still being fetched. Plugin `flatten` and the Flatten menu share this path, so both were affected, with any font and any writing mode. With the WASM renderer, the conversion now waits until the faces of the texts it converts are stored or have failed, lays those texts out again and then outlines them, reading the page as it is then. The wait gives up after 10 seconds. It is tracked as `:font` reflow work, so `waitForLayoutUpdate` and the MCP tools wait for the conversion. Without a pending font the conversion still runs at once. AI-assisted-by: claude-opus-5-5 --- .../data/workspace/path/shapes_to_path.cljs | 92 ++++++++++------ frontend/src/app/plugins/api.cljs | 4 +- frontend/src/app/render_wasm/api.cljs | 23 ++++ .../data/workspace_shapes_to_path_test.cljs | 104 ++++++++++++++++++ frontend/test/frontend_tests/runner.cljs | 2 + plugins/CHANGELOG.md | 1 + plugins/libs/plugin-types/index.d.ts | 3 + 7 files changed, 194 insertions(+), 35 deletions(-) create mode 100644 frontend/test/frontend_tests/data/workspace_shapes_to_path_test.cljs diff --git a/frontend/src/app/main/data/workspace/path/shapes_to_path.cljs b/frontend/src/app/main/data/workspace/path/shapes_to_path.cljs index fb522f6839..a59de61ae5 100644 --- a/frontend/src/app/main/data/workspace/path/shapes_to_path.cljs +++ b/frontend/src/app/main/data/workspace/path/shapes_to_path.cljs @@ -18,6 +18,7 @@ [app.common.uuid :as uuid] [app.main.data.changes :as dch] [app.main.data.helpers :as dsh] + [app.main.data.workspace.reflow :as wrf] [app.main.data.workspace.selection :as dws] [app.main.features :as features] [app.render-wasm.api :as wasm.api] @@ -41,7 +42,54 @@ (dissoc :selrect :points) (path/update-geometry))) +(defn- convert-to-path-in-wasm + "Converts the shapes `ids` to paths with the outlines WASM draws, reading + the page as it is when the event runs." + [ids] + (ptk/reify ::convert-to-path-in-wasm + ptk/WatchEvent + (watch [it state _] + (let [page-id (:current-page-id state) + objects (dsh/lookup-page-objects state) + selected + (->> ids + (filter #(contains? objects %)) + (remove #(ctn/has-any-copy-parent? objects (get objects %)))) + + children-ids + (into #{} + (mapcat #(cph/get-children-ids objects %)) + selected) + + changes + (-> (pcb/empty-changes it page-id) + (pcb/with-objects objects) + (pcb/update-shapes + selected + (fn [shape] + ;; Keep path content in world coordinates. + (if (cph/path-shape? shape) + (flatten-path shape) + (let [content (wasm.api/shape-to-path (:id shape))] + (-> shape + (assoc :type :path) + (cond-> (cph/text-shape? shape) + (assoc :fills + (->> (txt/node-seq txt/is-text-node? (:content shape)) + (map :fills) + (first)))) + (cond-> (cph/image-shape? shape) + (assoc :fill-image (get shape :metadata))) + (d/without-keys dissoc-attrs) + (path/update-geometry content)))))) + (pcb/remove-objects children-ids))] + (rx/of (dch/commit-changes changes)))))) + (defn convert-selected-to-path + "Converts the shapes `ids`, or the selection, to paths. With WASM, the + texts among them and their descendants are outlined once their fonts are + loaded: a font just chosen may still be on its way. The wait is tracked + as `:font` reflow work, so layout waits cover the conversion." ([] (convert-selected-to-path nil)) ([ids] @@ -49,40 +97,16 @@ ptk/WatchEvent (watch [it state _] (if (features/active-feature? state "render-wasm/v1") - (let [page-id (:current-page-id state) - objects (dsh/lookup-page-objects state) - selected - (->> (or ids (dsh/lookup-selected state)) - (remove #(ctn/has-any-copy-parent? objects (get objects %)))) - - children-ids - (into #{} - (mapcat #(cph/get-children-ids objects %)) - selected) - - changes - (-> (pcb/empty-changes it page-id) - (pcb/with-objects objects) - (pcb/update-shapes - selected - (fn [shape] - ;; Keep path content in world coordinates. - (if (cph/path-shape? shape) - (flatten-path shape) - (let [content (wasm.api/shape-to-path (:id shape))] - (-> shape - (assoc :type :path) - (cond-> (cph/text-shape? shape) - (assoc :fills - (->> (txt/node-seq txt/is-text-node? (:content shape)) - (map :fills) - (first)))) - (cond-> (cph/image-shape? shape) - (assoc :fill-image (get shape :metadata))) - (d/without-keys dissoc-attrs) - (path/update-geometry content)))))) - (pcb/remove-objects children-ids))] - (rx/of (dch/commit-changes changes))) + (let [objects (dsh/lookup-page-objects state) + ids (into #{} (or ids (dsh/lookup-selected state))) + shapes (into [] + (comp (mapcat #(cons % (cph/get-children-ids objects %))) + (keep objects)) + ids)] + (->> (rx/concat + (wasm.api/await-text-fonts shapes) + (rx/of (convert-to-path-in-wasm ids))) + (wrf/with-pending :font ids))) (let [page-id (:current-page-id state) objects (dsh/lookup-page-objects state) diff --git a/frontend/src/app/plugins/api.cljs b/frontend/src/app/plugins/api.cljs index 4220978faa..bce68ca68c 100644 --- a/frontend/src/app/plugins/api.cljs +++ b/frontend/src/app/plugins/api.cljs @@ -726,7 +726,9 @@ :else ;; convert-selected-to-path converts the shapes in place (keeping their - ;; ids), so return proxies for the same ids, now resolving as paths. + ;; ids), so return proxies for the same ids, resolving as paths. A + ;; text whose font still loads becomes a path once the font is in: + ;; `waitForLayoutUpdate` waits for it. (let [file-id (:current-file-id @st/state) page-id (:current-page-id @st/state) ids (mapv #(obj/get % "$id") shapes)] diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 1673bd7ddc..14ecc2a1d9 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -1859,6 +1859,29 @@ (rx/take 1) (rx/ignore)))) +;; Longest wait for the fonts of texts about to become paths. Past it, they +;; are outlined with the faces WASM holds. +(def ^:private ^:const TEXT_OUTLINE_FONT_TIMEOUT_MS 10000) + +(defn await-text-fonts + "Completes once the faces of the text `shapes` are stored in WASM or have + failed, after laying those texts out again: outlined while a face still + loads, a text would trace its fallback font. Completes at once when no + face is pending, and after `TEXT_OUTLINE_FONT_TIMEOUT_MS` at the latest." + [shapes] + (let [text-ids (into [] (comp (filter cfh/text-shape?) (map :id)) shapes) + pending (into #{} + (comp (filter cfh/text-shape?) + (mapcat (comp :pending-faces text-font-state-for-shape))) + shapes)] + (if (empty? pending) + (rx/empty) + (->> (rx/race (rx/concat (await-pending-faces pending) (rx/of :faces)) + (rx/map (constantly :timeout) (rx/timer TEXT_OUTLINE_FONT_TIMEOUT_MS))) + (rx/take 1) + (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. diff --git a/frontend/test/frontend_tests/data/workspace_shapes_to_path_test.cljs b/frontend/test/frontend_tests/data/workspace_shapes_to_path_test.cljs new file mode 100644 index 0000000000..eb4bf728a8 --- /dev/null +++ b/frontend/test/frontend_tests/data/workspace_shapes_to_path_test.cljs @@ -0,0 +1,104 @@ +;; 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.data.workspace-shapes-to-path-test + (:require + [app.common.test-helpers.compositions :as ctho] + [app.common.test-helpers.files :as cthf] + [app.common.test-helpers.ids-map :as cthi] + [app.common.types.path :as path] + [app.main.data.helpers :as dsh] + [app.main.data.workspace.path.shapes-to-path :as dwps] + [app.main.data.workspace.reflow :as wrf] + [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] + [frontend-tests.helpers.state :as ths] + [frontend-tests.helpers.wasm :as thw] + [potok.v2.core :as ptk])) + +(t/use-fixtures :each + {:before (fn [] + (cthi/reset-idmap!) + (thw/setup-wasm-mocks!)) + :after (fn [] + (thw/teardown-wasm-mocks!))}) + +(def ^:private text-font + {:font-id "gfont-zen-antique" + :font-variant-id "regular" + :font-weight "400" + :font-style "normal"}) + +(def ^:private outline + (path/from-plain [{:command :move-to :params {:x 0 :y 0}} + {:command :line-to :params {:x 10 :y 0}} + {:command :line-to :params {:x 10 :y 10}} + {:command :close-path :params {}}])) + +(defn- text-store + [] + (ths/setup-store (-> (cthf/sample-file :file1 :page-label :page1) + (ctho/add-text :text1 "漱石")))) + +(defn- shape-type + [store] + (:type (get (dsh/lookup-page-objects @store) (cthi/id :text1)))) + +(t/deftest ^:async text-whose-font-still-loads-becomes-a-path-once-the-font-is-stored + ;; Outlined while its font loads, a text traces the fallback face: the + ;; conversion waits for the face, then outlines the text. + (let [stored? (atom false)] + (await + (mock/with-mocks* + {wasm.fonts/get-content-fonts (mock/stub (constantly #{text-font})) + wasm.fonts/font-stored? (mock/stub (fn [_ _] @stored?)) + wasm.api/shape-to-path (mock/stub (constantly outline))} + (let [store (text-store) + face (wasm.fonts/font-data-key (wasm.fonts/make-font-data text-font))] + (ptk/emit! store (dwps/convert-selected-to-path #{(cthi/id :text1)})) + (await (async/settle)) + (t/is (= :text (shape-type store)) "the text waits for its font") + + (reset! stored? true) + (rx/push! wasm.fonts/font-stored-stream face) + (await (async/wait-for #(= :path (shape-type store)) + "storing the font converts the text"))))))) + +(t/deftest ^:async layout-waits-cover-a-conversion-waiting-for-its-font + ;; `waitForLayoutUpdate` (and the MCP tools after each call) wait on the + ;; reflow pending map: it must hold the text until it is a path. + (let [settled? (atom false)] + (await + (mock/with-mocks* + {wasm.fonts/get-content-fonts (mock/stub (constantly #{text-font})) + wasm.fonts/font-stored? (mock/stub (constantly false)) + wasm.api/shape-to-path (mock/stub (constantly outline))} + (let [store (text-store) + id (cthi/id :text1) + face (wasm.fonts/font-data-key (wasm.fonts/make-font-data text-font))] + (ptk/emit! store (dwps/convert-selected-to-path #{id})) + (rx/subs! (fn [_] (reset! settled? (shape-type store))) + (wrf/settled #{id})) + (await (async/settle)) + (t/is (false? @settled?) "the layout wait is still pending") + + (rx/push! wasm.fonts/font-stored-stream face) + (await (async/wait-for #(some? @settled?) "the layout wait settles")) + (t/is (= :path @settled?) "it settles once the text is a path")))))) + +(t/deftest ^:async text-with-its-fonts-loaded-becomes-a-path-at-once + (await + (mock/with-mocks* + {wasm.fonts/get-content-fonts (mock/stub (constantly #{text-font})) + wasm.api/shape-to-path (mock/stub (constantly outline))} + (let [store (text-store)] + (ptk/emit! store (dwps/convert-selected-to-path #{(cthi/id :text1)})) + (t/is (= :path (shape-type store))) + (await (async/settle)))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index a876f178eb..e1a1ac0b47 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -54,6 +54,7 @@ [frontend-tests.data.workspace-path-edition-test] [frontend-tests.data.workspace-reflow-test] [frontend-tests.data.workspace-shape-layout-test] + [frontend-tests.data.workspace-shapes-to-path-test] [frontend-tests.data.workspace-shortcuts-test] [frontend-tests.data.workspace-sync-text-wasm-test] [frontend-tests.data.workspace-texts-test] @@ -284,6 +285,7 @@ 'frontend-tests.data.workspace-path-edition-test 'frontend-tests.data.workspace-reflow-test 'frontend-tests.data.workspace-shape-layout-test + 'frontend-tests.data.workspace-shapes-to-path-test 'frontend-tests.data.workspace-shortcuts-test 'frontend-tests.data.workspace-sync-text-wasm-test 'frontend-tests.data.workspace-texts-test diff --git a/plugins/CHANGELOG.md b/plugins/CHANGELOG.md index 6c7c7b3b51..c79129df52 100644 --- a/plugins/CHANGELOG.md +++ b/plugins/CHANGELOG.md @@ -28,6 +28,7 @@ ### 🩹 Fixes +- **plugins-runtime:** `flatten()` outlines a text whose font is still loading once the font is in, instead of tracing a fallback font. Until then the returned shape is still a text; `penpot.waitForLayoutUpdate()` waits for the conversion. - **plugins-runtime:** Methods that rejected an invalid value with a plain string (`connectLibrary`, file `restore`, `remove`, `pin`, `export`, `findVersions`, `saveVersion`) now reject with an `Error` carrying the same message. - **plugin-runtime:** `LibraryComponent.instance()`, `createShapeFromSvg()` and `createShapeFromSvgWithImages()` now create shapes at the page root instead of inside a board at the page origin. Closes #12086. - **plugin-types, plugin-runtime:** Dashboard property setters now update values after backend success and log failures without interrupting the workspace. File moves reject projects in other teams, backend rejections include their type and code, and `project.fileCount` stays at its listing snapshot. Duplicated projects include their file count and pinned state. diff --git a/plugins/libs/plugin-types/index.d.ts b/plugins/libs/plugin-types/index.d.ts index aa12dbc5ff..ec65222781 100644 --- a/plugins/libs/plugin-types/index.d.ts +++ b/plugins/libs/plugin-types/index.d.ts @@ -1619,6 +1619,9 @@ export interface Context { /** * Converts the shapes into Paths. If the shapes are complex will put together * all its paths into one. + * A text whose font is still loading is converted once the font is loaded, + * so its returned shape stays a text until then; await + * `penpot.waitForLayoutUpdate()` before reading it as a path. * @param shapes to flatten */ flatten(shapes: Shape[]): Path[];