mirror of
https://github.com/penpot/penpot.git
synced 2025-06-04 04:31:37 +02:00
🐛 Fix incorrect handling of font embedding.
This commit is contained in:
parent
080dd88509
commit
a92820e910
1 changed files with 29 additions and 22 deletions
|
@ -31,6 +31,7 @@
|
||||||
")
|
")
|
||||||
|
|
||||||
;; -- Embed fonts into styles
|
;; -- Embed fonts into styles
|
||||||
|
|
||||||
(defn get-node-fonts
|
(defn get-node-fonts
|
||||||
[node]
|
[node]
|
||||||
(let [current-font (if (not (nil? (:font-id node)))
|
(let [current-font (if (not (nil? (:font-id node)))
|
||||||
|
@ -39,35 +40,39 @@
|
||||||
children-font (map get-node-fonts (:children node))]
|
children-font (map get-node-fonts (:children node))]
|
||||||
(reduce set/union (conj children-font current-font))))
|
(reduce set/union (conj children-font current-font))))
|
||||||
|
|
||||||
(defn get-local-font-css
|
(defn get-font-css
|
||||||
[font-id font-variant-id]
|
"Given a font and the variant-id, retrieves the style CSS for it."
|
||||||
(let [{:keys [family variants] :as font} (get @fonts/fontsdb font-id)
|
[{:keys [id backend family variants] :as font} font-variant-id]
|
||||||
{:keys [name weight style suffix] :as variant} (d/seek #(= (:id %) font-variant-id) variants)]
|
(if (= :google backend)
|
||||||
(-> (str/format font-face-template {:family family :suffix (or suffix font-variant-id) :width weight})
|
(-> (fonts/gfont-url family [{:id font-variant-id}])
|
||||||
(p/resolved))))
|
(js/fetch)
|
||||||
|
(p/then (fn [res] (.text res))))
|
||||||
|
|
||||||
(defn fetch-font-css
|
(let [{:keys [name weight style suffix] :as variant} (d/seek #(= (:id %) font-variant-id) variants)
|
||||||
[font-id font-variant-id]
|
result (str/fmt font-face-template {:family family
|
||||||
(let [{:keys [backend family] :as entry} (get @fonts/fontsdb font-id)]
|
:style style
|
||||||
(if (= :google backend)
|
:suffix (or suffix font-variant-id)
|
||||||
(-> (fonts/gfont-url family [{:id font-variant-id}])
|
:weight weight})]
|
||||||
(js/fetch)
|
(p/resolved result))))
|
||||||
(p/then (fn [res] (.text res))))
|
|
||||||
(get-local-font-css font-id font-variant-id))))
|
|
||||||
|
|
||||||
(defn get-text-font-data [text]
|
(defn get-font-data
|
||||||
(->> text
|
"Parses the CSS and retrieves the font data as DataURI."
|
||||||
|
[^string css]
|
||||||
|
(->> css
|
||||||
(re-seq #"url\(([^)]+)\)")
|
(re-seq #"url\(([^)]+)\)")
|
||||||
(map second)
|
(map second)
|
||||||
(map df/fetch-as-data-uri)
|
(map df/fetch-as-data-uri)
|
||||||
(p/all)))
|
(p/all)))
|
||||||
|
|
||||||
(defn embed-font [{:keys [font-id font-variant-id] :or {font-variant-id "regular"}}]
|
(defn embed-font
|
||||||
(let [{:keys [backend family]} (get @fonts/fontsdb font-id)]
|
"Given a font-id and font-variant-id, retrieves the CSS for it and
|
||||||
(p/let [font-text (fetch-font-css font-id font-variant-id)
|
convert all external urls to embedded data URI's."
|
||||||
url-to-data (get-text-font-data font-text)
|
[{:keys [font-id font-variant-id] :or {font-variant-id "regular"}}]
|
||||||
|
(let [{:keys [backend family] :as font} (get @fonts/fontsdb font-id)]
|
||||||
|
(p/let [css (get-font-css font font-variant-id)
|
||||||
|
url-to-data (get-font-data css)
|
||||||
replace-text (fn [text [url data]] (str/replace text url data))]
|
replace-text (fn [text [url data]] (str/replace text url data))]
|
||||||
(reduce replace-text font-text url-to-data))))
|
(reduce replace-text css url-to-data))))
|
||||||
|
|
||||||
(mf/defc embed-fontfaces-style
|
(mf/defc embed-fontfaces-style
|
||||||
{::mf/wrap-props false}
|
{::mf/wrap-props false}
|
||||||
|
@ -81,7 +86,9 @@
|
||||||
font-to-embed (if (empty? font-to-embed) #{txt/default-text-attrs} font-to-embed)
|
font-to-embed (if (empty? font-to-embed) #{txt/default-text-attrs} font-to-embed)
|
||||||
embeded (map embed-font font-to-embed)]
|
embeded (map embed-font font-to-embed)]
|
||||||
(-> (p/all embeded)
|
(-> (p/all embeded)
|
||||||
(p/then (fn [result] (reset! style (str/join "\n" result))))))))
|
(p/then (fn [result]
|
||||||
|
(reset! style (str/join "\n" result))))))))
|
||||||
|
|
||||||
|
|
||||||
(when (some? @style)
|
(when (some? @style)
|
||||||
[:style @style])))
|
[:style @style])))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue