       compact-image)
     image))
 
-(defn- hmetrics [^STBTTFontinfo font-info glyph-index]
-  (let [advance-width     (BufferUtils/createIntBuffer 1)
-        left-side-bearing (BufferUtils/createIntBuffer 1)]
+(defn-ms hmetrics [^MemoryStack ms ^STBTTFontinfo font-info glyph-index]
+  (let [advance-width     (.mallocInt ms 1)
+        left-side-bearing (.mallocInt ms 1)]
     (log-call "STBTruetype/stbtt_GetGlyphHMetrics" font-info (int glyph-index) advance-width left-side-bearing)
     (STBTruetype/stbtt_GetGlyphHMetrics font-info (int glyph-index) advance-width left-side-bearing)
     [(.get advance-width) (.get left-side-bearing)]))
 
-(defn- vmetrics [^STBTTFontinfo font-info]
-  (let [ascent   (BufferUtils/createIntBuffer 1)
-        descent  (BufferUtils/createIntBuffer 1)
-        line-gap (BufferUtils/createIntBuffer 1)]
+(defn-ms vmetrics [^MemoryStack ms ^STBTTFontinfo font-info]
+  (let [ascent   (.mallocInt ms 1)
+        descent  (.mallocInt ms 1)
+        line-gap (.mallocInt ms 1)]
     (log-call "STBTruetype/stbtt_GetFontVMetrics" font-info ascent descent line-gap)
     (STBTruetype/stbtt_GetFontVMetrics font-info ascent descent line-gap)
     [(.get ascent) (.get descent) (.get line-gap)]))
 
-(defn- glyph-bitmap-box [^STBTTFontinfo font-info scale glyph-index]
-  (let [ix0 (BufferUtils/createIntBuffer 1)
-        iy0 (BufferUtils/createIntBuffer 1)
-        ix1 (BufferUtils/createIntBuffer 1)
-        iy1 (BufferUtils/createIntBuffer 1)]
+(defn-ms glyph-bitmap-box [^MemoryStack ms ^STBTTFontinfo font-info scale glyph-index]
+  (let [ix0 (.mallocInt ms 1)
+        iy0 (.mallocInt ms 1)
+        ix1 (.mallocInt ms 1)
+        iy1 (.mallocInt ms 1)]
     (log-call "STBTruetype/stbtt_GetGlyphBitmapBox"
       font-info
       (int glyph-index)

  GlyphGraphics
  (glyph-graphics [this]
    ;; Adjust canvas to fit character atlas size
    (let [^STBTTFontinfo font-info   (make-font name-or-path)
          scale                      (STBTruetype/stbtt_ScaleForPixelHeight
                                       font-info
                                       size)
          [advance
           left-side-bearing]        (map (partial * scale)
                                       (hmetrics font-info
                                                 (STBTruetype/stbtt_FindGlyphIndex
                                                   font-info
                                                   (int \M))))

           (java.nio ByteBuffer)
           (java.nio.file Files Path)
           (org.lwjgl BufferUtils)
           (org.lwjgl.system MemoryStack)
           (org.lwjgl.stb STBTruetype STBTTFontinfo)
           (org.apache.commons.io IOUtils)))

(set! *warn-on-reflection* true)
(set! *unchecked-math* true)

(defmacro defn-ms [name [memory-stack & args] & body]
  `(defn ~name [~memory-stack ~@args]
     (.push ~memory-stack)
     (try
       ~@body
       (finally
         (.pop ~memory-stack)))))

(def calls (atom []))
;(add-watch calls :persist
;  (fn [_ _ _ calls]
;    (spit "calls.log" calls)))
    
(defn log-call [method-name & args]
  (spit "calls.log" {:method-name method-name :args args} :append true)
  (spit "calls.log" "\n" :append true)
  (swap! calls (fn [calls] (conj calls {:method-name method-name :args args}))))

(defn- byte-buffer->str [^ByteBuffer buffer]
