clojureでAAを生成する。
何だかまだclojureには慣れないなー*1
read-lines*とか自分で作らなければいけないはずがないとおもうのだけと。*2
(ns tool.aa (:use [clojure.contrib.command-line :only (with-command-line)] [clojure.contrib.duck-streams :only (reader)]) (:import java.io.File java.awt.image.BufferedImage (java.awt Font Color RenderingHints GraphicsEnvironment))) (defn read-lines* [] ((fn step [] (lazy-seq (when-let [line (read-line)] (cons line (step))))))) (defn main [args] (with-command-line args "AA to png file" [[width "image width" 600] [height "image height" 300] [font-path "font-path" "/usr/share/fonts/truetype/ttf-ipamonafont/ipagp-mona.ttf"] [output "output file" "foo.png"] remaining] (let [env (GraphicsEnvironment/getLocalGraphicsEnvironment) font (Font/createFont Font/TRUETYPE_FONT (File. font-path)) width (if (string? width) (Integer/parseInt width) width) height (if (string? height) (Integer/parseInt height) height)] (. env (registerFont font)) (let [coll (read-lines*) lineLnegth (count coll) fontHeight (/ (float height) lineLnegth) image (BufferedImage. width height BufferedImage/TYPE_INT_RGB) g (. image createGraphics)] (doto g (. setRenderingHint RenderingHints/KEY_TEXT_ANTIALIASING RenderingHints/VALUE_TEXT_ANTIALIAS_LCD_VBGR) (. setColor Color/WHITE) (. fillRect 0 0 (. image getWidth) (. image getHeight))) (doto g (. setColor Color/BLACK) (. setFont (. font deriveFont fontHeight))) (doseq [[i line] (map vector (iterate inc 0) coll)] (. g drawString line 10 (int (+ (* i fontHeight) fontHeight)))) (javax.imageio.ImageIO/write image "png" (File. output)))))) (main *command-line-args*)