]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
* scm/output-gnome.scm:
[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 (debug-enable 'backtrace)
21 (define-module (scm output-svg))
22 (define this-module (current-module))
23
24 (use-modules
25  (guile)
26  (ice-9 regex)
27  (lily)
28  (srfi srfi-13))
29
30 ;; GLobals
31 ;; FIXME: 2?
32 (define output-scale (* 2 scale-to-unit))
33
34 (define (dispatch expr)
35   (let ((keyword (car expr)))
36     (cond
37      ((eq? keyword 'some-func) "")
38      ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
39      (else
40       (if (module-defined? this-module keyword)
41           (apply (eval keyword this-module) (cdr expr))
42           (begin
43             (display
44              (string-append "undefined: " (symbol->string keyword) "\n"))
45             ""))))))
46
47 ;; Helper functions
48 (define-public (attributes attributes-alist)
49   (apply string-append
50          (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
51               attributes-alist)))
52
53 (define-public (eo entity . attributes-alist)
54   (format #f "<~S~a>\n" entity (attributes attributes-alist)))
55
56 (define-public (eoc entity . attributes-alist)
57   (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
58
59 (define-public (ec entity)
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 ;;; FONT may be font smob, or pango font string
95 (define (svg-font font)
96   (let ((name-style (if (string? font)
97                         (list font "Regular")
98                         (font-name-style font)))
99             (size (svg-font-size font))
100             (anchor "west"))
101         (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
102                 (car name-style) (cadr name-style) size anchor)))
103
104 ;;; FONT may be font smob, or pango font string
105 (define (svg-font-size font)
106   (if (string? font)
107       12
108       (* output-scale (font-size font))))
109
110 (define (fontify font expr)
111    (entity 'text expr (cons 'style (svg-font font))))
112
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114 ;;; stencil outputters
115 ;;;
116
117 ;;; catch-all for missing stuff
118 ;;; comment this out to see find out what functions you miss :-)
119 (define (dummy . foo) "")
120 (map (lambda (x) (module-define! this-module x dummy))
121      (append
122       (ly:all-stencil-expressions)
123       (ly:all-output-backend-commands)))
124
125 (define (rect-beam width slope thick blot-diameter)
126   (let* ((x width)
127          (y (* slope width))
128          (z (/ y x)))
129     (entity 'rect ""
130             ;; The stroke will stick out.  To use stroke,
131             ;; the stroke-width must be subtracted from all other dimensions.
132             ;;'(stroke-linejoin . "round")
133             ;;'(stroke-linecap . "round")
134             ;;`(stroke-width . ,blot-diameter)
135             ;;'(stroke . "red")
136             ;;'(fill . "orange")
137
138             `(x . 0)
139             `(y . ,(- (/ thick 2)))
140             `(width . ,width)
141             `(height . ,(+ thick (* (abs z) (/ thick 2))))
142             `(rx . ,(/ blot-diameter 2))
143             `(transform . ,(string-append
144                             (format #f "matrix (1, ~f, 0, 1, 0, 0)" (- z))
145                             (format #f " scale (~f, ~f)"
146                                     output-scale output-scale))))))
147
148 (define (beam width slope thick blot-diameter)
149   (let* ((b blot-diameter)
150          (t (- thick b))
151          (w (- width b))
152          (h (* w slope)))
153     (entity 'polygon ""
154             '(stroke-linejoin . "round")
155             '(stroke-linecap . "round")
156             `(stroke-width . ,blot-diameter)
157             '(stroke . "black")
158             '(fill . "black")
159             `(points . ,(string-join
160                          (map offset->point
161                               (list (cons (/ b 2) (/ t 2))
162                                     (cons (+ w (/ b 2)) (+ h (/ t 2)))
163                                     (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
164                                     (cons (/ b 2) (- (/ t 2)))))))
165             `(transform
166               . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
167
168 (define (path-beam width slope thick blot-diameter)
169   (let* ((b blot-diameter)
170          (t (- thick b))
171          (w (- width b))
172          (h (* w slope)))
173     (entity 'path ""
174             '(stroke-linejoin . "round")
175             '(stroke-linecap . "round")
176             `(stroke-width . ,blot-diameter)
177             '(stroke . "black")
178             '(fill . "black")
179             `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
180                            (/ b 2) (/ t 2)
181                            w (- h)
182                            0 (- t)
183                            (- w) h
184                            0 t))
185           `(transform
186             . ,(format #f "scale (~f, ~f)" output-scale output-scale)))))
187
188 (define (bezier-sandwich lst thick)
189   (let* ((first (list-tail lst 4))
190          (first-c0 (car (list-tail first 3)))
191          (second (list-head lst 4)))
192     (entity 'path ""
193             '(stroke-linejoin . "round")
194             '(stroke-linecap . "round")
195             `(stroke-width . ,thick)
196             '(stroke . "black")
197             '(fill . "black")
198             `(d . ,(string-append (svg-bezier first #f)
199                                   (svg-bezier second first-c0)))
200           `(transform
201             . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
202
203 (define (char font i)
204   (dispatch
205    `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
206
207 (define-public (comment s)
208   (string-append "<!-- " s " !-->\n"))
209
210 (define (dashed-line thick on off dx dy)
211   (draw-line thick 0 0 dx dy))
212
213 (define (draw-line thick x1 y1 x2 y2)
214   (entity 'line ""
215           '(stroke-linejoin . "round")
216           '(stroke-linecap . "round")
217           `(stroke-width . ,thick)
218           '(stroke . "black")
219           ;;'(fill . "black")
220           `(x1 . ,x1)
221           `(y1 . ,y1)
222           `(x2 . ,x2)
223           `(y2 . ,y2)
224           `(transform
225             . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
226
227 ;; WTF is this in every backend?
228 (define (horizontal-line x1 x2 th)
229   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
230
231 (define (filledbox breapth width depth height)
232   (round-filled-box breapth width depth height 0))
233
234 (define (named-glyph font name)
235   (dispatch
236    `(fontify ,font ,(entity 'tspan
237                             (integer->entity
238                              (ly:font-glyph-name-to-charcode font name))))))
239
240 (define (placebox x y expr)
241   (entity 'g
242           ;; FIXME -- JCN
243           ;;(dispatch expr)
244           expr
245           `(transform . ,(format #f "translate (~f, ~f)"
246                                  (* output-scale x)
247                                  (- (* output-scale y))))))
248
249 (define (polygon coords blot-diameter)
250   (entity 'polygon ""
251           '(stroke-linejoin . "round")
252           '(stroke-linecap . "round")
253           `(stroke-width . ,blot-diameter)
254           '(stroke . "black")
255           ;;'(fill . "black")
256           `(points . ,(string-join
257                        (map offset->point (ly:list->offsets '() coords))))
258           `(transform
259             . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
260
261 (define (round-filled-box breapth width depth height blot-diameter)
262   (entity 'rect ""
263           ;; The stroke will stick out.  To use stroke,
264           ;; the stroke-width must be subtracted from all other dimensions.
265           ;;'(stroke-linejoin . "round")
266           ;;'(stroke-linecap . "round")
267           ;;`(stroke-width . ,blot)
268           ;;'(stroke . "red")
269           ;;'(fill . "orange")
270
271           `(x . ,(- breapth))
272           `(y . ,(- height))
273           `(width . ,(+ breapth width))
274           `(height . ,(+ depth height))
275           `(ry . ,(/ blot-diameter 2))
276           `(transform
277             . ,(format #f "scale (~f, ~f)" output-scale output-scale))))
278
279 (define (text font string)
280   (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
281
282 (define (utf8-string pango-font-description string)
283   (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))