1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2002--2011 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 " ~s=\"~a\"" attr value)))
61 (define-public (eo entity . attributes-alist)
63 (format "<~S~a>\n" entity (attributes attributes-alist)))
65 (define-public (eoc entity . attributes-alist)
67 (format "<~S~a/>\n" entity (attributes attributes-alist)))
69 (define-public (ec entity)
71 (format "</~S>\n" entity))
73 (define-public (comment s)
74 (string-append "<!-- " s " -->\n"))
76 (define-public (entity entity string . attributes-alist)
77 (if (equal? string "")
78 (apply eoc entity attributes-alist)
80 (apply eo (cons entity attributes-alist)) string (ec entity))))
82 (define (offset->point o)
83 (ly:format "~4f ~4f" (car o) (- (cdr o))))
85 (define (number-list->point lst)
89 (cons (format "~S ~S" (car lst) (- (cadr lst)))
90 (helper (cddr lst)))))
92 (string-join (helper lst) " "))
95 (define (svg-bezier lst close)
96 (let* ((c0 (car (list-tail lst 3)))
97 (c123 (list-head lst 3)))
99 (if (not close) "M" "L")
101 "C" (string-join (map offset->point c123) " ")
102 (if (not close) "" "z"))))
107 (define (integer->entity integer)
108 (fancy-format "&#x~x;" integer))
110 (define (char->entity char)
111 (integer->entity (char->integer char)))
113 (define (string->entities string)
115 (map (lambda (x) (char->entity x)) (string->list string))))
117 (define svg-element-regexp
118 (make-regexp "^(<[a-z]+) ?(.*>)"))
120 (define scaled-element-regexp
121 (make-regexp "^(<[a-z]+ transform=\")(scale.[-0-9. ]+,[-0-9. ]+.\" .*>)"))
123 (define pango-description-regexp-comma
124 (make-regexp ",( Bold)?( Italic)?( Small-Caps)?[ -]([0-9.]+)$"))
126 (define pango-description-regexp-nocomma
127 (make-regexp "( Bold)?( Italic)?( Small-Caps)?[ -]([0-9.]+)$"))
129 (define (pango-description-to-text str expr)
131 (define (set-attribute attr val)
132 (set! alist (assoc-set! alist attr val)))
133 (let* ((match-1 (regexp-exec pango-description-regexp-comma str))
134 (match-2 (regexp-exec pango-description-regexp-nocomma str))
135 (match (if match-1 match-1 match-2)))
137 (if (regexp-match? match)
139 (set-attribute 'font-family (match:prefix match))
140 (if (string? (match:substring match 1))
141 (set-attribute 'font-weight "bold"))
142 (if (string? (match:substring match 2))
143 (set-attribute 'font-style "italic"))
144 (if (string? (match:substring match 3))
145 (set-attribute 'font-variant "small-caps"))
146 (set-attribute 'font-size
147 (/ (string->number (match:substring match 4))
149 (set-attribute 'text-anchor "start")
150 (set-attribute 'fill "currentColor"))
151 (ly:warning (_ "cannot decypher Pango description: ~a") str))
153 (apply entity 'text expr (reverse! alist))))
155 (define (dump-path path scale . rest)
157 (define (set-attribute attr val)
158 (set! alist (assoc-set! alist attr val)))
159 (if (not (null? rest))
160 (let* ((dx (car rest))
162 (total-x (+ dx next-horiz-adv)))
163 (if (or (not (zero? total-x))
165 (let ((x (ly:format "~4f" total-x))
166 (y (ly:format "~4f" dy)))
167 (set-attribute 'transform
169 "translate(" x ", " y ") "
170 "scale(" scale ", -" scale ")")))
171 (set-attribute 'transform
173 "scale(" scale ", -" scale ")"))))
174 (set-attribute 'transform (string-append
175 "scale(" scale ", -" scale ")")))
177 (set-attribute 'd path)
178 (set-attribute 'fill "currentColor")
179 (apply entity 'path "" (reverse alist)))
182 ;; A global variable for keeping track of the *cumulative*
183 ;; horizontal advance for glyph strings, but only if there
184 ;; is more than one glyph.
185 (define next-horiz-adv 0.0)
187 ;; Matches the required "unicode" attribute from <glyph>
188 (define glyph-unicode-value-regexp
189 (make-regexp "unicode=\"([^\"]+)\""))
191 ;; Matches the optional path data from <glyph>
192 (define glyph-path-regexp
193 (make-regexp "d=\"([-MmZzLlHhVvCcSsQqTt0-9.\n ]*)\""))
195 ;; Matches a complete <glyph> element with the glyph-name
196 ;; attribute value of NAME. For example:
198 ;; <glyph glyph-name="period" unicode="." horiz-adv-x="110"
199 ;; d="M0 55c0 30 25 55 55 55s55 -25 55
200 ;; -55s-25 -55 -55 -55s-55 25 -55 55z" />
202 ;; TODO: it would be better to use an XML library to extract
203 ;; the glyphs instead, and store them in a hash table. --pmccarty
205 (define (glyph-element-regexp name)
206 (make-regexp (string-append "<glyph"
207 "(([[:space:]]+[-a-z]+=\"[^\"]*\")+)?"
208 "[[:space:]]+glyph-name=\"("
211 "(([[:space:]]+[-a-z]+=\"[^\"]*\")+)?"
215 (define (extract-glyph all-glyphs name size . rest)
216 (let* ((new-name (regexp-quote name))
217 (regexp (regexp-exec (glyph-element-regexp new-name) all-glyphs))
218 (glyph (match:substring regexp))
219 (unicode-attr (regexp-exec glyph-unicode-value-regexp glyph))
220 (unicode-attr-value (match:substring unicode-attr 1))
221 (unicode-attr? (regexp-match? unicode-attr))
222 (d-attr (regexp-exec glyph-path-regexp glyph))
224 (d-attr? (regexp-match? d-attr))
225 ;; TODO: not urgent, but do not hardcode this value
227 (font-scale (ly:format "~4f" (/ size units-per-em)))
230 (if (and unicode-attr? (not unicode-attr-value))
231 (ly:warning (_ "Glyph must have a unicode value")))
233 (if d-attr? (set! d-attr-value (match:substring d-attr 1)))
236 ;; Glyph-strings with path data
237 (and d-attr? (not (null? rest)))
239 (set! path (apply dump-path d-attr-value
241 (list (cadr rest) (caddr rest))))
242 (set! next-horiz-adv (+ next-horiz-adv
245 ;; Glyph-strings without path data ("space")
246 ((and (not d-attr?) (not (null? rest)))
248 (set! next-horiz-adv (+ next-horiz-adv
251 ;; Font smobs with path data
252 ((and d-attr? (null? rest))
253 (set! path (dump-path d-attr-value font-scale))
255 ;; Font smobs without path data ("space")
259 (define (extract-glyph-info all-glyphs glyph size)
260 (let* ((offsets (list-head glyph 3))
261 (glyph-name (car (reverse glyph))))
262 (apply extract-glyph all-glyphs glyph-name size offsets)))
264 (define (svg-defs svg-font)
265 (let ((start (string-contains svg-font "<defs>"))
266 (end (string-contains svg-font "</defs>")))
267 (substring svg-font (+ start 7) (- end 1))))
269 (define (cache-font svg-font size glyph)
270 (let ((all-glyphs (svg-defs (cached-file-contents svg-font))))
272 (extract-glyph-info all-glyphs glyph size)
273 (extract-glyph all-glyphs glyph size))))
276 (define (music-string-to-path font size glyph)
277 (let* ((name-style (font-name-style font))
278 (scaled-size (/ size lily-unit-length))
279 (font-file (ly:find-file (string-append name-style ".svg"))))
282 (cache-font font-file scaled-size glyph)
283 (ly:warning (_ "cannot find SVG font ~S") font-file))))
286 (define (font-smob-to-path font glyph)
287 (let* ((name-style (font-name-style font))
288 (scaled-size (modified-font-metric-font-scaling font))
289 (font-file (ly:find-file (string-append name-style ".svg"))))
292 (cache-font font-file scaled-size glyph)
293 (ly:warning (_ "cannot find SVG font ~S") font-file))))
295 (define (woff-font-smob-to-text font expr)
296 (let* ((name-style (font-name-style font))
297 (scaled-size (modified-font-metric-font-scaling font))
298 (font-file (ly:find-file (string-append name-style ".woff")))
299 (charcode (ly:font-glyph-name-to-charcode font expr))
300 (char-lookup (format #f "&#~S;" charcode))
301 (glyph-by-name (eoc 'altglyph `(glyphname . ,expr)))
303 (comment "FIXME: how to select glyph by name, altglyph is broken?"))
304 (text (string-regexp-substitute "\n" ""
305 (string-append glyph-by-name apparently-broken char-lookup))))
307 (define (set-attribute attr val)
308 (set! alist (assoc-set! alist attr val)))
309 (set-attribute 'font-family name-style)
310 (set-attribute 'font-size scaled-size)
311 (apply entity 'text text (reverse! alist))))
313 (define font-smob-to-text
314 (if (not (ly:get-option 'svg-woff))
315 font-smob-to-path woff-font-smob-to-text))
317 (define (fontify font expr)
319 (pango-description-to-text font expr)
320 (font-smob-to-text font expr)))
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
323 ;;; stencil outputters
326 (define (bezier-sandwich lst thick)
327 (let* ((first (list-tail lst 4))
328 (second (list-head lst 4)))
330 '(stroke-linejoin . "round")
331 '(stroke-linecap . "round")
332 '(stroke . "currentColor")
333 '(fill . "currentColor")
334 `(stroke-width . ,thick)
335 `(d . ,(string-append (svg-bezier first #f)
336 (svg-bezier second #t))))))
338 (define (char font i)
340 `(fontify ,font ,(entity 'tspan (char->entity (integer->char i))))))
342 (define (circle radius thick is-filled)
345 '(stroke-linejoin . "round")
346 '(stroke-linecap . "round")
347 `(fill . ,(if is-filled "currentColor" "none"))
348 `(stroke . "currentColor")
349 `(stroke-width . ,thick)
352 (define (dashed-line thick on off dx dy phase)
353 (draw-line thick 0 0 dx dy
354 `(stroke-dasharray . ,(format "~a,~a" on off))))
356 (define (draw-line thick x1 y1 x2 y2 . alist)
357 (apply entity 'line ""
359 `((stroke-linejoin . "round")
360 (stroke-linecap . "round")
361 (stroke-width . ,thick)
362 (stroke . "currentColor")
369 (define (ellipse x-radius y-radius thick is-filled)
372 '(stroke-linejoin . "round")
373 '(stroke-linecap . "round")
374 `(fill . ,(if is-filled "currentColor" "none"))
375 `(stroke . "currentColor")
376 `(stroke-width . ,thick)
380 (define (partial-ellipse x-radius y-radius start-angle end-angle thick connect fill)
381 (define (make-ellipse-radius x-radius y-radius angle)
382 (/ (* x-radius y-radius)
383 (sqrt (+ (* (* y-radius y-radius)
384 (* (cos angle) (cos angle)))
385 (* (* x-radius x-radius)
386 (* (sin angle) (sin 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 (if (and (< (abs x-end) epsilon) (< (abs y-end) epsilon))
400 `(fill . ,(if fill "currentColor" "none"))
401 `(stroke . "currentColor")
402 `(stroke-width . ,thick)
403 '(stroke-linejoin . "round")
404 '(stroke-linecap . "round")
411 `(fill . ,(if fill "currentColor" "none"))
412 `(stroke . "currentColor")
413 `(stroke-width . ,thick)
414 '(stroke-linejoin . "round")
415 '(stroke-linecap . "round")
420 "M~4f ~4fA~4f ~4f 0 ~4f 0 ~4f ~4f"
421 (* start-radius (cos new-start-angle))
422 (- (* start-radius (sin new-start-angle)))
425 (if (> 0 (- new-start-angle new-end-angle)) 0 1)
426 (* end-radius (cos new-end-angle))
427 (- (* end-radius (sin new-end-angle))))
429 (ly:format "L~4f,~4f"
430 (* start-radius (cos new-start-angle))
431 (- (* start-radius (sin new-start-angle))))
434 (define (embedded-svg string)
437 (define (embedded-glyph-string font size cid glyphs)
439 (if (= 1 (length glyphs))
440 (set! path (music-string-to-path font size (car glyphs)))
443 (string-append (eo 'g)
446 (music-string-to-path font size x))
450 (set! next-horiz-adv 0.0)
453 (define (woff-glyph-string font-name size cid? w-x-y-named-glyphs)
454 (let* ((name-style (font-name-style font-name))
455 (family-designsize (regexp-exec (make-regexp "(.*)-([0-9]*)")
457 (family (if (regexp-match? family-designsize)
458 (match:substring family-designsize 1)
460 (design-size (if (regexp-match? family-designsize)
461 (match:substring family-designsize 2)
463 (scaled-size (/ size lily-unit-length))
464 (font (ly:paper-get-font paper `(((font-family . ,family)
466 `(design-size . design-size)))))))
467 (define (glyph-spec w x y g)
468 (let* ((charcode (ly:font-glyph-name-to-charcode font g))
469 (char-lookup (format #f "&#~S;" charcode))
470 (glyph-by-name (eoc 'altglyph `(glyphname . ,g)))
472 (comment "XFIXME: how to select glyph by name, altglyph is broken?")))
475 "<text~a font-family=\"~a\" font-size=\"~a\">~a</text>"
476 (if (or (> (abs x) 0.00001)
478 (ly:format " transform=\"translate(~4f,~4f)\"" x y)
480 name-style scaled-size
481 (string-regexp-substitute
483 (string-append glyph-by-name apparently-broken char-lookup)))))
485 (string-join (map (lambda (x) (apply glyph-spec x))
486 (reverse w-x-y-named-glyphs)) "\n")))
489 (if (not (ly:get-option 'svg-woff)) embedded-glyph-string woff-glyph-string))
491 (define (grob-cause offset grob)
494 (define (named-glyph font name)
495 (dispatch `(fontify ,font ,name)))
500 (define (oval x-radius y-radius thick is-filled)
501 (let ((x-max x-radius)
504 (y-min (- y-radius)))
507 '(stroke-linejoin . "round")
508 '(stroke-linecap . "round")
509 `(fill . ,(if is-filled "currentColor" "none"))
510 `(stroke . "currentColor")
511 `(stroke-width . ,thick)
512 `(d . ,(ly:format "M~4f ~4fC~4f ~4f ~4f ~4f ~4f ~4fS~4f ~4f ~4f ~4fz"
520 (define* (path thick commands #:optional (cap 'round) (join 'round) (fill? #f))
521 (define (convert-path-exps exps)
527 (cond ((memq head '(rmoveto rlineto lineto moveto)) 2)
528 ((memq head '(rcurveto curveto)) 6)
529 ((eq? head 'closepath) 0)
531 (args (take rest arity))
532 (svg-head (assoc-get head
542 (cons (format "~a~a" svg-head (number-list->point args))
543 (convert-path-exps (drop rest arity))))
546 (let* ((line-cap-styles '(butt round square))
547 (line-join-styles '(miter round bevel))
548 (cap-style (if (not (memv cap line-cap-styles))
550 (ly:warning (_ "unknown line-cap-style: ~S")
551 (symbol->string cap))
554 (join-style (if (not (memv join line-join-styles))
556 (ly:warning (_ "unknown line-join-style: ~S")
557 (symbol->string join))
561 `(stroke-width . ,thick)
562 `(stroke-linejoin . ,(symbol->string join-style))
563 `(stroke-linecap . ,(symbol->string cap-style))
564 '(stroke . "currentColor")
565 `(fill . ,(if fill? "currentColor" "none"))
566 `(d . ,(apply string-append (convert-path-exps commands))))))
568 (define (placebox x y expr)
569 (if (string-null? expr)
572 ((normal-element (regexp-exec svg-element-regexp expr))
573 (scaled-element (regexp-exec scaled-element-regexp expr))
574 (scaled? (if scaled-element #t #f))
575 (match (if scaled? scaled-element normal-element))
576 (string1 (match:substring match 1))
577 (string2 (match:substring match 2)))
580 (string-append string1
581 (ly:format "translate(~4f, ~4f) " x (- y))
584 (string-append string1
585 (ly:format " transform=\"translate(~4f, ~4f)\" "
590 (define (polygon coords blot-diameter is-filled)
593 '(stroke-linejoin . "round")
594 '(stroke-linecap . "round")
595 `(stroke-width . ,blot-diameter)
596 `(fill . ,(if is-filled "currentColor" "none"))
597 '(stroke . "currentColor")
598 `(points . ,(string-join
599 (map offset->point (ly:list->offsets '() coords))))))
601 (define (repeat-slash width slope thickness)
602 (define (euclidean-length x y)
603 (sqrt (+ (* x x) (* y y))))
604 (let* ((x-width (euclidean-length thickness (/ thickness slope)))
605 (height (* width slope)))
608 '(fill . "currentColor")
609 `(d . ,(ly:format "M0 0l~4f 0 ~4f ~4f ~4f 0z"
610 x-width width (- height) (- x-width))))))
615 (define (resetrotation ang x y)
621 (define (round-filled-box breapth width depth height blot-diameter)
624 ;; The stroke will stick out. To use stroke,
625 ;; the stroke-width must be subtracted from all other dimensions.
626 ;;'(stroke-linejoin . "round")
627 ;;'(stroke-linecap . "round")
628 ;;`(stroke-width . ,blot)
634 `(width . ,(+ breapth width))
635 `(height . ,(+ depth height))
636 `(ry . ,(/ blot-diameter 2))
637 '(fill . "currentColor")))
639 (define (setcolor r g b)
640 (format "<g color=\"rgb(~a%, ~a%, ~a%)\">\n"
641 (* 100 r) (* 100 g) (* 100 b)))
643 ;; rotate around given point
644 (define (setrotation ang x y)
645 (ly:format "<g transform=\"rotate(~4f, ~4f, ~4f)\">\n"
648 (define (setscale x y)
649 (ly:format "<g transform=\"scale(~4f, ~4f)\">\n"
652 (define (text font string)
653 (dispatch `(fontify ,font ,(entity 'tspan (string->entities string)))))
655 (define (url-link url x y)
657 (eo 'a `(xlink:href . ,url))
661 `(width . ,(- (cdr x) (car x)))
662 `(height . ,(- (cdr y) (car y)))
665 '(stroke-width . "0.0"))
668 (define (utf-8-string pango-font-description string)
669 (let ((escaped-string (string-regexp-substitute
671 (string-regexp-substitute "&" "&" string))))
672 (dispatch `(fontify ,pango-font-description
673 ,(entity 'tspan escaped-string)))))