1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2002--2006 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 lily-unit-length 1.75)
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))
42 (ly:warning (_ "undefined: ~S") keyword)
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)
53 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
55 (define-public (eoc entity . attributes-alist)
57 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
59 (define-public (ec entity)
61 (format #f "</~S>\n" entity))
65 (define-public (entity entity string . attributes-alist)
66 (if (equal? string "")
67 (apply eoc entity attributes-alist)
69 (apply eo (cons entity attributes-alist)) string (ec entity))))
71 (define (offset->point o)
72 (format #f " ~S,~S" (car o) (- (cdr o))))
74 (define (number-list->point lst)
78 (cons (format "~S,~S" (car lst) (cadr lst))
79 (helper (cddr lst)))))
81 (string-join (helper lst) " "))
84 (define (svg-bezier lst close)
85 (let* ((c0 (car (list-tail lst 3)))
86 (c123 (list-head lst 3)))
88 (if (not close) "M " "L ")
90 "C " (apply string-append (map offset->point c123))
91 (if (not close) "" (string-append
92 "L " (offset->point close))))))
97 (define (integer->entity integer)
98 (format #f "&#x~x;" integer))
100 (define (char->entity char)
101 (integer->entity (char->integer char)))
103 (define (string->entities string)
105 (map (lambda (x) (char->entity x)) (string->list string))))
107 (define pango-description-regexp-comma
108 (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
110 (define pango-description-regexp-nocomma
111 (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
113 (define (pango-description-to-svg-font str)
118 (match-1 (regexp-exec pango-description-regexp-comma str))
119 (match-2 (regexp-exec pango-description-regexp-nocomma str))
124 (if (regexp-match? match)
126 (set! family (match:substring match 1))
127 (if (< 0 (string-length (match:substring match 2)))
128 (set! style (match:substring match 2)))
130 (string->number (match:substring match 3))))
132 (ly:warning (_ "can't decypher Pango description: ~a") str))
136 (format "font-style:~a;" style)
139 (format "font-family:~a;~afont-size:~a;text-anchor:west"
142 (/ size lily-unit-length))
145 ;;; FONT may be font smob, or pango font string
146 (define (svg-font font)
148 (pango-description-to-svg-font font)
149 (let ((name-style (font-name-style font))
150 (size (modified-font-metric-font-scaling font))
153 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
154 (car name-style) (cadr name-style)
157 (define (fontify font expr)
159 `(style . ,(svg-font font))
160 '(fill . "currentColor")
163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 ;;; stencil outputters
167 ;;; catch-all for missing stuff
168 ;;; comment this out to see find out what functions you miss :-)
172 (define (dummy . foo) "")
173 (map (lambda (x) (module-define! this-module x dummy))
175 (ly:all-stencil-expressions)
176 (ly:all-output-backend-commands)))
179 (define (url-link url x y)
181 (eo 'a `(xlink:href . ,url))
185 `(width . ,(- (cdr x) (car x)))
186 `(height . ,(- (cdr y) (car y)))
189 '(stroke-width . "0.0"))
192 (define (grob-cause offset grob)
200 (define (bezier-sandwich lst thick)
201 (let* ((first (list-tail lst 4))
202 (first-c0 (car (list-tail first 3)))
203 (second (list-head lst 4)))
205 '(stroke-linejoin . "round")
206 '(stroke-linecap . "round")
207 '(stroke . "currentColor")
208 '(fill . "currentColor")
209 `(stroke-width . ,thick)
210 `(d . ,(string-append (svg-bezier first #f)
211 (svg-bezier second first-c0)))
214 (define (path thick commands)
215 (define (convert-path-exps exps)
222 ((memq head '(rmoveto rlineto lineto moveto)) 2)
223 ((memq head '(rcurveto curveto)) 6)
225 (args (take rest arity))
226 (svg-head (assoc-get head '((rmoveto . m)
235 (cons (format "~a~a "
236 svg-head (number-list->point args)
238 (convert-path-exps (drop rest arity))))
242 `(stroke-width . ,thick)
243 '(stroke-linejoin . "round")
244 '(stroke-linecap . "round")
245 '(stroke . "currentColor")
247 `(d . ,(string-join (convert-path-exps commands) " "))))
249 (define (char font i)
251 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
253 (define-public (comment s)
254 (string-append "<!-- " s " !-->\n"))
256 (define (draw-line thick x1 y1 x2 y2 . alist)
258 (apply entity 'line ""
260 `((stroke-linejoin . "round")
261 (stroke-linecap . "round")
262 (stroke-width . ,thick)
263 (stroke . "currentColor")
270 (define (dashed-line thick on off dx dy phase)
271 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
273 (define (named-glyph font name)
275 `(fontify ,font ,(entity 'tspan
277 (ly:font-glyph-name-to-charcode font name))))))
279 (define (placebox x y expr)
282 ;; FIXME: Not using GNU coding standards [translate ()] here
283 ;; to work around a bug in Microsoft Internet Explorer 6.0
284 `(transform . ,(format #f "translate(~f, ~f)"
287 (define (polygon coords blot-diameter is-filled)
290 '(stroke-linejoin . "round")
291 '(stroke-linecap . "round")
292 `(stroke-width . ,blot-diameter)
293 `(fill . ,(if is-filled "currentColor" "none"))
294 '(stroke . "currentColor")
295 `(points . ,(string-join
296 (map offset->point (ly:list->offsets '() coords))))
299 ;; rotate around given point
300 (define (setrotation ang x y)
301 (format "<g transform=\"rotate(~a,~a,~a)\">"
302 (number->string (* -1 ang))
304 (number->string (* -1 y))))
306 (define (resetrotation ang x y)
309 (define (round-filled-box breapth width depth height blot-diameter)
311 ;; The stroke will stick out. To use stroke,
312 ;; the stroke-width must be subtracted from all other dimensions.
313 ;;'(stroke-linejoin . "round")
314 ;;'(stroke-linecap . "round")
315 ;;`(stroke-width . ,blot)
321 `(width . ,(+ breapth width))
322 `(height . ,(+ depth height))
323 `(ry . ,(/ blot-diameter 2))
326 (define (circle radius thick is-filled)
329 '(stroke-linejoin . "round")
330 '(stroke-linecap . "round")
331 `(fill . ,(if is-filled "currentColor" "none"))
332 `(stroke . "currentColor")
333 `(stroke-width . ,thick)
336 (define (text font string)
337 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
339 (define (utf-8-string pango-font-description string)
340 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
344 (define (setcolor r g b)
345 (format "<g color=\"rgb(~a%,~a%,~a%)\">"
346 (* 100 r) (* 100 g) (* 100 b)