0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-12 10:09:03 -05:00

🐛 Fix incorrect handling of font embedding.

This commit is contained in:
Andrey Antukh 2021-04-09 14:12:53 +02:00
parent 080dd88509
commit a92820e910

View file

@ -31,6 +31,7 @@
")
;; -- Embed fonts into styles
(defn get-node-fonts
[node]
(let [current-font (if (not (nil? (:font-id node)))
@ -39,35 +40,39 @@
children-font (map get-node-fonts (:children node))]
(reduce set/union (conj children-font current-font))))
(defn get-local-font-css
[font-id font-variant-id]
(let [{:keys [family variants] :as font} (get @fonts/fontsdb font-id)
{:keys [name weight style suffix] :as variant} (d/seek #(= (:id %) font-variant-id) variants)]
(-> (str/format font-face-template {:family family :suffix (or suffix font-variant-id) :width weight})
(p/resolved))))
(defn get-font-css
"Given a font and the variant-id, retrieves the style CSS for it."
[{:keys [id backend family variants] :as font} font-variant-id]
(if (= :google backend)
(-> (fonts/gfont-url family [{:id font-variant-id}])
(js/fetch)
(p/then (fn [res] (.text res))))
(defn fetch-font-css
[font-id font-variant-id]
(let [{:keys [backend family] :as entry} (get @fonts/fontsdb font-id)]
(if (= :google backend)
(-> (fonts/gfont-url family [{:id font-variant-id}])
(js/fetch)
(p/then (fn [res] (.text res))))
(get-local-font-css font-id font-variant-id))))
(let [{:keys [name weight style suffix] :as variant} (d/seek #(= (:id %) font-variant-id) variants)
result (str/fmt font-face-template {:family family
:style style
:suffix (or suffix font-variant-id)
:weight weight})]
(p/resolved result))))
(defn get-text-font-data [text]
(->> text
(defn get-font-data
"Parses the CSS and retrieves the font data as DataURI."
[^string css]
(->> css
(re-seq #"url\(([^)]+)\)")
(map second)
(map df/fetch-as-data-uri)
(p/all)))
(defn embed-font [{:keys [font-id font-variant-id] :or {font-variant-id "regular"}}]
(let [{:keys [backend family]} (get @fonts/fontsdb font-id)]
(p/let [font-text (fetch-font-css font-id font-variant-id)
url-to-data (get-text-font-data font-text)
(defn embed-font
"Given a font-id and font-variant-id, retrieves the CSS for it and
convert all external urls to embedded data URI's."
[{: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))]
(reduce replace-text font-text url-to-data))))
(reduce replace-text css url-to-data))))
(mf/defc embed-fontfaces-style
{::mf/wrap-props false}
@ -81,7 +86,9 @@
font-to-embed (if (empty? font-to-embed) #{txt/default-text-attrs} font-to-embed)
embeded (map embed-font font-to-embed)]
(-> (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)
[:style @style])))