Eli Zaretskii writes: >> From: Joseph Turner >> Cc: 69602@debbugs.gnu.org, stephen.berman@gmx.net, juri@linkov.net >> Date: Thu, 07 Mar 2024 00:08:57 -0800 >> >> Eli Zaretskii writes: > I hoped :map allows its value to be a form that is evaluated when the > image is being processed, in which case that form could call > image-compute-scaling-factor when it produces the coordinates. Thanks! Would this require a change in C? > If that doesn't work, then... > >> When creating an image, we set its :map property according to the return >> value of `image-compute-scaling-factor'. Once the image is inserted into >> the buffer, the user may run `image-increase-size' or `image-rotate', >> which changes how the image is displayed but not its :map. >> >> Now, we need to rerun `image-compute-scaling-factor' and recompute :map. What I said here is wrong. `image-compute-scaling-factor' is not useful for recomputing :map, but `image--current-scaling' is. >> However, there is no hook which runs after the user runs those commands, >> so AFAICT there's no way for our code to know when to recompute :map. > > ...AFAIU, when an image is rescaled, we call > image-transform-properties to produce the updated image properties. There are two ways to rescale an image, `image-transform-properties' (defined in image-mode.el; works only on file-backed images in image-mode) and `image--change-size' (defined in image.el; works on any image object in any mode). For now, I'd like to focus on improving `image.el'. > So I guess you'd like that function to recompute the coordinates in > :map according to the transform? > > IOW, I don't understand why you think the problem can only be solved > in C: AFAIK almost all of the machinery that performs image transforms > is implemented in Lisp, and each time an image is rescaled, we > basically re-process the image descriptor anew. The attached patch adds two hooks in `image.el' which allow packages to recompute an image's map after it's rescaled or rotated. The following demonstrates `image-after-change-size-hooks': (progn (defun image--scale-map (map factor) "Scale MAP by FACTOR, destructively modifying it." (when (and factor (/= 1 factor)) (pcase-dolist (`(,`(,type . ,coords) ,_id ,_plist) map) (pcase-exhaustive type ('rect (setf (caar coords) (round (* (caar coords) factor))) (setf (cdar coords) (round (* (cdar coords) factor))) (setf (cadr coords) (round (* (cadr coords) factor))) (setf (cddr coords) (round (* (cddr coords) factor)))) ('circle (setf (caar coords) (round (* (caar coords) factor))) (setf (cdar coords) (round (* (cdar coords) factor))) (setf (cdr coords) (round (* (cdr coords) factor)))) ('poly (dotimes (i (length coords)) (aset coords i (round (* (aref coords i) factor)))))))) map) (defun image-rescale-image-map () "Recalculate and set :map property of image at point. Assumes that image has an :unscaled-map property." (when-let* ((image (image--get-imagemagick-and-warn)) (unscaled-image (image--image-without-parameters image)) (unscaled-map (image-property image :unscaled-map)) (scale (image--current-scaling image unscaled-image))) (setf (image-property image :map) (image--scale-map (copy-tree unscaled-map t) scale)))) (with-current-buffer (get-buffer-create "*image-properties-test*") (let* ((svg-string "\n\n\n\n\n\norggraphview\n\n\n\na\n\n\nHover me!\n\n\n\n\n\n") (scale 0.75) ; Adjust initial image scale (unscaled-map '(((circle (85 . 85) . 80) "1" (help-echo "Surprise!")))) (map (image--scale-map (copy-tree unscaled-map t) scale)) (image (create-image svg-string 'svg t :scale scale :map map :unscaled-map unscaled-map))) (add-hook 'image-after-change-size-hooks #'image-rescale-image-map nil t) (erase-buffer) (insert-image image) (goto-char (point-min)) (pop-to-buffer (current-buffer))))) After applying the attached patch, evaluate the above form, press "i +" and "i -" repeatedly to see the image and its map scale together. Thanks! Joseph