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))
66 (define-public (comment s)
67 (string-append "<!-- " s " -->\n"))
69 (define-public (entity entity string . attributes-alist)
70 (if (equal? string "")
71 (apply eoc entity attributes-alist)
73 (apply eo (cons entity attributes-alist)) string (ec entity))))
75 (define (offset->point o)
76 (format " ~S,~S" (car o) (- (cdr o))))
78 (define (number-list->point lst)
82 (cons (format "~S,~S" (car lst) (cadr lst))
83 (helper (cddr lst)))))
85 (string-join (helper lst) " "))
88 (define (svg-bezier lst close)
89 (let* ((c0 (car (list-tail lst 3)))
90 (c123 (list-head lst 3)))
92 (if (not close) "M " "L ")
94 "C " (apply string-append (map offset->point c123))
95 (if (not close) "" (string-append
96 "L " (offset->point close))))))
101 (define (integer->entity integer)
102 (fancy-format "&#x~x;" integer))
104 (define (char->entity char)
105 (integer->entity (char->integer char)))
107 (define (string->entities string)
109 (map (lambda (x) (char->entity x)) (string->list string))))
111 (define svg-element-regexp
112 (make-regexp "^(<[a-z]+) (.*>)"))
114 (define pango-description-regexp-comma
115 (make-regexp ",( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$"))
117 (define pango-description-regexp-nocomma
118 (make-regexp "( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$"))
120 (define (pango-description-to-svg-font str expr)
122 (define (set-attribute attr val)
123 (set! alist (assoc-set! alist attr val)))
125 ((match-1 (regexp-exec pango-description-regexp-comma str))
126 (match-2 (regexp-exec pango-description-regexp-nocomma str))
131 (if (regexp-match? match)
133 (set-attribute 'font-family (match:prefix match))
134 (if (string? (match:substring match 1))
135 (set-attribute 'font-weight "bold"))
136 (if (string? (match:substring match 2))
137 (set-attribute 'font-style "italic"))
138 (if (string? (match:substring match 3))
139 (set-attribute 'font-variant "small-caps"))
140 (set-attribute 'font-size
141 (/ (string->number (match:substring match 4))
143 (set-attribute 'text-anchor "start")
144 (set-attribute 'fill "currentColor"))
145 (ly:warning (_ "cannot decypher Pango description: ~a") str))
147 (apply entity 'text expr (reverse! alist))))
149 (define (font-smob-to-svg-font font expr)
150 (let ((name-style (font-name-style font))
151 (size (modified-font-metric-font-scaling font)))
154 ;; FIXME: The cdr of `name-style' cannot select the
155 ;; correct SVG font, so we ignore this information for now
156 `(font-family . ,(car name-style))
158 '(text-anchor . "start"))))
160 (define (fontify font expr)
162 (pango-description-to-svg-font font expr)
163 (font-smob-to-svg-font font expr)))
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 ;;; stencil outputters
169 (define (bezier-sandwich lst thick)
170 (let* ((first (list-tail lst 4))
171 (first-c0 (car (list-tail first 3)))
172 (second (list-head lst 4)))
174 '(stroke-linejoin . "round")
175 '(stroke-linecap . "round")
176 '(stroke . "currentColor")
177 '(fill . "currentColor")
178 `(stroke-width . ,thick)
179 `(d . ,(string-append (svg-bezier first #f)
180 (svg-bezier second first-c0)))
183 (define (char font i)
185 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
187 (define (circle radius thick is-filled)
190 '(stroke-linejoin . "round")
191 '(stroke-linecap . "round")
192 `(fill . ,(if is-filled "currentColor" "none"))
193 `(stroke . "currentColor")
194 `(stroke-width . ,thick)
197 (define (dashed-line thick on off dx dy phase)
198 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
200 (define (draw-line thick x1 y1 x2 y2 . alist)
201 (apply entity 'line ""
203 `((stroke-linejoin . "round")
204 (stroke-linecap . "round")
205 (stroke-width . ,thick)
206 (stroke . "currentColor")
213 (define (ellipse x-radius y-radius thick is-filled)
216 '(stroke-linejoin . "round")
217 '(stroke-linecap . "round")
218 `(fill . ,(if is-filled "currentColor" "none"))
219 `(stroke . "currentColor")
220 `(stroke-width . ,thick)
224 (define (embedded-svg string)
227 (define (grob-cause offset grob)
230 (define (named-glyph font name)
232 `(fontify ,font ,(entity 'tspan
234 (ly:font-glyph-name-to-charcode font name))))))
239 (define (oval x-radius y-radius thick is-filled)
240 (let ((x-max x-radius)
243 (y-min (- y-radius)))
246 '(stroke-linejoin . "round")
247 '(stroke-linecap . "round")
248 `(fill . ,(if is-filled "currentColor" "none"))
249 `(stroke . "currentColor")
250 `(stroke-width . ,thick)
251 `(d . ,(ly:format "M~4f,~4f C~4f,~4f ~4f,~4f ~4f,~4f S~4f,~4f ~4f,~4f"
259 (define (path thick commands)
260 (define (convert-path-exps exps)
267 ((memq head '(rmoveto rlineto lineto moveto)) 2)
268 ((memq head '(rcurveto curveto)) 6)
270 (args (take rest arity))
271 (svg-head (assoc-get head '((rmoveto . m)
280 (cons (format "~a~a "
281 svg-head (number-list->point args)
283 (convert-path-exps (drop rest arity))))
287 `(stroke-width . ,thick)
288 '(stroke-linejoin . "round")
289 '(stroke-linecap . "round")
290 '(stroke . "currentColor")
292 `(d . ,(string-join (convert-path-exps commands) " "))))
294 (define (placebox x y expr)
295 (if (not (string-null? expr))
297 ((match (regexp-exec svg-element-regexp expr))
298 (tagname (match:substring match 1))
299 (attributes (match:substring match 2)))
301 (string-append tagname
302 ;; FIXME: Not using GNU coding standards
303 ;; [translate ()] here to work around a
304 ;; bug in Microsoft Internet Explorer 6.0
305 (ly:format " transform=\"translate(~f, ~f)\" "
311 (define (polygon coords blot-diameter is-filled)
314 '(stroke-linejoin . "round")
315 '(stroke-linecap . "round")
316 `(stroke-width . ,blot-diameter)
317 `(fill . ,(if is-filled "currentColor" "none"))
318 '(stroke . "currentColor")
319 `(points . ,(string-join
320 (map offset->point (ly:list->offsets '() coords))))
326 (define (resetrotation ang x y)
329 (define (round-filled-box breapth width depth height blot-diameter)
331 ;; The stroke will stick out. To use stroke,
332 ;; the stroke-width must be subtracted from all other dimensions.
333 ;;'(stroke-linejoin . "round")
334 ;;'(stroke-linecap . "round")
335 ;;`(stroke-width . ,blot)
341 `(width . ,(+ breapth width))
342 `(height . ,(+ depth height))
343 `(ry . ,(/ blot-diameter 2))
344 '(fill . "currentColor")
347 (define (setcolor r g b)
348 (format "<g color=\"rgb(~a%,~a%,~a%)\">\n"
349 (* 100 r) (* 100 g) (* 100 b)
352 ;; rotate around given point
353 (define (setrotation ang x y)
354 (format "<g transform=\"rotate(~a,~a,~a)\">\n"
355 (number->string (* -1 ang))
357 (number->string (* -1 y))))
359 (define (text font string)
360 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
362 (define (url-link url x y)
364 (eo 'a `(xlink:href . ,url))
368 `(width . ,(- (cdr x) (car x)))
369 `(height . ,(- (cdr y) (car y)))
372 '(stroke-width . "0.0"))
375 (define (utf-8-string pango-font-description string)
376 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))