org-latex-preview: Strip redundant color switching

* lisp/org-latex-preview.el (org-latex-preview--create,
org-latex-preview--create-tex-file): Keep track of the last fragment's
foreground/background color, and only include LaTeX color specifications
as needed.
(org-latex-preview-create-image-async): Provide the processing type so
`org-latex-preview--create-tex-file' can change the style of color
setting as appropriate.  This allows the dvipng route to use postscript
special statements instead of regular color declarations.
This commit is contained in:
TEC 2023-01-04 01:35:22 +08:00
parent a3087aba81
commit b77f8b6e12
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 27 additions and 8 deletions

View File

@ -811,7 +811,7 @@ Some of the options can be changed using the variable
(let* ((processing-info (let* ((processing-info
(cdr (assq processing-type org-latex-preview-process-alist))) (cdr (assq processing-type org-latex-preview-process-alist)))
(imagetype (or (plist-get processing-info :image-output-type) "png")) (imagetype (or (plist-get processing-info :image-output-type) "png"))
fragment-info) fragment-info prev-fg prev-bg)
(save-excursion (save-excursion
(dolist (element elements) (dolist (element elements)
(let* ((beg (org-element-property :begin element)) (let* ((beg (org-element-property :begin element))
@ -843,15 +843,21 @@ Some of the options can be changed using the variable
bg)))) bg))))
(options (org-combine-plists (options (org-combine-plists
org-latex-preview-options org-latex-preview-options
(list :foreground fg :background bg)))) (list :foreground fg
:background bg
:continue-color
(and (equal prev-bg bg)
(equal prev-fg fg))))))
(if-let ((path-info (org-latex-preview--get-cached hash))) (if-let ((path-info (org-latex-preview--get-cached hash)))
(org-latex-preview--update-overlay (org-latex-preview--update-overlay
(org-latex-preview--make-overlay beg end) (org-latex-preview--make-overlay beg end)
path-info) path-info)
(push (list :string (org-latex-preview--tex-styled value options) (push (list :string (org-latex-preview--tex-styled
processing-type value options)
:overlay (org-latex-preview--make-overlay beg end) :overlay (org-latex-preview--make-overlay beg end)
:key hash) :key hash)
fragment-info))))) fragment-info))
(setq prev-fg fg prev-bg bg))))
(when fragment-info (when fragment-info
(org-latex-preview-create-image-async (org-latex-preview-create-image-async
processing-type processing-type
@ -884,7 +890,8 @@ during processing to hold more information on the fragments."
'face 'org-latex-preview-processing-face)) 'face 'org-latex-preview-processing-face))
(let* ((extended-info (let* ((extended-info
(append processing-info (append processing-info
(list :fragments fragments-info (list :processor processing-type
:fragments fragments-info
:org-buffer (current-buffer) :org-buffer (current-buffer)
:texfile (org-latex-preview--create-tex-file :texfile (org-latex-preview--create-tex-file
processing-info fragments-info)))) processing-info fragments-info))))
@ -1337,8 +1344,11 @@ process."
header-file (plist-get processing-info :latex-precompiler) header-file (plist-get processing-info :latex-precompiler)
"fmt"))))) "fmt")))))
(defun org-latex-preview--tex-styled (value options &optional html-p) (defun org-latex-preview--tex-styled (processing-type value options &optional html-p)
"Apply LaTeX style commands to VALUE based on OPTIONS. "Apply LaTeX style commands to VALUE based on OPTIONS.
If PROCESSING-TYPE is dvipng, the colours are set with DVI
\"\\special\" commands instead of \"\\color\" and
\"\\pagecolor\".
VALUE is the math fragment text to be previewed. VALUE is the math fragment text to be previewed.
@ -1354,8 +1364,17 @@ HTML-P, if true, uses colors required for HTML processing."
('default (org-latex-preview--attr-color :background)) ('default (org-latex-preview--attr-color :background))
("Transparent" nil) ("Transparent" nil)
(bg (org-latex-preview--format-color bg))))) (bg (org-latex-preview--format-color bg)))))
(concat (and bg (format "\\pagecolor[rgb]{%s}" bg)) (concat (and (not (plist-get options :continue-color))
(and fg (format "\\color[rgb]{%s}" fg)) (if (eq processing-type 'dvipng)
(concat (and fg (format "\\special{color rgb %s}"
(subst-char-in-string
?, ?\s fg)))
(and bg (format "\\special{background rgb %s}"
(subst-char-in-string
?, ?\s bg))))
(concat
(and bg (format "\\pagecolor[rgb]{%s}" bg))
(and fg (format "\\color[rgb]{%s}" fg)))))
"%\n" "%\n"
value))) value)))