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 (debug-enable 'backtrace)
21 (define-module (scm output-svg))
22 (define this-module (current-module))
31 (define lily-unit-length 1.75)
33 (define (dispatch expr)
34 (let ((keyword (car expr)))
36 ((eq? keyword 'some-func) "")
37 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
39 (if (module-defined? this-module keyword)
40 (apply (eval keyword this-module) (cdr expr))
43 (string-append "undefined: " (symbol->string keyword) "\n"))
47 (define-public (attributes attributes-alist)
49 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
52 (define-public (eo entity . attributes-alist)
54 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
56 (define-public (eoc entity . attributes-alist)
58 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
60 (define-public (ec entity)
62 (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 (display (format "Cannot decypher Pango description: ~a\n" 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))
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;;; stencil outputters
155 ;;; catch-all for missing stuff
156 ;;; comment this out to see find out what functions you miss :-)
160 (define (dummy . foo) "")
161 (map (lambda (x) (module-define! this-module x dummy))
163 (ly:all-stencil-expressions)
164 (ly:all-output-backend-commands)))
167 (define (url-link url x y)
169 (eo 'a `(xlink:href . ,url))
173 `(width . ,(- (cdr x) (car x)))
174 `(height . ,(- (cdr y) (car y)))
177 '(stroke-width . "0.0"))
180 (define (grob-cause offset grob)
187 (define (rect-beam width slope thick blot-diameter)
192 ;; The stroke will stick out. To use stroke,
193 ;; the stroke-width must be subtracted from all other dimensions.
194 ;;'(stroke-linejoin . "round")
195 ;;'(stroke-linecap . "round")
196 ;;`(stroke-width . ,blot-diameter)
201 `(y . ,(- (/ thick 2)))
203 `(height . ,(+ thick (* (abs z) (/ thick 2))))
204 `(rx . ,(/ blot-diameter 2))
205 `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)" z)
208 (define (beam width slope thick blot-diameter)
209 (let* ((b blot-diameter)
214 '(stroke-linejoin . "round")
215 '(stroke-linecap . "round")
216 `(stroke-width . ,blot-diameter)
219 `(points . ,(string-join
221 (list (cons (/ b 2) (/ t 2))
222 (cons (+ w (/ b 2)) (+ h (/ t 2)))
223 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
224 (cons (/ b 2) (- (/ t 2)))))))
227 (define (path-beam width slope thick blot-diameter)
228 (let* ((b blot-diameter)
233 '(stroke-linejoin . "round")
234 '(stroke-linecap . "round")
235 `(stroke-width . ,blot-diameter)
238 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
246 (define (bezier-sandwich lst thick)
247 (let* ((first (list-tail lst 4))
248 (first-c0 (car (list-tail first 3)))
249 (second (list-head lst 4)))
251 '(stroke-linejoin . "round")
252 '(stroke-linecap . "round")
253 `(stroke-width . ,thick)
256 `(d . ,(string-append (svg-bezier first #f)
257 (svg-bezier second first-c0)))
260 (define (char font i)
262 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
264 (define-public (comment s)
265 (string-append "<!-- " s " !-->\n"))
267 (define (draw-line thick x1 y1 x2 y2 . alist)
269 (apply entity 'line ""
271 `((stroke-linejoin . "round")
272 (stroke-linecap . "round")
273 (stroke-width . ,thick)
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 ;; WTF is this in every backend?
286 (define (horizontal-line x1 x2 th)
287 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
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)"
307 (define (polygon coords blot-diameter)
309 '(stroke-linejoin . "round")
310 '(stroke-linecap . "round")
311 `(stroke-width . ,blot-diameter)
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 (text font string)
336 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
338 (define (utf8-string pango-font-description string)
339 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))