]> 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
9 (debug-enable 'backtrace)
10 (define-module (scm output-svg))
11 (define this-module (current-module))
12
13 (use-modules
14  (guile)
15  (ice-9 regex)
16  (lily))
17
18 ;; GLobals
19 ;; FIXME: 2?
20 (define output-scale (* 2 scale-to-unit))
21
22 (define (stderr string . rest)
23   (apply format (cons (current-error-port) (cons string rest)))
24   (force-output (current-error-port)))
25
26 (define (debugf string . rest)
27   (if #f
28       (apply stderr (cons string rest))))
29
30 (define (dispatch expr)
31   (let ((keyword (car expr)))
32     (cond
33      ((eq? keyword 'some-func) "")
34      ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
35      (else
36       (if (module-defined? this-module keyword)
37           (apply (eval keyword this-module) (cdr expr))
38           (begin
39             (display
40              (string-append "undefined: " (symbol->string keyword) "\n"))
41             ""))))))
42   
43 ;; Helper functions
44 (define-public (attributes attributes-alist)
45   (apply string-append
46          (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
47               attributes-alist)))
48
49 (define-public (eo entity . attributes-alist)
50   (format #f "<~S~a>\n" entity (attributes attributes-alist)))
51
52 (define-public (eoc entity . attributes-alist)
53   (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
54
55 (define-public (ec entity)
56   (format #f "</~S>\n" entity))
57
58 (define-public (entity entity string . attributes-alist)
59   (if (equal? string "")
60       (apply eoc entity attributes-alist)
61       (string-append
62        (apply eo (cons entity attributes-alist)) string (ec entity))))
63
64 (define (control->list c)
65   (list (car c) (cdr c)))
66
67 (define (control->string c)
68   (string-append
69    (number->string (car c)) ","
70    ;; lose the -1
71    (number->string (* -1 (cdr c))) " "))
72
73 (define (control-flip-y c)
74   (cons (car c) (* -1 (cdr c))))
75
76 (define (ly:numbers->string lst)
77   (string-append
78    (number->string (car lst))
79    (if (null? (cdr lst))
80        ""
81        (string-append "," (ly:numbers->string (cdr lst))))))
82
83 (define (svg-bezier lst close)
84   (let* ((c0 (car (list-tail lst 3)))
85          (c123 (list-head lst 3)))
86     (string-append
87      (if (not close) "M " "L ")
88      (control->string c0)
89      "C " (apply string-append (map control->string c123))
90      (if (not close) "" (string-append
91                          "L " (control->string close))))))
92
93 (define (sqr x)
94   (* x x))
95
96 (define (font-size font)
97   (let* ((designsize (ly:font-design-size font))
98          (magnification (* (ly:font-magnification font)))
99          (ops 2)
100          (scaling (* ops magnification designsize)))
101     (debugf "scaling:~S\n" scaling)
102     (debugf "magnification:~S\n" magnification)
103     (debugf "design:~S\n" designsize)
104     scaling))
105
106 (define (integer->entity integer)
107   (format #f "&#x~x;" integer))
108                    
109 (define (char->entity char)
110   (integer->entity (char->integer char)))
111                    
112 (define (string->entities string)
113   (apply string-append
114          (map (lambda (x) (char->entity x)) (string->list string))))
115
116 (define (svg-font font)
117   (let* ((encoding (ly:font-encoding font))
118          (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start))
119          (family (font-family font)))
120    (format #f "font-family:~a;font-weight:~a;font-size:~a;text-anchor:~S;"
121            (otf-name-mangling font family)
122            (otf-weight-mangling font family)
123            (font-size font) anchor)))
124
125 (define (fontify font expr)
126    (entity 'text expr (cons 'style (svg-font font))))
127
128 ;; FIXME
129 (define-public (otf-name-mangling font family)
130   ;; Hmm, family is bigcheese20/26?
131   (if (string=? (substring family 0 (min (string-length family) 9))
132                 "bigcheese")
133       "LilyPond"
134       (if (string=? family "aybabtu")
135           "LilyPondBraces"
136           family)))
137
138 (define-public (otf-weight-mangling font family)
139   ;; Hmm, family is bigcheese20/26?
140   (if (string=? (substring family 0 (min (string-length family) 9))
141                 "bigcheese")
142       (substring family 9)
143       "Regular"))
144
145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146 ;;; stencil outputters
147 ;;;
148
149 ;;; catch-all for missing stuff
150 ;;; comment this out to see find out what functions you miss :-)
151 (define (dummy . foo) "")
152 (map (lambda (x) (module-define! this-module x dummy))
153      (append
154       (ly:all-stencil-expressions)
155       (ly:all-output-backend-commands)))
156
157 (define (beam width slope thick blot)
158   (let* ((x width)
159          (y (* slope width))
160          (z (sqrt (+ (sqr x) (sqr y)))))
161     (entity 'rect ""
162             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
163             `(x . "0")
164             `(y . ,(number->string (* output-scale (- 0 (/ thick 2)))))
165             `(width . ,(number->string (* output-scale width)))
166             `(height . ,(number->string (* output-scale thick)))
167             `(ry . ,(number->string (* output-scale (/ blot 2))))
168             `(transform .
169                         ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
170                                  (/ x z)
171                                  (* -1 (/ y z))
172                                  1 1)))))
173
174 (define (bezier-sandwich lst thick)
175   (let* ((first (list-tail lst 4))
176          (first-c0 (car (list-tail first 3)))
177          (second (list-head lst 4)))
178     (entity 'path ""
179             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" thick))
180             `(transform . ,(format #f "scale (~f, ~f)"
181                                    output-scale output-scale))
182             `(d . ,(string-append (svg-bezier first #f)
183                                   (svg-bezier second first-c0))))))
184
185 (define (char font i)
186   (dispatch
187    `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
188
189 (define-public (comment s)
190   (string-append "<!-- " s " !-->\n"))
191
192 (define (filledbox breapth width depth height)
193   (round-filled-box breapth width depth height 0))
194
195 (define (named-glyph font name)
196   (dispatch
197    `(fontify ,font ,(entity 'tspan
198                             (integer->entity
199                              (ly:font-glyph-name-to-charcode font name))))))
200
201 (define (placebox x y expr)
202   (entity 'g
203           ;; FIXME -- JCN
204           ;;(dispatch expr)
205           expr
206           `(transform . ,(format #f "translate (~f, ~f)"
207                                  (* output-scale x)
208                                  (- 0 (* output-scale y))))))
209
210 (define (round-filled-box breapth width depth height blot-diameter)
211   (entity 'rect ""
212           `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot-diameter))
213           `(x . ,(number->string (* output-scale (- 0 breapth))))
214           `(y . ,(number->string (* output-scale (- 0 height))))
215           `(width . ,(number->string (* output-scale (+ breapth width))))
216           `(height . ,(number->string (* output-scale (+ depth height))))
217           `(ry . ,(number->string (/ blot-diameter 2)))))
218
219 (define (text font string)
220   (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
221
222 ;; WTF is this in every backend?
223 (define (horizontal-line x1 x2 th)
224   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))