1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2002--2008 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 pango-description-regexp-comma
111 (make-regexp "([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
113 (define pango-description-regexp-nocomma
114 (make-regexp "([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
116 (define (pango-description-to-svg-font str)
121 (match-1 (regexp-exec pango-description-regexp-comma str))
122 (match-2 (regexp-exec pango-description-regexp-nocomma str))
127 (if (regexp-match? match)
129 (set! family (match:substring match 1))
130 (if (< 0 (string-length (match:substring match 2)))
131 (set! style (match:substring match 2)))
133 (string->number (match:substring match 3))))
135 (ly:warning (_ "cannot decypher Pango description: ~a") str))
139 (format "font-style:~a;" style)
142 (format "font-family:~a;~afont-size:~a;text-anchor:west"
145 (/ size lily-unit-length))
148 ;;; FONT may be font smob, or pango font string
149 (define (svg-font font)
151 (pango-description-to-svg-font font)
152 (let ((name-style (font-name-style font))
153 (size (modified-font-metric-font-scaling font))
156 (format "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
157 (car name-style) (cadr name-style)
160 (define (fontify font expr)
162 `(style . ,(svg-font font))
163 '(fill . "currentColor")
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167 ;;; stencil outputters
170 ;;; catch-all for missing stuff
171 ;;; comment this out to see find out what functions you miss :-)
175 (define (dummy . foo) "")
176 (map (lambda (x) (module-define! this-module x dummy))
178 (ly:all-stencil-expressions)
179 (ly:all-output-backend-commands)))
182 (define (url-link url x y)
184 (eo 'a `(xlink:href . ,url))
188 `(width . ,(- (cdr x) (car x)))
189 `(height . ,(- (cdr y) (car y)))
192 '(stroke-width . "0.0"))
195 (define (grob-cause offset grob)
203 (define (bezier-sandwich lst thick)
204 (let* ((first (list-tail lst 4))
205 (first-c0 (car (list-tail first 3)))
206 (second (list-head lst 4)))
208 '(stroke-linejoin . "round")
209 '(stroke-linecap . "round")
210 '(stroke . "currentColor")
211 '(fill . "currentColor")
212 `(stroke-width . ,thick)
213 `(d . ,(string-append (svg-bezier first #f)
214 (svg-bezier second first-c0)))
217 (define (path thick commands)
218 (define (convert-path-exps exps)
225 ((memq head '(rmoveto rlineto lineto moveto)) 2)
226 ((memq head '(rcurveto curveto)) 6)
228 (args (take rest arity))
229 (svg-head (assoc-get head '((rmoveto . m)
238 (cons (format "~a~a "
239 svg-head (number-list->point args)
241 (convert-path-exps (drop rest arity))))
245 `(stroke-width . ,thick)
246 '(stroke-linejoin . "round")
247 '(stroke-linecap . "round")
248 '(stroke . "currentColor")
250 `(d . ,(string-join (convert-path-exps commands) " "))))
252 (define (char font i)
254 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
256 (define-public (comment s)
257 (string-append "<!-- " s " !-->\n"))
259 (define (draw-line thick x1 y1 x2 y2 . alist)
261 (apply entity 'line ""
263 `((stroke-linejoin . "round")
264 (stroke-linecap . "round")
265 (stroke-width . ,thick)
266 (stroke . "currentColor")
273 (define (dashed-line thick on off dx dy phase)
274 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
276 (define (named-glyph font name)
278 `(fontify ,font ,(entity 'tspan
280 (ly:font-glyph-name-to-charcode font name))))))
282 (define (placebox x y expr)
285 ;; FIXME: Not using GNU coding standards [translate ()] here
286 ;; to work around a bug in Microsoft Internet Explorer 6.0
287 `(transform . ,(ly:format "translate(~f, ~f)"
290 (define (polygon coords blot-diameter is-filled)
293 '(stroke-linejoin . "round")
294 '(stroke-linecap . "round")
295 `(stroke-width . ,blot-diameter)
296 `(fill . ,(if is-filled "currentColor" "none"))
297 '(stroke . "currentColor")
298 `(points . ,(string-join
299 (map offset->point (ly:list->offsets '() coords))))
302 ;; rotate around given point
303 (define (setrotation ang x y)
304 (format "<g transform=\"rotate(~a,~a,~a)\">"
305 (number->string (* -1 ang))
307 (number->string (* -1 y))))
309 (define (resetrotation ang x y)
312 (define (round-filled-box breapth width depth height blot-diameter)
314 ;; The stroke will stick out. To use stroke,
315 ;; the stroke-width must be subtracted from all other dimensions.
316 ;;'(stroke-linejoin . "round")
317 ;;'(stroke-linecap . "round")
318 ;;`(stroke-width . ,blot)
324 `(width . ,(+ breapth width))
325 `(height . ,(+ depth height))
326 `(ry . ,(/ blot-diameter 2))
329 (define (circle radius thick is-filled)
332 '(stroke-linejoin . "round")
333 '(stroke-linecap . "round")
334 `(fill . ,(if is-filled "currentColor" "none"))
335 `(stroke . "currentColor")
336 `(stroke-width . ,thick)
339 (define (ellipse x-radius y-radius thick is-filled)
342 '(stroke-linejoin . "round")
343 '(stroke-linecap . "round")
344 `(fill . ,(if is-filled "currentColor" "none"))
345 `(stroke . "currentColor")
346 `(stroke-width . ,thick)
350 (define (oval x-radius y-radius thick is-filled)
351 (let ((x-max x-radius)
354 (y-min (- y-radius)))
357 '(stroke-linejoin . "round")
358 '(stroke-linecap . "round")
359 `(fill . ,(if is-filled "currentColor" "none"))
360 `(stroke . "currentColor")
361 `(stroke-width . ,thick)
362 `(d . ,(ly:format "M~4f,~4f C~4f,~4f ~4f,~4f ~4f,~4f S~4f,~4f ~4f,~4f"
370 (define (text font string)
371 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
373 (define (utf-8-string pango-font-description string)
374 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
378 (define (setcolor r g b)
379 (format "<g color=\"rgb(~a%,~a%,~a%)\">"
380 (* 100 r) (* 100 g) (* 100 b)