]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
* scm/lily-library.scm (char->unicode-index): New function.
[lilypond.git] / scm / output-svg.scm
1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c)  2002--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6
7 ;;;; http://www.w3.org/TR/SVG11
8
9 ;;; FIXME
10
11 ;;; * sodipodi gets confuseed by dashes in font names.
12 ;;;
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.
16 ;;;
17 ;;; * inkscape fails to map Feta fonts to private use area (PUA) E000
18 ;;;   (sodipodi is fine).
19
20 (debug-enable 'backtrace)
21 (define-module (scm output-svg))
22 (define this-module (current-module))
23
24 (use-modules
25  (guile)
26  (ice-9 regex)
27  (lily))
28
29 ;; GLobals
30 ;; FIXME: 2?
31 (define output-scale (* 2 scale-to-unit))
32
33 (define (stderr string . rest)
34   (apply format (cons (current-error-port) (cons string rest)))
35   (force-output (current-error-port)))
36
37 (define (debugf string . rest)
38   (if #f
39       (apply stderr (cons string rest))))
40
41
42 (define (dispatch expr)
43   (let ((keyword (car expr)))
44     (cond
45      ((eq? keyword 'some-func) "")
46      ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
47      (else
48       (if (module-defined? this-module keyword)
49           (apply (eval keyword this-module) (cdr expr))
50           (begin
51             (display
52              (string-append "undefined: " (symbol->string keyword) "\n"))
53             ""))))))
54   
55 ;; Helper functions
56 (define (tagify tag string . attribute-alist)
57   (string-append
58    "<"
59    tag
60    (apply string-append
61           (map (lambda (x)
62                  (string-append " " (symbol->string (car x)) "='" (cdr x) "'"))
63                attribute-alist))
64    ">"
65    string "</" tag ">\n"))
66
67 (define (control->list c)
68   (list (car c) (cdr c)))
69
70 (define (control->string c)
71   (string-append
72    (number->string (car c)) ","
73    ;; lose the -1
74    (number->string (* -1 (cdr c))) " "))
75
76 (define (control-flip-y c)
77   (cons (car c) (* -1 (cdr c))))
78
79 (define (ly:numbers->string lst)
80   (string-append
81    (number->string (car lst))
82    (if (null? (cdr lst))
83        ""
84        (string-append "," (ly:numbers->string (cdr lst))))))
85
86 (define (svg-bezier lst close)
87   (let* ((c0 (car (list-tail lst 3)))
88          (c123 (list-head lst 3)))
89     (string-append
90      (if (not close) "M " "L ")
91      (control->string c0)
92      "C " (apply string-append (map control->string c123))
93      (if (not close) "" (string-append
94                          "L " (control->string close))))))
95
96 (define (sqr x)
97   (* x x))
98
99 (define (font-size font)
100   (let* ((designsize (ly:font-design-size font))
101          (magnification (* (ly:font-magnification font)))
102          (ops 2)
103          (scaling (* ops magnification designsize)))
104     (debugf "scaling:~S\n" scaling)
105     (debugf "magnification:~S\n" magnification)
106     (debugf "design:~S\n" designsize)
107     scaling))
108
109 (define (char->entity font char)
110   (format #f "&#x~x;" (char->unicode-index font char)))
111                    
112 (define (string->entities font string)
113   (apply string-append
114          (map (lambda (x) (char->entity font x)) (string->list string))))
115
116 (define (svg-font font)
117   (let* ((encoding (ly:font-encoding font))
118          (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'middle)))
119    (format #f "font-family:~a;font-size:~a;text-anchor:~S;"
120            (font-family font) (font-size font) anchor)))
121
122 (define (fontify font expr)
123    (tagify "text" expr (cons 'style (svg-font font))))
124
125 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126
127
128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129 ;;; stencil outputters
130 ;;;
131
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))
136      (append
137       (ly:all-stencil-expressions)
138       (ly:all-output-backend-commands)))
139
140 (define (beam width slope thick blot)
141   (let* ((x width)
142          (y (* slope width))
143          (z (sqrt (+ (sqr x) (sqr y)))))
144     (tagify "rect" ""
145             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
146             `(x . "0")
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))))
151             `(transform .
152                         ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
153                                  (/ x z)
154                                  (* -1 (/ y z))
155                                  1 1)))))
156
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)))
161     (tagify "path" ""
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))))))
167
168 (define (char font i)
169   (dispatch
170    `(fontify ,font ,(tagify "tspan" (char->entity font (integer->char i))))))
171
172 (define (comment s)
173   (string-append "<!-- " s " !-->\n"))
174
175 (define (filledbox breapth width depth height)
176   (round-filled-box breapth width depth height 0))
177
178 (define (placebox x y expr)
179   (tagify "g"
180           ;; FIXME -- JCN
181           ;;(dispatch expr)
182           expr
183           `(transform . ,(format #f "translate (~f, ~f)"
184                                  (* output-scale x)
185                                  (- 0 (* output-scale y))))))
186
187 (define (round-filled-box breapth width depth height blot-diameter)
188   (tagify "rect" ""
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)))))
195
196 (define (text font string)
197   (dispatch `(fontify ,font ,(tagify "tspan" (string->entities font string)))))
198
199 ;; WTF is this in every backend?
200 (define (horizontal-line x1 x2 th)
201   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))