]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
* lily/program-option.cc: rename from scm-option.cc
[lilypond.git] / scm / backend-library.scm
1 ;;;; backend-library.scm -- helpers for the backends.
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c)  2005 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
9 ;; backend helpers.
10
11 (define-public (ly:system command)
12   (let* ((status 0)
13          (dev-null "/dev/null")
14          (silenced (if (or (ly:get-option 'verbose)
15                            (not (access? dev-null W_OK)))
16                        command
17                        (format #f "~a > ~a 2>&1 " command dev-null))))
18     (if (ly:get-option 'verbose)
19         (ly:message (_ "Invoking `~a'...") command))
20     
21     (set! status (system silenced))
22     (if (> status 0)
23         (begin
24           (ly:message (_ "`~a' failed (~a)") command status)
25           (ly:progress "\n")
26           ;; hmmm.  what's the best failure option? 
27           (throw 'ly-file-failed)))))
28
29 (define-public (sanitize-command-option str)
30   (string-append
31    "\""
32    (regexp-substitute/global #f "[^- 0-9,.a-zA-Z'\"\\]" str 'pre 'post)
33    "\""))
34
35 (define-public (postscript->pdf papersizename name)
36   (let* ((pdf-name (string-append (basename name ".ps") ".pdf"))
37          (cmd (format #f
38                       "gs\
39  ~a\
40  -dCompatibilityLevel=1.4 \
41  -sPAPERSIZE=~a\
42  -q\
43  -dNOPAUSE\
44  -dBATCH\
45  -r1200 \
46  -sDEVICE=pdfwrite\
47  -sOutputFile=~S\
48  -c .setpdfwrite\
49  -f ~S\
50 "
51                       (if (ly:get-option 'gs-font-load)
52                           " -dNOSAFER "
53                           " -dSAFER ")
54                       (sanitize-command-option papersizename)
55                       pdf-name
56                       name)))
57     ;; The wrapper on windows cannot handle `=' signs,
58     ;; gs has a workaround with #.
59     (if (eq? PLATFORM 'windows)
60         (begin
61           (set! cmd (string-regexp-substitute "=" "#" cmd))
62           (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))))
63
64     (if (access? pdf-name W_OK)
65         (delete-file pdf-name))
66
67     (ly:message (_ "Converting to `~a'...") pdf-name)
68     (ly:progress "\n")
69     (ly:system cmd)
70     (if (running-from-gui?) (delete-file name))))
71
72 (use-modules (scm ps-to-png))
73 (define-public (postscript->png resolution paper-size-name name)
74     ;; Do not try to guess the name of the png file,
75     ;; GS produces PNG files like BASE-page%d.png.
76     ;;(ly:message (_ "Converting to `~a'...")
77     ;;      (string-append (basename name ".ps") "-page1.png" )))
78   (let ((paper-size (sanitize-command-option paper-size-name))
79         (verbose? (ly:get-option 'verbose))
80         (rename-page-1? #f))
81     (ly:message (_ "Converting to ~a...") "PNG")
82     (make-ps-images name resolution paper-size rename-page-1? verbose?)
83     (ly:progress "\n")))
84
85 (define-public (postprocess-output paper-book module filename formats)
86   (for-each
87    (lambda (f)
88      ((eval (string->symbol (string-append "convert-to-" f)) module)
89       paper-book filename))
90    formats))
91
92 (define-public (completize-formats formats)
93   (define new-fmts '())
94
95   (if (member "png" formats)
96       (set! formats (cons "ps" formats)))
97   (if (member "pdf" formats)
98       (set! formats (cons "ps" formats)))
99
100   (for-each
101    (lambda (x)
102      (if (member x formats) (set! new-fmts (cons x new-fmts))))
103    '("tex" "dvi" "ps" "pdf" "png"))
104
105   (uniq-list (reverse new-fmts)))
106
107 (define (header-to-file file-name key value)
108   (set! key (symbol->string key))
109   (if (not (equal? "-" file-name))
110       (set! file-name (string-append file-name "." key)))
111   (ly:message (_ "Writing header field `~a' to `~a'...")
112               key
113               (if (equal? "-" file-name) "<stdout>" file-name))
114   (if (equal? file-name "-")
115       (display value)
116       (display value (open-file file-name "w")))
117   (ly:progress "\n")
118   "")
119
120 (define-public (output-scopes scopes fields basename)
121   (define (output-scope scope)
122     (apply
123      string-append
124      (module-map
125       (lambda (sym var)
126         (let ((val (if (variable-bound? var) (variable-ref var) "")))
127           (if (and (memq sym fields) (string? val))
128               (header-to-file basename sym val))
129           ""))
130       scope)))
131   (apply string-append (map output-scope scopes)))
132