3 (in-package #:vectometry
)
5 (defparameter *horizontal-alignments
*
6 #(:before
:left
:center
:right
:after
))
8 (defparameter *vertical-alignments
*
9 #(:below
:bottom
:middle
:top
:atop
))
11 (defun rotate-alignment (horizontal vertical rotation
)
12 (let ((h (position horizontal
*horizontal-alignments
*))
13 (v (position vertical
*vertical-alignments
*)))
15 (- (length *horizontal-alignments
*) i
1)))
17 (error "Invalid horizontal alignment ~S" horizontal
))
19 (error "Invalid vertical alignment ~S" vertical
))
23 (psetf h
(invert v
) v h
))
25 (psetf v
(invert h
) h v
))
27 (psetf h
(invert h
) v
(invert v
))))
28 (values (aref *horizontal-alignments
* h
)
29 (aref *vertical-alignments
* v
)))))
31 (defun draw-box-text (box text
&key size loader
32 (horizontal :left
) (vertical :bottom
)
34 (let ((stringbox (string-bounding-box text size loader
))
37 (center (centerpoint box
)))
38 (flet ((handle-rotation (point degrees h v
)
41 (rotate-degrees degrees
)
42 (let ((box* (if (= degrees
180)
45 (setf box
* (displace box
* (neg (minpoint box
))))
46 (return-from draw-box-text
47 (draw-box-text box
* text
:size size
:loader loader
48 :horizontal h
:vertical v
53 (multiple-value-bind (h v
)
54 (rotate-alignment horizontal vertical rotation
)
55 (handle-rotation (bottom-right box
) 90 h v
)))
57 (multiple-value-bind (h v
)
58 (rotate-alignment horizontal vertical rotation
)
59 (handle-rotation (top-left box
) -
90 h v
)))
61 (multiple-value-bind (h v
)
62 (rotate-alignment horizontal vertical rotation
)
63 (handle-rotation (maxpoint box
) 180 h v
))))
65 (:before
(setf x
(- (xmin box
) (width stringbox
))))
67 (:center
(setf x
(- (x center
) (/ (width stringbox
) 2))))
68 (:right
(setf x
(- (xmax box
) (xmax stringbox
))))
69 (:after
(setf x
(xmax box
))))
71 (:atop
(setf y
(ymax box
)))
72 (:top
(setf y
(- (ymax box
) size
)))
73 (:middle
(setf y
(- (y center
) (/ size
2))))
75 (:below
(setf y
(- (ymin box
) size
))))
76 (let ((origin (point x y
)))
77 (draw-string origin text
)))))
81 (with-box-canvas (box 0 0 800 800)
82 (let ((tbox (box 50 50 700 500))
83 (font (get-font "~/Documents/Marydale.ttf")))
85 (set-stroke-color (hsv-color 1 1 1))
88 (draw-box-text tbox
"CENTER ATOP" :size
18 :loader font
92 (draw-box-text tbox
"AFTER BELOW" :size
18 :loader font