]> git.donarmstrong.com Git - lilypond.git/blob - scm/framework-svg.scm
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / scm / framework-svg.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2010 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;;                Patrick McCarty <pnorcks@gmail.com>
5 ;;;;
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.
10 ;;;;
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.
15 ;;;;
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/>.
18
19 ;;;; Recommendations:
20 ;;;; http://www.w3.org/TR/SVG11/
21 ;;;; http://www.w3.org/TR/SVGTiny12/
22 ;;;;
23 ;;;; Working draft:
24 ;;;; http://www.w3.org/TR/SVGPrint/ -- for <pageSet> and <page>
25
26 ;;;; TODO:
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
31
32 (define-module (scm framework-svg))
33
34 (use-modules
35   (guile)
36   (lily)
37   (scm page)
38   (scm output-svg)
39   (srfi srfi-1)
40   (srfi srfi-2)
41   (srfi srfi-13)
42   (ice-9 regex))
43
44 (define format ergonomic-simple-format)
45
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)))
53
54     `((xmlns . "http://www.w3.org/2000/svg")
55       (xmlns:xlink . "http://www.w3.org/1999/xlink")
56       (version . "1.2")
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)))))
61
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)))
66
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)
71                                         ""))
72     (ly:outputter-dump-stencil outputter page)
73     (dump (ec 'svg))
74     (ly:outputter-close outputter)))
75
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))
81          (filename "")
82          (file-suffix (lambda (num)
83                         (if (= page-count 1) "" (format "-page-~a" num)))))
84
85     (for-each
86       (lambda (page)
87         (set! page-number (1+ page-number))
88         (set! filename (format "~a~a.svg"
89                                basename
90                                (file-suffix page-number)))
91         (dump-page paper filename page page-number page-count))
92       page-stencils)))