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)
51 (map (lambda (x) (format " ~s=\"~a\"" (car x) (cdr x)))
54 (define-public (eo entity . attributes-alist)
56 (format "<~S~a>\n" entity (attributes attributes-alist)))
58 (define-public (eoc entity . attributes-alist)
60 (format "<~S~a/>\n" entity (attributes attributes-alist)))
62 (define-public (ec entity)
64 (format "</~S>\n" entity))
66 (define-public (comment s)
67 (string-append "<!-- " s " -->\n"))
69 (define-public (entity entity string . attributes-alist)
70 (if (equal? string "")
71 (apply eoc entity attributes-alist)
73 (apply eo (cons entity attributes-alist)) string (ec entity))))
75 (define (offset->point o)
76 (format " ~S,~S" (car o) (- (cdr o))))
78 (define (number-list->point lst)
82 (cons (format "~S,~S" (car lst) (cadr lst))
83 (helper (cddr lst)))))
85 (string-join (helper lst) " "))
88 (define (svg-bezier lst close)
89 (let* ((c0 (car (list-tail lst 3)))
90 (c123 (list-head lst 3)))
92 (if (not close) "M " "L ")
94 "C " (apply string-append (map offset->point c123))
95 (if (not close) "" (string-append
96 "L " (offset->point close))))))
101 (define (integer->entity integer)
102 (fancy-format "&#x~x;" integer))
104 (define (char->entity char)
105 (integer->entity (char->integer char)))
107 (define (string->entities string)
109 (map (lambda (x) (char->entity x)) (string->list string))))
111 (define svg-element-regexp
112 (make-regexp "^(<[a-z]+) ?(.*>)"))
114 (define scaled-element-regexp
115 (make-regexp "^(<[a-z]+ transform=\")(scale.[-0-9. ]+,[-0-9. ]+.\" .*>)"))
117 (define pango-description-regexp-comma
118 (make-regexp ",( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$"))
120 (define pango-description-regexp-nocomma
121 (make-regexp "( Bold)?( Italic)?( Small-Caps)? ([0-9.]+)$"))
123 (define (pango-description-to-text str expr)
125 (define (set-attribute attr val)
126 (set! alist (assoc-set! alist attr val)))
128 ((match-1 (regexp-exec pango-description-regexp-comma str))
129 (match-2 (regexp-exec pango-description-regexp-nocomma str))
134 (if (regexp-match? match)
136 (set-attribute 'font-family (match:prefix match))
137 (if (string? (match:substring match 1))
138 (set-attribute 'font-weight "bold"))
139 (if (string? (match:substring match 2))
140 (set-attribute 'font-style "italic"))
141 (if (string? (match:substring match 3))
142 (set-attribute 'font-variant "small-caps"))
143 (set-attribute 'font-size
144 (/ (string->number (match:substring match 4))
146 (set-attribute 'text-anchor "start")
147 (set-attribute 'fill "currentColor"))
148 (ly:warning (_ "cannot decypher Pango description: ~a") str))
150 (apply entity 'text expr (reverse! alist))))
152 (define (dump-path path scale . rest)
154 (define (set-attribute attr val)
155 (set! alist (assoc-set! alist attr val)))
156 (if (not (null? rest))
157 (let* ((dx (car rest))
159 (total-x (+ dx next-horiz-adv)))
160 (if (or (not (= 0 (inexact->exact total-x)))
161 (not (= 0 (inexact->exact dy))))
162 (let ((x (ly:format "~4f" total-x))
163 (y (ly:format "~4f" dy)))
164 (set-attribute 'transform
166 "translate(" x ", " y ") "
167 "scale(" scale ", -" scale ")")))
168 (set-attribute 'transform
170 "scale(" scale ", -" scale ")"))))
171 (set-attribute 'transform (string-append
172 "scale(" scale ", -" scale ")")))
174 (set-attribute 'd path)
175 (apply entity 'path "" (reverse alist)))
178 ;; A global variable for keeping track of the *cumulative*
179 ;; horizontal advance for glyph strings, but only if there
180 ;; is more than one glyph.
181 (define next-horiz-adv 0.0)
183 ;; Matches the required "unicode" attribute from <glyph>
184 (define glyph-unicode-value-regexp
185 (make-regexp "unicode=\"([^\"]+)\""))
187 ;; Matches the optional path data from <glyph>
188 (define glyph-path-regexp
189 (make-regexp "d=\"([-MmZzLlHhVvCcSsQqTt0-9.\n ]*)\""))
191 ;; Matches a complete <glyph> element with the glyph-name
192 ;; attribute value of NAME. For example:
194 ;; <glyph glyph-name="period" unicode="." horiz-adv-x="110"
195 ;; d="M0 55c0 30 25 55 55 55s55 -25 55
196 ;; -55s-25 -55 -55 -55s-55 25 -55 55z" />
198 ;; TODO: it would be better to use an XML library to extract
199 ;; the glyphs instead, and store them in a hash table. --pmccarty
201 (define (glyph-element-regexp name)
202 (make-regexp (string-append "<glyph"
203 "(([\r\n\t ]+[-a-z]+=\"[^\"]*\")+)?"
204 "[\r\n\t ]+glyph-name=\"("
207 "(([\r\n\t ]+[-a-z]+=\"[^\"]*\")+)?"
211 (define (extract-glyph all-glyphs name size . rest)
212 (let* ((new-name (regexp-quote name))
213 (regexp (regexp-exec (glyph-element-regexp new-name) all-glyphs))
214 (glyph (match:substring regexp))
215 (unicode-attr (regexp-exec glyph-unicode-value-regexp glyph))
216 (unicode-attr-value (match:substring unicode-attr 1))
217 (unicode-attr? (regexp-match? unicode-attr))
218 (d-attr (regexp-exec glyph-path-regexp glyph))
220 (d-attr? (regexp-match? d-attr))
221 ;; TODO: not urgent, but do not hardcode this value
223 (font-scale (ly:format "~4f" (/ size units-per-em)))
226 (if (and unicode-attr? (not unicode-attr-value))
227 (ly:warning (_ "Glyph must have a unicode value")))
229 (if d-attr? (set! d-attr-value (match:substring d-attr 1)))
232 ;; Glyph-strings with path data
233 (and d-attr? (not (null? rest)))
235 (set! path (apply dump-path d-attr-value
237 (list (cadr rest) (caddr rest))))
238 (set! next-horiz-adv (+ next-horiz-adv
241 ;; Glyph-strings without path data ("space")
242 ((and (not d-attr?) (not (null? rest)))
244 (set! next-horiz-adv (+ next-horiz-adv
247 ;; Font smobs with path data
248 ((and d-attr? (null? rest))
249 (set! path (dump-path d-attr-value font-scale))
251 ;; Font smobs without path data ("space")
255 (define (extract-glyph-info all-glyphs glyph size)
256 (let* ((offsets (list-head glyph 3))
257 (glyph-name (car (reverse glyph))))
258 (apply extract-glyph all-glyphs glyph-name size offsets)))
260 (define (svg-defs svg-font)
261 (let ((start (string-contains svg-font "<defs>"))
262 (end (string-contains svg-font "</defs>")))
263 (substring svg-font (+ start 7) (- end 1))))
265 (define (cache-font svg-font size glyph)
266 (let ((all-glyphs (svg-defs (cached-file-contents svg-font))))
268 (extract-glyph-info all-glyphs glyph size)
269 (extract-glyph all-glyphs glyph size))))
272 (define (feta-alphabet-to-path font size glyph)
273 (let* ((name-style (font-name-style font))
274 (scaled-size (/ size lily-unit-length))
275 (font-file (ly:find-file (string-append name-style ".svg"))))
278 (cache-font font-file scaled-size glyph)
279 (ly:warning (_ "cannot find SVG font ~S") font-file))))
282 (define (font-smob-to-path font glyph)
283 (let* ((name-style (font-name-style font))
284 (scaled-size (modified-font-metric-font-scaling font))
285 (font-file (ly:find-file (string-append name-style ".svg"))))
288 (cache-font font-file scaled-size glyph)
289 (ly:warning (_ "cannot find SVG font ~S") font-file))))
292 (define (fontify font expr)
294 (pango-description-to-text font expr)
295 (font-smob-to-path font expr)))
297 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
298 ;;; stencil outputters
301 (define (bezier-sandwich lst thick)
302 (let* ((first (list-tail lst 4))
303 (first-c0 (car (list-tail first 3)))
304 (second (list-head lst 4)))
306 '(stroke-linejoin . "round")
307 '(stroke-linecap . "round")
308 '(stroke . "currentColor")
309 '(fill . "currentColor")
310 `(stroke-width . ,thick)
311 `(d . ,(string-append (svg-bezier first #f)
312 (svg-bezier second first-c0)))
315 (define (char font i)
317 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
319 (define (circle radius thick is-filled)
322 '(stroke-linejoin . "round")
323 '(stroke-linecap . "round")
324 `(fill . ,(if is-filled "currentColor" "none"))
325 `(stroke . "currentColor")
326 `(stroke-width . ,thick)
329 (define (dashed-line thick on off dx dy phase)
330 (draw-line thick 0 0 dx dy `(style . ,(format "stroke-dasharray:~a,~a;" on off))))
332 (define (draw-line thick x1 y1 x2 y2 . alist)
333 (apply entity 'line ""
335 `((stroke-linejoin . "round")
336 (stroke-linecap . "round")
337 (stroke-width . ,thick)
338 (stroke . "currentColor")
345 (define (ellipse x-radius y-radius thick is-filled)
348 '(stroke-linejoin . "round")
349 '(stroke-linecap . "round")
350 `(fill . ,(if is-filled "currentColor" "none"))
351 `(stroke . "currentColor")
352 `(stroke-width . ,thick)
356 (define (embedded-svg string)
359 (define (glyph-string font size cid glyphs)
361 (if (= 1 (length glyphs))
362 (set! path (feta-alphabet-to-path font size (car glyphs)))
365 (string-append (eo 'g)
368 (feta-alphabet-to-path font size x))
372 (set! next-horiz-adv 0.0)
375 (define (grob-cause offset grob)
378 (define (named-glyph font name)
379 (dispatch `(fontify ,font ,name)))
384 (define (oval x-radius y-radius thick is-filled)
385 (let ((x-max x-radius)
388 (y-min (- y-radius)))
391 '(stroke-linejoin . "round")
392 '(stroke-linecap . "round")
393 `(fill . ,(if is-filled "currentColor" "none"))
394 `(stroke . "currentColor")
395 `(stroke-width . ,thick)
396 `(d . ,(ly:format "M~4f,~4f C~4f,~4f ~4f,~4f ~4f,~4f S~4f,~4f ~4f,~4f"
404 (define (path thick commands)
405 (define (convert-path-exps exps)
412 ((memq head '(rmoveto rlineto lineto moveto)) 2)
413 ((memq head '(rcurveto curveto)) 6)
415 (args (take rest arity))
416 (svg-head (assoc-get head '((rmoveto . m)
425 (cons (format "~a~a "
426 svg-head (number-list->point args)
428 (convert-path-exps (drop rest arity))))
432 `(stroke-width . ,thick)
433 '(stroke-linejoin . "round")
434 '(stroke-linecap . "round")
435 '(stroke . "currentColor")
437 `(d . ,(string-join (convert-path-exps commands) " "))))
439 (define (placebox x y expr)
440 (if (string-null? expr)
443 ((normal-element (regexp-exec svg-element-regexp expr))
444 (scaled-element (regexp-exec scaled-element-regexp expr))
445 (scaled? (if scaled-element #t #f))
446 (match (if scaled? scaled-element normal-element))
447 (string1 (match:substring match 1))
448 (string2 (match:substring match 2)))
451 (string-append string1
452 (ly:format "translate(~4f, ~4f) " x (- y))
455 (string-append string1
456 (ly:format " transform=\"translate(~4f, ~4f)\" "
461 (define (polygon coords blot-diameter is-filled)
464 '(stroke-linejoin . "round")
465 '(stroke-linecap . "round")
466 `(stroke-width . ,blot-diameter)
467 `(fill . ,(if is-filled "currentColor" "none"))
468 '(stroke . "currentColor")
469 `(points . ,(string-join
470 (map offset->point (ly:list->offsets '() coords))))
476 (define (resetrotation ang x y)
479 (define (round-filled-box breapth width depth height blot-diameter)
481 ;; The stroke will stick out. To use stroke,
482 ;; the stroke-width must be subtracted from all other dimensions.
483 ;;'(stroke-linejoin . "round")
484 ;;'(stroke-linecap . "round")
485 ;;`(stroke-width . ,blot)
491 `(width . ,(+ breapth width))
492 `(height . ,(+ depth height))
493 `(ry . ,(/ blot-diameter 2))
494 '(fill . "currentColor")
497 (define (setcolor r g b)
498 (format "<g color=\"rgb(~a%,~a%,~a%)\">\n"
499 (* 100 r) (* 100 g) (* 100 b)
502 ;; rotate around given point
503 (define (setrotation ang x y)
504 (format "<g transform=\"rotate(~a,~a,~a)\">\n"
505 (number->string (* -1 ang))
507 (number->string (* -1 y))))
509 (define (text font string)
510 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
512 (define (url-link url x y)
514 (eo 'a `(xlink:href . ,url))
518 `(width . ,(- (cdr x) (car x)))
519 `(height . ,(- (cdr y) (car y)))
522 '(stroke-width . "0.0"))
525 (define (utf-8-string pango-font-description string)
526 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))