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 (format " ~S,~S" (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 " (apply string-append (map offset->point c123))
100 (if (not close) "" (string-append
101 "L " (offset->point close))))))
106 (define (integer->entity integer)
107 (fancy-format "&#x~x;" integer))
109 (define (char->entity char)
110 (integer->entity (char->integer char)))
112 (define (string->entities string)
114 (map (lambda (x) (char->entity x)) (string->list string))))
116 (define svg-element-regexp
117 (make-regexp "^(<[a-z]+) ?(.*>)"))
119 (define scaled-element-regexp
120 (make-regexp "^(<[a-z]+ transform=\")(scale.[-0-9. ]+,[-0-9. ]+.\" .*>)"))
122 (define pango-description-regexp-comma
123 (make-regexp ",( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$"))
125 (define pango-description-regexp-nocomma
126 (make-regexp "( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$"))
128 (define (pango-description-to-text str expr)
130 (define (set-attribute attr val)
131 (set! alist (assoc-set! alist attr val)))
133 ((match-1 (regexp-exec pango-description-regexp-comma str))
134 (match-2 (regexp-exec pango-description-regexp-nocomma str))
139 (if (regexp-match? match)
141 (set-attribute 'font-family (match:prefix match))
142 (if (string? (match:substring match 1))
143 (set-attribute 'font-weight "bold"))
144 (if (string? (match:substring match 2))
145 (set-attribute 'font-style "italic"))
146 (if (string? (match:substring match 3))
147 (set-attribute 'font-variant "small-caps"))
148 (set-attribute 'font-size
149 (/ (string->number (match:substring match 4))
151 (set-attribute 'text-anchor "start")
152 (set-attribute 'fill "currentColor"))
153 (ly:warning (_ "cannot decypher Pango description: ~a") str))
155 (apply entity 'text expr (reverse! alist))))
157 (define (dump-path path scale . rest)
159 (define (set-attribute attr val)
160 (set! alist (assoc-set! alist attr val)))
161 (if (not (null? rest))
162 (let* ((dx (car rest))
164 (total-x (+ dx next-horiz-adv)))
165 (if (or (not (zero? total-x))
167 (let ((x (ly:format "~4f" total-x))
168 (y (ly:format "~4f" dy)))
169 (set-attribute 'transform
171 "translate(" x ", " y ") "
172 "scale(" scale ", -" scale ")")))
173 (set-attribute 'transform
175 "scale(" scale ", -" scale ")"))))
176 (set-attribute 'transform (string-append
177 "scale(" scale ", -" scale ")")))
179 (set-attribute 'd path)
180 (set-attribute 'fill "currentColor")
181 (apply entity 'path "" (reverse alist)))
184 ;; A global variable for keeping track of the *cumulative*
185 ;; horizontal advance for glyph strings, but only if there
186 ;; is more than one glyph.
187 (define next-horiz-adv 0.0)
189 ;; Matches the required "unicode" attribute from <glyph>
190 (define glyph-unicode-value-regexp
191 (make-regexp "unicode=\"([^\"]+)\""))
193 ;; Matches the optional path data from <glyph>
194 (define glyph-path-regexp
195 (make-regexp "d=\"([-MmZzLlHhVvCcSsQqTt0-9.\n ]*)\""))
197 ;; Matches a complete <glyph> element with the glyph-name
198 ;; attribute value of NAME. For example:
200 ;; <glyph glyph-name="period" unicode="." horiz-adv-x="110"
201 ;; d="M0 55c0 30 25 55 55 55s55 -25 55
202 ;; -55s-25 -55 -55 -55s-55 25 -55 55z" />
204 ;; TODO: it would be better to use an XML library to extract
205 ;; the glyphs instead, and store them in a hash table. --pmccarty
207 (define (glyph-element-regexp name)
208 (make-regexp (string-append "<glyph"
209 "(([\r\n\t ]+[-a-z]+=\"[^\"]*\")+)?"
210 "[\r\n\t ]+glyph-name=\"("
213 "(([\r\n\t ]+[-a-z]+=\"[^\"]*\")+)?"
217 (define (extract-glyph all-glyphs name size . rest)
218 (let* ((new-name (regexp-quote name))
219 (regexp (regexp-exec (glyph-element-regexp new-name) all-glyphs))
220 (glyph (match:substring regexp))
221 (unicode-attr (regexp-exec glyph-unicode-value-regexp glyph))
222 (unicode-attr-value (match:substring unicode-attr 1))
223 (unicode-attr? (regexp-match? unicode-attr))
224 (d-attr (regexp-exec glyph-path-regexp glyph))
226 (d-attr? (regexp-match? d-attr))
227 ;; TODO: not urgent, but do not hardcode this value
229 (font-scale (ly:format "~4f" (/ size units-per-em)))
232 (if (and unicode-attr? (not unicode-attr-value))
233 (ly:warning (_ "Glyph must have a unicode value")))
235 (if d-attr? (set! d-attr-value (match:substring d-attr 1)))
238 ;; Glyph-strings with path data
239 (and d-attr? (not (null? rest)))
241 (set! path (apply dump-path d-attr-value
243 (list (cadr rest) (caddr rest))))
244 (set! next-horiz-adv (+ next-horiz-adv
247 ;; Glyph-strings without path data ("space")
248 ((and (not d-attr?) (not (null? rest)))
250 (set! next-horiz-adv (+ next-horiz-adv
253 ;; Font smobs with path data
254 ((and d-attr? (null? rest))
255 (set! path (dump-path d-attr-value font-scale))
257 ;; Font smobs without path data ("space")
261 (define (extract-glyph-info all-glyphs glyph size)
262 (let* ((offsets (list-head glyph 3))
263 (glyph-name (car (reverse glyph))))
264 (apply extract-glyph all-glyphs glyph-name size offsets)))
266 (define (svg-defs svg-font)
267 (let ((start (string-contains svg-font "<defs>"))
268 (end (string-contains svg-font "</defs>")))
269 (substring svg-font (+ start 7) (- end 1))))
271 (define (cache-font svg-font size glyph)
272 (let ((all-glyphs (svg-defs (cached-file-contents svg-font))))
274 (extract-glyph-info all-glyphs glyph size)
275 (extract-glyph all-glyphs glyph size))))
278 (define (feta-alphabet-to-path font size glyph)
279 (let* ((name-style (font-name-style font))
280 (scaled-size (/ size lily-unit-length))
281 (font-file (ly:find-file (string-append name-style ".svg"))))
284 (cache-font font-file scaled-size glyph)
285 (ly:warning (_ "cannot find SVG font ~S") font-file))))
288 (define (font-smob-to-path font glyph)
289 (let* ((name-style (font-name-style font))
290 (scaled-size (modified-font-metric-font-scaling font))
291 (font-file (ly:find-file (string-append name-style ".svg"))))
294 (cache-font font-file scaled-size glyph)
295 (ly:warning (_ "cannot find SVG font ~S") font-file))))
298 (define (fontify font expr)
300 (pango-description-to-text font expr)
301 (font-smob-to-path font expr)))
303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
304 ;;; stencil outputters
307 (define (bezier-sandwich lst thick)
308 (let* ((first (list-tail lst 4))
309 (first-c0 (car (list-tail first 3)))
310 (second (list-head lst 4)))
312 '(stroke-linejoin . "round")
313 '(stroke-linecap . "round")
314 '(stroke . "currentColor")
315 '(fill . "currentColor")
316 `(stroke-width . ,thick)
317 `(d . ,(string-append (svg-bezier first #f)
318 (svg-bezier second first-c0)))
321 (define (char font i)
323 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
325 (define (circle radius thick is-filled)
328 '(stroke-linejoin . "round")
329 '(stroke-linecap . "round")
330 `(fill . ,(if is-filled "currentColor" "none"))
331 `(stroke . "currentColor")
332 `(stroke-width . ,thick)
335 (define (dashed-line thick on off dx dy phase)
336 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
338 (define (draw-line thick x1 y1 x2 y2 . alist)
339 (apply entity 'line ""
341 `((stroke-linejoin . "round")
342 (stroke-linecap . "round")
343 (stroke-width . ,thick)
344 (stroke . "currentColor")
351 (define (ellipse x-radius y-radius thick is-filled)
354 '(stroke-linejoin . "round")
355 '(stroke-linecap . "round")
356 `(fill . ,(if is-filled "currentColor" "none"))
357 `(stroke . "currentColor")
358 `(stroke-width . ,thick)
362 (define (embedded-svg string)
365 (define (glyph-string font size cid glyphs)
367 (if (= 1 (length glyphs))
368 (set! path (feta-alphabet-to-path font size (car glyphs)))
371 (string-append (eo 'g)
374 (feta-alphabet-to-path font size x))
378 (set! next-horiz-adv 0.0)
381 (define (grob-cause offset grob)
384 (define (named-glyph font name)
385 (dispatch `(fontify ,font ,name)))
390 (define (oval x-radius y-radius thick is-filled)
391 (let ((x-max x-radius)
394 (y-min (- y-radius)))
397 '(stroke-linejoin . "round")
398 '(stroke-linecap . "round")
399 `(fill . ,(if is-filled "currentColor" "none"))
400 `(stroke . "currentColor")
401 `(stroke-width . ,thick)
402 `(d . ,(ly:format "M~4f,~4f C~4f,~4f ~4f,~4f ~4f,~4f S~4f,~4f ~4f,~4f"
410 (define (path thick commands)
411 (define (convert-path-exps exps)
418 ((memq head '(rmoveto rlineto lineto moveto)) 2)
419 ((memq head '(rcurveto curveto)) 6)
421 (args (take rest arity))
422 (svg-head (assoc-get head '((rmoveto . m)
431 (cons (format "~a~a "
432 svg-head (number-list->point args)
434 (convert-path-exps (drop rest arity))))
438 `(stroke-width . ,thick)
439 '(stroke-linejoin . "round")
440 '(stroke-linecap . "round")
441 '(stroke . "currentColor")
443 `(d . ,(string-join (convert-path-exps commands) " "))))
445 (define (placebox x y expr)
446 (if (string-null? expr)
449 ((normal-element (regexp-exec svg-element-regexp expr))
450 (scaled-element (regexp-exec scaled-element-regexp expr))
451 (scaled? (if scaled-element #t #f))
452 (match (if scaled? scaled-element normal-element))
453 (string1 (match:substring match 1))
454 (string2 (match:substring match 2)))
457 (string-append string1
458 (ly:format "translate(~4f, ~4f) " x (- y))
461 (string-append string1
462 (ly:format " transform=\"translate(~4f, ~4f)\" "
467 (define (polygon coords blot-diameter is-filled)
470 '(stroke-linejoin . "round")
471 '(stroke-linecap . "round")
472 `(stroke-width . ,blot-diameter)
473 `(fill . ,(if is-filled "currentColor" "none"))
474 '(stroke . "currentColor")
475 `(points . ,(string-join
476 (map offset->point (ly:list->offsets '() coords))))
482 (define (resetrotation ang x y)
485 (define (round-filled-box breapth width depth height blot-diameter)
487 ;; The stroke will stick out. To use stroke,
488 ;; the stroke-width must be subtracted from all other dimensions.
489 ;;'(stroke-linejoin . "round")
490 ;;'(stroke-linecap . "round")
491 ;;`(stroke-width . ,blot)
497 `(width . ,(+ breapth width))
498 `(height . ,(+ depth height))
499 `(ry . ,(/ blot-diameter 2))
500 '(fill . "currentColor")
503 (define (setcolor r g b)
504 (format "<g color=\"rgb(~a%,~a%,~a%)\">\n"
505 (* 100 r) (* 100 g) (* 100 b)
508 ;; rotate around given point
509 (define (setrotation ang x y)
510 (format "<g transform=\"rotate(~a,~a,~a)\">\n"
511 (number->string (* -1 ang))
513 (number->string (* -1 y))))
515 (define (text font string)
516 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
518 (define (url-link url x y)
520 (eo 'a `(xlink:href . ,url))
524 `(width . ,(- (cdr x) (car x)))
525 `(height . ,(- (cdr y) (car y)))
528 '(stroke-width . "0.0"))
531 (define (utf-8-string pango-font-description string)
532 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))