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

Code before:

;; IMPORTANT: It should be noted that only TTF fonts can be stored.
(defn- store-font-buffer
  [font-data font-array-buffer emoji? fallback?]
  (let [id-buffer (:family-id-buffer font-data)
        size (.-byteLength font-array-buffer)
        ptr  (h/call wasm/internal-module "_alloc_bytes" size)
        heap (gobj/get ^js wasm/internal-module "HEAPU8")
        mem  (js/Uint8Array. (.-buffer heap) ptr size)]
    (.set mem (js/Uint8Array. font-array-buffer))
    (h/call wasm/internal-module "_store_font"
            (aget id-buffer 0)
            (aget id-buffer 1)
            (aget id-buffer 2)
            (aget id-buffer 3)
            (:weight font-data)
            (:style font-data)
            emoji?
            fallback?)
    true))

(defn- store-font-url
  [font-data font-url emoji? fallback?]
  (->> (http/send! {:method :get
                    :uri font-url
                    :response-type :blob})
       (rx/map :body)
       (rx/mapcat wapi/read-file-as-array-buffer)
       (rx/map (fn [array-buffer] (store-font-buffer font-data array-buffer 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/" font-id))
    :builtin
    (dm/str (u/join cf/public-uri "fonts/" asset-id))))

(defn- store-font-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))

Review comment (suggestion):
You can use `:response-type :buffer` and avoid the `wapi/read-file-as-array-buffer`

Code after:
;; IMPORTANT: It should be noted that only TTF fonts can be stored.
(defn- store-font-buffer
  [font-data font-array-buffer emoji? fallback?]
  (let [id-buffer (:family-id-buffer font-data)
        size (.-byteLength font-array-buffer)
        ptr  (h/call wasm/internal-module "_alloc_bytes" size)
        heap (gobj/get ^js wasm/internal-module "HEAPU8")
        mem  (js/Uint8Array. (.-buffer heap) ptr size)]
    (.set mem (js/Uint8Array. font-array-buffer))
    (h/call wasm/internal-module "_store_font"
            (aget id-buffer 0)
            (aget id-buffer 1)
            (aget id-buffer 2)
            (aget id-buffer 3)
            (:weight font-data)
            (:style font-data)
            emoji?
            fallback?)
    true))

(defn- store-font-url
  [font-data font-url emoji? fallback?]
  (->> (http/send! {:method :get
                    :uri font-url
                    :response-type :buffer})
       (rx/map (fn [{:keys [body]}]
                 (store-font-buffer 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/" font-id))
    :builtin
    (dm/str (u/join cf/public-uri "fonts/" asset-id))))

(defn- store-font-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)