From 1223971314fe3ebea5e6d4b7d116ebfade0091a4 Mon Sep 17 00:00:00 2001 From: TEC Date: Wed, 28 Dec 2022 10:38:39 +0800 Subject: [PATCH] org-latex-preview: use currentColor in SVGs * lisp/org-latex-preview.el (org-place-latex-image, org-create-formula-image-async, org--make-preview-overlay, org-latex-preview--dvisvgm-callback): Edit the dvisvgm generated SVGs to use "currentColor" instead of a hardcoded foreground color. To then set the color of the displayed images, set the overlay face as appropriate. (org-create-latex-preview): Now that the foreground is generalised, when producing the sha1 hash for an SVG image, the current foreground value should not affect it. --- lisp/org-latex-preview.el | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/lisp/org-latex-preview.el b/lisp/org-latex-preview.el index 03677e0de..d7d92aff5 100644 --- a/lisp/org-latex-preview.el +++ b/lisp/org-latex-preview.el @@ -298,7 +298,13 @@ as a string. It defaults to \"png\"." (delete-overlay o)))) (overlay-put ov 'display - (list 'image :type imagetype :file image :ascent 'center)))) + (list 'image :type imagetype :file image :ascent 'center)) + (if (eq imagetype 'svg) + (let ((face (or (and (> beg 1) + (get-text-property (1- beg) 'face)) + 'default))) + (overlay-put ov 'face face)) + (overlay-put ov 'face nil)))) (defun org-clear-latex-preview (&optional beg end) "Remove all overlays with LaTeX fragment images in current buffer. @@ -540,7 +546,10 @@ Some of the options can be changed using the variable org-latex-default-packages-alist org-latex-packages-alist org-format-latex-options - value fg bg)))) + value + (if (eq imagetype 'svg) + 'svg fg) + bg)))) (movefile (format "%s_%s.%s" absprefix hash imagetype)) (options (org-combine-plists org-format-latex-options @@ -584,13 +593,16 @@ MOVEFILES." (org-latex-preview--tex-compile-async extended-info)) (img-extract-async (org-latex-preview--image-extract-async extended-info))) + (if (eq processing-type 'dvisvgm) + (plist-put (cdr img-extract-async) :success + #'org-latex-preview--dvisvgm-callback) + (plist-put (cdr img-extract-async) :success + #'org-latex-preview--cleanup-callback)) (if (and (eq processing-type 'dvipng) (member "--follow" (car img-extract-async))) (apply #'org-async-call img-extract-async) (plist-put (cdr tex-compile-async) :success img-extract-async) (plist-put (cdr tex-compile-async) :failure img-extract-async)) - (plist-put (cdr img-extract-async) :success - #'org-latex-preview--cleanup-callback) (apply #'org-async-call tex-compile-async)))) (defun org-preview-latex--create-tex-file (processing-info preview-strings) @@ -732,6 +744,24 @@ The path of the created LaTeX file is returned." (when (file-exists-p (concat basename ext)) (delete-file (concat basename ext)))))) +(defun org-latex-preview--dvisvgm-callback (_exit-code _stdout extended-info) + "TODO" + (let* ((basename (file-name-base (plist-get extended-info :texfile))) + (svg-images + (directory-files (file-name-directory (plist-get extended-info :texfile)) + t (rx (literal basename) (* anything) ".svg")))) + (dolist (svg-file svg-images) + (with-temp-buffer + (insert-file-contents svg-file) + (goto-char (point-min)) + (when (re-search-forward "