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
10 ;;;; * check: blot+scaling
11 ;;;; * inkscape page/pageSet support
12 ;;;; * inkscape SVG-font support
13 ;;;; - use fontconfig/fc-cache for now, see output-gnome.scm
15 (debug-enable 'backtrace)
16 (define-module (scm output-svg))
17 (define this-module (current-module))
27 (define output-scale (* 2 scale-to-unit))
29 (define (debugf string . rest)
31 (apply stderr (cons string rest))))
33 (define (dispatch expr)
34 (let ((keyword (car expr)))
36 ((eq? keyword 'some-func) "")
37 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
39 (if (module-defined? this-module keyword)
40 (apply (eval keyword this-module) (cdr expr))
43 (string-append "undefined: " (symbol->string keyword) "\n"))
47 (define-public (attributes attributes-alist)
49 (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
52 (define-public (eo entity . attributes-alist)
53 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
55 (define-public (eoc entity . attributes-alist)
56 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
58 (define-public (ec entity)
59 (format #f "</~S>\n" entity))
61 (define-public (entity entity string . attributes-alist)
62 (if (equal? string "")
63 (apply eoc entity attributes-alist)
65 (apply eo (cons entity attributes-alist)) string (ec entity))))
67 (define (offset->point o)
68 (format #f " ~S,~S" (car o) (cdr o)))
70 (define (svg-bezier lst close)
71 (let* ((c0 (car (list-tail lst 3)))
72 (c123 (list-head lst 3)))
74 (if (not close) "M " "L ")
76 "C " (apply string-append (map offset->point c123))
77 (if (not close) "" (string-append
78 "L " (offset->point close))))))
83 (define (font-size font)
84 (let* ((designsize (ly:font-design-size font))
85 (magnification (* (ly:font-magnification font)))
87 (scaling (* ops magnification designsize)))
88 (debugf "scaling:~S\n" scaling)
89 (debugf "magnification:~S\n" magnification)
90 (debugf "design:~S\n" designsize)
93 (define (integer->entity integer)
94 (format #f "&#x~x;" integer))
96 (define (char->entity char)
97 (integer->entity (char->integer char)))
99 (define (string->entities string)
101 (map (lambda (x) (char->entity x)) (string->list string))))
103 (define (svg-font font)
104 (let* ((encoding (ly:font-encoding font))
105 (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start))
106 (family (font-family font)))
107 (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~S;"
108 (otf-name-mangling font family)
109 (otf-style-mangling font family)
110 (font-size font) anchor)))
112 (define (fontify font expr)
113 (entity 'text expr (cons 'style (svg-font font))))
116 (define-public (otf-name-mangling font family)
117 ;; Hmm, family is bigcheese20/26?
118 (if (string=? (substring family 0 (min (string-length family) 9))
121 (if (string=? family "aybabtu")
125 (define-public (otf-style-mangling font family)
126 ;; Hmm, family is bigcheese20/26?
127 (if (string=? (substring family 0 (min (string-length family) 9))
132 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
133 ;;; stencil outputters
136 ;;; catch-all for missing stuff
137 ;;; comment this out to see find out what functions you miss :-)
138 (define (dummy . foo) "")
139 (map (lambda (x) (module-define! this-module x dummy))
141 (ly:all-stencil-expressions)
142 (ly:all-output-backend-commands)))
144 (define (beam width slope thick blot-diameter)
147 (z (sqrt (+ (sqr x) (sqr y)))))
149 ;; The stroke will stick out. To use stroke,
150 ;; the stroke-width must be subtracted from all other dimensions.
151 ;;'(stroke-linejoin . "round")
152 ;;'(stroke-linecap . "round")
153 ;;`(stroke-width . ,blot-diameter)
158 `(y . ,(- (/ thick 2)))
159 `(width . ,(+ width (* slope blot-diameter)))
161 `(rx . ,(/ blot-diameter 2))
162 `(transform . ,(string-append
163 (format #f "matrix (~f, ~f, 0, 1, 0, 0)"
164 (/ x z) (* -1 (/ y z)))
165 (format #f " scale (~f, ~f)"
166 output-scale output-scale))))))
168 (define (bezier-sandwich lst thick)
169 (let* ((first (list-tail lst 4))
170 (first-c0 (car (list-tail first 3)))
171 (second (list-head lst 4)))
173 '(stroke-linejoin . "round")
174 '(stroke-linecap . "round")
175 `(stroke-width . ,thick)
178 `(d . ,(string-append (svg-bezier first #f)
179 (svg-bezier second first-c0)))
181 . ,(format #f "scale (~f, -~f)" output-scale output-scale)))))
183 (define (char font i)
185 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
187 (define-public (comment s)
188 (string-append "<!-- " s " !-->\n"))
190 (define (dashed-line thick on off dx dy)
191 (draw-line thick 0 0 dx dy))
193 (define (draw-line thick x1 y1 x2 y2)
195 '(stroke-linejoin . "round")
196 '(stroke-linecap . "round")
197 `(stroke-width . ,thick)
205 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
207 ;; WTF is this in every backend?
208 (define (horizontal-line x1 x2 th)
209 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
211 (define (filledbox breapth width depth height)
212 (round-filled-box breapth width depth height 0))
214 (define (named-glyph font name)
216 `(fontify ,font ,(entity 'tspan
218 (ly:font-glyph-name-to-charcode font name))))))
220 (define (placebox x y expr)
225 `(transform . ,(format #f "translate (~f, ~f)"
227 (- (* output-scale y))))))
229 (define (polygon coords blot-diameter)
231 '(stroke-linejoin . "round")
232 '(stroke-linecap . "round")
233 `(stroke-width . ,blot-diameter)
236 `(points . ,(string-join
237 (map offset->point (ly:list->offsets '() coords))))
239 . ,(format #f "scale (~f, -~f)" output-scale output-scale))))
241 (define (round-filled-box breapth width depth height blot-diameter)
243 ;; The stroke will stick out. To use stroke,
244 ;; the stroke-width must be subtracted from all other dimensions.
245 ;;'(stroke-linejoin . "round")
246 ;;'(stroke-linecap . "round")
247 ;;`(stroke-width . ,blot)
253 `(width . ,(+ breapth width))
254 `(height . ,(+ depth height))
255 `(ry . ,(/ blot-diameter 2))
256 ;;`(transform . ,(scale))))
258 . ,(format #f "scale (~f, ~f)" output-scale output-scale))))
260 (define (text font string)
261 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))