]> git.donarmstrong.com Git - lilypond.git/blob - scm/framework-svg.scm
fb2125c428aa4511435c56d7042789c128f9e401
[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   (let* ((file-name (if (list? font) (pango-pf-file-name font)
63                         (ly:font-file-name font)))
64          (lower-name (string-downcase font-name)))
65     ;; only embed emmentaler for now
66     (if (equal? (substring lower-name 0 (min (string-length lower-name) 10)) "emmentaler")
67         (string-append
68          "@font-face {
69 font-family: '"
70          font-name
71          "';
72 font-weight: normal;
73 font-style: normal;
74 src: url('"
75    (string-downcase font-name)
76    ".woff');
77 }
78 ")
79         "")))
80
81 (define (woff-header paper)
82   "TODO:
83       * add (ly:version) to font name
84       * copy woff font with version alongside svg output
85 "
86   (string-append
87    (eo 'defs)
88    (eo 'style '(text . "style/css"))
89    "<![CDATA[
90 "
91    (define-fonts paper svg-define-font svg-define-font)
92    "]]>
93 "
94    (ec 'style)
95    (ec 'defs)))
96
97 (define (dump-page paper filename page page-number page-count)
98   (let* ((outputter (ly:make-paper-outputter (open-file filename "wb") 'svg))
99          (dump (lambda (str) (display str (ly:outputter-port outputter))))
100          (lookup (lambda (x) (ly:output-def-lookup paper x)))
101          (unit-length (lookup 'output-scale))
102          (output-scale (* lily-unit->mm-factor unit-length))
103          (device-width (lookup 'paper-width))
104          (device-height (lookup 'paper-height))
105          (page-width (* output-scale device-width))
106          (page-height (* output-scale device-height)))
107
108     (if (ly:get-option 'svg-woff)
109         (module-define! (ly:outputter-module outputter) 'paper paper))
110     (dump (svg-begin page-width page-height
111                      0 0 device-width device-height))
112     (if (ly:get-option 'svg-woff)
113         (dump (woff-header paper)))
114     (dump (comment (format "Page: ~S/~S" page-number page-count)))
115     (ly:outputter-output-scheme outputter
116                                 `(begin (set! lily-unit-length ,unit-length)
117                                         ""))
118     (ly:outputter-dump-stencil outputter page)
119     (dump (svg-end))
120     (ly:outputter-close outputter)))
121
122 (define (dump-preview paper stencil filename)
123   (let* ((outputter (ly:make-paper-outputter (open-file filename "wb") 'svg))
124          (dump (lambda (str) (display str (ly:outputter-port outputter))))
125          (lookup (lambda (x) (ly:output-def-lookup paper x)))
126          (unit-length (lookup 'output-scale))
127          (x-extent (ly:stencil-extent stencil X))
128          (y-extent (ly:stencil-extent stencil Y))
129          (left-x (car x-extent))
130          (top-y (cdr y-extent))
131          (device-width (interval-length x-extent))
132          (device-height (interval-length y-extent))
133          (output-scale (* lily-unit->mm-factor unit-length))
134          (svg-width (* output-scale device-width))
135          (svg-height (* output-scale device-height)))
136
137     (if (ly:get-option 'svg-woff)
138         (module-define! (ly:outputter-module outputter) 'paper paper))
139     (dump (svg-begin svg-width svg-height
140                      left-x (- top-y) device-width device-height))
141     (if (ly:get-option svg-woff)
142         (dump (woff-header paper)))
143     (ly:outputter-output-scheme outputter
144                                 `(begin (set! lily-unit-length ,unit-length)
145                                         ""))
146     (ly:outputter-dump-stencil outputter stencil)
147     (dump (svg-end))
148     (ly:outputter-close outputter)))
149
150
151 (define (output-framework basename book scopes fields)
152   (let* ((paper (ly:paper-book-paper book))
153          (page-stencils (map page-stencil (ly:paper-book-pages book)))
154          (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
155          (page-count (length page-stencils))
156          (filename "")
157          (file-suffix (lambda (num)
158                         (if (= page-count 1) "" (format "-page-~a" num)))))
159     (for-each
160       (lambda (page)
161         (set! page-number (1+ page-number))
162         (set! filename (format "~a~a.svg"
163                                basename
164                                (file-suffix page-number)))
165         (dump-page paper filename page page-number page-count))
166       page-stencils)))
167
168 (define (output-preview-framework basename book scopes fields)
169   (let* ((paper (ly:paper-book-paper book))
170          (systems (relevant-book-systems book))
171          (to-dump-systems (relevant-dump-systems systems)))
172     (dump-preview paper
173                   (stack-stencils Y DOWN 0.0
174                                   (map paper-system-stencil
175                                        (reverse to-dump-systems)))
176                   (format "~a.preview.svg" basename))))