Subject: [PATCH] docview: Use svg images when using mupdf for conversion

From: Visuwesh <[email-masked]>
Date: Sat, 24 Sep 2022 15:48:59 +0530

Tags: patch

Severity: wishlist

Attached patch makes mupdf produce svg images rather than png when svg
support is available.  This makes a noticeable improvement in image
quality when zooming in.  This also improves epub, odt, docx,
etc. rendering since they also end up using mupdf.
I'm not sure if this is NEWS worthy though.

I've also attached two images before.png and after.png to illustrate the
difference in rendering before and after applying this patch.

>From 3b01128362e28317dfaa738a8c5bbf0a02a22d8f Mon Sep 17 00:00:00 2001
From: Visuwesh <[email-masked]>
Date: Sat, 24 Sep 2022 15:40:27 +0530
Subject: [PATCH] docview: Use svg images when using mupdf for conversion

* lisp/doc-view.el (doc-view-mupdf-use-svg, doc-view-svg-background)
(doc-view-svg-foreground): New user options.
(doc-view-insert-image): Add :background and :foreground image
attributes when display svg images.
(doc-view-set-up-single-converter): Produce svg images when using
mupdf.
---
 lisp/doc-view.el | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index aa0f9fd383..fbd1427946 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -209,6 +209,23 @@ doc-view-pdf->png-converter-function
           function)
   :version "24.4")
 
+(defcustom doc-view-mupdf-use-svg (image-type-available-p 'svg)
+  "Whether to use svg images for PDF files."
+  :type 'boolean
+  :version "29.1")
+
+(defcustom doc-view-svg-background "white"
+  "Background color for svg images.
+See `doc-view-mupdf-use-svg'."
+  :type 'color
+  :version "29.1")
+
+(defcustom doc-view-svg-foreground "black"
+  "Foreground color for svg images.
+See `doc-view-mupdf-use-svg'."
+  :type 'color
+  :version "29.1")
+
 (defcustom doc-view-ghostscript-options
   '("-dSAFER" ;; Avoid security problems when rendering files from untrusted
     ;; sources.
@@ -1562,6 +1579,9 @@ doc-view-insert-image
 			    (setq args `(,@args :width ,doc-view-image-width)))
                           (unless (member :transform-smoothing args)
                             (setq args `(,@args :transform-smoothing t)))
+                          (when (eq doc-view--image-type 'svg)
+                            (setq args `(,@args :background ,doc-view-svg-background
+                                               :foreground ,doc-view-svg-foreground)))
 			  (apply #'create-image file doc-view--image-type nil args))))
 	     (slice (doc-view-current-slice))
 	     (img-width (and image (car (image-size image))))
@@ -1983,7 +2003,11 @@ doc-view-set-up-single-converter
   (pcase-let ((`(,conv-function ,type ,extension)
                (pcase doc-view-doc-type
                  ('djvu (list #'doc-view-djvu->tiff-converter-ddjvu 'tiff "tif"))
-                 (_     (list doc-view-pdf->png-converter-function  'png  "png")))))
+                 (_ (if (and (eq doc-view-pdf->png-converter-function
+                                 #'doc-view-pdf->png-converter-mupdf)
+                             doc-view-mupdf-use-svg)
+                        (list doc-view-pdf->png-converter-function 'svg "svg")
+                      (list doc-view-pdf->png-converter-function 'png "png"))))))
     (setq-local doc-view-single-page-converter-function conv-function)
     (setq-local doc-view--image-type type)
     (setq-local doc-view--image-file-pattern (concat "page-%s." extension))))
-- 
2.35.1