-
Notifications
You must be signed in to change notification settings - Fork 6
/
shapedesc.lisp
218 lines (215 loc) · 9.36 KB
/
shapedesc.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
(in-package #:sdf/base)
(defun serialize-shape (shape &key allow-ratios normalize edge-colors normalize-order)
(let ((ox nil)
(oy nil))
;; when normalize-order is true, start all contours at point with lowest
;; Y then lowest X
(when normalize-order
(let ((es (shape-to-edit-shape shape))
(changed nil))
(setf (contours es)
(loop for c1 in (contours es)
for c2 = (if (typep c1 'es-contour-vertex) c1 (eprev c1))
for x1 = (p-rx (point c2))
for y1 = (p-ry (point c2))
do (unless (eql c2 c1)
(setf changed t))
(map-modifying-contour
c1 (lambda (n)
(when (typep n 'es-contour-vertex)
(let* ((pn (point n))
(py (p-ry pn)))
(when (or (< py y1)
(and (= py y1)
(< (p-rx pn) x1)))
(setf changed t
c2 n
x1 (p-rx pn)
y1 py))))
n))
collect c2))
(when changed
(setf shape (edit-shape-to-shape es)))))
(with-output-to-string (s)
(labels ((pf (X)
;; sbcl is doing odd things with ~e, ~f prints too
;; many 0s for large/small #s, and ~g is confusing.
;; prin1 seems to work well enough though?
(let ((*read-default-float-format* 'double-float))
(prin1 x s))
;; (format nil "~e" 1.5d0)->"1.4999999999999997d+0"
;; (format nil "~f" 1d30)->"1000000000000000000000000000000.0"
;; (format nil "~g" 1.5d0)->"1.5 "
;;(let ((*read-default-float-format* 'double-float))
;; (list (prin1-to-string 1.5d0)
;; (prin1-to-string 1d30)) -> ("1.5d0" "1.0d30")
#++
(let ((fx (if (floatp x) "~,,,,,,'ee" "~s"))
(fy (if (floatp y) "~,,,,,,'ee" "~s")))
(format s "~@?, ~@?" fx x fy y)))
(p (p)
(let ((x (if (or (integerp (p-rx p)) allow-ratios)
(p-rx p)
(p-dx p)))
(y (if (or (integerp (p-ry p)) allow-ratios)
(p-ry p)
(p-dy p))))
(when normalize
;; when normalize is true, subtract first point
;; from all points, and turn 1.0, 1.0 into 1, 1 (or
;; if allow-ratios is true, rationalize all floats)
(unless ox
(setf ox x)
(setf oy y))
(setf x (- x ox)
y (- y oy)))
(if allow-ratios
(setf x (rationalize x)
y (rationalize y))
(let ((fx (floor x))
(fy (floor y)))
(when (= x fx)
(setf x fx))
(when (= y fy)
(setf y fy))))
(pf x) (format s ",") (pf y)))
(c (n &optional semi)
(let ((e (gethash n edge-colors)))
(when e
(cond
((or (equalp e '(t nil t))
(eql e #b101))
(format s "m"))
((or (equalp e '(nil t t))
(eql e #b011))
(format s "c"))
((or (equalp e '(t t nil))
(eql e #b110))
(format s "y"))
(t (format s "w")))
(when semi
(format s "; "))))))
(let ((prev-contour nil))
(map-contour-segments
shape
(lambda (c# node end)
(unless (eql c# prev-contour)
(setf prev-contour c#)
(format s "~&{ "))
(etypecase node
(point
(p node)
(format s "; "))
(segment
(when edge-colors
(c node t))
;; nothing to do
)
(bezier2
(when edge-colors
(c node))
(format s "(")
(p (b2-c1 node))
(format s "); ")))
(when end
(format s "# }")))))))))
(defparameter *dump-parse* nil)
#++ (setf *dump-parse* t)
(defun parse-shape (str)
(with-input-from-string (s str)
(with-shape-builder (shape)
(labels ((color ()
(let ((n (peek-char t s)))
(when (position n "cmyw" :test 'char-equal)
(list :color
(intern (string-upcase (read-char s)) :keyword)))))
(number ()
(let ((x (loop for n = (peek-char t s)
while (or (digit-char-p n)
(position n "+-.edsf/"
:test 'char-equal))
collect (read-char s))))
(when x
(when *dump-parse*
(format t " number = ~s~%" x))
(parse-number:parse-number (coerce x 'string)))))
(c (x)
(let ((n (peek-char t s)))
(when (char= n x)
(read-char s))))
(\, () (c #\,))
(\; () (c #\;))
(\( () (c #\())
(\) () (c #\)))
(\{ () (c #\{))
(\} () (c #\}))
(\# () (when (c #\#) '(:hash)))
(pair ()
(let ((a (number)))
(when a
(unless (\,)
(error "expected , after ~s while parsing point?" a))
(let ((b (number)))
(unless b
(error "expected number after ~s while parsing point?"
a))
(when *dump-parse*
(format t " pair = ~s ~s~%" a b))
(list :p a b)))))
(control ()
(when (\()
(when *dump-parse*
(format t " ("))
(let ((c1 (pair))
(c2 (when (\;)
(pair))))
(unless (\))
(error "expected ) after reading control point ~s,~s?~% got ~s"
c1 c2 (peek-char nil s)))
(when *dump-parse*
(format t " control = ~s ~s~%" c1 c2))
(list :control c1 c2))))
(contour ()
(when (peek-char t s nil nil)
(unless (\{)
(error "got ~s instead of contour?" (read-char s)))
(let ((p1 (cdr (pair))))
(assert p1)
(start-contour (first p1) (second p1))
(loop with cont = nil
with color = nil
for s = (\;)
for (x . p) = (or (pair) (control) (color) (\#))
while x
do (case x
((or :p :hash)
(when (eql x :hash)
(setf p p1))
;; todo: handle colors?
(if cont
(quadratic-to
(first cont) (second cont)
(first p) (second p))
(line-to (first p) (second p)))
(setf cont nil
color nil)
(when (eql x :hash)
;; #\# must be last thing in contour
;; if present
(loop-finish)))
(:control
(when (second p)
(error "too manu points in control point?~%~s" p))
(when cont
(error "got 2 control points in a row?~% ~s ~s"
cont p))
(setf cont (cdr (first p))))
(:color
(setf color p))))
(let ((end (\})))
(unless end
(error "expected } at end of contour, got ~s?"
(read-char s)))
(end-contour)))
t)))
(loop while (contour))))))