penpot/frontend/src/app/worker/thumbnails.cljs
Belén Albeza b0dbbe8285
🐛 Fix blurry file thumbnails rendered with render-wasm (#13026)
The wasm path drew file thumbnails on a 252px wide canvas, wrapped the
PNG in an SVG and sent it to the rasterizer, which scaled it up to its
default 300px width. The result was blurry on the dashboard on high-DPI
screens and in the link preview card.

Render the wasm thumbnail at 600px wide, twice the old size, and upload
the canvas PNG as is, without the rasterizer round trip. The SVG render
path keeps its current size.

Closes #13025

AI-assisted-by: claude-opus-5-5
2026-11-16 13:32:38 +01:00

157 lines
6.1 KiB
Clojure

;; 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 app.worker.thumbnails
(:require
["react-dom/server" :as rds]
[app.common.geom.rect :as grc]
[app.common.geom.shapes.bounds :as gsb]
[app.common.logging :as log]
[app.common.render-wasm.wasm :as wasm]
[app.common.types.color :as cc]
[app.common.uri :as u]
[app.config :as cf]
[app.main.fonts :as fonts]
[app.main.render :as render]
[app.render-wasm.api :as wasm.api]
[app.util.http :as http]
[app.worker.impl :as impl]
[app.worker.wasm :as wwasm]
[beicon.v2.core :as rx]
[okulary.core :as l]
[promesa.core :as p]
[rumext.v2 :as mf]))
(log/set-level! :trace)
(def ^:private ^:const thumbnail-aspect-ratio (/ 2 3))
(defn- handle-response
[{:keys [body status] :as response}]
(cond
(http/success? response)
(rx/of (:body response))
(= status 413)
(rx/throw {:type :validation
:code :request-body-too-large
:hint "request body too large"})
(and (http/client-error? response)
(map? body))
(rx/throw body)
:else
(rx/throw {:type :unexpected-error
:code :unhandled-http-response
:http-status status
:http-body body})))
(defn- request-data-for-thumbnail
[file-id revn strip-frames-with-thumbnails]
(let [path "api/main/methods/get-file-data-for-thumbnail"
params {:file-id file-id
:revn revn
:strip-frames-with-thumbnails strip-frames-with-thumbnails}
request {:method :get
:uri (u/join cf/public-uri path)
:credentials "include"
:query params}]
(->> (http/send! request)
(rx/map http/conditional-decode-transit)
(rx/mapcat handle-response))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SVG RENDERING (LEGACY RENDER)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- render-thumbnail
[{:keys [page file-id revn] :as params}]
(try
(binding [fonts/loaded-hints (l/atom #{})]
(let [objects (:objects page)
frame (some->> page :thumbnail-frame-id (get objects))
background-color (:background page)
element (if frame
(mf/element render/frame-svg #js
{:objects objects
:frame frame
:use-thumbnails true
:background-color background-color
:aspect-ratio (/ 2 3)})
(mf/element render/page-svg #js
{:data page
:use-thumbnails true
:embed true
:aspect-ratio (/ 2 3)}))
data (rds/renderToStaticMarkup element)]
{:data data
:fonts @fonts/loaded-hints
:file-id file-id
:revn revn}))
(catch :default cause
(js/console.error "unexpected error on rendering thumbnail" cause)
nil)))
(defmethod impl/handler :thumbnails/generate-for-file
[{:keys [file-id revn] :as message} _]
(->> (request-data-for-thumbnail file-id revn true)
(rx/map render-thumbnail)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; WASM RENDERING
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- render-thumbnail-with-wasm
[{:keys [file-id revn width] :as _message}]
(->> (rx/from @wwasm/module)
(rx/mapcat #(request-data-for-thumbnail file-id revn false))
(rx/mapcat
(fn [{:keys [page] :as _file}]
(rx/create
(fn [subs]
(let [bgcolor (or (:background page) cc/canvas)
height (* width thumbnail-aspect-ratio)
canvas (js/OffscreenCanvas. width height)
init? (wasm.api/init-canvas-context canvas)]
(if init?
(let [objects (:objects page)
frame (some->> page :thumbnail-frame-id (get objects))
vbox (if frame
(-> (gsb/get-object-bounds objects frame)
(grc/fix-aspect-ratio thumbnail-aspect-ratio))
(render/calculate-dimensions objects thumbnail-aspect-ratio))
zoom (/ width (:width vbox))]
(reset! wasm/disable-request-render? true)
(wasm.api/initialize-viewport
objects zoom vbox
:background bgcolor
:force-sync true
:on-render
(fn []
(if frame
(wasm.api/render-sync-shape (:id frame))
(wasm.api/render-sync))
(->> (.convertToBlob ^js canvas #js {:type "image/png"})
(p/fnly (fn [blob cause]
(wasm.api/clear-canvas)
(if cause
(rx/error! subs cause)
(rx/push! subs
{:blob blob
:file-id file-id
:revn revn}))
(rx/end! subs)))))))
(rx/error! subs (ex-info "unable to init wasm canvas" {:file-id file-id})))
nil)))))))
(defmethod impl/handler :thumbnails/generate-for-file-wasm
[message _]
(wwasm/submit #(render-thumbnail-with-wasm message)))