]> code.delx.au - gnu-emacs/blob - lisp/image.el
(create-image): Doc fix.
[gnu-emacs] / lisp / image.el
1 ;;; image.el --- image API
2
3 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Keywords: multimedia
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (defconst image-type-regexps
28 '(("^/\\*.*XPM.\\*/" . xpm)
29 ("^P[1-6]" . pbm)
30 ("^GIF8" . gif)
31 ("JFIF" . jpeg)
32 ("^\211PNG\r\n" . png)
33 ("^#define" . xbm)
34 ("^\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
35 ("^%!PS" . ghostscript))
36 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
37 When the first bytes of an image file match REGEXP, it is assumed to
38 be of image type IMAGE-TYPE.")
39
40
41 ;;;###autoload
42 (defun image-type-from-data (data)
43 "Determine the image type from image data DATA.
44 Value is a symbol specifying the image type or nil if type cannot
45 be determined."
46 (let ((types image-type-regexps)
47 type)
48 (while (and types (null type))
49 (let ((regexp (car (car types)))
50 (image-type (cdr (car types))))
51 (when (string-match regexp data)
52 (setq type image-type))
53 (setq types (cdr types))))
54 type))
55
56
57 ;;;###autoload
58 (defun image-type-from-file-header (file)
59 "Determine the type of image file FILE from its first few bytes.
60 Value is a symbol specifying the image type, or nil if type cannot
61 be determined."
62 (unless (file-name-directory file)
63 (setq file (expand-file-name file data-directory)))
64 (setq file (expand-file-name file))
65 (let ((header (with-temp-buffer
66 (insert-file-contents-literally file nil 0 256)
67 (buffer-string))))
68 (image-type-from-data header)))
69
70
71 ;;;###autoload
72 (defun image-type-available-p (type)
73 "Value is non-nil if image type TYPE is available.
74 Image types are symbols like `xbm' or `jpeg'."
75 (and (boundp 'image-types) (not (null (memq type image-types)))))
76
77
78 ;;;###autoload
79 (defun create-image (file-or-data &optional type data-p &rest props)
80 "Create an image.
81 FILE-OR-DATA is an image file name or image data.
82 Optional TYPE is a symbol describing the image type. If TYPE is omitted
83 or nil, try to determine the image type from its first few bytes
84 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
85 use its file extension.as image type.
86 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
87 Optional PROPS are additional image attributes to assign to the image,
88 like, e.g. `:mask MASK'.
89 Value is the image created, or nil if images of type TYPE are not supported."
90 (when (and (not data-p) (not (stringp file-or-data)))
91 (error "Invalid image file name `%s'" file-or-data))
92 (cond ((null data-p)
93 ;; FILE-OR-DATA is a file name.
94 (unless (or type
95 (setq type (image-type-from-file-header file-or-data)))
96 (let ((extension (file-name-extension file-or-data)))
97 (unless extension
98 (error "Cannot determine image type"))
99 (setq type (intern extension)))))
100 (t
101 ;; FILE-OR-DATA contains image data.
102 (unless type
103 (setq type (image-type-from-data file-or-data)))))
104 (unless type
105 (error "Cannot determine image type"))
106 (unless (symbolp type)
107 (error "Invalid image type `%s'" type))
108 (when (image-type-available-p type)
109 (append (list 'image :type type (if data-p :data :file) file-or-data)
110 props)))
111
112
113 ;;;###autoload
114 (defun put-image (image pos &optional string area)
115 "Put image IMAGE in front of POS in the current buffer.
116 IMAGE must be an image created with `create-image' or `defimage'.
117 IMAGE is displayed by putting an overlay into the current buffer with a
118 `before-string' STRING that has a `display' property whose value is the
119 image. STRING is defaulted if you omit it.
120 POS may be an integer or marker.
121 AREA is where to display the image. AREA nil or omitted means
122 display it in the text area, a value of `left-margin' means
123 display it in the left marginal area, a value of `right-margin'
124 means display it in the right marginal area."
125 (unless string (setq string "x"))
126 (let ((buffer (current-buffer)))
127 (unless (eq (car-safe image) 'image)
128 (error "Not an image: %s" image))
129 (unless (or (null area) (memq area '(left-margin right-margin)))
130 (error "Invalid area %s" area))
131 (setq string (copy-sequence string))
132 (let ((overlay (make-overlay pos pos buffer))
133 (prop (if (null area) image (list (list 'margin area) image))))
134 (put-text-property 0 (length string) 'display prop string)
135 (overlay-put overlay 'put-image t)
136 (overlay-put overlay 'before-string string))))
137
138
139 ;;;###autoload
140 (defun insert-image (image &optional string area)
141 "Insert IMAGE into current buffer at point.
142 IMAGE is displayed by inserting STRING into the current buffer
143 with a `display' property whose value is the image. STRING is
144 defaulted if you omit it.
145 AREA is where to display the image. AREA nil or omitted means
146 display it in the text area, a value of `left-margin' means
147 display it in the left marginal area, a value of `right-margin'
148 means display it in the right marginal area."
149 ;; Use a space as least likely to cause trouble when it's a hidden
150 ;; character in the buffer.
151 (unless string (setq string " "))
152 (unless (eq (car-safe image) 'image)
153 (error "Not an image: %s" image))
154 (unless (or (null area) (memq area '(left-margin right-margin)))
155 (error "Invalid area %s" area))
156 (if area
157 (setq image (list (list 'margin area) image))
158 ;; Cons up a new spec equal but not eq to `image' so that
159 ;; inserting it twice in a row (adjacently) displays two copies of
160 ;; the image. Don't try to avoid this by looking at the display
161 ;; properties on either side so that we DTRT more often with
162 ;; cut-and-paste. (Yanking killed image text next to another copy
163 ;; of it loses anyway.)
164 (setq image (cons 'image (cdr image))))
165 (let ((start (point)))
166 (insert string)
167 (add-text-properties start (point)
168 (list 'display image
169 ;; `image' has the right properties to
170 ;; mark an intangible field.
171 'intangible image
172 'rear-nonsticky (list 'display)))))
173
174
175 ;;;###autoload
176 (defun remove-images (start end &optional buffer)
177 "Remove images between START and END in BUFFER.
178 Remove only images that were put in BUFFER with calls to `put-image'.
179 BUFFER nil or omitted means use the current buffer."
180 (unless buffer
181 (setq buffer (current-buffer)))
182 (let ((overlays (overlays-in start end)))
183 (while overlays
184 (let ((overlay (car overlays)))
185 (when (overlay-get overlay 'put-image)
186 (delete-overlay overlay)))
187 (setq overlays (cdr overlays)))))
188
189
190 ;;;###autoload
191 (defun find-image (specs)
192 "Find an image, choosing one of a list of image specifications.
193
194 SPECS is a list of image specifications.
195
196 Each image specification in SPECS is a property list. The contents of
197 a specification are image type dependent. All specifications must at
198 least contain the properties `:type TYPE' and either `:file FILE' or
199 `:data DATA', where TYPE is a symbol specifying the image type,
200 e.g. `xbm', FILE is the file to load the image from, and DATA is a
201 string containing the actual image data. The specification whose TYPE
202 is supported, and FILE exists, is used to construct the image
203 specification to be returned. Return nil if no specification is
204 satisfied.
205
206 The image is looked for first on `load-path' and then in `data-directory'."
207 (let (image)
208 (while (and specs (null image))
209 (let* ((spec (car specs))
210 (type (plist-get spec :type))
211 (data (plist-get spec :data))
212 (file (plist-get spec :file))
213 found)
214 (when (image-type-available-p type)
215 (cond ((stringp file)
216 (let ((path load-path))
217 (while (and (not found) path)
218 (let ((try-file (expand-file-name file (car path))))
219 (when (file-readable-p try-file)
220 (setq found try-file)))
221 (setq path (cdr path)))
222 (unless found
223 (let ((try-file (expand-file-name file data-directory)))
224 (if (file-readable-p try-file)
225 (setq found try-file))))
226 (if found
227 (setq image
228 (cons 'image (plist-put (copy-sequence spec)
229 :file found))))))
230 ((not (null data))
231 (setq image (cons 'image spec)))))
232 (setq specs (cdr specs))))
233 image))
234
235
236 ;;;###autoload
237 (defmacro defimage (symbol specs &optional doc)
238 "Define SYMBOL as an image.
239
240 SPECS is a list of image specifications. DOC is an optional
241 documentation string.
242
243 Each image specification in SPECS is a property list. The contents of
244 a specification are image type dependent. All specifications must at
245 least contain the properties `:type TYPE' and either `:file FILE' or
246 `:data DATA', where TYPE is a symbol specifying the image type,
247 e.g. `xbm', FILE is the file to load the image from, and DATA is a
248 string containing the actual image data. The first image
249 specification whose TYPE is supported, and FILE exists, is used to
250 define SYMBOL.
251
252 Example:
253
254 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
255 (:type xbm :file \"~/test1.xbm\")))"
256 `(defvar ,symbol (find-image ',specs) ,doc))
257
258
259 (provide 'image)
260
261 ;;; image.el ends here