1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2002--2015 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))
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24 ;;; set by framework-gnome.scm
36 (define fancy-format format)
37 (define format ergonomic-simple-format)
39 (define lily-unit-length 1.7573)
42 (define-public (attributes attributes-alist)
48 (set! value (ly:format "~4f" value)))
49 (format #f " ~s=\"~a\"" attr value)))
52 (define-public (eo entity . attributes-alist)
54 (format #f "<~S~a>\n" entity (attributes attributes-alist)))
56 (define-public (eoc entity . attributes-alist)
58 (format #f "<~S~a/>\n" entity (attributes attributes-alist)))
60 (define-public (ec entity)
62 (format #f "</~S>\n" entity))
64 (define (start-enclosing-id-node s)
65 (string-append "<g id=\"" s "\">\n"))
67 (define (end-enclosing-id-node)
70 (define-public (comment s)
71 (string-append "<!-- " s " -->\n"))
73 (define-public (entity entity string . attributes-alist)
74 (if (string-null? string)
75 (apply eoc entity attributes-alist)
77 (apply eo entity attributes-alist) string (ec entity))))
79 (define (offset->point o)
80 (ly:format "~4f ~4f" (car o) (- (cdr o))))
82 (define (number-list->point lst)
86 (cons (ly:format "~4f ~4f" (car lst) (- (cadr lst)))
87 (helper (cddr lst)))))
89 (string-join (helper lst) " "))
92 (define (svg-bezier lst close)
93 (let* ((c0 (car (list-tail lst 3)))
94 (c123 (list-head lst 3)))
96 (if (not close) "M" "L")
98 "C" (string-join (map offset->point c123) " ")
99 (if (not close) "" "z"))))
104 (define (integer->entity integer)
105 (fancy-format "&#x~x;" integer))
107 (define (char->entity char)
108 (integer->entity (char->integer char)))
110 (define (string->entities string)
112 (map char->entity (string->list string))))
114 (define svg-element-regexp
115 (make-regexp "^(<[a-z]+) ?(.*>)"))
117 (define scaled-element-regexp
118 (make-regexp "^(<[a-z]+ transform=\")(scale.[-0-9. ]+,[-0-9. ]+.\" .*>)"))
120 (define pango-description-regexp-comma
121 (make-regexp ",( Bold)?( Italic)?( Small-Caps)?[ -]([0-9.]+)$"))
123 (define pango-description-regexp-nocomma
124 (make-regexp "( Bold)?( Italic)?( Small-Caps)?[ -]([0-9.]+)$"))
126 (define (pango-description-to-text str expr)
128 (define (set-attribute attr val)
129 (set! alist (assoc-set! alist attr val)))
130 (let* ((match-1 (regexp-exec pango-description-regexp-comma str))
131 (match-2 (regexp-exec pango-description-regexp-nocomma str))
132 (match (if match-1 match-1 match-2)))
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 (zero? total-x))
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 (set-attribute 'fill "currentColor")
176 (apply entity 'path "" (reverse alist)))
179 ;; A global variable for keeping track of the *cumulative*
180 ;; horizontal advance for glyph strings, but only if there
181 ;; is more than one glyph.
182 (define next-horiz-adv 0.0)
184 ;; Matches the required "unicode" attribute from <glyph>
185 (define glyph-unicode-value-regexp
186 (make-regexp "unicode=\"([^\"]+)\""))
188 ;; Matches the optional path data from <glyph>
189 (define glyph-path-regexp
190 (make-regexp "d=\"([-MmZzLlHhVvCcSsQqTt0-9.\n ]*)\""))
192 ;; Matches a complete <glyph> element with the glyph-name
193 ;; attribute value of NAME. For example:
195 ;; <glyph glyph-name="period" unicode="." horiz-adv-x="110"
196 ;; d="M0 55c0 30 25 55 55 55s55 -25 55
197 ;; -55s-25 -55 -55 -55s-55 25 -55 55z" />
199 ;; TODO: it would be better to use an XML library to extract
200 ;; the glyphs instead, and store them in a hash table. --pmccarty
202 (define (glyph-element-regexp name)
203 (make-regexp (string-append "<glyph"
204 "(([[:space:]]+[-a-z]+=\"[^\"]*\")+)?"
205 "[[:space:]]+glyph-name=\"("
208 "(([[:space:]]+[-a-z]+=\"[^\"]*\")+)?"
212 (define (extract-glyph all-glyphs name size . rest)
213 (let* ((new-name (regexp-quote name))
214 (regexp (regexp-exec (glyph-element-regexp new-name) all-glyphs))
215 (glyph (match:substring regexp))
216 (unicode-attr (regexp-exec glyph-unicode-value-regexp glyph))
217 (unicode-attr-value (match:substring unicode-attr 1))
218 (unicode-attr? (regexp-match? unicode-attr))
219 (d-attr (regexp-exec glyph-path-regexp glyph))
221 (d-attr? (regexp-match? d-attr))
222 ;; TODO: not urgent, but do not hardcode this value
224 (font-scale (ly:format "~4f" (/ size units-per-em)))
227 (if (and unicode-attr? (not unicode-attr-value))
228 (ly:warning (_ "Glyph must have a unicode value")))
230 (if d-attr? (set! d-attr-value (match:substring d-attr 1)))
233 ;; Glyph-strings with path data
234 (and d-attr? (not (null? rest)))
236 (set! path (apply dump-path d-attr-value
238 (list (caddr rest) (cadddr rest))))
239 (set! next-horiz-adv (+ next-horiz-adv
242 ;; Glyph-strings without path data ("space")
243 ((and (not d-attr?) (not (null? rest)))
245 (set! next-horiz-adv (+ next-horiz-adv
248 ;; Font smobs with path data
249 ((and d-attr? (null? rest))
250 (set! path (dump-path d-attr-value font-scale))
252 ;; Font smobs without path data ("space")
256 (define (extract-glyph-info all-glyphs glyph size)
257 (let* ((offsets (list-head glyph 4))
258 (glyph-name (car (reverse glyph))))
259 (apply extract-glyph all-glyphs glyph-name size offsets)))
261 (define (svg-defs svg-font)
262 (let ((start (string-contains svg-font "<defs>"))
263 (end (string-contains svg-font "</defs>")))
264 (substring svg-font (+ start 7) (- end 1))))
266 (define (cache-font svg-font size glyph)
267 (let ((all-glyphs (svg-defs (cached-file-contents svg-font))))
269 (extract-glyph-info all-glyphs glyph size)
270 (extract-glyph all-glyphs glyph size))))
273 (define (music-string-to-path font size glyph)
274 (let* ((name-style (font-name-style font))
275 (scaled-size (/ size lily-unit-length))
276 (font-file (ly:find-file (string-append name-style ".svg"))))
279 (cache-font font-file scaled-size glyph)
280 (ly:warning (_ "cannot find SVG font ~S") font-file))))
283 (define (font-smob-to-path font glyph)
284 (let* ((name-style (font-name-style font))
285 (scaled-size (modified-font-metric-font-scaling font))
286 (font-file (ly:find-file (string-append name-style ".svg"))))
289 (cache-font font-file scaled-size glyph)
290 (ly:warning (_ "cannot find SVG font ~S") font-file))))
292 (define (woff-font-smob-to-text font expr)
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 ".woff")))
296 (charcode (ly:font-glyph-name-to-charcode font expr))
297 (char-lookup (format #f "&#~S;" charcode))
298 (glyph-by-name (eoc 'altglyph `(glyphname . ,expr)))
300 (comment "FIXME: how to select glyph by name, altglyph is broken?"))
301 (text (string-regexp-substitute "\n" ""
302 (string-append glyph-by-name apparently-broken char-lookup))))
304 (define (set-attribute attr val)
305 (set! alist (assoc-set! alist attr val)))
306 (set-attribute 'font-family name-style)
307 (set-attribute 'font-size scaled-size)
308 (apply entity 'text text (reverse! alist))))
310 (define font-smob-to-text
311 (if (not (ly:get-option 'svg-woff))
312 font-smob-to-path woff-font-smob-to-text))
314 (define (fontify font expr)
316 (pango-description-to-text font expr)
317 (font-smob-to-text font expr)))
319 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
320 ;;; stencil outputters
323 (define (char font i)
324 (fontify font (entity 'tspan (char->entity (integer->char i)))))
326 (define (circle radius thick is-filled)
329 '(stroke-linejoin . "round")
330 '(stroke-linecap . "round")
331 `(fill . ,(if is-filled "currentColor" "none"))
332 `(stroke . "currentColor")
333 `(stroke-width . ,thick)
336 (define (dashed-line thick on off dx dy phase)
337 (draw-line thick 0 0 dx dy
338 `(stroke-dasharray . ,(format #f "~a,~a" on off))))
340 (define (draw-line thick x1 y1 x2 y2 . alist)
341 (apply entity 'line ""
343 `((stroke-linejoin . "round")
344 (stroke-linecap . "round")
345 (stroke-width . ,thick)
346 (stroke . "currentColor")
353 (define (ellipse x-radius y-radius thick is-filled)
356 '(stroke-linejoin . "round")
357 '(stroke-linecap . "round")
358 `(fill . ,(if is-filled "currentColor" "none"))
359 `(stroke . "currentColor")
360 `(stroke-width . ,thick)
364 (define (partial-ellipse x-radius y-radius start-angle end-angle thick connect fill)
365 (define (make-ellipse-radius x-radius y-radius angle)
366 (/ (* x-radius y-radius)
367 (sqrt (+ (* (* y-radius y-radius)
368 (* (cos angle) (cos angle)))
369 (* (* x-radius x-radius)
370 (* (sin angle) (sin angle)))))))
372 ((new-start-angle (* PI-OVER-180 (angle-0-360 start-angle)))
373 (start-radius (make-ellipse-radius x-radius y-radius new-start-angle))
374 (new-end-angle (* PI-OVER-180 (angle-0-360 end-angle)))
375 (end-radius (make-ellipse-radius x-radius y-radius new-end-angle))
377 (x-end (- (* end-radius (cos new-end-angle))
378 (* start-radius (cos new-start-angle))))
379 (y-end (- (* end-radius (sin new-end-angle))
380 (* start-radius (sin new-start-angle)))))
381 (if (and (< (abs x-end) epsilon) (< (abs y-end) epsilon))
384 `(fill . ,(if fill "currentColor" "none"))
385 `(stroke . "currentColor")
386 `(stroke-width . ,thick)
387 '(stroke-linejoin . "round")
388 '(stroke-linecap . "round")
395 `(fill . ,(if fill "currentColor" "none"))
396 `(stroke . "currentColor")
397 `(stroke-width . ,thick)
398 '(stroke-linejoin . "round")
399 '(stroke-linecap . "round")
404 "M~4f ~4fA~4f ~4f 0 ~4f 0 ~4f ~4f"
405 (* start-radius (cos new-start-angle))
406 (- (* start-radius (sin new-start-angle)))
409 (if (> 0 (- new-start-angle new-end-angle)) 0 1)
410 (* end-radius (cos new-end-angle))
411 (- (* end-radius (sin new-end-angle))))
413 (ly:format "L~4f,~4f"
414 (* start-radius (cos new-start-angle))
415 (- (* start-radius (sin new-start-angle))))
418 (define (embedded-svg string)
421 (define (embedded-glyph-string pango-font font size cid glyphs)
423 (if (= 1 (length glyphs))
424 (set! path (music-string-to-path font size (car glyphs)))
427 (string-append (eo 'g)
430 (music-string-to-path font size x))
434 (set! next-horiz-adv 0.0)
437 (define (woff-glyph-string pango-font font-name size cid? w-h-x-y-named-glyphs)
438 (let* ((name-style (font-name-style font-name))
439 (family-designsize (regexp-exec (make-regexp "(.*)-([0-9]*)")
441 (family (if (regexp-match? family-designsize)
442 (match:substring family-designsize 1)
444 (design-size (if (regexp-match? family-designsize)
445 (match:substring family-designsize 2)
447 (scaled-size (/ size lily-unit-length))
448 (font (ly:paper-get-font paper `(((font-family . ,family)
450 `(design-size . design-size)))))))
451 (define (glyph-spec w h x y g) ; h not used
452 (let* ((charcode (ly:font-glyph-name-to-charcode font g))
453 (char-lookup (format #f "&#~S;" charcode))
454 (glyph-by-name (eoc 'altglyph `(glyphname . ,g)))
456 (comment "XFIXME: how to select glyph by name, altglyph is broken?")))
459 "<text~a font-family=\"~a\" font-size=\"~a\">~a</text>"
460 (if (or (> (abs x) 0.00001)
462 (ly:format " transform=\"translate(~4f,~4f)\"" x y)
464 name-style scaled-size
465 (string-regexp-substitute
467 (string-append glyph-by-name apparently-broken char-lookup)))))
469 (string-join (map (lambda (x) (apply glyph-spec x))
470 (reverse w-h-x-y-named-glyphs)) "\n")))
473 (if (not (ly:get-option 'svg-woff)) embedded-glyph-string woff-glyph-string))
475 (define (grob-cause offset grob)
476 (and (ly:get-option 'point-and-click)
477 (let* ((cause (ly:grob-property grob 'cause))
478 (music-origin (if (ly:stream-event? cause)
479 (ly:event-property cause 'origin)))
480 (point-and-click (ly:get-option 'point-and-click)))
481 (and (ly:input-location? music-origin)
482 (cond ((boolean? point-and-click) point-and-click)
483 ((symbol? point-and-click)
484 (ly:in-event-class? cause point-and-click))
485 (else (any (lambda (t)
486 (ly:in-event-class? cause t))
488 (let* ((location (ly:input-file-line-char-column music-origin)))
490 (ly:format "<a style=\"color:inherit;\" xlink:href=\"textedit://~a:~a:~a:~a\">\n"
491 ;; Backslashes are not valid
492 ;; file URI path separators.
493 (ly:string-percent-encode
494 (ly:string-substitute "\\" "/" (car location)))
498 (1+ (cadddr location))))))))
500 (define (named-glyph font name)
503 (define (no-origin) "</a>\n")
505 (define* (path thick commands #:optional (cap 'round) (join 'round) (fill? #f))
506 (define (convert-path-exps exps)
512 (cond ((memq head '(rmoveto rlineto lineto moveto)) 2)
513 ((memq head '(rcurveto curveto)) 6)
514 ((eq? head 'closepath) 0)
516 (args (take rest arity))
517 (svg-head (assoc-get head
527 (cons (format #f "~a~a" svg-head (number-list->point args))
528 (convert-path-exps (drop rest arity))))
531 (let* ((line-cap-styles '(butt round square))
532 (line-join-styles '(miter round bevel))
533 (cap-style (if (not (memv cap line-cap-styles))
535 (ly:warning (_ "unknown line-cap-style: ~S")
536 (symbol->string cap))
539 (join-style (if (not (memv join line-join-styles))
541 (ly:warning (_ "unknown line-join-style: ~S")
542 (symbol->string join))
546 `(stroke-width . ,thick)
547 `(stroke-linejoin . ,(symbol->string join-style))
548 `(stroke-linecap . ,(symbol->string cap-style))
549 '(stroke . "currentColor")
550 `(fill . ,(if fill? "currentColor" "none"))
551 `(d . ,(string-concatenate (convert-path-exps commands))))))
553 (define (placebox x y expr)
554 (if (string-null? expr)
557 ((normal-element (regexp-exec svg-element-regexp expr))
558 (scaled-element (regexp-exec scaled-element-regexp expr))
559 (scaled? (if scaled-element #t #f))
560 (match (if scaled? scaled-element normal-element))
561 (string1 (match:substring match 1))
562 (string2 (match:substring match 2)))
565 (string-append string1
566 (ly:format "translate(~4f, ~4f) " x (- y))
569 (string-append string1
570 (ly:format " transform=\"translate(~4f, ~4f)\" "
575 (define (polygon coords blot-diameter is-filled)
578 '(stroke-linejoin . "round")
579 '(stroke-linecap . "round")
580 `(stroke-width . ,blot-diameter)
581 `(fill . ,(if is-filled "currentColor" "none"))
582 '(stroke . "currentColor")
583 `(points . ,(string-join
584 (map offset->point (ly:list->offsets '() coords))))))
589 (define (resetrotation ang x y)
595 (define (round-filled-box breapth width depth height blot-diameter)
598 ;; The stroke will stick out. To use stroke,
599 ;; the stroke-width must be subtracted from all other dimensions.
600 ;;'(stroke-linejoin . "round")
601 ;;'(stroke-linecap . "round")
602 ;;`(stroke-width . ,blot)
608 `(width . ,(+ breapth width))
609 `(height . ,(+ depth height))
610 `(ry . ,(/ blot-diameter 2))
611 '(fill . "currentColor")))
613 (define (setcolor r g b)
614 (ly:format "<g color=\"rgb(~4f%, ~4f%, ~4f%)\">\n"
615 (* 100 r) (* 100 g) (* 100 b)))
617 ;; rotate around given point
618 (define (setrotation ang x y)
619 (ly:format "<g transform=\"rotate(~4f, ~4f, ~4f)\">\n"
622 (define (setscale x y)
623 (ly:format "<g transform=\"scale(~4f, ~4f)\">\n"
626 (define (text font string)
627 (fontify font (entity 'tspan (string->entities string))))
629 (define (url-link url x y)
631 (eo 'a `(xlink:href . ,url))
635 `(width . ,(- (cdr x) (car x)))
636 `(height . ,(- (cdr y) (car y)))
639 '(stroke-width . "0.0"))
642 (define (utf-8-string pango-font-description string)
643 (let ((escaped-string (string-regexp-substitute
645 (string-regexp-substitute "&" "&" string))))
646 (fontify pango-font-description
647 (entity 'tspan escaped-string))))