Skip to content

Instantly share code, notes, and snippets.

@xach
Created June 6, 2011 00:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xach/1009573 to your computer and use it in GitHub Desktop.
Save xach/1009573 to your computer and use it in GitHub Desktop.
Fitting a string into a predetermined width
;;;; 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))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment