1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2002--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;; http://www.w3.org/TR/SVG11
8 ;;;; http://www.w3.org/TR/SVG12/ -- page, pageSet in draft
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?
16 ;;;; * inkscape page/pageSet support
17 ;;;; * inkscape SVG-font support
18 ;;;; - use fontconfig/fc-cache for now, see output-gnome.scm
20 (define-module (scm output-svg))
21 (define this-module (current-module))
31 (define fancy-format format)
32 (define format ergonomic-simple-format)
34 (define lily-unit-length 1.7573)
36 (define (dispatch expr)
37 (let ((keyword (car expr)))
39 ((eq? keyword 'some-func) "")
40 ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
42 (if (module-defined? this-module keyword)
43 (apply (eval keyword this-module) (cdr expr))
45 (ly:warning (_ "undefined: ~S") keyword)
49 (define-public (attributes attributes-alist)
55 (set! value (ly:format "~4f" value)))
56 (format " ~s=\"~a\"" attr value)))
59 (define-public (eo entity . attributes-alist)
61 (format "<~S~a>\n" entity (attributes attributes-alist)))
63 (define-public (eoc entity . attributes-alist)
65 (format "<~S~a/>\n" entity (attributes attributes-alist)))
67 (define-public (ec entity)
69 (format "</~S>\n" entity))
71 (define-public (comment s)
72 (string-append "<!-- " s " -->\n"))
74 (define-public (entity entity string . attributes-alist)
75 (if (equal? string "")
76 (apply eoc entity attributes-alist)
78 (apply eo (cons entity attributes-alist)) string (ec entity))))
80 (define (offset->point o)
81 (ly:format "~4f ~4f" (car o) (- (cdr o))))
83 (define (number-list->point lst)
87 (cons (format "~S,~S" (car lst) (cadr lst))
88 (helper (cddr lst)))))
90 (string-join (helper lst) " "))
93 (define (svg-bezier lst close)
94 (let* ((c0 (car (list-tail lst 3)))
95 (c123 (list-head lst 3)))
97 (if (not close) "M" "L")
99 "C" (string-join (map offset->point c123) " ")
100 (if (not close) "" "z"))))
105 (define (integer->entity integer)
106 (fancy-format "&#x~x;" integer))
108 (define (char->entity char)
109 (integer->entity (char->integer char)))
111 (define (string->entities string)
113 (map (lambda (x) (char->entity x)) (string->list string))))
115 (define svg-element-regexp
116 (make-regexp "^(<[a-z]+) ?(.*>)"))
118 (define scaled-element-regexp
119 (make-regexp "^(<[a-z]+ transform=\")(scale.[-0-9. ]+,[-0-9. ]+.\" .*>)"))
121 (define pango-description-regexp-comma
122 (make-regexp ",( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$"))
124 (define pango-description-regexp-nocomma
125 (make-regexp "( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$"))
127 (define (pango-description-to-text str expr)
129 (define (set-attribute attr val)
130 (set! alist (assoc-set! alist attr val)))
132 ((match-1 (regexp-exec pango-description-regexp-comma str))
133 (match-2 (regexp-exec pango-description-regexp-nocomma str))
138 (if (regexp-match? match)
140 (set-attribute 'font-family (match:prefix match))
141 (if (string? (match:substring match 1))
142 (set-attribute 'font-weight "bold"))
143 (if (string? (match:substring match 2))
144 (set-attribute 'font-style "italic"))
145 (if (string? (match:substring match 3))
146 (set-attribute 'font-variant "small-caps"))
147 (set-attribute 'font-size
148 (/ (string->number (match:substring match 4))
150 (set-attribute 'text-anchor "start")
151 (set-attribute 'fill "currentColor"))
152 (ly:warning (_ "cannot decypher Pango description: ~a") str))
154 (apply entity 'text expr (reverse! alist))))
156 (define (dump-path path scale . rest)
158 (define (set-attribute attr val)
159 (set! alist (assoc-set! alist attr val)))
160 (if (not (null? rest))
161 (let* ((dx (car rest))
163 (total-x (+ dx next-horiz-adv)))
164 (if (or (not (zero? total-x))
166 (let ((x (ly:format "~4f" total-x))
167 (y (ly:format "~4f" dy)))
168 (set-attribute 'transform
170 "translate(" x ", " y ") "
171 "scale(" scale ", -" scale ")")))
172 (set-attribute 'transform
174 "scale(" scale ", -" scale ")"))))
175 (set-attribute 'transform (string-append
176 "scale(" scale ", -" scale ")")))
178 (set-attribute 'd path)
179 (set-attribute 'fill "currentColor")
180 (apply entity 'path "" (reverse alist)))
183 ;; A global variable for keeping track of the *cumulative*
184 ;; horizontal advance for glyph strings, but only if there
185 ;; is more than one glyph.
186 (define next-horiz-adv 0.0)
188 ;; Matches the required "unicode" attribute from <glyph>
189 (define glyph-unicode-value-regexp
190 (make-regexp "unicode=\"([^\"]+)\""))
192 ;; Matches the optional path data from <glyph>
193 (define glyph-path-regexp
194 (make-regexp "d=\"([-MmZzLlHhVvCcSsQqTt0-9.\n ]*)\""))
196 ;; Matches a complete <glyph> element with the glyph-name
197 ;; attribute value of NAME. For example:
199 ;; <glyph glyph-name="period" unicode="." horiz-adv-x="110"
200 ;; d="M0 55c0 30 25 55 55 55s55 -25 55
201 ;; -55s-25 -55 -55 -55s-55 25 -55 55z" />
203 ;; TODO: it would be better to use an XML library to extract
204 ;; the glyphs instead, and store them in a hash table. --pmccarty
206 (define (glyph-element-regexp name)
207 (make-regexp (string-append "<glyph"
208 "(([\r\n\t ]+[-a-z]+=\"[^\"]*\")+)?"
209 "[\r\n\t ]+glyph-name=\"("
212 "(([\r\n\t ]+[-a-z]+=\"[^\"]*\")+)?"
216 (define (extract-glyph all-glyphs name size . rest)
217 (let* ((new-name (regexp-quote name))
218 (regexp (regexp-exec (glyph-element-regexp new-name) all-glyphs))
219 (glyph (match:substring regexp))
220 (unicode-attr (regexp-exec glyph-unicode-value-regexp glyph))
221 (unicode-attr-value (match:substring unicode-attr 1))
222 (unicode-attr? (regexp-match? unicode-attr))
223 (d-attr (regexp-exec glyph-path-regexp glyph))
225 (d-attr? (regexp-match? d-attr))
226 ;; TODO: not urgent, but do not hardcode this value
228 (font-scale (ly:format "~4f" (/ size units-per-em)))
231 (if (and unicode-attr? (not unicode-attr-value))
232 (ly:warning (_ "Glyph must have a unicode value")))
234 (if d-attr? (set! d-attr-value (match:substring d-attr 1)))
237 ;; Glyph-strings with path data
238 (and d-attr? (not (null? rest)))
240 (set! path (apply dump-path d-attr-value
242 (list (cadr rest) (caddr rest))))
243 (set! next-horiz-adv (+ next-horiz-adv
246 ;; Glyph-strings without path data ("space")
247 ((and (not d-attr?) (not (null? rest)))
249 (set! next-horiz-adv (+ next-horiz-adv
252 ;; Font smobs with path data
253 ((and d-attr? (null? rest))
254 (set! path (dump-path d-attr-value font-scale))
256 ;; Font smobs without path data ("space")
260 (define (extract-glyph-info all-glyphs glyph size)
261 (let* ((offsets (list-head glyph 3))
262 (glyph-name (car (reverse glyph))))
263 (apply extract-glyph all-glyphs glyph-name size offsets)))
265 (define (svg-defs svg-font)
266 (let ((start (string-contains svg-font "<defs>"))
267 (end (string-contains svg-font "</defs>")))
268 (substring svg-font (+ start 7) (- end 1))))
270 (define (cache-font svg-font size glyph)
271 (let ((all-glyphs (svg-defs (cached-file-contents svg-font))))
273 (extract-glyph-info all-glyphs glyph size)
274 (extract-glyph all-glyphs glyph size))))
277 (define (feta-alphabet-to-path font size glyph)
278 (let* ((name-style (font-name-style font))
279 (scaled-size (/ size lily-unit-length))
280 (font-file (ly:find-file (string-append name-style ".svg"))))
283 (cache-font font-file scaled-size glyph)
284 (ly:warning (_ "cannot find SVG font ~S") font-file))))
287 (define (font-smob-to-path font glyph)
288 (let* ((name-style (font-name-style font))
289 (scaled-size (modified-font-metric-font-scaling font))
290 (font-file (ly:find-file (string-append name-style ".svg"))))
293 (cache-font font-file scaled-size glyph)
294 (ly:warning (_ "cannot find SVG font ~S") font-file))))
297 (define (fontify font expr)
299 (pango-description-to-text font expr)
300 (font-smob-to-path font expr)))
302 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
303 ;;; stencil outputters
306 (define (bezier-sandwich lst thick)
307 (let* ((first (list-tail lst 4))
308 (second (list-head lst 4)))
310 '(stroke-linejoin . "round")
311 '(stroke-linecap . "round")
312 '(stroke . "currentColor")
313 '(fill . "currentColor")
314 `(stroke-width . ,thick)
315 `(d . ,(string-append (svg-bezier first #f)
316 (svg-bezier second #t)))
319 (define (char font i)
321 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
323 (define (circle radius thick is-filled)
326 '(stroke-linejoin . "round")
327 '(stroke-linecap . "round")
328 `(fill . ,(if is-filled "currentColor" "none"))
329 `(stroke . "currentColor")
330 `(stroke-width . ,thick)
333 (define (dashed-line thick on off dx dy phase)
334 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
336 (define (draw-line thick x1 y1 x2 y2 . alist)
337 (apply entity 'line ""
339 `((stroke-linejoin . "round")
340 (stroke-linecap . "round")
341 (stroke-width . ,thick)
342 (stroke . "currentColor")
349 (define (ellipse x-radius y-radius thick is-filled)
352 '(stroke-linejoin . "round")
353 '(stroke-linecap . "round")
354 `(fill . ,(if is-filled "currentColor" "none"))
355 `(stroke . "currentColor")
356 `(stroke-width . ,thick)
360 (define (embedded-svg string)
363 (define (glyph-string font size cid glyphs)
365 (if (= 1 (length glyphs))
366 (set! path (feta-alphabet-to-path font size (car glyphs)))
369 (string-append (eo 'g)
372 (feta-alphabet-to-path font size x))
376 (set! next-horiz-adv 0.0)
379 (define (grob-cause offset grob)
382 (define (named-glyph font name)
383 (dispatch `(fontify ,font ,name)))
388 (define (oval x-radius y-radius thick is-filled)
389 (let ((x-max x-radius)
392 (y-min (- y-radius)))
395 '(stroke-linejoin . "round")
396 '(stroke-linecap . "round")
397 `(fill . ,(if is-filled "currentColor" "none"))
398 `(stroke . "currentColor")
399 `(stroke-width . ,thick)
400 `(d . ,(ly:format "M~4f,~4f C~4f,~4f ~4f,~4f ~4f,~4f S~4f,~4f ~4f,~4f"
408 (define (path thick commands)
409 (define (convert-path-exps exps)
416 ((memq head '(rmoveto rlineto lineto moveto)) 2)
417 ((memq head '(rcurveto curveto)) 6)
419 (args (take rest arity))
420 (svg-head (assoc-get head '((rmoveto . m)
429 (cons (format "~a~a "
430 svg-head (number-list->point args)
432 (convert-path-exps (drop rest arity))))
436 `(stroke-width . ,thick)
437 '(stroke-linejoin . "round")
438 '(stroke-linecap . "round")
439 '(stroke . "currentColor")
441 `(d . ,(string-join (convert-path-exps commands) " "))))
443 (define (placebox x y expr)
444 (if (string-null? expr)
447 ((normal-element (regexp-exec svg-element-regexp expr))
448 (scaled-element (regexp-exec scaled-element-regexp expr))
449 (scaled? (if scaled-element #t #f))
450 (match (if scaled? scaled-element normal-element))
451 (string1 (match:substring match 1))
452 (string2 (match:substring match 2)))
455 (string-append string1
456 (ly:format "translate(~4f, ~4f) " x (- y))
459 (string-append string1
460 (ly:format " transform=\"translate(~4f, ~4f)\" "
465 (define (polygon coords blot-diameter is-filled)
468 '(stroke-linejoin . "round")
469 '(stroke-linecap . "round")
470 `(stroke-width . ,blot-diameter)
471 `(fill . ,(if is-filled "currentColor" "none"))
472 '(stroke . "currentColor")
473 `(points . ,(string-join
474 (map offset->point (ly:list->offsets '() coords))))
480 (define (resetrotation ang x y)
483 (define (round-filled-box breapth width depth height blot-diameter)
485 ;; The stroke will stick out. To use stroke,
486 ;; the stroke-width must be subtracted from all other dimensions.
487 ;;'(stroke-linejoin . "round")
488 ;;'(stroke-linecap . "round")
489 ;;`(stroke-width . ,blot)
495 `(width . ,(+ breapth width))
496 `(height . ,(+ depth height))
497 `(ry . ,(/ blot-diameter 2))
498 '(fill . "currentColor")
501 (define (setcolor r g b)
502 (format "<g color=\"rgb(~a%,~a%,~a%)\">\n"
503 (* 100 r) (* 100 g) (* 100 b)
506 ;; rotate around given point
507 (define (setrotation ang x y)
508 (format "<g transform=\"rotate(~a,~a,~a)\">\n"
509 (number->string (* -1 ang))
511 (number->string (* -1 y))))
513 (define (text font string)
514 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
516 (define (url-link url x y)
518 (eo 'a `(xlink:href . ,url))
522 `(width . ,(- (cdr x) (car x)))
523 `(height . ,(- (cdr y) (car y)))
526 '(stroke-width . "0.0"))
529 (define (utf-8-string pango-font-description string)
530 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))