diff options
| author | Jim Porter | 2024-06-23 12:25:25 -0700 |
|---|---|---|
| committer | Jim Porter | 2024-07-04 12:14:37 -0700 |
| commit | 6d082f3c79269f00308d6e8b7d31d6a119376fe2 (patch) | |
| tree | 29cbb23773a685de02ea81ae4e4f1277cda3fa22 | |
| parent | 3ce7e4ee3f1f8bf85c2c455ac624bec6c7cd10a8 (diff) | |
| download | emacs-6d082f3c79269f00308d6e8b7d31d6a119376fe2.tar.gz emacs-6d082f3c79269f00308d6e8b7d31d6a119376fe2.zip | |
In SHR, keep track of image sizes as specified by the HTML
Previously, these values got lost when zooming the image.
* lisp/net/shr.el (shr-tag-img): Set 'image-dom-size'...
(shr-zoom-image): ... use it. Rename 'size' to 'zoom'.
(shr-image-fetched): Rename 'image-size' to 'image-zoom'.
(shr-put-image): Accept the zoom level as ':zoom' and document it.
Previously, FLAGS was a mix of alist and plist(!).
* test/lisp/net/shr-tests.el (shr-test/zoom-image): Rename "size" to
"zoom".
| -rw-r--r-- | lisp/net/shr.el | 38 | ||||
| -rw-r--r-- | test/lisp/net/shr-tests.el | 6 |
2 files changed, 29 insertions, 15 deletions
diff --git a/lisp/net/shr.el b/lisp/net/shr.el index fe061adae29..7e9a8c6d1c0 100644 --- a/lisp/net/shr.el +++ b/lisp/net/shr.el | |||
| @@ -633,13 +633,14 @@ full-buffer size." | |||
| 633 | (point-max))) | 633 | (point-max))) |
| 634 | (start (or (previous-single-property-change end 'image-url) | 634 | (start (or (previous-single-property-change end 'image-url) |
| 635 | (point-min))) | 635 | (point-min))) |
| 636 | (size (get-text-property (point) 'image-size)) | 636 | (dom-size (get-text-property (point) 'image-dom-size)) |
| 637 | (next-size (cond ((or (eq size 'default) | 637 | (zoom (get-text-property (point) 'image-zoom)) |
| 638 | (null size)) | 638 | (next-zoom (cond ((or (eq zoom 'default) |
| 639 | (null zoom)) | ||
| 639 | 'original) | 640 | 'original) |
| 640 | ((eq size 'original) | 641 | ((eq zoom 'original) |
| 641 | 'full) | 642 | 'full) |
| 642 | ((eq size 'full) | 643 | ((eq zoom 'full) |
| 643 | 'default))) | 644 | 'default))) |
| 644 | (buffer-read-only nil)) | 645 | (buffer-read-only nil)) |
| 645 | ;; Delete the old picture. | 646 | ;; Delete the old picture. |
| @@ -648,7 +649,9 @@ full-buffer size." | |||
| 648 | (url-retrieve url #'shr-image-fetched | 649 | (url-retrieve url #'shr-image-fetched |
| 649 | `(,(current-buffer) ,start | 650 | `(,(current-buffer) ,start |
| 650 | ,(set-marker (make-marker) end) | 651 | ,(set-marker (make-marker) end) |
| 651 | ((size . ,next-size))) | 652 | (:zoom ,next-zoom |
| 653 | :width ,(car dom-size) | ||
| 654 | :height ,(cdr dom-size))) | ||
| 652 | t))))) | 655 | t))))) |
| 653 | 656 | ||
| 654 | ;;; Utility functions. | 657 | ;;; Utility functions. |
| @@ -1095,7 +1098,7 @@ the mouse click event." | |||
| 1095 | (while properties | 1098 | (while properties |
| 1096 | (let ((type (pop properties)) | 1099 | (let ((type (pop properties)) |
| 1097 | (value (pop properties))) | 1100 | (value (pop properties))) |
| 1098 | (unless (memq type '(display image-size)) | 1101 | (unless (memq type '(display image-zoom)) |
| 1099 | (put-text-property start (point) type value))))))))))) | 1102 | (put-text-property start (point) type value))))))))))) |
| 1100 | (kill-buffer image-buffer))) | 1103 | (kill-buffer image-buffer))) |
| 1101 | 1104 | ||
| @@ -1132,9 +1135,19 @@ the mouse click event." | |||
| 1132 | (defun shr-put-image (spec alt &optional flags) | 1135 | (defun shr-put-image (spec alt &optional flags) |
| 1133 | "Insert image SPEC with a string ALT. Return image. | 1136 | "Insert image SPEC with a string ALT. Return image. |
| 1134 | SPEC is either an image data blob, or a list where the first | 1137 | SPEC is either an image data blob, or a list where the first |
| 1135 | element is the data blob and the second element is the content-type." | 1138 | element is the data blob and the second element is the content-type. |
| 1139 | |||
| 1140 | FLAGS is a property list specifying optional parameters for the image. | ||
| 1141 | You can specify the following optional properties: | ||
| 1142 | |||
| 1143 | * `:zoom': The zoom level for the image. One of `default', `original', | ||
| 1144 | or `full'. | ||
| 1145 | * `:width': The width of the image as specified by the HTML \"width\" | ||
| 1146 | attribute. | ||
| 1147 | * `:height': The height of the image as specified by the HTML | ||
| 1148 | \"height\" attribute." | ||
| 1136 | (if (display-graphic-p) | 1149 | (if (display-graphic-p) |
| 1137 | (let* ((size (cdr (assq 'size flags))) | 1150 | (let* ((zoom (plist-get flags :zoom)) |
| 1138 | (data (if (consp spec) | 1151 | (data (if (consp spec) |
| 1139 | (car spec) | 1152 | (car spec) |
| 1140 | spec)) | 1153 | spec)) |
| @@ -1142,13 +1155,13 @@ element is the data blob and the second element is the content-type." | |||
| 1142 | (cadr spec))) | 1155 | (cadr spec))) |
| 1143 | (start (point)) | 1156 | (start (point)) |
| 1144 | (image (cond | 1157 | (image (cond |
| 1145 | ((eq size 'original) | 1158 | ((eq zoom 'original) |
| 1146 | (create-image data nil t :ascent shr-image-ascent | 1159 | (create-image data nil t :ascent shr-image-ascent |
| 1147 | :format content-type)) | 1160 | :format content-type)) |
| 1148 | ((eq content-type 'image/svg+xml) | 1161 | ((eq content-type 'image/svg+xml) |
| 1149 | (when (image-type-available-p 'svg) | 1162 | (when (image-type-available-p 'svg) |
| 1150 | (create-image data 'svg t :ascent shr-image-ascent))) | 1163 | (create-image data 'svg t :ascent shr-image-ascent))) |
| 1151 | ((eq size 'full) | 1164 | ((eq zoom 'full) |
| 1152 | (ignore-errors | 1165 | (ignore-errors |
| 1153 | (shr-rescale-image data content-type | 1166 | (shr-rescale-image data content-type |
| 1154 | (plist-get flags :width) | 1167 | (plist-get flags :width) |
| @@ -1192,7 +1205,7 @@ element is the data blob and the second element is the content-type." | |||
| 1192 | ;; image slices. | 1205 | ;; image slices. |
| 1193 | (overlay-put overlay 'face 'shr-sliced-image))) | 1206 | (overlay-put overlay 'face 'shr-sliced-image))) |
| 1194 | (insert-image image alt)) | 1207 | (insert-image image alt)) |
| 1195 | (put-text-property start (point) 'image-size size) | 1208 | (put-text-property start (point) 'image-zoom zoom) |
| 1196 | (when (and (not inline) shr-max-inline-image-size) | 1209 | (when (and (not inline) shr-max-inline-image-size) |
| 1197 | (insert "\n")) | 1210 | (insert "\n")) |
| 1198 | (when (and shr-image-animate | 1211 | (when (and shr-image-animate |
| @@ -1907,6 +1920,7 @@ The preference is a float determined from `shr-prefer-media-type'." | |||
| 1907 | (put-text-property start (point) 'keymap shr-image-map) | 1920 | (put-text-property start (point) 'keymap shr-image-map) |
| 1908 | (put-text-property start (point) 'shr-alt alt) | 1921 | (put-text-property start (point) 'shr-alt alt) |
| 1909 | (put-text-property start (point) 'image-url url) | 1922 | (put-text-property start (point) 'image-url url) |
| 1923 | (put-text-property start (point) 'image-dom-size (cons width height)) | ||
| 1910 | (put-text-property start (point) 'image-displayer | 1924 | (put-text-property start (point) 'image-displayer |
| 1911 | (shr-image-displayer shr-content-function)) | 1925 | (shr-image-displayer shr-content-function)) |
| 1912 | (put-text-property start (point) 'help-echo | 1926 | (put-text-property start (point) 'help-echo |
diff --git a/test/lisp/net/shr-tests.el b/test/lisp/net/shr-tests.el index c813103b408..f8559df5272 100644 --- a/test/lisp/net/shr-tests.el +++ b/test/lisp/net/shr-tests.el | |||
| @@ -172,14 +172,14 @@ settings, then once more for each (OPTION . VALUE) pair.") | |||
| 172 | (shr-test-wait-for (lambda () (= put-image-calls 2)) | 172 | (shr-test-wait-for (lambda () (= put-image-calls 2)) |
| 173 | "Timed out waiting to zoom image") | 173 | "Timed out waiting to zoom image") |
| 174 | ;; Check that we have a single image at original size. | 174 | ;; Check that we have a single image at original size. |
| 175 | (let (image-sizes) | 175 | (let (image-zooms) |
| 176 | (goto-char (point-min)) | 176 | (goto-char (point-min)) |
| 177 | (while (< (point) (point-max)) | 177 | (while (< (point) (point-max)) |
| 178 | (when (get-text-property (point) 'display) | 178 | (when (get-text-property (point) 'display) |
| 179 | (push (get-text-property (point) 'image-size) image-sizes)) | 179 | (push (get-text-property (point) 'image-zoom) image-zooms)) |
| 180 | (goto-char (or (next-single-property-change (point) 'display) | 180 | (goto-char (or (next-single-property-change (point) 'display) |
| 181 | (point-max)))) | 181 | (point-max)))) |
| 182 | (should (equal image-sizes '(original)))))))))) | 182 | (should (equal image-zooms '(original)))))))))) |
| 183 | 183 | ||
| 184 | (require 'shr) | 184 | (require 'shr) |
| 185 | 185 | ||