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))
62 (define-public (entity entity string . attributes-alist)
63 (if (equal? string "")
64 (apply eoc entity attributes-alist)
66 (apply eo (cons entity attributes-alist)) string (ec entity))))
68 (define (offset->point o)
69 (format #f " ~S,~S" (car o) (- (cdr o))))
71 (define (svg-bezier lst close)
72 (let* ((c0 (car (list-tail lst 3)))
73 (c123 (list-head lst 3)))
75 (if (not close) "M " "L ")
77 "C " (apply string-append (map offset->point c123))
78 (if (not close) "" (string-append
79 "L " (offset->point close))))))
84 (define (integer->entity integer)
85 (format #f "&#x~x;" integer))
87 (define (char->entity char)
88 (integer->entity (char->integer char)))
90 (define (string->entities string)
92 (map (lambda (x) (char->entity x)) (string->list string))))
94 (define pango-description-regexp-comma
95 (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
97 (define pango-description-regexp-nocomma
98 (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
100 (define (pango-description-to-svg-font str)
105 (match-1 (regexp-exec pango-description-regexp-comma str))
106 (match-2 (regexp-exec pango-description-regexp-nocomma str))
111 (if (regexp-match? match)
113 (set! family (match:substring match 1))
114 (if (< 0 (string-length (match:substring match 2)))
115 (set! style (match:substring match 2)))
117 (string->number (match:substring match 3))))
119 (ly:warning (_ "can't decypher Pango description: ~a") str))
123 (format "font-style:~a;" style)
126 (format "font-family:~a;~afont-size:~a;text-anchor:west"
129 (/ size lily-unit-length))
132 ;;; FONT may be font smob, or pango font string
133 (define (svg-font font)
135 (pango-description-to-svg-font font)
136 (let ((name-style (font-name-style font))
137 (size (modified-font-metric-font-scaling font))
140 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
141 (car name-style) (cadr name-style)
144 (define (fontify font expr)
146 `(style . ,(svg-font font))
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 ;;; stencil outputters
153 ;;; catch-all for missing stuff
154 ;;; comment this out to see find out what functions you miss :-)
158 (define (dummy . foo) "")
159 (map (lambda (x) (module-define! this-module x dummy))
161 (ly:all-stencil-expressions)
162 (ly:all-output-backend-commands)))
165 (define (url-link url x y)
167 (eo 'a `(xlink:href . ,url))
171 `(width . ,(- (cdr x) (car x)))
172 `(height . ,(- (cdr y) (car y)))
175 '(stroke-width . "0.0"))
178 (define (grob-cause offset grob)
185 (define (rect-beam width slope thick blot-diameter)
190 ;; The stroke will stick out. To use stroke,
191 ;; the stroke-width must be subtracted from all other dimensions.
192 ;;'(stroke-linejoin . "round")
193 ;;'(stroke-linecap . "round")
194 ;;`(stroke-width . ,blot-diameter)
199 `(y . ,(- (/ thick 2)))
201 `(height . ,(+ thick (* (abs z) (/ thick 2))))
202 `(rx . ,(/ blot-diameter 2))
203 `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)" z)
206 (define (beam width slope thick blot-diameter)
207 (let* ((b blot-diameter)
212 '(stroke-linejoin . "round")
213 '(stroke-linecap . "round")
214 `(stroke-width . ,blot-diameter)
217 `(points . ,(string-join
219 (list (cons (/ b 2) (/ t 2))
220 (cons (+ w (/ b 2)) (+ h (/ t 2)))
221 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
222 (cons (/ b 2) (- (/ t 2)))))))
225 (define (path-beam width slope thick blot-diameter)
226 (let* ((b blot-diameter)
231 '(stroke-linejoin . "round")
232 '(stroke-linecap . "round")
233 `(stroke-width . ,blot-diameter)
236 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
244 (define (bezier-sandwich lst thick)
245 (let* ((first (list-tail lst 4))
246 (first-c0 (car (list-tail first 3)))
247 (second (list-head lst 4)))
249 '(stroke-linejoin . "round")
250 '(stroke-linecap . "round")
251 `(stroke-width . ,thick)
254 `(d . ,(string-append (svg-bezier first #f)
255 (svg-bezier second first-c0)))
258 (define (char font i)
260 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
262 (define-public (comment s)
263 (string-append "<!-- " s " !-->\n"))
265 (define (draw-line thick x1 y1 x2 y2 . alist)
267 (apply entity 'line ""
269 `((stroke-linejoin . "round")
270 (stroke-linecap . "round")
271 (stroke-width . ,thick)
280 (define (dashed-line thick on off dx dy)
281 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
283 ;; WTF is this in every backend?
284 (define (horizontal-line x1 x2 th)
285 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
287 (define (filledbox breapth width depth height)
288 (round-filled-box breapth width depth height 0))
290 (define (named-glyph font name)
292 `(fontify ,font ,(entity 'tspan
294 (ly:font-glyph-name-to-charcode font name))))))
296 (define (placebox x y expr)
301 `(transform . ,(format #f "translate (~f, ~f)"
305 (define (polygon coords blot-diameter)
307 '(stroke-linejoin . "round")
308 '(stroke-linecap . "round")
309 `(stroke-width . ,blot-diameter)
312 `(points . ,(string-join
313 (map offset->point (ly:list->offsets '() coords))))
316 (define (round-filled-box breapth width depth height blot-diameter)
318 ;; The stroke will stick out. To use stroke,
319 ;; the stroke-width must be subtracted from all other dimensions.
320 ;;'(stroke-linejoin . "round")
321 ;;'(stroke-linecap . "round")
322 ;;`(stroke-width . ,blot)
328 `(width . ,(+ breapth width))
329 `(height . ,(+ depth height))
330 `(ry . ,(/ blot-diameter 2))
333 (define (text font string)
334 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
336 (define (utf8-string pango-font-description string)
337 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))