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 "([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
116 (define pango-description-regexp-nocomma
117 (make-regexp "([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
119 (define (pango-description-to-svg-font str)
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! family (match:substring match 1))
133 (if (< 0 (string-length (match:substring match 2)))
134 (set! style (match:substring match 2)))
136 (string->number (match:substring match 3))))
138 (ly:warning (_ "cannot decypher Pango description: ~a") str))
142 (format "font-style:~a;" style)
145 (format "font-family:~a;~afont-size:~a;text-anchor:west"
148 (/ size lily-unit-length))
151 ;;; FONT may be font smob, or pango font string
152 (define (svg-font font)
154 (pango-description-to-svg-font font)
155 (let ((name-style (font-name-style font))
156 (size (modified-font-metric-font-scaling font))
159 (format "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
160 (car name-style) (cadr name-style)
163 (define (fontify font expr)
165 `(style . ,(svg-font font))
166 '(fill . "currentColor")
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170 ;;; stencil outputters
173 ;;; catch-all for missing stuff
174 ;;; comment this out to see find out what functions you miss :-)
178 (define (dummy . foo) "")
179 (map (lambda (x) (module-define! this-module x dummy))
181 (ly:all-stencil-expressions)
182 (ly:all-output-backend-commands)))
185 (define (url-link url x y)
187 (eo 'a `(xlink:href . ,url))
191 `(width . ,(- (cdr x) (car x)))
192 `(height . ,(- (cdr y) (car y)))
195 '(stroke-width . "0.0"))
198 (define (grob-cause offset grob)
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 . "currentColor")
214 '(fill . "currentColor")
215 `(stroke-width . ,thick)
216 `(d . ,(string-append (svg-bezier first #f)
217 (svg-bezier second first-c0)))
220 (define (path thick commands)
221 (define (convert-path-exps exps)
228 ((memq head '(rmoveto rlineto lineto moveto)) 2)
229 ((memq head '(rcurveto curveto)) 6)
231 (args (take rest arity))
232 (svg-head (assoc-get head '((rmoveto . m)
241 (cons (format "~a~a "
242 svg-head (number-list->point args)
244 (convert-path-exps (drop rest arity))))
248 `(stroke-width . ,thick)
249 '(stroke-linejoin . "round")
250 '(stroke-linecap . "round")
251 '(stroke . "currentColor")
253 `(d . ,(string-join (convert-path-exps commands) " "))))
255 (define (char font i)
257 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
259 (define-public (comment s)
260 (string-append "<!-- " s " !-->\n"))
262 (define (draw-line thick x1 y1 x2 y2 . alist)
264 (apply entity 'line ""
266 `((stroke-linejoin . "round")
267 (stroke-linecap . "round")
268 (stroke-width . ,thick)
269 (stroke . "currentColor")
276 (define (dashed-line thick on off dx dy phase)
277 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
279 (define (named-glyph font name)
281 `(fontify ,font ,(entity 'tspan
283 (ly:font-glyph-name-to-charcode font name))))))
285 (define (placebox x y expr)
287 ((match (regexp-exec svg-element-regexp expr))
288 (tagname (match:substring match 1))
289 (attributes (match:substring match 2)))
291 (string-append tagname
292 ;; FIXME: Not using GNU coding standards
293 ;; [translate ()] here to work around a
294 ;; bug in Microsoft Internet Explorer 6.0
295 (ly:format " transform=\"translate(~f, ~f)\" " x (- y))
299 (define (polygon coords blot-diameter is-filled)
302 '(stroke-linejoin . "round")
303 '(stroke-linecap . "round")
304 `(stroke-width . ,blot-diameter)
305 `(fill . ,(if is-filled "currentColor" "none"))
306 '(stroke . "currentColor")
307 `(points . ,(string-join
308 (map offset->point (ly:list->offsets '() coords))))
311 ;; rotate around given point
312 (define (setrotation ang x y)
313 (format "<g transform=\"rotate(~a,~a,~a)\">\n"
314 (number->string (* -1 ang))
316 (number->string (* -1 y))))
318 (define (resetrotation ang x y)
321 (define (round-filled-box breapth width depth height blot-diameter)
323 ;; The stroke will stick out. To use stroke,
324 ;; the stroke-width must be subtracted from all other dimensions.
325 ;;'(stroke-linejoin . "round")
326 ;;'(stroke-linecap . "round")
327 ;;`(stroke-width . ,blot)
333 `(width . ,(+ breapth width))
334 `(height . ,(+ depth height))
335 `(ry . ,(/ blot-diameter 2))
338 (define (circle radius thick is-filled)
341 '(stroke-linejoin . "round")
342 '(stroke-linecap . "round")
343 `(fill . ,(if is-filled "currentColor" "none"))
344 `(stroke . "currentColor")
345 `(stroke-width . ,thick)
348 (define (ellipse x-radius y-radius thick is-filled)
351 '(stroke-linejoin . "round")
352 '(stroke-linecap . "round")
353 `(fill . ,(if is-filled "currentColor" "none"))
354 `(stroke . "currentColor")
355 `(stroke-width . ,thick)
359 (define (oval x-radius y-radius thick is-filled)
360 (let ((x-max x-radius)
363 (y-min (- y-radius)))
366 '(stroke-linejoin . "round")
367 '(stroke-linecap . "round")
368 `(fill . ,(if is-filled "currentColor" "none"))
369 `(stroke . "currentColor")
370 `(stroke-width . ,thick)
371 `(d . ,(ly:format "M~4f,~4f C~4f,~4f ~4f,~4f ~4f,~4f S~4f,~4f ~4f,~4f"
379 (define (text font string)
380 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
382 (define (utf-8-string pango-font-description string)
383 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
387 (define (setcolor r g b)
388 (format "<g color=\"rgb(~a%,~a%,~a%)\">\n"
389 (* 100 r) (* 100 g) (* 100 b)