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