render: font: Add font-line-width procedure.
authorDavid Thompson <dthompson2@worcester.edu>
Sun, 22 Apr 2018 00:07:38 +0000 (20:07 -0400)
committerDavid Thompson <dthompson2@worcester.edu>
Sun, 22 Apr 2018 00:07:38 +0000 (20:07 -0400)
* chickadee/render/font.scm (font-line-width): New procedure.

chickadee/render/font.scm

index 1a01ad4..f63450b 100644 (file)
@@ -42,6 +42,7 @@
             font?
             font-face
             font-line-height
+            font-line-width
             font-bold?
             font-italic?
             draw-text*
 
 (set-record-type-printer! <font> display-font)
 
+(define (font-line-width font text)
+  "Return the width of TEXT when rendered with FONT."
+  (let loop ((width 0.0)
+             (i 0))
+    (if (< i (string-length text))
+        (let ((char (or (font-ref font (string-ref text i))
+                        (font-ref font #\?))))
+          (loop (+ width (font-char-advance char))
+                (+ i 1)))
+        width)))
+
 (define* (load-tile-font file tile-width tile-height characters #:key
                          (face "untitled") (margin 0) (spacing 0))
   "Load the font named FACE from FILE, a bitmap image containing the