1 ;;;; cl-vectors -- Rasterizer and paths manipulation library
2 ;;;; Copyright (C) 2007 Frédéric Jolliton <frederic@jolliton.com>
4 ;;;; This library is free software; you can redistribute it and/or
5 ;;;; modify it under the terms of the Lisp Lesser GNU Public License
6 ;;;; (http://opensource.franz.com/preamble.html), known as the LLGPL.
8 ;;;; This library is distributed in the hope that it will be useful, but
9 ;;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lisp
11 ;;;; Lesser GNU Public License for more details.
13 (defpackage #:net.tuxee.paths-ttf
14 (:use
#:cl
#:net.tuxee.paths
#:zpb-ttf
)
15 (:nicknames
#:paths-ttf
)
16 (:export
#:paths-from-glyph
20 (in-package #:net.tuxee.paths-ttf
)
22 (defun paths-from-glyph (glyph &key
(offset (make-point 0 0))
23 (scale-x 1.0) (scale-y 1.0)
25 "Extract paths from a glyph."
26 (flet ((point (p) (p+ (make-point (* (x p
) scale-x
)
30 (do-contours (contour glyph
)
31 (let ((path (create-path :polygon
))
33 (do-contour-segments (a b c
) contour
35 (pb (when b
(point b
)))
38 (assert (and (= (point-x last-point
) (point-x pa
))
39 (= (point-y last-point
) (point-y pa
))))
43 (make-bezier-curve (list pb
))
46 (setq last-point pc
)))
47 (when (minusp (* scale-x scale-y
))
50 (setq result
(nreverse result
))
51 (when (and auto-orient result
)
52 (path-orient (car result
) auto-orient
(cdr result
)))
55 (defun paths-from-string (font-loader text
&key
(offset (make-point 0 0))
56 (scale-x 1.0) (scale-y 1.0)
57 (kerning t
) (auto-orient nil
))
58 "Extract paths from a string."
61 for previous-char
= nil then char
63 for previous-glyph
= nil then glyph
64 for glyph
= (find-glyph char font-loader
)
65 do
(when previous-char
68 (make-point (* scale-x
69 (+ (advance-width previous-glyph
)
71 (kerning-offset previous-char
76 (let ((glyph-paths (paths-from-glyph glyph
77 :offset offset
:auto-orient auto-orient
78 :scale-x scale-x
:scale-y scale-y
)))
79 (push glyph-paths result
)))
80 (apply #'nconc
(nreverse result
))))
82 (defun make-string-path (font-loader text
&key
(position (make-point 0 0)) (size 12)
83 (halign :left
) (valign :baseline
)
84 (inverted t
) (kerning t
))
85 (let* ((em (units/em font-loader
))
90 (setq scale-y
(- scale-y
)))
91 (let ((bb (string-bounding-box text font-loader
:kerning kerning
)))
92 (setq position
(p- position
102 (/ (+ (aref bb
0) (aref bb
2)) 2.0)))
111 (/ (+ (aref bb
1) (aref bb
3)) 2.0))))
113 (paths-from-string font-loader text
:offset position
114 :scale-x scale-x
:scale-y scale-y