penpot/frontend/src/app/main/fonts.clj
2025-08-07 11:14:40 +02:00

53 lines
1.5 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.main.fonts
"A fonts loading macros."
(:require
[app.common.uuid :as uuid]
[clojure.data.json :as json]
[clojure.java.io :as io]
[cuerdas.core :as str]))
(defn- parse-gfont-variant
[variant files]
(cond
(= "regular" variant)
{:id "regular" :name "400" :weight "400" :style "normal" :ttf-url (get files "regular")}
(= "italic" variant)
{:id "italic" :name "400 Italic" :weight "400" :style "italic" :ttf-url (get files "italic")}
:else
(when-let [[id weight style] (re-find #"^(\d+)(.*)$" variant)]
{:id id
:name (str/trim (str weight " " (str/capital style)))
:weight weight
:style (if (str/empty? style) "normal" style)
:ttf-url (get files id)})))
(defn- parse-gfont
[font]
(let [family (get font "family")
variants (get font "variants")
files (get font "files")]
{:id (str "gfont-" (str/slug family))
:uuid (uuid/random)
:family family
:name family
:variants (into [] (comp (map (fn [variant] (parse-gfont-variant variant files)))
(filter identity))
variants)}))
(defmacro preload-gfonts
[path]
(let [data (slurp (io/resource path))
data (json/read-str data)]
`~(mapv parse-gfont (get data "items"))))