]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
(dump-page): Implement landscape.
[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 ;;;; TODO:
10 ;;;;  * missing stencils: line, dashed-line ...
11 ;;;;  * rounded corners on stencils: rect, bezier (inkscape bug?)
12 ;;;;  * inkscape page/pageSet support
13
14 (debug-enable 'backtrace)
15 (define-module (scm output-svg))
16 (define this-module (current-module))
17
18 (use-modules
19  (guile)
20  (ice-9 regex)
21  (lily))
22
23 ;; GLobals
24 ;; FIXME: 2?
25 (define output-scale (* 2 scale-to-unit))
26
27 (define indent-level 0)
28 (define (indent s . add) s)
29
30 ;;(define (indentation indent str)
31 ;;   (regexp-substitute/global #f "\(\n\)[ \t]*" str 'pre 1 indent 'post))
32
33 (define (indent s . add)
34   (let ((before indent-level)
35         (after (apply + (cons indent-level add)))
36         (after? (and (not (null? add)) (> (car add) 0))))
37     (set! indent-level after)
38     (if after?
39       (string-append (make-string before #\ ) s)
40       (string-append (make-string after #\ ) s))))
41
42 (define (debugf string . rest)
43   (if #f
44       (apply stderr (cons string rest))))
45
46 (define (dispatch expr)
47   (let ((keyword (car expr)))
48     (cond
49      ((eq? keyword 'some-func) "")
50      ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
51      (else
52       (if (module-defined? this-module keyword)
53           (apply (eval keyword this-module) (cdr expr))
54           (begin
55             (display
56              (string-append "undefined: " (symbol->string keyword) "\n"))
57             ""))))))
58   
59 ;; Helper functions
60 (define-public (attributes attributes-alist)
61   (apply string-append
62          (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
63               attributes-alist)))
64
65 (define-public (eo entity . attributes-alist)
66   (indent (format #f "<~S~a>\n" entity (attributes attributes-alist)) 2))
67
68 (define-public (eoc entity . attributes-alist)
69   (indent (format #f "<~S~a/>\n" entity (attributes attributes-alist))))
70
71 (define-public (ec entity)
72   (indent (format #f "</~S>\n" entity) -2))
73
74 (define-public (entity entity string . attributes-alist)
75   (if (equal? string "")
76       (apply eoc entity attributes-alist)
77       (string-append
78        (apply eo (cons entity attributes-alist)) string (ec entity))))
79
80 (define (control->list c)
81   (list (car c) (cdr c)))
82
83 (define (control->string c)
84   (string-append
85    (number->string (car c)) ","
86    ;; lose the -1
87    (number->string (* -1 (cdr c))) " "))
88
89 (define (control-flip-y c)
90   (cons (car c) (* -1 (cdr c))))
91
92 (define (ly:numbers->string lst)
93   (string-append
94    (number->string (car lst))
95    (if (null? (cdr lst))
96        ""
97        (string-append "," (ly:numbers->string (cdr lst))))))
98
99 (define (svg-bezier lst close)
100   (let* ((c0 (car (list-tail lst 3)))
101          (c123 (list-head lst 3)))
102     (string-append
103      (if (not close) "M " "L ")
104      (control->string c0)
105      "C " (apply string-append (map control->string c123))
106      (if (not close) "" (string-append
107                          "L " (control->string close))))))
108
109 (define (sqr x)
110   (* x x))
111
112 (define (font-size font)
113   (let* ((designsize (ly:font-design-size font))
114          (magnification (* (ly:font-magnification font)))
115          (ops 2)
116          (scaling (* ops magnification designsize)))
117     (debugf "scaling:~S\n" scaling)
118     (debugf "magnification:~S\n" magnification)
119     (debugf "design:~S\n" designsize)
120     scaling))
121
122 (define (integer->entity integer)
123   (format #f "&#x~x;" integer))
124                    
125 (define (char->entity char)
126   (integer->entity (char->integer char)))
127                    
128 (define (string->entities string)
129   (apply string-append
130          (map (lambda (x) (char->entity x)) (string->list string))))
131
132 (define (svg-font font)
133   (let* ((encoding (ly:font-encoding font))
134          (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start))
135          (family (font-family font)))
136    (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~S;"
137            (otf-name-mangling font family)
138            (otf-style-mangling font family)
139            (font-size font) anchor)))
140
141 (define (fontify font expr)
142    (entity 'text expr (cons 'style (svg-font font))))
143
144 ;; FIXME
145 (define-public (otf-name-mangling font family)
146   ;; Hmm, family is bigcheese20/26?
147   (if (string=? (substring family 0 (min (string-length family) 9))
148                 "bigcheese")
149       "LilyPond"
150       (if (string=? family "aybabtu")
151           "LilyPondBraces"
152           family)))
153
154 (define-public (otf-style-mangling font family)
155   ;; Hmm, family is bigcheese20/26?
156   (if (string=? (substring family 0 (min (string-length family) 9))
157                 "bigcheese")
158       (substring family 9)
159       "Regular"))
160
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 ;;; stencil outputters
163 ;;;
164
165 ;;; catch-all for missing stuff
166 ;;; comment this out to see find out what functions you miss :-)
167 (define (dummy . foo) "")
168 (map (lambda (x) (module-define! this-module x dummy))
169      (append
170       (ly:all-stencil-expressions)
171       (ly:all-output-backend-commands)))
172
173 (define (beam width slope thick blot)
174   (let* ((x width)
175          (y (* slope width))
176          (z (sqrt (+ (sqr x) (sqr y)))))
177     (entity 'rect ""
178             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
179             `(x . "0")
180             `(y . ,(number->string (* output-scale (- 0 (/ thick 2)))))
181             `(width . ,(number->string (* output-scale width)))
182             `(height . ,(number->string (* output-scale thick)))
183             `(ry . ,(number->string (* output-scale (/ blot 2))))
184             `(transform .
185                         ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
186                                  (/ x z)
187                                  (* -1 (/ y z))
188                                  1 1)))))
189
190 (define (bezier-sandwich lst thick)
191   (let* ((first (list-tail lst 4))
192          (first-c0 (car (list-tail first 3)))
193          (second (list-head lst 4)))
194     (entity 'path ""
195             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" thick))
196             `(transform . ,(format #f "scale (~f, ~f)"
197                                    output-scale output-scale))
198             `(d . ,(string-append (svg-bezier first #f)
199                                   (svg-bezier second first-c0))))))
200
201 (define (char font i)
202   (dispatch
203    `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
204
205 (define-public (comment s)
206   (string-append "<!-- " s " !-->\n"))
207
208 (define (filledbox breapth width depth height)
209   (round-filled-box breapth width depth height 0))
210
211 (define (named-glyph font name)
212   (dispatch
213    `(fontify ,font ,(entity 'tspan
214                             (integer->entity
215                              (ly:font-glyph-name-to-charcode font name))))))
216
217 (define (placebox x y expr)
218   (entity 'g
219           ;; FIXME -- JCN
220           ;;(dispatch expr)
221           expr
222           `(transform . ,(format #f "translate (~f, ~f)"
223                                  (* output-scale x)
224                                  (- 0 (* output-scale y))))))
225
226 (define (round-filled-box breapth width depth height blot-diameter)
227   (entity 'rect ""
228           `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot-diameter))
229           `(x . ,(number->string (* output-scale (- 0 breapth))))
230           `(y . ,(number->string (* output-scale (- 0 height))))
231           `(width . ,(number->string (* output-scale (+ breapth width))))
232           `(height . ,(number->string (* output-scale (+ depth height))))
233           `(ry . ,(number->string (/ blot-diameter 2)))))
234
235 (define (text font string)
236   (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
237
238 ;; WTF is this in every backend?
239 (define (horizontal-line x1 x2 th)
240   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))