Skip to content

Instantly share code, notes, and snippets.

@shlomiv
Created February 18, 2015 11:44
Show Gist options
  • Save shlomiv/15b4e6758a97b74c3e50 to your computer and use it in GitHub Desktop.
Save shlomiv/15b4e6758a97b74c3e50 to your computer and use it in GitHub Desktop.
example writing antialiased text and an image on top of an existing image
(ns banner-maker.core
(:require [clojure.java.io :refer [as-url input-stream as-file file resource]])
(:import [java.awt Font RenderingHints])
(:gen-class))
(defn register-font [font]
(let [f (Font/createFont Font/TRUETYPE_FONT (input-stream font))]
(.registerFont (java.awt.GraphicsEnvironment/getLocalGraphicsEnvironment) f)
f))
(defn get-string-width [g font text]
(.stringWidth (.getFontMetrics g font) text))
(defn add-location[text save-as]
(let [f (.deriveFont (register-font (resource "myriad-web-pro.ttf")) (float 37))
img (javax.imageio.ImageIO/read (resource "banner-skel.png"))
loc (javax.imageio.ImageIO/read (resource "location.png"))
g (.getGraphics img)
w (.getWidth img)
h (.getHeight img)
l (get-string-width g f text)]
(doto g
(.setFont f)
(.setRenderingHint RenderingHints/KEY_TEXT_ANTIALIASING RenderingHints/VALUE_TEXT_ANTIALIAS_ON)
(.setRenderingHint RenderingHints/KEY_ANTIALIASING RenderingHints/VALUE_ANTIALIAS_ON)
(.drawString text (- w 43 l) (+ (/ h 2) 12))
(.drawImage loc (- w 85 l) (- (/ h 2) 18) nil)
(.dispose))
(javax.imageio.ImageIO/write img "png" (file save-as))))
(defn -main
([] (println "Usage: banner-maker \"location\" output-file-name"))
([text save-as] (add-location text save-as)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment