[LyX/master] Redeem customizable background in lyxpreview2bitmap.py

Koji Yokota <[email protected]> Fri, 24 Jul 2026 05:46:41 +0000
Newsgroups gmane.editors.lyx.cvs
Message-ID <[email protected]>
commit f4d527086d60a153555e0fd606fb76d47c235d45
Author: Koji Yokota <[email protected]>
Date:   Fri Jul 24 14:43:16 2026 +0900

    Redeem customizable background in lyxpreview2bitmap.py
---
 lib/scripts/legacy_lyxpreview2ppm.py | 45 +++++++++++++++++++++++++++++++++---
 lib/scripts/lyxpreview2bitmap.py     | 16 +++++++++----
 2 files changed, 53 insertions(+), 8 deletions(-)

diff --git a/lib/scripts/legacy_lyxpreview2ppm.py b/lib/scripts/legacy_lyxpreview2ppm.py
index 575a34be5a..5d6e00ab1f 100644
--- a/lib/scripts/legacy_lyxpreview2ppm.py
+++ b/lib/scripts/legacy_lyxpreview2ppm.py
@@ -202,6 +202,8 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
     use_polyglossia_re = re.compile(b"\\s*\\\\usepackage{polyglossia}")
     use_preview_re = re.compile(b"\\s*\\\\usepackage\\[([^]]+)\\]{preview}")
     fg_color_gr = make_texcolor(fg_color, True)
+    if bg_color != "transparent":
+        bg_color_gr = make_texcolor(bg_color, True)
 
     tmp = tempfile.TemporaryFile()
 
@@ -230,7 +232,16 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
                 tmp.write(line)
                 continue
             else:
-                tmp.write(b"""
+                if bg_color != "transparent":
+                    tmp.write(b"""
+\\usepackage{color}
+\\definecolor{lyxfg}{rgb}{%s}
+\\definecolor{lyxbg}{rgb}{%s}
+\\pagecolor{lyxbg}
+\\usepackage{polyglossia}
+""" % (fg_color_gr, bg_color_gr))
+                else:
+                    tmp.write(b"""
 \\usepackage{color}
 \\definecolor{lyxfg}{rgb}{%s}
 \\usepackage{polyglossia}
@@ -241,7 +252,20 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
         # Preview options: add the options lyx and tightpage
         previewopts = match.group(1)
         if not polyglossia:
-            tmp.write(b"""
+            if bg_color != "transparent":
+                tmp.write(b"""
+\\usepackage{color}
+\\definecolor{lyxfg}{rgb}{%s}
+\\definecolor{lyxbg}{rgb}{%s}
+\\pagecolor{lyxbg}
+\\makeatletter
+\\def\\@tempa{cmr}
+\\ifx\\f@family\\@tempa
+  \\IfFileExists{lmodern.sty}{\\usepackage{lmodern}}{\\usepackage{ae,aecompl}}
+\\fi
+""" % (fg_color_gr, bg_color_gr))
+            else:
+                tmp.write(b"""
 \\usepackage{color}
 \\definecolor{lyxfg}{rgb}{%s}
 \\makeatletter
@@ -253,6 +277,9 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
         tmp.write(b"""
 \\usepackage[%s,tightpage]{preview}
 \\makeatletter
+""" % previewopts)
+        if bg_color != "transparent":
+            tmp.write(b"""
 \\ifdefined\\AddToHook
   \\AddToHook{env/preview/before}{\\leavevmode\\begingroup\\special{ps::clippath fill}}
   \\AddToHook{env/preview/after}{\\endgroup}
@@ -260,6 +287,18 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
   \\g@addto@macro\\preview{\\leavevmode\\begingroup\\special{ps::clippath fill}}
   \\g@addto@macro\\endpreview{\\endgroup}
 \\fi
+""")
+        else:
+            tmp.write(b"""
+\\ifdefined\\AddToHook
+  \\AddToHook{env/preview/before}{\\leavevmode\\begingroup\\color{lyxbg}\\special{background \\current@color}\\special{ps::clippath fill}}
+  \\AddToHook{env/preview/after}{\\endgroup}
+\\else
+  \\g@addto@macro\\preview{\\leavevmode\\begingroup\\color{lyxbg}\\special{background \\current@color}\\special{ps::clippath fill}}
+  \\g@addto@macro\\endpreview{\\endgroup}
+\\fi
+""")
+        tmp.write(b"""
 \\g@addto@macro\\preview{\\color{lyxfg}}
 \\let\\pr@set@pagerightoffset\\@empty
 \\ifx\\pagerightoffset\\@undefined\\else
@@ -272,7 +311,7 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
 \\fi
 \\g@addto@macro\\pr@ship@end{\\pr@set@pagerightoffset}
 \\makeatother
-""" % previewopts)
+""")
     if success:
         copyfileobj(tmp, open(latex_file,"wb"), 1)
 
diff --git a/lib/scripts/lyxpreview2bitmap.py b/lib/scripts/lyxpreview2bitmap.py
index 1542af1663..497bf66c15 100755
--- a/lib/scripts/lyxpreview2bitmap.py
+++ b/lib/scripts/lyxpreview2bitmap.py
@@ -39,7 +39,8 @@
 #   --dpi=<res>:   A scale factor, used to ascertain the resolution of the
 #                  generated image which is then passed to gs.
 #   --fg=<color>:  The foreground color as a hexadecimal string, eg '000000'.
-#   --bg=<color>:  The background color as a hexadecimal string, eg 'faf0e6'.
+#   --bg=<color>:  The background color as a hexadecimal string, eg 'faf0e6' or
+#                  keyword 'transparent' for transparent background.
 #   --latex=<exe>: The converter for latex files. Default is latex.
 #   --bibtex=<exe>: The converter for bibtex files. Default is bibtex.
 #   --lilypond:    Preprocess through lilypond-book. Default is false.
@@ -94,7 +95,7 @@ Options:
   --dpi=<res>:   Resolution per inch (default: 128)
   --png, --ppm:  Select the output format (default: png)
   --fg=<color>:  Foreground color (default: black, ie '000000')
-  --bg=<color>:  Background color (default: white, ie 'ffffff')
+  --bg=<color>:  Background color (default: transparent)
   --latex=<exe>: Specify the executable for latex (default: latex)
   --bibtex=<exe>: Specify the executable for bibtex (default: bibtex)
   --lilypond:    Preprocess through lilypond-book (default: false)
@@ -314,7 +315,7 @@ def main(argv):
     # Set defaults.
     dpi = 128
     fg_color = "000000"
-    bg_color = "ffffff"
+    bg_color = "transparent"
     bibtex = None
     latex = None
     lilypond = False
@@ -331,6 +332,7 @@ def main(argv):
         error(f"{err}\n{usage(script_name)}")
 
     opts.reverse()
+
     for opt, val in opts:
         if opt in ("-h", "--help"):
             print(usage(script_name))
@@ -390,10 +392,14 @@ def main(argv):
     progress("File to process: %s" % input_path)
 
     fg_color = bytes(fg_color, 'ascii')
-    bg_color = bytes(bg_color, 'ascii')
+    if bg_color != "transparent":
+        bg_color = bytes(bg_color, 'ascii')
 
     fg_color_dvipng = make_texcolor(fg_color, False).decode('ascii')
-    bg_color_dvipng = "Transparent"
+    if bg_color == "transparent":
+        bg_color_dvipng = "Transparent"
+    else:
+        bg_color_dvipng = make_texcolor(bg_color, False).decode('ascii')
 
     # External programs used by the script.
     latex = find_exe_or_terminate(latex or latex_commands)
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs