1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2002--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;; http://www.w3.org/TR/SVG11
9 (debug-enable 'backtrace)
10 (define-module (scm output-svg))
11 (define this-module (current-module))
20 (define output-scale (* 2 scale-to-unit))
22 (define (stderr string . rest)
23 (apply format (cons (current-error-port) (cons string rest)))
24 (force-output (current-error-port)))
26 (define (debugf string . rest)
28 (apply stderr (cons string rest))))
30 (define (dispatch expr)
31 (let ((keyword (car expr)))
33 ((eq? keyword 'some-func) "")
34 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
36 (if (module-defined? this-module keyword)
37 (apply (eval keyword this-module) (cdr expr))
40 (string-append "undefined: " (symbol->string keyword) "\n"))
44 (define-public (attributes attributes-alist)
46 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
49 (define-public (eo entity . attributes-alist)
50 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
52 (define-public (eoc entity . attributes-alist)
53 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
55 (define-public (ec entity)
56 (format #f "</~S>\n" entity))
58 (define-public (entity entity string . attributes-alist)
59 (if (equal? string "")
60 (apply eoc entity attributes-alist)
62 (apply eo (cons entity attributes-alist)) string (ec entity))))
64 (define (control->list c)
65 (list (car c) (cdr c)))
67 (define (control->string c)
69 (number->string (car c)) ","
71 (number->string (* -1 (cdr c))) " "))
73 (define (control-flip-y c)
74 (cons (car c) (* -1 (cdr c))))
76 (define (ly:numbers->string lst)
78 (number->string (car lst))
81 (string-append "," (ly:numbers->string (cdr lst))))))
83 (define (svg-bezier lst close)
84 (let* ((c0 (car (list-tail lst 3)))
85 (c123 (list-head lst 3)))
87 (if (not close) "M " "L ")
89 "C " (apply string-append (map control->string c123))
90 (if (not close) "" (string-append
91 "L " (control->string close))))))
96 (define (font-size font)
97 (let* ((designsize (ly:font-design-size font))
98 (magnification (* (ly:font-magnification font)))
100 (scaling (* ops magnification designsize)))
101 (debugf "scaling:~S\n" scaling)
102 (debugf "magnification:~S\n" magnification)
103 (debugf "design:~S\n" designsize)
106 (define (integer->entity integer)
107 (format #f "&#x~x;" integer))
109 (define (char->entity char)
110 (integer->entity (char->integer char)))
112 (define (string->entities string)
114 (map (lambda (x) (char->entity x)) (string->list string))))
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)))
125 (define (fontify font expr)
126 (entity 'text expr (cons 'style (svg-font font))))
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))
134 (if (string=? family "aybabtu")
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))
145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146 ;;; stencil outputters
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))
154 (ly:all-stencil-expressions)
155 (ly:all-output-backend-commands)))
157 (define (beam width slope thick blot)
160 (z (sqrt (+ (sqr x) (sqr y)))))
162 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
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))))
169 ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
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)))
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))))))
185 (define (char font i)
187 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
189 (define-public (comment s)
190 (string-append "<!-- " s " !-->\n"))
192 (define (filledbox breapth width depth height)
193 (round-filled-box breapth width depth height 0))
195 (define (named-glyph font name)
197 `(fontify ,font ,(entity 'tspan
199 (ly:font-glyph-name-to-charcode font name))))))
201 (define (placebox x y expr)
206 `(transform . ,(format #f "translate (~f, ~f)"
208 (- 0 (* output-scale y))))))
210 (define (round-filled-box breapth width depth height blot-diameter)
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)))))
219 (define (text font string)
220 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
222 ;; WTF is this in every backend?
223 (define (horizontal-line x1 x2 th)
224 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))