| 1 | (import scheme)
|
|---|
| 2 | (import (chicken base))
|
|---|
| 3 | (import (chicken process-context))
|
|---|
| 4 | (import cairo)
|
|---|
| 5 |
|
|---|
| 6 | (define width 256)
|
|---|
| 7 | (define height 128)
|
|---|
| 8 |
|
|---|
| 9 | (define (paint-rectangle! ctx x y w h)
|
|---|
| 10 | (cairo-new-path ctx)
|
|---|
| 11 | (cairo-rectangle ctx x y w h)
|
|---|
| 12 | (cairo-fill ctx))
|
|---|
| 13 |
|
|---|
| 14 | (define (paint-outline! ctx x y w h)
|
|---|
| 15 | (cairo-new-path ctx)
|
|---|
| 16 | (cairo-rectangle ctx x y w h)
|
|---|
| 17 | (cairo-stroke ctx))
|
|---|
| 18 |
|
|---|
| 19 | (define-record pen r g b)
|
|---|
| 20 |
|
|---|
| 21 | (define black-pen (make-pen 0 0 0))
|
|---|
| 22 | (define white-pen (make-pen 1 1 1))
|
|---|
| 23 |
|
|---|
| 24 | (define (select-pen! ctx pen)
|
|---|
| 25 | (cairo-set-source-rgb ctx (pen-r pen) (pen-g pen) (pen-b pen)))
|
|---|
| 26 |
|
|---|
| 27 | (define (paint-text! ctx x y text)
|
|---|
| 28 | (cairo-move-to ctx x y)
|
|---|
| 29 | (cairo-show-text ctx text))
|
|---|
| 30 |
|
|---|
| 31 | (define (paint-text-in-rectangle! ctx x y w h text)
|
|---|
| 32 | (select-pen! ctx black-pen)
|
|---|
| 33 | (paint-rectangle! ctx x y w h)
|
|---|
| 34 | (select-pen! ctx white-pen)
|
|---|
| 35 | (paint-text! ctx x y text))
|
|---|
| 36 |
|
|---|
| 37 | (define (paint-text-in-outline! ctx x y w h text)
|
|---|
| 38 | (select-pen! ctx black-pen)
|
|---|
| 39 | (paint-outline! ctx x y w h)
|
|---|
| 40 | (paint-text! ctx x y text))
|
|---|
| 41 |
|
|---|
| 42 | (define font-matrix (make-cairo-matrix-type))
|
|---|
| 43 |
|
|---|
| 44 | (define (setup-cairo! ctx)
|
|---|
| 45 | (select-pen! ctx white-pen)
|
|---|
| 46 | (paint-rectangle! ctx 0 0 width height)
|
|---|
| 47 |
|
|---|
| 48 | (select-pen! ctx black-pen)
|
|---|
| 49 | (cairo-set-line-width ctx 2)
|
|---|
| 50 |
|
|---|
| 51 | (cairo-select-font-face ctx
|
|---|
| 52 | "sans-serif"
|
|---|
| 53 | CAIRO_FONT_SLANT_NORMAL
|
|---|
| 54 | CAIRO_FONT_WEIGHT_NORMAL)
|
|---|
| 55 | (cairo-set-font-size ctx 16)
|
|---|
| 56 |
|
|---|
| 57 | ;; HACK: if you draw text and a rectangle at the same x/y
|
|---|
| 58 | ;; coordinate, the text is drawn above and the rectangle below, so
|
|---|
| 59 | ;; obtain the font matrix and translate it to land inside the
|
|---|
| 60 | ;; rectangle (move x by 1/8 of the font size and y by 9/8)
|
|---|
| 61 | (cairo-get-font-matrix ctx font-matrix)
|
|---|
| 62 | (cairo-matrix-translate font-matrix 0.125 1.125)
|
|---|
| 63 | (cairo-set-font-matrix ctx font-matrix))
|
|---|
| 64 |
|
|---|
| 65 | (define (paint-scene! ctx)
|
|---|
| 66 | (paint-text-in-rectangle! ctx 24 24 128 24 "Hello")
|
|---|
| 67 | (paint-text-in-outline! ctx 72 56 128 24 "world"))
|
|---|
| 68 |
|
|---|
| 69 | (define (preview-image! path)
|
|---|
| 70 | (let* ((surface (cairo-image-surface-create CAIRO_FORMAT_RGB24
|
|---|
| 71 | width
|
|---|
| 72 | height))
|
|---|
| 73 | (ctx (cairo-create surface)))
|
|---|
| 74 | (setup-cairo! ctx)
|
|---|
| 75 | (paint-scene! ctx)
|
|---|
| 76 | (cairo-surface-write-to-png surface path)))
|
|---|
| 77 |
|
|---|
| 78 | (preview-image! (car (command-line-arguments)))
|
|---|
| 79 |
|
|---|