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))
30 (define format ergonomic-simple-format)
32 (define lily-unit-length 1.75)
34 (define (dispatch expr)
35 (let ((keyword (car expr)))
37 ((eq? keyword 'some-func) "")
38 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
40 (if (module-defined? this-module keyword)
41 (apply (eval keyword this-module) (cdr expr))
43 (ly:warning (_ "undefined: ~S") keyword)
47 (define-public (attributes attributes-alist)
49 (map (lambda (x) (format " ~s=\"~a\"" (car x) (cdr x)))
52 (define-public (eo entity . attributes-alist)
54 (format "<~S~a>\n" entity (attributes attributes-alist)))
56 (define-public (eoc entity . attributes-alist)
58 (format "<~S~a/>\n" entity (attributes attributes-alist)))
60 (define-public (ec entity)
62 (format "</~S>\n" entity))
66 (define-public (entity entity string . attributes-alist)
67 (if (equal? string "")
68 (apply eoc entity attributes-alist)
70 (apply eo (cons entity attributes-alist)) string (ec entity))))
72 (define (offset->point o)
73 (format " ~S,~S" (car o) (- (cdr o))))
75 (define (number-list->point lst)
79 (cons (format "~S,~S" (car lst) (cadr lst))
80 (helper (cddr lst)))))
82 (string-join (helper lst) " "))
85 (define (svg-bezier lst close)
86 (let* ((c0 (car (list-tail lst 3)))
87 (c123 (list-head lst 3)))
89 (if (not close) "M " "L ")
91 "C " (apply string-append (map offset->point c123))
92 (if (not close) "" (string-append
93 "L " (offset->point close))))))
98 (define (integer->entity integer)
99 (format "&#x~x;" integer))
101 (define (char->entity char)
102 (integer->entity (char->integer char)))
104 (define (string->entities string)
106 (map (lambda (x) (char->entity x)) (string->list string))))
108 (define pango-description-regexp-comma
109 (make-regexp "([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
111 (define pango-description-regexp-nocomma
112 (make-regexp "([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
114 (define (pango-description-to-svg-font str)
119 (match-1 (regexp-exec pango-description-regexp-comma str))
120 (match-2 (regexp-exec pango-description-regexp-nocomma str))
125 (if (regexp-match? match)
127 (set! family (match:substring match 1))
128 (if (< 0 (string-length (match:substring match 2)))
129 (set! style (match:substring match 2)))
131 (string->number (match:substring match 3))))
133 (ly:warning (_ "cannot decypher Pango description: ~a") str))
137 (format "font-style:~a;" style)
140 (format "font-family:~a;~afont-size:~a;text-anchor:west"
143 (/ size lily-unit-length))
146 ;;; FONT may be font smob, or pango font string
147 (define (svg-font font)
149 (pango-description-to-svg-font font)
150 (let ((name-style (font-name-style font))
151 (size (modified-font-metric-font-scaling font))
154 (format "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
155 (car name-style) (cadr name-style)
158 (define (fontify font expr)
160 `(style . ,(svg-font font))
161 '(fill . "currentColor")
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)
283 ;; FIXME: Not using GNU coding standards [translate ()] here
284 ;; to work around a bug in Microsoft Internet Explorer 6.0
285 `(transform . ,(format "translate(~f, ~f)"
288 (define (polygon coords blot-diameter is-filled)
291 '(stroke-linejoin . "round")
292 '(stroke-linecap . "round")
293 `(stroke-width . ,blot-diameter)
294 `(fill . ,(if is-filled "currentColor" "none"))
295 '(stroke . "currentColor")
296 `(points . ,(string-join
297 (map offset->point (ly:list->offsets '() coords))))
300 ;; rotate around given point
301 (define (setrotation ang x y)
302 (format "<g transform=\"rotate(~a,~a,~a)\">"
303 (number->string (* -1 ang))
305 (number->string (* -1 y))))
307 (define (resetrotation ang x y)
310 (define (round-filled-box breapth width depth height blot-diameter)
312 ;; The stroke will stick out. To use stroke,
313 ;; the stroke-width must be subtracted from all other dimensions.
314 ;;'(stroke-linejoin . "round")
315 ;;'(stroke-linecap . "round")
316 ;;`(stroke-width . ,blot)
322 `(width . ,(+ breapth width))
323 `(height . ,(+ depth height))
324 `(ry . ,(/ blot-diameter 2))
327 (define (circle radius thick is-filled)
330 '(stroke-linejoin . "round")
331 '(stroke-linecap . "round")
332 `(fill . ,(if is-filled "currentColor" "none"))
333 `(stroke . "currentColor")
334 `(stroke-width . ,thick)
337 (define (text font string)
338 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
340 (define (utf-8-string pango-font-description string)
341 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
345 (define (setcolor r g b)
346 (format "<g color=\"rgb(~a%,~a%,~a%)\">"
347 (* 100 r) (* 100 g) (* 100 b)