mirror of
https://github.com/penpot/penpot.git
synced 2026-05-14 04:24:01 +00:00
430 lines
19 KiB
Clojure
430 lines
19 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
;;
|
|
;; Copyright (c) KALEIDOS INC
|
|
|
|
(ns app.render-wasm.api.fonts
|
|
(:require
|
|
[app.common.data :as d]
|
|
[app.common.data.macros :as dm]
|
|
[app.common.logging :as log]
|
|
[app.common.types.text :as txt]
|
|
[app.common.uuid :as uuid]
|
|
[app.config :as cf]
|
|
[app.main.fonts :as fonts]
|
|
[app.main.store :as st]
|
|
[app.render-wasm.helpers :as h]
|
|
[app.render-wasm.wasm :as wasm]
|
|
[app.util.http :as http]
|
|
[beicon.v2.core :as rx]
|
|
[cuerdas.core :as str]
|
|
[goog.object :as gobj]
|
|
[lambdaisland.uri :as u]
|
|
[okulary.core :as l]))
|
|
|
|
(def ^:private fonts
|
|
(l/derived :fonts st/state))
|
|
|
|
(def ^:private default-font-size 14)
|
|
(def ^:private default-line-height 1.2)
|
|
(def ^:private default-letter-spacing 0.0)
|
|
|
|
(defn- google-font-id->uuid
|
|
[font-id]
|
|
(let [font (fonts/get-font-data font-id)]
|
|
(:uuid font)))
|
|
|
|
(defn- custom-font-id->uuid
|
|
[font-id]
|
|
(uuid/uuid (subs font-id (inc (str/index-of font-id "-")))))
|
|
|
|
(defn- font-backend
|
|
[font-id]
|
|
(cond
|
|
(str/starts-with? font-id "gfont-")
|
|
:google
|
|
(str/starts-with? font-id "custom-")
|
|
:custom
|
|
:else
|
|
:builtin))
|
|
|
|
(defn- font-db-data
|
|
[font-id font-variant-id font-weight-fallback font-style-fallback]
|
|
(let [font (fonts/get-font-data font-id)
|
|
closest-variant (fonts/find-closest-variant font font-weight-fallback font-style-fallback)
|
|
variant (fonts/get-variant font font-variant-id)]
|
|
(if (or (nil? closest-variant) (= closest-variant variant))
|
|
variant
|
|
closest-variant)))
|
|
|
|
(defn- font-id->uuid [font-id]
|
|
(case (font-backend font-id)
|
|
:google
|
|
(google-font-id->uuid font-id)
|
|
:custom
|
|
(custom-font-id->uuid font-id)
|
|
:builtin
|
|
uuid/zero))
|
|
|
|
|
|
(defn ^:private font-id->asset-id [font-id font-variant-id font-weight font-style]
|
|
(case (font-backend font-id)
|
|
:google
|
|
font-id
|
|
:custom
|
|
(let [font-uuid (custom-font-id->uuid font-id)
|
|
matching-font (some (fn [[_ font]]
|
|
(and (= (:font-id font) font-uuid)
|
|
(= (str (:font-weight font)) (str font-weight))
|
|
font))
|
|
(seq @fonts))]
|
|
(when matching-font
|
|
(:ttf-file-id matching-font)))
|
|
:builtin
|
|
(let [variant (font-db-data font-id font-variant-id font-weight font-style)]
|
|
(:ttf-url variant))))
|
|
|
|
(defn update-text-layout
|
|
[id]
|
|
(when wasm/context-initialized?
|
|
(let [shape-id-buffer (uuid/get-u32 id)]
|
|
(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)))))
|
|
|
|
;; IMPORTANT: Only TTF fonts can be stored.
|
|
(defn- store-font-buffer
|
|
[font-data font-array-buffer emoji? fallback?]
|
|
(let [font-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 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))
|
|
|
|
;; This variable will store the fonts that are currently being fetched
|
|
;; so we don't fetch more than once the same font
|
|
(def fetching (atom #{}))
|
|
|
|
(defn- fetch-font
|
|
[font-data font-url emoji? fallback?]
|
|
(when-not (contains? @fetching font-url)
|
|
(swap! fetching conj font-url)
|
|
{:key font-url
|
|
:callback
|
|
(fn []
|
|
(->> (http/send! {:method :get
|
|
:uri font-url
|
|
:response-type :buffer})
|
|
(rx/map (fn [{:keys [body]}]
|
|
(swap! fetching disj font-url)
|
|
(store-font-buffer font-data body emoji? fallback?)))
|
|
(rx/catch (fn [cause]
|
|
(swap! fetching disj font-url)
|
|
(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 font-weight font-style]
|
|
(let [variant (font-db-data font-id font-variant-id font-weight font-style)]
|
|
(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 font-weight font-style]
|
|
(case (font-backend font-id)
|
|
:google
|
|
(google-font-ttf-url font-id font-variant-id font-weight font-style)
|
|
: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 font-stored?
|
|
[font-data emoji?]
|
|
(when-let [id-buffer (uuid/get-u32 (:wasm-id font-data))]
|
|
(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)
|
|
(:weight font-data)
|
|
(:style font-data)
|
|
emoji?))))
|
|
|
|
(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)
|
|
(:weight font-data)
|
|
(:style-name font-data))
|
|
id-buffer (uuid/get-u32 (:wasm-id font-data))
|
|
font-data (assoc font-data :family-id-buffer id-buffer)
|
|
font-stored? (font-stored? font-data emoji?)]
|
|
(when-not font-stored?
|
|
(fetch-font font-data uri emoji? fallback?)))))
|
|
|
|
(defn serialize-font-style
|
|
[font-style]
|
|
(case font-style
|
|
"normal" 0
|
|
"regular" 0
|
|
"italic" 1
|
|
0))
|
|
|
|
(defn normalize-font-id
|
|
[font-id]
|
|
(try
|
|
(if ^boolean (str/starts-with? font-id "gfont-")
|
|
(google-font-id->uuid font-id)
|
|
(let [no-prefix (subs font-id (inc (str/index-of font-id "-")))]
|
|
(if (or (nil? no-prefix) (not (string? no-prefix)) (str/blank? no-prefix))
|
|
uuid/zero
|
|
(uuid/parse no-prefix))))
|
|
(catch :default _e
|
|
uuid/zero)))
|
|
|
|
(defn normalize-span-font
|
|
[span paragraph]
|
|
(let [font-id (:font-id span)
|
|
font-variant-id (:font-variant-id span)
|
|
font-weight-fallback (or (:font-weight span) (:font-weight paragraph))
|
|
font-style-fallback (or (:font-style span) (:font-style paragraph))
|
|
font-data (font-db-data font-id font-variant-id font-weight-fallback font-style-fallback)]
|
|
(-> span
|
|
(assoc :font-variant-id (or (:id font-data) (:id font-data) font-variant-id)
|
|
:font-weight (or (:weight font-data) font-weight-fallback)
|
|
:font-style (or (:style font-data) font-style-fallback)))))
|
|
|
|
(defn normalize-paragraph-font
|
|
[paragraph]
|
|
(let [font-id (:font-id paragraph)
|
|
font-variant-id (:font-variant-id paragraph)
|
|
font-weight-fallback (:font-weight paragraph)
|
|
font-style-fallback (:font-style paragraph)
|
|
font-data (font-db-data font-id font-variant-id font-weight-fallback font-style-fallback)]
|
|
(-> paragraph
|
|
(assoc :font-variant-id (or (:id font-data) (:id font-data) font-variant-id)
|
|
:font-weight (or (:weight font-data) font-weight-fallback)
|
|
:font-style (or (:style font-data) font-style-fallback)))))
|
|
|
|
(defn serialize-font-size
|
|
[font-size]
|
|
(cond
|
|
(number? font-size)
|
|
font-size
|
|
|
|
(string? font-size)
|
|
(or (d/parse-double font-size) default-font-size)))
|
|
|
|
(defn serialize-font-weight
|
|
[font-weight]
|
|
(if (number? font-weight)
|
|
font-weight
|
|
(let [font-weight-str (str font-weight)]
|
|
(cond
|
|
(re-matches #"\d+" font-weight-str)
|
|
(js/Number font-weight-str)
|
|
|
|
(str/includes? font-weight-str "bold")
|
|
700
|
|
(str/includes? font-weight-str "black")
|
|
900
|
|
(str/includes? font-weight-str "extrabold")
|
|
800
|
|
(str/includes? font-weight-str "extralight")
|
|
200
|
|
(str/includes? font-weight-str "light")
|
|
300
|
|
(str/includes? font-weight-str "medium")
|
|
500
|
|
(str/includes? font-weight-str "semibold")
|
|
600
|
|
(str/includes? font-weight-str "thin")
|
|
100
|
|
:else
|
|
400))))
|
|
|
|
(defn serialize-line-height
|
|
([line-height]
|
|
(serialize-line-height line-height default-line-height))
|
|
([line-height default-value]
|
|
(cond
|
|
(number? line-height)
|
|
line-height
|
|
|
|
(string? line-height)
|
|
(or (d/parse-double line-height) default-value))))
|
|
|
|
(defn serialize-letter-spacing
|
|
[letter-spacing]
|
|
(cond
|
|
(number? letter-spacing)
|
|
letter-spacing
|
|
|
|
(string? letter-spacing)
|
|
(or (d/parse-double letter-spacing) default-letter-spacing)))
|
|
|
|
|
|
(defn normalize-font-variant
|
|
[font-variant-id]
|
|
(if (or (nil? font-variant-id) (str/blank? font-variant-id))
|
|
"regular"
|
|
font-variant-id))
|
|
|
|
(defn make-font-data
|
|
[font]
|
|
(let [font-id (get font :font-id)
|
|
font-variant-id (get font :font-variant-id)
|
|
normalized-variant-id (when font-variant-id
|
|
(-> font-variant-id
|
|
(str/lower)
|
|
(str/replace #"\s+" "")))
|
|
font-weight-fallback (or (get font :font-weight) 400)
|
|
font-style-fallback (or (get font :font-style) "normal")
|
|
emoji? (get font :is-emoji false)
|
|
fallback? (get font :is-fallback false)
|
|
font-data (font-db-data font-id normalized-variant-id font-weight-fallback font-style-fallback)
|
|
wasm-id (font-id->uuid font-id)
|
|
raw-weight (or (:weight font-data) font-weight-fallback)
|
|
weight (serialize-font-weight raw-weight)
|
|
style (cond
|
|
(str/includes? (or normalized-variant-id "") "italic") "italic"
|
|
(str/includes? raw-weight "italic") "italic"
|
|
:else font-style-fallback)
|
|
variant-id (or (:id font-data) normalized-variant-id)
|
|
asset-id (font-id->asset-id font-id variant-id raw-weight style)]
|
|
{:wasm-id wasm-id
|
|
:font-id font-id
|
|
:font-variant-id variant-id
|
|
:style (serialize-font-style style)
|
|
:style-name style
|
|
:weight weight
|
|
:emoji? emoji?
|
|
:fallback? fallback?
|
|
:asset-id asset-id}))
|
|
|
|
(defn store-font
|
|
[font]
|
|
(let [{:keys [asset-id emoji? fallback?] :as font-data} (make-font-data font)]
|
|
(store-font-id font-data asset-id emoji? fallback?)))
|
|
|
|
;; FIXME: This is a temporary function to load the fallback fonts for the editor.
|
|
;; Once we render the editor content within wasm, we can remove this function.
|
|
(defn load-fallback-fonts-for-editor!
|
|
[fonts]
|
|
(doseq [font fonts]
|
|
(fonts/ensure-loaded! (:font-id font) (:font-variant-id font))))
|
|
|
|
|
|
(defn get-content-fonts
|
|
"Extends from app.main.fonts/get-content-fonts. Extracts the fonts used by the content of a text shape, resolving the correct font variant info."
|
|
[content]
|
|
(let [paragraph-set (first (get content :children))
|
|
paragraphs (get paragraph-set :children)]
|
|
(->> paragraphs
|
|
(mapcat #(get % :children))
|
|
(filter txt/is-text-node?)
|
|
(reduce
|
|
(fn [result {:keys [font-id font-variant-id font-weight font-style] :as node}]
|
|
(let [resolved-font-id (or font-id (:font-id txt/default-typography))
|
|
resolved-variant-id (or font-variant-id (:font-variant-id txt/default-typography))
|
|
font-weight-fallback (or font-weight (:font-weight txt/default-typography) 400)
|
|
font-style-fallback (or font-style (:font-style txt/default-typography) "normal")
|
|
font-data (font-db-data resolved-font-id resolved-variant-id font-weight-fallback font-style-fallback)
|
|
font-ref {:font-id resolved-font-id
|
|
:font-variant-id (or (:id font-data) (:name font-data) resolved-variant-id)
|
|
:font-weight (or (:weight font-data) font-weight-fallback)
|
|
:font-style (or (:style font-data) font-style-fallback)}]
|
|
(conj result font-ref)))
|
|
#{}))))
|
|
|
|
(defn store-fonts
|
|
[fonts]
|
|
(keep (fn [font] (store-font font)) fonts))
|
|
|
|
(defn add-emoji-font
|
|
[fonts]
|
|
(conj fonts {:font-id "gfont-noto-color-emoji"
|
|
:font-variant-id "regular"
|
|
:style 0
|
|
:weight 400
|
|
:is-emoji true
|
|
:is-fallback true}))
|
|
|
|
(def noto-fonts
|
|
{:japanese {:font-id "gfont-noto-sans-jp" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:chinese {:font-id "gfont-noto-sans-sc" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:korean {:font-id "gfont-noto-sans-kr" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:arabic {:font-id "gfont-noto-sans-arabic" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:cyrillic {:font-id "gfont-noto-sans" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:greek {:font-id "gfont-noto-sans" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:hebrew {:font-id "gfont-noto-sans-hebrew" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:thai {:font-id "gfont-noto-sans-thai" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:devanagari {:font-id "gfont-noto-sans" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:tamil {:font-id "gfont-noto-sans-tamil" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:latin-ext {:font-id "gfont-noto-sans" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:vietnamese {:font-id "gfont-noto-sans" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:armenian {:font-id "gfont-noto-sans-armenian" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:bengali {:font-id "gfont-noto-sans-bengali" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:cherokee {:font-id "gfont-noto-sans-cherokee" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:ethiopic {:font-id "gfont-noto-sans-ethiopic" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:georgian {:font-id "gfont-noto-sans-georgian" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:gujarati {:font-id "gfont-noto-sans-gujarati" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:gurmukhi {:font-id "gfont-noto-sans-gurmukhi" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:khmer {:font-id "gfont-noto-sans-khmer" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:lao {:font-id "gfont-noto-sans-lao" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:malayalam {:font-id "gfont-noto-sans-malayalam" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:myanmar {:font-id "gfont-noto-sans-myanmar" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:sinhala {:font-id "gfont-noto-sans-sinhala" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:telugu {:font-id "gfont-noto-sans-telugu" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:tibetan {:font-id "gfont-noto-serif-tibetan" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:javanese {:font-id "gfont-noto-sans-javanese" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:kannada {:font-id "gfont-noto-sans-kannada" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:oriya {:font-id "gfont-noto-sans-oriya" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:mongolian {:font-id "gfont-noto-sans-mongolian" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:syriac {:font-id "gfont-noto-sans-syriac" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:tifinagh {:font-id "gfont-noto-sans-tifinagh" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:coptic {:font-id "gfont-noto-sans-coptic" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:ol-chiki {:font-id "gfont-noto-sans-ol-chiki" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:vai {:font-id "gfont-noto-sans-vai" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:shavian {:font-id "gfont-noto-sans-shavian" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:osmanya {:font-id "gfont-noto-sans-osmanya" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:runic {:font-id "gfont-noto-sans-runic" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:old-italic {:font-id "gfont-noto-sans-old-italic" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:brahmi {:font-id "gfont-noto-sans-brahmi" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:modi {:font-id "gfont-noto-sans-modi" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:sora-sompeng {:font-id "gfont-noto-sans-sora-sompeng" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:bamum {:font-id "gfont-noto-sans-bamum" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:meroitic {:font-id "gfont-noto-sans-meroitic" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:symbols {:font-id "gfont-noto-sans-symbols" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:symbols-2 {:font-id "gfont-noto-sans-symbols-2" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}
|
|
:music {:font-id "gfont-noto-music" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true}})
|
|
|
|
(defn add-noto-fonts [fonts languages]
|
|
(reduce (fn [acc lang]
|
|
(if-let [font (get noto-fonts lang)]
|
|
(conj acc font)
|
|
acc))
|
|
fonts
|
|
languages))
|