diff options
| author | Alan Third | 2023-12-30 11:15:17 +0000 |
|---|---|---|
| committer | Alan Third | 2023-12-30 11:24:12 +0000 |
| commit | 536674138d53e81f9e1d8bd7f3f7e744cc04fae2 (patch) | |
| tree | 08504197a585bd9a96ae6dc563a7791591c5246f /src | |
| parent | 1b0a1e5d224831c09ac655af5bd75b47827ae98d (diff) | |
| download | emacs-536674138d53e81f9e1d8bd7f3f7e744cc04fae2.tar.gz emacs-536674138d53e81f9e1d8bd7f3f7e744cc04fae2.zip | |
; Improve documentation of SVG image loading
* src/image.c (svg_load_image): Add comments explaining the process.
Diffstat (limited to 'src')
| -rw-r--r-- | src/image.c | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/src/image.c b/src/image.c index 651ec0b34e5..a26c1ba7d45 100644 --- a/src/image.c +++ b/src/image.c | |||
| @@ -11804,7 +11804,17 @@ svg_css_length_to_pixels (RsvgLength length, double dpi, int font_size) | |||
| 11804 | 11804 | ||
| 11805 | Use librsvg to do most of the image processing. | 11805 | Use librsvg to do most of the image processing. |
| 11806 | 11806 | ||
| 11807 | Return true when successful. */ | 11807 | Return true when successful. |
| 11808 | |||
| 11809 | The basic process, which is used for all versions of librsvg, is to | ||
| 11810 | load the SVG and parse it, then extract the image dimensions. We | ||
| 11811 | then use those image dimensions to calculate the final size and | ||
| 11812 | wrap the SVG data inside another SVG we build on the fly. This | ||
| 11813 | wrapper does the necessary resizing and setting of foreground and | ||
| 11814 | background colors and is then parsed and rasterized. | ||
| 11815 | |||
| 11816 | It should also be noted that setting up the SVG prior to 2.32 was | ||
| 11817 | done differently, but the overall process is the same. */ | ||
| 11808 | static bool | 11818 | static bool |
| 11809 | svg_load_image (struct frame *f, struct image *img, char *contents, | 11819 | svg_load_image (struct frame *f, struct image *img, char *contents, |
| 11810 | ptrdiff_t size, char *filename) | 11820 | ptrdiff_t size, char *filename) |
| @@ -11858,7 +11868,13 @@ svg_load_image (struct frame *f, struct image *img, char *contents, | |||
| 11858 | Lisp_Object lcss = image_spec_value (img->spec, QCcss, NULL); | 11868 | Lisp_Object lcss = image_spec_value (img->spec, QCcss, NULL); |
| 11859 | if (!STRINGP (lcss)) | 11869 | if (!STRINGP (lcss)) |
| 11860 | { | 11870 | { |
| 11861 | /* Generate the CSS for the SVG image. */ | 11871 | /* Generate the CSS for the SVG image. |
| 11872 | |||
| 11873 | We use this to set the font (font-family in CSS lingo) and | ||
| 11874 | the font size. We can extend this to handle any CSS values | ||
| 11875 | SVG supports, however it's only available in librsvg 2.48 and | ||
| 11876 | above so some things we could set here are handled in the | ||
| 11877 | wrapper below. */ | ||
| 11862 | /* FIXME: The below calculations leave enough space for a font | 11878 | /* FIXME: The below calculations leave enough space for a font |
| 11863 | size up to 9999, if it overflows we just throw an error but | 11879 | size up to 9999, if it overflows we just throw an error but |
| 11864 | should probably increase the buffer size. */ | 11880 | should probably increase the buffer size. */ |
| @@ -11904,7 +11920,23 @@ svg_load_image (struct frame *f, struct image *img, char *contents, | |||
| 11904 | if (err) goto rsvg_error; | 11920 | if (err) goto rsvg_error; |
| 11905 | #endif | 11921 | #endif |
| 11906 | 11922 | ||
| 11907 | /* Get the image dimensions. */ | 11923 | /* Get the image dimensions. |
| 11924 | |||
| 11925 | There are a couple of approaches used here, depending on the | ||
| 11926 | contents of the SVG, and which version of librsvg we're using. | ||
| 11927 | With librsvg versions prior to 2.46 we ask librsvg for the size | ||
| 11928 | of the image, however this may include pats of the image that are | ||
| 11929 | outside of the viewbox. | ||
| 11930 | |||
| 11931 | librsvg 2.46 allows us to request the image's "intrinsic | ||
| 11932 | dimensions", which are the sizes given in the SVG in CSS units. | ||
| 11933 | So, for example, if the image defines it's width as "10mm", we | ||
| 11934 | are given a struct that we need to translate into pixel values | ||
| 11935 | ourself (see svg_css_length_to_pixels). | ||
| 11936 | |||
| 11937 | 2.52 introduces a function that will give us the pixel sizes | ||
| 11938 | directly, assuming we provide the correct screen DPI values. | ||
| 11939 | */ | ||
| 11908 | #if LIBRSVG_CHECK_VERSION (2, 46, 0) | 11940 | #if LIBRSVG_CHECK_VERSION (2, 46, 0) |
| 11909 | gdouble gviewbox_width = 0, gviewbox_height = 0; | 11941 | gdouble gviewbox_width = 0, gviewbox_height = 0; |
| 11910 | gboolean has_viewbox = FALSE; | 11942 | gboolean has_viewbox = FALSE; |
| @@ -12096,6 +12128,8 @@ svg_load_image (struct frame *f, struct image *img, char *contents, | |||
| 12096 | FRAME_DISPLAY_INFO (f)->resy); | 12128 | FRAME_DISPLAY_INFO (f)->resy); |
| 12097 | 12129 | ||
| 12098 | #if LIBRSVG_CHECK_VERSION (2, 48, 0) | 12130 | #if LIBRSVG_CHECK_VERSION (2, 48, 0) |
| 12131 | /* Set the CSS for the wrapped SVG. See the comment above the | ||
| 12132 | previous use of 'css'. */ | ||
| 12099 | rsvg_handle_set_stylesheet (rsvg_handle, (guint8 *)css, strlen (css), NULL); | 12133 | rsvg_handle_set_stylesheet (rsvg_handle, (guint8 *)css, strlen (css), NULL); |
| 12100 | #endif | 12134 | #endif |
| 12101 | #else | 12135 | #else |