-(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" )))
+(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
+ (basename (basename name ".ps") ".eps")
+ ".pdf"))
+ (is-eps (string-match "\\.eps$" name))
+ (paper-size-string (if is-eps
+ " -dEPSCrop "
+ (format "-dDEVICEWIDTHPOINTS=~,2f \
+-dDEVICEHEIGHTPOINTS=~,2f "
+ paper-width paper-height )))
+
+ (cmd (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 (ly:get-option 'gs-load-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))))