1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2002--2010 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
36 (define fancy-format format)
37 (define format ergonomic-simple-format)
39 (define lily-unit-length 1.7573)
41 (define (dispatch expr)
42 (let ((keyword (car expr)))
43 (cond ((eq? keyword 'some-func) "")
44 (else (if (module-defined? this-module keyword)
45 (apply (eval keyword this-module) (cdr expr))
46 (begin (ly:warning (_ "undefined: ~S") keyword)
50 (define-public (attributes attributes-alist)
56 (set! value (ly:format "~4f" value)))
57 (format " ~s=\"~a\"" attr value)))
60 (define-public (eo entity . attributes-alist)
62 (format "<~S~a>\n" entity (attributes attributes-alist)))
64 (define-public (eoc entity . attributes-alist)
66 (format "<~S~a/>\n" entity (attributes attributes-alist)))
68 (define-public (ec entity)
70 (format "</~S>\n" entity))
72 (define-public (comment s)
73 (string-append "<!-- " s " -->\n"))
75 (define-public (entity entity string . attributes-alist)
76 (if (equal? string "")
77 (apply eoc entity attributes-alist)
79 (apply eo (cons entity attributes-alist)) string (ec entity))))
81 (define (offset->point o)
82 (ly:format "~4f ~4f" (car o) (- (cdr o))))
84 (define (number-list->point lst)
88 (cons (format "~S ~S" (car lst) (- (cadr lst)))
89 (helper (cddr lst)))))
91 (string-join (helper lst) " "))
94 (define (svg-bezier lst close)
95 (let* ((c0 (car (list-tail lst 3)))
96 (c123 (list-head lst 3)))
98 (if (not close) "M" "L")
100 "C" (string-join (map offset->point c123) " ")
101 (if (not close) "" "z"))))
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)))
132 (let* ((match-1 (regexp-exec pango-description-regexp-comma str))
133 (match-2 (regexp-exec pango-description-regexp-nocomma str))
134 (match (if match-1 match-1 match-2)))
136 (if (regexp-match? match)
138 (set-attribute 'font-family (match:prefix match))
139 (if (string? (match:substring match 1))
140 (set-attribute 'font-weight "bold"))
141 (if (string? (match:substring match 2))
142 (set-attribute 'font-style "italic"))
143 (if (string? (match:substring match 3))
144 (set-attribute 'font-variant "small-caps"))
145 (set-attribute 'font-size
146 (/ (string->number (match:substring match 4))
148 (set-attribute 'text-anchor "start")
149 (set-attribute 'fill "currentColor"))
150 (ly:warning (_ "cannot decypher Pango description: ~a") str))
152 (apply entity 'text expr (reverse! alist))))
154 (define (dump-path path scale . rest)
156 (define (set-attribute attr val)
157 (set! alist (assoc-set! alist attr val)))
158 (if (not (null? rest))
159 (let* ((dx (car rest))
161 (total-x (+ dx next-horiz-adv)))
162 (if (or (not (zero? total-x))
164 (let ((x (ly:format "~4f" total-x))
165 (y (ly:format "~4f" dy)))
166 (set-attribute 'transform
168 "translate(" x ", " y ") "
169 "scale(" scale ", -" scale ")")))
170 (set-attribute 'transform
172 "scale(" scale ", -" scale ")"))))
173 (set-attribute 'transform (string-append
174 "scale(" scale ", -" scale ")")))
176 (set-attribute 'd path)
177 (set-attribute 'fill "currentColor")
178 (apply entity 'path "" (reverse alist)))
181 ;; A global variable for keeping track of the *cumulative*
182 ;; horizontal advance for glyph strings, but only if there
183 ;; is more than one glyph.
184 (define next-horiz-adv 0.0)
186 ;; Matches the required "unicode" attribute from <glyph>
187 (define glyph-unicode-value-regexp
188 (make-regexp "unicode=\"([^\"]+)\""))
190 ;; Matches the optional path data from <glyph>
191 (define glyph-path-regexp
192 (make-regexp "d=\"([-MmZzLlHhVvCcSsQqTt0-9.\n ]*)\""))
194 ;; Matches a complete <glyph> element with the glyph-name
195 ;; attribute value of NAME. For example:
197 ;; <glyph glyph-name="period" unicode="." horiz-adv-x="110"
198 ;; d="M0 55c0 30 25 55 55 55s55 -25 55
199 ;; -55s-25 -55 -55 -55s-55 25 -55 55z" />
201 ;; TODO: it would be better to use an XML library to extract
202 ;; the glyphs instead, and store them in a hash table. --pmccarty
204 (define (glyph-element-regexp name)
205 (make-regexp (string-append "<glyph"
206 "(([[:space:]]+[-a-z]+=\"[^\"]*\")+)?"
207 "[[:space:]]+glyph-name=\"("
210 "(([[:space:]]+[-a-z]+=\"[^\"]*\")+)?"
214 (define (extract-glyph all-glyphs name size . rest)
215 (let* ((new-name (regexp-quote name))
216 (regexp (regexp-exec (glyph-element-regexp new-name) all-glyphs))
217 (glyph (match:substring regexp))
218 (unicode-attr (regexp-exec glyph-unicode-value-regexp glyph))
219 (unicode-attr-value (match:substring unicode-attr 1))
220 (unicode-attr? (regexp-match? unicode-attr))
221 (d-attr (regexp-exec glyph-path-regexp glyph))
223 (d-attr? (regexp-match? d-attr))
224 ;; TODO: not urgent, but do not hardcode this value
226 (font-scale (ly:format "~4f" (/ size units-per-em)))
229 (if (and unicode-attr? (not unicode-attr-value))
230 (ly:warning (_ "Glyph must have a unicode value")))
232 (if d-attr? (set! d-attr-value (match:substring d-attr 1)))
235 ;; Glyph-strings with path data
236 (and d-attr? (not (null? rest)))
238 (set! path (apply dump-path d-attr-value
240 (list (cadr rest) (caddr rest))))
241 (set! next-horiz-adv (+ next-horiz-adv
244 ;; Glyph-strings without path data ("space")
245 ((and (not d-attr?) (not (null? rest)))
247 (set! next-horiz-adv (+ next-horiz-adv
250 ;; Font smobs with path data
251 ((and d-attr? (null? rest))
252 (set! path (dump-path d-attr-value font-scale))
254 ;; Font smobs without path data ("space")
258 (define (extract-glyph-info all-glyphs glyph size)
259 (let* ((offsets (list-head glyph 3))
260 (glyph-name (car (reverse glyph))))
261 (apply extract-glyph all-glyphs glyph-name size offsets)))
263 (define (svg-defs svg-font)
264 (let ((start (string-contains svg-font "<defs>"))
265 (end (string-contains svg-font "</defs>")))
266 (substring svg-font (+ start 7) (- end 1))))
268 (define (cache-font svg-font size glyph)
269 (let ((all-glyphs (svg-defs (cached-file-contents svg-font))))
271 (extract-glyph-info all-glyphs glyph size)
272 (extract-glyph all-glyphs glyph size))))
275 (define (music-string-to-path font size glyph)
276 (let* ((name-style (font-name-style font))
277 (scaled-size (/ size lily-unit-length))
278 (font-file (ly:find-file (string-append name-style ".svg"))))
281 (cache-font font-file scaled-size glyph)
282 (ly:warning (_ "cannot find SVG font ~S") font-file))))
285 (define (font-smob-to-path font glyph)
286 (let* ((name-style (font-name-style font))
287 (scaled-size (modified-font-metric-font-scaling font))
288 (font-file (ly:find-file (string-append name-style ".svg"))))
291 (cache-font font-file scaled-size glyph)
292 (ly:warning (_ "cannot find SVG font ~S") font-file))))
294 (define (woff-font-smob-to-text font expr)
295 (let* ((name-style (font-name-style font))
296 (scaled-size (modified-font-metric-font-scaling font))
297 (font-file (ly:find-file (string-append name-style ".woff")))
298 (charcode (ly:font-glyph-name-to-charcode font expr))
299 (char-lookup (format #f "&#~S;" charcode))
300 (glyph-by-name (eoc 'altglyph `(glyphname . ,expr)))
302 (comment "FIXME: how to select glyph by name, altglyph is broken?"))
303 (text (string-regexp-substitute "\n" ""
304 (string-append glyph-by-name apparently-broken char-lookup))))
306 (define (set-attribute attr val)
307 (set! alist (assoc-set! alist attr val)))
308 (set-attribute 'font-family name-style)
309 (set-attribute 'font-size scaled-size)
310 (apply entity 'text text (reverse! alist))))
312 (define font-smob-to-text
313 (if (not (ly:get-option 'svg-woff))
314 font-smob-to-path woff-font-smob-to-text))
316 (define (fontify font expr)
318 (pango-description-to-text font expr)
319 (font-smob-to-text font expr)))
321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
322 ;;; stencil outputters
325 (define (bezier-sandwich lst thick)
326 (let* ((first (list-tail lst 4))
327 (second (list-head lst 4)))
329 '(stroke-linejoin . "round")
330 '(stroke-linecap . "round")
331 '(stroke . "currentColor")
332 '(fill . "currentColor")
333 `(stroke-width . ,thick)
334 `(d . ,(string-append (svg-bezier first #f)
335 (svg-bezier second #t))))))
337 (define (char font i)
339 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
341 (define (circle radius thick is-filled)
344 '(stroke-linejoin . "round")
345 '(stroke-linecap . "round")
346 `(fill . ,(if is-filled "currentColor" "none"))
347 `(stroke . "currentColor")
348 `(stroke-width . ,thick)
351 (define (dashed-line thick on off dx dy phase)
352 (draw-line thick 0 0 dx dy
353 `(stroke-dasharray . ,(format "~a,~a" on off))))
355 (define (draw-line thick x1 y1 x2 y2 . alist)
356 (apply entity 'line ""
358 `((stroke-linejoin . "round")
359 (stroke-linecap . "round")
360 (stroke-width . ,thick)
361 (stroke . "currentColor")
368 (define (ellipse x-radius y-radius thick is-filled)
371 '(stroke-linejoin . "round")
372 '(stroke-linecap . "round")
373 `(fill . ,(if is-filled "currentColor" "none"))
374 `(stroke . "currentColor")
375 `(stroke-width . ,thick)
379 (define (partial-ellipse x-radius y-radius start-angle end-angle thick connect fill)
380 (define (make-ellipse-radius x-radius y-radius angle)
381 (/ (* x-radius y-radius)
382 (sqrt (+ (* (* y-radius y-radius)
383 (* (cos angle) (cos angle)))
384 (* (* x-radius x-radius)
385 (* (sin angle) (sin angle)))))))
387 ((dummy (format #t "INFO XR ~a YR ~a SA ~a EA ~a\n" x-radius y-radius start-angle end-angle))
388 (new-start-angle (* PI-OVER-180 (angle-0-360 start-angle)))
389 (start-radius (make-ellipse-radius x-radius y-radius new-start-angle))
390 (new-end-angle (* PI-OVER-180 (angle-0-360 end-angle)))
391 (end-radius (make-ellipse-radius x-radius y-radius new-end-angle))
393 (x-end (- (* end-radius (cos new-end-angle))
394 (* start-radius (cos new-start-angle))))
395 (y-end (- (* end-radius (sin new-end-angle))
396 (* start-radius (sin new-start-angle))))
397 (dummy (format #t "INFO NSA ~a SR ~a NEA ~a ER ~a\n" new-start-angle start-radius new-end-angle end-radius)))
398 (if (and (< (abs x-end) epsilon) (< (abs y-end) epsilon))
401 `(fill . ,(if fill "currentColor" "none"))
402 `(stroke . "currentColor")
403 `(stroke-width . ,thick)
404 '(stroke-linejoin . "round")
405 '(stroke-linecap . "round")
412 `(fill . ,(if fill "currentColor" "none"))
413 `(stroke . "currentColor")
414 `(stroke-width . ,thick)
415 '(stroke-linejoin . "round")
416 '(stroke-linecap . "round")
421 "M~4f ~4fA~4f ~4f 0 ~4f 0 ~4f ~4f"
422 (* start-radius (cos new-start-angle))
423 (- (* start-radius (sin new-start-angle)))
426 (if (> 0 (- new-start-angle new-end-angle)) 0 1)
427 (* end-radius (cos new-end-angle))
428 (- (* end-radius (sin new-end-angle))))
430 (ly:format "L~4f,~4f"
431 (* start-radius (cos new-start-angle))
432 (- (* start-radius (sin new-start-angle))))
435 (define (connected-shape pointlist thick x-scale y-scale connect fill)
438 `(fill . ,(if fill "currentColor" "none"))
439 `(stroke . "currentColor")
440 `(stroke-width . ,thick)
441 '(stroke-linejoin . "round")
442 '(stroke-linecap . "round")
450 (if (eq? (length x) 6)
451 (lambda (x1 x2 x3 x4 x5 x6)
452 (ly:format "C~4f ~4f ~4f ~4f ~4f ~4f"
460 (ly:format "L~4f ~4f"
462 (- (* y-scale x2)))))
465 (if connect "z " "")))))
467 (define (embedded-svg string)
470 (define (embedded-glyph-string font size cid glyphs)
472 (if (= 1 (length glyphs))
473 (set! path (music-string-to-path font size (car glyphs)))
476 (string-append (eo 'g)
479 (music-string-to-path font size x))
483 (set! next-horiz-adv 0.0)
486 (define (woff-glyph-string font-name size cid? w-x-y-named-glyphs)
487 (let* ((name-style (font-name-style font-name))
488 (family-designsize (regexp-exec (make-regexp "(.*)-([0-9]*)")
490 (family (if (regexp-match? family-designsize)
491 (match:substring family-designsize 1)
493 (design-size (if (regexp-match? family-designsize)
494 (match:substring family-designsize 2)
496 (scaled-size (/ size lily-unit-length))
497 (font (ly:paper-get-font paper `(((font-family . ,family)
499 `(design-size . design-size)))))))
500 (define (glyph-spec w x y g)
501 (let* ((charcode (ly:font-glyph-name-to-charcode font g))
502 (char-lookup (format #f "&#~S;" charcode))
503 (glyph-by-name (eoc 'altglyph `(glyphname . ,g)))
505 (comment "XFIXME: how to select glyph by name, altglyph is broken?")))
508 "<text~a font-family=\"~a\" font-size=\"~a\">~a</text>"
509 (if (or (> (abs x) 0.00001)
511 (ly:format " transform=\"translate(~4f,~4f)\"" x y)
513 name-style scaled-size
514 (string-regexp-substitute
516 (string-append glyph-by-name apparently-broken char-lookup)))))
518 (string-join (map (lambda (x) (apply glyph-spec x))
519 (reverse w-x-y-named-glyphs)) "\n")))
522 (if (not (ly:get-option 'svg-woff)) embedded-glyph-string woff-glyph-string))
524 (define (grob-cause offset grob)
527 (define (named-glyph font name)
528 (dispatch `(fontify ,font ,name)))
533 (define (oval x-radius y-radius thick is-filled)
534 (let ((x-max x-radius)
537 (y-min (- y-radius)))
540 '(stroke-linejoin . "round")
541 '(stroke-linecap . "round")
542 `(fill . ,(if is-filled "currentColor" "none"))
543 `(stroke . "currentColor")
544 `(stroke-width . ,thick)
545 `(d . ,(ly:format "M~4f ~4fC~4f ~4f ~4f ~4f ~4f ~4fS~4f ~4f ~4f ~4fz"
553 (define (path thick commands)
554 (define (convert-path-exps exps)
560 (cond ((memq head '(rmoveto rlineto lineto moveto)) 2)
561 ((memq head '(rcurveto curveto)) 6)
562 ((eq? head 'closepath) 0)
564 (args (take rest arity))
565 (svg-head (assoc-get head
575 (cons (format "~a~a" svg-head (number-list->point args))
576 (convert-path-exps (drop rest arity))))
580 `(stroke-width . ,thick)
581 '(stroke-linejoin . "round")
582 '(stroke-linecap . "round")
583 '(stroke . "currentColor")
585 `(d . ,(apply string-append (convert-path-exps commands)))))
587 (define (placebox x y expr)
588 (if (string-null? expr)
591 ((normal-element (regexp-exec svg-element-regexp expr))
592 (scaled-element (regexp-exec scaled-element-regexp expr))
593 (scaled? (if scaled-element #t #f))
594 (match (if scaled? scaled-element normal-element))
595 (string1 (match:substring match 1))
596 (string2 (match:substring match 2)))
599 (string-append string1
600 (ly:format "translate(~4f, ~4f) " x (- y))
603 (string-append string1
604 (ly:format " transform=\"translate(~4f, ~4f)\" "
609 (define (polygon coords blot-diameter is-filled)
612 '(stroke-linejoin . "round")
613 '(stroke-linecap . "round")
614 `(stroke-width . ,blot-diameter)
615 `(fill . ,(if is-filled "currentColor" "none"))
616 '(stroke . "currentColor")
617 `(points . ,(string-join
618 (map offset->point (ly:list->offsets '() coords))))))
620 (define (repeat-slash width slope thickness)
621 (define (euclidean-length x y)
622 (sqrt (+ (* x x) (* y y))))
623 (let* ((x-width (euclidean-length thickness (/ thickness slope)))
624 (height (* width slope)))
627 '(fill . "currentColor")
628 `(d . ,(ly:format "M0 0l~4f 0 ~4f ~4f ~4f 0z"
629 x-width width (- height) (- x-width))))))
634 (define (resetrotation ang x y)
637 (define (round-filled-box breapth width depth height blot-diameter)
640 ;; The stroke will stick out. To use stroke,
641 ;; the stroke-width must be subtracted from all other dimensions.
642 ;;'(stroke-linejoin . "round")
643 ;;'(stroke-linecap . "round")
644 ;;`(stroke-width . ,blot)
650 `(width . ,(+ breapth width))
651 `(height . ,(+ depth height))
652 `(ry . ,(/ blot-diameter 2))
653 '(fill . "currentColor")))
655 (define (setcolor r g b)
656 (format "<g color=\"rgb(~a%, ~a%, ~a%)\">\n"
657 (* 100 r) (* 100 g) (* 100 b)))
659 ;; rotate around given point
660 (define (setrotation ang x y)
661 (ly:format "<g transform=\"rotate(~4f, ~4f, ~4f)\">\n"
664 (define (text font string)
665 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
667 (define (url-link url x y)
669 (eo 'a `(xlink:href . ,url))
673 `(width . ,(- (cdr x) (car x)))
674 `(height . ,(- (cdr y) (car y)))
677 '(stroke-width . "0.0"))
680 (define (utf-8-string pango-font-description string)
681 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))