]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
*** empty log message ***
[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           ))
148
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 ;;; stencil outputters
151 ;;;
152
153 ;;; catch-all for missing stuff
154 ;;; comment this out to see find out what functions you miss :-)
155
156 (if #f
157     (begin
158       (define (dummy . foo) "")
159       (map (lambda (x) (module-define! this-module x dummy))
160            (append
161             (ly:all-stencil-expressions)
162             (ly:all-output-backend-commands)))
163       ))
164
165 (define (url-link url x y)
166   (string-append
167    (eo 'a `(xlink:href . ,url))
168    (eoc 'rect
169         `(x . ,(car x))
170         `(y . ,(car y))
171         `(width . ,(- (cdr x) (car x)))
172         `(height . ,(- (cdr y) (car y)))
173         '(fill . "none")
174         '(stroke . "none")
175         '(stroke-width . "0.0"))
176    (ec 'a)))
177
178 (define (grob-cause offset grob)
179   "")
180
181 (define (no-origin)
182   "")
183
184
185 (define (rect-beam width slope thick blot-diameter)
186   (let* ((x width)
187          (y (* slope width))
188          (z (/ y x)))
189     (entity 'rect ""
190             ;; The stroke will stick out.  To use stroke,
191             ;; the stroke-width must be subtracted from all other dimensions.
192             ;;'(stroke-linejoin . "round")
193             ;;'(stroke-linecap . "round")
194             ;;`(stroke-width . ,blot-diameter)
195             ;;'(stroke . "red")
196             ;;'(fill . "orange")
197
198             `(x . 0)
199             `(y . ,(- (/ thick 2)))
200             `(width . ,width)
201             `(height . ,(+ thick (* (abs z) (/ thick 2))))
202             `(rx . ,(/ blot-diameter 2))
203             `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)"  z)
204                         ))))
205
206 (define (beam width slope thick blot-diameter)
207   (let* ((b blot-diameter)
208          (t (- thick b))
209          (w (- width b))
210          (h (* w slope)))
211     (entity 'polygon ""
212             '(stroke-linejoin . "round")
213             '(stroke-linecap . "round")
214             `(stroke-width . ,blot-diameter)
215             '(stroke . "black")
216             '(fill . "black")
217             `(points . ,(string-join
218                          (map offset->point
219                               (list (cons (/ b 2) (/ t 2))
220                                     (cons (+ w (/ b 2)) (+ h (/ t 2)))
221                                     (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
222                                     (cons (/ b 2) (- (/ t 2)))))))
223             )))
224
225 (define (path-beam width slope thick blot-diameter)
226   (let* ((b blot-diameter)
227          (t (- thick b))
228          (w (- width b))
229          (h (* w slope)))
230     (entity 'path ""
231             '(stroke-linejoin . "round")
232             '(stroke-linecap . "round")
233             `(stroke-width . ,blot-diameter)
234             '(stroke . "black")
235             '(fill . "black")
236             `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
237                            (/ b 2) (/ t 2)
238                            w (- h)
239                            0 (- t)
240                            (- w) h
241                            0 t))
242             )))
243
244 (define (bezier-sandwich lst thick)
245   (let* ((first (list-tail lst 4))
246          (first-c0 (car (list-tail first 3)))
247          (second (list-head lst 4)))
248     (entity 'path ""
249             '(stroke-linejoin . "round")
250             '(stroke-linecap . "round")
251             `(stroke-width . ,thick)
252             '(stroke . "black")
253             '(fill . "black")
254             `(d . ,(string-append (svg-bezier first #f)
255                                   (svg-bezier second first-c0)))
256             )))
257
258 (define (char font i)
259   (dispatch
260    `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
261
262 (define-public (comment s)
263   (string-append "<!-- " s " !-->\n"))
264
265 (define (draw-line thick x1 y1 x2 y2 . alist)
266   
267   (apply entity 'line ""
268          (append
269           `((stroke-linejoin . "round")
270             (stroke-linecap . "round")
271             (stroke-width . ,thick)
272             (stroke . "black")
273             ;;'(fill . "black")
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 ;; WTF is this in every backend?
284 (define (horizontal-line x1 x2 th)
285   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
286
287 (define (filledbox breapth width depth height)
288   (round-filled-box breapth width depth height 0))
289
290 (define (named-glyph font name)
291   (dispatch
292    `(fontify ,font ,(entity 'tspan
293                             (integer->entity
294                              (ly:font-glyph-name-to-charcode font name))))))
295
296 (define (placebox x y expr)
297   (entity 'g
298           ;; FIXME -- JCN
299           ;;(dispatch expr)
300           expr
301           `(transform . ,(format #f "translate (~f, ~f)"
302                                  x (- y)))))
303
304
305 (define (polygon coords blot-diameter)
306   (entity 'polygon ""
307           '(stroke-linejoin . "round")
308           '(stroke-linecap . "round")
309           `(stroke-width . ,blot-diameter)
310           '(stroke . "black")
311           ;;'(fill . "black")
312           `(points . ,(string-join
313                        (map offset->point (ly:list->offsets '() coords))))
314   ))
315
316 (define (round-filled-box breapth width depth height blot-diameter)
317   (entity 'rect ""
318           ;; The stroke will stick out.  To use stroke,
319           ;; the stroke-width must be subtracted from all other dimensions.
320           ;;'(stroke-linejoin . "round")
321           ;;'(stroke-linecap . "round")
322           ;;`(stroke-width . ,blot)
323           ;;'(stroke . "red")
324           ;;'(fill . "orange")
325
326           `(x . ,(- breapth))
327           `(y . ,(- height))
328           `(width . ,(+ breapth width))
329           `(height . ,(+ depth height))
330           `(ry . ,(/ blot-diameter 2))
331           ))
332
333 (define (text font string)
334   (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
335
336 (define (utf8-string pango-font-description string)
337   (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))