]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
(setcolor): implement (re)setcolor with <g>
[lilypond.git] / scm / output-svg.scm
1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 2002--2005 Jan Nieuwenhuizen <janneke@gnu.org>
6
7 ;;;; http://www.w3.org/TR/SVG11
8 ;;;; http://www.w3.org/TR/SVG12/ -- page, pageSet in draft
9
10 ;;;; TODO:
11 ;;;;  * .cff MUST NOT be in fc's fontpath.
12 ;;;;    - workaround: remove mf/out from ~/.fonts.conf,
13 ;;;;      instead add ~/.fonts and symlink all /mf/out/*otf there.
14 ;;;;    - bug in fontconfig/freetype/pango?
15
16 ;;;;  * inkscape page/pageSet support
17 ;;;;  * inkscape SVG-font support
18 ;;;;    - use fontconfig/fc-cache for now, see output-gnome.scm
19
20 (define-module (scm output-svg))
21 (define this-module (current-module))
22
23 (use-modules
24  (guile)
25  (ice-9 regex)
26  (lily)
27  (srfi srfi-13))
28
29
30 (define lily-unit-length 1.75)
31
32 (define (dispatch expr)
33   (let ((keyword (car expr)))
34     (cond
35      ((eq? keyword 'some-func) "")
36      ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
37      (else
38       (if (module-defined? this-module keyword)
39           (apply (eval keyword this-module) (cdr expr))
40           (begin
41             (ly:warning (_ "undefined: ~S") keyword)
42             ""))))))
43
44 ;; Helper functions
45 (define-public (attributes attributes-alist)
46   (apply string-append
47          (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
48               attributes-alist)))
49
50 (define-public (eo entity . attributes-alist)
51   "o = open"
52   (format #f "<~S~a>\n" entity (attributes attributes-alist)))
53
54 (define-public (eoc entity . attributes-alist)
55   " oc = open/close"
56   (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
57
58 (define-public (ec entity)
59   "c = close"
60   (format #f "</~S>\n" entity))
61
62 (define-public (entity entity string . attributes-alist)
63   (if (equal? string "")
64       (apply eoc entity attributes-alist)
65       (string-append
66        (apply eo (cons entity attributes-alist)) string (ec entity))))
67
68 (define (offset->point o)
69   (format #f " ~S,~S" (car o)  (- (cdr o))))
70
71 (define (svg-bezier lst close)
72   (let* ((c0 (car (list-tail lst 3)))
73          (c123 (list-head lst 3)))
74     (string-append
75      (if (not close) "M " "L ")
76      (offset->point c0)
77      "C " (apply string-append (map offset->point c123))
78      (if (not close) "" (string-append
79                          "L " (offset->point close))))))
80
81 (define (sqr x)
82   (* x x))
83
84 (define (integer->entity integer)
85   (format #f "&#x~x;" integer))
86
87 (define (char->entity char)
88   (integer->entity (char->integer char)))
89
90 (define (string->entities string)
91   (apply string-append
92          (map (lambda (x) (char->entity x)) (string->list string))))
93
94 (define pango-description-regexp-comma
95   (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
96
97 (define pango-description-regexp-nocomma
98   (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
99
100 (define (pango-description-to-svg-font str)
101   (let*
102       ((size 4.0)
103        (family "Helvetica")
104        (style #f)
105        (match-1 (regexp-exec pango-description-regexp-comma str))
106        (match-2 (regexp-exec pango-description-regexp-nocomma str))
107        (match (if match-1
108                   match-1
109                   match-2)))
110
111     (if (regexp-match? match)
112         (begin
113           (set! family (match:substring match 1))
114           (if (< 0 (string-length (match:substring match 2)))
115               (set! style (match:substring match 2)))
116           (set! size
117                 (string->number (match:substring match 3))))
118
119         (ly:warning (_ "can't decypher Pango description: ~a") str))
120
121     (set! style
122           (if (string? style)
123               (format "font-style:~a;" style)
124               ""))
125     
126     (format "font-family:~a;~afont-size:~a;text-anchor:west"
127             family
128             style
129             (/ size lily-unit-length))
130     ))
131
132 ;;; FONT may be font smob, or pango font string
133 (define (svg-font font)
134   (if (string? font)
135       (pango-description-to-svg-font font)
136       (let ((name-style (font-name-style font))
137             (size (modified-font-metric-font-scaling font))
138             (anchor "west"))
139
140         (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
141                 (car name-style) (cadr name-style)
142                 size anchor))))
143
144 (define (fontify font expr)
145   (entity 'text expr
146           `(style . ,(svg-font font))
147           '(fill . "currentColor")
148           ))
149
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;;; stencil outputters
152 ;;;
153
154 ;;; catch-all for missing stuff
155 ;;; comment this out to see find out what functions you miss :-)
156
157 (if #f
158     (begin
159       (define (dummy . foo) "")
160       (map (lambda (x) (module-define! this-module x dummy))
161            (append
162             (ly:all-stencil-expressions)
163             (ly:all-output-backend-commands)))
164       ))
165
166 (define (url-link url x y)
167   (string-append
168    (eo 'a `(xlink:href . ,url))
169    (eoc 'rect
170         `(x . ,(car x))
171         `(y . ,(car y))
172         `(width . ,(- (cdr x) (car x)))
173         `(height . ,(- (cdr y) (car y)))
174         '(fill . "none")
175         '(stroke . "none")
176         '(stroke-width . "0.0"))
177    (ec 'a)))
178
179 (define (grob-cause offset grob)
180   "")
181
182 (define (no-origin)
183   "")
184
185
186 (define (rect-beam width slope thick blot-diameter)
187   (let* ((x width)
188          (y (* slope width))
189          (z (/ y x)))
190     (entity 'rect ""
191             ;; The stroke will stick out.  To use stroke,
192             ;; the stroke-width must be subtracted from all other dimensions.
193             ;;'(stroke-linejoin . "round")
194             ;;'(stroke-linecap . "round")
195             ;;`(stroke-width . ,blot-diameter)
196             ;;'(stroke . "red")
197             ;;'(fill . "orange")
198
199             `(x . 0)
200             `(y . ,(- (/ thick 2)))
201             `(width . ,width)
202             `(height . ,(+ thick (* (abs z) (/ thick 2))))
203             `(rx . ,(/ blot-diameter 2))
204             `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)"  z)
205                         ))))
206
207 (define (beam width slope thick blot-diameter)
208   (let* ((b blot-diameter)
209          (t (- thick b))
210          (w (- width b))
211          (h (* w slope)))
212     (entity 'polygon ""
213             '(stroke-linejoin . "round")
214             '(stroke-linecap . "round")
215             `(stroke-width . ,blot-diameter)
216             '(stroke . "currentColor")
217             '(fill . "currentColor")
218             `(points . ,(string-join
219                          (map offset->point
220                               (list (cons (/ b 2) (/ t 2))
221                                     (cons (+ w (/ b 2)) (+ h (/ t 2)))
222                                     (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
223                                     (cons (/ b 2) (- (/ t 2)))))))
224             )))
225
226 (define (path-beam width slope thick blot-diameter)
227   (let* ((b blot-diameter)
228          (t (- thick b))
229          (w (- width b))
230          (h (* w slope)))
231     (entity 'path ""
232             '(stroke-linejoin . "round")
233             '(stroke-linecap . "round")
234             `(stroke-width . ,blot-diameter)
235             '(stroke . "currentColor")
236             '(fill . "currentColor")
237             `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
238                            (/ b 2) (/ t 2)
239                            w (- h)
240                            0 (- t)
241                            (- w) h
242                            0 t))
243             )))
244
245 (define (bezier-sandwich lst thick)
246   (let* ((first (list-tail lst 4))
247          (first-c0 (car (list-tail first 3)))
248          (second (list-head lst 4)))
249     (entity 'path ""
250             '(stroke-linejoin . "round")
251             '(stroke-linecap . "round")
252             `(stroke-width . ,thick)
253             '(stroke . "currentColor")
254             '(fill . "currentColor")
255             `(d . ,(string-append (svg-bezier first #f)
256                                   (svg-bezier second first-c0)))
257             )))
258
259 (define (char font i)
260   (dispatch
261    `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
262
263 (define-public (comment s)
264   (string-append "<!-- " s " !-->\n"))
265
266 (define (draw-line thick x1 y1 x2 y2 . alist)
267   
268   (apply entity 'line ""
269          (append
270           `((stroke-linejoin . "round")
271             (stroke-linecap . "round")
272             (stroke-width . ,thick)
273             (stroke . "currentColor")
274             (x1 . ,x1)
275             (y1 . ,(- y1))
276             (x2 . ,x2)
277             (y2 . ,(- y2)))
278           alist)))
279
280 (define (dashed-line thick on off dx dy)
281   (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
282
283 (define (filledbox breapth width depth height)
284   (round-filled-box breapth width depth height 0))
285
286 (define (named-glyph font name)
287   (dispatch
288    `(fontify ,font ,(entity 'tspan
289                             (integer->entity
290                              (ly:font-glyph-name-to-charcode font name))))))
291
292 (define (placebox x y expr)
293   (entity 'g
294           ;; FIXME -- JCN
295           ;;(dispatch expr)
296           expr
297           `(transform . ,(format #f "translate (~f, ~f)"
298                                  x (- y)))))
299
300 (define (polygon coords blot-diameter is-filled)
301   (entity
302    'polygon ""
303    '(stroke-linejoin . "round")
304    '(stroke-linecap . "round")
305    `(stroke-width . ,blot-diameter)
306    `(fill . ,(if is-filled "currentColor" "none"))
307    '(stroke . "currentColor")
308    `(points . ,(string-join
309                 (map offset->point (ly:list->offsets '() coords))))
310    ))
311
312 (define (round-filled-box breapth width depth height blot-diameter)
313   (entity 'rect ""
314           ;; The stroke will stick out.  To use stroke,
315           ;; the stroke-width must be subtracted from all other dimensions.
316           ;;'(stroke-linejoin . "round")
317           ;;'(stroke-linecap . "round")
318           ;;`(stroke-width . ,blot)
319           ;;'(stroke . "red")
320           ;;'(fill . "orange")
321
322           `(x . ,(- breapth))
323           `(y . ,(- height))
324           `(width . ,(+ breapth width))
325           `(height . ,(+ depth height))
326           `(ry . ,(/ blot-diameter 2))
327           ))
328
329 (define (circle radius thick is-filled)
330   (entity
331    'circle ""
332    '(stroke-linejoin . "round")
333    '(stroke-linecap . "round")
334    `(fill . ,(if is-filled "currentColor" "none"))
335    `(stroke . "currentColor")
336    `(stroke-width . ,thick)
337    `(r . ,radius)))
338
339 (define (text font string)
340   (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
341
342 (define (utf8-string pango-font-description string)
343   (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
344
345
346
347 (define (setcolor r g b)
348   (format "<g color=\"rgb(~a%,~a%,~a%)\">"
349           (* 100 r) (* 100 g) (* 100 b)
350           ))
351
352 (define (resetcolor)
353   "</g>")