Mercurial > hg > indyvon
changeset 105:24e98602b37e
Use Guava MapMaker to cache text layouts.
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Tue, 24 May 2011 18:43:49 +0400 |
parents | 9b81174f0511 |
children | f42e2b9e1ad9 |
files | src/net/kryshen/indyvon/layers.clj |
diffstat | 1 files changed, 16 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/layers.clj Thu May 19 17:01:46 2011 +0400 +++ b/src/net/kryshen/indyvon/layers.clj Tue May 24 18:43:49 2011 +0400 @@ -27,7 +27,10 @@ (java.awt Font Cursor Image Toolkit Point) java.awt.image.ImageObserver (java.awt.geom AffineTransform Point2D$Double) - (java.awt.font FontRenderContext TextLayout))) + (java.awt.font FontRenderContext TextLayout) + java.util.concurrent.TimeUnit + com.google.common.collect.MapMaker + com.google.common.base.Function)) ;; Define as macro to avoid unnecessary calculation of inner and outer ;; sizes in the first case. @@ -172,24 +175,23 @@ (defn- re-split [^java.util.regex.Pattern re s] (seq (.split re s))) -(def ^:private text-layout-cache (atom {})) +(def ^{:private true} text-layout-cache + (-> (MapMaker.) + (.softValues) + (.expireAfterAccess (long 1) TimeUnit/SECONDS) + (.makeComputingMap + (reify Function + (apply [_ k] + (TextLayout. ^String (k 0) + ^Font (k 1) + ^FontRenderContext (k 2))))))) -(defn- get-text-layout - [^String line ^Font font ^FontRenderContext font-context] - (let [key [line font font-context]] - (or (if-let [^SoftReference softref (@text-layout-cache key)] - (.get softref) - (do (swap! text-layout-cache dissoc key) - false)) - (let [layout (TextLayout. line font font-context)] - ;;(println "text-layout-cache miss" line) - (swap! text-layout-cache assoc key (SoftReference. layout)) - layout)))) +(defn- get-text-layout [line font font-context] + (get text-layout-cache [line font font-context])) (defn- layout-text [lines ^Font font ^FontRenderContext font-context] (map #(get-text-layout % font font-context) lines)) - ;;(map #(TextLayout. ^String % font font-context) lines)) (defn- text-width [layouts] (reduce (fn [w ^TextLayout tl] (max w (.getAdvance tl))) 0 layouts))