]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
* scm/output-svg.scm (dashed-line): new function body.
[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--2005 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 ;;;;  * .cff MUST NOT be in fc's fontpath.
12 ;;;;    - workaround: remove mf/out from ~/.fonts.conf,
13 ;;;;      instead add ~/.fonts and symlink all /mf/out/*otf there.
14 ;;;;    - bug in fontconfig/freetype/pango?
15
16 ;;;;  * inkscape page/pageSet support
17 ;;;;  * inkscape SVG-font support
18 ;;;;    - use fontconfig/fc-cache for now, see output-gnome.scm
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  (srfi srfi-13))
29
30
31 (define lily-unit-length 1.75)
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 (integer->entity integer)
84   (format #f "&#x~x;" integer))
85
86 (define (char->entity char)
87   (integer->entity (char->integer char)))
88
89 (define (string->entities string)
90   (apply string-append
91          (map (lambda (x) (char->entity x)) (string->list string))))
92
93 (define pango-description-regexp-comma
94   (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
95
96 (define pango-description-regexp-nocomma
97   (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
98
99 (define (pango-description-to-svg-font str)
100   (let*
101       ((size 4.0)
102        (family "Helvetica")
103        (style #f)
104        (match-1 (regexp-exec pango-description-regexp-comma str))
105        (match-2 (regexp-exec pango-description-regexp-nocomma str))
106        (match (if match-1
107                   match-1
108                   match-2))
109        )
110
111     (if (regexp-match? match)
112         (begin
113           (set! family (match:substring match 1))
114           (if (< 0 (string-length (match:substring match 2)))
115               (set! style (match:substring match 2)))
116           (set! size
117                 (string->number (match:substring match 3))))
118
119         (display (format "Cannot decypher Pango description:  ~a\n" str)))
120
121     (set! style
122           (if (string? style)
123               (format "font-style:~a;" style)
124               ""))
125     
126     (format "font-family:~a;~afont-size:~a;text-anchor:west"
127             family
128             style
129             (/ size lily-unit-length))
130     ))
131
132 ;;; FONT may be font smob, or pango font string
133 (define (svg-font font)
134   (if (string? font)
135       (pango-description-to-svg-font font)
136       (let ((name-style (font-name-style font))
137             (size (modified-font-metric-font-scaling font))
138             (anchor "west"))
139
140         (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
141                 (car name-style) (cadr name-style)
142                 size anchor))))
143
144 (define (fontify font expr)
145   (entity 'text expr
146           `(style . ,(svg-font font))
147           ))
148
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 ;;; stencil outputters
151 ;;;
152
153 ;;; catch-all for missing stuff
154 ;;; comment this out to see find out what functions you miss :-)
155 (define (dummy . foo) "")
156 (map (lambda (x) (module-define! this-module x dummy))
157      (append
158       (ly:all-stencil-expressions)
159       (ly:all-output-backend-commands)))
160
161 (define (rect-beam width slope thick blot-diameter)
162   (let* ((x width)
163          (y (* slope width))
164          (z (/ y x)))
165     (entity 'rect ""
166             ;; The stroke will stick out.  To use stroke,
167             ;; the stroke-width must be subtracted from all other dimensions.
168             ;;'(stroke-linejoin . "round")
169             ;;'(stroke-linecap . "round")
170             ;;`(stroke-width . ,blot-diameter)
171             ;;'(stroke . "red")
172             ;;'(fill . "orange")
173
174             `(x . 0)
175             `(y . ,(- (/ thick 2)))
176             `(width . ,width)
177             `(height . ,(+ thick (* (abs z) (/ thick 2))))
178             `(rx . ,(/ blot-diameter 2))
179             `(transform . ,(format #f "matrix (1, ~f, 0, 1, 0, 0)"  z)
180                            ))))
181
182 (define (beam width slope thick blot-diameter)
183   (let* ((b blot-diameter)
184          (t (- thick b))
185          (w (- width b))
186          (h (* w slope)))
187     (entity 'polygon ""
188             '(stroke-linejoin . "round")
189             '(stroke-linecap . "round")
190             `(stroke-width . ,blot-diameter)
191             '(stroke . "black")
192             '(fill . "black")
193             `(points . ,(string-join
194                          (map offset->point
195                               (list (cons (/ b 2) (/ t 2))
196                                     (cons (+ w (/ b 2)) (+ h (/ t 2)))
197                                     (cons (+ w (/ b 2)) (+ h (- (/ t 2))))
198                                     (cons (/ b 2) (- (/ t 2)))))))
199             )))
200
201 (define (path-beam width slope thick blot-diameter)
202   (let* ((b blot-diameter)
203          (t (- thick b))
204          (w (- width b))
205          (h (* w slope)))
206     (entity 'path ""
207             '(stroke-linejoin . "round")
208             '(stroke-linecap . "round")
209             `(stroke-width . ,blot-diameter)
210             '(stroke . "black")
211             '(fill . "black")
212             `(d . ,(format #f "M ~S,~S l ~S,~S l ~S,~S l ~S,~S l ~S,~S"
213                            (/ b 2) (/ t 2)
214                            w (- h)
215                            0 (- t)
216                            (- w) h
217                            0 t))
218           )))
219
220 (define (bezier-sandwich lst thick)
221   (let* ((first (list-tail lst 4))
222          (first-c0 (car (list-tail first 3)))
223          (second (list-head lst 4)))
224     (entity 'path ""
225             '(stroke-linejoin . "round")
226             '(stroke-linecap . "round")
227             `(stroke-width . ,thick)
228             '(stroke . "black")
229             '(fill . "black")
230             `(d . ,(string-append (svg-bezier first #f)
231                                   (svg-bezier second first-c0)))
232           )))
233
234 (define (char font i)
235   (dispatch
236    `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
237
238 (define-public (comment s)
239   (string-append "<!-- " s " !-->\n"))
240
241 (define (draw-line thick x1 y1 x2 y2 . alist)
242   
243   (apply entity 'line ""
244          (append
245           `((stroke-linejoin . "round")
246             (stroke-linecap . "round")
247             (stroke-width . ,thick)
248             (stroke . "black")
249             ;;'(fill . "black")
250             (x1 . ,x1)
251             (y1 . ,y1)
252             (x2 . ,x2)
253             (y2 . ,y2))
254           alist)))
255
256 (define (dashed-line thick on off dx dy)
257   (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
258
259 ;; WTF is this in every backend?
260 (define (horizontal-line x1 x2 th)
261   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
262
263 (define (filledbox breapth width depth height)
264   (round-filled-box breapth width depth height 0))
265
266 (define (named-glyph font name)
267   (dispatch
268    `(fontify ,font ,(entity 'tspan
269                             (integer->entity
270                              (ly:font-glyph-name-to-charcode font name))))))
271
272 (define (placebox x y expr)
273   (entity 'g
274           ;; FIXME -- JCN
275           ;;(dispatch expr)
276           expr
277           `(transform . ,(format #f "translate (~f, ~f)"
278                                  x (- y)))))
279
280
281 (define (polygon coords blot-diameter)
282   (entity 'polygon ""
283           '(stroke-linejoin . "round")
284           '(stroke-linecap . "round")
285           `(stroke-width . ,blot-diameter)
286           '(stroke . "black")
287           ;;'(fill . "black")
288           `(points . ,(string-join
289                        (map offset->point (ly:list->offsets '() coords))))
290   ))
291
292 (define (round-filled-box breapth width depth height blot-diameter)
293   (entity 'rect ""
294           ;; The stroke will stick out.  To use stroke,
295           ;; the stroke-width must be subtracted from all other dimensions.
296           ;;'(stroke-linejoin . "round")
297           ;;'(stroke-linecap . "round")
298           ;;`(stroke-width . ,blot)
299           ;;'(stroke . "red")
300           ;;'(fill . "orange")
301
302           `(x . ,(- breapth))
303           `(y . ,(- height))
304           `(width . ,(+ breapth width))
305           `(height . ,(+ depth height))
306           `(ry . ,(/ blot-diameter 2))
307           ))
308
309 (define (text font string)
310   (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
311
312 (define (utf8-string pango-font-description string)
313   (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))