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 ;;;; * font selection: name, size, design size
12 ;;;; * .cff MUST NOT be in fc's fontpath.
13 ;;;; - workaround: remove mf/out from ~/.fonts.conf,
14 ;;;; instead add ~/.fonts and symlink all /mf/out/*otf there.
15 ;;;; - bug in fontconfig/freetype/pango?
17 ;;;; * inkscape page/pageSet support
18 ;;;; * inkscape SVG-font support
19 ;;;; - use fontconfig/fc-cache for now, see output-gnome.scm
21 (debug-enable 'backtrace)
22 (define-module (scm output-svg))
23 (define this-module (current-module))
33 (define output-scale (* 2 scale-to-unit))
35 (define (debugf string . rest)
37 (apply stderr (cons string rest))))
39 (define (dispatch expr)
40 (let ((keyword (car expr)))
42 ((eq? keyword 'some-func) "")
43 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
45 (if (module-defined? this-module keyword)
46 (apply (eval keyword this-module) (cdr expr))
49 (string-append "undefined: " (symbol->string keyword) "\n"))
53 (define-public (attributes attributes-alist)
55 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
58 (define-public (eo entity . attributes-alist)
59 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
61 (define-public (eoc entity . attributes-alist)
62 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
64 (define-public (ec entity)
65 (format #f "</~S>\n" entity))
67 (define-public (entity entity string . attributes-alist)
68 (if (equal? string "")
69 (apply eoc entity attributes-alist)
71 (apply eo (cons entity attributes-alist)) string (ec entity))))
73 (define (offset->point o)
74 (format #f " ~S,~S" (car o) (cdr o)))
76 (define (svg-bezier lst close)
77 (let* ((c0 (car (list-tail lst 3)))
78 (c123 (list-head lst 3)))
80 (if (not close) "M " "L ")
82 "C " (apply string-append (map offset->point c123))
83 (if (not close) "" (string-append
84 "L " (offset->point close))))))
89 (define (font-size font)
90 (let* ((designsize (ly:font-design-size font))
91 (magnification (* (ly:font-magnification font)))
92 (scaling (* output-scale magnification designsize)))
93 (debugf "scaling:~S\n" scaling)
94 (debugf "magnification:~S\n" magnification)
95 (debugf "design:~S\n" designsize)
98 (define (integer->entity integer)
99 (format #f "&#x~x;" integer))
101 (define (char->entity char)
102 (integer->entity (char->integer char)))
104 (define (string->entities string)
106 (map (lambda (x) (char->entity x)) (string->list string))))
108 ;; FIXME: font can be pango font-name or smob
109 ;; determine size and style properly.
110 (define (svg-font font)
111 (let ((name-style (if (string? font) (list font "Regular")
112 (font-name-style font)))
113 (size (if (string? font) 12 (font-size font)))
115 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
120 (define (fontify font expr)
121 (entity 'text expr (cons 'style (svg-font font))))
123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 ;;; stencil outputters
127 ;;; catch-all for missing stuff
128 ;;; comment this out to see find out what functions you miss :-)
129 (define (dummy . foo) "")
130 (map (lambda (x) (module-define! this-module x dummy))
132 (ly:all-stencil-expressions)
133 (ly:all-output-backend-commands)))
135 (define (rect-beam width slope thick blot-diameter)
140 ;; The stroke will stick out. To use stroke,
141 ;; the stroke-width must be subtracted from all other dimensions.
142 ;;'(stroke-linejoin . "round")
143 ;;'(stroke-linecap . "round")
144 ;;`(stroke-width . ,blot-diameter)
149 `(y . ,(- (/ thick 2)))
151 `(height . ,(+ thick (* (abs z) (/ thick 2))))
152 `(rx . ,(/ blot-diameter 2))
153 `(transform . ,(string-append
154 (format #f "matrix (1, ~f, 0, 1, 0, 0)" (- z))
155 (format #f " scale (~f, ~f)"
156 output-scale output-scale))))))
158 (define (beam width slope thick blot-diameter)
159 (let* ((b blot-diameter)
164 '(stroke-linejoin . "round")
165 '(stroke-linecap . "round")
166 `(stroke-width . ,blot-diameter)
169 `(points . ,(string-join
171 (list (cons (/ b 2) (/ t 2))
172 (cons (+ w (/ b 2)) (+ h (/ t 2)))
173 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
174 (cons (/ b 2) (- (/ t 2)))))))
176 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
178 (define (path-beam width slope thick blot-diameter)
179 (let* ((b blot-diameter)
184 '(stroke-linejoin . "round")
185 '(stroke-linecap . "round")
186 `(stroke-width . ,blot-diameter)
189 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
196 . ,(format #f "scale (~f, ~f)" output-scale output-scale)))))
198 (define (bezier-sandwich lst thick)
199 (let* ((first (list-tail lst 4))
200 (first-c0 (car (list-tail first 3)))
201 (second (list-head lst 4)))
203 '(stroke-linejoin . "round")
204 '(stroke-linecap . "round")
205 `(stroke-width . ,thick)
208 `(d . ,(string-append (svg-bezier first #f)
209 (svg-bezier second first-c0)))
211 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
213 (define (char font i)
215 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
217 (define-public (comment s)
218 (string-append "<!-- " s " !-->\n"))
220 (define (dashed-line thick on off dx dy)
221 (draw-line thick 0 0 dx dy))
223 (define (draw-line thick x1 y1 x2 y2)
225 '(stroke-linejoin . "round")
226 '(stroke-linecap . "round")
227 `(stroke-width . ,thick)
235 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
237 ;; WTF is this in every backend?
238 (define (horizontal-line x1 x2 th)
239 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
241 (define (filledbox breapth width depth height)
242 (round-filled-box breapth width depth height 0))
244 (define (named-glyph font name)
246 `(fontify ,font ,(entity 'tspan
248 (ly:font-glyph-name-to-charcode font name))))))
250 (define (placebox x y expr)
255 `(transform . ,(format #f "translate (~f, ~f)"
257 (- (* output-scale y))))))
259 (define (polygon coords blot-diameter)
261 '(stroke-linejoin . "round")
262 '(stroke-linecap . "round")
263 `(stroke-width . ,blot-diameter)
266 `(points . ,(string-join
267 (map offset->point (ly:list->offsets '() coords))))
269 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
271 (define (round-filled-box breapth width depth height blot-diameter)
273 ;; The stroke will stick out. To use stroke,
274 ;; the stroke-width must be subtracted from all other dimensions.
275 ;;'(stroke-linejoin . "round")
276 ;;'(stroke-linecap . "round")
277 ;;`(stroke-width . ,blot)
283 `(width . ,(+ breapth width))
284 `(height . ,(+ depth height))
285 `(ry . ,(/ blot-diameter 2))
287 . ,(format #f "scale (~f, ~f)" output-scale output-scale))))
289 (define (text font string)
290 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
292 (define (utf8-string pango-font-description string)
293 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))