1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2002--2006 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))
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 (ly:warning (_ "can't decypher Pango description: ~a") 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))
149 '(fill . "currentColor")
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
153 ;;; stencil outputters
156 ;;; catch-all for missing stuff
157 ;;; comment this out to see find out what functions you miss :-)
161 (define (dummy . foo) "")
162 (map (lambda (x) (module-define! this-module x dummy))
164 (ly:all-stencil-expressions)
165 (ly:all-output-backend-commands)))
168 (define (url-link url x y)
170 (eo 'a `(xlink:href . ,url))
174 `(width . ,(- (cdr x) (car x)))
175 `(height . ,(- (cdr y) (car y)))
178 '(stroke-width . "0.0"))
181 (define (grob-cause offset grob)
189 (define (bezier-sandwich lst thick)
190 (let* ((first (list-tail lst 4))
191 (first-c0 (car (list-tail first 3)))
192 (second (list-head lst 4)))
194 '(stroke-linejoin . "round")
195 '(stroke-linecap . "round")
196 `(stroke-width . ,thick)
197 '(stroke . "currentColor")
198 '(fill . "currentColor")
199 `(d . ,(string-append (svg-bezier first #f)
200 (svg-bezier second first-c0)))
203 (define (char font i)
205 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
207 (define-public (comment s)
208 (string-append "<!-- " s " !-->\n"))
210 (define (draw-line thick x1 y1 x2 y2 . alist)
212 (apply entity 'line ""
214 `((stroke-linejoin . "round")
215 (stroke-linecap . "round")
216 (stroke-width . ,thick)
217 (stroke . "currentColor")
224 (define (dashed-line thick on off dx dy)
225 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
227 (define (named-glyph font name)
229 `(fontify ,font ,(entity 'tspan
231 (ly:font-glyph-name-to-charcode font name))))))
233 (define (placebox x y expr)
239 `(transform . ,(format #f "translate(~f, ~f)"
242 (define (polygon coords blot-diameter is-filled)
245 '(stroke-linejoin . "round")
246 '(stroke-linecap . "round")
247 `(stroke-width . ,blot-diameter)
248 `(fill . ,(if is-filled "currentColor" "none"))
249 '(stroke . "currentColor")
250 `(points . ,(string-join
251 (map offset->point (ly:list->offsets '() coords))))
254 (define (round-filled-box breapth width depth height blot-diameter)
256 ;; The stroke will stick out. To use stroke,
257 ;; the stroke-width must be subtracted from all other dimensions.
258 ;;'(stroke-linejoin . "round")
259 ;;'(stroke-linecap . "round")
260 ;;`(stroke-width . ,blot)
266 `(width . ,(+ breapth width))
267 `(height . ,(+ depth height))
268 `(ry . ,(/ blot-diameter 2))
271 (define (circle radius thick is-filled)
274 '(stroke-linejoin . "round")
275 '(stroke-linecap . "round")
276 `(fill . ,(if is-filled "currentColor" "none"))
277 `(stroke . "currentColor")
278 `(stroke-width . ,thick)
281 (define (text font string)
282 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
284 (define (utf-8-string pango-font-description string)
285 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
289 (define (setcolor r g b)
290 (format "<g color=\"rgb(~a%,~a%,~a%)\">"
291 (* 100 r) (* 100 g) (* 100 b)