regen_calt replaces calt code in .glyphs file
This commit is contained in:
parent
c93969873d
commit
3c37a4c5f2
File diff suppressed because one or more lines are too long
|
@ -1,105 +0,0 @@
|
|||
(ns gen-calt
|
||||
(:require
|
||||
[clojure.string :as str]
|
||||
[parse-glyphs :as parse]))
|
||||
|
||||
(def ignores
|
||||
{ ["slash" "asterisk"]
|
||||
(str
|
||||
" ignore sub slash' asterisk slash;\n"
|
||||
" ignore sub asterisk slash' asterisk;\n")
|
||||
|
||||
["asterisk" "slash"]
|
||||
(str
|
||||
" ignore sub slash asterisk' slash;\n"
|
||||
" ignore sub asterisk' slash asterisk;\n")
|
||||
|
||||
["asterisk" "asterisk"]
|
||||
(str
|
||||
" ignore sub slash asterisk' asterisk;\n"
|
||||
" ignore sub asterisk' asterisk slash;\n")
|
||||
|
||||
["asterisk" "asterisk" "asterisk"]
|
||||
(str
|
||||
" ignore sub slash asterisk' asterisk asterisk;\n"
|
||||
" ignore sub asterisk' asterisk asterisk slash;\n")
|
||||
|
||||
["question" "equal"]
|
||||
" ignore sub parenleft question' equal;\n"
|
||||
|
||||
["less" "equal"]
|
||||
" ignore sub parenleft question less' equal;\n"
|
||||
|
||||
["question" "colon"]
|
||||
" ignore sub parenleft question' colon;\n"
|
||||
|
||||
["less" "bar" "bar"]
|
||||
" ignore sub less' bar bar greater;\n"
|
||||
|
||||
["bar" "bar" "greater"]
|
||||
" ignore sub less bar' bar greater;\n"
|
||||
})
|
||||
|
||||
(defn liga->rule
|
||||
"[f f i] => { [LIG LIG i] f_f_i.liga
|
||||
[LIG f i] LIG
|
||||
[ f f i] LIG }"
|
||||
[liga]
|
||||
(case (count liga)
|
||||
2 (let [[a b] liga]
|
||||
(str/replace
|
||||
(str " lookup 1_2 {\n"
|
||||
" ignore sub 1 1' 2;\n"
|
||||
" ignore sub 1' 2 2;\n"
|
||||
(get ignores liga)
|
||||
" sub LIG 2' by 1_2.liga;\n"
|
||||
" sub 1' 2 by LIG;\n"
|
||||
" } 1_2;")
|
||||
#"\d" {"1" a "2" b}))
|
||||
3 (let [[a b c] liga]
|
||||
(str/replace
|
||||
(str " lookup 1_2_3 {\n"
|
||||
" ignore sub 1 1' 2 3;\n"
|
||||
" ignore sub 1' 2 3 3;\n"
|
||||
(get ignores liga)
|
||||
" sub LIG LIG 3' by 1_2_3.liga;\n"
|
||||
" sub LIG 2' 3 by LIG;\n"
|
||||
" sub 1' 2 3 by LIG;\n"
|
||||
" } 1_2_3;")
|
||||
#"\d" {"1" a "2" b "3" c}))
|
||||
4 (let [[a b c d] liga]
|
||||
(str/replace
|
||||
(str " lookup 1_2_3_4 {\n"
|
||||
" ignore sub 1 1' 2 3 4;\n"
|
||||
" ignore sub 1' 2 3 4 4;\n"
|
||||
(get ignores liga)
|
||||
" sub LIG LIG LIG 4' by 1_2_3_4.liga;\n"
|
||||
" sub LIG LIG 3' 4 by LIG;\n"
|
||||
" sub LIG 2' 3 4 by LIG;\n"
|
||||
" sub 1' 2 3 4 by LIG;\n"
|
||||
" } 1_2_3_4;")
|
||||
#"\d" {"1" a "2" b "3" c "4" d}))))
|
||||
|
||||
(defn -main [& args]
|
||||
(let [file (or (first args) "FiraCode.glyphs")
|
||||
_ (println "Looking for ligatures in" file "...\n")
|
||||
font (parse/parse (slurp file))
|
||||
ligas (for [g (:glyphs font)
|
||||
:let [name (:glyphname g)]
|
||||
:when (str/ends-with? name ".liga")
|
||||
:let [[_ liga] (re-matches #"([a-z_]+)\.liga" name)]]
|
||||
(str/split liga #"_"))] ;; [ ["dash" "greater" "greater"] ... ]
|
||||
|
||||
(println "### start of generated calt\n")
|
||||
(println (->> ligas (sort-by count) (reverse) (map liga->rule) (str/join "\n\n")))
|
||||
(println "\n### end of generated calt\n")
|
||||
|
||||
(println "Total ligatures count:" (count ligas))
|
||||
(println " " (->> ligas
|
||||
(group-by count)
|
||||
(sort-by first)
|
||||
(map (fn [[k v]] (str (count v) (case k 2 " pairs", 3 " triples", 4 " quadruples"))))
|
||||
(str/join ", ")))
|
||||
(println)))
|
||||
|
||||
;; (-main)
|
|
@ -1,7 +1,9 @@
|
|||
(ns parse-glyphs
|
||||
(ns glyphs
|
||||
(:require
|
||||
[clojure.java.io :as io]
|
||||
[fipp.edn :as fipp]))
|
||||
[clojure.string :as str]
|
||||
[fipp.edn :as fipp]
|
||||
[flatland.ordered.map :refer [ordered-map]]))
|
||||
|
||||
(def ^:dynamic *str)
|
||||
(def ^:dynamic *pos)
|
||||
|
@ -23,13 +25,17 @@
|
|||
(skip-ws!)
|
||||
(when (= \" (current-char))
|
||||
(let [sb (StringBuilder.)]
|
||||
(loop []
|
||||
(advance!)
|
||||
(let [ch (current-char)]
|
||||
(cond
|
||||
(= ch \\) (do (.append sb \\) (advance!) (.append sb (current-char)) (recur))
|
||||
(= ch \") (do (advance!) (str sb))
|
||||
:else (do (.append sb ch) (recur))))))))
|
||||
(->
|
||||
(loop []
|
||||
(advance!)
|
||||
(let [ch (current-char)]
|
||||
(cond
|
||||
(= ch \\) (do (.append sb \\) (advance!) (.append sb (current-char)) (recur))
|
||||
(= ch \") (do (advance!) (str sb))
|
||||
:else (do (.append sb ch) (recur)))))
|
||||
(str/replace "\\012" "\n")
|
||||
(str/replace "\\\"" "\"")
|
||||
(str/replace "\\\\" "\\")))))
|
||||
|
||||
(defn parse-string! []
|
||||
(skip-ws!)
|
||||
|
@ -41,8 +47,8 @@
|
|||
:else (do (.append sb ch) (advance!) (recur)))))
|
||||
(let [res (str sb)]
|
||||
(cond
|
||||
(re-matches #"[0-9]+" res) (Integer/parseInt res)
|
||||
(re-matches #"[0-9]+\.[0-9]+" res) (Double/parseDouble res)
|
||||
(re-matches #"-?[1-9][0-9]*" res) (Integer/parseInt res)
|
||||
(re-matches #"-?[0-9]+\.[0-9]+" res) (Double/parseDouble res)
|
||||
(re-matches #"[a-zA-Z][a-zA-Z\.0-9]*" res) (keyword res)
|
||||
:else res))))
|
||||
|
||||
|
@ -53,7 +59,7 @@
|
|||
(skip-ws!)
|
||||
(when (= \{ (current-char))
|
||||
(advance!)
|
||||
(loop [m {}]
|
||||
(loop [m (ordered-map)]
|
||||
(skip-ws!)
|
||||
(if (= \} (current-char))
|
||||
(do (advance!) m)
|
||||
|
@ -92,6 +98,24 @@
|
|||
*pos (atom 0)]
|
||||
(parse-anything!)))
|
||||
|
||||
(def escapes {"\n" "\\012"
|
||||
"\"" "\\\""
|
||||
"\\" "\\\\"})
|
||||
|
||||
(def escape-re #"[\n\"\\]")
|
||||
|
||||
(defn serialize [form]
|
||||
(cond
|
||||
(string? form) (if (re-matches #"[a-zA-Z0-9._/]+" form)
|
||||
form
|
||||
(str \" (str/replace form escape-re escapes) \"))
|
||||
(keyword? form) (name form)
|
||||
(number? form) (str form)
|
||||
(sequential? form) (str "(\n" (str/join ",\n" (map serialize form)) "\n)")
|
||||
(map? form) (str "{\n" (str/join "\n" (for [[k v] form] (str (serialize k) " = " (serialize v) ";"))) "\n}")))
|
||||
|
||||
; (-> (slurp "FiraCode.glyphs") parse serialize (->> (spit "FiraCode_saved.glyphs")))
|
||||
|
||||
(defn -main [& args]
|
||||
(let [font (-> (slurp "FiraCode.glyphs") parse)]
|
||||
(with-open [os (io/writer "clojure/FiraCode.edn")]
|
||||
|
@ -124,4 +148,6 @@
|
|||
(count glyphs))))))
|
||||
|
||||
;; (-main)
|
||||
;; (save-not600)
|
||||
;; (save-not600)
|
||||
;; (-> (slurp "FiraCode.glyphs") parse keys)
|
||||
;;
|
|
@ -0,0 +1,118 @@
|
|||
(ns regen-calt
|
||||
(:require
|
||||
[clojure.string :as str]
|
||||
[glyphs :as glyphs]))
|
||||
|
||||
(def ignores
|
||||
{ ["slash" "asterisk"]
|
||||
(str
|
||||
" ignore sub slash' asterisk slash;\n"
|
||||
" ignore sub asterisk slash' asterisk;\n")
|
||||
|
||||
["asterisk" "slash"]
|
||||
(str
|
||||
" ignore sub slash asterisk' slash;\n"
|
||||
" ignore sub asterisk' slash asterisk;\n")
|
||||
|
||||
["asterisk" "asterisk"]
|
||||
(str
|
||||
" ignore sub slash asterisk' asterisk;\n"
|
||||
" ignore sub asterisk' asterisk slash;\n")
|
||||
|
||||
["asterisk" "asterisk" "asterisk"]
|
||||
(str
|
||||
" ignore sub slash asterisk' asterisk asterisk;\n"
|
||||
" ignore sub asterisk' asterisk asterisk slash;\n")
|
||||
|
||||
["question" "equal"]
|
||||
" ignore sub parenleft question' equal;\n"
|
||||
|
||||
["less" "equal"]
|
||||
" ignore sub parenleft question less' equal;\n"
|
||||
|
||||
["question" "colon"]
|
||||
" ignore sub parenleft question' colon;\n"
|
||||
|
||||
["less" "bar" "bar"]
|
||||
" ignore sub less' bar bar greater;\n"
|
||||
|
||||
["bar" "bar" "greater"]
|
||||
" ignore sub less bar' bar greater;\n"
|
||||
})
|
||||
|
||||
(defn liga->rule
|
||||
"[f f i] => { [LIG LIG i] f_f_i.liga
|
||||
[LIG f i] LIG
|
||||
[ f f i] LIG }"
|
||||
[liga]
|
||||
(case (count liga)
|
||||
2 (let [[a b] liga]
|
||||
(str/replace
|
||||
(str "lookup 1_2 {\n"
|
||||
" ignore sub 1 1' 2;\n"
|
||||
" ignore sub 1' 2 2;\n"
|
||||
(get ignores liga)
|
||||
" sub LIG 2' by 1_2.liga;\n"
|
||||
" sub 1' 2 by LIG;\n"
|
||||
"} 1_2;")
|
||||
#"\d" {"1" a "2" b}))
|
||||
3 (let [[a b c] liga]
|
||||
(str/replace
|
||||
(str "lookup 1_2_3 {\n"
|
||||
" ignore sub 1 1' 2 3;\n"
|
||||
" ignore sub 1' 2 3 3;\n"
|
||||
(get ignores liga)
|
||||
" sub LIG LIG 3' by 1_2_3.liga;\n"
|
||||
" sub LIG 2' 3 by LIG;\n"
|
||||
" sub 1' 2 3 by LIG;\n"
|
||||
"} 1_2_3;")
|
||||
#"\d" {"1" a "2" b "3" c}))
|
||||
4 (let [[a b c d] liga]
|
||||
(str/replace
|
||||
(str "lookup 1_2_3_4 {\n"
|
||||
" ignore sub 1 1' 2 3 4;\n"
|
||||
" ignore sub 1' 2 3 4 4;\n"
|
||||
(get ignores liga)
|
||||
" sub LIG LIG LIG 4' by 1_2_3_4.liga;\n"
|
||||
" sub LIG LIG 3' 4 by LIG;\n"
|
||||
" sub LIG 2' 3 4 by LIG;\n"
|
||||
" sub 1' 2 3 4 by LIG;\n"
|
||||
"} 1_2_3_4;")
|
||||
#"\d" {"1" a "2" b "3" c "4" d}))))
|
||||
|
||||
(defn index-of [pred xs]
|
||||
(reduce (fn [i x] (if (pred x) (reduced i) (inc i))) 0 xs))
|
||||
|
||||
(defn replace-calt [font calt]
|
||||
(let [features (:features font)
|
||||
idx (index-of #(= "calt" (:name %)) features)
|
||||
code (get-in features [idx :code])
|
||||
code' (str/replace code
|
||||
#"### start of generated calt\n[^#]+\n### end of generated calt\n"
|
||||
(str "### start of generated calt\n" calt "\n### end of generated calt\n"))]
|
||||
(assoc-in font [:features idx :code] code')))
|
||||
|
||||
(defn -main [& args]
|
||||
(let [file (or (first args) "FiraCode.glyphs")
|
||||
_ (println "Parsing" file "...")
|
||||
font (glyphs/parse (slurp file))
|
||||
ligas (for [g (:glyphs font)
|
||||
:let [name (:glyphname g)]
|
||||
:when (str/ends-with? name ".liga")
|
||||
:let [[_ liga] (re-matches #"([a-z_]+)\.liga" name)]]
|
||||
(str/split liga #"_")) ;; [ ["dash" "greater" "greater"] ... ]
|
||||
calt (->> ligas (sort-by count) (reverse) (map liga->rule) (str/join "\n\n"))
|
||||
font' (replace-calt font calt)]
|
||||
|
||||
(println "Saving" file "...")
|
||||
(spit file (glyphs/serialize font'))
|
||||
|
||||
(println "Total ligatures count:" (count ligas))
|
||||
(println " " (->> ligas
|
||||
(group-by count)
|
||||
(sort-by first)
|
||||
(map (fn [[k v]] (str (count v) (case k 2 " pairs", 3 " triples", 4 " quadruples"))))
|
||||
(str/join ", ")))
|
||||
(println)))
|
||||
|
||||
;; (-main)
|
Loading…
Reference in New Issue