Permalink
Browse files

Added style support for text

  • Loading branch information...
2 parents fa14eb0 + 432e117 commit e12c9d21b8538eeb6827f81b5cea9af671d0ab16 @stathissideris committed Dec 11, 2012
Showing with 34 additions and 10 deletions.
  1. +2 −1 src/dali/backend.clj
  2. +8 −2 src/dali/backend/java_2d.clj
  3. +8 −0 src/dali/core.clj
  4. +12 −3 src/dali/style.clj
  5. +4 −4 src/dali/test.clj
View
@@ -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]
@@ -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
@@ -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))
@@ -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))
@@ -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)
View
@@ -1,3 +1,4 @@
+
(ns dali.core
(:use [dali.math]
[dali.utils]))
@@ -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}]
View
@@ -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 ;;;;;;
View
@@ -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
@@ -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])

0 comments on commit e12c9d2

Please sign in to comment.