From 63260127f5f4aeed7843e6c17a82ea74eedc9e8e Mon Sep 17 00:00:00 2001 From: Alan Third Date: Mon, 12 Apr 2021 20:30:12 +0100 Subject: [PATCH] Allow use of em in image spec sizes * src/image.c (image_get_length): New function. (compute_image_size): Use image_get_length to set the sizes, and pass in the image struct instead of just the spec. (image_set_transform): (imagemagick_load_image): (svg_load_image): Use the image instead of hte spec in compute_image_size. (syms_of_image): Add 'em' as a symbol. --- src/image.c | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/src/image.c b/src/image.c index 6fe0b23f73..575b4c7200 100644 --- a/src/image.c +++ b/src/image.c @@ -1987,46 +1987,63 @@ scale_image_size (int size, size_t divisor, size_t multiplier) return INT_MAX; } +/* Return a size in pixels either directly from the value specified by + SYMBOL, or from a CONS of the form (VALUE . UNITS). If the value + doesn't exist in the image spec, or is invalid, return -1. */ +static int +image_get_length (struct image *img, Lisp_Object symbol) +{ + Lisp_Object value = image_spec_value (img->spec, symbol, NULL); + + if (FIXNATP (value)) + return min (XFIXNAT (value), INT_MAX); + if (CONSP (value) && NUMBERP (CAR (value)) && EQ (Qem, CDR (value))) + return min (img->face_font_size * XFLOATINT (CAR (value)), INT_MAX); + + return -1; +} + /* Compute the desired size of an image with native size WIDTH x HEIGHT. Use SPEC to deduce the size. Store the desired size into *D_WIDTH x *D_HEIGHT. Store -1 x -1 if the native size is OK. */ static void compute_image_size (size_t width, size_t height, - Lisp_Object spec, + struct image *img, int *d_width, int *d_height) { Lisp_Object value; + int int_value; int desired_width = -1, desired_height = -1, max_width = -1, max_height = -1; double scale = 1; - value = image_spec_value (spec, QCscale, NULL); + value = image_spec_value (img->spec, QCscale, NULL); if (NUMBERP (value)) scale = XFLOATINT (value); - value = image_spec_value (spec, QCmax_width, NULL); - if (FIXNATP (value)) - max_width = min (XFIXNAT (value), INT_MAX); + int_value = image_get_length (img, QCmax_width); + if (int_value >= 0) + max_width = int_value; - value = image_spec_value (spec, QCmax_height, NULL); - if (FIXNATP (value)) - max_height = min (XFIXNAT (value), INT_MAX); + int_value = image_get_length (img, QCmax_height); + if (int_value >= 0) + max_height = int_value; /* 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); - if (FIXNATP (value)) + int_value = image_get_length (img, QCwidth); + if (int_value >= 0) { - desired_width = min (XFIXNAT (value) * scale, INT_MAX); + desired_width = int_value; /* :width overrides :max-width. */ max_width = -1; } - value = image_spec_value (spec, QCheight, NULL); - if (FIXNATP (value)) + int_value = image_get_length (img, QCheight); + if (int_value >= 0) { - desired_height = min (XFIXNAT (value) * scale, INT_MAX); + desired_height = int_value; /* :height overrides :max-height. */ max_height = -1; } @@ -2216,7 +2233,7 @@ image_set_transform (struct frame *f, struct image *img) } else #endif - compute_image_size (img->width, img->height, img->spec, &width, &height); + compute_image_size (img->width, img->height, img, &width, &height); /* Determine rotation. */ double rotation = 0.0; @@ -9210,7 +9227,7 @@ imagemagick_load_image (struct frame *f, struct image *img, compute_image_size (MagickGetImageWidth (image_wand), MagickGetImageHeight (image_wand), - img->spec, &desired_width, &desired_height); + img, &desired_width, &desired_height); if (desired_width != -1 && desired_height != -1) { @@ -10068,7 +10085,7 @@ svg_load_image (struct frame *f, struct image *img, char *contents, viewbox_height = dimension_data.height; } - compute_image_size (viewbox_width, viewbox_height, img->spec, + compute_image_size (viewbox_width, viewbox_height, img, &width, &height); width *= FRAME_SCALE_FACTOR (f); @@ -10777,6 +10794,8 @@ syms_of_image (void) DEFSYM (QCmax_width, ":max-width"); DEFSYM (QCmax_height, ":max-height"); + DEFSYM (Qem, "em"); + #ifdef HAVE_NATIVE_TRANSFORMS DEFSYM (Qscale, "scale"); DEFSYM (Qrotate, "rotate"); -- 2.29.2