-(define-public (postscript->pdf papersizename name)
- (let* ((cmd (format #f "ps2pdf -sPAPERSIZE=~a '~a'"
- (sanitize-command-option papersizename) name))
- (pdf-name (string-append (basename name ".ps") ".pdf" )))
-
- (if (access? pdf-name W_OK)
- (delete-file pdf-name))
+(define-public (search-executable names)
+ (define (helper path lst)
+ (if (null? (cdr lst))
+ (car lst)
+ (if (search-path path (car lst)) (car lst)
+ (helper path (cdr lst)))))
+
+ (let ((path (parse-path (getenv "PATH"))))
+ (helper path names)))
+
+(define-public (search-gs)
+
+ ;; must be sure that we don't catch stuff from old GUBs.
+ (search-executable '("gs")))
+
+(define-public (postscript->pdf paper-width paper-height name)
+ (let* ((pdf-name (string-append
+ (dir-basename name ".ps" ".eps")
+ ".pdf"))
+ (is-eps (string-match "\\.eps$" name))
+ (paper-size-string (if is-eps
+ " -dEPSCrop "
+ (ly:format "-dDEVICEWIDTHPOINTS=~$ \
+-dDEVICEHEIGHTPOINTS=~$ "
+ paper-width paper-height )))
+
+ (cmd (simple-format #f
+ "~a\
+ ~a\
+ ~a\
+ ~a\
+ -dCompatibilityLevel=1.4 \
+ -dNOPAUSE\
+ -dBATCH\
+ -r1200 \
+ -sDEVICE=pdfwrite\
+ -sOutputFile=~S\
+ -c .setpdfwrite\
+ -f ~S\
+"
+ (search-gs)
+ (if (ly:get-option 'verbose) "" "-q")
+ (if (or (ly:get-option 'gs-load-fonts)
+ (ly:get-option 'gs-load-lily-fonts))
+
+ " -dNOSAFER "
+ " -dSAFER ")
+ paper-size-string
+ pdf-name
+ name)))
+ ;; The wrapper on windows cannot handle `=' signs,
+ ;; gs has a workaround with #.
+ (if (eq? PLATFORM 'windows)
+ (begin
+ (set! cmd (string-regexp-substitute "=" "#" cmd))
+ (set! cmd (string-regexp-substitute "-dSAFER " "" cmd))
+ (if (access? pdf-name W_OK)
+ (delete-file pdf-name))))