]> git.donarmstrong.com Git - lilypond.git/blob - scm/framework-svg.scm
ebd4eb6b42fa33b2f0cfc9dfcd069e1e5c384517
[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 paper-system)
39   (scm output-svg)
40   (srfi srfi-1)
41   (srfi srfi-2)
42   (srfi srfi-13)
43   (ice-9 regex))
44
45 (define format ergonomic-simple-format)
46
47 (define (svg-begin . rest)
48   (eo 'svg
49       '(xmlns . "http://www.w3.org/2000/svg")
50       '(xmlns:xlink . "http://www.w3.org/1999/xlink")
51       '(version . "1.2")
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)))))
57
58 (define (svg-end)
59   (ec 'svg))
60
61 (define (svg-define-font font font-name scaling)
62   (string-append
63    "@font-face {
64 font-family: '"
65    font-name
66 "';
67 font-weight: normal;
68 font-style: normal;
69 src: url('"
70    (string-downcase font-name)
71    ".woff');
72 }
73 "))
74
75 (define (woff-header paper)
76   "TODO:
77       * add (ly:version) to font name
78       * copy woff font with version alongside svg output
79 "
80   (string-append
81    (eo 'defs)
82    (eo 'style '(text . "style/css"))
83    "<![CDATA[
84 "
85    (define-fonts paper svg-define-font)
86    "]]>
87 "
88    (ec 'style)
89    (ec 'defs)))
90
91 (define (dump-page paper filename page page-number page-count)
92   (let* ((outputter (ly:make-paper-outputter (open-file filename "wb") 'svg))
93          (dump (lambda (str) (display str (ly:outputter-port outputter))))
94          (lookup (lambda (x) (ly:output-def-lookup paper x)))
95          (unit-length (lookup 'output-scale))
96          (output-scale (* lily-unit->mm-factor unit-length))
97          (device-width (lookup 'paper-width))
98          (device-height (lookup 'paper-height))
99          (page-width (* output-scale device-width))
100          (page-height (* output-scale device-height)))
101
102     (dump (svg-begin page-width page-height
103                      0 0 device-width device-height))
104     (if (ly:get-option 'svg-woff)
105         (dump (woff-header paper)))
106     (dump (comment (format "Page: ~S/~S" page-number page-count)))
107     (ly:outputter-output-scheme outputter
108                                 `(begin (set! lily-unit-length ,unit-length)
109                                         ""))
110     (ly:outputter-dump-stencil outputter page)
111     (dump (svg-end))
112     (ly:outputter-close outputter)))
113
114 (define (dump-preview paper stencil filename)
115   (let* ((outputter (ly:make-paper-outputter (open-file filename "wb") 'svg))
116          (dump (lambda (str) (display str (ly:outputter-port outputter))))
117          (lookup (lambda (x) (ly:output-def-lookup paper x)))
118          (unit-length (lookup 'output-scale))
119          (x-extent (ly:stencil-extent stencil X))
120          (y-extent (ly:stencil-extent stencil Y))
121          (left-x (car x-extent))
122          (top-y (cdr y-extent))
123          (device-width (interval-length x-extent))
124          (device-height (interval-length y-extent))
125          (output-scale (* lily-unit->mm-factor unit-length))
126          (svg-width (* output-scale device-width))
127          (svg-height (* output-scale device-height)))
128
129     (dump (svg-begin svg-width svg-height
130                      left-x (- top-y) device-width device-height))
131     (if (ly:get-option svg-woff)
132         (dump (woff-header paper)))
133     (ly:outputter-output-scheme outputter
134                                 `(begin (set! lily-unit-length ,unit-length)
135                                         ""))
136     (ly:outputter-dump-stencil outputter stencil)
137     (dump (svg-end))
138     (ly:outputter-close outputter)))
139
140
141 (define (output-framework basename book scopes fields)
142   (let* ((paper (ly:paper-book-paper book))
143          (page-stencils (map page-stencil (ly:paper-book-pages book)))
144          (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
145          (page-count (length page-stencils))
146          (filename "")
147          (file-suffix (lambda (num)
148                         (if (= page-count 1) "" (format "-page-~a" num)))))
149     (for-each
150       (lambda (page)
151         (set! page-number (1+ page-number))
152         (set! filename (format "~a~a.svg"
153                                basename
154                                (file-suffix page-number)))
155         (dump-page paper filename page page-number page-count))
156       page-stencils)))
157
158 (define (output-preview-framework basename book scopes fields)
159   (let* ((paper (ly:paper-book-paper book))
160          (systems (relevant-book-systems book))
161          (to-dump-systems (relevant-dump-systems systems)))
162     (dump-preview paper
163                   (stack-stencils Y DOWN 0.0
164                                   (map paper-system-stencil
165                                        (reverse to-dump-systems)))
166                   (format "~a.preview.svg" basename))))