]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
* Documentation/user/tutorial.itely (Running LilyPond for the
[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
63 (define-public (setcolor r g b))
64
65 (define-public (resetcolor))
66
67
68 (define-public (entity entity string . attributes-alist)
69   (if (equal? string "")
70       (apply eoc entity attributes-alist)
71       (string-append
72        (apply eo (cons entity attributes-alist)) string (ec entity))))
73
74 (define (offset->point o)
75   (format #f " ~S,~S" (car o)  (- (cdr o))))
76
77 (define (svg-bezier lst close)
78   (let* ((c0 (car (list-tail lst 3)))
79          (c123 (list-head lst 3)))
80     (string-append
81      (if (not close) "M " "L ")
82      (offset->point c0)
83      "C " (apply string-append (map offset->point c123))
84      (if (not close) "" (string-append
85                          "L " (offset->point close))))))
86
87 (define (sqr x)
88   (* x x))
89
90 (define (integer->entity integer)
91   (format #f "&#x~x;" integer))
92
93 (define (char->entity char)
94   (integer->entity (char->integer char)))
95
96 (define (string->entities string)
97   (apply string-append
98          (map (lambda (x) (char->entity x)) (string->list string))))
99
100 (define pango-description-regexp-comma
101   (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
102
103 (define pango-description-regexp-nocomma
104   (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
105
106 (define (pango-description-to-svg-font str)
107   (let*
108       ((size 4.0)
109        (family "Helvetica")
110        (style #f)
111        (match-1 (regexp-exec pango-description-regexp-comma str))
112        (match-2 (regexp-exec pango-description-regexp-nocomma str))
113        (match (if match-1
114                   match-1
115                   match-2)))
116
117     (if (regexp-match? match)
118         (begin
119           (set! family (match:substring match 1))
120           (if (< 0 (string-length (match:substring match 2)))
121               (set! style (match:substring match 2)))
122           (set! size
123                 (string->number (match:substring match 3))))
124
125         (ly:warning (_ "can't decypher Pango description: ~a") str))
126
127     (set! style
128           (if (string? style)
129               (format "font-style:~a;" style)
130               ""))
131     
132     (format "font-family:~a;~afont-size:~a;text-anchor:west"
133             family
134             style
135             (/ size lily-unit-length))
136     ))
137
138 ;;; FONT may be font smob, or pango font string
139 (define (svg-font font)
140   (if (string? font)
141       (pango-description-to-svg-font font)
142       (let ((name-style (font-name-style font))
143             (size (modified-font-metric-font-scaling font))
144             (anchor "west"))
145
146         (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
147                 (car name-style) (cadr name-style)
148                 size anchor))))
149
150 (define (fontify font expr)
151   (entity 'text expr
152           `(style . ,(svg-font font))
153           '(fill . "currentColor")
154           ))
155
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;;; stencil outputters
158 ;;;
159
160 ;;; catch-all for missing stuff
161 ;;; comment this out to see find out what functions you miss :-)
162
163 (if #f
164     (begin
165       (define (dummy . foo) "")
166       (map (lambda (x) (module-define! this-module x dummy))
167            (append
168             (ly:all-stencil-expressions)
169             (ly:all-output-backend-commands)))
170       ))
171
172 (define (url-link url x y)
173   (string-append
174    (eo 'a `(xlink:href . ,url))
175    (eoc 'rect
176         `(x . ,(car x))
177         `(y . ,(car y))
178         `(width . ,(- (cdr x) (car x)))
179         `(height . ,(- (cdr y) (car y)))
180         '(fill . "none")
181         '(stroke . "none")
182         '(stroke-width . "0.0"))
183    (ec 'a)))
184
185 (define (grob-cause offset grob)
186   "")
187
188 (define (no-origin)
189   "")
190
191
192 (define (rect-beam width slope thick blot-diameter)
193   (let* ((x width)
194          (y (* slope width))
195          (z (/ y x)))
196     (entity 'rect ""
197             ;; The stroke will stick out.  To use stroke,
198             ;; the stroke-width must be subtracted from all other dimensions.
199             ;;'(stroke-linejoin . "round")
200             ;;'(stroke-linecap . "round")
201             ;;`(stroke-width . ,blot-diameter)
202             ;;'(stroke . "red")
203             ;;'(fill . "orange")
204
205             `(x . 0)
206             `(y . ,(- (/ thick 2)))
207             `(width . ,width)
208             `(height . ,(+ thick (* (abs z) (/ thick 2))))
209             `(rx . ,(/ blot-diameter 2))
210             `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)"  z)
211                         ))))
212
213 (define (beam width slope thick blot-diameter)
214   (let* ((b blot-diameter)
215          (t (- thick b))
216          (w (- width b))
217          (h (* w slope)))
218     (entity 'polygon ""
219             '(stroke-linejoin . "round")
220             '(stroke-linecap . "round")
221             `(stroke-width . ,blot-diameter)
222             '(stroke . "currentColor")
223             '(fill . "currentColor")
224             `(points . ,(string-join
225                          (map offset->point
226                               (list (cons (/ b 2) (/ t 2))
227                                     (cons (+ w (/ b 2)) (+ h (/ t 2)))
228                                     (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
229                                     (cons (/ b 2) (- (/ t 2)))))))
230             )))
231
232 (define (path-beam width slope thick blot-diameter)
233   (let* ((b blot-diameter)
234          (t (- thick b))
235          (w (- width b))
236          (h (* w slope)))
237     (entity 'path ""
238             '(stroke-linejoin . "round")
239             '(stroke-linecap . "round")
240             `(stroke-width . ,blot-diameter)
241             '(stroke . "currentColor")
242             '(fill . "currentColor")
243             `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
244                            (/ b 2) (/ t 2)
245                            w (- h)
246                            0 (- t)
247                            (- w) h
248                            0 t))
249             )))
250
251 (define (bezier-sandwich lst thick)
252   (let* ((first (list-tail lst 4))
253          (first-c0 (car (list-tail first 3)))
254          (second (list-head lst 4)))
255     (entity 'path ""
256             '(stroke-linejoin . "round")
257             '(stroke-linecap . "round")
258             `(stroke-width . ,thick)
259             '(stroke . "currentColor")
260             '(fill . "currentColor")
261             `(d . ,(string-append (svg-bezier first #f)
262                                   (svg-bezier second first-c0)))
263             )))
264
265 (define (char font i)
266   (dispatch
267    `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
268
269 (define-public (comment s)
270   (string-append "<!-- " s " !-->\n"))
271
272 (define (draw-line thick x1 y1 x2 y2 . alist)
273   
274   (apply entity 'line ""
275          (append
276           `((stroke-linejoin . "round")
277             (stroke-linecap . "round")
278             (stroke-width . ,thick)
279             (stroke . "currentColor")
280             (x1 . ,x1)
281             (y1 . ,(- y1))
282             (x2 . ,x2)
283             (y2 . ,(- y2)))
284           alist)))
285
286 (define (dashed-line thick on off dx dy)
287   (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
288
289 (define (filledbox breapth width depth height)
290   (round-filled-box breapth width depth height 0))
291
292 (define (named-glyph font name)
293   (dispatch
294    `(fontify ,font ,(entity 'tspan
295                             (integer->entity
296                              (ly:font-glyph-name-to-charcode font name))))))
297
298 (define (placebox x y expr)
299   (entity 'g
300           ;; FIXME -- JCN
301           ;;(dispatch expr)
302           expr
303           `(transform . ,(format #f "translate (~f, ~f)"
304                                  x (- y)))))
305
306 (define (polygon coords blot-diameter is-filled)
307   (entity
308    'polygon ""
309    '(stroke-linejoin . "round")
310    '(stroke-linecap . "round")
311    `(stroke-width . ,blot-diameter)
312    `(fill . ,(if is-filled "currentColor" "none"))
313    '(stroke . "currentColor")
314    `(points . ,(string-join
315                 (map offset->point (ly:list->offsets '() coords))))
316    ))
317
318 (define (round-filled-box breapth width depth height blot-diameter)
319   (entity 'rect ""
320           ;; The stroke will stick out.  To use stroke,
321           ;; the stroke-width must be subtracted from all other dimensions.
322           ;;'(stroke-linejoin . "round")
323           ;;'(stroke-linecap . "round")
324           ;;`(stroke-width . ,blot)
325           ;;'(stroke . "red")
326           ;;'(fill . "orange")
327
328           `(x . ,(- breapth))
329           `(y . ,(- height))
330           `(width . ,(+ breapth width))
331           `(height . ,(+ depth height))
332           `(ry . ,(/ blot-diameter 2))
333           ))
334
335 (define (circle radius thick is-filled)
336   (entity
337    'circle ""
338    '(stroke-linejoin . "round")
339    '(stroke-linecap . "round")
340    `(fill . ,(if is-filled "currentColor" "none"))
341    `(stroke . "currentColor")
342    `(stroke-width . ,thick)
343    `(r . ,radius)))
344
345 (define (text font string)
346   (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
347
348 (define (utf8-string pango-font-description string)
349   (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
350
351
352
353 (define (setcolor r g b)
354   (format "<g color=\"rgb(~a%,~a%,~a%)\">"
355           (* 100 r) (* 100 g) (* 100 b)
356           ))
357
358 (define (resetcolor)
359   "</g>")