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))
32 (define output-scale (* 2 scale-to-unit))
34 (define (dispatch expr)
35 (let ((keyword (car expr)))
37 ((eq? keyword 'some-func) "")
38 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
40 (if (module-defined? this-module keyword)
41 (apply (eval keyword this-module) (cdr expr))
44 (string-append "undefined: " (symbol->string keyword) "\n"))
48 (define-public (attributes attributes-alist)
50 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
53 (define-public (eo entity . attributes-alist)
54 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
56 (define-public (eoc entity . attributes-alist)
57 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
59 (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 ;;; FONT may be font smob, or pango font string
95 (define (svg-font font)
96 (let ((name-style (if (string? font)
98 (font-name-style font)))
99 (size (svg-font-size font))
101 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
102 (car name-style) (cadr name-style) size anchor)))
104 ;;; FONT may be font smob, or pango font string
105 (define (svg-font-size font)
108 (* output-scale (font-size font))))
110 (define (fontify font expr)
111 (entity 'text expr (cons 'style (svg-font font))))
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114 ;;; stencil outputters
117 ;;; catch-all for missing stuff
118 ;;; comment this out to see find out what functions you miss :-)
119 (define (dummy . foo) "")
120 (map (lambda (x) (module-define! this-module x dummy))
122 (ly:all-stencil-expressions)
123 (ly:all-output-backend-commands)))
125 (define (rect-beam width slope thick blot-diameter)
130 ;; The stroke will stick out. To use stroke,
131 ;; the stroke-width must be subtracted from all other dimensions.
132 ;;'(stroke-linejoin . "round")
133 ;;'(stroke-linecap . "round")
134 ;;`(stroke-width . ,blot-diameter)
139 `(y . ,(- (/ thick 2)))
141 `(height . ,(+ thick (* (abs z) (/ thick 2))))
142 `(rx . ,(/ blot-diameter 2))
143 `(transform . ,(string-append
144 (format #f "matrix (1, ~f, 0, 1, 0, 0)" (- z))
145 (format #f " scale (~f, ~f)"
146 output-scale output-scale))))))
148 (define (beam width slope thick blot-diameter)
149 (let* ((b blot-diameter)
154 '(stroke-linejoin . "round")
155 '(stroke-linecap . "round")
156 `(stroke-width . ,blot-diameter)
159 `(points . ,(string-join
161 (list (cons (/ b 2) (/ t 2))
162 (cons (+ w (/ b 2)) (+ h (/ t 2)))
163 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
164 (cons (/ b 2) (- (/ t 2)))))))
166 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
168 (define (path-beam width slope thick blot-diameter)
169 (let* ((b blot-diameter)
174 '(stroke-linejoin . "round")
175 '(stroke-linecap . "round")
176 `(stroke-width . ,blot-diameter)
179 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
186 . ,(format #f "scale (~f, ~f)" output-scale output-scale)))))
188 (define (bezier-sandwich lst thick)
189 (let* ((first (list-tail lst 4))
190 (first-c0 (car (list-tail first 3)))
191 (second (list-head lst 4)))
193 '(stroke-linejoin . "round")
194 '(stroke-linecap . "round")
195 `(stroke-width . ,thick)
198 `(d . ,(string-append (svg-bezier first #f)
199 (svg-bezier second first-c0)))
201 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
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 (dashed-line thick on off dx dy)
211 (draw-line thick 0 0 dx dy))
213 (define (draw-line thick x1 y1 x2 y2)
215 '(stroke-linejoin . "round")
216 '(stroke-linecap . "round")
217 `(stroke-width . ,thick)
225 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
227 ;; WTF is this in every backend?
228 (define (horizontal-line x1 x2 th)
229 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
231 (define (filledbox breapth width depth height)
232 (round-filled-box breapth width depth height 0))
234 (define (named-glyph font name)
236 `(fontify ,font ,(entity 'tspan
238 (ly:font-glyph-name-to-charcode font name))))))
240 (define (placebox x y expr)
245 `(transform . ,(format #f "translate (~f, ~f)"
247 (- (* output-scale y))))))
249 (define (polygon coords blot-diameter)
251 '(stroke-linejoin . "round")
252 '(stroke-linecap . "round")
253 `(stroke-width . ,blot-diameter)
256 `(points . ,(string-join
257 (map offset->point (ly:list->offsets '() coords))))
259 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
261 (define (round-filled-box breapth width depth height blot-diameter)
263 ;; The stroke will stick out. To use stroke,
264 ;; the stroke-width must be subtracted from all other dimensions.
265 ;;'(stroke-linejoin . "round")
266 ;;'(stroke-linecap . "round")
267 ;;`(stroke-width . ,blot)
273 `(width . ,(+ breapth width))
274 `(height . ,(+ depth height))
275 `(ry . ,(/ blot-diameter 2))
277 . ,(format #f "scale (~f, ~f)" output-scale output-scale))))
279 (define (text font string)
280 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
282 (define (utf8-string pango-font-description string)
283 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))