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
11 ;;; * sodipodi gets confuseed by dashes in font names.
13 ;;; removing feta-nummer*.pfa (LilyPond-feta-nummer),
14 ;;; feta-braces*.pfa (LilyPond-feta-braces), feta-din*.pfa
15 ;;; (LilyPond-feta-din) from font path shows feta fonts in sodipodi.
17 ;;; * inkscape fails to map Feta fonts to private use area (PUA) E000
18 ;;; (sodipodi is fine).
20 (debug-enable 'backtrace)
21 (define-module (scm output-svg))
22 (define this-module (current-module))
31 (define output-scale (* 2 scale-to-unit))
33 (define (stderr string . rest)
34 (apply format (cons (current-error-port) (cons string rest)))
35 (force-output (current-error-port)))
37 (define (debugf string . rest)
39 (apply stderr (cons string rest))))
42 (define (dispatch expr)
43 (let ((keyword (car expr)))
45 ((eq? keyword 'some-func) "")
46 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
48 (if (module-defined? this-module keyword)
49 (apply (eval keyword this-module) (cdr expr))
52 (string-append "undefined: " (symbol->string keyword) "\n"))
56 (define (tagify tag string . attribute-alist)
62 (string-append " " (symbol->string (car x)) "='" (cdr x) "'"))
65 string "</" tag ">\n"))
67 (define (control->list c)
68 (list (car c) (cdr c)))
70 (define (control->string c)
72 (number->string (car c)) ","
74 (number->string (* -1 (cdr c))) " "))
76 (define (control-flip-y c)
77 (cons (car c) (* -1 (cdr c))))
79 (define (ly:numbers->string lst)
81 (number->string (car lst))
84 (string-append "," (ly:numbers->string (cdr lst))))))
86 (define (svg-bezier lst close)
87 (let* ((c0 (car (list-tail lst 3)))
88 (c123 (list-head lst 3)))
90 (if (not close) "M " "L ")
92 "C " (apply string-append (map control->string c123))
93 (if (not close) "" (string-append
94 "L " (control->string close))))))
99 (define (font-size font)
100 (let* ((designsize (ly:font-design-size font))
101 (magnification (* (ly:font-magnification font)))
103 (scaling (* ops magnification designsize)))
104 (debugf "scaling:~S\n" scaling)
105 (debugf "magnification:~S\n" magnification)
106 (debugf "design:~S\n" designsize)
109 (define (char->entity font char)
110 (format #f "&#x~x;" (char->unicode-index font char)))
112 (define (string->entities font string)
114 (map (lambda (x) (char->entity font x)) (string->list string))))
116 (define (svg-font font)
117 (let* ((encoding (ly:font-encoding font))
118 (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start)))
119 (format #f "font-family:~a;font-size:~a;text-anchor:~S;"
120 (font-family font) (font-size font) anchor)))
122 (define (fontify font expr)
123 (tagify "text" expr (cons 'style (svg-font font))))
125 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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 (beam width slope thick blot)
143 (z (sqrt (+ (sqr x) (sqr y)))))
145 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
147 `(y . ,(number->string (* output-scale (- 0 (/ thick 2)))))
148 `(width . ,(number->string (* output-scale width)))
149 `(height . ,(number->string (* output-scale thick)))
150 `(ry . ,(number->string (* output-scale (/ blot 2))))
152 ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
157 (define (bezier-sandwich lst thick)
158 (let* ((first (list-tail lst 4))
159 (first-c0 (car (list-tail first 3)))
160 (second (list-head lst 4)))
162 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" thick))
163 `(transform . ,(format #f "scale (~f, ~f)"
164 output-scale output-scale))
165 `(d . ,(string-append (svg-bezier first #f)
166 (svg-bezier second first-c0))))))
168 (define (char font i)
170 `(fontify ,font ,(tagify "tspan" (char->entity font (integer->char i))))))
173 (string-append "<!-- " s " !-->\n"))
175 (define (filledbox breapth width depth height)
176 (round-filled-box breapth width depth height 0))
178 (define (placebox x y expr)
183 `(transform . ,(format #f "translate (~f, ~f)"
185 (- 0 (* output-scale y))))))
187 (define (round-filled-box breapth width depth height blot-diameter)
189 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot-diameter))
190 `(x . ,(number->string (* output-scale (- 0 breapth))))
191 `(y . ,(number->string (* output-scale (- 0 height))))
192 `(width . ,(number->string (* output-scale (+ breapth width))))
193 `(height . ,(number->string (* output-scale (+ depth height))))
194 `(ry . ,(number->string (/ blot-diameter 2)))))
196 (define (text font string)
197 (dispatch `(fontify ,font ,(tagify "tspan" (string->entities font string)))))
199 ;; WTF is this in every backend?
200 (define (horizontal-line x1 x2 th)
201 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))