0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-18 10:41:29 -05:00

♻️ Refactor how fonts are passed to raster

This commit is contained in:
Aitor 2023-11-23 12:42:35 +01:00 committed by Andrey Antukh
parent 8f356b679d
commit 39635cf5df
3 changed files with 35 additions and 6 deletions

View file

@ -248,6 +248,28 @@
(get-default-variant font)))
;; Font embedding functions
(defn get-node-fonts
"Extracts the fonts used by some node"
[node]
(let [nodes (.from js/Array (dom/query-all node "[style*=font]"))
result (.reduce nodes (fn [obj node]
(let [style (.-style node)
font-family (.-fontFamily style)
[_ font] (first
(filter (fn [[_ {:keys [id family]}]]
(or (= family font-family)
(= id font-family)))
@fontsdb))
font-id (:id font)
font-variant (get-variant font (.-fontVariant style))
font-variant-id (:id font-variant)]
(obj/set!
obj
(dm/str font-id ":" font-variant-id)
{:font-id font-id
:font-variant-id font-variant-id})))
#js {})]
(.values js/Object result)))
(defn get-content-fonts
"Extracts the fonts used by the content of a text shape"

View file

@ -10,6 +10,7 @@
[app.main.data.modal :as modal]
[app.main.data.workspace.colors :as dwc]
[app.main.data.workspace.undo :as dwu]
[app.main.fonts :as fonts]
[app.main.rasterizer :as thr]
[app.main.store :as st]
[app.main.ui.context :as ctx]
@ -133,11 +134,15 @@
handle-draw-picker-canvas
(mf/use-callback
(fn []
(let [svg-node (dom/get-element "render")]
(->> (rx/of {:node svg-node
:width (:width vport)
:result "image-bitmap"})
(rx/tap #(js/console.log "render-node" %))
(let [svg-node (dom/get-element "render")
fonts (fonts/get-node-fonts svg-node)
result {:node svg-node
:width (:width vport)
:result "image-bitmap"}]
(->> (fonts/render-font-styles-cached fonts)
(rx/map (fn [styles]
(assoc result
:styles styles)))
(rx/mapcat thr/render-node)
(rx/subs (fn [image-bitmap]
(.drawImage canvas-context image-bitmap 0 0)

View file

@ -238,7 +238,9 @@
:scope "penpot/rasterizer"
:payload payload}]
(when-not (identical? js/window js/parent)
(.postMessage js/parent message parent-origin))))
(if (instance? js/ImageBitmap payload)
(.postMessage js/parent message parent-origin #js [payload])
(.postMessage js/parent message parent-origin)))))
(defn- send-success!
"Sends a success message."