1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2002--2012 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Patrick McCarty <pnorcks@gmail.com>
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (scm output-svg))
20 (define this-module (current-module))
22 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; set by framework-gnome.scm
37 (define fancy-format format)
38 (define format ergonomic-simple-format)
40 (define lily-unit-length 1.7573)
42 (define (dispatch expr)
43 (let ((keyword (car expr)))
44 (cond ((eq? keyword 'some-func) "")
45 (else (if (module-defined? this-module keyword)
46 (apply (eval keyword this-module) (cdr expr))
47 (begin (ly:warning (_ "undefined: ~S") keyword)
51 (define-public (attributes attributes-alist)
57 (set! value (ly:format "~4f" value)))
58 (format #f " ~s=\"~a\"" attr value)))
61 (define-public (eo entity . attributes-alist)
63 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
65 (define-public (eoc entity . attributes-alist)
67 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
69 (define-public (ec entity)
71 (format #f "</~S>\n" entity))
73 (define (start-enclosing-id-node s)
74 (string-append "<g id=\"" s "\">\n"))
76 (define (end-enclosing-id-node)
79 (define-public (comment s)
80 (string-append "<!-- " s " -->\n"))
82 (define-public (entity entity string . attributes-alist)
83 (if (equal? string "")
84 (apply eoc entity attributes-alist)
86 (apply eo (cons entity attributes-alist)) string (ec entity))))
88 (define (offset->point o)
89 (ly:format "~4f ~4f" (car o) (- (cdr o))))
91 (define (number-list->point lst)
95 (cons (format #f "~S ~S" (car lst) (- (cadr lst)))
96 (helper (cddr lst)))))
98 (string-join (helper lst) " "))
101 (define (svg-bezier lst close)
102 (let* ((c0 (car (list-tail lst 3)))
103 (c123 (list-head lst 3)))
105 (if (not close) "M" "L")
107 "C" (string-join (map offset->point c123) " ")
108 (if (not close) "" "z"))))
113 (define (integer->entity integer)
114 (fancy-format "&#x~x;" integer))
116 (define (char->entity char)
117 (integer->entity (char->integer char)))
119 (define (string->entities string)
121 (map (lambda (x) (char->entity x)) (string->list string))))
123 (define svg-element-regexp
124 (make-regexp "^(<[a-z]+) ?(.*>)"))
126 (define scaled-element-regexp
127 (make-regexp "^(<[a-z]+ transform=\")(scale.[-0-9. ]+,[-0-9. ]+.\" .*>)"))
129 (define pango-description-regexp-comma
130 (make-regexp ",( Bold)?( Italic)?( Small-Caps)?[ -]([0-9.]+)$"))
132 (define pango-description-regexp-nocomma
133 (make-regexp "( Bold)?( Italic)?( Small-Caps)?[ -]([0-9.]+)$"))
135 (define (pango-description-to-text str expr)
137 (define (set-attribute attr val)
138 (set! alist (assoc-set! alist attr val)))
139 (let* ((match-1 (regexp-exec pango-description-regexp-comma str))
140 (match-2 (regexp-exec pango-description-regexp-nocomma str))
141 (match (if match-1 match-1 match-2)))
143 (if (regexp-match? match)
145 (set-attribute 'font-family (match:prefix match))
146 (if (string? (match:substring match 1))
147 (set-attribute 'font-weight "bold"))
148 (if (string? (match:substring match 2))
149 (set-attribute 'font-style "italic"))
150 (if (string? (match:substring match 3))
151 (set-attribute 'font-variant "small-caps"))
152 (set-attribute 'font-size
153 (/ (string->number (match:substring match 4))
155 (set-attribute 'text-anchor "start")
156 (set-attribute 'fill "currentColor"))
157 (ly:warning (_ "cannot decypher Pango description: ~a") str))
159 (apply entity 'text expr (reverse! alist))))
161 (define (dump-path path scale . rest)
163 (define (set-attribute attr val)
164 (set! alist (assoc-set! alist attr val)))
165 (if (not (null? rest))
166 (let* ((dx (car rest))
168 (total-x (+ dx next-horiz-adv)))
169 (if (or (not (zero? total-x))
171 (let ((x (ly:format "~4f" total-x))
172 (y (ly:format "~4f" dy)))
173 (set-attribute 'transform
175 "translate(" x ", " y ") "
176 "scale(" scale ", -" scale ")")))
177 (set-attribute 'transform
179 "scale(" scale ", -" scale ")"))))
180 (set-attribute 'transform (string-append
181 "scale(" scale ", -" scale ")")))
183 (set-attribute 'd path)
184 (set-attribute 'fill "currentColor")
185 (apply entity 'path "" (reverse alist)))
188 ;; A global variable for keeping track of the *cumulative*
189 ;; horizontal advance for glyph strings, but only if there
190 ;; is more than one glyph.
191 (define next-horiz-adv 0.0)
193 ;; Matches the required "unicode" attribute from <glyph>
194 (define glyph-unicode-value-regexp
195 (make-regexp "unicode=\"([^\"]+)\""))
197 ;; Matches the optional path data from <glyph>
198 (define glyph-path-regexp
199 (make-regexp "d=\"([-MmZzLlHhVvCcSsQqTt0-9.\n ]*)\""))
201 ;; Matches a complete <glyph> element with the glyph-name
202 ;; attribute value of NAME. For example:
204 ;; <glyph glyph-name="period" unicode="." horiz-adv-x="110"
205 ;; d="M0 55c0 30 25 55 55 55s55 -25 55
206 ;; -55s-25 -55 -55 -55s-55 25 -55 55z" />
208 ;; TODO: it would be better to use an XML library to extract
209 ;; the glyphs instead, and store them in a hash table. --pmccarty
211 (define (glyph-element-regexp name)
212 (make-regexp (string-append "<glyph"
213 "(([[:space:]]+[-a-z]+=\"[^\"]*\")+)?"
214 "[[:space:]]+glyph-name=\"("
217 "(([[:space:]]+[-a-z]+=\"[^\"]*\")+)?"
221 (define (extract-glyph all-glyphs name size . rest)
222 (let* ((new-name (regexp-quote name))
223 (regexp (regexp-exec (glyph-element-regexp new-name) all-glyphs))
224 (glyph (match:substring regexp))
225 (unicode-attr (regexp-exec glyph-unicode-value-regexp glyph))
226 (unicode-attr-value (match:substring unicode-attr 1))
227 (unicode-attr? (regexp-match? unicode-attr))
228 (d-attr (regexp-exec glyph-path-regexp glyph))
230 (d-attr? (regexp-match? d-attr))
231 ;; TODO: not urgent, but do not hardcode this value
233 (font-scale (ly:format "~4f" (/ size units-per-em)))
236 (if (and unicode-attr? (not unicode-attr-value))
237 (ly:warning (_ "Glyph must have a unicode value")))
239 (if d-attr? (set! d-attr-value (match:substring d-attr 1)))
242 ;; Glyph-strings with path data
243 (and d-attr? (not (null? rest)))
245 (set! path (apply dump-path d-attr-value
247 (list (cadr rest) (caddr rest))))
248 (set! next-horiz-adv (+ next-horiz-adv
251 ;; Glyph-strings without path data ("space")
252 ((and (not d-attr?) (not (null? rest)))
254 (set! next-horiz-adv (+ next-horiz-adv
257 ;; Font smobs with path data
258 ((and d-attr? (null? rest))
259 (set! path (dump-path d-attr-value font-scale))
261 ;; Font smobs without path data ("space")
265 (define (extract-glyph-info all-glyphs glyph size)
266 (let* ((offsets (list-head glyph 3))
267 (glyph-name (car (reverse glyph))))
268 (apply extract-glyph all-glyphs glyph-name size offsets)))
270 (define (svg-defs svg-font)
271 (let ((start (string-contains svg-font "<defs>"))
272 (end (string-contains svg-font "</defs>")))
273 (substring svg-font (+ start 7) (- end 1))))
275 (define (cache-font svg-font size glyph)
276 (let ((all-glyphs (svg-defs (cached-file-contents svg-font))))
278 (extract-glyph-info all-glyphs glyph size)
279 (extract-glyph all-glyphs glyph size))))
282 (define (music-string-to-path font size glyph)
283 (let* ((name-style (font-name-style font))
284 (scaled-size (/ size lily-unit-length))
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 (font-smob-to-path font glyph)
293 (let* ((name-style (font-name-style font))
294 (scaled-size (modified-font-metric-font-scaling font))
295 (font-file (ly:find-file (string-append name-style ".svg"))))
298 (cache-font font-file scaled-size glyph)
299 (ly:warning (_ "cannot find SVG font ~S") font-file))))
301 (define (woff-font-smob-to-text font expr)
302 (let* ((name-style (font-name-style font))
303 (scaled-size (modified-font-metric-font-scaling font))
304 (font-file (ly:find-file (string-append name-style ".woff")))
305 (charcode (ly:font-glyph-name-to-charcode font expr))
306 (char-lookup (format #f "&#~S;" charcode))
307 (glyph-by-name (eoc 'altglyph `(glyphname . ,expr)))
309 (comment "FIXME: how to select glyph by name, altglyph is broken?"))
310 (text (string-regexp-substitute "\n" ""
311 (string-append glyph-by-name apparently-broken char-lookup))))
313 (define (set-attribute attr val)
314 (set! alist (assoc-set! alist attr val)))
315 (set-attribute 'font-family name-style)
316 (set-attribute 'font-size scaled-size)
317 (apply entity 'text text (reverse! alist))))
319 (define font-smob-to-text
320 (if (not (ly:get-option 'svg-woff))
321 font-smob-to-path woff-font-smob-to-text))
323 (define (fontify font expr)
325 (pango-description-to-text font expr)
326 (font-smob-to-text font expr)))
328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 ;;; stencil outputters
332 (define (bezier-sandwich lst thick)
333 (let* ((first (list-tail lst 4))
334 (second (list-head lst 4)))
336 '(stroke-linejoin . "round")
337 '(stroke-linecap . "round")
338 '(stroke . "currentColor")
339 '(fill . "currentColor")
340 `(stroke-width . ,thick)
341 `(d . ,(string-append (svg-bezier first #f)
342 (svg-bezier second #t))))))
344 (define (char font i)
346 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
348 (define (circle radius thick is-filled)
351 '(stroke-linejoin . "round")
352 '(stroke-linecap . "round")
353 `(fill . ,(if is-filled "currentColor" "none"))
354 `(stroke . "currentColor")
355 `(stroke-width . ,thick)
358 (define (dashed-line thick on off dx dy phase)
359 (draw-line thick 0 0 dx dy
360 `(stroke-dasharray . ,(format #f "~a,~a" on off))))
362 (define (draw-line thick x1 y1 x2 y2 . alist)
363 (apply entity 'line ""
365 `((stroke-linejoin . "round")
366 (stroke-linecap . "round")
367 (stroke-width . ,thick)
368 (stroke . "currentColor")
375 (define (ellipse x-radius y-radius thick is-filled)
378 '(stroke-linejoin . "round")
379 '(stroke-linecap . "round")
380 `(fill . ,(if is-filled "currentColor" "none"))
381 `(stroke . "currentColor")
382 `(stroke-width . ,thick)
386 (define (partial-ellipse x-radius y-radius start-angle end-angle thick connect fill)
387 (define (make-ellipse-radius x-radius y-radius angle)
388 (/ (* x-radius y-radius)
389 (sqrt (+ (* (* y-radius y-radius)
390 (* (cos angle) (cos angle)))
391 (* (* x-radius x-radius)
392 (* (sin angle) (sin angle)))))))
394 ((new-start-angle (* PI-OVER-180 (angle-0-360 start-angle)))
395 (start-radius (make-ellipse-radius x-radius y-radius new-start-angle))
396 (new-end-angle (* PI-OVER-180 (angle-0-360 end-angle)))
397 (end-radius (make-ellipse-radius x-radius y-radius new-end-angle))
399 (x-end (- (* end-radius (cos new-end-angle))
400 (* start-radius (cos new-start-angle))))
401 (y-end (- (* end-radius (sin new-end-angle))
402 (* start-radius (sin new-start-angle)))))
403 (if (and (< (abs x-end) epsilon) (< (abs y-end) epsilon))
406 `(fill . ,(if fill "currentColor" "none"))
407 `(stroke . "currentColor")
408 `(stroke-width . ,thick)
409 '(stroke-linejoin . "round")
410 '(stroke-linecap . "round")
417 `(fill . ,(if fill "currentColor" "none"))
418 `(stroke . "currentColor")
419 `(stroke-width . ,thick)
420 '(stroke-linejoin . "round")
421 '(stroke-linecap . "round")
426 "M~4f ~4fA~4f ~4f 0 ~4f 0 ~4f ~4f"
427 (* start-radius (cos new-start-angle))
428 (- (* start-radius (sin new-start-angle)))
431 (if (> 0 (- new-start-angle new-end-angle)) 0 1)
432 (* end-radius (cos new-end-angle))
433 (- (* end-radius (sin new-end-angle))))
435 (ly:format "L~4f,~4f"
436 (* start-radius (cos new-start-angle))
437 (- (* start-radius (sin new-start-angle))))
440 (define (embedded-svg string)
443 (define (embedded-glyph-string font size cid glyphs)
445 (if (= 1 (length glyphs))
446 (set! path (music-string-to-path font size (car glyphs)))
449 (string-append (eo 'g)
452 (music-string-to-path font size x))
456 (set! next-horiz-adv 0.0)
459 (define (woff-glyph-string font-name size cid? w-x-y-named-glyphs)
460 (let* ((name-style (font-name-style font-name))
461 (family-designsize (regexp-exec (make-regexp "(.*)-([0-9]*)")
463 (family (if (regexp-match? family-designsize)
464 (match:substring family-designsize 1)
466 (design-size (if (regexp-match? family-designsize)
467 (match:substring family-designsize 2)
469 (scaled-size (/ size lily-unit-length))
470 (font (ly:paper-get-font paper `(((font-family . ,family)
472 `(design-size . design-size)))))))
473 (define (glyph-spec w x y g)
474 (let* ((charcode (ly:font-glyph-name-to-charcode font g))
475 (char-lookup (format #f "&#~S;" charcode))
476 (glyph-by-name (eoc 'altglyph `(glyphname . ,g)))
478 (comment "XFIXME: how to select glyph by name, altglyph is broken?")))
481 "<text~a font-family=\"~a\" font-size=\"~a\">~a</text>"
482 (if (or (> (abs x) 0.00001)
484 (ly:format " transform=\"translate(~4f,~4f)\"" x y)
486 name-style scaled-size
487 (string-regexp-substitute
489 (string-append glyph-by-name apparently-broken char-lookup)))))
491 (string-join (map (lambda (x) (apply glyph-spec x))
492 (reverse w-x-y-named-glyphs)) "\n")))
495 (if (not (ly:get-option 'svg-woff)) embedded-glyph-string woff-glyph-string))
497 (define (grob-cause offset grob)
500 (define (named-glyph font name)
501 (dispatch `(fontify ,font ,name)))
506 (define (oval x-radius y-radius thick is-filled)
507 (let ((x-max x-radius)
510 (y-min (- y-radius)))
513 '(stroke-linejoin . "round")
514 '(stroke-linecap . "round")
515 `(fill . ,(if is-filled "currentColor" "none"))
516 `(stroke . "currentColor")
517 `(stroke-width . ,thick)
518 `(d . ,(ly:format "M~4f ~4fC~4f ~4f ~4f ~4f ~4f ~4fS~4f ~4f ~4f ~4fz"
526 (define* (path thick commands #:optional (cap 'round) (join 'round) (fill? #f))
527 (define (convert-path-exps exps)
533 (cond ((memq head '(rmoveto rlineto lineto moveto)) 2)
534 ((memq head '(rcurveto curveto)) 6)
535 ((eq? head 'closepath) 0)
537 (args (take rest arity))
538 (svg-head (assoc-get head
548 (cons (format #f "~a~a" svg-head (number-list->point args))
549 (convert-path-exps (drop rest arity))))
552 (let* ((line-cap-styles '(butt round square))
553 (line-join-styles '(miter round bevel))
554 (cap-style (if (not (memv cap line-cap-styles))
556 (ly:warning (_ "unknown line-cap-style: ~S")
557 (symbol->string cap))
560 (join-style (if (not (memv join line-join-styles))
562 (ly:warning (_ "unknown line-join-style: ~S")
563 (symbol->string join))
567 `(stroke-width . ,thick)
568 `(stroke-linejoin . ,(symbol->string join-style))
569 `(stroke-linecap . ,(symbol->string cap-style))
570 '(stroke . "currentColor")
571 `(fill . ,(if fill? "currentColor" "none"))
572 `(d . ,(apply string-append (convert-path-exps commands))))))
574 (define (placebox x y expr)
575 (if (string-null? expr)
578 ((normal-element (regexp-exec svg-element-regexp expr))
579 (scaled-element (regexp-exec scaled-element-regexp expr))
580 (scaled? (if scaled-element #t #f))
581 (match (if scaled? scaled-element normal-element))
582 (string1 (match:substring match 1))
583 (string2 (match:substring match 2)))
586 (string-append string1
587 (ly:format "translate(~4f, ~4f) " x (- y))
590 (string-append string1
591 (ly:format " transform=\"translate(~4f, ~4f)\" "
596 (define (polygon coords blot-diameter is-filled)
599 '(stroke-linejoin . "round")
600 '(stroke-linecap . "round")
601 `(stroke-width . ,blot-diameter)
602 `(fill . ,(if is-filled "currentColor" "none"))
603 '(stroke . "currentColor")
604 `(points . ,(string-join
605 (map offset->point (ly:list->offsets '() coords))))))
607 (define (repeat-slash width slope thickness)
608 (define (euclidean-length x y)
609 (sqrt (+ (* x x) (* y y))))
610 (let* ((x-width (euclidean-length thickness (/ thickness slope)))
611 (height (* width slope)))
614 '(fill . "currentColor")
615 `(d . ,(ly:format "M0 0l~4f 0 ~4f ~4f ~4f 0z"
616 x-width width (- height) (- x-width))))))
621 (define (resetrotation ang x y)
627 (define (round-filled-box breapth width depth height blot-diameter)
630 ;; The stroke will stick out. To use stroke,
631 ;; the stroke-width must be subtracted from all other dimensions.
632 ;;'(stroke-linejoin . "round")
633 ;;'(stroke-linecap . "round")
634 ;;`(stroke-width . ,blot)
640 `(width . ,(+ breapth width))
641 `(height . ,(+ depth height))
642 `(ry . ,(/ blot-diameter 2))
643 '(fill . "currentColor")))
645 (define (setcolor r g b)
646 (format #f "<g color=\"rgb(~a%, ~a%, ~a%)\">\n"
647 (* 100 r) (* 100 g) (* 100 b)))
649 ;; rotate around given point
650 (define (setrotation ang x y)
651 (ly:format "<g transform=\"rotate(~4f, ~4f, ~4f)\">\n"
654 (define (setscale x y)
655 (ly:format "<g transform=\"scale(~4f, ~4f)\">\n"
658 (define (text font string)
659 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
661 (define (url-link url x y)
663 (eo 'a `(xlink:href . ,url))
667 `(width . ,(- (cdr x) (car x)))
668 `(height . ,(- (cdr y) (car y)))
671 '(stroke-width . "0.0"))
674 (define (utf-8-string pango-font-description string)
675 (let ((escaped-string (string-regexp-substitute
677 (string-regexp-substitute "&" "&" string))))
678 (dispatch `(fontify ,pango-font-description
679 ,(entity 'tspan escaped-string)))))