Mercurial > hg > indyvon
changeset 25:07ee065cbb3e
Avoid some code repetition by using macros.
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Mon, 21 Jun 2010 01:18:50 +0400 |
parents | c17e3588ede9 |
children | 1237f7555029 |
files | src/indyvon/core.clj |
diffstat | 1 files changed, 18 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/src/indyvon/core.clj Mon Jun 21 00:54:45 2010 +0400 +++ b/src/indyvon/core.clj Mon Jun 21 01:18:50 2010 +0400 @@ -106,6 +106,20 @@ [(+ (s 0) offset offset) (+ (s 1) offset offset)])))))) +;; Define as macro to avoid unnecessary calculation of inner and outer +;; sizes in the first case. +(defmacro align-xy [inner outer align first center last] + `(case ~align + ~first 0 + ~center (/ (- ~outer ~inner) 2) + ~last (- ~outer ~inner))) + +(defmacro align-x [inner outer align] + `(align-xy ~inner ~outer ~align :left :center :right)) + +(defmacro align-y [inner outer align] + `(align-xy ~inner ~outer ~align :top :center :bottom)) + (defn- re-split [re s] (seq (.split re s))) @@ -132,18 +146,12 @@ font (.getFont g) font-context (:font-context c) layouts (layout-text lines font font-context) - y (case v-align - :top 0 - :center (/ (- h (text-height layouts)) 2) - :bottom (- h (text-height layouts)))] + y (align-y (text-height layouts) h v-align)] (loop [layouts layouts, y y] (when-first [layout layouts] (let [ascent (.getAscent layout) lh (+ ascent (.getDescent layout) (.getLeading layout)) - x (case h-align - :left 0 - :center (/ (- w (.getAdvance layout)) 2) - :right (- w (.getAdvance layout)))] + x (align-x (.getAdvance layout) w h-align)] (.draw layout g x (+ y ascent)) (recur (next layouts) (+ y lh))))))) (size [l c] @@ -180,14 +188,8 @@ width (:width c) height (:height c)] (dosync - (case h-align - :left nil - :center (alter x + (/ (- @last-width width) 2)) - :right (alter x + (- @last-width width))) - (case v-align - :top nil - :center (alter y + (/ (- @last-height height) 2)) - :bottom (alter y + (- @last-height height))) + (alter x + (align-x width @last-width h-align)) + (alter y + (align-y height @last-height v-align)) (ref-set last-width width) (ref-set last-height height)) (draw! content c g