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