]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
* scm/page-layout.scm (default-page-make-stencil): only add header
[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             (display
42              (string-append "undefined: " (symbol->string keyword) "\n"))
43             ""))))))
44
45 ;; Helper functions
46 (define-public (attributes attributes-alist)
47   (apply string-append
48          (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
49               attributes-alist)))
50
51 (define-public (eo entity . attributes-alist)
52   "o = open"
53   (format #f "<~S~a>\n" entity (attributes attributes-alist)))
54
55 (define-public (eoc entity . attributes-alist)
56   " oc = open/close"
57   (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
58
59 (define-public (ec entity)
60   "c = close"
61   (format #f "</~S>\n" entity))
62
63 (define-public (entity entity string . attributes-alist)
64   (if (equal? string "")
65       (apply eoc entity attributes-alist)
66       (string-append
67        (apply eo (cons entity attributes-alist)) string (ec entity))))
68
69 (define (offset->point o)
70   (format #f " ~S,~S" (car o)  (- (cdr o))))
71
72 (define (svg-bezier lst close)
73   (let* ((c0 (car (list-tail lst 3)))
74          (c123 (list-head lst 3)))
75     (string-append
76      (if (not close) "M " "L ")
77      (offset->point c0)
78      "C " (apply string-append (map offset->point c123))
79      (if (not close) "" (string-append
80                          "L " (offset->point close))))))
81
82 (define (sqr x)
83   (* x x))
84
85 (define (integer->entity integer)
86   (format #f "&#x~x;" integer))
87
88 (define (char->entity char)
89   (integer->entity (char->integer char)))
90
91 (define (string->entities string)
92   (apply string-append
93          (map (lambda (x) (char->entity x)) (string->list string))))
94
95 (define pango-description-regexp-comma
96   (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
97
98 (define pango-description-regexp-nocomma
99   (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
100
101 (define (pango-description-to-svg-font str)
102   (let*
103       ((size 4.0)
104        (family "Helvetica")
105        (style #f)
106        (match-1 (regexp-exec pango-description-regexp-comma str))
107        (match-2 (regexp-exec pango-description-regexp-nocomma str))
108        (match (if match-1
109                   match-1
110                   match-2)))
111
112     (if (regexp-match? match)
113         (begin
114           (set! family (match:substring match 1))
115           (if (< 0 (string-length (match:substring match 2)))
116               (set! style (match:substring match 2)))
117           (set! size
118                 (string->number (match:substring match 3))))
119
120         (display (format "Cannot decypher Pango description:  ~a\n" str)))
121
122     (set! style
123           (if (string? style)
124               (format "font-style:~a;" style)
125               ""))
126     
127     (format "font-family:~a;~afont-size:~a;text-anchor:west"
128             family
129             style
130             (/ size lily-unit-length))
131     ))
132
133 ;;; FONT may be font smob, or pango font string
134 (define (svg-font font)
135   (if (string? font)
136       (pango-description-to-svg-font font)
137       (let ((name-style (font-name-style font))
138             (size (modified-font-metric-font-scaling font))
139             (anchor "west"))
140
141         (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
142                 (car name-style) (cadr name-style)
143                 size anchor))))
144
145 (define (fontify font expr)
146   (entity 'text expr
147           `(style . ,(svg-font font))
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 . "black")
217             '(fill . "black")
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 . "black")
236             '(fill . "black")
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 . "black")
254             '(fill . "black")
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 . "black")
274             ;;'(fill . "black")
275             (x1 . ,x1)
276             (y1 . ,y1)
277             (x2 . ,x2)
278             (y2 . ,y2))
279           alist)))
280
281 (define (dashed-line thick on off dx dy)
282   (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
283
284 ;; WTF is this in every backend?
285 (define (horizontal-line x1 x2 th)
286   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
287
288 (define (filledbox breapth width depth height)
289   (round-filled-box breapth width depth height 0))
290
291 (define (named-glyph font name)
292   (dispatch
293    `(fontify ,font ,(entity 'tspan
294                             (integer->entity
295                              (ly:font-glyph-name-to-charcode font name))))))
296
297 (define (placebox x y expr)
298   (entity 'g
299           ;; FIXME -- JCN
300           ;;(dispatch expr)
301           expr
302           `(transform . ,(format #f "translate (~f, ~f)"
303                                  x (- y)))))
304
305
306 (define (polygon coords blot-diameter)
307   (entity 'polygon ""
308           '(stroke-linejoin . "round")
309           '(stroke-linecap . "round")
310           `(stroke-width . ,blot-diameter)
311           '(stroke . "black")
312           ;;'(fill . "black")
313           `(points . ,(string-join
314                        (map offset->point (ly:list->offsets '() coords))))
315   ))
316
317 (define (round-filled-box breapth width depth height blot-diameter)
318   (entity 'rect ""
319           ;; The stroke will stick out.  To use stroke,
320           ;; the stroke-width must be subtracted from all other dimensions.
321           ;;'(stroke-linejoin . "round")
322           ;;'(stroke-linecap . "round")
323           ;;`(stroke-width . ,blot)
324           ;;'(stroke . "red")
325           ;;'(fill . "orange")
326
327           `(x . ,(- breapth))
328           `(y . ,(- height))
329           `(width . ,(+ breapth width))
330           `(height . ,(+ depth height))
331           `(ry . ,(/ blot-diameter 2))
332           ))
333
334 (define (text font string)
335   (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
336
337 (define (utf8-string pango-font-description string)
338   (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))