Created
June 6, 2011 00:13
-
-
Save xach/1009573 to your computer and use it in GitHub Desktop.
Fitting a string into a predetermined width
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;; 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