1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2004--2010 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))
45 (define format ergonomic-simple-format)
47 (define (svg-begin . rest)
49 '(xmlns . "http://www.w3.org/2000/svg")
50 '(xmlns:xlink . "http://www.w3.org/1999/xlink")
52 `(width . ,(ly:format "~2fmm" (first rest)))
53 `(height . ,(ly:format "~2fmm" (second rest)))
54 `(viewBox . ,(ly:format "~4f ~4f ~4f ~4f"
55 (third rest) (fourth rest)
56 (fifth rest) (sixth rest)))))
61 (define (dump-page paper filename page page-number page-count)
62 (let* ((outputter (ly:make-paper-outputter (open-file filename "wb") 'svg))
63 (dump (lambda (str) (display str (ly:outputter-port outputter))))
64 (lookup (lambda (x) (ly:output-def-lookup paper x)))
65 (unit-length (lookup 'output-scale))
66 (output-scale (* lily-unit->mm-factor unit-length))
67 (device-width (lookup 'paper-width))
68 (device-height (lookup 'paper-height))
69 (page-width (* output-scale device-width))
70 (page-height (* output-scale device-height)))
72 (dump (svg-begin page-width page-height
73 0 0 device-width device-height))
74 (dump (comment (format "Page: ~S/~S" page-number page-count)))
75 (ly:outputter-output-scheme outputter
76 `(begin (set! lily-unit-length ,unit-length)
78 (ly:outputter-dump-stencil outputter page)
80 (ly:outputter-close outputter)))
82 (define (dump-preview paper stencil filename)
83 (let* ((outputter (ly:make-paper-outputter (open-file filename "wb") 'svg))
84 (dump (lambda (str) (display str (ly:outputter-port outputter))))
85 (lookup (lambda (x) (ly:output-def-lookup paper x)))
86 (unit-length (lookup 'output-scale))
87 (x-extent (ly:stencil-extent stencil X))
88 (y-extent (ly:stencil-extent stencil Y))
89 (left-x (car x-extent))
90 (top-y (cdr y-extent))
91 (device-width (interval-length x-extent))
92 (device-height (interval-length y-extent))
93 (output-scale (* lily-unit->mm-factor unit-length))
94 (svg-width (* output-scale device-width))
95 (svg-height (* output-scale device-height)))
97 (dump (svg-begin svg-width svg-height
98 left-x (- top-y) device-width device-height))
99 (ly:outputter-output-scheme outputter
100 `(begin (set! lily-unit-length ,unit-length)
102 (ly:outputter-dump-stencil outputter stencil)
104 (ly:outputter-close outputter)))
107 (define (output-framework basename book scopes fields)
108 (let* ((paper (ly:paper-book-paper book))
109 (page-stencils (map page-stencil (ly:paper-book-pages book)))
110 (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
111 (page-count (length page-stencils))
113 (file-suffix (lambda (num)
114 (if (= page-count 1) "" (format "-page-~a" num)))))
117 (set! page-number (1+ page-number))
118 (set! filename (format "~a~a.svg"
120 (file-suffix page-number)))
121 (dump-page paper filename page page-number page-count))
124 (define (output-preview-framework basename book scopes fields)
125 (let* ((paper (ly:paper-book-paper book))
126 (systems (relevant-book-systems book))
127 (to-dump-systems (relevant-dump-systems systems)))
129 (stack-stencils Y DOWN 0.0
130 (map paper-system-stencil
131 (reverse to-dump-systems)))
132 (format "~a.preview.svg" basename))))