>From c46da5f8237c3c658e011e35faddb300ea553059 Mon Sep 17 00:00:00 2001 From: Elias Pipping Date: Sun, 26 Jun 2011 20:02:29 +0200 Subject: [PATCH 1/2] Make doc-view-pdf->png-1 customizable This allows mupdf's mudraw to be used as a pdf->png converter Not only is it faster than ghostscript, it also does anti-aliasing --- lisp/doc-view.el | 73 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/lisp/doc-view.el b/lisp/doc-view.el index 53e7811..05f7422 100644 --- a/lisp/doc-view.el +++ b/lisp/doc-view.el @@ -154,6 +154,26 @@ :type 'file :group 'doc-view) +(defcustom doc-view-pdfdraw-program "mudraw" + "Program to convert PDF files to PNG." + :type 'file + :group 'doc-view) + +(defcustom doc-view-pdf->png-converter-invocation + 'doc-view-pdf->png-converter-invocation-ghostscript + "Called to convert a PDF file into a PNG file" + :type '(radio (function-item doc-view-pdf->png-converter-invocation-ghostscript :doc "Use ghostscript") + (function-item doc-view-pdf->png-converter-invocation-mupdf :doc "Use mupdf") + function) + :group 'doc-view) + +(defcustom doc-view-ps->png-converter-invocation + 'doc-view-ps->png-converter-invocation-ghostscript + "Called to convert a PS file into a PNG file" + :type '(radio (function-item doc-view-ps->png-converter-invocation-ghostscript :doc "Use ghostscript") + function) + :group 'doc-view) + (defcustom doc-view-ghostscript-options '("-dSAFER" ;; Avoid security problems when rendering files from untrusted ;; sources. @@ -643,6 +663,7 @@ OpenDocument format)." (executable-find doc-view-dvipdfm-program))))) ((or (eq type 'postscript) (eq type 'ps) (eq type 'eps) (eq type 'pdf)) + ;; FIXME: allow mupdf here (and doc-view-ghostscript-program (executable-find doc-view-ghostscript-program))) ((eq type 'odf) @@ -802,6 +823,25 @@ Should be invoked when the cached images aren't up-to-date." (list "-o" pdf dvi) callback))) +(defun doc-view-pdf->png-converter-invocation-ghostscript (resolution pdf png &optional page) + `((command . ,doc-view-ghostscript-program) + (arguments . (,@doc-view-ghostscript-options + ,(format "-r%d" resolution) + ,@(if page `(,(format "-dFirstPage=%d" page))) + ,@(if page `(,(format "-dLastPage=%d" page))) + ,(concat "-sOutputFile=" png) + ,pdf)))) + +(defalias 'doc-view-ps->png-converter-invocation-ghostscript + 'doc-view-pdf->png-converter-invocation-ghostscript) + +(defun doc-view-pdf->png-converter-invocation-mupdf (resolution pdf png &optional page) + `((command . ,doc-view-pdfdraw-program) + (arguments . (,(concat "-o" png) + ,(format "-r%d" resolution) + ,pdf + ,@(if page `(,(format "%d" page))))))) + (defun doc-view-odf->pdf (odf callback) "Convert ODF to PDF asynchronously and call CALLBACK when finished. The converted PDF is put into the current cache directory, and it @@ -812,12 +852,14 @@ is named like ODF with the extension turned to pdf." (defun doc-view-pdf/ps->png (pdf-ps png) "Convert PDF-PS to PNG asynchronously." - (doc-view-start-process - "pdf/ps->png" doc-view-ghostscript-program - (append doc-view-ghostscript-options - (list (format "-r%d" (round doc-view-resolution)) - (concat "-sOutputFile=" png) - pdf-ps)) + (let ((invocation (case doc-view-doc-type + (pdf (funcall doc-view-pdf->png-converter-invocation + (round doc-view-resolution) pdf-ps png)) + (t (funcall doc-view-ps->png-converter-invocation + (round doc-view-resolution) pdf-ps png))))) + (doc-view-start-process + "pdf/ps->png" (cdr (assoc 'command invocation)) + (cdr (assoc 'arguments invocation)) (let ((resolution doc-view-resolution)) (lambda () ;; Only create the resolution file when it's all done, so it also @@ -829,7 +871,7 @@ is named like ODF with the extension turned to pdf." (when doc-view-current-timer (cancel-timer doc-view-current-timer) (setq doc-view-current-timer nil)) - (doc-view-display (current-buffer) 'force)))) + (doc-view-display (current-buffer) 'force))))) ;; Update the displayed pages as soon as they're done generating. (when doc-view-conversion-refresh-interval (setq doc-view-current-timer @@ -840,17 +882,12 @@ is named like ODF with the extension turned to pdf." (defun doc-view-pdf->png-1 (pdf png page callback) "Convert a PAGE of a PDF file to PNG asynchronously. Call CALLBACK with no arguments when done." - (doc-view-start-process - "pdf->png-1" doc-view-ghostscript-program - (append doc-view-ghostscript-options - (list (format "-r%d" (round doc-view-resolution)) - ;; Sadly, `gs' only supports the page-range - ;; for PDF files. - (format "-dFirstPage=%d" page) - (format "-dLastPage=%d" page) - (concat "-sOutputFile=" png) - pdf)) - callback)) + (let ((invocation (funcall doc-view-pdf->png-converter-invocation + (round doc-view-resolution) pdf png page))) + (doc-view-start-process + "pdf/ps->png" (cdr (assoc 'command invocation)) + (cdr (assoc 'arguments invocation)) + callback))) (declare-function clear-image-cache "image.c" (&optional filter)) -- 1.7.12