Repository: penpot/penpot
File: frontend/src/app/render_wasm/api/fonts.cljs (ClojureScript)

Code before:
            (aget font-id-buffer 0)
            (aget font-id-buffer 1)
            (aget font-id-buffer 2)
            (aget font-id-buffer 3)
            (:weight font-data)
            (:style font-data)
            emoji?
            fallback?)
    true))

(defn- fetch-font
  [shape-id font-data font-url emoji? fallback?]
  {:key font-url
   :callback #(->> (http/send! {:method :get
                                :uri font-url
                                :response-type :buffer})
                   (rx/catch (fn [cause]
                               (log/error :hint (dm/str "Could not fetch font " font-url)
                                          :cause cause)
                               (rx/empty)))
                   (rx/map (fn [{:keys [body]}]
                             (store-font-buffer shape-id font-data body emoji? fallback?))))})

(defn- google-font-ttf-url
  [font-id font-variant-id]
  (let [variant (font-db-data font-id font-variant-id)]
    (if-let [ttf-url (:ttf-url variant)]
      (str/replace ttf-url "https://fonts.gstatic.com/s/" (u/join cf/public-uri "/internal/gfonts/font/"))
      nil)))

(defn- font-id->ttf-url
  [font-id asset-id font-variant-id]
  (case (font-backend font-id)
    :google
    (google-font-ttf-url font-id font-variant-id)
    :custom
    (dm/str (u/join cf/public-uri "assets/by-id/" asset-id))
    :builtin
    (dm/str (u/join cf/public-uri "fonts/" asset-id))))

(defn- store-font-id
  [shape-id font-data asset-id emoji? fallback?]
  (when asset-id
    (let [uri (font-id->ttf-url (:font-id font-data) asset-id (:font-variant-id font-data))
          id-buffer (uuid/get-u32 (:wasm-id font-data))
          font-data (assoc font-data :family-id-buffer id-buffer)
          font-stored? (not= 0 (h/call wasm/internal-module "_is_font_uploaded"
                                       (aget id-buffer 0)
                                       (aget id-buffer 1)
                                       (aget id-buffer 2)
                                       (aget id-buffer 3)

Review comment (suggestion):
I suggest put `catch` as last element of this stream chain.
Also, split the hit on two keys on logging for avoid doing unnecesary concat:

```clojure
(log/error :hint "could not fetch font"
           :font-url font-url
           :cause cause)
```

Code after:
            (aget font-id-buffer 0)
            (aget font-id-buffer 1)
            (aget font-id-buffer 2)
            (aget font-id-buffer 3)
            (:weight font-data)
            (:style font-data)
            emoji?
            fallback?)

    (h/call wasm/internal-module "_update_shape_text_layout_for"
            (aget shape-id-buffer 0)
            (aget shape-id-buffer 1)
            (aget shape-id-buffer 2)
            (aget shape-id-buffer 3))

    true))

(defn- fetch-font
  [shape-id font-data font-url emoji? fallback?]
  {:key font-url
   :callback #(->> (http/send! {:method :get
                                :uri font-url
                                :response-type :buffer})
                   (rx/map (fn [{:keys [body]}]
                             (store-font-buffer shape-id font-data body emoji? fallback?)))
                   (rx/catch (fn [cause]
                               (log/error :hint "Could not fetch font"
                                          :font-url font-url
                                          :cause cause)
                               (rx/empty))))})

(defn- google-font-ttf-url
  [font-id font-variant-id]
  (let [variant (font-db-data font-id font-variant-id)]
    (if-let [ttf-url (:ttf-url variant)]
      (str/replace ttf-url "https://fonts.gstatic.com/s/" (u/join cf/public-uri "/internal/gfonts/font/"))
      nil)))

(defn- font-id->ttf-url
  [font-id asset-id font-variant-id]
  (case (font-backend font-id)
    :google
    (google-font-ttf-url font-id font-variant-id)
    :custom
    (dm/str (u/join cf/public-uri "assets/by-id/" asset-id))
    :builtin
    (dm/str (u/join cf/public-uri "fonts/" asset-id))))

(defn- store-font-id
  [shape-id font-data asset-id emoji? fallback?]
  (when asset-id