]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
6b4cf35dcc2663a4e947aed3330e1e79f981b489
[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 ;;; FIXME
10
11 ;;; * sodipodi gets confuseed by dashes in font names.
12 ;;;
13 ;;;   removing feta-nummer*.pfa (LilyPond-feta-nummer),
14 ;;;   feta-braces*.pfa (LilyPond-feta-braces), feta-din*.pfa
15 ;;;   (LilyPond-feta-din) from font path shows feta fonts in sodipodi.
16 ;;;
17 ;;; * inkscape fails to map Feta fonts to private use area (PUA) E000
18 ;;;   (sodipodi is fine).
19
20 ;;; * maybe we need to have a unicode mapping somehow, we could
21 ;;;   - use OpenType instead of Type1
22 ;;; http://lists.gnu.org/archive/html/lilypond-devel/2004-05/msg00098.html
23 ;;;
24 ;;;   - or fix the pangofc-afm-decoder and add it to Pango (no chance?)
25 ;;;     or have fontconfig read AFM files
26 ;;;  http://lists.gnu.org/archive/html/lilypond-devel/2004-05/msg00103.html
27
28
29 (debug-enable 'backtrace)
30 (define-module (scm output-svg))
31 (define this-module (current-module))
32
33 (use-modules
34  (guile)
35  (ice-9 regex)
36  (lily))
37
38 ;; GLobals
39 ;; FIXME: 2?
40 (define output-scale (* 2 scale-to-unit))
41
42 (define (stderr string . rest)
43   (apply format (cons (current-error-port) (cons string rest)))
44   (force-output (current-error-port)))
45
46 (define (debugf string . rest)
47   (if #f
48       (apply stderr (cons string rest))))
49
50
51 (define (dispatch expr)
52   (let ((keyword (car expr)))
53     (cond
54      ((eq? keyword 'some-func) "")
55      ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
56      (else
57       (if (module-defined? this-module keyword)
58           (apply (eval keyword this-module) (cdr expr))
59           (begin
60             (display
61              (string-append "undefined: " (symbol->string keyword) "\n"))
62             ""))))))
63   
64 ;; Helper functions
65 (define (tagify tag string . attribute-alist)
66   (string-append
67    "<"
68    tag
69    (apply string-append
70           (map (lambda (x)
71                  (string-append " " (symbol->string (car x)) "='" (cdr x) "'"))
72                attribute-alist))
73    ">"
74    string "</" tag ">\n"))
75
76 (define (control->list c)
77   (list (car c) (cdr c)))
78
79 (define (control->string c)
80   (string-append
81    (number->string (car c)) ","
82    ;; lose the -1
83    (number->string (* -1 (cdr c))) " "))
84
85 (define (control-flip-y c)
86   (cons (car c) (* -1 (cdr c))))
87
88 (define (ly:numbers->string lst)
89   (string-append
90    (number->string (car lst))
91    (if (null? (cdr lst))
92        ""
93        (string-append "," (ly:numbers->string (cdr lst))))))
94
95 (define (svg-bezier lst close)
96   (let* ((c0 (car (list-tail lst 3)))
97          (c123 (list-head lst 3)))
98     (string-append
99      (if (not close) "M " "L ")
100      (control->string c0)
101      "C " (apply string-append (map control->string c123))
102      (if (not close) "" (string-append
103                          "L " (control->string close))))))
104
105 (define (sqr x)
106   (* x x))
107
108 (define (font-size font)
109   (let* ((designsize (ly:font-design-size font))
110          (magnification (* (ly:font-magnification font)))
111          (ops 2)
112          (scaling (* ops magnification designsize)))
113     (debugf "scaling:~S\n" scaling)
114     (debugf "magnification:~S\n" magnification)
115     (debugf "design:~S\n" designsize)
116     scaling))
117
118 (define (char->entity font char)
119   (format #f "&#x~x;" (char->unicode-index font char)))
120                    
121 (define (string->entities font string)
122   (apply string-append
123          (map (lambda (x) (char->entity font x)) (string->list string))))
124
125 (define (svg-font font)
126   (let* ((encoding (ly:font-encoding font))
127          (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start)))
128    (format #f "font-family:~a;font-size:~a;text-anchor:~S;"
129            (font-family font) (font-size font) anchor)))
130
131 (define (fontify font expr)
132    (tagify "text" expr (cons 'style (svg-font font))))
133
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135
136
137 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 ;;; stencil outputters
139 ;;;
140
141 ;;; catch-all for missing stuff
142 ;;; comment this out to see find out what functions you miss :-)
143 (define (dummy . foo) "")
144 (map (lambda (x) (module-define! this-module x dummy))
145      (append
146       (ly:all-stencil-expressions)
147       (ly:all-output-backend-commands)))
148
149 (define (beam width slope thick blot)
150   (let* ((x width)
151          (y (* slope width))
152          (z (sqrt (+ (sqr x) (sqr y)))))
153     (tagify "rect" ""
154             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
155             `(x . "0")
156             `(y . ,(number->string (* output-scale (- 0 (/ thick 2)))))
157             `(width . ,(number->string (* output-scale width)))
158             `(height . ,(number->string (* output-scale thick)))
159             `(ry . ,(number->string (* output-scale (/ blot 2))))
160             `(transform .
161                         ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
162                                  (/ x z)
163                                  (* -1 (/ y z))
164                                  1 1)))))
165
166 (define (bezier-sandwich lst thick)
167   (let* ((first (list-tail lst 4))
168          (first-c0 (car (list-tail first 3)))
169          (second (list-head lst 4)))
170     (tagify "path" ""
171             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" thick))
172             `(transform . ,(format #f "scale (~f, ~f)"
173                                    output-scale output-scale))
174             `(d . ,(string-append (svg-bezier first #f)
175                                   (svg-bezier second first-c0))))))
176
177 (define (char font i)
178   (dispatch
179    `(fontify ,font ,(tagify "tspan" (char->entity font (integer->char i))))))
180
181 (define (comment s)
182   (string-append "<!-- " s " !-->\n"))
183
184 (define (filledbox breapth width depth height)
185   (round-filled-box breapth width depth height 0))
186
187 (define (placebox x y expr)
188   (tagify "g"
189           ;; FIXME -- JCN
190           ;;(dispatch expr)
191           expr
192           `(transform . ,(format #f "translate (~f, ~f)"
193                                  (* output-scale x)
194                                  (- 0 (* output-scale y))))))
195
196 (define (round-filled-box breapth width depth height blot-diameter)
197   (tagify "rect" ""
198           `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot-diameter))
199           `(x . ,(number->string (* output-scale (- 0 breapth))))
200           `(y . ,(number->string (* output-scale (- 0 height))))
201           `(width . ,(number->string (* output-scale (+ breapth width))))
202           `(height . ,(number->string (* output-scale (+ depth height))))
203           `(ry . ,(number->string (/ blot-diameter 2)))))
204
205 (define (text font string)
206   (dispatch `(fontify ,font ,(tagify "tspan" (string->entities font string)))))
207
208 ;; WTF is this in every backend?
209 (define (horizontal-line x1 x2 th)
210   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))