2 ;; framework-svg.scm -- structure for SVG output
4 ;;;; source file of the GNU LilyPond music typesetter
6 ;;;; (c) 2004--2007 Jan Nieuwenhuizen <janneke@gnu.org>
8 (define-module (scm framework-svg))
15 (use-modules (srfi srfi-1)
20 (define format ergonomic-simple-format)
22 (define-public (output-framework basename book scopes fields)
23 (let* ((filename (format "~a.svg" basename))
24 (outputter (ly:make-paper-outputter (open-file filename "wb")
25 (ly:get-option 'backend)))
26 (dump (lambda (str) (display str (ly:outputter-port outputter))))
27 (paper (ly:paper-book-paper book))
28 (unit-length (ly:output-def-lookup paper 'output-scale))
29 (output-scale (* lily-unit->mm-factor
31 (page-stencils (map page-stencil (ly:paper-book-pages book)))
32 (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t))
33 (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
34 (page-count (length page-stencils))
35 (paper-width (ly:output-def-lookup paper 'paper-width))
36 (paper-height (ly:output-def-lookup paper 'paper-height))
37 (page-width (inexact->exact (ceiling (* output-scale paper-width))))
38 (page-height (inexact->exact (ceiling (* output-scale paper-height))))
39 (page-set? (or (> page-count 1) landscape?)))
41 (ly:outputter-output-scheme outputter
42 `(begin (set! lily-unit-length ,unit-length) ""))
44 '(xmlns . "http://www.w3.org/2000/svg")
45 '(xmlns:xlink . "http://www.w3.org/1999/xlink")
48 ;; Argggghhhh: SVG takes the px <-> mm mapping from the windowing system
49 `(width . ,(format "~s" page-width))
50 `(height . ,(format "~s" page-height))))
52 (dump (dump-fonts outputter paper))
55 ;; FIXME: only use pages if there are more than one, pageSet is
56 ;; not supported by all SVG applications yet.
57 (if page-set? (eo 'pageSet) "")
58 (eo 'g `(transform . ,(format "scale(~a, ~a) "
59 output-scale output-scale)))))
63 (set! page-number (1+ page-number))
64 (dump-page outputter page page-number page-count landscape? page-set?))
67 (if page-set? (eo 'pageSet) "")
71 (if page-set? (ec 'pageSet) "")
74 (ly:outputter-close outputter)
77 (define (dump-page outputter page page-number page-count landscape? page-set?)
78 (define (dump str) (display str (ly:outputter-port outputter)))
80 (dump (comment (format "Page: ~S/~S" page-number page-count)))
81 (if (or landscape? page-set?)
84 (eo 'page '(page-orientation . "270"))
87 (dump (string-append (eo 'g )))
88 (ly:outputter-dump-stencil outputter page)
89 (dump (string-append (ec 'g)))
90 (if (or landscape? page-set?)
93 (define (embed-font string)
94 (let ((start (string-contains string "<defs>"))
95 (end (string-contains string "</defs>")))
96 (substring string (+ start 7) (- end 1))))
98 (define (dump-fonts outputter paper)
99 (let* ((fonts (ly:paper-fonts paper))
100 (font-names (uniq-list (sort
102 (map ly:font-file-name fonts)) string<?)))
105 (let ((file-name (ly:find-file (string-append x ".svg"))))
106 (if file-name (embed-font (cached-file-contents file-name))
107 (begin (ly:warning "cannot find SVG font ~S" x) ""))))
108 (filter string? font-names))))
109 (entity 'defs (string-join svgs "\n"))))