From: Tassilo Horn Date: Fri, 8 Apr 2011 20:09:19 +0000 (+0200) Subject: Fit contents to emacs window support for doc-view by Sho Nakatani. X-Git-Tag: emacs-pretest-24.0.90~104^2~275^2~363 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=d7b898799f330e0543b3f01277114a9e98a7a456;p=emacs.git Fit contents to emacs window support for doc-view by Sho Nakatani. 2011-03-28 Sho Nakatani gmail.com> * doc-view.el (doc-view-fit-width-to-window) (doc-view-fit-height-to-window, doc-view-fit-page-to-window): New functions for fitting the shown image to the Emacs window size. (doc-view-mode-map): Add bindings for the new functions. --- diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 551e12f94ba..07d77bc9d64 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,10 @@ +2011-04-08 Sho Nakatani + + * doc-view.el (doc-view-fit-width-to-window) + (doc-view-fit-height-to-window, doc-view-fit-page-to-window): New + functions for fitting the shown image to the Emacs window size. + (doc-view-mode-map): Add bindings for the new functions. + 2011-03-24 Juanma Barranquero * vc-annotate.el (vc-annotate-show-log-revision-at-line): diff --git a/lisp/doc-view.el b/lisp/doc-view.el index 7bead624cc7..ab0d6bf837b 100644 --- a/lisp/doc-view.el +++ b/lisp/doc-view.el @@ -328,6 +328,10 @@ Can be `dvi', `pdf', or `ps'.") ;; Zoom in/out. (define-key map "+" 'doc-view-enlarge) (define-key map "-" 'doc-view-shrink) + ;; Fit the image to the window + (define-key map "W" 'doc-view-fit-width-to-window) + (define-key map "H" 'doc-view-fit-height-to-window) + (define-key map "P" 'doc-view-fit-page-to-window) ;; Killing the buffer (and the process) (define-key map (kbd "k") 'doc-view-kill-proc-and-buffer) (define-key map (kbd "K") 'doc-view-kill-proc) @@ -664,6 +668,78 @@ OpenDocument format)." (interactive (list doc-view-shrink-factor)) (doc-view-enlarge (/ 1.0 factor))) +(defun doc-view-fit-width-to-window () + "Fit the image width to the window width." + (interactive) + (let ((win-width (- (nth 2 (window-inside-pixel-edges)) + (nth 0 (window-inside-pixel-edges)))) + (slice (doc-view-current-slice))) + (if (not slice) + (let ((img-width (car (image-display-size + (image-get-display-property) t)))) + (doc-view-enlarge (/ (float win-width) (float img-width)))) + + ;; If slice is set + (let* ((slice-width (nth 2 slice)) + (scale-factor (/ (float win-width) (float slice-width))) + (new-slice (mapcar (lambda (x) (ceiling (* scale-factor x))) slice))) + + (doc-view-enlarge scale-factor) + (setf (doc-view-current-slice) new-slice) + (doc-view-goto-page (doc-view-current-page)))))) + +(defun doc-view-fit-height-to-window () + "Fit the image height to the window height." + (interactive) + (let ((win-height (- (nth 3 (window-inside-pixel-edges)) + (nth 1 (window-inside-pixel-edges)))) + (slice (doc-view-current-slice))) + (if (not slice) + (let ((img-height (cdr (image-display-size + (image-get-display-property) t)))) + ;; When users call 'doc-view-fit-height-to-window', + ;; they might want to go to next page by typing SPC + ;; ONLY once. So I used '(- win-height 1)' instead of + ;; 'win-height' + (doc-view-enlarge (/ (float (- win-height 1)) (float img-height)))) + + ;; If slice is set + (let* ((slice-height (nth 3 slice)) + (scale-factor (/ (float (- win-height 1)) (float slice-height))) + (new-slice (mapcar (lambda (x) (ceiling (* scale-factor x))) slice))) + + (doc-view-enlarge scale-factor) + (setf (doc-view-current-slice) new-slice) + (doc-view-goto-page (doc-view-current-page)))))) + +(defun doc-view-fit-page-to-window () + "Fit the image to the window. +More specifically, this function enlarges image by: + +min {(window-width / image-width), (window-height / image-height)} times." + (interactive) + (let ((win-width (- (nth 2 (window-inside-pixel-edges)) + (nth 0 (window-inside-pixel-edges)))) + (win-height (- (nth 3 (window-inside-pixel-edges)) + (nth 1 (window-inside-pixel-edges)))) + (slice (doc-view-current-slice))) + (if (not slice) + (let ((img-width (car (image-display-size + (image-get-display-property) t))) + (img-height (cdr (image-display-size + (image-get-display-property) t)))) + (doc-view-enlarge (min (/ (float win-width) (float img-width)) + (/ (float (- win-height 1)) (float img-height))))) + ;; If slice is set + (let* ((slice-width (nth 2 slice)) + (slice-height (nth 3 slice)) + (scale-factor (min (/ (float win-width) (float slice-width)) + (/ (float (- win-height 1)) (float slice-height)))) + (new-slice (mapcar (lambda (x) (ceiling (* scale-factor x))) slice))) + (doc-view-enlarge scale-factor) + (setf (doc-view-current-slice) new-slice) + (doc-view-goto-page (doc-view-current-page)))))) + (defun doc-view-reconvert-doc () "Reconvert the current document. Should be invoked when the cached images aren't up-to-date."