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))
34 (debug-enable 'backtrace)
35 (read-enable 'positions)))
36 (define lily-unit-length 1.75)
38 (define (dispatch expr)
39 (let ((keyword (car expr)))
41 ((eq? keyword 'some-func) "")
42 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
44 (if (module-defined? this-module keyword)
45 (apply (eval keyword this-module) (cdr expr))
48 (string-append "undefined: " (symbol->string keyword) "\n"))
52 (define-public (attributes attributes-alist)
54 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
57 (define-public (eo entity . attributes-alist)
58 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
60 (define-public (eoc entity . attributes-alist)
61 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
63 (define-public (ec entity)
64 (format #f "</~S>\n" entity))
66 (define-public (entity entity string . attributes-alist)
67 (if (equal? string "")
68 (apply eoc entity attributes-alist)
70 (apply eo (cons entity attributes-alist)) string (ec entity))))
72 (define (offset->point o)
73 (format #f " ~S,~S" (car o) (cdr o)))
75 (define (svg-bezier lst close)
76 (let* ((c0 (car (list-tail lst 3)))
77 (c123 (list-head lst 3)))
79 (if (not close) "M " "L ")
81 "C " (apply string-append (map offset->point c123))
82 (if (not close) "" (string-append
83 "L " (offset->point close))))))
88 (define (integer->entity integer)
89 (format #f "&#x~x;" integer))
91 (define (char->entity char)
92 (integer->entity (char->integer char)))
94 (define (string->entities string)
96 (map (lambda (x) (char->entity x)) (string->list string))))
98 (define pango-description-regexp
99 (make-regexp "^([^,]+)+, ?([-a-zA-Z_]*) ([0-9.]+)$"))
101 (define (pango-description-to-svg-font str)
106 (match (regexp-exec pango-description-regexp str)))
108 (if (regexp-match? match)
110 (set! family (match:substring match 1))
111 (if (< 0 (string-length (match:substring match 2)))
112 (set! style (match:substring match 2)))
114 (string->number (match:substring match 3))))
116 (display (format "Cannot decypher Pango description: ~a\n" str)))
120 (format "font-style:~a;" style)
123 (format "font-family:~a;~afont-size:~a;text-anchor:west"
126 (/ size lily-unit-length))
129 ;;; FONT may be font smob, or pango font string
130 (define (svg-font font)
132 (pango-description-to-svg-font font)
133 (let ((name-style (font-name-style font))
134 (size (modified-font-metric-font-scaling font))
137 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
138 (car name-style) (cadr name-style)
141 (define (fontify font expr)
142 (entity 'text expr (cons 'style (svg-font font))))
144 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
145 ;;; stencil outputters
148 ;;; catch-all for missing stuff
149 ;;; comment this out to see find out what functions you miss :-)
150 (define (dummy . foo) "")
151 (map (lambda (x) (module-define! this-module x dummy))
153 (ly:all-stencil-expressions)
154 (ly:all-output-backend-commands)))
156 (define (rect-beam width slope thick blot-diameter)
161 ;; The stroke will stick out. To use stroke,
162 ;; the stroke-width must be subtracted from all other dimensions.
163 ;;'(stroke-linejoin . "round")
164 ;;'(stroke-linecap . "round")
165 ;;`(stroke-width . ,blot-diameter)
170 `(y . ,(- (/ thick 2)))
172 `(height . ,(+ thick (* (abs z) (/ thick 2))))
173 `(rx . ,(/ blot-diameter 2))
174 `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)" (- z))
177 (define (beam width slope thick blot-diameter)
178 (let* ((b blot-diameter)
183 '(stroke-linejoin . "round")
184 '(stroke-linecap . "round")
185 `(stroke-width . ,blot-diameter)
188 `(points . ,(string-join
190 (list (cons (/ b 2) (/ t 2))
191 (cons (+ w (/ b 2)) (+ h (/ t 2)))
192 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
193 (cons (/ b 2) (- (/ t 2)))))))
196 (define (path-beam width slope thick blot-diameter)
197 (let* ((b blot-diameter)
202 '(stroke-linejoin . "round")
203 '(stroke-linecap . "round")
204 `(stroke-width . ,blot-diameter)
207 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
215 (define (bezier-sandwich lst thick)
216 (let* ((first (list-tail lst 4))
217 (first-c0 (car (list-tail first 3)))
218 (second (list-head lst 4)))
220 '(stroke-linejoin . "round")
221 '(stroke-linecap . "round")
222 `(stroke-width . ,thick)
225 `(d . ,(string-append (svg-bezier first #f)
226 (svg-bezier second first-c0)))
229 (define (char font i)
231 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
233 (define-public (comment s)
234 (string-append "<!-- " s " !-->\n"))
236 (define (dashed-line thick on off dx dy)
237 (draw-line thick 0 0 dx dy))
239 (define (draw-line thick x1 y1 x2 y2)
241 '(stroke-linejoin . "round")
242 '(stroke-linecap . "round")
243 `(stroke-width . ,thick)
252 ;; WTF is this in every backend?
253 (define (horizontal-line x1 x2 th)
254 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
256 (define (filledbox breapth width depth height)
257 (round-filled-box breapth width depth height 0))
259 (define (named-glyph font name)
261 `(fontify ,font ,(entity 'tspan
263 (ly:font-glyph-name-to-charcode font name))))))
265 (define (placebox x y expr)
270 `(transform . ,(format #f "translate (~f, ~f)"
274 (define (polygon coords blot-diameter)
276 '(stroke-linejoin . "round")
277 '(stroke-linecap . "round")
278 `(stroke-width . ,blot-diameter)
281 `(points . ,(string-join
282 (map offset->point (ly:list->offsets '() coords))))
285 (define (round-filled-box breapth width depth height blot-diameter)
287 ;; The stroke will stick out. To use stroke,
288 ;; the stroke-width must be subtracted from all other dimensions.
289 ;;'(stroke-linejoin . "round")
290 ;;'(stroke-linecap . "round")
291 ;;`(stroke-width . ,blot)
297 `(width . ,(+ breapth width))
298 `(height . ,(+ depth height))
299 `(ry . ,(/ blot-diameter 2))
302 (define (text font string)
303 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
305 (define (utf8-string pango-font-description string)
306 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))