Skip to content

Commit

Permalink
Added style support for text
Browse files Browse the repository at this point in the history
  • Loading branch information
stathissideris committed Dec 11, 2012
2 parents fa14eb0 + 432e117 commit e12c9d2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/dali/backend.clj
Expand Up @@ -37,7 +37,8 @@
(render-path [this shape])
(render-image [this shape])

(render-group [this shape]))
(render-group [this shape])
(text-bounds [this shape]))


(defmacro delegate-op-to-backend [op & shape-types]
Expand Down
10 changes: 8 additions & 2 deletions src/dali/backend/java_2d.clj
Expand Up @@ -194,7 +194,7 @@
size (.getSize *DEFAULT-FONT*)}}]
(Font. family java.awt.Font/PLAIN size))

(defn text-bounds
(defn text-bounds-java-2d
[backend {content :content {position :position} :geometry :as text}]
(let [font (font->java-font (get-in text [:style :font]))
rect (.getStringBounds
Expand All @@ -218,10 +218,12 @@
(let [st (if (has-stroke? shape) (:stroke shape) DEFAULT-STROKE)]
(isolate-style backend
(set-stroke backend (eval-dynamic-style
backend
shape
(get-in shape [:style :stroke])))
(if (has-transform? shape)
(with-transform backend (eval-dynamic-style
backend
shape
(:transform shape))
(draw backend shape))
Expand All @@ -231,10 +233,12 @@
(when (has-fill? shape)
(isolate-style backend
(set-fill backend (eval-dynamic-style
backend
shape
(get-in shape [:style :fill])))
(if (has-transform? shape)
(with-transform backend (eval-dynamic-style
backend
shape
(:transform shape))
(fill backend shape))
Expand Down Expand Up @@ -336,7 +340,9 @@
(assoc shape
:style (deep-merge (:style group)
(:style shape)))] ;;shape takes precedence
(render this merged))))))
(render this merged)))))
(text-bounds [this shape]
(text-bounds-java-2d this shape)))

(defn buffered-image-type [type]
(if (keyword? type)
Expand Down
8 changes: 8 additions & 0 deletions src/dali/core.clj
@@ -1,3 +1,4 @@

(ns dali.core
(:use [dali.math]
[dali.utils]))
Expand Down Expand Up @@ -156,6 +157,13 @@
{:type :group
:content content}))

(defmacro dynamic [& body]
{:type :dynamic-value
:code `'~body})

(defn dynamic-value? [x]
(if (and (map? x)) (= :dynamic-value (:type x))))


(defn rectangle->polygon
[{{[px py] :position [w h] :dimensions} :geometry :as shape}]
Expand Down
15 changes: 12 additions & 3 deletions src/dali/style.clj
Expand Up @@ -135,9 +135,18 @@
:stops stops
:cycle-method cycle-method}))

(defn eval-dynamic-style [shape style]
(postwalk (fn eval-dynamic-style-fn [x]
(if (function? x) (x shape) x)) style))
(declare #^{:dynamic true} backend)
(declare #^{:dynamic true} this)

(defn eval-dynamic-style [the-backend shape style]
(binding [*ns* (the-ns 'dali.style)
backend the-backend
this shape]
(postwalk (fn eval-dynamic-style-fn [x]
(if (dynamic-value? x)
(let [code (:code x)]
(eval `(do ~@code)))
x)) style)))

;;;;;; fills ;;;;;;

Expand Down
8 changes: 4 additions & 4 deletions src/dali/test.clj
Expand Up @@ -45,9 +45,9 @@

(doto backend
(render-text (text {:fill (color 150 0 150)
:transform [:translate #(minus (center %))
:transform [:translate (dynamic (minus (center this)))
:rotate 30
:translate #(center %)]}
:translate (dynamic (center this))]}
[185 25] "Testing the dali library"))
(render (arrow
{:stroke {:width 2
Expand Down Expand Up @@ -75,11 +75,11 @@
(rectangle {:stroke {:width 8
:color (color 0 130 0)
:join :miter}
:transform [:translate #(minus (center %))
:transform [:translate (dynamic (minus (center this)))
:skew [0.2 0.2]
:scale 0.8
:rotate 30
:translate center]}
:translate (dynamic (center this))]}
[330 170] [100 70]))

(draw (rotate-around (rectangle [160 100] [60 60])
Expand Down

0 comments on commit e12c9d2

Please sign in to comment.