Subject: Re: bug#77841: Some svg image's black color is replaced by emacs's theme foreground color

From: Alan Third <[email-masked]>
Date: Sun, 4 May 2025 19:11:33 +0100

On Sun, May 04, 2025 at 07:17:46PM +0200, Manuel Giraud wrote:
> Thanks Alan.  I've tested your patch and here is what I observe. What's
> good:
> 
>        - SVG Emacs "widgets" are still using the default face color and
>          are updating accordingly when changing theme
> 
>        - I use Gnus and I see the same behaviour for the SVG icon in the
>          modeline
> 
>        - The german flag example provided by Billy is displayed
>          correctly (filled with black where needed) whatever the theme
>          in use
> 
>        - The example from Ihor shows 'x²=2' written in white on black
>          whatever the theme in use

Thanks for checking.

> What's not so good:
> 
> I use DocView (with mupdf to generate SVG pages) and I have set
> 'doc-view-svg-face' to "inherit default" so that DocView buffers are
> also following the theme.  This used to work but now, with this patch,
> it does not render correctly.  For example, the text stays black instead
> of white with a dark background theme.
> 
> Maybe, it is something that should be fixed in DocView (?)

We could probably fix it by getting Docview to set the :css property
for the SVG image to something like:

    svg {
      fill: currentcolor;
    }

Is the background OK?

The attached might work, but only if you're using librsvg 2.48 or
above... I can think of a way to fix *that* problem, but it'll need a
little more thought.

-- 
Alan Third
>From 4410cfe1f81ff3fcb33de63acfa90c596081b392 Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Sun, 4 May 2025 19:08:00 +0100
Subject: [PATCH] Use css to set SVG foreground in docview

* lisp/doc-view.el (doc-view-insert-image): Use CSS to set the fill
attribute to the desired foreground color.
---
 lisp/doc-view.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index 0a6bfea22cb..d3e40e531f8 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -1677,7 +1677,7 @@ doc-view-insert-image
                             (setq args `(,@args :transform-smoothing t)))
                           (when (eq doc-view--image-type 'svg)
                             (setq args `(,@args :background ,(face-background 'doc-view-svg-face)
-                                                :foreground ,(face-foreground 'doc-view-svg-face))))
+                                                :css ,(concat "svg{fill:" (face-foreground 'doc-view-svg-face) "}"))))
 			  (apply #'create-image file doc-view--image-type nil args))))
 	     (slice (doc-view-current-slice))
 	     (img-width (and image (car (image-size image))))
-- 
2.45.2