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)
53 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
55 (define-public (eoc entity . attributes-alist)
56 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
58 (define-public (ec entity)
59 (format #f "</~S>\n" entity))
61 (define-public (entity entity string . attributes-alist)
62 (if (equal? string "")
63 (apply eoc entity attributes-alist)
65 (apply eo (cons entity attributes-alist)) string (ec entity))))
67 (define (offset->point o)
68 (format #f " ~S,~S" (car o) (- (cdr o))))
70 (define (svg-bezier lst close)
71 (let* ((c0 (car (list-tail lst 3)))
72 (c123 (list-head lst 3)))
74 (if (not close) "M " "L ")
76 "C " (apply string-append (map offset->point c123))
77 (if (not close) "" (string-append
78 "L " (offset->point close))))))
83 (define (integer->entity integer)
84 (format #f "&#x~x;" integer))
86 (define (char->entity char)
87 (integer->entity (char->integer char)))
89 (define (string->entities string)
91 (map (lambda (x) (char->entity x)) (string->list string))))
93 (define pango-description-regexp-comma
94 (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
96 (define pango-description-regexp-nocomma
97 (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
99 (define (pango-description-to-svg-font str)
104 (match-1 (regexp-exec pango-description-regexp-comma str))
105 (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 (display (format "Cannot decypher Pango description: ~a\n" 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 :-)
155 (define (dummy . foo) "")
156 (map (lambda (x) (module-define! this-module x dummy))
158 (ly:all-stencil-expressions)
159 (ly:all-output-backend-commands)))
161 (define (rect-beam width slope thick blot-diameter)
166 ;; The stroke will stick out. To use stroke,
167 ;; the stroke-width must be subtracted from all other dimensions.
168 ;;'(stroke-linejoin . "round")
169 ;;'(stroke-linecap . "round")
170 ;;`(stroke-width . ,blot-diameter)
175 `(y . ,(- (/ thick 2)))
177 `(height . ,(+ thick (* (abs z) (/ thick 2))))
178 `(rx . ,(/ blot-diameter 2))
179 `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)" z)
182 (define (beam width slope thick blot-diameter)
183 (let* ((b blot-diameter)
188 '(stroke-linejoin . "round")
189 '(stroke-linecap . "round")
190 `(stroke-width . ,blot-diameter)
193 `(points . ,(string-join
195 (list (cons (/ b 2) (/ t 2))
196 (cons (+ w (/ b 2)) (+ h (/ t 2)))
197 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
198 (cons (/ b 2) (- (/ t 2)))))))
201 (define (path-beam width slope thick blot-diameter)
202 (let* ((b blot-diameter)
207 '(stroke-linejoin . "round")
208 '(stroke-linecap . "round")
209 `(stroke-width . ,blot-diameter)
212 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
220 (define (bezier-sandwich lst thick)
221 (let* ((first (list-tail lst 4))
222 (first-c0 (car (list-tail first 3)))
223 (second (list-head lst 4)))
225 '(stroke-linejoin . "round")
226 '(stroke-linecap . "round")
227 `(stroke-width . ,thick)
230 `(d . ,(string-append (svg-bezier first #f)
231 (svg-bezier second first-c0)))
234 (define (char font i)
236 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
238 (define-public (comment s)
239 (string-append "<!-- " s " !-->\n"))
241 (define (draw-line thick x1 y1 x2 y2 . alist)
243 (apply entity 'line ""
245 `((stroke-linejoin . "round")
246 (stroke-linecap . "round")
247 (stroke-width . ,thick)
256 (define (dashed-line thick on off dx dy)
257 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
259 ;; WTF is this in every backend?
260 (define (horizontal-line x1 x2 th)
261 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
263 (define (filledbox breapth width depth height)
264 (round-filled-box breapth width depth height 0))
266 (define (named-glyph font name)
268 `(fontify ,font ,(entity 'tspan
270 (ly:font-glyph-name-to-charcode font name))))))
272 (define (placebox x y expr)
277 `(transform . ,(format #f "translate (~f, ~f)"
281 (define (polygon coords blot-diameter)
283 '(stroke-linejoin . "round")
284 '(stroke-linecap . "round")
285 `(stroke-width . ,blot-diameter)
288 `(points . ,(string-join
289 (map offset->point (ly:list->offsets '() coords))))
292 (define (round-filled-box breapth width depth height blot-diameter)
294 ;; The stroke will stick out. To use stroke,
295 ;; the stroke-width must be subtracted from all other dimensions.
296 ;;'(stroke-linejoin . "round")
297 ;;'(stroke-linecap . "round")
298 ;;`(stroke-width . ,blot)
304 `(width . ,(+ breapth width))
305 `(height . ,(+ depth height))
306 `(ry . ,(/ blot-diameter 2))
309 (define (text font string)
310 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
312 (define (utf8-string pango-font-description string)
313 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))