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