-
Notifications
You must be signed in to change notification settings - Fork 2
/
org-limit-image-size.el
140 lines (109 loc) · 4.8 KB
/
org-limit-image-size.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
;;; org-limit-image-size.el --- Limit Inline Image Size for Org-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2021 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei <misohena@gmail.com>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; (with-eval-after-load "org"
;; (require 'org-limit-image-size)
;; (org-limit-image-size-activate))
;;; Code:
(require 'org)
;;;; Image Size Setting
(defcustom org-limit-image-size '(0.90 . 0.4) "Maximum image size"
:group 'org
:type
'(choice (integer :tag "Pixels")
(float :tag "Ratio to frame size")
(cons :tag "Width and Height"
(choice (integer :tag "Width in pixels")
(float :tag "Width as a ratio to frame width"))
(choice (integer :tag "Height in pixels")
(float :tag "Height as a ratio to frame height")))))
(defun org-limit-image-size--get-size (width-p)
"Return the maximum size of the image in pixels.
If WIDTH-P is non-nil, return width, otherwise return height."
(let ((limit-size
(if (consp org-limit-image-size)
(if width-p (car org-limit-image-size) (cdr org-limit-image-size))
org-limit-image-size)))
(if (floatp limit-size)
(ceiling (* limit-size
(if width-p (frame-text-width) (frame-text-height))))
limit-size)))
;;;; Activate/Deactivate
(defun org-limit-image-size-activate ()
(interactive)
(advice-add #'org-display-inline-images :around
#'org-limit-image-size--org-display-inline-images))
(defun org-limit-image-size-deactivate ()
(interactive)
(advice-remove #'org-display-inline-images
#'org-limit-image-size--org-display-inline-images))
;;;; Override Functions
(defun org-limit-image-size--org-display-inline-images (old-func &rest args)
"Forces :max-width and :max-height properties to be added to
the create-image function while org-display-inline-images is
running."
(cl-letf* ((old-create-image (symbol-function #'create-image))
((symbol-function #'create-image)
(lambda (&rest args)
(apply #'org-limit-image-size--create-image
old-create-image args))))
(apply old-func args)))
(defun org-limit-image-size--create-image
(old-create-image file-or-data &optional type data-p &rest props)
"Call OLD-CREATE-IMAGE by adding :max-width and :max-height to the PROPS."
(when org-limit-image-size
(let ((max-width (org-limit-image-size--get-size t))
(max-height (org-limit-image-size--get-size nil))
(width (plist-get props :width))
(height (plist-get props :height)))
;; Use imagemagick if available (for Emacs Version < 27).
(when (and (null type)
(image-type-available-p 'imagemagick))
(setq type 'imagemagick))
;; Limit :width and :height property.
;;@todo clone props?
(when (and (numberp width) (> width max-width))
(when (numberp height)
(setq props
(plist-put props
:height
(setq height (/ (* height max-width) width)))))
(setq props
(plist-put props
:width
(setq width max-width))))
(when (and (numberp height) (> height max-height))
(when (numberp width)
(setq props
(plist-put props
:width
(setq width (/ (* width max-height) height)))))
(setq props
(plist-put props
:height
(setq height max-height))))
;; Remove :width nil.
;; Some environments fail when :width nil and :max-width are
;; specified at the same time (Emacs 26 and ImageMagick).
(unless (plist-get props :width)
(setq props (org-plist-delete props :width)))
;; Add :max-width and :max-height
(setq props
(plist-put props :max-width max-width))
(setq props
(plist-put props :max-height max-height))))
(apply old-create-image file-or-data type data-p props))
(provide 'org-limit-image-size)
;;; org-limit-image-size.el ends here