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
10 ;;;; * missing stencils: line, dashed-line ...
11 ;;;; * rounded corners on stencils: rect, bezier (inkscape bug?)
12 ;;;; * inkscape page/pageSet support
14 (debug-enable 'backtrace)
15 (define-module (scm output-svg))
16 (define this-module (current-module))
25 (define output-scale (* 2 scale-to-unit))
27 (define indent-level 0)
28 (define (indent s . add) s)
30 ;;(define (indentation indent str)
31 ;; (regexp-substitute/global #f "\(\n\)[ \t]*" str 'pre 1 indent 'post))
33 (define (indent s . add)
34 (let ((before indent-level)
35 (after (apply + (cons indent-level add)))
36 (after? (and (not (null? add)) (> (car add) 0))))
37 (set! indent-level after)
39 (string-append (make-string before #\ ) s)
40 (string-append (make-string after #\ ) s))))
42 (define (debugf string . rest)
44 (apply stderr (cons string rest))))
46 (define (dispatch expr)
47 (let ((keyword (car expr)))
49 ((eq? keyword 'some-func) "")
50 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
52 (if (module-defined? this-module keyword)
53 (apply (eval keyword this-module) (cdr expr))
56 (string-append "undefined: " (symbol->string keyword) "\n"))
60 (define-public (attributes attributes-alist)
62 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
65 (define-public (eo entity . attributes-alist)
66 (indent (format #f "<~S~a>\n" entity (attributes attributes-alist)) 2))
68 (define-public (eoc entity . attributes-alist)
69 (indent (format #f "<~S~a/>\n" entity (attributes attributes-alist))))
71 (define-public (ec entity)
72 (indent (format #f "</~S>\n" entity) -2))
74 (define-public (entity entity string . attributes-alist)
75 (if (equal? string "")
76 (apply eoc entity attributes-alist)
78 (apply eo (cons entity attributes-alist)) string (ec entity))))
80 (define (control->list c)
81 (list (car c) (cdr c)))
83 (define (control->string c)
85 (number->string (car c)) ","
87 (number->string (* -1 (cdr c))) " "))
89 (define (control-flip-y c)
90 (cons (car c) (* -1 (cdr c))))
92 (define (ly:numbers->string lst)
94 (number->string (car lst))
97 (string-append "," (ly:numbers->string (cdr lst))))))
99 (define (svg-bezier lst close)
100 (let* ((c0 (car (list-tail lst 3)))
101 (c123 (list-head lst 3)))
103 (if (not close) "M " "L ")
105 "C " (apply string-append (map control->string c123))
106 (if (not close) "" (string-append
107 "L " (control->string close))))))
112 (define (font-size font)
113 (let* ((designsize (ly:font-design-size font))
114 (magnification (* (ly:font-magnification font)))
116 (scaling (* ops magnification designsize)))
117 (debugf "scaling:~S\n" scaling)
118 (debugf "magnification:~S\n" magnification)
119 (debugf "design:~S\n" designsize)
122 (define (integer->entity integer)
123 (format #f "&#x~x;" integer))
125 (define (char->entity char)
126 (integer->entity (char->integer char)))
128 (define (string->entities string)
130 (map (lambda (x) (char->entity x)) (string->list string))))
132 (define (svg-font font)
133 (let* ((encoding (ly:font-encoding font))
134 (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start))
135 (family (font-family font)))
136 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~S;"
137 (otf-name-mangling font family)
138 (otf-style-mangling font family)
139 (font-size font) anchor)))
141 (define (fontify font expr)
142 (entity 'text expr (cons 'style (svg-font font))))
145 (define-public (otf-name-mangling font family)
146 ;; Hmm, family is bigcheese20/26?
147 (if (string=? (substring family 0 (min (string-length family) 9))
150 (if (string=? family "aybabtu")
154 (define-public (otf-style-mangling font family)
155 ;; Hmm, family is bigcheese20/26?
156 (if (string=? (substring family 0 (min (string-length family) 9))
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 ;;; stencil outputters
165 ;;; catch-all for missing stuff
166 ;;; comment this out to see find out what functions you miss :-)
167 (define (dummy . foo) "")
168 (map (lambda (x) (module-define! this-module x dummy))
170 (ly:all-stencil-expressions)
171 (ly:all-output-backend-commands)))
173 (define (beam width slope thick blot)
176 (z (sqrt (+ (sqr x) (sqr y)))))
178 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
180 `(y . ,(number->string (* output-scale (- 0 (/ thick 2)))))
181 `(width . ,(number->string (* output-scale width)))
182 `(height . ,(number->string (* output-scale thick)))
183 `(ry . ,(number->string (* output-scale (/ blot 2))))
185 ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
190 (define (bezier-sandwich lst thick)
191 (let* ((first (list-tail lst 4))
192 (first-c0 (car (list-tail first 3)))
193 (second (list-head lst 4)))
195 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" thick))
196 `(transform . ,(format #f "scale (~f, ~f)"
197 output-scale output-scale))
198 `(d . ,(string-append (svg-bezier first #f)
199 (svg-bezier second first-c0))))))
201 (define (char font i)
203 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
205 (define-public (comment s)
206 (string-append "<!-- " s " !-->\n"))
208 (define (filledbox breapth width depth height)
209 (round-filled-box breapth width depth height 0))
211 (define (named-glyph font name)
213 `(fontify ,font ,(entity 'tspan
215 (ly:font-glyph-name-to-charcode font name))))))
217 (define (placebox x y expr)
222 `(transform . ,(format #f "translate (~f, ~f)"
224 (- 0 (* output-scale y))))))
226 (define (round-filled-box breapth width depth height blot-diameter)
228 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot-diameter))
229 `(x . ,(number->string (* output-scale (- 0 breapth))))
230 `(y . ,(number->string (* output-scale (- 0 height))))
231 `(width . ,(number->string (* output-scale (+ breapth width))))
232 `(height . ,(number->string (* output-scale (+ depth height))))
233 `(ry . ,(number->string (/ blot-diameter 2)))))
235 (define (text font string)
236 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
238 ;; WTF is this in every backend?
239 (define (horizontal-line x1 x2 th)
240 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))