Skip to content

Commit 43bce4a

Browse files
committed
refactor font
1 parent cf34052 commit 43bce4a

File tree

5 files changed

+14
-44
lines changed

5 files changed

+14
-44
lines changed

src/renderer/attribute/impl/font_size.cljs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,17 @@
33
(:require
44
[renderer.attribute.hierarchy :as attribute.hierarchy]
55
[renderer.attribute.views :as attribute.views]
6-
[renderer.utils.dom :as utils.dom]
7-
[renderer.utils.element :as utils.element]
6+
[renderer.element.impl.text :as element.impl.text]
87
[renderer.utils.length :as utils.length]))
98

109
(defmethod attribute.hierarchy/description [:default :font-size]
1110
[]
1211
"The font-size attribute refers to the size of the font from baseline to
1312
baseline when multiple lines of text are set solid in a multiline layout environment.")
1413

15-
(defn get-font-size!
16-
[{:keys [content] :as el}]
17-
(when-let [svg (utils.dom/canvas-element!)]
18-
(let [dom-el (utils.element/->dom-element el)]
19-
(.appendChild svg dom-el)
20-
(set! (.-innerHTML dom-el) (if (empty? content) "\u00a0" content))
21-
(let [computed-style (.getComputedStyle js/window dom-el nil)
22-
font-size (.getPropertyValue computed-style "font-size")]
23-
(.remove dom-el)
24-
font-size))))
25-
2614
(defmethod attribute.hierarchy/update-attr :font-size
2715
[el attribute f & more]
28-
(let [font-size (get-font-size! el)
16+
(let [font-size (:font-size (element.impl.text/get-computed-styles! el))
2917
font-size (utils.length/unit->px font-size)]
3018
(assoc-in el [:attrs attribute] (str (apply f font-size more)))))
3119

src/renderer/attribute/impl/font_style.cljs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,17 @@
33
(:require
44
[renderer.attribute.hierarchy :as attribute.hierarchy]
55
[renderer.attribute.views :as attribute.views]
6-
[renderer.utils.dom :as utils.dom]
7-
[renderer.utils.element :as utils.element]
6+
[renderer.element.impl.text :as element.impl.text]
87
[renderer.utils.length :as utils.length]))
98

109
(defmethod attribute.hierarchy/description [:default :font-style]
1110
[]
1211
"The font-size attribute refers to the size of the font from baseline to
1312
baseline when multiple lines of text are set solid in a multiline layout environment.")
1413

15-
(defn get-font-size!
16-
[{:keys [content] :as el}]
17-
(when-let [svg (utils.dom/canvas-element!)]
18-
(let [dom-el (utils.element/->dom-element el)]
19-
(.appendChild svg dom-el)
20-
(set! (.-innerHTML dom-el) (if (empty? content) "\u00a0" content))
21-
(let [computed-style (.getComputedStyle js/window dom-el nil)
22-
font-size (.getPropertyValue computed-style "font-size")]
23-
(.remove dom-el)
24-
font-size))))
25-
2614
(defmethod attribute.hierarchy/update-attr :font-style
2715
[el attribute f & more]
28-
(let [font-size (get-font-size! el)
16+
(let [font-size (:font-size (element.impl.text/get-computed-styles! el))
2917
font-size (utils.length/unit->px font-size)]
3018
(assoc-in el [:attrs attribute] (str (apply f font-size more)))))
3119

src/renderer/element/impl/renderable.cljs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,10 @@
55
[renderer.element.hierarchy :as element.hierarchy]
66
[renderer.element.subs :as-alias element.subs]
77
[renderer.element.views :as element.views]
8-
[renderer.tool.subs :as-alias tool.subs]
9-
[renderer.utils.bounds :as utils.bounds]
10-
[renderer.utils.dom :as utils.dom]
11-
[renderer.utils.element :as utils.element]))
8+
[renderer.tool.subs :as-alias tool.subs]))
129

1310
(derive ::element.hierarchy/renderable ::element.hierarchy/element)
1411

15-
(defmethod element.hierarchy/bbox ::element.hierarchy/renderable
16-
[{:keys [content] :as el}]
17-
(when-let [svg (utils.dom/canvas-element!)]
18-
(let [dom-el (utils.element/->dom-element el)]
19-
(.appendChild svg dom-el)
20-
(set! (.-innerHTML dom-el) (if (empty? content) "\u00a0" content))
21-
(let [bbox (utils.bounds/dom-el->bbox dom-el)]
22-
(.remove dom-el)
23-
bbox))))
24-
2512
(defmethod element.hierarchy/render ::element.hierarchy/renderable
2613
[el]
2714
(let [child-els @(rf/subscribe [::element.subs/filter-visible (:children el)])

src/renderer/element/impl/text.cljs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,13 @@
128128
(let [computed-style (.getComputedStyle js/window dom-el nil)
129129
font-style (.getPropertyValue computed-style "font-style")
130130
font-size (.getPropertyValue computed-style "font-size")
131-
font-weight (.getPropertyValue computed-style "font-weight")]
131+
font-weight (.getPropertyValue computed-style "font-weight")
132+
bbox (utils.bounds/dom-el->bbox dom-el)]
132133
(.remove dom-el)
133134
{:font-style font-style
134135
:font-size font-size
135-
:font-weight font-weight}))))
136+
:font-weight font-weight
137+
:bbox bbox}))))
136138

137139
(defn font-file->path-data
138140
[file content x y font-size]
@@ -193,3 +195,7 @@
193195
(-> (js/fetch (default-font-path font-style font-weight))
194196
(.then (fn [response]
195197
(font-file->path-data response content x y font-size)))))))
198+
199+
(defmethod element.hierarchy/bbox :text
200+
[el]
201+
(:bbox (get-computed-styles! el)))

src/renderer/utils/attribute.cljs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"floodColor"
108108
"floodOpacity"
109109
"fontFamily"
110+
"fontSize"
110111
"fontSizeAdjust"
111112
"fontStretch"
112113
"fontStyle"

0 commit comments

Comments
 (0)