From mboxrd@z Thu Jan 1 00:00:00 1970 From: Vladimir Alexiev Subject: [PATCH] org-display-inline-images to reduce image size Date: Fri, 7 Jan 2011 16:31:10 +0000 (UTC) Message-ID: References: <48E592E3.5040208@gmx.de> <170f0450810061950k6bbb5773nd1d1fcb024fd35a1@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Return-path: Received: from [140.186.70.92] (port=43033 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PbFEA-0007Yl-SL for emacs-orgmode@gnu.org; Fri, 07 Jan 2011 11:32:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PbFE0-00054b-UL for emacs-orgmode@gnu.org; Fri, 07 Jan 2011 11:31:38 -0500 Received: from lo.gmane.org ([80.91.229.12]:42872) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PbFE0-00054T-IF for emacs-orgmode@gnu.org; Fri, 07 Jan 2011 11:31:36 -0500 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1PbFDv-0003YS-Kp for emacs-orgmode@gnu.org; Fri, 07 Jan 2011 17:31:31 +0100 Received: from client-93-123-21-121.ip.daticum.com ([93.123.21.121]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 07 Jan 2011 17:31:31 +0100 Received: from vladimir by client-93-123-21-121.ip.daticum.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 07 Jan 2011 17:31:31 +0100 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Here's a patch that introduces two custom options org-display-inline-image-width, org-display-inline-image-height and patches org-display-inline-images to respect them as max width, height settings for inline images. (Someone please defcustom them for me, I only know defvar syntax). org-display-inline-images uses overlay-put. (iimage.el uses text-properties, which are recommended in emacs). (The refs below are (elisp) info nodes.) Both overlays and text-properties use "Display Property", "Other Display Specifications" to setup an image. Of all "Image" formats, only "ImageMagick Images" (maybe XBM and PS) support scaling. That is, IF you build Emacs with ImageMagick (a big if indeed!) So the patch uses scaling if (fboundp 'imagemagick-types) and in this case you must call (imagemagick-register-types) in your .emacs; else it uses a slice the top left corner (cropping the rest). diff --git a/doc/org.texi b/doc/org.texi index 96ea986..9e1c5cf 100755 --- a/doc/org.texi +++ b/doc/org.texi @@ -3266,13 +3266,31 @@ variable @code{org-display-internal-link-with-indirect- buffer}}. @vindex org-startup-with-inline-images @cindex @code{inlineimages}, STARTUP keyword @cindex @code{noinlineimages}, STARTUP keyword -Toggle the inline display of linked images. Normally this will only inline +Toggle the inline display of linked images. +Works only for links that start with @code{file:}, +"." (current dir), "/" (root dir) or "~" (home dir). +Normally this will only inline images that have no description part in the link, i.e. images that will also be inlined during export. When called with a prefix argument, also display images that do have a link description. You can ask for inline images to be displayed at startup by configuring the variable @code{org-startup-with-inline-images}@footnote{with corresponding @code{#+STARTUP} keywords @code{inlineimages} and @code{inlineimages}}. + +@vindex org-display-inline-image-width +@vindex org-display-inline-image-height +Maximum image size that Orgmode will display inline. Images are reduced by EITHER: +@itemize @bullet +@itemize Scaling down: supported when Emacs is compiled with ImageMagic, in which case + the function @code{imagemagick-types} is bound. You should invoke + @code{(imagemagick-register-types)} in your @code{.emacs}, OR +@itemize Slicing the top left corner and cropping the rest +@end itemize +Integer specifies maximum number of pixels. +Floating number specifies maximum ratio to the frame width/height respectively. +nil places no restriction on the respective dimension. +If both are set, scaling can distort the aspect ratio + @orgcmd{C-c %,org-mark-ring-push} @cindex mark ring Push the current position onto the mark ring, to be able to return Modified lisp/org.el diff --git a/lisp/org.el b/lisp/org.el index 53039e4..836286c 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -16156,6 +16156,55 @@ INCLUDE-LINKED is passed to `org-display-inline- images'." (length org-inline-image-overlays)) (message "No images to display inline")))) +;; TODO: defcustom +(defvar org-display-inline-image-width nil +"*Maximum image width that Orgmode will display inline. +Images are reduced by: +- Scaling down: supported when Emacs is compiled with ImageMagic, in which case + (fboundp 'imagemagick-types). You should invoke (imagemagick-register-types) in .emacs. +- Otherwise: slicing the top left corner and cropping the rest +Integer specifies maximum number of pixels. +Floating point specifies maximum ratio to the frame width. +nil places no restriction. +If both org-display-inline-image-width and org-display-inline-image-height are set, +scaling can distort the aspect ratio") + +;; TODO: defcustom +(defvar org-display-inline-image-height nil +"*Maximum image height that Orgmode will display inline. +Works similarly to org-display-inline-image-width, which see.") + +;; TODO tested only in GNU Emacs 24.0.50.1 +;; TODO scaling not tested (don't have ImageMagick)! +(defun org-display-inline-images-scale-or-slice (img) + ;; Return eventually scaled or sliced down version of image IMG. + ;; Scaling is an image property (after 'image) + ;; Slicing is a display property (for overlay or text-property) (before 'image). + (when (or org-display-inline-image-width org-display-inline-image-height) + (let (c w h width height width height) + (setq c (image-size img 'pixels)) + (setq w (car c) h (cdr c)) + (setq width (cond + ((integerp org-display-inline-image-width) + org-display-inline-image-width) + ((floatp org-display-inline-image-width) + (truncate (* org-display-inline-image-width (frame-pixel- width)))))) + (setq height (cond + ((integerp org-display-inline-image-height) + org-display-inline-image-height) + ((floatp org-display-inline-image-height) + (truncate (* org-display-inline-image-height (frame-pixel- height)))))) + (setq width (and width (> w width) width)) ; if set and w>width then width + (setq height (and height (> h height) height)) ; if height and h>height then height + (cond ((fboundp 'imagemagick-types) ; do scaling (it's preferred) + ;; FIXME: here we assume that :width :height are given to ImageMagick in pixels, + ;; but they could be ratio of original size + (if width (setq img (append img (list :width width)))) + (if height (setq img (append img (list :height height))))) + ((or width height) ; do slicing + (setq img (list (list 'slice 0 0 width height) img)))))) + img) + (defun org-display-inline-images (&optional include-linked refresh beg end) "Display inline images. Normally only links without a description part are inlined, because this @@ -16190,7 +16239,7 @@ BEG and END default to the buffer boundaries." (setq img (save-match-data (create-image file))) (when img (setq ov (make-overlay (match-beginning 0) (match-end 0))) - (overlay-put ov 'display img) + (overlay-put ov 'display (org-display-inline-images-scale-or- slice img)) (overlay-put ov 'face 'default) (overlay-put ov 'org-image-overlay t) (overlay-put ov 'modification-hooks TINYCHANGE