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))
42 (string-append "undefined: " (symbol->string keyword) "\n"))
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))
63 (define-public (entity entity string . attributes-alist)
64 (if (equal? string "")
65 (apply eoc entity attributes-alist)
67 (apply eo (cons entity attributes-alist)) string (ec entity))))
69 (define (offset->point o)
70 (format #f " ~S,~S" (car o) (- (cdr o))))
72 (define (svg-bezier lst close)
73 (let* ((c0 (car (list-tail lst 3)))
74 (c123 (list-head lst 3)))
76 (if (not close) "M " "L ")
78 "C " (apply string-append (map offset->point c123))
79 (if (not close) "" (string-append
80 "L " (offset->point close))))))
85 (define (integer->entity integer)
86 (format #f "&#x~x;" integer))
88 (define (char->entity char)
89 (integer->entity (char->integer char)))
91 (define (string->entities string)
93 (map (lambda (x) (char->entity x)) (string->list string))))
95 (define pango-description-regexp-comma
96 (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
98 (define pango-description-regexp-nocomma
99 (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
101 (define (pango-description-to-svg-font str)
106 (match-1 (regexp-exec pango-description-regexp-comma str))
107 (match-2 (regexp-exec pango-description-regexp-nocomma str))
112 (if (regexp-match? match)
114 (set! family (match:substring match 1))
115 (if (< 0 (string-length (match:substring match 2)))
116 (set! style (match:substring match 2)))
118 (string->number (match:substring match 3))))
120 (display (format "Cannot decypher Pango description: ~a\n" str)))
124 (format "font-style:~a;" style)
127 (format "font-family:~a;~afont-size:~a;text-anchor:west"
130 (/ size lily-unit-length))
133 ;;; FONT may be font smob, or pango font string
134 (define (svg-font font)
136 (pango-description-to-svg-font font)
137 (let ((name-style (font-name-style font))
138 (size (modified-font-metric-font-scaling font))
141 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
142 (car name-style) (cadr name-style)
145 (define (fontify font expr)
147 `(style . ,(svg-font font))
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;;; stencil outputters
154 ;;; catch-all for missing stuff
155 ;;; comment this out to see find out what functions you miss :-)
159 (define (dummy . foo) "")
160 (map (lambda (x) (module-define! this-module x dummy))
162 (ly:all-stencil-expressions)
163 (ly:all-output-backend-commands)))
166 (define (url-link url x y)
168 (eo 'a `(xlink:href . ,url))
172 `(width . ,(- (cdr x) (car x)))
173 `(height . ,(- (cdr y) (car y)))
176 '(stroke-width . "0.0"))
179 (define (grob-cause offset grob)
186 (define (rect-beam width slope thick blot-diameter)
191 ;; The stroke will stick out. To use stroke,
192 ;; the stroke-width must be subtracted from all other dimensions.
193 ;;'(stroke-linejoin . "round")
194 ;;'(stroke-linecap . "round")
195 ;;`(stroke-width . ,blot-diameter)
200 `(y . ,(- (/ thick 2)))
202 `(height . ,(+ thick (* (abs z) (/ thick 2))))
203 `(rx . ,(/ blot-diameter 2))
204 `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)" z)
207 (define (beam width slope thick blot-diameter)
208 (let* ((b blot-diameter)
213 '(stroke-linejoin . "round")
214 '(stroke-linecap . "round")
215 `(stroke-width . ,blot-diameter)
218 `(points . ,(string-join
220 (list (cons (/ b 2) (/ t 2))
221 (cons (+ w (/ b 2)) (+ h (/ t 2)))
222 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
223 (cons (/ b 2) (- (/ t 2)))))))
226 (define (path-beam width slope thick blot-diameter)
227 (let* ((b blot-diameter)
232 '(stroke-linejoin . "round")
233 '(stroke-linecap . "round")
234 `(stroke-width . ,blot-diameter)
237 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
245 (define (bezier-sandwich lst thick)
246 (let* ((first (list-tail lst 4))
247 (first-c0 (car (list-tail first 3)))
248 (second (list-head lst 4)))
250 '(stroke-linejoin . "round")
251 '(stroke-linecap . "round")
252 `(stroke-width . ,thick)
255 `(d . ,(string-append (svg-bezier first #f)
256 (svg-bezier second first-c0)))
259 (define (char font i)
261 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
263 (define-public (comment s)
264 (string-append "<!-- " s " !-->\n"))
266 (define (draw-line thick x1 y1 x2 y2 . alist)
268 (apply entity 'line ""
270 `((stroke-linejoin . "round")
271 (stroke-linecap . "round")
272 (stroke-width . ,thick)
281 (define (dashed-line thick on off dx dy)
282 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
284 ;; WTF is this in every backend?
285 (define (horizontal-line x1 x2 th)
286 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
288 (define (filledbox breapth width depth height)
289 (round-filled-box breapth width depth height 0))
291 (define (named-glyph font name)
293 `(fontify ,font ,(entity 'tspan
295 (ly:font-glyph-name-to-charcode font name))))))
297 (define (placebox x y expr)
302 `(transform . ,(format #f "translate (~f, ~f)"
306 (define (polygon coords blot-diameter)
308 '(stroke-linejoin . "round")
309 '(stroke-linecap . "round")
310 `(stroke-width . ,blot-diameter)
313 `(points . ,(string-join
314 (map offset->point (ly:list->offsets '() coords))))
317 (define (round-filled-box breapth width depth height blot-diameter)
319 ;; The stroke will stick out. To use stroke,
320 ;; the stroke-width must be subtracted from all other dimensions.
321 ;;'(stroke-linejoin . "round")
322 ;;'(stroke-linecap . "round")
323 ;;`(stroke-width . ,blot)
329 `(width . ,(+ breapth width))
330 `(height . ,(+ depth height))
331 `(ry . ,(/ blot-diameter 2))
334 (define (text font string)
335 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
337 (define (utf8-string pango-font-description string)
338 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))