1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2002--2004 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?
14 ;;;; * inkscape page/pageSet support
15 ;;;; * inkscape SVG-font support
16 ;;;; - use fontconfig/fc-cache for now, see output-gnome.scm
18 (debug-enable 'backtrace)
19 (define-module (scm output-svg))
20 (define this-module (current-module))
30 (define output-scale (* 2 scale-to-unit))
32 (define (debugf string . rest)
34 (apply stderr (cons string rest))))
36 (define (dispatch expr)
37 (let ((keyword (car expr)))
39 ((eq? keyword 'some-func) "")
40 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
42 (if (module-defined? this-module keyword)
43 (apply (eval keyword this-module) (cdr expr))
46 (string-append "undefined: " (symbol->string keyword) "\n"))
50 (define-public (attributes attributes-alist)
52 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
55 (define-public (eo entity . attributes-alist)
56 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
58 (define-public (eoc entity . attributes-alist)
59 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
61 (define-public (ec entity)
62 (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 (font-size font)
87 (let* ((designsize (ly:font-design-size font))
88 (magnification (* (ly:font-magnification font)))
89 (scaling (* output-scale magnification designsize)))
90 (debugf "scaling:~S\n" scaling)
91 (debugf "magnification:~S\n" magnification)
92 (debugf "design:~S\n" designsize)
95 (define (integer->entity integer)
96 (format #f "&#x~x;" integer))
98 (define (char->entity char)
99 (integer->entity (char->integer char)))
101 (define (string->entities string)
103 (map (lambda (x) (char->entity x)) (string->list string))))
105 ;; FIXME: font can be pango font-name or smob
106 ;; determine size and style properly.
107 (define (svg-font font)
108 (let ((family (if (string? font) font (font-family font)))
109 (size (if (string? font) 12 (font-size font)))
111 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
113 (otf-style-mangling font family)
116 (define (fontify font expr)
117 (entity 'text expr (cons 'style (svg-font font))))
119 (define-public (otf-style-mangling font family)
120 ;; Hmm, family is emmentaler20/26?
121 (if (string=? (substring family 0 (min (string-length family) 10))
123 ;; urg; currently empty
124 ;;(substring family 10)
128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129 ;;; stencil outputters
132 ;;; catch-all for missing stuff
133 ;;; comment this out to see find out what functions you miss :-)
134 (define (dummy . foo) "")
135 (map (lambda (x) (module-define! this-module x dummy))
137 (ly:all-stencil-expressions)
138 (ly:all-output-backend-commands)))
140 (define (rect-beam width slope thick blot-diameter)
145 ;; The stroke will stick out. To use stroke,
146 ;; the stroke-width must be subtracted from all other dimensions.
147 ;;'(stroke-linejoin . "round")
148 ;;'(stroke-linecap . "round")
149 ;;`(stroke-width . ,blot-diameter)
154 `(y . ,(- (/ thick 2)))
156 `(height . ,(+ thick (* (abs z) (/ thick 2))))
157 `(rx . ,(/ blot-diameter 2))
158 `(transform . ,(string-append
159 (format #f "matrix (1, ~f, 0, 1, 0, 0)" (- z))
160 (format #f " scale (~f, ~f)"
161 output-scale output-scale))))))
163 (define (beam width slope thick blot-diameter)
164 (let* ((b blot-diameter)
169 '(stroke-linejoin . "round")
170 '(stroke-linecap . "round")
171 `(stroke-width . ,blot-diameter)
174 `(points . ,(string-join
176 (list (cons (/ b 2) (/ t 2))
177 (cons (+ w (/ b 2)) (+ h (/ t 2)))
178 (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
179 (cons (/ b 2) (- (/ t 2)))))))
181 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
183 (define (path-beam width slope thick blot-diameter)
184 (let* ((b blot-diameter)
189 '(stroke-linejoin . "round")
190 '(stroke-linecap . "round")
191 `(stroke-width . ,blot-diameter)
194 `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
201 . ,(format #f "scale (~f, ~f)" output-scale output-scale)))))
203 (define (bezier-sandwich lst thick)
204 (let* ((first (list-tail lst 4))
205 (first-c0 (car (list-tail first 3)))
206 (second (list-head lst 4)))
208 '(stroke-linejoin . "round")
209 '(stroke-linecap . "round")
210 `(stroke-width . ,thick)
213 `(d . ,(string-append (svg-bezier first #f)
214 (svg-bezier second first-c0)))
216 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
218 (define (char font i)
220 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
222 (define-public (comment s)
223 (string-append "<!-- " s " !-->\n"))
225 (define (dashed-line thick on off dx dy)
226 (draw-line thick 0 0 dx dy))
228 (define (draw-line thick x1 y1 x2 y2)
230 '(stroke-linejoin . "round")
231 '(stroke-linecap . "round")
232 `(stroke-width . ,thick)
240 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
242 ;; WTF is this in every backend?
243 (define (horizontal-line x1 x2 th)
244 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
246 (define (filledbox breapth width depth height)
247 (round-filled-box breapth width depth height 0))
249 (define (named-glyph font name)
251 `(fontify ,font ,(entity 'tspan
253 (ly:font-glyph-name-to-charcode font name))))))
255 (define (placebox x y expr)
260 `(transform . ,(format #f "translate (~f, ~f)"
262 (- (* output-scale y))))))
264 (define (polygon coords blot-diameter)
266 '(stroke-linejoin . "round")
267 '(stroke-linecap . "round")
268 `(stroke-width . ,blot-diameter)
271 `(points . ,(string-join
272 (map offset->point (ly:list->offsets '() coords))))
274 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
276 (define (round-filled-box breapth width depth height blot-diameter)
278 ;; The stroke will stick out. To use stroke,
279 ;; the stroke-width must be subtracted from all other dimensions.
280 ;;'(stroke-linejoin . "round")
281 ;;'(stroke-linecap . "round")
282 ;;`(stroke-width . ,blot)
288 `(width . ,(+ breapth width))
289 `(height . ,(+ depth height))
290 `(ry . ,(/ blot-diameter 2))
292 . ,(format #f "scale (~f, ~f)" output-scale output-scale))))
294 (define (text font string)
295 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
297 (define (utf8-string pango-font-description string)
298 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))