(use cairo data-structures srfi-13) (define s (cairo-image-surface-create-from-png "gazette-chicken.png")) (define c (cairo-create s)) (define text (string-append "\"" (string-upcase (second (argv))) "\"")) (define (text-width ctxt text) (let ((ex (make-cairo-text-extents-type))) (cairo-text-extents ctxt text ex) (cairo-text-extents-width ex))) (define (text->rows ctxt text max-width) (let* ((w (text-width ctxt text)) (f (inexact->exact (floor (* (string-length text) (/ max-width w)))))) (let loop ((s text) (r '())) (cond ((string-null? s) (reverse r)) ((< (text-width ctxt s) max-width) (loop "" (cons s r))) ((string-index s #\space (if (< f (string-length s)) f 0)) => (lambda (i) (loop (string-drop s (add1 i)) (cons (string-take s (add1 i)) r)))) (else (loop "" (cons s r))))))) (cairo-select-font-face c "Impact" CAIRO_FONT_SLANT_NORMAL CAIRO_FONT_WEIGHT_NORMAL) (cairo-set-font-size c 30) (cairo-move-to c 20 50) (cairo-set-source-rgba c 1 0 0 1) (cairo-show-text c "The chicken says:") (cairo-set-font-size c 20) (let ((rs (text->rows c text 220.0))) (when (> (length rs) 4) (error (sprintf "String too long by ~a chars." (string-length (last rs))))) (for-each (lambda (t o) (cairo-move-to c 20 (+ 100 (* o 30))) (cairo-show-text c t)) rs '(0 1 2 3))) (cairo-surface-write-to-png s "foo.png")