1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2002--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;; http://www.w3.org/TR/SVG11
8 ;;;; http://www.w3.org/TR/SVG12/ -- page, pageSet in draft
11 ;;;; * .cff MUST NOT be in fc's fontpath.
12 ;;;; - workaround: remove mf/out from ~/.fonts.conf,
13 ;;;; instead add ~/.fonts and symlink all /mf/out/*otf there.
14 ;;;; - bug in fontconfig/freetype/pango?
16 ;;;; * inkscape page/pageSet support
17 ;;;; * inkscape SVG-font support
18 ;;;; - use fontconfig/fc-cache for now, see output-gnome.scm
20 (define-module (scm output-svg))
21 (define this-module (current-module))
31 (define fancy-format format)
32 (define format ergonomic-simple-format)
34 (define lily-unit-length 1.75)
36 (define (dispatch expr)
37 (let ((keyword (car expr)))
39 ((eq? keyword 'some-func) "")
40 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
42 (if (module-defined? this-module keyword)
43 (apply (eval keyword this-module) (cdr expr))
45 (ly:warning (_ "undefined: ~S") keyword)
49 (define-public (attributes attributes-alist)
51 (map (lambda (x) (format " ~s=\"~a\"" (car x) (cdr x)))
54 (define-public (eo entity . attributes-alist)
56 (format "<~S~a>\n" entity (attributes attributes-alist)))
58 (define-public (eoc entity . attributes-alist)
60 (format "<~S~a/>\n" entity (attributes attributes-alist)))
62 (define-public (ec entity)
64 (format "</~S>\n" entity))
68 (define-public (entity entity string . attributes-alist)
69 (if (equal? string "")
70 (apply eoc entity attributes-alist)
72 (apply eo (cons entity attributes-alist)) string (ec entity))))
74 (define (offset->point o)
75 (format " ~S,~S" (car o) (- (cdr o))))
77 (define (number-list->point lst)
81 (cons (format "~S,~S" (car lst) (cadr lst))
82 (helper (cddr lst)))))
84 (string-join (helper lst) " "))
87 (define (svg-bezier lst close)
88 (let* ((c0 (car (list-tail lst 3)))
89 (c123 (list-head lst 3)))
91 (if (not close) "M " "L ")
93 "C " (apply string-append (map offset->point c123))
94 (if (not close) "" (string-append
95 "L " (offset->point close))))))
100 (define (integer->entity integer)
101 (fancy-format "&#x~x;" integer))
103 (define (char->entity char)
104 (integer->entity (char->integer char)))
106 (define (string->entities string)
108 (map (lambda (x) (char->entity x)) (string->list string))))
110 (define svg-element-regexp
111 (make-regexp "^(<[a-z]+) (.*>)"))
113 (define pango-description-regexp-comma
114 (make-regexp ",( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$"))
116 (define pango-description-regexp-nocomma
117 (make-regexp "( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$"))
119 (define (pango-description-to-svg-font str expr)
121 (define (set-attribute attr val)
122 (set! alist (assoc-set! alist attr val)))
124 ((match-1 (regexp-exec pango-description-regexp-comma str))
125 (match-2 (regexp-exec pango-description-regexp-nocomma str))
130 (if (regexp-match? match)
132 (set-attribute 'font-family (match:prefix match))
133 (if (string? (match:substring match 1))
134 (set-attribute 'font-weight "bold"))
135 (if (string? (match:substring match 2))
136 (set-attribute 'font-style "italic"))
137 (if (string? (match:substring match 3))
138 (set-attribute 'font-variant "small-caps"))
139 (set-attribute 'font-size
140 (/ (string->number (match:substring match 4))
142 (set-attribute 'text-anchor "start")
143 (set-attribute 'fill "currentColor"))
144 (ly:warning (_ "cannot decypher Pango description: ~a") str))
146 (apply entity 'text expr (reverse! alist))))
148 (define (font-smob-to-svg-font font expr)
149 (let ((name-style (font-name-style font))
150 (size (modified-font-metric-font-scaling font)))
153 ;; FIXME: The cdr of `name-style' cannot select the
154 ;; correct SVG font, so we ignore this information for now
155 `(font-family . ,(car name-style))
157 '(text-anchor . "start"))))
159 (define (fontify font expr)
161 (pango-description-to-svg-font font expr)
162 (font-smob-to-svg-font font expr)))
164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
165 ;;; stencil outputters
168 ;;; catch-all for missing stuff
169 ;;; comment this out to see find out what functions you miss :-)
173 (define (dummy . foo) "")
174 (map (lambda (x) (module-define! this-module x dummy))
176 (ly:all-stencil-expressions)
177 (ly:all-output-backend-commands)))
180 (define (url-link url x y)
182 (eo 'a `(xlink:href . ,url))
186 `(width . ,(- (cdr x) (car x)))
187 `(height . ,(- (cdr y) (car y)))
190 '(stroke-width . "0.0"))
193 (define (grob-cause offset grob)
201 (define (bezier-sandwich lst thick)
202 (let* ((first (list-tail lst 4))
203 (first-c0 (car (list-tail first 3)))
204 (second (list-head lst 4)))
206 '(stroke-linejoin . "round")
207 '(stroke-linecap . "round")
208 '(stroke . "currentColor")
209 '(fill . "currentColor")
210 `(stroke-width . ,thick)
211 `(d . ,(string-append (svg-bezier first #f)
212 (svg-bezier second first-c0)))
215 (define (path thick commands)
216 (define (convert-path-exps exps)
223 ((memq head '(rmoveto rlineto lineto moveto)) 2)
224 ((memq head '(rcurveto curveto)) 6)
226 (args (take rest arity))
227 (svg-head (assoc-get head '((rmoveto . m)
236 (cons (format "~a~a "
237 svg-head (number-list->point args)
239 (convert-path-exps (drop rest arity))))
243 `(stroke-width . ,thick)
244 '(stroke-linejoin . "round")
245 '(stroke-linecap . "round")
246 '(stroke . "currentColor")
248 `(d . ,(string-join (convert-path-exps commands) " "))))
250 (define (char font i)
252 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
254 (define-public (comment s)
255 (string-append "<!-- " s " !-->\n"))
257 (define (draw-line thick x1 y1 x2 y2 . alist)
259 (apply entity 'line ""
261 `((stroke-linejoin . "round")
262 (stroke-linecap . "round")
263 (stroke-width . ,thick)
264 (stroke . "currentColor")
271 (define (dashed-line thick on off dx dy phase)
272 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
274 (define (named-glyph font name)
276 `(fontify ,font ,(entity 'tspan
278 (ly:font-glyph-name-to-charcode font name))))))
280 (define (placebox x y expr)
282 ((match (regexp-exec svg-element-regexp expr))
283 (tagname (match:substring match 1))
284 (attributes (match:substring match 2)))
286 (string-append tagname
287 ;; FIXME: Not using GNU coding standards
288 ;; [translate ()] here to work around a
289 ;; bug in Microsoft Internet Explorer 6.0
290 (ly:format " transform=\"translate(~f, ~f)\" " x (- y))
294 (define (polygon coords blot-diameter is-filled)
297 '(stroke-linejoin . "round")
298 '(stroke-linecap . "round")
299 `(stroke-width . ,blot-diameter)
300 `(fill . ,(if is-filled "currentColor" "none"))
301 '(stroke . "currentColor")
302 `(points . ,(string-join
303 (map offset->point (ly:list->offsets '() coords))))
306 ;; rotate around given point
307 (define (setrotation ang x y)
308 (format "<g transform=\"rotate(~a,~a,~a)\">\n"
309 (number->string (* -1 ang))
311 (number->string (* -1 y))))
313 (define (resetrotation ang x y)
316 (define (round-filled-box breapth width depth height blot-diameter)
318 ;; The stroke will stick out. To use stroke,
319 ;; the stroke-width must be subtracted from all other dimensions.
320 ;;'(stroke-linejoin . "round")
321 ;;'(stroke-linecap . "round")
322 ;;`(stroke-width . ,blot)
328 `(width . ,(+ breapth width))
329 `(height . ,(+ depth height))
330 `(ry . ,(/ blot-diameter 2))
331 '(fill . "currentColor")
334 (define (circle radius thick is-filled)
337 '(stroke-linejoin . "round")
338 '(stroke-linecap . "round")
339 `(fill . ,(if is-filled "currentColor" "none"))
340 `(stroke . "currentColor")
341 `(stroke-width . ,thick)
344 (define (ellipse x-radius y-radius thick is-filled)
347 '(stroke-linejoin . "round")
348 '(stroke-linecap . "round")
349 `(fill . ,(if is-filled "currentColor" "none"))
350 `(stroke . "currentColor")
351 `(stroke-width . ,thick)
355 (define (oval x-radius y-radius thick is-filled)
356 (let ((x-max x-radius)
359 (y-min (- y-radius)))
362 '(stroke-linejoin . "round")
363 '(stroke-linecap . "round")
364 `(fill . ,(if is-filled "currentColor" "none"))
365 `(stroke . "currentColor")
366 `(stroke-width . ,thick)
367 `(d . ,(ly:format "M~4f,~4f C~4f,~4f ~4f,~4f ~4f,~4f S~4f,~4f ~4f,~4f"
375 (define (text font string)
376 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
378 (define (utf-8-string pango-font-description string)
379 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
381 (define (embedded-svg string)
384 (define (setcolor r g b)
385 (format "<g color=\"rgb(~a%,~a%,~a%)\">\n"
386 (* 100 r) (* 100 g) (* 100 b)