diff options
| author | Gerd Moellmann | 2000-01-01 16:33:32 +0000 |
|---|---|---|
| committer | Gerd Moellmann | 2000-01-01 16:33:32 +0000 |
| commit | 162dec0193a63fdad8ee7d840183360b00b15fa7 (patch) | |
| tree | 70277e10d8b5bdedf06cd89fd72218ab56f6f24f | |
| parent | 45158a9105d2693faa02b5dba9b52b77dab7d9e1 (diff) | |
| download | emacs-162dec0193a63fdad8ee7d840183360b00b15fa7.tar.gz emacs-162dec0193a63fdad8ee7d840183360b00b15fa7.zip | |
(defimage): Handle specifications containing :data
instead of :file.
(image-type-from-data): New function.
(image-type-from-file-header): Use it.
(create-image): Add parameter DATA-P.
| -rw-r--r-- | lisp/ChangeLog | 8 | ||||
| -rw-r--r-- | lisp/image.el | 88 |
2 files changed, 62 insertions, 34 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index f38b7da5806..032ede03339 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,11 @@ | |||
| 1 | 2000-01-01 Gerd Moellmann <gerd@gnu.org> | ||
| 2 | |||
| 3 | * image.el (defimage): Handle specifications containing :data | ||
| 4 | instead of :file. | ||
| 5 | (image-type-from-data): New function. | ||
| 6 | (image-type-from-file-header): Use it. | ||
| 7 | (create-image): Add parameter DATA-P. | ||
| 8 | |||
| 1 | 1999-12-31 Richard M. Stallman <rms@caffeine.ai.mit.edu> | 9 | 1999-12-31 Richard M. Stallman <rms@caffeine.ai.mit.edu> |
| 2 | 10 | ||
| 3 | * echistory.el (electric-command-history): Call Command-history-setup | 11 | * echistory.el (electric-command-history): Call Command-history-setup |
diff --git a/lisp/image.el b/lisp/image.el index 9b28d4f2eb2..81ca8cfc4a9 100644 --- a/lisp/image.el +++ b/lisp/image.el | |||
| @@ -39,28 +39,36 @@ be of image type IMAGE-TYPE.") | |||
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | ;;;###autoload | 41 | ;;;###autoload |
| 42 | (defun image-type-from-file-header (file) | 42 | (defun image-type-from-data (data) |
| 43 | "Determine the type of image file FILE from its first few bytes. | 43 | "Determine the image type from image data DATA. |
| 44 | Value is a symbol specifying the image type, or nil if type cannot | 44 | Value is a symbol specifying the image type or nil if type cannot |
| 45 | be determined." | 45 | be determined." |
| 46 | (unless (file-name-directory file) | 46 | (let ((types image-type-regexps) |
| 47 | (setq file (concat data-directory file))) | ||
| 48 | (setq file (expand-file-name file)) | ||
| 49 | (let ((header (with-temp-buffer | ||
| 50 | (insert-file-contents-literally file nil 0 256) | ||
| 51 | (buffer-string))) | ||
| 52 | (types image-type-regexps) | ||
| 53 | type) | 47 | type) |
| 54 | (while (and types (null type)) | 48 | (while (and types (null type)) |
| 55 | (let ((regexp (car (car types))) | 49 | (let ((regexp (car (car types))) |
| 56 | (image-type (cdr (car types)))) | 50 | (image-type (cdr (car types)))) |
| 57 | (when (string-match regexp header) | 51 | (when (string-match regexp data) |
| 58 | (setq type image-type)) | 52 | (setq type image-type)) |
| 59 | (setq types (cdr types)))) | 53 | (setq types (cdr types)))) |
| 60 | type)) | 54 | type)) |
| 61 | 55 | ||
| 62 | 56 | ||
| 63 | ;;;###autoload | 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 | ||
| 64 | (defun image-type-available-p (type) | 72 | (defun image-type-available-p (type) |
| 65 | "Value is non-nil if image type TYPE is available. | 73 | "Value is non-nil if image type TYPE is available. |
| 66 | Image types are symbols like `xbm' or `jpeg'." | 74 | Image types are symbols like `xbm' or `jpeg'." |
| @@ -68,26 +76,38 @@ Image types are symbols like `xbm' or `jpeg'." | |||
| 68 | 76 | ||
| 69 | 77 | ||
| 70 | ;;;###autoload | 78 | ;;;###autoload |
| 71 | (defun create-image (file &optional type &rest props) | 79 | (defun create-image (file-or-data &optional type data-p &rest props) |
| 72 | "Create an image which will be loaded from FILE. | 80 | "Create an image. |
| 81 | FILE-OR-DATA is an image file name or image data. | ||
| 73 | Optional TYPE is a symbol describing the image type. If TYPE is omitted | 82 | Optional TYPE is a symbol describing the image type. If TYPE is omitted |
| 74 | or nil, try to determine the image file type from its first few bytes. | 83 | or nil, try to determine the image type from its first few bytes |
| 75 | If that doesn't work, use FILE's extension as image type. | 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. | ||
| 76 | Optional PROPS are additional image attributes to assign to the image, | 87 | Optional PROPS are additional image attributes to assign to the image, |
| 77 | like, e.g. `:heuristic-mask t'. | 88 | like, e.g. `:heuristic-mask t'. |
| 78 | Value is the image created, or nil if images of type TYPE are not supported." | 89 | Value is the image created, or nil if images of type TYPE are not supported." |
| 79 | (unless (stringp file) | 90 | (unless (stringp file-or-data) |
| 80 | (error "Invalid image file name %s" file)) | 91 | (error "Invalid image file name or data `%s'" file-or-data)) |
| 81 | (unless (or type | 92 | (cond ((null data-p) |
| 82 | (setq type (image-type-from-file-header file))) | 93 | ;; FILE-OR-DATA is a file name. |
| 83 | (let ((extension (file-name-extension file))) | 94 | (unless (or type |
| 84 | (unless extension | 95 | (setq type (image-type-from-file-header file-or-data))) |
| 85 | (error "Cannot determine image type")) | 96 | (let ((extension (file-name-extension file-or-data))) |
| 86 | (setq type (intern extension)))) | 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")) | ||
| 87 | (unless (symbolp type) | 106 | (unless (symbolp type) |
| 88 | (error "Invalid image type %s" type)) | 107 | (error "Invalid image type `%s'" type)) |
| 89 | (when (image-type-available-p type) | 108 | (when (image-type-available-p type) |
| 90 | (append (list 'image :type type :file file) props))) | 109 | (append (list 'image :type type (if data-p :data :file) file-or-data) |
| 110 | props))) | ||
| 91 | 111 | ||
| 92 | 112 | ||
| 93 | ;;;###autoload | 113 | ;;;###autoload |
| @@ -178,17 +198,17 @@ Example: | |||
| 178 | (let (image) | 198 | (let (image) |
| 179 | (while (and specs (null image)) | 199 | (while (and specs (null image)) |
| 180 | (let* ((spec (car specs)) | 200 | (let* ((spec (car specs)) |
| 181 | (data (plist-get spec :data)) | ||
| 182 | (type (plist-get spec :type)) | 201 | (type (plist-get spec :type)) |
| 202 | (data (plist-get spec :data)) | ||
| 183 | (file (plist-get spec :file))) | 203 | (file (plist-get spec :file))) |
| 184 | (when (and (image-type-available-p type) ; Image type is supported | 204 | (when (image-type-available-p type) |
| 185 | (or data (stringp file))) ; Data or file was specified | 205 | (cond ((stringp file) |
| 186 | (if data | 206 | (setq file (expand-file-name file data-directory)) |
| 187 | (setq image (cons 'image spec)) | 207 | (when (file-readable-p file) |
| 188 | (setq file (expand-file-name file data-directory)) | 208 | (setq image (cons 'image (plist-put spec :file file))))) |
| 189 | (when (file-readable-p file) | 209 | ((stringp data) |
| 190 | (setq image (cons 'image (plist-put spec :file file))))) | 210 | (setq image (cons 'image spec))))) |
| 191 | (setq specs (cdr specs))))) | 211 | (setq specs (cdr specs)))) |
| 192 | `(defvar ,symbol ',image ,doc))) | 212 | `(defvar ,symbol ',image ,doc))) |
| 193 | 213 | ||
| 194 | 214 | ||