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