1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2002--2005 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 lily-unit-length 1.75)
32 (define (dispatch expr)
33 (let ((keyword (car expr)))
35 ((eq? keyword 'some-func) "")
36 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
38 (if (module-defined? this-module keyword)
39 (apply (eval keyword this-module) (cdr expr))
41 (ly:warning (_ "undefined: ~S") keyword)
45 (define-public (attributes attributes-alist)
47 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
50 (define-public (eo entity . attributes-alist)
52 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
54 (define-public (eoc entity . attributes-alist)
56 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
58 (define-public (ec entity)
60 (format #f "</~S>\n" entity))
64 (define-public (entity entity string . attributes-alist)
65 (if (equal? string "")
66 (apply eoc entity attributes-alist)
68 (apply eo (cons entity attributes-alist)) string (ec entity))))
70 (define (offset->point o)
71 (format #f " ~S,~S" (car o) (- (cdr o))))
73 (define (svg-bezier lst close)
74 (let* ((c0 (car (list-tail lst 3)))
75 (c123 (list-head lst 3)))
77 (if (not close) "M " "L ")
79 "C " (apply string-append (map offset->point c123))
80 (if (not close) "" (string-append
81 "L " (offset->point close))))))
86 (define (integer->entity integer)
87 (format #f "&#x~x;" integer))
89 (define (char->entity char)
90 (integer->entity (char->integer char)))
92 (define (string->entities string)
94 (map (lambda (x) (char->entity x)) (string->list string))))
96 (define pango-description-regexp-comma
97 (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
99 (define pango-description-regexp-nocomma
100 (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
102 (define (pango-description-to-svg-font str)
107 (match-1 (regexp-exec pango-description-regexp-comma str))
108 (match-2 (regexp-exec pango-description-regexp-nocomma str))
113 (if (regexp-match? match)
115 (set! family (match:substring match 1))
116 (if (< 0 (string-length (match:substring match 2)))
117 (set! style (match:substring match 2)))
119 (string->number (match:substring match 3))))
121 (ly:warning (_ "can't decypher Pango description: ~a") str))
125 (format "font-style:~a;" style)
128 (format "font-family:~a;~afont-size:~a;text-anchor:west"
131 (/ size lily-unit-length))
134 ;;; FONT may be font smob, or pango font string
135 (define (svg-font font)
137 (pango-description-to-svg-font font)
138 (let ((name-style (font-name-style font))
139 (size (modified-font-metric-font-scaling font))
142 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
143 (car name-style) (cadr name-style)
146 (define (fontify font expr)
148 `(style . ,(svg-font font))
149 '(fill . "currentColor")
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
153 ;;; stencil outputters
156 ;;; catch-all for missing stuff
157 ;;; comment this out to see find out what functions you miss :-)
161 (define (dummy . foo) "")
162 (map (lambda (x) (module-define! this-module x dummy))
164 (ly:all-stencil-expressions)
165 (ly:all-output-backend-commands)))
168 (define (url-link url x y)
170 (eo 'a `(xlink:href . ,url))
174 `(width . ,(- (cdr x) (car x)))
175 `(height . ,(- (cdr y) (car y)))
178 '(stroke-width . "0.0"))
181 (define (grob-cause offset grob)
188 (define (rect-beam width slope thick blot-diameter)
193 ;; The stroke will stick out. To use stroke,
194 ;; the stroke-width must be subtracted from all other dimensions.
195 ;;'(stroke-linejoin . "round")
196 ;;'(stroke-linecap . "round")
197 ;;`(stroke-width . ,blot-diameter)
202 `(y . ,(- (/ thick 2)))
204 `(height . ,(+ thick (* (abs z) (/ thick 2))))
205 `(rx . ,(/ blot-diameter 2))
206 `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)" z)
209 (define (beam width slope thick blot-diameter)
210 (let* ((b blot-diameter)
215 '(stroke-linejoin . "round")
216 '(stroke-linecap . "round")
217 `(stroke-width . ,blot-diameter)
218 '(stroke . "currentColor")
219 '(fill . "currentColor")
220 `(points . ,(string-join
222 (list (cons (/ b 2) (/ t 2))
223 (cons (+ w (/ b 2)) (+ h (/ t 2)))
224 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
225 (cons (/ b 2) (- (/ t 2)))))))
228 (define (path-beam width slope thick blot-diameter)
229 (let* ((b blot-diameter)
234 '(stroke-linejoin . "round")
235 '(stroke-linecap . "round")
236 `(stroke-width . ,blot-diameter)
237 '(stroke . "currentColor")
238 '(fill . "currentColor")
239 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
247 (define (bezier-sandwich lst thick)
248 (let* ((first (list-tail lst 4))
249 (first-c0 (car (list-tail first 3)))
250 (second (list-head lst 4)))
252 '(stroke-linejoin . "round")
253 '(stroke-linecap . "round")
254 `(stroke-width . ,thick)
255 '(stroke . "currentColor")
256 '(fill . "currentColor")
257 `(d . ,(string-append (svg-bezier first #f)
258 (svg-bezier second first-c0)))
261 (define (char font i)
263 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
265 (define-public (comment s)
266 (string-append "<!-- " s " !-->\n"))
268 (define (draw-line thick x1 y1 x2 y2 . alist)
270 (apply entity 'line ""
272 `((stroke-linejoin . "round")
273 (stroke-linecap . "round")
274 (stroke-width . ,thick)
275 (stroke . "currentColor")
282 (define (dashed-line thick on off dx dy)
283 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
285 (define (filledbox breapth width depth height)
286 (round-filled-box breapth width depth height 0))
288 (define (named-glyph font name)
290 `(fontify ,font ,(entity 'tspan
292 (ly:font-glyph-name-to-charcode font name))))))
294 (define (placebox x y expr)
299 `(transform . ,(format #f "translate (~f, ~f)"
302 (define (polygon coords blot-diameter is-filled)
305 '(stroke-linejoin . "round")
306 '(stroke-linecap . "round")
307 `(stroke-width . ,blot-diameter)
308 `(fill . ,(if is-filled "currentColor" "none"))
309 '(stroke . "currentColor")
310 `(points . ,(string-join
311 (map offset->point (ly:list->offsets '() coords))))
314 (define (round-filled-box breapth width depth height blot-diameter)
316 ;; The stroke will stick out. To use stroke,
317 ;; the stroke-width must be subtracted from all other dimensions.
318 ;;'(stroke-linejoin . "round")
319 ;;'(stroke-linecap . "round")
320 ;;`(stroke-width . ,blot)
326 `(width . ,(+ breapth width))
327 `(height . ,(+ depth height))
328 `(ry . ,(/ blot-diameter 2))
331 (define (circle radius thick is-filled)
334 '(stroke-linejoin . "round")
335 '(stroke-linecap . "round")
336 `(fill . ,(if is-filled "currentColor" "none"))
337 `(stroke . "currentColor")
338 `(stroke-width . ,thick)
341 (define (text font string)
342 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
344 (define (utf8-string pango-font-description string)
345 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
349 (define (setcolor r g b)
350 (format "<g color=\"rgb(~a%,~a%,~a%)\">"
351 (* 100 r) (* 100 g) (* 100 b)