changeset 87:beb89bd18839

Faster clipping calculation (fixes performance bottleneck).
author Mikhail Kryshen <mikhail@kryshen.net>
date Wed, 06 Oct 2010 18:09:37 +0400
parents 069ea63803a2
children 18abc7d66d49
files src/net/kryshen/indyvon/core.clj
diffstat 1 files changed, 30 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/core.clj	Wed Oct 06 18:08:21 2010 +0400
+++ b/src/net/kryshen/indyvon/core.clj	Wed Oct 06 18:09:37 2010 +0400
@@ -212,19 +212,37 @@
     (.concatenate tr *initial-transform*) ; component -> absolute
     tr))
 
+;; (defn- clip
+;;   "Intersect clipping area with the specified shape or bounds.
+;;    Returns new clip (Shape or nil if empty)."
+;;   ([x y w h]
+;;      (clip (Rectangle2D$Double. x y w h)))
+;;   ([shape]
+;;      (let [a1 (Area. shape)
+;;            a2 (if (instance? Area *clip*) *clip* (Area. *clip*))]
+;;        (.transform a1 (relative-transform))
+;;        (.intersect a1 a2)
+;;        (if (.isEmpty a1)
+;;          nil
+;;          a1))))
+
+;; Use faster clipping calculation provided by Graphics2D.
 (defn- clip
-  "Intersect clipping area with the specified shape or bounds.
-   Returns new clip (Shape or nil if empty)."
-  ([x y w h]
-     (clip (Rectangle2D$Double. x y w h)))
-  ([shape]
-     (let [a1 (Area. shape)
-           a2 (if (instance? Area *clip*) *clip* (Area. *clip*))]
-       (.transform a1 (relative-transform))
-       (.intersect a1 a2)
-       (if (.isEmpty a1)
-         nil
-         a1))))
+  "Intersect clipping area with the specified bounds in current
+   transform coordinates. Returns new clip in the AWT component
+   coordinates (Shape or nil if empty)."
+  [x y w h]
+  (let [^Graphics2D clip-g (.create *graphics*)]
+    (doto clip-g
+      (.setClip x y w h)
+      (.setTransform *initial-transform*)
+      (.clip *clip*))
+    (try
+      (if (.isEmpty (.getClipBounds clip-g))
+        nil
+        (.getClip clip-g))
+      (finally
+       (.dispose clip-g)))))
 
 (defn- ^Graphics2D apply-theme
   "Set graphics' color and font to match theme.