From 1dced88a90a5cf3e940747d2958b02b95ea3491c Mon Sep 17 00:00:00 2001
From: Yuan Fu <casouri@gmail.com>
Date: Wed, 8 Jul 2020 15:21:48 -0400
Subject: [PATCH] Remove imagemagick code in doc-view and add centering
 commands

The problem with old imagemagick code is that, doc-view checks for
imagemagick and only rescales the image if imagemagic is present.
Now that we have natice rescaling, we want to rescale image regardless.

* lisp/doc-view.el (doc-view-mode-map): Add binding for centering
commands.
(doc-view-mode-p, doc-view-enlarge, doc-view-scale-reset,
doc-view-insert-image): Remove imagemagick code.
(doc-view--auto-center-horizontally,
doc-view--auto-center-vertically): New variables.
(doc-view-toggle-auto-center-page-horizontally,
doc-view-toggle-auto-center-page-vertically,
doc-view-auto-center,
doc-view-center-page-horizontally,
doc-view-center-page-vertically): New functions.
---
 lisp/doc-view.el | 79 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 67 insertions(+), 12 deletions(-)

diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index de342f1519..db0e1de233 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -435,6 +435,11 @@ doc-view-mode-map
     (define-key map (kbd "c m")       'doc-view-set-slice-using-mouse)
     (define-key map (kbd "c b")       'doc-view-set-slice-from-bounding-box)
     (define-key map (kbd "c r")       'doc-view-reset-slice)
+    ;; Centering the image
+    (define-key map (kbd "c h")       'doc-view-center-page-horizontally)
+    (define-key map (kbd "c v")       'doc-view-center-page-vertically)
+    (define-key map (kbd "c H")       'doc-view-toggle-auto-center-page-horizontally)
+    (define-key map (kbd "c V")       'doc-view-toggle-auto-center-page-vertically)
     ;; Searching
     (define-key map (kbd "C-s")       'doc-view-search)
     (define-key map (kbd "<find>")    'doc-view-search)
@@ -740,8 +745,7 @@ doc-view-mode-p
 Document types are symbols like `dvi', `ps', `pdf', or `odf' (any
 OpenDocument format)."
   (and (display-graphic-p)
-       (or (image-type-available-p 'imagemagick)
-	   (image-type-available-p 'png))
+       (image-type-available-p 'png)
        (cond
 	((eq type 'dvi)
 	 (and (doc-view-mode-p 'pdf)
@@ -769,10 +773,7 @@ doc-view-shrink-factor
 (defun doc-view-enlarge (factor)
   "Enlarge the document by FACTOR."
   (interactive (list doc-view-shrink-factor))
-  (if (and doc-view-scale-internally
-           (eq (plist-get (cdr (doc-view-current-image)) :type)
-               'imagemagick))
-      ;; ImageMagick supports on-the-fly-rescaling.
+  (if doc-view-scale-internally
       (let ((new (ceiling (* factor doc-view-image-width))))
         (unless (equal new doc-view-image-width)
           (setq-local doc-view-image-width new)
@@ -792,9 +793,7 @@ doc-view-shrink
 (defun doc-view-scale-reset ()
   "Reset the document size/zoom level to the initial one."
   (interactive)
-  (if (and doc-view-scale-internally
-           (eq (plist-get (cdr (doc-view-current-image)) :type)
-               'imagemagick))
+  (if doc-view-scale-internally
       (progn
 	(kill-local-variable 'doc-view-image-width)
 	(doc-view-insert-image
@@ -927,6 +926,63 @@ doc-view-fit-window-to-page
     (when new-frame-params
       (modify-frame-parameters (selected-frame) new-frame-params))))
 
+(defvar-local doc-view--auto-center-horizontally nil
+  "If non-nil, automatically center page horizontally.")
+
+(defvar-local doc-view--auto-center-vertically nil
+  "If non-nil, automatically center page vertically.")
+
+(defun doc-view-toggle-auto-center-page-horizontally ()
+  "Toggle whether to automatically center page horizontally."
+  (interactive)
+  (setq doc-view--auto-center-horizontally
+        (not doc-view--auto-center-horizontally))
+  (message "Auto center horizontally: %s"
+           (if doc-view--auto-center-horizontally
+               "on" "off")))
+
+(defun doc-view-toggle-auto-center-page-vertically ()
+  "Toggle whether to automatically center page vertically."
+  (interactive)
+  (setq doc-view--auto-center-vertically
+        (not doc-view--auto-center-vertically))
+  (message "Auto center vertically: %s"
+           (if doc-view--auto-center-vertically
+               "on" "off")))
+
+(defun doc-view-auto-center ()
+  "Automatically center page."
+  (when doc-view--auto-center-horizontally
+    (doc-view-center-page-horizontally))
+  (when doc-view--auto-center-vertically
+    (doc-view-center-page-vertically)))
+
+(defun doc-view-center-page-horizontally ()
+  "Center page horizontally when page is wider than window."
+  (interactive)
+  (let ((page-width (car (image-size (doc-view-current-image) 'pixel)))
+        (window-width (window-body-width nil 'pixel))
+        ;; How much do we scroll in order to center the page?
+        (pixel-hscroll 0)
+        ;; How many pixels are there in a column?
+        (col-in-pixel (/ (window-body-width nil 'pixel)
+                         (window-body-width nil))))
+    (when (> page-width window-width)
+      (setq pixel-hscroll (/ (- page-width window-width) 2))
+      (set-window-hscroll (selected-window)
+                          (/ pixel-hscroll col-in-pixel)))))
+
+(defun doc-view-center-page-vertically ()
+  "Center page vertically when page is wider than window."
+  (interactive)
+  (let ((page-height (cdr (image-size (doc-view-current-image) 'pixel)))
+        (window-height (window-body-height nil 'pixel))
+        ;; How much do we scroll in order to center the page?
+        (pixel-scroll 0))
+    (when (> page-height window-height)
+      (setq pixel-scroll (/ (- page-height window-height) 2))
+      (set-window-vscroll (selected-window) pixel-scroll 'pixel))))
+
 (defun doc-view-reconvert-doc ()
   "Reconvert the current document.
 Should be invoked when the cached images aren't up-to-date."
@@ -1393,12 +1449,11 @@ doc-view-insert-image
     ;; Only insert the image if the buffer is visible.
     (when (window-live-p (overlay-get ol 'window))
       (let* ((image (if (and file (file-readable-p file))
-			(if (not (and doc-view-scale-internally
-				      (fboundp 'imagemagick-types)))
+			(if (not doc-view-scale-internally)
 			    (apply #'create-image file doc-view--image-type nil args)
 			  (unless (member :width args)
 			    (setq args `(,@args :width ,doc-view-image-width)))
-			  (apply #'create-image file 'imagemagick nil args))))
+			  (apply #'create-image file doc-view--image-type nil args))))
 	     (slice (doc-view-current-slice))
 	     (img-width (and image (car (image-size image))))
 	     (displayed-img-width (if (and image slice)
-- 
2.27.0

