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/paths.html
10 ;;; TODO: character selects by index from [custom] fonts
12 (debug-enable 'backtrace)
13 (define-module (scm output-svg))
14 (define this-module (current-module))
23 (define output-scale (* 2 scale-to-unit))
24 (define line-thickness 0)
26 (define (stderr string . rest)
27 (apply format (cons (current-error-port) (cons string rest)))
28 (force-output (current-error-port)))
30 (define (debugf string . rest)
32 (apply stderr (cons string rest))))
35 (define (dispatch expr)
36 (let ((keyword (car expr)))
38 ((eq? keyword 'some-func) "")
39 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
41 (if (module-defined? this-module keyword)
42 (apply (eval keyword this-module) (cdr expr))
45 (string-append "undefined: " (symbol->string keyword) "\n"))
49 (define (tagify tag string . attribute-alist)
55 (string-append " " (symbol->string (car x)) "='" (cdr x) "'"))
58 string "</" tag ">\n"))
60 (define (control->list c)
61 (list (car c) (cdr c)))
63 (define (control->string c)
65 (number->string (car c)) ","
67 (number->string (* -1 (cdr c))) " "))
69 (define (control-flip-y c)
70 (cons (car c) (* -1 (cdr c))))
72 (define (ly:numbers->string l)
74 (number->string (car l))
77 (string-append "," (ly:numbers->string (cdr l))))))
79 (define (svg-bezier l close)
80 (let* ((c0 (car (list-tail l 3)))
81 (c123 (list-head l 3)))
83 (if (not close) "M " "L ")
85 "C " (apply string-append (map control->string c123))
86 (if (not close) "" (string-append
87 "L " (control->string close))))));; " Z")))))
93 (define (fontify font expr)
94 (tagify "text" expr (cons 'style (svg-font font))))
95 ;; (cons 'unicode-range "U+EE00-EEFF"))))
97 ;;;;;;;;;;;;;;;;;;; share this utf8 stuff from output-gnome
101 ((< i #x80) (list (integer->char i)))
102 ((< i #x800) (map integer->char
103 (list (+ #xc0 (quotient i #x40))
104 (+ #x80 (modulo i #x40)))))
106 (let ((x (quotient i #x1000))
107 (y (modulo i #x1000)))
110 (+ #x80 (quotient y #x40))
111 (+ #x80 (modulo y #x40))))))
114 (define (custom-utf8 i)
117 (utf8 (+ #xee00 i))))
119 (define (string->utf8-string string)
121 (apply append (map utf8 (map char->integer (string->list string))))))
123 (define (char->utf8-string char)
124 (list->string (utf8 (char->integer char))))
125 ;; (list->string (custom-utf8 (char->integer char))))
126 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130 ;;; stencil outputters
133 ;;; catch-all for missing stuff
134 ;;; comment this out to see find out what functions you miss :-)
135 (define (dummy . foo) "")
136 (map (lambda (x) (module-define! this-module x dummy))
138 (ly:all-stencil-expressions)
139 (ly:all-output-backend-commands)))
141 (define (beam width slope thick blot)
144 (z (sqrt (+ (sqr x) (sqr y)))))
146 `(style . ,(format "fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-opacity:1;stroke-width:~f;stroke-linejoin:round;stroke-linecap:round;" line-thickness))
148 `(y . ,(number->string (* output-scale (- 0 (/ thick 2)))))
149 `(width . ,(number->string (* output-scale width)))
150 `(height . ,(number->string (* output-scale thick)))
151 ;;`(ry . ,(number->string (* output-scale half-lt)))
152 `(ry . ,(number->string (* output-scale (/ line-thickness 2))))
154 ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
159 (define (bezier-sandwich l thick)
160 (let* (;;(l (eval urg-l this-module))
161 (first (list-tail l 4))
162 (first-c0 (car (list-tail first 3)))
163 (second (list-head l 4)))
165 `(stroke . "#000000")
166 `(stroke-width . ,(number->string line-thickness))
167 `(transform . ,(format #f "scale (~f, ~f)"
168 output-scale output-scale))
169 `(d . ,(string-append (svg-bezier first #f)
170 (svg-bezier second first-c0))))))
172 (define (char font i)
174 `(fontify ,font ,(tagify "tspan" (char->utf8-string
175 (integer->char i))))))
178 (string-append "<!-- " s " !-->\n"))
180 (define (filledbox breapth width depth height)
181 (round-filled-box breapth width depth height line-thickness))
183 (define (lily-def key val)
185 ((equal? key "lilypondpaperoutputscale")
187 ;; If we just use transform scale (output-scale),
188 ;; all fonts come out scaled too (ie, much too big)
189 ;; So, we manually scale all other stuff.
190 (set! output-scale (* scale-to-unit (string->number val))))
191 ((equal? key "lilypondpaperlinethickness")
192 (set! line-thickness (* scale-to-unit (string->number val)))))
195 (define (placebox x y expr)
200 `(transform . ,(format #f "translate (~f, ~f)"
202 (- 0 (* output-scale y))))))
204 (define (round-filled-box breapth width depth height blot-diameter)
206 `(style . ,(format "fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-opacity:1;stroke-width:~f;stroke-linejoin:miter;stroke-linecap:butt;" line-thickness))
207 `(x . ,(number->string (* output-scale (- 0 breapth))))
208 `(y . ,(number->string (* output-scale (- 0 height))))
209 `(width . ,(number->string (* output-scale (+ breapth width))))
210 `(height . ,(number->string (* output-scale (+ depth height))))
211 ;;`(ry . ,(number->string (* output-scale half-lt)))
212 `(ry . ,(number->string (/ blot-diameter 2)))))
214 (define (svg-font font)
215 (define (font-family)
216 (let ((name (ly:font-name font)))
218 (regexp-substitute/global #f "^GNU-(.*)-[.0-9]*$" name 'pre 1 'post)
220 (stderr "font-name: ~S\n" (ly:font-name font))
221 ;; TODO s/filename/file-name/
222 (stderr "font-filename: ~S\n" (ly:font-filename font))
223 (stderr "font-size: ~S\n" (font-size))
227 (let* ((designsize (ly:font-design-size font))
228 (magnification (* (ly:font-magnification font)))
229 (scaling (* magnification designsize)))
230 (debugf "scaling:~S\n" scaling)
231 (debugf "magnification:~S\n" magnification)
232 (debugf "design:~S\n" designsize)
235 (format #f "font-family:~a;font-size:~a;fill:black;text-anchor:start;"
236 (font-family) (font-size)))
238 (define (text font string)
239 (dispatch `(fontify ,font ,(tagify "tspan" (string->utf8-string string)))))
241 ;; WTF is this in every backend?
242 (define (horizontal-line x1 x2 th)
243 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))