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 diff
     1.1 --- a/src/indyvon/core.clj	Mon Jun 21 00:54:45 2010 +0400
     1.2 +++ b/src/indyvon/core.clj	Mon Jun 21 01:18:50 2010 +0400
     1.3 @@ -106,6 +106,20 @@
     1.4               [(+ (s 0) offset offset)
     1.5                (+ (s 1) offset offset)]))))))
     1.6  
     1.7 +;; Define as macro to avoid unnecessary calculation of inner and outer
     1.8 +;; sizes in the first case.
     1.9 +(defmacro align-xy [inner outer align first center last]
    1.10 +  `(case ~align
    1.11 +         ~first 0
    1.12 +         ~center (/ (- ~outer ~inner) 2)
    1.13 +         ~last (- ~outer ~inner)))
    1.14 +
    1.15 +(defmacro align-x [inner outer align]
    1.16 +  `(align-xy ~inner ~outer ~align :left :center :right))
    1.17 +
    1.18 +(defmacro align-y [inner outer align]
    1.19 +  `(align-xy ~inner ~outer ~align :top :center :bottom))
    1.20 +
    1.21  (defn- re-split [re s]
    1.22    (seq (.split re s)))
    1.23  
    1.24 @@ -132,18 +146,12 @@
    1.25                   font (.getFont g)
    1.26                   font-context (:font-context c)
    1.27                   layouts (layout-text lines font font-context)
    1.28 -                 y (case v-align
    1.29 -                     :top 0
    1.30 -                     :center (/ (- h (text-height layouts)) 2)
    1.31 -                     :bottom (- h (text-height layouts)))]
    1.32 +                 y (align-y (text-height layouts) h v-align)]
    1.33               (loop [layouts layouts, y y]
    1.34                 (when-first [layout layouts]
    1.35                   (let [ascent (.getAscent layout)
    1.36                         lh (+ ascent (.getDescent layout) (.getLeading layout))
    1.37 -                       x (case h-align
    1.38 -                           :left 0
    1.39 -                           :center (/ (- w (.getAdvance layout)) 2)
    1.40 -                           :right (- w (.getAdvance layout)))]
    1.41 +                       x (align-x (.getAdvance layout) w h-align)]
    1.42                     (.draw layout g x (+ y ascent))
    1.43                     (recur (next layouts) (+ y lh)))))))
    1.44          (size [l c]
    1.45 @@ -180,14 +188,8 @@
    1.46                width (:width c)
    1.47                height (:height c)]
    1.48            (dosync
    1.49 -           (case h-align
    1.50 -                 :left nil
    1.51 -                 :center (alter x + (/ (- @last-width width) 2))
    1.52 -                 :right (alter x + (- @last-width width)))
    1.53 -           (case v-align
    1.54 -                 :top nil
    1.55 -                 :center (alter y + (/ (- @last-height height) 2))
    1.56 -                 :bottom (alter y + (- @last-height height)))
    1.57 +           (alter x + (align-x width @last-width h-align))
    1.58 +           (alter y + (align-y height @last-height v-align))
    1.59             (ref-set last-width width)
    1.60             (ref-set last-height height))
    1.61            (draw! content c g