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 ;;; * maybe we need to have a unicode mapping somehow, we could
21 ;;; - use OpenType instead of Type1
22 ;;; http://lists.gnu.org/archive/html/lilypond-devel/2004-05/msg00098.html
24 ;;; - or fix the pangofc-afm-decoder and add it to Pango (no chance?)
25 ;;; or have fontconfig read AFM files
26 ;;; http://lists.gnu.org/archive/html/lilypond-devel/2004-05/msg00103.html
29 (debug-enable 'backtrace)
30 (define-module (scm output-svg))
31 (define this-module (current-module))
40 (define output-scale (* 2 scale-to-unit))
42 (define (stderr string . rest)
43 (apply format (cons (current-error-port) (cons string rest)))
44 (force-output (current-error-port)))
46 (define (debugf string . rest)
48 (apply stderr (cons string rest))))
51 (define (dispatch expr)
52 (let ((keyword (car expr)))
54 ((eq? keyword 'some-func) "")
55 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
57 (if (module-defined? this-module keyword)
58 (apply (eval keyword this-module) (cdr expr))
61 (string-append "undefined: " (symbol->string keyword) "\n"))
65 (define (tagify tag string . attribute-alist)
71 (string-append " " (symbol->string (car x)) "='" (cdr x) "'"))
74 string "</" tag ">\n"))
76 (define (control->list c)
77 (list (car c) (cdr c)))
79 (define (control->string c)
81 (number->string (car c)) ","
83 (number->string (* -1 (cdr c))) " "))
85 (define (control-flip-y c)
86 (cons (car c) (* -1 (cdr c))))
88 (define (ly:numbers->string lst)
90 (number->string (car lst))
93 (string-append "," (ly:numbers->string (cdr lst))))))
95 (define (svg-bezier lst close)
96 (let* ((c0 (car (list-tail lst 3)))
97 (c123 (list-head lst 3)))
99 (if (not close) "M " "L ")
101 "C " (apply string-append (map control->string c123))
102 (if (not close) "" (string-append
103 "L " (control->string close))))))
108 (define (font-size font)
109 (let* ((designsize (ly:font-design-size font))
110 (magnification (* (ly:font-magnification font)))
112 (scaling (* ops magnification designsize)))
113 (debugf "scaling:~S\n" scaling)
114 (debugf "magnification:~S\n" magnification)
115 (debugf "design:~S\n" designsize)
118 (define (char->entity font char)
119 (format #f "&#x~x;" (char->unicode-index font char)))
121 (define (string->entities font string)
123 (map (lambda (x) (char->entity font x)) (string->list string))))
125 (define (svg-font font)
126 (let* ((encoding (ly:font-encoding font))
127 (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start)))
128 (format #f "font-family:~a;font-size:~a;text-anchor:~S;"
129 (font-family font) (font-size font) anchor)))
131 (define (fontify font expr)
132 (tagify "text" expr (cons 'style (svg-font font))))
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
137 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 ;;; stencil outputters
141 ;;; catch-all for missing stuff
142 ;;; comment this out to see find out what functions you miss :-)
143 (define (dummy . foo) "")
144 (map (lambda (x) (module-define! this-module x dummy))
146 (ly:all-stencil-expressions)
147 (ly:all-output-backend-commands)))
149 (define (beam width slope thick blot)
152 (z (sqrt (+ (sqr x) (sqr y)))))
154 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
156 `(y . ,(number->string (* output-scale (- 0 (/ thick 2)))))
157 `(width . ,(number->string (* output-scale width)))
158 `(height . ,(number->string (* output-scale thick)))
159 `(ry . ,(number->string (* output-scale (/ blot 2))))
161 ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
166 (define (bezier-sandwich lst thick)
167 (let* ((first (list-tail lst 4))
168 (first-c0 (car (list-tail first 3)))
169 (second (list-head lst 4)))
171 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" thick))
172 `(transform . ,(format #f "scale (~f, ~f)"
173 output-scale output-scale))
174 `(d . ,(string-append (svg-bezier first #f)
175 (svg-bezier second first-c0))))))
177 (define (char font i)
179 `(fontify ,font ,(tagify "tspan" (char->entity font (integer->char i))))))
182 (string-append "<!-- " s " !-->\n"))
184 (define (filledbox breapth width depth height)
185 (round-filled-box breapth width depth height 0))
187 (define (placebox x y expr)
192 `(transform . ,(format #f "translate (~f, ~f)"
194 (- 0 (* output-scale y))))))
196 (define (round-filled-box breapth width depth height blot-diameter)
198 `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot-diameter))
199 `(x . ,(number->string (* output-scale (- 0 breapth))))
200 `(y . ,(number->string (* output-scale (- 0 height))))
201 `(width . ,(number->string (* output-scale (+ breapth width))))
202 `(height . ,(number->string (* output-scale (+ depth height))))
203 `(ry . ,(number->string (/ blot-diameter 2)))))
205 (define (text font string)
206 (dispatch `(fontify ,font ,(tagify "tspan" (string->entities font string)))))
208 ;; WTF is this in every backend?
209 (define (horizontal-line x1 x2 th)
210 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))