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
8 ;;;; http://www.w3.org/TR/SVG12/ -- page, pageSet in draft
11 ;;;; * inkscape page/pageSet support
12 ;;;; * inkscape SVG-font support
13 ;;;; - use fontconfig/fc-cache for now, see output-gnome.scm
15 (debug-enable 'backtrace)
16 (define-module (scm output-svg))
17 (define this-module (current-module))
27 (define output-scale (* 2 scale-to-unit))
29 (define (debugf string . rest)
31 (apply stderr (cons string rest))))
33 (define (dispatch expr)
34 (let ((keyword (car expr)))
36 ((eq? keyword 'some-func) "")
37 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
39 (if (module-defined? this-module keyword)
40 (apply (eval keyword this-module) (cdr expr))
43 (string-append "undefined: " (symbol->string keyword) "\n"))
47 (define-public (attributes attributes-alist)
49 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
52 (define-public (eo entity . attributes-alist)
53 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
55 (define-public (eoc entity . attributes-alist)
56 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
58 (define-public (ec entity)
59 (format #f "</~S>\n" entity))
61 (define-public (entity entity string . attributes-alist)
62 (if (equal? string "")
63 (apply eoc entity attributes-alist)
65 (apply eo (cons entity attributes-alist)) string (ec entity))))
67 (define (offset->point o)
68 (format #f " ~S,~S" (car o) (cdr o)))
70 (define (svg-bezier lst close)
71 (let* ((c0 (car (list-tail lst 3)))
72 (c123 (list-head lst 3)))
74 (if (not close) "M " "L ")
76 "C " (apply string-append (map offset->point c123))
77 (if (not close) "" (string-append
78 "L " (offset->point close))))))
83 (define (font-size font)
84 (let* ((designsize (ly:font-design-size font))
85 (magnification (* (ly:font-magnification font)))
87 (scaling (* ops magnification designsize)))
88 (debugf "scaling:~S\n" scaling)
89 (debugf "magnification:~S\n" magnification)
90 (debugf "design:~S\n" designsize)
93 (define (integer->entity integer)
94 (format #f "&#x~x;" integer))
96 (define (char->entity char)
97 (integer->entity (char->integer char)))
99 (define (string->entities string)
101 (map (lambda (x) (char->entity x)) (string->list string))))
103 (define (svg-font font)
104 (let* ((encoding (ly:font-encoding font))
105 (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start))
106 (family (font-family font)))
107 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~S;"
108 (otf-name-mangling font family)
109 (otf-style-mangling font family)
110 (font-size font) anchor)))
112 (define (fontify font expr)
113 (entity 'text expr (cons 'style (svg-font font))))
116 (define-public (otf-name-mangling font family)
117 ;; Hmm, family is bigcheese20/26?
118 (if (string=? (substring family 0 (min (string-length family) 9))
121 (if (string=? family "aybabtu")
125 (define-public (otf-style-mangling font family)
126 ;; Hmm, family is bigcheese20/26?
127 (if (string=? (substring family 0 (min (string-length family) 9))
132 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
133 ;;; stencil outputters
136 ;;; catch-all for missing stuff
137 ;;; comment this out to see find out what functions you miss :-)
138 (define (dummy . foo) "")
139 (map (lambda (x) (module-define! this-module x dummy))
141 (ly:all-stencil-expressions)
142 (ly:all-output-backend-commands)))
144 (define (rect-beam width slope thick blot-diameter)
149 ;; The stroke will stick out. To use stroke,
150 ;; the stroke-width must be subtracted from all other dimensions.
151 ;;'(stroke-linejoin . "round")
152 ;;'(stroke-linecap . "round")
153 ;;`(stroke-width . ,blot-diameter)
158 `(y . ,(- (/ thick 2)))
160 `(height . ,(+ thick (* (abs z) (/ thick 2))))
161 `(rx . ,(/ blot-diameter 2))
162 `(transform . ,(string-append
163 (format #f "matrix (1, ~f, 0, 1, 0, 0)" (- z))
164 (format #f " scale (~f, ~f)"
165 output-scale output-scale))))))
167 (define (beam width slope thick blot-diameter)
168 (let* ((b blot-diameter)
173 '(stroke-linejoin . "round")
174 '(stroke-linecap . "round")
175 `(stroke-width . ,blot-diameter)
178 `(points . ,(string-join
180 (list (cons (/ b 2) (/ t 2))
181 (cons (+ w (/ b 2)) (+ h (/ t 2)))
182 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
183 (cons (/ b 2) (- (/ t 2)))))))
185 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
187 (define (path-beam width slope thick blot-diameter)
188 (let* ((b blot-diameter)
193 '(stroke-linejoin . "round")
194 '(stroke-linecap . "round")
195 `(stroke-width . ,blot-diameter)
198 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
205 . ,(format #f "scale (~f, ~f)" output-scale output-scale)))))
207 (define (bezier-sandwich lst thick)
208 (let* ((first (list-tail lst 4))
209 (first-c0 (car (list-tail first 3)))
210 (second (list-head lst 4)))
212 '(stroke-linejoin . "round")
213 '(stroke-linecap . "round")
214 `(stroke-width . ,thick)
217 `(d . ,(string-append (svg-bezier first #f)
218 (svg-bezier second first-c0)))
220 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
222 (define (char font i)
224 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
226 (define-public (comment s)
227 (string-append "<!-- " s " !-->\n"))
229 (define (dashed-line thick on off dx dy)
230 (draw-line thick 0 0 dx dy))
232 (define (draw-line thick x1 y1 x2 y2)
234 '(stroke-linejoin . "round")
235 '(stroke-linecap . "round")
236 `(stroke-width . ,thick)
244 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
246 ;; WTF is this in every backend?
247 (define (horizontal-line x1 x2 th)
248 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
250 (define (filledbox breapth width depth height)
251 (round-filled-box breapth width depth height 0))
253 (define (named-glyph font name)
255 `(fontify ,font ,(entity 'tspan
257 (ly:font-glyph-name-to-charcode font name))))))
259 (define (placebox x y expr)
264 `(transform . ,(format #f "translate (~f, ~f)"
266 (- (* output-scale y))))))
268 (define (polygon coords blot-diameter)
270 '(stroke-linejoin . "round")
271 '(stroke-linecap . "round")
272 `(stroke-width . ,blot-diameter)
275 `(points . ,(string-join
276 (map offset->point (ly:list->offsets '() coords))))
278 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
280 (define (round-filled-box breapth width depth height blot-diameter)
282 ;; The stroke will stick out. To use stroke,
283 ;; the stroke-width must be subtracted from all other dimensions.
284 ;;'(stroke-linejoin . "round")
285 ;;'(stroke-linecap . "round")
286 ;;`(stroke-width . ,blot)
292 `(width . ,(+ breapth width))
293 `(height . ,(+ depth height))
294 `(ry . ,(/ blot-diameter 2))
296 . ,(format #f "scale (~f, ~f)" output-scale output-scale))))
298 (define (text font string)
299 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))