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)) (scale-x 1.0) (scale-y 1.0) (auto-orient nil
))
23 (flet ((point (p) (p+ (make-point (* (x p
) scale-x
)
27 (do-contours (contour glyph
)
28 (let ((path (create-path :polygon
))
30 (do-contour-segments (a b c
) contour
32 (pb (when b
(point b
)))
35 (assert (and (= (point-x last-point
) (point-x pa
))
36 (= (point-y last-point
) (point-y pa
))))
39 (path-extend path
(make-bezier-curve (list pb
)) pc
)
40 (path-extend path
(make-straight-line) pc
))
41 (setf last-point pc
)))
42 (when (minusp (* scale-x scale-y
))
45 (setq result
(nreverse result
))
47 (path-orient (car result
) auto-orient
(cdr result
)))
50 (defun paths-from-string (font-loader text
&key
(offset (make-point 0 0)) (scale-x 1.0) (scale-y 1.0) (kerning t
) (auto-orient nil
))
53 for previous-char
= nil then char
55 for previous-glyph
= nil then glyph
56 for glyph
= (find-glyph char font-loader
)
57 do
(when previous-char
60 (make-point (* scale-x
61 (+ (advance-width previous-glyph
)
63 (kerning-offset previous-char
68 (let ((glyph-paths (paths-from-glyph glyph
69 :offset offset
:auto-orient auto-orient
70 :scale-x scale-x
:scale-y scale-y
)))
71 (push glyph-paths result
)))
72 (apply #'nconc
(nreverse result
))))
74 (defun make-string-path (font-loader text
75 &key
(position (make-point 0 0)) (size 12)
76 (halign :left
) (valign :baseline
) (inverted t
) (kerning t
))
77 (let* ((em (units/em font-loader
))
82 (setf scale-y
(- scale-y
)))
83 (let ((bb (string-bounding-box text font-loader
:kerning kerning
)))
84 (setf position
(p- position
94 (/ (+ (aref bb
0) (aref bb
2)) 2.0)))
103 (/ (+ (aref bb
1) (aref bb
3)) 2.0))))
105 (paths-from-string font-loader text
:offset position
106 :scale-x scale-x
:scale-y scale-y