]> git.donarmstrong.com Git - lilypond.git/blob - scm/framework-svg.scm
Merge branch 'master' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond into jneeman
[lilypond.git] / scm / framework-svg.scm
1 ;;
2 ;; framework-svg.scm -- structure for SVG output
3 ;;;;
4 ;;;;  source file of the GNU LilyPond music typesetter
5 ;;;; 
6 ;;;; (c) 2004--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7
8 (define-module (scm framework-svg))
9
10 (use-modules (guile)
11              (lily)
12              (scm page)
13              (scm output-svg))
14
15 (use-modules (srfi srfi-1)
16              (srfi srfi-2)
17              (srfi srfi-13)
18              (ice-9 regex))
19
20 (define-public (output-framework basename book scopes fields)
21   (let* ((filename (format "~a.svg" basename))
22          (outputter  (ly:make-paper-outputter (open-file filename "wb")
23                                               (ly:get-option 'backend)))
24          (dump (lambda (str) (display str (ly:outputter-port outputter))))
25          (paper (ly:paper-book-paper book))
26          (unit-length (ly:output-def-lookup paper 'output-scale))
27          (output-scale (* lily-unit->mm-factor
28                           unit-length))
29          (page-stencils (map page-stencil (ly:paper-book-pages book)))
30          (landscape? (eq? (ly:output-def-lookup paper 'landscape) #t))
31          (page-number (1- (ly:output-def-lookup paper 'first-page-number)))
32          (page-count (length page-stencils))
33          (paper-width (ly:output-def-lookup paper 'paper-width))
34          (paper-height (ly:output-def-lookup paper 'paper-height))
35          (page-width (inexact->exact (ceiling (* output-scale paper-width))))
36          (page-height (inexact->exact (ceiling (* output-scale paper-height))))
37          (page-set? (or (> page-count 1) landscape?)))
38
39     (ly:outputter-output-scheme outputter
40                                 `(begin (set! lily-unit-length ,unit-length) ""))
41     (dump (eo 'svg
42               '(xmlns . "http://www.w3.org/2000/svg")
43               '(xmlns:xlink . "http://www.w3.org/1999/xlink")
44               '(version . "1.2")
45
46               ;; Argggghhhh: SVG takes the px <-> mm mapping from the windowing system
47               `(width . ,(format #f "~s" page-width))
48               `(height . ,(format #f "~s" page-height))))
49     
50     (dump (dump-fonts outputter paper))
51     (dump
52      (string-append
53       ;; FIXME: only use pages if there are more than one, pageSet is
54       ;; not supported by all SVG applications yet.
55       (if page-set? (eo 'pageSet) "")
56       (eo 'g `(transform . ,(format "scale(~a, ~a) "
57                                     output-scale output-scale)))))
58     
59     (for-each
60      (lambda (page)
61        (set! page-number (1+ page-number))
62        (dump-page outputter page page-number page-count landscape? page-set?))
63      page-stencils)
64     
65     (if page-set? (eo 'pageSet) "")
66     (dump
67      (string-append
68       (ec 'g)
69       (if page-set? (ec 'pageSet) "")
70       (ec 'svg)))
71     
72     (ly:outputter-close outputter)
73     ))
74
75 (define (dump-page outputter page page-number page-count landscape? page-set?)
76   (define (dump str) (display str (ly:outputter-port outputter)))
77   
78   (dump (comment (format #f "Page: ~S/~S" page-number page-count)))
79   (if (or landscape? page-set?)
80       (dump
81        (if landscape?
82            (eo 'page '(page-orientation . "270"))
83            (eo 'page))))
84   
85   (dump (string-append (eo 'g )))
86   (ly:outputter-dump-stencil outputter page)
87   (dump (string-append (ec 'g)))
88   (if (or landscape? page-set?)
89       (dump (ec 'page))))
90
91 (define (embed-font string)
92   (let ((start (string-contains string "<defs>"))
93         (end (string-contains string "</defs>")))
94     (substring string (+ start 7) (- end 1))))
95
96 (define (dump-fonts outputter paper)
97   (let* ((fonts (ly:paper-fonts paper))
98          (font-names (uniq-list (sort
99                                  (filter string?
100                                          (map ly:font-file-name fonts)) string<?)))
101          (svgs (map
102                 (lambda (x)
103                   (let ((file-name (ly:find-file (string-append x ".svg"))))
104                     (if file-name (embed-font (cached-file-contents file-name))
105                         (begin (ly:warning "cannot find SVG font ~S" x) ""))))
106                 (filter string? font-names))))
107     (entity 'defs (string-join svgs "\n"))))
108