public
Last active

Fitting a string into a predetermined width

  • Download Gist
scratch.lisp
Common Lisp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
;;;; scratch.lisp
 
(defpackage #:scratch
(:use #:cl #:vecto)
(:import-from #:zpb-ttf
#:xmin
#:xmax))
 
(in-package #:scratch)
 
(defun target-size (string loader target-width)
(let* ((bbox (string-bounding-box string 100 loader))
(width (- (xmax bbox) (xmin bbox))))
(* 100 (/ target-width width))))
 
(defun example (&key (width 640) (height 480)
(font-file #p "~/times.ttf")
output-file string)
(with-canvas (:width width :height height)
(let* ((loader (get-font font-file))
(target-size (target-size string loader (* width 0.75))))
(set-rgb-fill 0 0 0)
(clear-canvas)
(set-rgb-fill 1 1 1)
(set-font loader target-size)
(draw-centered-string (/ width 2) (/ height 2) string)
(save-png output-file))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.