bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#56182: 28.1; Display of SVG file with transparent background is inco


From: Pascal Quesseveur
Subject: bug#56182: 28.1; Display of SVG file with transparent background is incorrect
Date: Tue, 28 Jun 2022 20:38:11 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (windows-nt)

>"AT" == Alan Third <alan@idiocy.org> a écrit :

  AT> I can't reproduce it.

>From what I understand the way to deal with background transparency of
SVG images has changed in version 28.1 in function svg_load_image
(comments about opacity are still there but I think they are
irrelevant). Now the SVG image is encapsulated in another SVG image in
which a rect element is defined with the background color of the
image.

I don't know why it doesn't work on the W10 computers I work on. I
don't know if the problem comes from this modificatino either. It
seems to me that the displayed color is BGR instead of RGB and the
screen gamma correction is not applied. For what it's worth I managed
to work around the problem in lisp. With version 27.1 I had
implemented an advice to improve the taking into account of the
background color in the style of SVG file. So I extended that advice
to solve my problem.

(defun qsr/create-image (orig-fun file-or-data &optional type data-p &rest 
props)
  "Advice around create-image. When image type is SVG, look for a
background color in SVG style and define this color as background
property in returned image.

When emacs 28 on Windows correct background color."
  (let ((data-format
         (and data-p (or (plist-get props :format) t))))
    (setq type (ignore-error unknown-image-type
                 (image-type file-or-data type data-format)))
    (let ((image (apply orig-fun file-or-data type data-p props))
          color)
      (when image
        ;; Regexp to match
        ;; <svg xmlns="http://www"; style="width:566px;background:#dddddd;"
        ;; or
        ;; <style ..>...background-color::#dddddd;...</style>
        (if (and (eq type 'svg) data-p
                 (or (string-match
                      
"<svg[^>]*\\<style=\"[^\"]*\\<background[^:]*:\\([^;]*\\);"
                      file-or-data)
                     (string-match
                      "<style[^<]*background-color: *\\([^;]*\\);"
                      file-or-data)))
            (setq color (match-string 1 file-or-data))
          ;; No color defined in svg element, use default background.
          (setq color (face-attribute 'default :background))
          (when (and (= emacs-major-version 28)
                     (eq system-type 'windows-nt))
            ;; When emacs 28 transform color.
            (let ((crgb (color-values color))
                  (gamma (frame-parameter nil 'screen-gamma)))
              (when crgb
                (let ((r (elt crgb 0))
                      (g (elt crgb 1))
                      (b (elt crgb 2)))
                  ;; If screen gamma correction is defined in frame
                  ;; parameters, inverse gamma correction.
                  (when gamma
                    (let ((invgc (* gamma 0.45445)))
                      ;; are r g b always 2 bytes?
                      (setq r (* (expt (/ r 65535.0) invgc) 65535.0)
                            g (* (expt (/ g 65535.0) invgc) 65535.0)
                            b (* (expt (/ b 65535.0) invgc) 65535.0))))
                  ;; color is defined as BGR.
                  (setq color (format "#%4X%4X%4X" b g r)))
                )))
          )
        (setq image (nconc image (list :background color))))
      image)))
    
(advice-add 'create-image :around #'qsr/create-image)


-- 
Pascal Quesseveur
pquessev@gmail.com





reply via email to

[Prev in Thread] Current Thread [Next in Thread]