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