Merge pull request #6205 from penpot/elenatorro-10528-fix-google-font-variant-styles

🐛 Fix Google Fonts load by parsing italic variant ids correctly
This commit is contained in:
Elena Torró 2025-04-02 17:16:51 +02:00 committed by GitHub
commit 2ed780e14d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 32 deletions

View file

@ -22,10 +22,12 @@
{:id "italic" :name "italic" :weight "400" :style "italic" :ttf-url (get files "italic")} {:id "italic" :name "italic" :weight "400" :style "italic" :ttf-url (get files "italic")}
:else :else
(when-let [[a b c] (re-find #"^(\d+)(.*)$" variant)] (when-let [[id weight style] (re-find #"^(\d+)(.*)$" variant)]
(if (str/empty? c) {:id id
{:id a :name b :weight b :style "normal" :ttf-url (get files a)} :name variant
{:id a :name (str b " (" c ")") :weight b :style c :ttf-url (get files c)})))) :weight weight
:style (if (str/empty? style) "normal" style)
:ttf-url (get files id)})))
(defn- parse-gfont (defn- parse-gfont
[font] [font]

View file

@ -10,7 +10,7 @@
[app.common.data.macros :as dm] [app.common.data.macros :as dm]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.config :as cf] [app.config :as cf]
[app.main.fonts :as f] [app.main.fonts :as fonts]
[app.main.store :as st] [app.main.store :as st]
[app.render-wasm.helpers :as h] [app.render-wasm.helpers :as h]
[app.render-wasm.wasm :as wasm] [app.render-wasm.wasm :as wasm]
@ -27,7 +27,7 @@
(defn- google-font-id->uuid (defn- google-font-id->uuid
[font-id] [font-id]
(let [font (get @f/fontsdb font-id)] (let [font (fonts/get-font-data font-id)]
(:uuid font))) (:uuid font)))
(defn- custom-font-id->uuid (defn- custom-font-id->uuid
@ -39,14 +39,12 @@
(google-font-id->uuid font-id) (google-font-id->uuid font-id)
(custom-font-id->uuid font-id))) (custom-font-id->uuid font-id)))
(defn ^:private font-id->ttf-id [font-id font-style font-weight] (defn ^:private font-id->ttf-id [font-id font-variant-id]
(if (str/starts-with? font-id "gfont-") (if (str/starts-with? font-id "gfont-") font-id
font-id
(let [font-uuid (custom-font-id->uuid font-id) (let [font-uuid (custom-font-id->uuid font-id)
matching-font (d/seek (fn [[_ font]] matching-font (d/seek (fn [[_ font]]
(and (= (:font-id font) font-uuid) (and (= (:font-id font) font-uuid)
(= (:font-style font) font-style) (= (:font-variant-id font) font-variant-id)))
(= (:font-weight font) font-weight)))
(seq @fonts))] (seq @fonts))]
(when matching-font (when matching-font
(:ttf-file-id (second matching-font)))))) (:ttf-file-id (second matching-font))))))
@ -80,13 +78,13 @@
(defn- google-font-ttf-url (defn- google-font-ttf-url
[font-id font-variant-id] [font-id font-variant-id]
(let [font (get @f/fontsdb font-id) (let [font (fonts/get-font-data font-id)
variant (d/seek (fn [variant] variant (fonts/get-variant font font-variant-id)]
(= (:id variant) font-variant-id)) (if-let [ttf-url (:ttf-url variant)]
(:variants font)) (str/replace ttf-url "http://fonts.gstatic.com/s/" (u/join cf/public-uri "/internal/gfonts/font/"))
file (-> (:ttf-url variant) (do
(str/replace "http://fonts.gstatic.com/s/" (u/join cf/public-uri "/internal/gfonts/font/")))] (println "Variant TTF URL not found for" font-id font-variant-id)
file)) nil))))
(defn- font-id->ttf-url (defn- font-id->ttf-url
[font-id font-variant-id] [font-id font-variant-id]
@ -137,16 +135,19 @@
(keep (fn [font] (keep (fn [font]
(let [font-id (dm/get-prop font :font-id) (let [font-id (dm/get-prop font :font-id)
font-variant-id (dm/get-prop font :font-variant-id) font-variant-id (dm/get-prop font :font-variant-id)
variant-parts (str/split font-variant-id #"\-")
variant-parts (if (= (count variant-parts) 1)
(conj variant-parts "400")
variant-parts)
style (first variant-parts)
weight (serialize-font-weight (last variant-parts))
asset-id (font-id->ttf-id font-id style weight)
wasm-id (font-id->uuid font-id) wasm-id (font-id->uuid font-id)
weight (serialize-font-weight
(if-let [weight-match (re-find #"\d+" font-variant-id)]
(js/parseInt weight-match)
400))
style (serialize-font-style (cond
(str/includes? font-variant-id "italic") "italic"
:else "normal"))
asset-id (font-id->ttf-id font-id font-variant-id)
font-data {:wasm-id wasm-id font-data {:wasm-id wasm-id
:font-variant-id font-variant-id :font-variant-id font-variant-id
:style (serialize-font-style style) :style style
:weight weight}] :weight weight}]
(store-font-id font-data asset-id))) fonts)) (store-font-id font-data asset-id))) fonts))