Permalink
Browse files

Realized (font) didn't allow styles to be combined. Added.

  • Loading branch information...
1 parent 135b147 commit 55afbcc3f53aa229e567b55302c63d0b66ed51bf @daveray committed Oct 21, 2011
Showing with 22 additions and 3 deletions.
  1. +19 −3 src/seesaw/font.clj
  2. +3 −0 test/seesaw/test/font.clj
View
@@ -16,6 +16,10 @@
(:import [java.awt Font]))
(def ^{:private true} style-table (constant-map Font :bold :plain :italic))
+(defn- get-style-mask [v]
+ (if (keyword? v)
+ (get-style-mask [v])
+ (reduce bit-or 0 (map style-table v))))
(def ^{:private true} name-table (constant-map Font :monospaced :serif :sans-serif))
@@ -31,12 +35,24 @@
:name The name of the font. Besides string values, also possible are
any of :monospaced, :serif, :sans-serif.
- :style The style. One of :bold, :plain, :italic. Default: :plain.
+ :style The style. One of :bold, :plain, :italic, or a set of those values
+ to combine them. Default: :plain.
:size The size of the font. Default: 12.
:from A Font from which to derive the new Font.
Returns a java.awt.Font instance.
- "
+
+ Examples:
+
+ ; Create a font from a font-spec (see JavaDocs)
+ (font \"ARIAL-ITALIC-20\")
+
+ ; Create a 12 pt bold and italic monospace
+ (font :style #{:bold :italic} :name :monospaced)
+
+ See:
+ http://download.oracle.com/javase/6/docs/api/java/awt/Font.html
+ "
[& args]
(if (= 1 (count args))
(let [v (first args)]
@@ -45,7 +61,7 @@
(Font/decode (str v))))
(let [{:keys [style size from] :as opts} args
font-name (:name opts)
- font-style (get style-table (or style :plain))
+ font-style (get-style-mask (or style :plain))
font-size (or size 12)
^Font from (to-font from)]
(if from
@@ -23,6 +23,9 @@
(it "can create a bold font"
(let [f (font :style :bold )]
(expect (= Font/BOLD (.getStyle f)))))
+ (it "can create a bold & italic font"
+ (let [f (font :style #{:bold :italic} )]
+ (expect (= (bit-or Font/BOLD Font/ITALIC) (.getStyle f)))))
(it "can create a plain font"
(let [f (font)]
(expect (= Font/PLAIN (.getStyle f)))))

0 comments on commit 55afbcc

Please sign in to comment.