Skip to content

Commit

Permalink
Realized (font) didn't allow styles to be combined. Added.
Browse files Browse the repository at this point in the history
  • Loading branch information
daveray committed Oct 21, 2011
1 parent 135b147 commit 55afbcc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/seesaw/font.clj
Expand Up @@ -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))

Expand All @@ -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)]
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/seesaw/test/font.clj
Expand Up @@ -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)))))
Expand Down

0 comments on commit 55afbcc

Please sign in to comment.