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 ((new-start-angle (* PI-OVER-180 (angle-0-360 start-angle)))
388 (start-radius (make-ellipse-radius x-radius y-radius new-start-angle))
389 (new-end-angle (* PI-OVER-180 (angle-0-360 end-angle)))
390 (end-radius (make-ellipse-radius x-radius y-radius new-end-angle))
392 (x-end (- (* end-radius (cos new-end-angle))
393 (* start-radius (cos new-start-angle))))
394 (y-end (- (* end-radius (sin new-end-angle))
395 (* start-radius (sin new-start-angle)))))
396 (if (and (< (abs x-end) epsilon) (< (abs y-end) epsilon))
399 `(fill . ,(if fill "currentColor" "none"))
400 `(stroke . "currentColor")
401 `(stroke-width . ,thick)
402 '(stroke-linejoin . "round")
403 '(stroke-linecap . "round")
410 `(fill . ,(if fill "currentColor" "none"))
411 `(stroke . "currentColor")
412 `(stroke-width . ,thick)
413 '(stroke-linejoin . "round")
414 '(stroke-linecap . "round")
419 "M~4f ~4fA~4f ~4f 0 ~4f 0 ~4f ~4f"
420 (* start-radius (cos new-start-angle))
421 (- (* start-radius (sin new-start-angle)))
424 (if (> 0 (- new-start-angle new-end-angle)) 0 1)
425 (* end-radius (cos new-end-angle))
426 (- (* end-radius (sin new-end-angle))))
428 (ly:format "L~4f,~4f"
429 (* start-radius (cos new-start-angle))
430 (- (* start-radius (sin new-start-angle))))
433 (define (connected-shape pointlist thick x-scale y-scale connect fill)
436 `(fill . ,(if fill "currentColor" "none"))
437 `(stroke . "currentColor")
438 `(stroke-width . ,thick)
439 '(stroke-linejoin . "round")
440 '(stroke-linecap . "round")
448 (if (eq? (length x) 6)
449 (lambda (x1 x2 x3 x4 x5 x6)
450 (ly:format "C~4f ~4f ~4f ~4f ~4f ~4f"
458 (ly:format "L~4f ~4f"
460 (- (* y-scale x2)))))
463 (if connect "z " "")))))
465 (define (embedded-svg string)
468 (define (embedded-glyph-string font size cid glyphs)
470 (if (= 1 (length glyphs))
471 (set! path (music-string-to-path font size (car glyphs)))
474 (string-append (eo 'g)
477 (music-string-to-path font size x))
481 (set! next-horiz-adv 0.0)
484 (define (woff-glyph-string font-name size cid? w-x-y-named-glyphs)
485 (let* ((name-style (font-name-style font-name))
486 (family-designsize (regexp-exec (make-regexp "(.*)-([0-9]*)")
488 (family (if (regexp-match? family-designsize)
489 (match:substring family-designsize 1)
491 (design-size (if (regexp-match? family-designsize)
492 (match:substring family-designsize 2)
494 (scaled-size (/ size lily-unit-length))
495 (font (ly:paper-get-font paper `(((font-family . ,family)
497 `(design-size . design-size)))))))
498 (define (glyph-spec w x y g)
499 (let* ((charcode (ly:font-glyph-name-to-charcode font g))
500 (char-lookup (format #f "&#~S;" charcode))
501 (glyph-by-name (eoc 'altglyph `(glyphname . ,g)))
503 (comment "XFIXME: how to select glyph by name, altglyph is broken?")))
506 "<text~a font-family=\"~a\" font-size=\"~a\">~a</text>"
507 (if (or (> (abs x) 0.00001)
509 (ly:format " transform=\"translate(~4f,~4f)\"" x y)
511 name-style scaled-size
512 (string-regexp-substitute
514 (string-append glyph-by-name apparently-broken char-lookup)))))
516 (string-join (map (lambda (x) (apply glyph-spec x))
517 (reverse w-x-y-named-glyphs)) "\n")))
520 (if (not (ly:get-option 'svg-woff)) embedded-glyph-string woff-glyph-string))
522 (define (grob-cause offset grob)
525 (define (named-glyph font name)
526 (dispatch `(fontify ,font ,name)))
531 (define (oval x-radius y-radius thick is-filled)
532 (let ((x-max x-radius)
535 (y-min (- y-radius)))
538 '(stroke-linejoin . "round")
539 '(stroke-linecap . "round")
540 `(fill . ,(if is-filled "currentColor" "none"))
541 `(stroke . "currentColor")
542 `(stroke-width . ,thick)
543 `(d . ,(ly:format "M~4f ~4fC~4f ~4f ~4f ~4f ~4f ~4fS~4f ~4f ~4f ~4fz"
551 (define (path thick commands)
552 (define (convert-path-exps exps)
558 (cond ((memq head '(rmoveto rlineto lineto moveto)) 2)
559 ((memq head '(rcurveto curveto)) 6)
560 ((eq? head 'closepath) 0)
562 (args (take rest arity))
563 (svg-head (assoc-get head
573 (cons (format "~a~a" svg-head (number-list->point args))
574 (convert-path-exps (drop rest arity))))
578 `(stroke-width . ,thick)
579 '(stroke-linejoin . "round")
580 '(stroke-linecap . "round")
581 '(stroke . "currentColor")
583 `(d . ,(apply string-append (convert-path-exps commands)))))
585 (define (placebox x y expr)
586 (if (string-null? expr)
589 ((normal-element (regexp-exec svg-element-regexp expr))
590 (scaled-element (regexp-exec scaled-element-regexp expr))
591 (scaled? (if scaled-element #t #f))
592 (match (if scaled? scaled-element normal-element))
593 (string1 (match:substring match 1))
594 (string2 (match:substring match 2)))
597 (string-append string1
598 (ly:format "translate(~4f, ~4f) " x (- y))
601 (string-append string1
602 (ly:format " transform=\"translate(~4f, ~4f)\" "
607 (define (polygon coords blot-diameter is-filled)
610 '(stroke-linejoin . "round")
611 '(stroke-linecap . "round")
612 `(stroke-width . ,blot-diameter)
613 `(fill . ,(if is-filled "currentColor" "none"))
614 '(stroke . "currentColor")
615 `(points . ,(string-join
616 (map offset->point (ly:list->offsets '() coords))))))
618 (define (repeat-slash width slope thickness)
619 (define (euclidean-length x y)
620 (sqrt (+ (* x x) (* y y))))
621 (let* ((x-width (euclidean-length thickness (/ thickness slope)))
622 (height (* width slope)))
625 '(fill . "currentColor")
626 `(d . ,(ly:format "M0 0l~4f 0 ~4f ~4f ~4f 0z"
627 x-width width (- height) (- x-width))))))
632 (define (resetrotation ang x y)
635 (define (round-filled-box breapth width depth height blot-diameter)
638 ;; The stroke will stick out. To use stroke,
639 ;; the stroke-width must be subtracted from all other dimensions.
640 ;;'(stroke-linejoin . "round")
641 ;;'(stroke-linecap . "round")
642 ;;`(stroke-width . ,blot)
648 `(width . ,(+ breapth width))
649 `(height . ,(+ depth height))
650 `(ry . ,(/ blot-diameter 2))
651 '(fill . "currentColor")))
653 (define (setcolor r g b)
654 (format "<g color=\"rgb(~a%, ~a%, ~a%)\">\n"
655 (* 100 r) (* 100 g) (* 100 b)))
657 ;; rotate around given point
658 (define (setrotation ang x y)
659 (ly:format "<g transform=\"rotate(~4f, ~4f, ~4f)\">\n"
662 (define (text font string)
663 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
665 (define (url-link url x y)
667 (eo 'a `(xlink:href . ,url))
671 `(width . ,(- (cdr x) (car x)))
672 `(height . ,(- (cdr y) (car y)))
675 '(stroke-width . "0.0"))
678 (define (utf-8-string pango-font-description string)
679 (dispatch `(fontify ,pango-font-description ,(entity 'tspan string))))