 (defrecord TTFFont [name-or-path size transparent]
   GlyphGraphics
   (glyph-graphics [this]
-    #_{:post [(glyph-graphics? %)]}
     ;; 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 (int \M)))
+           left-side-bearing]        (map (partial * scale)
+                                       (hmetrics font-info
+                                                 (STBTruetype/stbtt_FindGlyphIndex
+                                                   font-info
+                                                   (int \M))))
           characters                 (displayable-characters font-info)
           char-width                 (int (Math/ceil advance))
           char-height                size
-          ascent                     (BufferUtils/createIntBuffer 1)
-          descent                    (BufferUtils/createIntBuffer 1)
-          line-gap                   (BufferUtils/createIntBuffer 1)
+          [ascent
+           descent
+           line-gap]                 (vmetrics font-info)
           _ (log/info "characters per line" (characters-per-line char-width (count characters)))
           cwidth     2048
           cheight    (zutil/next-pow-2 (* char-height (int (Math/ceil (/ (count characters)

          height     cheight ;(max cwidth cheight)
          antialias  true
          char-idxs   (character-idxs char-width characters)]
      (STBTruetype/stbtt_GetFontVMetrics font-info ascent descent line-gap)
      (log/info "characters" (count characters) "cwidth" cwidth "cheight" cheight)
      (log/info "glyph image width" width "height" height)
      (log/info "char-idxs" char-idxs)
      ;(log/info "characters" (vec characters))
      ;(log/info "character-idxs" (vec (character-idxs characters)))
      (let [texture-image (zimg/image width height)
            ascent        (.get ascent)]
        ;; Loop through each character, drawing it
        (doseq [[c col row]  (reverse char-idxs)]
          (try
            (let [x  (* col char-width)
                  y  (* row char-height)
                  chr-img (char-image font-info char-width char-height ascent scale c)
                    #_#_[x0 y0 x1 y1] (codepoint-bitmap-box font-info scale (int c))
                    #_#_cy (int (+ y baseline y0))]
              (log/info "thread" (-> (Thread/currentThread) .getName))
              (log/info "drawing" c "@" x y)
              (zimg/draw-image texture-image
                             chr-img
                             x

    [(.get ix0) (.get iy0) (.get ix1) (.get iy1)]))


(defn- char-image [font-info char-width char-height ascent scale codepoint glyph-index]
  (let [;scale 0.015625
        [x0 y0 x1 y1]         (glyph-bitmap-box font-info scale glyph-index)
        baseline              (int (* ascent scale))
        [_ left-side-bearing] (map (partial * scale) (hmetrics font-info glyph-index))
        y                     (+ baseline y0)
        img                   (zimg/image char-width char-height 1)
        chr-img               (zimg/image char-width char-height 1)]
    ;(log/info "char-width" (char codepoint) "(" (int codepoint) ")" char-width char-height
    ;        "ascent" ascent "scale" scale "baseline" baseline
    ;        "left-side-bearing" left-side-bearing "y" y)
    ;(log/info "x0" x0 "y0" y0 "x1" x1 "y1" y1)
    ;; draw greyscale font
    (log-call "STBTruetype/stbtt_MakeGlyphBitmapSubpixel"
      font-info
      (get img :byte-buffer)
      char-width 
