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