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 ;;;; * inkscape page/pageSet support
11 ;;;; * inkscape SVG-font support
12 ;;;; - use fontconfig/fc-cache for now, see output-gnome.scm
14 (debug-enable 'backtrace)
15 (define-module (scm output-svg))
16 (define this-module (current-module))
26 (define output-scale (* 2 scale-to-unit))
28 (define (debugf string . rest)
30 (apply stderr (cons string rest))))
32 (define (dispatch expr)
33 (let ((keyword (car expr)))
35 ((eq? keyword 'some-func) "")
36 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
38 (if (module-defined? this-module keyword)
39 (apply (eval keyword this-module) (cdr expr))
42 (string-append "undefined: " (symbol->string keyword) "\n"))
46 (define-public (attributes attributes-alist)
48 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
51 (define-public (eo entity . attributes-alist)
52 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
54 (define-public (eoc entity . attributes-alist)
55 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
57 (define-public (ec entity)
58 (format #f "</~S>\n" entity))
60 (define-public (entity entity string . attributes-alist)
61 (if (equal? string "")
62 (apply eoc entity attributes-alist)
64 (apply eo (cons entity attributes-alist)) string (ec entity))))
66 (define (offset->point o)
67 (format #f " ~S,~S" (car o) (cdr o)))
69 (define (svg-bezier lst close)
70 (let* ((c0 (car (list-tail lst 3)))
71 (c123 (list-head lst 3)))
73 (if (not close) "M " "L ")
75 "C " (apply string-append (map offset->point c123))
76 (if (not close) "" (string-append
77 "L " (offset->point close))))))
82 (define (font-size font)
83 (let* ((designsize (ly:font-design-size font))
84 (magnification (* (ly:font-magnification font)))
86 (scaling (* ops magnification designsize)))
87 (debugf "scaling:~S\n" scaling)
88 (debugf "magnification:~S\n" magnification)
89 (debugf "design:~S\n" designsize)
92 (define (integer->entity integer)
93 (format #f "&#x~x;" integer))
95 (define (char->entity char)
96 (integer->entity (char->integer char)))
98 (define (string->entities string)
100 (map (lambda (x) (char->entity x)) (string->list string))))
102 (define (svg-font font)
103 (let* ((encoding (ly:font-encoding font))
104 (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start))
105 (family (font-family font)))
106 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~S;"
107 (otf-name-mangling font family)
108 (otf-style-mangling font family)
109 (font-size font) anchor)))
111 (define (fontify font expr)
112 (entity 'text expr (cons 'style (svg-font font))))
115 (define-public (otf-name-mangling font family)
116 ;; Hmm, family is bigcheese20/26?
117 (if (string=? (substring family 0 (min (string-length family) 9))
120 (if (string=? family "aybabtu")
124 (define-public (otf-style-mangling font family)
125 ;; Hmm, family is bigcheese20/26?
126 (if (string=? (substring family 0 (min (string-length family) 9))
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ;;; stencil outputters
135 ;;; catch-all for missing stuff
136 ;;; comment this out to see find out what functions you miss :-)
137 (define (dummy . foo) "")
138 (map (lambda (x) (module-define! this-module x dummy))
140 (ly:all-stencil-expressions)
141 (ly:all-output-backend-commands)))
143 (define (rect-beam width slope thick blot-diameter)
148 ;; The stroke will stick out. To use stroke,
149 ;; the stroke-width must be subtracted from all other dimensions.
150 ;;'(stroke-linejoin . "round")
151 ;;'(stroke-linecap . "round")
152 ;;`(stroke-width . ,blot-diameter)
157 `(y . ,(- (/ thick 2)))
159 `(height . ,(+ thick (* (abs z) (/ thick 2))))
160 `(rx . ,(/ blot-diameter 2))
161 `(transform . ,(string-append
162 (format #f "matrix (1, ~f, 0, 1, 0, 0)" (- z))
163 (format #f " scale (~f, ~f)"
164 output-scale output-scale))))))
166 (define (beam width slope thick blot-diameter)
167 (let* ((b blot-diameter)
172 '(stroke-linejoin . "round")
173 '(stroke-linecap . "round")
174 `(stroke-width . ,blot-diameter)
177 `(points . ,(string-join
179 (list (cons (/ b 2) (/ t 2))
180 (cons (+ w (/ b 2)) (+ h (/ t 2)))
181 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
182 (cons (/ b 2) (- (/ t 2)))))))
184 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
186 (define (path-beam width slope thick blot-diameter)
187 (let* ((b blot-diameter)
192 '(stroke-linejoin . "round")
193 '(stroke-linecap . "round")
194 `(stroke-width . ,blot-diameter)
197 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
204 . ,(format #f "scale (~f, ~f)" output-scale output-scale)))))
206 (define (bezier-sandwich lst thick)
207 (let* ((first (list-tail lst 4))
208 (first-c0 (car (list-tail first 3)))
209 (second (list-head lst 4)))
211 '(stroke-linejoin . "round")
212 '(stroke-linecap . "round")
213 `(stroke-width . ,thick)
216 `(d . ,(string-append (svg-bezier first #f)
217 (svg-bezier second first-c0)))
219 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
221 (define (char font i)
223 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
225 (define-public (comment s)
226 (string-append "<!-- " s " !-->\n"))
228 (define (dashed-line thick on off dx dy)
229 (draw-line thick 0 0 dx dy))
231 (define (draw-line thick x1 y1 x2 y2)
233 '(stroke-linejoin . "round")
234 '(stroke-linecap . "round")
235 `(stroke-width . ,thick)
243 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
245 ;; WTF is this in every backend?
246 (define (horizontal-line x1 x2 th)
247 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
249 (define (filledbox breapth width depth height)
250 (round-filled-box breapth width depth height 0))
252 (define (named-glyph font name)
254 `(fontify ,font ,(entity 'tspan
256 (ly:font-glyph-name-to-charcode font name))))))
258 (define (placebox x y expr)
263 `(transform . ,(format #f "translate (~f, ~f)"
265 (- (* output-scale y))))))
267 (define (polygon coords blot-diameter)
269 '(stroke-linejoin . "round")
270 '(stroke-linecap . "round")
271 `(stroke-width . ,blot-diameter)
274 `(points . ,(string-join
275 (map offset->point (ly:list->offsets '() coords))))
277 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
279 (define (round-filled-box breapth width depth height blot-diameter)
281 ;; The stroke will stick out. To use stroke,
282 ;; the stroke-width must be subtracted from all other dimensions.
283 ;;'(stroke-linejoin . "round")
284 ;;'(stroke-linecap . "round")
285 ;;`(stroke-width . ,blot)
291 `(width . ,(+ breapth width))
292 `(height . ,(+ depth height))
293 `(ry . ,(/ blot-diameter 2))
295 . ,(format #f "scale (~f, ~f)" output-scale output-scale))))
297 (define (text font string)
298 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))