Skip to content

Commit

Permalink
The size of icons can be fixed without 'convert' by cropping them.
Browse files Browse the repository at this point in the history
  • Loading branch information
cvmat committed Dec 5, 2009
1 parent d3a00c1 commit 6bbf2af
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -6,6 +6,10 @@
images with/without wget.
(twittering-get-twits): retrieve images with
`twittering-retrieve-image'.
(twittering-use-convert): add a new variable specifying whether
the external command `convert' should be used.
(twittering-make-display-spec-for-icon): new function for
generating properties for icon with/without cropping the image.

2009-12-04 Yuto Hayamizu <y.hayamizu@gmail.com>

Expand Down
45 changes: 37 additions & 8 deletions twittering-mode.el
Expand Up @@ -289,16 +289,20 @@ directory. You should change through function'twittering-icon-mode'")

(defvar twittering-image-stack nil)
(defvar twittering-image-type-cache nil)
(defvar twittering-convert-program "/usr/bin/convert")
(defvar twittering-convert-program (executable-find "convert"))
(defvar twittering-convert-fix-size nil)
(defvar twittering-use-convert (not (null (executable-find "convert")))
"*This variable makes a sense only if `twittering-convert-fix-size'
is non-nil. If this variable is non-nil, icon images are converted by
invoking \"convert\". Otherwise, cropped images are displayed.")
(defvar twittering-use-wget nil
"*If non-nil, icon images are retrieved by invoking \"wget\".
Otherwise, they are retrieved by `url-retrieve'.")

(defun twittering-image-type (file-name)
(if (and (not (assoc file-name twittering-image-type-cache))
(file-exists-p file-name))
(if twittering-convert-fix-size
(if (and twittering-convert-fix-size twittering-use-convert)
(let ((tmpfile (make-temp-file "emacstwit" nil ".png")))
(let ((coding-system-for-read 'raw-text)
(coding-system-for-write 'binary))
Expand All @@ -314,7 +318,8 @@ Otherwise, they are retrieved by `url-retrieve'.")
((string-match "JPEG" file-output) 'jpeg)
((string-match "PNG" file-output) 'png)
((string-match "GIF" file-output) 'gif)
((string-match "bitmap" file-output)
((and twittering-use-convert
(string-match "bitmap" file-output))
(let ((coding-system-for-read 'raw-text)
(coding-system-for-write 'binary))
(with-temp-buffer
Expand Down Expand Up @@ -687,6 +692,32 @@ Otherwise, they are retrieved by `url-retrieve'.")
(concat (md5 icon-url nil nil 'iso-2022-7bit)
(or (ffap-file-suffix icon-url) ".img")))

(defun twittering-make-display-spec-for-icon (fullpath)
"Return the specification for `display' text property, which limits
the size of an icon image FULLPATH up to FIXED-LENGTH.
If the size of the image exceeds FIXED-LENGTH, the center of the
image are displayed."
(let* ((image-spec
`(image :type ,(twittering-image-type fullpath)
:file ,fullpath)))
(if (and twittering-convert-fix-size (not twittering-use-convert))
(let* ((size (if (file-exists-p fullpath)
(image-size image-spec t)
'(48 . 48)))
(width (car size))
(height (cdr size))
(fixed-length twittering-convert-fix-size)
(half-fixed-length (/ fixed-length 2))
(slice-spec
(if (or (< fixed-length width) (< fixed-length height))
`(slice ,(max 0 (- (/ width 2) half-fixed-length))
,(max 0 (- (/ height 2) half-fixed-length))
,fixed-length ,fixed-length)
`(slice 0 0 ,fixed-length ,fixed-length))))
`(display (,image-spec ,slice-spec)))
`(display ,image-spec))))

(defun twittering-format-status (status format-str)
;; Formatting strategy:
;;
Expand Down Expand Up @@ -716,11 +747,9 @@ Otherwise, they are retrieved by `url-retrieve'.")
(add-to-list 'twittering-image-stack profile-image-url))

(when (and icon-string twittering-icon-mode)
(set-text-properties
1 2 `(display
(image :type ,(twittering-image-type fullpath)
:file ,fullpath))
icon-string)
(let ((display-spec
(twittering-make-display-spec-for-icon fullpath)))
(set-text-properties 1 2 display-spec icon-string))
icon-string)
)))))
(let ((cursor 0)
Expand Down

0 comments on commit 6bbf2af

Please sign in to comment.