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))
63 (define-public (setcolor r g b))
65 (define-public (resetcolor))
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 #f " ~S,~S" (car o) (- (cdr o))))
77 (define (svg-bezier lst close)
78 (let* ((c0 (car (list-tail lst 3)))
79 (c123 (list-head lst 3)))
81 (if (not close) "M " "L ")
83 "C " (apply string-append (map offset->point c123))
84 (if (not close) "" (string-append
85 "L " (offset->point close))))))
90 (define (integer->entity integer)
91 (format #f "&#x~x;" integer))
93 (define (char->entity char)
94 (integer->entity (char->integer char)))
96 (define (string->entities string)
98 (map (lambda (x) (char->entity x)) (string->list string))))
100 (define pango-description-regexp-comma
101 (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
103 (define pango-description-regexp-nocomma
104 (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
106 (define (pango-description-to-svg-font str)
111 (match-1 (regexp-exec pango-description-regexp-comma str))
112 (match-2 (regexp-exec pango-description-regexp-nocomma str))
117 (if (regexp-match? match)
119 (set! family (match:substring match 1))
120 (if (< 0 (string-length (match:substring match 2)))
121 (set! style (match:substring match 2)))
123 (string->number (match:substring match 3))))
125 (ly:warning (_ "can't decypher Pango description: ~a") str))
129 (format "font-style:~a;" style)
132 (format "font-family:~a;~afont-size:~a;text-anchor:west"
135 (/ size lily-unit-length))
138 ;;; FONT may be font smob, or pango font string
139 (define (svg-font font)
141 (pango-description-to-svg-font font)
142 (let ((name-style (font-name-style font))
143 (size (modified-font-metric-font-scaling font))
146 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
147 (car name-style) (cadr name-style)
150 (define (fontify font expr)
152 `(style . ,(svg-font font))
153 '(fill . "currentColor")
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;;; stencil outputters
160 ;;; catch-all for missing stuff
161 ;;; comment this out to see find out what functions you miss :-)
165 (define (dummy . foo) "")
166 (map (lambda (x) (module-define! this-module x dummy))
168 (ly:all-stencil-expressions)
169 (ly:all-output-backend-commands)))
172 (define (url-link url x y)
174 (eo 'a `(xlink:href . ,url))
178 `(width . ,(- (cdr x) (car x)))
179 `(height . ,(- (cdr y) (car y)))
182 '(stroke-width . "0.0"))
185 (define (grob-cause offset grob)
192 (define (rect-beam width slope thick blot-diameter)
197 ;; The stroke will stick out. To use stroke,
198 ;; the stroke-width must be subtracted from all other dimensions.
199 ;;'(stroke-linejoin . "round")
200 ;;'(stroke-linecap . "round")
201 ;;`(stroke-width . ,blot-diameter)
206 `(y . ,(- (/ thick 2)))
208 `(height . ,(+ thick (* (abs z) (/ thick 2))))
209 `(rx . ,(/ blot-diameter 2))
210 `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)" z)
213 (define (beam width slope thick blot-diameter)
214 (let* ((b blot-diameter)
219 '(stroke-linejoin . "round")
220 '(stroke-linecap . "round")
221 `(stroke-width . ,blot-diameter)
222 '(stroke . "currentColor")
223 '(fill . "currentColor")
224 `(points . ,(string-join
226 (list (cons (/ b 2) (/ t 2))
227 (cons (+ w (/ b 2)) (+ h (/ t 2)))
228 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
229 (cons (/ b 2) (- (/ t 2)))))))
232 (define (path-beam width slope thick blot-diameter)
233 (let* ((b blot-diameter)
238 '(stroke-linejoin . "round")
239 '(stroke-linecap . "round")
240 `(stroke-width . ,blot-diameter)
241 '(stroke . "currentColor")
242 '(fill . "currentColor")
243 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
251 (define (bezier-sandwich lst thick)
252 (let* ((first (list-tail lst 4))
253 (first-c0 (car (list-tail first 3)))
254 (second (list-head lst 4)))
256 '(stroke-linejoin . "round")
257 '(stroke-linecap . "round")
258 `(stroke-width . ,thick)
259 '(stroke . "currentColor")
260 '(fill . "currentColor")
261 `(d . ,(string-append (svg-bezier first #f)
262 (svg-bezier second first-c0)))
265 (define (char font i)
267 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
269 (define-public (comment s)
270 (string-append "<!-- " s " !-->\n"))
272 (define (draw-line thick x1 y1 x2 y2 . alist)
274 (apply entity 'line ""
276 `((stroke-linejoin . "round")
277 (stroke-linecap . "round")
278 (stroke-width . ,thick)
279 (stroke . "currentColor")
286 (define (dashed-line thick on off dx dy)
287 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
289 (define (filledbox breapth width depth height)
290 (round-filled-box breapth width depth height 0))
292 (define (named-glyph font name)
294 `(fontify ,font ,(entity 'tspan
296 (ly:font-glyph-name-to-charcode font name))))))
298 (define (placebox x y expr)
303 `(transform . ,(format #f "translate (~f, ~f)"
306 (define (polygon coords blot-diameter is-filled)
309 '(stroke-linejoin . "round")
310 '(stroke-linecap . "round")
311 `(stroke-width . ,blot-diameter)
312 `(fill . ,(if is-filled "currentColor" "none"))
313 '(stroke . "currentColor")
314 `(points . ,(string-join
315 (map offset->point (ly:list->offsets '() coords))))
318 (define (round-filled-box breapth width depth height blot-diameter)
320 ;; The stroke will stick out. To use stroke,
321 ;; the stroke-width must be subtracted from all other dimensions.
322 ;;'(stroke-linejoin . "round")
323 ;;'(stroke-linecap . "round")
324 ;;`(stroke-width . ,blot)
330 `(width . ,(+ breapth width))
331 `(height . ,(+ depth height))
332 `(ry . ,(/ blot-diameter 2))
335 (define (circle radius thick is-filled)
338 '(stroke-linejoin . "round")
339 '(stroke-linecap . "round")
340 `(fill . ,(if is-filled "currentColor" "none"))
341 `(stroke . "currentColor")
342 `(stroke-width . ,thick)
345 (define (text font string)
346 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
348 (define (utf8-string pango-font-description string)
349 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
353 (define (setcolor r g b)
354 (format "<g color=\"rgb(~a%,~a%,~a%)\">"
355 (* 100 r) (* 100 g) (* 100 b)