;; ~font — web font component
;;
;; Works like ~tw: emits @font-face CSS into the cssx scope,
;; sets font-family on the parent element via spread.
;;
;; Usage:
;;   (div (~font :family "Pretext Serif" :src "/static/fonts/DejaVuSerif.ttf")
;;     "This text uses DejaVu Serif")
;;
;;   (p (~font :family "Pretext Serif")   ;; reuse already-declared font
;;     "No :src needed after first declaration")

;; Track which font families have already emitted @font-face rules
(define *font-declared* (dict))

(defcomp
  ~font
  (&key family src weight style format)
  (let
    ((fam (or family "serif"))
      (wt (or weight "normal"))
      (st (or style "normal"))
      (fmt (or format "truetype")))
    (when
      (and src (not (has-key? *font-declared* fam)))
      (dict-set! *font-declared* fam true)
      (collect!
        "cssx"
        (str
          "@font-face{font-family:'"
          fam
          "';src:url('"
          src
          "') format('"
          fmt
          "');font-weight:"
          wt
          ";font-style:"
          st
          ";}")))
    (make-spread {:style (str "font-family:'" fam "',serif;")})))