1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2004--2009 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/>.
20 ;;;; http://www.w3.org/TR/SVG11/
21 ;;;; http://www.w3.org/TR/SVGTiny12/
24 ;;;; http://www.w3.org/TR/SVGPrint/ -- for <pageSet> and <page>
27 ;;;; * Once <pageSet> and <page> are supported by Inkscape and
28 ;;;; other user agents, add a -d option (-dsvg-multiple-page)
29 ;;;; that will create a single SVG file containing all pages
30 ;;;; of output. --pmccarty
32 (define-module (scm framework-svg))
44 (define format ergonomic-simple-format)
46 (define (svg-header paper unit-length)
47 (let* ((lookup (lambda (x) (ly:output-def-lookup paper x)))
48 (output-scale (* lily-unit->mm-factor unit-length))
49 (paper-width (lookup 'paper-width))
50 (paper-height (lookup 'paper-height))
51 (page-width (* output-scale paper-width))
52 (page-height (* output-scale paper-height)))
54 `((xmlns . "http://www.w3.org/2000/svg")
55 (xmlns:xlink . "http://www.w3.org/1999/xlink")
57 (width . ,(ly:format "~2fmm" page-width))
58 (height . ,(ly:format "~2fmm" page-height))
59 (viewBox . ,(ly:format "0 0 ~4f ~4f"
60 paper-width paper-height)))))
62 (define (dump-page paper filename page page-number page-count)
63 (let* ((outputter (ly:make-paper-outputter (open-file filename "wb") 'svg))
64 (dump (lambda (str) (display str (ly:outputter-port outputter))))
65 (unit-length (ly:output-def-lookup paper 'output-scale)))
67 (dump (apply eo 'svg (svg-header paper unit-length)))
68 (dump (comment (format "Page: ~S/~S" page-number page-count)))
69 (ly:outputter-output-scheme outputter
70 `(begin (set! lily-unit-length ,unit-length)
72 (ly:outputter-dump-stencil outputter page)
74 (ly:outputter-close outputter)))
76 (define (output-framework basename book scopes fields)
77 (let* ((paper (ly:paper-book-paper book))
78 (page-stencils (map page-stencil (ly:paper-book-pages book)))
79 (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
80 (page-count (length page-stencils))
82 (file-suffix (lambda (num)
83 (if (= page-count 1) "" (format "-page-~a" num)))))
87 (set! page-number (1+ page-number))
88 (set! filename (format "~a~a.svg"
90 (file-suffix page-number)))
91 (dump-page paper filename page page-number page-count))