🐛 Fix variants naming and handling of gfonts variants.

This commit is contained in:
Andrey Antukh 2020-04-28 13:24:53 +02:00 committed by Alonso Torres
parent b9210d45f3
commit 07981487bf
4 changed files with 46 additions and 47 deletions

View file

@ -18,16 +18,16 @@
[variant] [variant]
(cond (cond
(= "regular" variant) (= "regular" variant)
{:name "regular" :width "400" :style "normal"} {:name "regular" :weight "400" :style "normal"}
(= "italic" variant) (= "italic" variant)
{:name "italic" :width "400" :style "italic"} {:name "italic" :weight "400" :style "italic"}
:else :else
(when-let [[a b c] (re-find #"^(\d+)(.*)$" variant)] (when-let [[a b c] (re-find #"^(\d+)(.*)$" variant)]
(if (str/empty? c) (if (str/empty? c)
{:name a :width b :style "normal"} {:id a :name b :weight b :style "normal"}
{:name a :width b :style c})))) {:id a :name (str b " (" c ")") :weight b :style c}))))
(defn- parse-gfont (defn- parse-gfont
[font] [font]

View file

@ -27,50 +27,50 @@
[{:id "sourcesanspro" [{:id "sourcesanspro"
:name "Source Sans Pro" :name "Source Sans Pro"
:family "sourcesanspro" :family "sourcesanspro"
:variants [{:name "100" :weight "100" :style "normal"} :variants [{:id "100" :name "100" :weight "100" :style "normal"}
{:name "100italic" :weight "100" :style "italic"} {:id "100italic" :name "100 (italic)" :weight "100" :style "italic"}
{:name "200" :weight "200" :style "normal"} {:id "200" :name "200" :weight "200" :style "normal"}
{:name "200italic" :weight "200" :style "italic"} {:id "200italic" :name "200 (italic)" :weight "200" :style "italic"}
{:name "300" :weight "300" :style "normal"} {:id "300" :name "300" :weight "300" :style "normal"}
{:name "300italic" :weight "300" :style "italic"} {:id "300italic" :name "300 (italic)" :weight "300" :style "italic"}
{:name "regular" :weight "400" :style "normal"} {:id "regular" :name "regular" :weight "400" :style "normal"}
{:name "italic" :weight "400" :style "italic"} {:id "italic" :name "italic" :weight "400" :style "italic"}
{:name "500" :weight "500" :style "normal"} {:id "500" :name "500" :weight "500" :style "normal"}
{:name "500italic" :weight "500" :style "italic"} {:id "500italic" :name "500 (italic)" :weight "500" :style "italic"}
{:name "bold" :weight "bold" :style "normal"} {:id "bold" :name "bold" :weight "bold" :style "normal"}
{:name "bolditalic" :weight "bold" :style "italic"} {:id "bolditalic" :name "bold (italic)" :weight "bold" :style "italic"}
{:name "black" :weight "900" :style "normal"} {:id "black" :name "black" :weight "900" :style "normal"}
{:name "blackitalic" :weight "900" :style "italic"}]} {:id "blackitalic" :name "black (italic)" :weight "900" :style "italic"}]}
{:id "roboto" {:id "roboto"
:family "roboto" :family "roboto"
:name "Roboto" :name "Roboto"
:variants [{:name "100" :weight "100" :style "normal"} :variants [{:id "100" :name "100" :weight "100" :style "normal"}
{:name "100italic" :weight "100" :style "italic"} {:id "100italic" :name "100 (italic)" :weight "100" :style "italic"}
{:name "200" :weight "200" :style "normal"} {:id "200" :name "200" :weight "200" :style "normal"}
{:name "200italic" :weight "200" :style "italic"} {:id "200italic" :name "200 (italic)" :weight "200" :style "italic"}
{:name "regular" :weight "400" :style "normal"} {:id "regular" :name "regular" :weight "400" :style "normal"}
{:name "italic" :weight "400" :style "italic"} {:id "italic" :name "italic" :weight "400" :style "italic"}
{:name "500" :weight "500" :style "normal"} {:id "500" :name "500" :weight "500" :style "normal"}
{:name "500italic" :weight "500" :style "italic"} {:id "500italic" :name "500 (italic)" :weight "500" :style "italic"}
{:name "bold" :weight "bold" :style "normal"} {:id "bold" :name "bold" :weight "bold" :style "normal"}
{:name "bolditalic" :weight "bold" :style "italic"} {:id "bolditalic" :name "bold (italic)" :weight "bold" :style "italic"}
{:name "black" :weight "900" :style "normal"} {:id "black" :name "black" :weight "900" :style "normal"}
{:name "blackitalic" :weight "900" :style "italic"}]} {:id "blackitalic" :name "black (italic)" :weight "900" :style "italic"}]}
{:id "robotocondensed" {:id "robotocondensed"
:family "robotocondensed" :family "robotocondensed"
:name "Roboto Condensed" :name "Roboto Condensed"
:variants [{:name "100" :weight "100" :style "normal"} :variants [{:id "100" :name "100" :weight "100" :style "normal"}
{:name "100italic" :weight "100" :style "italic"} {:id "100italic" :name "100 (italic)" :weight "100" :style "italic"}
{:name "200" :weight "200" :style "normal"} {:id "200" :name "200" :weight "200" :style "normal"}
{:name "200italic" :weight "200" :style "italic"} {:id "200italic" :name "200 (italic)" :weight "200" :style "italic"}
{:name "regular" :weight "400" :style "normal"} {:id "regular" :name "regular" :weight "400" :style "normal"}
{:name "italic" :weight "400" :style "italic"} {:id "italic" :name "italic" :weight "400" :style "italic"}
{:name "500" :weight "500" :style "normal"} {:id "500" :name "500" :weight "500" :style "normal"}
{:name "500italic" :weight "500" :style "italic"} {:id "500italic" :name "500 (italic)" :weight "500" :style "italic"}
{:name "bold" :weight "bold" :style "normal"} {:id "bold" :name "bold" :weight "bold" :style "normal"}
{:name "bolditalic" :weight "bold" :style "italic"} {:id "bolditalic" :name "bold (italic)" :weight "bold" :style "italic"}
{:name "black" :weight "900" :style "normal"} {:id "black" :name "black" :weight "900" :style "normal"}
{:name "blackitalic" :weight "900" :style "italic"}]}]) {:id "blackitalic" :name "black (italic)" :weight "900" :style "italic"}]}])
(defonce fontsdb (l/atom {})) (defonce fontsdb (l/atom {}))
(defonce fontsview (l/atom {})) (defonce fontsview (l/atom {}))
@ -126,7 +126,7 @@
[{:keys [id family variants ::on-loaded] :as font}] [{:keys [id family variants ::on-loaded] :as font}]
(js/console.log "[debug:fonts]: loading google font" id) (js/console.log "[debug:fonts]: loading google font" id)
(let [base (str "https://fonts.googleapis.com/css?family=" family) (let [base (str "https://fonts.googleapis.com/css?family=" family)
variants (str/join "," (map :name variants)) variants (str/join "," (map :id variants))
uri (str base ":" variants "&display=block") uri (str base ":" variants "&display=block")
node (create-link-node uri)] node (create-link-node uri)]
(.addEventListener node "load" (fn [event] (when (fn? on-loaded) (.addEventListener node "load" (fn [event] (when (fn? on-loaded)

View file

@ -152,7 +152,6 @@
font-size (obj/get data "fontSize") font-size (obj/get data "fontSize")
fill (obj/get data "fill") fill (obj/get data "fill")
opacity (obj/get data "opacity") opacity (obj/get data "opacity")
fontsdb (mf/deref fonts/fontsdb) fontsdb (mf/deref fonts/fontsdb)
base #js {:textDecoration text-decoration base #js {:textDecoration text-decoration
@ -174,7 +173,7 @@
(fonts/ensure-loaded! font-id) (fonts/ensure-loaded! font-id)
(let [font-family (or (:family font) (let [font-family (or (:family font)
(obj/get data "fontFamily")) (obj/get data "fontFamily"))
font-variant (d/seek #(= font-variant-id (:name %)) font-variant (d/seek #(= font-variant-id (:id %))
(:variants font)) (:variants font))
font-style (or (:style font-variant) font-style (or (:style font-variant)
(obj/get data "fontStyle")) (obj/get data "fontStyle"))

View file

@ -76,7 +76,7 @@
(fn [event] (fn [event]
(let [id (-> (dom/get-target event) (let [id (-> (dom/get-target event)
(dom/get-value)) (dom/get-value))
variant (d/seek #(= id (:name %)) (:variants font))] variant (d/seek #(= id (:id %)) (:variants font))]
(dwt/set-font! editor (:id font) (:family font)) (dwt/set-font! editor (:id font) (:family font))
(dwt/set-font-variant! editor id (:weight variant) (:style variant)))) (dwt/set-font-variant! editor id (:weight variant) (:style variant))))
] ]
@ -112,7 +112,7 @@
[:select.input-select {:value font-var [:select.input-select {:value font-var
:on-change on-font-variant-change} :on-change on-font-variant-change}
(for [variant (:variants font)] (for [variant (:variants font)]
[:option {:value (:name variant) [:option {:value (:id variant)
:key (pr-str variant)} :key (pr-str variant)}
(:name variant)])]]])) (:name variant)])]]]))