(defun shr-rescale-image (data &optional force)
"Rescale DATA, if too big, to fit the current buffer.
If FORCE, rescale the image anyway."
- (let ((image (create-image data nil t :ascent 100)))
- (if (or (not (fboundp 'imagemagick-types))
- (not (get-buffer-window (current-buffer))))
- image
- (let* ((size (image-size image t))
- (width (car size))
- (height (cdr size))
- (edges (window-inside-pixel-edges
- (get-buffer-window (current-buffer))))
- (window-width (truncate (* shr-max-image-proportion
- (- (nth 2 edges) (nth 0 edges)))))
- (window-height (truncate (* shr-max-image-proportion
- (- (nth 3 edges) (nth 1 edges)))))
- scaled-image)
- (when (or force
- (> height window-height))
- (setq image (or (create-image data 'imagemagick t
- :height window-height
- :ascent 100)
- image))
- (setq size (image-size image t)))
- (when (> (car size) window-width)
- (setq image (or
- (create-image data 'imagemagick t
- :width window-width
- :ascent 100)
- image)))
- image))))
+ (if (or (not (fboundp 'imagemagick-types))
+ (not (get-buffer-window (current-buffer))))
+ (create-image data nil t :ascent 100)
+ (let ((edges (window-inside-pixel-edges
+ (get-buffer-window (current-buffer)))))
+ (create-image
+ data 'imagemagick t
+ :ascent 100
+ :max-width (truncate (* shr-max-image-proportion
+ (- (nth 2 edges) (nth 0 edges))))
+ :max-height (truncate (* shr-max-image-proportion
+ (- (nth 3 edges) (nth 1 edges))))))))
;; url-cache-extract autoloads url-cache.
(declare-function url-cache-create-filename "url-cache" (url))
static unsigned long *colors_in_color_table (int *n);
#endif
+Lisp_Object QCmax_width, QCmax_height;
+
/* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
id, which is just an int that this section returns. Bitmaps are
reference counted so they can be shared among frames.
#endif /* HAVE_GIF */
+static void
+compute_image_size (size_t width, size_t height,
+ Lisp_Object spec,
+ int *d_width, int *d_height)
+{
+ Lisp_Object value;
+ int desired_width, desired_height;
+
+ /* If width and/or height is set in the display spec assume we want
+ to scale to those values. If either h or w is unspecified, the
+ unspecified should be calculated from the specified to preserve
+ aspect ratio. */
+ value = image_spec_value (spec, QCwidth, NULL);
+ desired_width = (INTEGERP (value) ? XFASTINT (value) : -1);
+ value = image_spec_value (spec, QCheight, NULL);
+ desired_height = (INTEGERP (value) ? XFASTINT (value) : -1);
+
+ if (desired_width == -1)
+ {
+ value = image_spec_value (spec, QCmax_width, NULL);
+ if (INTEGERP (value) &&
+ width > XFASTINT (value))
+ {
+ /* The image is wider than :max-width. */
+ desired_width = XFASTINT (value);
+ if (desired_height == -1)
+ {
+ value = image_spec_value (spec, QCmax_height, NULL);
+ if (INTEGERP (value))
+ {
+ /* We have no specified height, but we have a
+ :max-height value, so check that we satisfy both
+ conditions. */
+ desired_height = (double) desired_width / width * height;
+ if (desired_height > XFASTINT (value))
+ {
+ desired_height = XFASTINT (value);
+ desired_width = (double) desired_height / height * width;
+ }
+ }
+ else
+ {
+ /* We have no specified height and no specified
+ max-height, so just compute the height. */
+ desired_height = (double) desired_width / width * height;
+ }
+ }
+ }
+ }
+
+ if (desired_height == -1)
+ {
+ value = image_spec_value (spec, QCmax_height, NULL);
+ if (INTEGERP (value) &&
+ height > XFASTINT (value))
+ desired_height = XFASTINT (value);
+ }
+
+ if (desired_width != -1 && desired_height == -1)
+ /* w known, calculate h. */
+ desired_height = (double) desired_width / width * height;
+
+ if (desired_width == -1 && desired_height != -1)
+ /* h known, calculate w. */
+ desired_width = (double) desired_height / height * width;
+
+ *d_width = desired_width;
+ *d_height = desired_height;
+}
+
/***********************************************************************
ImageMagick
***********************************************************************/
IMAGEMAGICK_BACKGROUND,
IMAGEMAGICK_HEIGHT,
IMAGEMAGICK_WIDTH,
+ IMAGEMAGICK_MAX_HEIGHT,
+ IMAGEMAGICK_MAX_WIDTH,
IMAGEMAGICK_ROTATION,
IMAGEMAGICK_CROP,
IMAGEMAGICK_LAST
{":background", IMAGE_STRING_OR_NIL_VALUE, 0},
{":height", IMAGE_INTEGER_VALUE, 0},
{":width", IMAGE_INTEGER_VALUE, 0},
+ {":max-height", IMAGE_INTEGER_VALUE, 0},
+ {":max-width", IMAGE_INTEGER_VALUE, 0},
{":rotation", IMAGE_NUMBER_VALUE, 0},
{":crop", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
};
PixelSetBlue (bg_wand, (double) bgcolor.blue / 65535);
}
- /* If width and/or height is set in the display spec assume we want
- to scale to those values. If either h or w is unspecified, the
- unspecified should be calculated from the specified to preserve
- aspect ratio. */
- value = image_spec_value (img->spec, QCwidth, NULL);
- desired_width = (INTEGERP (value) ? XFASTINT (value) : -1);
- value = image_spec_value (img->spec, QCheight, NULL);
- desired_height = (INTEGERP (value) ? XFASTINT (value) : -1);
-
- height = MagickGetImageHeight (image_wand);
- width = MagickGetImageWidth (image_wand);
+ compute_image_size (MagickGetImageWidth (image_wand),
+ MagickGetImageHeight (image_wand),
+ img->spec, &desired_width, &desired_height);
- if (desired_width != -1 && desired_height == -1)
- /* w known, calculate h. */
- desired_height = (double) desired_width / width * height;
- if (desired_width == -1 && desired_height != -1)
- /* h known, calculate w. */
- desired_width = (double) desired_height / height * width;
if (desired_width != -1 && desired_height != -1)
{
status = MagickScaleImage (image_wand, desired_width, desired_height);
DEFSYM (Qheuristic, "heuristic");
DEFSYM (Qpostscript, "postscript");
+ DEFSYM (QCmax_width, ":max-width");
+ DEFSYM (QCmax_height, ":max-height");
#ifdef HAVE_GHOSTSCRIPT
ADD_IMAGE_TYPE (Qpostscript);
DEFSYM (QCloader, ":loader");