diff options
| author | Alan Third | 2025-05-24 21:12:22 +0100 |
|---|---|---|
| committer | Alan Third | 2025-05-24 21:40:52 +0100 |
| commit | 23cc82f53be320d5031a2b8bf74df90e274f16a7 (patch) | |
| tree | d2863a4122a44a6019ca0419f5054cb91a69a335 | |
| parent | 0c2e6ec16a60797a3c612c0247564855479068c9 (diff) | |
| download | emacs-scratch/svg-colors.tar.gz emacs-scratch/svg-colors.zip | |
Move CSS into the SVG wrapperscratch/svg-colors
This allows CSS to be used with librsvg < 2.48.
* src/image.c (svg_load_image): Move CSS construction and include
"color". Also append the CSS passed in by the user rather than
replacing it.
| -rw-r--r-- | src/image.c | 56 |
1 files changed, 26 insertions, 30 deletions
diff --git a/src/image.c b/src/image.c index 745668d4cd1..c5caaf739e7 100644 --- a/src/image.c +++ b/src/image.c | |||
| @@ -12078,26 +12078,6 @@ svg_load_image (struct frame *f, struct image *img, char *contents, | |||
| 12078 | 12078 | ||
| 12079 | rsvg_handle_set_dpi_x_y (rsvg_handle, FRAME_DISPLAY_INFO (f)->resx, | 12079 | rsvg_handle_set_dpi_x_y (rsvg_handle, FRAME_DISPLAY_INFO (f)->resx, |
| 12080 | FRAME_DISPLAY_INFO (f)->resy); | 12080 | FRAME_DISPLAY_INFO (f)->resy); |
| 12081 | |||
| 12082 | #if LIBRSVG_CHECK_VERSION (2, 48, 0) | ||
| 12083 | Lisp_Object lcss = image_spec_value (img->spec, QCcss, NULL); | ||
| 12084 | if (!STRINGP (lcss)) | ||
| 12085 | { | ||
| 12086 | /* Generate the CSS for the SVG image. | ||
| 12087 | |||
| 12088 | We use this to set the font (font-family in CSS lingo) and | ||
| 12089 | the font size. We can extend this to handle any CSS values | ||
| 12090 | SVG supports, however it's only available in librsvg 2.48 and | ||
| 12091 | above so some things we could set here are handled in the | ||
| 12092 | wrapper below. */ | ||
| 12093 | lcss = make_formatted_string ("svg{font-family:\"%s\";font-size:%dpx}", | ||
| 12094 | img->face_font_family, | ||
| 12095 | img->face_font_size); | ||
| 12096 | rsvg_handle_set_stylesheet (rsvg_handle, (guint8 *) SDATA (lcss), | ||
| 12097 | SBYTES (lcss), NULL); | ||
| 12098 | } | ||
| 12099 | #endif | ||
| 12100 | |||
| 12101 | #else | 12081 | #else |
| 12102 | /* Make a handle to a new rsvg object. */ | 12082 | /* Make a handle to a new rsvg object. */ |
| 12103 | rsvg_handle = rsvg_handle_new (); | 12083 | rsvg_handle = rsvg_handle_new (); |
| @@ -12265,9 +12245,9 @@ svg_load_image (struct frame *f, struct image *img, char *contents, | |||
| 12265 | static char const wrapper[] = | 12245 | static char const wrapper[] = |
| 12266 | "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" " | 12246 | "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" " |
| 12267 | "xmlns:xi=\"http://www.w3.org/2001/XInclude\" " | 12247 | "xmlns:xi=\"http://www.w3.org/2001/XInclude\" " |
| 12268 | "style=\"color: #%06X;\" " | ||
| 12269 | "width=\"%d\" height=\"%d\" preserveAspectRatio=\"none\" " | 12248 | "width=\"%d\" height=\"%d\" preserveAspectRatio=\"none\" " |
| 12270 | "viewBox=\"0 0 %f %f\">" | 12249 | "viewBox=\"0 0 %f %f\">" |
| 12250 | "<style>%s</style>" | ||
| 12271 | "<rect width=\"100%%\" height=\"100%%\" fill=\"#%06X\"/>" | 12251 | "<rect width=\"100%%\" height=\"100%%\" fill=\"#%06X\"/>" |
| 12272 | "<xi:include href=\"data:image/svg+xml;base64,%s\"></xi:include>" | 12252 | "<xi:include href=\"data:image/svg+xml;base64,%s\"></xi:include>" |
| 12273 | "</svg>"; | 12253 | "</svg>"; |
| @@ -12296,10 +12276,33 @@ svg_load_image (struct frame *f, struct image *img, char *contents, | |||
| 12296 | #endif | 12276 | #endif |
| 12297 | 12277 | ||
| 12298 | unsigned int color = foreground & 0xFFFFFF, fill = background & 0xFFFFFF; | 12278 | unsigned int color = foreground & 0xFFFFFF, fill = background & 0xFFFFFF; |
| 12279 | |||
| 12280 | Lisp_Object user_css = image_spec_value (img->spec, QCcss, NULL); | ||
| 12281 | if (!STRINGP (user_css)) | ||
| 12282 | user_css = make_string("", 0); | ||
| 12283 | |||
| 12284 | /* Generate the CSS for the SVG image. | ||
| 12285 | |||
| 12286 | We use this to set the "current color", font (font-family in CSS | ||
| 12287 | lingo) and the font size. We can extend this to handle any CSS | ||
| 12288 | values SVG supports. We append the user CSS, which should allow | ||
| 12289 | the user to over-ride anything we set in our defaults. This gets | ||
| 12290 | inserted in the SVG's style element. */ | ||
| 12291 | Lisp_Object css = make_formatted_string ("svg{" | ||
| 12292 | " font-family: \"%s\";" | ||
| 12293 | " font-size: %dpx;" | ||
| 12294 | " color: #%06X;" | ||
| 12295 | "}" | ||
| 12296 | "%s", | ||
| 12297 | img->face_font_family, | ||
| 12298 | img->face_font_size, | ||
| 12299 | color, SDATA(user_css)); | ||
| 12300 | |||
| 12299 | wrapped_contents | 12301 | wrapped_contents |
| 12300 | = make_formatted_string (wrapper, color, width, height, | 12302 | = make_formatted_string (wrapper, width, height, |
| 12301 | viewbox_width, viewbox_height, | 12303 | viewbox_width, viewbox_height, |
| 12302 | fill, SSDATA (encoded_contents)); | 12304 | SDATA(css), fill, |
| 12305 | SSDATA (encoded_contents)); | ||
| 12303 | } | 12306 | } |
| 12304 | 12307 | ||
| 12305 | /* Now we parse the wrapped version. */ | 12308 | /* Now we parse the wrapped version. */ |
| @@ -12321,13 +12324,6 @@ svg_load_image (struct frame *f, struct image *img, char *contents, | |||
| 12321 | 12324 | ||
| 12322 | rsvg_handle_set_dpi_x_y (rsvg_handle, FRAME_DISPLAY_INFO (f)->resx, | 12325 | rsvg_handle_set_dpi_x_y (rsvg_handle, FRAME_DISPLAY_INFO (f)->resx, |
| 12323 | FRAME_DISPLAY_INFO (f)->resy); | 12326 | FRAME_DISPLAY_INFO (f)->resy); |
| 12324 | |||
| 12325 | #if LIBRSVG_CHECK_VERSION (2, 48, 0) | ||
| 12326 | /* Set the CSS for the wrapped SVG. See the comment above the | ||
| 12327 | previous use of 'css'. */ | ||
| 12328 | rsvg_handle_set_stylesheet (rsvg_handle, (guint8 *) SDATA (lcss), | ||
| 12329 | SBYTES (lcss), NULL); | ||
| 12330 | #endif | ||
| 12331 | #else | 12327 | #else |
| 12332 | /* Make a handle to a new rsvg object. */ | 12328 | /* Make a handle to a new rsvg object. */ |
| 12333 | rsvg_handle = rsvg_handle_new (); | 12329 | rsvg_handle = rsvg_handle_new (); |