]> git.donarmstrong.com Git - lilypond.git/blob - scm/backend-library.scm
* lily/lily-guile.cc (gulp_file_to_string): take size argument.
[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 (search-executable names)
36   (define (helper path lst)
37     (if (null? (cdr lst))
38         (car lst)
39         (if (search-path path (car lst)) (car lst)
40             (helper path (cdr lst)))))
41
42   (let ((path (parse-path (getenv "PATH"))))
43     (helper path names)))
44
45 (define-public (search-gs)
46   (search-executable '("gs-nox" "gs-8.15" "gs")))
47
48 (define-public (postscript->pdf papersizename name)
49   (let* ((pdf-name (string-append (basename name ".ps") ".pdf"))
50          (cmd (format #f
51                       "~a\
52  ~a\
53  ~a\
54  -dCompatibilityLevel=1.4 \
55  -sPAPERSIZE=~a\
56  -dNOPAUSE\
57  -dBATCH\
58  -r1200 \
59  -sDEVICE=pdfwrite\
60  -sOutputFile=~S\
61  -c .setpdfwrite\
62  -f ~S\
63 "
64                       (search-gs)
65                       (if (ly:get-option 'verbose) "" "-q")
66                       (if (ly:get-option 'gs-font-load)
67                           " -dNOSAFER "
68                           " -dSAFER ")
69                       (sanitize-command-option papersizename)
70                       pdf-name
71                       name)))
72     ;; The wrapper on windows cannot handle `=' signs,
73     ;; gs has a workaround with #.
74     (if (eq? PLATFORM 'windows)
75         (begin
76           (set! cmd (string-regexp-substitute "=" "#" cmd))
77           (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))))
78
79     (if (access? pdf-name W_OK)
80         (delete-file pdf-name))
81
82     (ly:message (_ "Converting to `~a'...") pdf-name)
83     (ly:progress "\n")
84     (ly:system cmd)
85     ))
86
87 (use-modules (scm ps-to-png))
88
89 (define-public (postscript->png resolution paper-size-name name)
90     ;; Do not try to guess the name of the png file,
91     ;; GS produces PNG files like BASE-page%d.png.
92     ;;(ly:message (_ "Converting to `~a'...")
93     ;;      (string-append (basename name ".ps") "-page1.png" )))
94   (let ((paper-size (sanitize-command-option paper-size-name))
95         (verbose (ly:get-option 'verbose))
96         (rename-page-1 #f))
97
98     (ly:message (_ "Converting to ~a...") "PNG")
99     (make-ps-images name resolution paper-size rename-page-1 verbose
100                     (ly:get-option 'anti-alias-factor))
101     (ly:progress "\n")))
102
103 (define-public (postprocess-output paper-book module filename formats)
104   (let*
105       ((completed (completize-formats formats))
106        (base (string-regexp-substitute "\\.[a-z]+$" "" filename))
107        (intermediate (remove
108                       (lambda (x)
109                         (member x formats)) 
110                       completed)))
111     (for-each
112      (lambda (f)
113        ((eval (string->symbol (string-append "convert-to-" f)) module)
114         paper-book filename))
115      completed)
116
117     (if (ly:get-option 'delete-intermediate-files)
118         (for-each
119          (lambda (f)
120            (delete-file (string-append base "." f)))
121          intermediate))
122     ))
123
124 (define-public (completize-formats formats)
125   (define new-fmts '())
126
127   (if (member "png" formats)
128       (set! formats (cons "ps" formats)))
129   (if (member "pdf" formats)
130       (set! formats (cons "ps" formats)))
131
132   (for-each
133    (lambda (x)
134      (if (member x formats) (set! new-fmts (cons x new-fmts))))
135    '("tex" "dvi" "ps" "pdf" "png"))
136
137   (uniq-list (reverse new-fmts)))
138
139 (define (header-to-file file-name key value)
140   (set! key (symbol->string key))
141   (if (not (equal? "-" file-name))
142       (set! file-name (string-append file-name "." key)))
143   (ly:message (_ "Writing header field `~a' to `~a'...")
144               key
145               (if (equal? "-" file-name) "<stdout>" file-name))
146   (if (equal? file-name "-")
147       (display value)
148       (display value (open-file file-name "w")))
149   (ly:progress "\n")
150   "")
151
152 (define-public (output-scopes scopes fields basename)
153   (define (output-scope scope)
154     (apply
155      string-append
156      (module-map
157       (lambda (sym var)
158         (let ((val (if (variable-bound? var) (variable-ref var) "")))
159           (if (and (memq sym fields) (string? val))
160               (header-to-file basename sym val))
161           ""))
162       scope)))
163   (apply string-append (map output-scope scopes)))
164