]> git.donarmstrong.com Git - lilypond.git/blob - scm/framework-svg.scm
Web-ja: update introduction
[lilypond.git] / scm / framework-svg.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2015 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 (mkdirs dir-name mode)
62   (let loop ((dir-name (string-split dir-name #\/)) (root ""))
63     (if (pair? dir-name)
64         (let ((dir (string-append root (car dir-name))))
65           (if (not (file-exists? dir))
66               (mkdir dir mode))
67           (loop (cdr dir-name) (string-append dir "/"))))))
68
69 (define output-dir #f)
70
71 (define (svg-define-font font font-name scaling)
72   (let* ((base-file-name (basename (if (list? font) (pango-pf-file-name font)
73                                        (ly:font-file-name font)) ".otf"))
74          (woff-file-name (string-regexp-substitute "([.]otf)?$" ".woff"
75                                                    base-file-name))
76          (woff-file (or (ly:find-file woff-file-name) "/no-such-file.woff"))
77          (url (string-append output-dir "/fonts/" (lilypond-version) "/"
78                              (basename woff-file-name)))
79          (lower-name (string-downcase font-name)))
80     (if (file-exists? woff-file)
81         (begin
82           (if (not (file-exists? url))
83               (begin
84                 (ly:message (_ "Updating font into: ~a") url)
85                 (mkdirs (string-append output-dir "/" (dirname url)) #o700)
86                 (copy-file woff-file url)
87                 (ly:progress "\n")))
88           (ly:format
89            "@font-face {
90 font-family: '~a';
91 font-weight: normal;
92 font-style: normal;
93 src: url('~a');
94 }
95 "
96            font-name url))
97         "")))
98
99 (define (woff-header paper dir)
100   "TODO:
101       * add (ly:version) to font name
102       * copy woff font with version alongside svg output
103 "
104   (set! output-dir dir)
105   (string-append
106    (eo 'defs)
107    (eo 'style '(text . "style/css"))
108    "<![CDATA[
109 "
110    (define-fonts paper svg-define-font svg-define-font)
111    "]]>
112 "
113    (ec 'style)
114    (ec 'defs)))
115
116 (define (dump-page paper filename page page-number page-count)
117   (let* ((outputter (ly:make-paper-outputter (open-file filename "wb") 'svg))
118          (dump (lambda (str) (display str (ly:outputter-port outputter))))
119          (lookup (lambda (x) (ly:output-def-lookup paper x)))
120          (unit-length (lookup 'output-scale))
121          (output-scale (* lily-unit->mm-factor unit-length))
122          (device-width (lookup 'paper-width))
123          (device-height (lookup 'paper-height))
124          (page-width (* output-scale device-width))
125          (page-height (* output-scale device-height)))
126
127     (if (ly:get-option 'svg-woff)
128         (module-define! (ly:outputter-module outputter) 'paper paper))
129     (dump (svg-begin page-width page-height
130                      0 0 device-width device-height))
131     (if (ly:get-option 'svg-woff)
132         (module-remove! (ly:outputter-module outputter) 'paper))
133     (if (ly:get-option 'svg-woff)
134         (dump (woff-header paper (dirname filename))))
135     (dump (comment (format #f "Page: ~S/~S" page-number page-count)))
136     (ly:outputter-output-scheme outputter
137                                 `(begin (set! lily-unit-length ,unit-length)
138                                         ""))
139     (ly:outputter-dump-stencil outputter page)
140     (dump (svg-end))
141     (ly:outputter-close outputter)))
142
143 (define (dump-preview paper stencil filename)
144   (let* ((outputter (ly:make-paper-outputter (open-file filename "wb") 'svg))
145          (dump (lambda (str) (display str (ly:outputter-port outputter))))
146          (lookup (lambda (x) (ly:output-def-lookup paper x)))
147          (unit-length (lookup 'output-scale))
148          (x-extent (ly:stencil-extent stencil X))
149          (y-extent (ly:stencil-extent stencil Y))
150          (left-x (car x-extent))
151          (top-y (cdr y-extent))
152          (device-width (interval-length x-extent))
153          (device-height (interval-length y-extent))
154          (output-scale (* lily-unit->mm-factor unit-length))
155          (svg-width (* output-scale device-width))
156          (svg-height (* output-scale device-height)))
157
158     (if (ly:get-option 'svg-woff)
159         (module-define! (ly:outputter-module outputter) 'paper paper))
160     (dump (svg-begin svg-width svg-height
161                      left-x (- top-y) device-width device-height))
162     (if (ly:get-option 'svg-woff)
163         (module-remove! (ly:outputter-module outputter) 'paper))
164     (if (ly:get-option 'svg-woff)
165         (dump (woff-header paper (dirname filename))))
166     (ly:outputter-output-scheme outputter
167                                 `(begin (set! lily-unit-length ,unit-length)
168                                         ""))
169     (ly:outputter-dump-stencil outputter stencil)
170     (dump (svg-end))
171     (ly:outputter-close outputter)))
172
173
174 (define (output-framework basename book scopes fields)
175   (let* ((paper (ly:paper-book-paper book))
176          (page-stencils (map page-stencil (ly:paper-book-pages book)))
177          (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
178          (page-count (length page-stencils))
179          (filename "")
180          (file-suffix (lambda (num)
181                         (if (= page-count 1) "" (format #f "-page-~a" num)))))
182     (for-each
183      (lambda (page)
184        (set! page-number (1+ page-number))
185        (set! filename (format #f "~a~a.svg"
186                               basename
187                               (file-suffix page-number)))
188        (dump-page paper filename page page-number page-count))
189      page-stencils)))
190
191 (define (output-preview-framework basename book scopes fields)
192   (let* ((paper (ly:paper-book-paper book))
193          (systems (relevant-book-systems book))
194          (to-dump-systems (relevant-dump-systems systems)))
195     (dump-preview paper
196                   (stack-stencils Y DOWN 0.0
197                                   (map paper-system-stencil
198                                        (reverse to-dump-systems)))
199                   (format #f "~a.preview.svg" basename))))