From 590eec373dd3f23e374f0e388787abd959568aac Mon Sep 17 00:00:00 2001 From: yongjin Date: Tue, 2 Jun 2026 15:19:23 +0900 Subject: [PATCH] :bug: Preload fill images for bitmap exports --- exporter/src/app/browser.cljs | 29 ++++++++++++++++++ exporter/src/app/renderer/bitmap.cljs | 2 ++ frontend/src/app/main/render.cljs | 20 ++++++++----- frontend/src/app/main/ui/shapes/fills.cljs | 16 ++++++---- frontend/src/app/render.cljs | 14 ++++++--- .../render_export_images_test.cljs | 30 +++++++++++++++++++ frontend/test/frontend_tests/runner.cljs | 2 ++ 7 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 frontend/test/frontend_tests/render_export_images_test.cljs diff --git a/exporter/src/app/browser.cljs b/exporter/src/app/browser.cljs index 9a02e0a5ae..ab5c5bec6f 100644 --- a/exporter/src/app/browser.cljs +++ b/exporter/src/app/browser.cljs @@ -79,6 +79,35 @@ :cause (ex-message cause)) (p/resolved nil)))))) +(defn wait-for-images + "Wait until HTML and SVG images currently present in the page are loadable." + ([page] (wait-for-images page nil)) + ([page {:keys [timeout] :or {timeout 15000}}] + (-> (.evaluate ^js page + (js* "() => { + const hrefOf = (node) => { + if (node.currentSrc) return node.currentSrc; + if (node.src) return node.src; + const href = node.getAttribute && (node.getAttribute('href') || node.getAttribute('xlink:href')); + return href || ''; + }; + const urls = Array.from(document.querySelectorAll('img, image')) + .map(hrefOf) + .filter((href) => href && !href.startsWith('data:')); + return Promise.all(urls.map((url) => new Promise((resolve) => { + const img = new Image(); + img.crossOrigin = 'anonymous'; + img.onload = () => resolve(true); + img.onerror = () => resolve(false); + img.src = url; + }))).then(() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)))); +}")) + (p/timeout timeout) + (p/catch (fn [cause] + (l/warn :hint "wait-for-images timed out; continuing anyway" + :cause (ex-message cause)) + (p/resolved nil)))))) + (defn wait-for ([locator] (wait-for locator nil)) ([locator {:keys [state timeout] :or {state "visible" timeout 10000}}] diff --git a/exporter/src/app/renderer/bitmap.cljs b/exporter/src/app/renderer/bitmap.cljs index ceefc75df5..52edad7d07 100644 --- a/exporter/src/app/renderer/bitmap.cljs +++ b/exporter/src/app/renderer/bitmap.cljs @@ -48,6 +48,7 @@ (bw/nav! page (str uri)) (bw/sleep page 1000) ; the good old fix with sleep (bw/wait-for-fonts page) + (bw/wait-for-images page) (bw/eval! page (js* "() => document.body.style.background = 'transparent'")) ;; take the screnshot of requested objects, one by one @@ -59,6 +60,7 @@ :share-id share-id :object-id (mapv :id objects) :route "objects" + :render-embed true :skip-children skip-children} uri (-> (cf/get-internal-uri) (u/ensure-path-slash) diff --git a/frontend/src/app/main/render.cljs b/frontend/src/app/main/render.cljs index 4a45630454..e6bf46313f 100644 --- a/frontend/src/app/main/render.cljs +++ b/frontend/src/app/main/render.cljs @@ -647,17 +647,21 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defn- get-image-data [shape] - (cond - (= :image (:type shape)) - [(:metadata shape)] + (let [fill-images (->> (:fills shape) + (keep :fill-image))] + (cond + (= :image (:type shape)) + (cond-> [(:metadata shape)] + (seq fill-images) (into fill-images)) - (some? (:fill-image shape)) - [(:fill-image shape)] + (some? (:fill-image shape)) + (cond-> [(:fill-image shape)] + (seq fill-images) (into fill-images)) - :else - [])) + :else + (vec fill-images)))) -(defn- populate-images-cache +(defn populate-images-cache [objects] (let [images (->> objects (vals) diff --git a/frontend/src/app/main/ui/shapes/fills.cljs b/frontend/src/app/main/ui/shapes/fills.cljs index 41238e7608..06bcc777de 100644 --- a/frontend/src/app/main/ui/shapes/fills.cljs +++ b/frontend/src/app/main/ui/shapes/fills.cljs @@ -44,7 +44,8 @@ height (dm/get-prop selrect :height) has-image? (or (some? metadata) - (some? image)) + (some? image) + (some :fill-image fills)) uri (cond (some? metadata) @@ -53,7 +54,7 @@ (some? image) (cf/resolve-file-media image)) - uris (into [uri] + uris (into (cond-> [] (some? uri) (conj uri)) (comp (keep :fill-image) (map cf/resolve-file-media)) @@ -136,21 +137,26 @@ [:> :image image-props]) [:> :rect props]))) - (when ^boolean has-image? + (when (some? uri) (let [tf (-> image :transform) img-x (if (some? tf) (* (get tf :x 0) width) 0) img-y (if (some? tf) (* (get tf :y 0) height) 0) img-w (if (some? tf) (* (get tf :width 1) width) width) img-h (if (some? tf) (* (get tf :height 1) height) height)] [:g - ;; We add this shape to add a padding so the patter won't repeat + ;; We add this shape to add a padding so the pattern won't repeat ;; Issue: https://tree.taiga.io/project/penpot/issue/5583 + ;; + ;; This padding image only applies to legacy/top-level shape images. + ;; Nested fill images already render inside the fill loop above; emitting + ;; an extra without a href can break SVG pattern rasterization + ;; during bitmap export. [:rect {:x 0 :y 0 :width (* width no-repeat-padding) :height (* height no-repeat-padding) :fill "none"}] - [:image {:href uri + [:image {:href (get embed uri uri) :preserveAspectRatio "none" :x img-x :y img-y diff --git a/frontend/src/app/render.cljs b/frontend/src/app/render.cljs index f758822d2b..33fb4cb537 100644 --- a/frontend/src/app/render.cljs +++ b/frontend/src/app/render.cljs @@ -146,8 +146,12 @@ (rx/observe-on :async) (rx/map (comp :objects second)) (rx/map (fn [objects] - (let [objects (render/adapt-objects-for-shape objects object-id)] - #(assoc % :objects objects))))))))) + (render/adapt-objects-for-shape objects object-id))) + (rx/merge-map (fn [objects] + (rx/concat + (->> (render/populate-images-cache objects) + (rx/ignore)) + (rx/of #(assoc % :objects objects)))))))))) (def ^:private schema:render-objects [:map {:title "render-objets"} @@ -155,6 +159,7 @@ [:file-id ::sm/uuid] [:share-id {:optional true} ::sm/uuid] [:embed {:optional true} :boolean] + [:render-embed {:optional true} :boolean] [:skip-children {:optional true} :boolean] [:object-id [:or [:vector ::sm/uuid] ::sm/uuid]]]) @@ -170,8 +175,9 @@ (defn- render-objects [params] (try - (let [{:keys [file-id page-id embed share-id object-id skip-children wasm scale] :as params} - (coerce-render-objects-params params)] + (let [{:keys [file-id page-id embed render-embed share-id object-id skip-children wasm scale] :as params} + (coerce-render-objects-params params) + embed (if (some? render-embed) render-embed embed)] (st/emit! (fetch-objects-bundle :file-id file-id :page-id page-id :share-id share-id :object-id object-id)) (if (uuid? object-id) (mf/html diff --git a/frontend/test/frontend_tests/render_export_images_test.cljs b/frontend/test/frontend_tests/render_export_images_test.cljs new file mode 100644 index 0000000000..505753c14d --- /dev/null +++ b/frontend/test/frontend_tests/render_export_images_test.cljs @@ -0,0 +1,30 @@ +;; 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 INC Sucursal en EspaƱa SL + +(ns frontend-tests.render-export-images-test + (:require + [app.main.render :as render] + [cljs.test :refer [deftest is testing]])) + +(def image-a + {:id #uuid "00000000-0000-0000-0000-000000000001" + :name "image-a"}) + +(def image-b + {:id #uuid "00000000-0000-0000-0000-000000000002" + :name "image-b"}) + +(deftest export-image-cache-includes-images-from-fills-vector + (let [shape {:type :rect + :fills [{:fill-color "#ffffff" + :fill-opacity 1} + {:fill-opacity 1 + :fill-image image-a} + {:fill-opacity 0.5 + :fill-image image-b}]} + images (#'render/get-image-data shape)] + (testing "all image fills are preloaded before static export markup is rendered" + (is (= [image-a image-b] images))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 8443e2c218..e2b1dd0137 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -108,6 +108,7 @@ [frontend-tests.plugins.utils-test] [frontend-tests.plugins.value-objects-test] [frontend-tests.render-dimensions-test] + [frontend-tests.render-export-images-test] [frontend-tests.render-wasm.process-objects-test] [frontend-tests.render-wasm.serialization-test] [frontend-tests.render-wasm.shape-proxy-test] @@ -365,6 +366,7 @@ 'frontend-tests.ui.mcp-authorize-test 'frontend-tests.ui.routes-test 'frontend-tests.render-dimensions-test + 'frontend-tests.render-export-images-test 'frontend-tests.text-editor-paste-guard-test 'frontend-tests.ui.settings-password-schema-test 'frontend-tests.ui.settings-shortcuts-test