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 (tagify tag string . attribute-alist)
50 (string-append " " (symbol->string (car x)) "='" (cdr x) "'"))
53 string "</" tag ">\n"))
55 (define (control->list c)
56 (list (car c) (cdr c)))
58 (define (control->string c)
60 (number->string (car c)) ","
62 (number->string (* -1 (cdr c))) " "))
64 (define (control-flip-y c)
65 (cons (car c) (* -1 (cdr c))))
67 (define (ly:numbers->string lst)
69 (number->string (car lst))
72 (string-append "," (ly:numbers->string (cdr lst))))))
74 (define (svg-bezier lst close)
75 (let* ((c0 (car (list-tail lst 3)))
76 (c123 (list-head lst 3)))
78 (if (not close) "M " "L ")
80 "C " (apply string-append (map control->string c123))
81 (if (not close) "" (string-append
82 "L " (control->string close))))))
87 (define (font-size font)
88 (let* ((designsize (ly:font-design-size font))
89 (magnification (* (ly:font-magnification font)))
91 (scaling (* ops magnification designsize)))
92 (debugf "scaling:~S\n" scaling)
93 (debugf "magnification:~S\n" magnification)
94 (debugf "design:~S\n" designsize)
97 (define (integer->entity integer)
98 (format #f "&#x~x;" integer))
100 (define (char->entity char)
101 (integer->entity (char->integer char)))
103 (define (string->entities string)
105 (map (lambda (x) (char->entity x)) (string->list string))))
107 (define (svg-font font)
108 (let* ((encoding (ly:font-encoding font))
109 (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start))
110 (family (font-family font)))
111 (format #f "font-family:~a;font-weight:~a;font-size:~a;text-anchor:~S;"
112 (otf-name-mangling font family)
113 (otf-weight-mangling font family)
114 (font-size font) anchor)))
116 (define (fontify font expr)
117 (tagify "text" expr (cons 'style (svg-font font))))
119 (define-public (otf-name-mangling font family)
120 ;; Hmm, family is bigcheese20/26?
121 (if (string=? (substring family 0 (min (string-length family) 9))
126 (define-public (otf-weight-mangling font family)
127 ;; Hmm, family is bigcheese20/26?
128 (if (string=? (substring family 0 (min (string-length family) 9))
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 ;;; stencil outputters
137 ;;; catch-all for missing stuff
138 ;;; comment this out to see find out what functions you miss :-)
139 (define (dummy . foo) "")
140 (map (lambda (x) (module-define! this-module x dummy))
142 (ly:all-stencil-expressions)
143 (ly:all-output-backend-commands)))
145 (define (beam width slope thick blot)
148 (z (sqrt (+ (sqr x) (sqr y)))))
150 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
152 `(y . ,(number->string (* output-scale (- 0 (/ thick 2)))))
153 `(width . ,(number->string (* output-scale width)))
154 `(height . ,(number->string (* output-scale thick)))
155 `(ry . ,(number->string (* output-scale (/ blot 2))))
157 ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
162 (define (bezier-sandwich lst thick)
163 (let* ((first (list-tail lst 4))
164 (first-c0 (car (list-tail first 3)))
165 (second (list-head lst 4)))
167 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" thick))
168 `(transform . ,(format #f "scale (~f, ~f)"
169 output-scale output-scale))
170 `(d . ,(string-append (svg-bezier first #f)
171 (svg-bezier second first-c0))))))
173 (define (char font i)
175 `(fontify ,font ,(tagify "tspan" (char->entity (integer->char i))))))
178 (string-append "<!-- " s " !-->\n"))
180 (define (filledbox breapth width depth height)
181 (round-filled-box breapth width depth height 0))
183 (define (named-glyph font name)
185 `(fontify ,font ,(tagify "tspan"
187 (ly:font-glyph-name-to-charcode font name))))))
189 (define (placebox x y expr)
194 `(transform . ,(format #f "translate (~f, ~f)"
196 (- 0 (* output-scale y))))))
198 (define (round-filled-box breapth width depth height blot-diameter)
200 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot-diameter))
201 `(x . ,(number->string (* output-scale (- 0 breapth))))
202 `(y . ,(number->string (* output-scale (- 0 height))))
203 `(width . ,(number->string (* output-scale (+ breapth width))))
204 `(height . ,(number->string (* output-scale (+ depth height))))
205 `(ry . ,(number->string (/ blot-diameter 2)))))
207 (define (text font string)
208 (dispatch `(fontify ,font ,(tagify "tspan" (string->entities string)))))
210 ;; WTF is this in every backend?
211 (define (horizontal-line x1 x2 th)
212 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))