Create a gist now

Instantly share code, notes, and snippets.

Fitting a string into a predetermined width
;;;; scratch.lisp
(defpackage #:scratch
(:use #:cl #:vecto)
(:import-from #:zpb-ttf
(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)
(set-rgb-fill 1 1 1)
(set-font loader target-size)
(draw-centered-string (/ width 2) (/ height 2) string)
(save-png output-file))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment