]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
Fix some bugs in the dynamic engraver and PostScript backend
[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--2006 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 (define-module (scm output-svg))
21 (define this-module (current-module))
22
23 (use-modules
24  (guile)
25  (ice-9 regex)
26  (lily)
27  (srfi srfi-13))
28
29
30 (define lily-unit-length 1.75)
31
32 (define (dispatch expr)
33   (let ((keyword (car expr)))
34     (cond
35      ((eq? keyword 'some-func) "")
36      ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
37      (else
38       (if (module-defined? this-module keyword)
39           (apply (eval keyword this-module) (cdr expr))
40           (begin
41             (ly:warning (_ "undefined: ~S") keyword)
42             ""))))))
43
44 ;; Helper functions
45 (define-public (attributes attributes-alist)
46   (apply string-append
47          (map (lambda (x) (format #f " ~s=\"~a\"" (car x) (cdr x)))
48               attributes-alist)))
49
50 (define-public (eo entity . attributes-alist)
51   "o = open"
52   (format #f "<~S~a>\n" entity (attributes attributes-alist)))
53
54 (define-public (eoc entity . attributes-alist)
55   " oc = open/close"
56   (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
57
58 (define-public (ec entity)
59   "c = close"
60   (format #f "</~S>\n" entity))
61
62
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 (integer->entity integer)
87   (format #f "&#x~x;" integer))
88
89 (define (char->entity char)
90   (integer->entity (char->integer char)))
91
92 (define (string->entities string)
93   (apply string-append
94          (map (lambda (x) (char->entity x)) (string->list string))))
95
96 (define pango-description-regexp-comma
97   (make-regexp "^([^,]+), ?([-a-zA-Z_]*) ([0-9.]+)$"))
98
99 (define pango-description-regexp-nocomma
100   (make-regexp "^([^ ]+) ([-a-zA-Z_]*) ?([0-9.]+)$"))
101
102 (define (pango-description-to-svg-font str)
103   (let*
104       ((size 4.0)
105        (family "Helvetica")
106        (style #f)
107        (match-1 (regexp-exec pango-description-regexp-comma str))
108        (match-2 (regexp-exec pango-description-regexp-nocomma str))
109        (match (if match-1
110                   match-1
111                   match-2)))
112
113     (if (regexp-match? match)
114         (begin
115           (set! family (match:substring match 1))
116           (if (< 0 (string-length (match:substring match 2)))
117               (set! style (match:substring match 2)))
118           (set! size
119                 (string->number (match:substring match 3))))
120
121         (ly:warning (_ "can't decypher Pango description: ~a") str))
122
123     (set! style
124           (if (string? style)
125               (format "font-style:~a;" style)
126               ""))
127     
128     (format "font-family:~a;~afont-size:~a;text-anchor:west"
129             family
130             style
131             (/ size lily-unit-length))
132     ))
133
134 ;;; FONT may be font smob, or pango font string
135 (define (svg-font font)
136   (if (string? font)
137       (pango-description-to-svg-font font)
138       (let ((name-style (font-name-style font))
139             (size (modified-font-metric-font-scaling font))
140             (anchor "west"))
141
142         (format #f "font-family:~a;font-style:~a;font-size:~a;text-anchor:~a;"
143                 (car name-style) (cadr name-style)
144                 size anchor))))
145
146 (define (fontify font expr)
147   (entity 'text expr
148           `(style . ,(svg-font font))
149           '(fill . "currentColor")
150           ))
151
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
153 ;;; stencil outputters
154 ;;;
155
156 ;;; catch-all for missing stuff
157 ;;; comment this out to see find out what functions you miss :-)
158
159 (if #f
160     (begin
161       (define (dummy . foo) "")
162       (map (lambda (x) (module-define! this-module x dummy))
163            (append
164             (ly:all-stencil-expressions)
165             (ly:all-output-backend-commands)))
166       ))
167
168 (define (url-link url x y)
169   (string-append
170    (eo 'a `(xlink:href . ,url))
171    (eoc 'rect
172         `(x . ,(car x))
173         `(y . ,(car y))
174         `(width . ,(- (cdr x) (car x)))
175         `(height . ,(- (cdr y) (car y)))
176         '(fill . "none")
177         '(stroke . "none")
178         '(stroke-width . "0.0"))
179    (ec 'a)))
180
181 (define (grob-cause offset grob)
182   "")
183
184 (define (no-origin)
185   "")
186
187
188
189 (define (bezier-sandwich lst thick)
190   (let* ((first (list-tail lst 4))
191          (first-c0 (car (list-tail first 3)))
192          (second (list-head lst 4)))
193     (entity 'path ""
194             '(stroke-linejoin . "round")
195             '(stroke-linecap . "round")
196             `(stroke-width . ,thick)
197             '(stroke . "currentColor")
198             '(fill . "currentColor")
199             `(d . ,(string-append (svg-bezier first #f)
200                                   (svg-bezier second first-c0)))
201             )))
202
203 (define (char font i)
204   (dispatch
205    `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
206
207 (define-public (comment s)
208   (string-append "<!-- " s " !-->\n"))
209
210 (define (draw-line thick x1 y1 x2 y2 . alist)
211   
212   (apply entity 'line ""
213          (append
214           `((stroke-linejoin . "round")
215             (stroke-linecap . "round")
216             (stroke-width . ,thick)
217             (stroke . "currentColor")
218             (x1 . ,x1)
219             (y1 . ,(- y1))
220             (x2 . ,x2)
221             (y2 . ,(- y2)))
222           alist)))
223
224 (define (dashed-line thick on off dx dy)
225   (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
226
227 (define (named-glyph font name)
228   (dispatch
229    `(fontify ,font ,(entity 'tspan
230                             (integer->entity
231                              (ly:font-glyph-name-to-charcode font name))))))
232
233 (define (placebox x y expr)
234   (entity 'g
235           expr
236           ;; FIXME: Not using GNU coding standards [translate ()] here
237           ;; to work around a bug in Microsoft Internet Explorer 6.0
238           `(transform . ,(format #f "translate(~f, ~f)"
239                                  x (- y)))))
240
241 (define (polygon coords blot-diameter is-filled)
242   (entity
243    'polygon ""
244    '(stroke-linejoin . "round")
245    '(stroke-linecap . "round")
246    `(stroke-width . ,blot-diameter)
247    `(fill . ,(if is-filled "currentColor" "none"))
248    '(stroke . "currentColor")
249    `(points . ,(string-join
250                 (map offset->point (ly:list->offsets '() coords))))
251    ))
252
253 ;; rotate around given point
254 (define (setrotation ang x y)
255   (format "<g transform=\"rotate(~a,~a,~a)\">"
256     (number->string (* -1 ang))
257     (number->string x)
258     (number->string (* -1 y))))
259
260 (define (resetrotation ang x y)
261   "</g>")
262
263 (define (round-filled-box breapth width depth height blot-diameter)
264   (entity 'rect ""
265           ;; The stroke will stick out.  To use stroke,
266           ;; the stroke-width must be subtracted from all other dimensions.
267           ;;'(stroke-linejoin . "round")
268           ;;'(stroke-linecap . "round")
269           ;;`(stroke-width . ,blot)
270           ;;'(stroke . "red")
271           ;;'(fill . "orange")
272
273           `(x . ,(- breapth))
274           `(y . ,(- height))
275           `(width . ,(+ breapth width))
276           `(height . ,(+ depth height))
277           `(ry . ,(/ blot-diameter 2))
278           ))
279
280 (define (circle radius thick is-filled)
281   (entity
282    'circle ""
283    '(stroke-linejoin . "round")
284    '(stroke-linecap . "round")
285    `(fill . ,(if is-filled "currentColor" "none"))
286    `(stroke . "currentColor")
287    `(stroke-width . ,thick)
288    `(r . ,radius)))
289
290 (define (text font string)
291   (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
292
293 (define (utf-8-string pango-font-description string)
294   (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))
295
296
297
298 (define (setcolor r g b)
299   (format "<g color=\"rgb(~a%,~a%,~a%)\">"
300           (* 100 r) (* 100 g) (* 100 b)
301           ))
302
303 (define (resetcolor)
304   "</g>")