2 ;; framework-svg.scm -- structure for SVG output
4 ;;;; source file of the GNU LilyPond music typesetter
6 ;;;; (c) 2004--2006 Jan Nieuwenhuizen <janneke@gnu.org>
8 (define-module (scm framework-svg))
15 (use-modules (srfi srfi-1)
20 (define-public (output-framework basename book scopes fields)
21 (let* ((filename (format "~a.svg" basename))
22 (outputter (ly:make-paper-outputter (open-file filename "wb")
24 (dump (lambda (str) (display str (ly:outputter-port outputter))))
25 (paper (ly:paper-book-paper book))
26 (unit-length (ly:output-def-lookup paper 'output-scale))
27 (output-scale (* lily-unit->mm-factor
29 (page-stencils (map page-stencil (ly:paper-book-pages book)))
30 (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t))
31 (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
32 (page-count (length page-stencils))
33 (paper-width (ly:output-def-lookup paper 'paper-width))
34 (paper-height (ly:output-def-lookup paper 'paper-height))
35 (page-width (inexact->exact (ceiling (* output-scale paper-width))))
36 (page-height (inexact->exact (ceiling (* output-scale paper-height))))
37 (page-set? (or (> page-count 1) landscape?)))
39 (ly:outputter-output-scheme outputter
40 `(begin (set! lily-unit-length ,unit-length) ""))
42 '(xmlns . "http://www.w3.org/2000/svg")
43 '(xmlns:xlink . "http://www.w3.org/1999/xlink")
46 ;; Argggghhhh: SVG takes the px <-> mm mapping from the windowing system
47 `(width . ,(format #f "~s" page-width))
48 `(height . ,(format #f "~s" page-height))))
50 (dump (dump-fonts outputter paper))
53 ;; FIXME: only use pages if there are more than one, pageSet is
54 ;; not supported by all SVG applications yet.
55 (if page-set? (eo 'pageSet) "")
56 (eo 'g `(transform . ,(format "scale(~a, ~a) "
57 output-scale output-scale)))))
61 (set! page-number (1+ page-number))
62 (dump-page outputter page page-number page-count landscape? page-set?))
65 (if page-set? (eo 'pageSet) "")
69 (if page-set? (ec 'pageSet) "")
72 (define (dump-page outputter page page-number page-count landscape? page-set?)
73 (define (dump str) (display str (ly:outputter-port outputter)))
75 (dump (comment (format #f "Page: ~S/~S" page-number page-count)))
76 (if (or landscape? page-set?)
79 (eo 'page '(page-orientation . "270"))
82 (dump (string-append (eo 'g )))
83 (ly:outputter-dump-stencil outputter page)
84 (dump (string-append (ec 'g)))
85 (if (or landscape? page-set?)
88 (define (embed-font string)
89 (let ((start (string-contains string "<defs>"))
90 (end (string-contains string "</defs>")))
91 (substring string (+ start 7) (- end 1))))
93 (define (dump-fonts outputter paper)
94 (let* ((fonts (ly:paper-fonts paper))
95 (font-names (uniq-list (sort
97 (map ly:font-file-name fonts)) string<?)))
100 (let ((file-name (ly:find-file (string-append x ".svg"))))
101 (if file-name (embed-font (cached-file-contents file-name))
102 (begin (ly:warning "cannot find SVG font ~S" x) ""))))
103 (filter string? font-names))))
104 (entity 'defs (string-join svgs "\n"))))