Merge pull request #6652 from penpot/elenatorro-fix-load-fonts

🐛 Fix fonts initialization
This commit is contained in:
Alejandro Alonso 2025-06-09 09:31:00 +02:00 committed by GitHub
commit d008ea9edd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 67 additions and 54 deletions

View file

@ -189,11 +189,12 @@
(defn- get-string-length [string] (+ (count string) 1)) (defn- get-string-length [string] (+ (count string) 1))
(defn- store-image (defn- fetch-image
[id] [id]
(let [buffer (uuid/get-u32 id) (let [buffer (uuid/get-u32 id)
url (cf/resolve-file-media {:id id})] url (cf/resolve-file-media {:id id})]
(->> (http/send! {:method :get {:key url
:callback #(->> (http/send! {:method :get
:uri url :uri url
:response-type :blob}) :response-type :blob})
(rx/map :body) (rx/map :body)
@ -209,7 +210,7 @@
(aget buffer 1) (aget buffer 1)
(aget buffer 2) (aget buffer 2)
(aget buffer 3)) (aget buffer 3))
true)))))) true))))}))
(defn- get-fill-images (defn- get-fill-images
[leaf] [leaf]
@ -227,7 +228,7 @@
(aget buffer 2) (aget buffer 2)
(aget buffer 3))] (aget buffer 3))]
(when (zero? cached-image?) (when (zero? cached-image?)
(store-image id)))))) (fetch-image id))))))
(defn set-shape-text-images (defn set-shape-text-images
[content] [content]
@ -269,7 +270,7 @@
(aget buffer 2) (aget buffer 2)
(aget buffer 3))] (aget buffer 3))]
(when (zero? cached-image?) (when (zero? cached-image?)
(store-image id)))) (fetch-image id))))
image-fills)))) image-fills))))
(defn set-shape-strokes (defn set-shape-strokes
@ -308,7 +309,7 @@
(dm/get-prop image :height)) (dm/get-prop image :height))
(h/call wasm/internal-module "_add_shape_stroke_fill") (h/call wasm/internal-module "_add_shape_stroke_fill")
(when (== cached-image? 0) (when (== cached-image? 0)
(store-image id))) (fetch-image id)))
(some? color) (some? color)
(do (do
@ -640,9 +641,10 @@
(swap! languages into (t/get-languages text)) (swap! languages into (t/get-languages text))
(t/write-shape-text leaves paragraph text)) (t/write-shape-text leaves paragraph text))
(recur (inc index)))))) (recur (inc index))))))
(let [updated-fonts (let [updated-fonts
(-> fonts (-> fonts
(cond-> emoji? (f/add-emoji-font)) (cond-> @emoji? (f/add-emoji-font))
(f/add-noto-fonts @languages))] (f/add-noto-fonts @languages))]
(f/store-fonts updated-fonts)))) (f/store-fonts updated-fonts))))
@ -742,8 +744,6 @@
(set-shape-svg-raw-content (get-static-markup shape))) (set-shape-svg-raw-content (get-static-markup shape)))
(when (some? corners) (set-shape-corners corners)) (when (some? corners) (set-shape-corners corners))
(when (some? shadows) (set-shape-shadows shadows)) (when (some? shadows) (set-shape-shadows shadows))
(when (and (= type :text) (some? content))
(set-shape-text content))
(when (= type :text) (when (= type :text)
(set-shape-grow-type grow-type)) (set-shape-grow-type grow-type))
(when (or (ctl/any-layout? shape) (when (or (ctl/any-layout? shape)
@ -764,16 +764,20 @@
pending))) pending)))
(defn process-object (defn process-pending
[shape] [pending]
(let [pending (set-object [] shape)] (when-let [pending (-> (d/index-by :key :callback pending) vals)]
(when-let [pending (seq pending)]
(->> (rx/from pending) (->> (rx/from pending)
(rx/mapcat identity) (rx/mapcat (fn [callback] (callback)))
(rx/reduce conj []) (rx/reduce conj [])
(rx/subs! (fn [_] (rx/subs! (fn [_]
(clear-drawing-cache) (clear-drawing-cache)
(request-render "set-objects"))))))) (request-render "set-objects"))))))
(defn process-object
[shape]
(let [pending (set-object [] shape)]
(process-pending pending)))
(defn set-objects (defn set-objects
[objects] [objects]
@ -790,13 +794,7 @@
(perf/end-measure "set-objects") (perf/end-measure "set-objects")
(clear-drawing-cache) (clear-drawing-cache)
(request-render "set-objects") (request-render "set-objects")
(when-let [pending (seq pending)] (process-pending pending)))
(->> (rx/from pending)
(rx/mapcat identity)
(rx/reduce conj [])
(rx/subs! (fn [_]
(clear-drawing-cache)
(request-render "set-objects")))))))
(defn set-structure-modifiers (defn set-structure-modifiers
[entries] [entries]

View file

@ -94,13 +94,14 @@
fallback?) fallback?)
true)) true))
(defn- store-font-url (defn- fetch-font
[font-data font-url emoji? fallback?] [font-data font-url emoji? fallback?]
(->> (http/send! {:method :get {:key font-url
:callback #(->> (http/send! {:method :get
:uri font-url :uri font-url
:response-type :buffer}) :response-type :buffer})
(rx/map (fn [{:keys [body]}] (rx/map (fn [{:keys [body]}]
(store-font-buffer font-data body emoji? fallback?))))) (store-font-buffer font-data body emoji? fallback?))))})
(defn- google-font-ttf-url (defn- google-font-ttf-url
[font-id font-variant-id] [font-id font-variant-id]
@ -131,9 +132,10 @@
(aget id-buffer 2) (aget id-buffer 2)
(aget id-buffer 3) (aget id-buffer 3)
(:weight font-data) (:weight font-data)
(:style font-data)))] (:style font-data)
emoji?))]
(when-not font-stored? (when-not font-stored?
(store-font-url font-data uri emoji? fallback?))))) (fetch-font font-data uri emoji? fallback?)))))
(defn serialize-font-style (defn serialize-font-style
[font-style] [font-style]

View file

@ -148,7 +148,7 @@
(h/call wasm/internal-module "_set_shape_text_content")) (h/call wasm/internal-module "_set_shape_text_content"))
(def ^:private emoji-pattern #"[\uD83C-\uDBFF][\uDC00-\uDFFF]") (def ^:private emoji-pattern #"[\uD83C-\uDBFF][\uDC00-\uDFFF]|[\u2600-\u27BF]")
(def ^:private unicode-ranges (def ^:private unicode-ranges
{:japanese #"[\u3040-\u30FF\u31F0-\u31FF\uFF66-\uFF9F]" {:japanese #"[\u3040-\u30FF\u31F0-\u31FF\uFF66-\uFF9F]"
@ -198,7 +198,7 @@
(defn contains-emoji? [text] (defn contains-emoji? [text]
(boolean (re-find emoji-pattern text))) (boolean (some #(re-find emoji-pattern %) (seq text))))
(defn get-languages [text] (defn get-languages [text]
(reduce-kv (fn [result lang pattern] (reduce-kv (fn [result lang pattern]

View file

@ -66,7 +66,7 @@ impl FontStore {
is_emoji: bool, is_emoji: bool,
is_fallback: bool, is_fallback: bool,
) -> Result<(), String> { ) -> Result<(), String> {
if self.has_family(&family) { if self.has_family(&family, is_emoji) {
return Ok(()); return Ok(());
} }
@ -92,9 +92,14 @@ impl FontStore {
Ok(()) Ok(())
} }
pub fn has_family(&self, family: &FontFamily) -> bool { pub fn has_family(&self, family: &FontFamily, is_emoji: bool) -> bool {
let serialized = format!("{}", family); let alias = format!("{}", family);
self.font_provider.family_names().any(|x| x == serialized) let font_name = if is_emoji {
DEFAULT_EMOJI_FONT
} else {
alias.as_str()
};
self.font_provider.family_names().any(|x| x == font_name)
} }
pub fn get_fallback(&self) -> &HashSet<String> { pub fn get_fallback(&self) -> &HashSet<String> {

View file

@ -31,11 +31,19 @@ pub extern "C" fn store_font(
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn is_font_uploaded(a: u32, b: u32, c: u32, d: u32, weight: u32, style: u8) -> bool { pub extern "C" fn is_font_uploaded(
a: u32,
b: u32,
c: u32,
d: u32,
weight: u32,
style: u8,
is_emoji: bool,
) -> bool {
with_state!(state, { with_state!(state, {
let id = uuid_from_u32_quartet(a, b, c, d); let id = uuid_from_u32_quartet(a, b, c, d);
let family = FontFamily::new(id, weight, style.into()); let family = FontFamily::new(id, weight, style.into());
let res = state.render_state().fonts().has_family(&family); let res = state.render_state().fonts().has_family(&family, is_emoji);
res res
}) })