]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/lyxpreview2bitmap.py
The way this was previously, it had to fail if the GUI language
[lyx.git] / lib / scripts / lyxpreview2bitmap.py
index a7d76236fba969b982b648e2a3859bf81db0ead3..95bb895380a0e4de7efdaa1585ca121e7b624c05 100755 (executable)
@@ -77,7 +77,7 @@
 
 import getopt, glob, os, re, shutil, string, sys
 
-from legacy_lyxpreview2ppm import legacy_conversion_step1
+from legacy_lyxpreview2ppm import extract_resolution, legacy_conversion_step1
 
 from lyxpreview_tools import bibtex_commands, check_latex_log, copyfileobj, \
      error, filter_pages, find_exe, find_exe_or_terminate, \
@@ -159,60 +159,28 @@ def extract_metrics_info(dvipng_stdout):
 
 
 def fix_latex_file(latex_file, pdf_output):
-    documentclass_re = re.compile("(\\\\documentclass\[)(1[012]pt,?)(.+)")
-    def_re = re.compile(r"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z])(.+)")
-    usepackage_re = re.compile("\\\\usepackage")
-    userpreamble_re = re.compile("User specified LaTeX commands")
-    enduserpreamble_re = re.compile("\\\\makeatother")
+    def_re = re.compile(r"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z]+)")
 
     tmp = mkstemp()
 
-    in_user_preamble = 0
-    usepkg = 0
-    changed = 0
+    changed = False
     macros = []
     for line in open(latex_file, 'r').readlines():
-        match = documentclass_re.match(line)
-        if match != None:
-            changed = 1
-            tmp.write("%s%s\n" % (match.group(1), match.group(3)))
-            continue
-
-        if not pdf_output and not usepkg:
-            if userpreamble_re.search(line) != None:
-                in_user_preamble = 1
-            elif enduserpreamble_re.search(line) != None:
-                in_user_preamble = 0
-            if usepackage_re.match(line) != None and in_user_preamble:
-                usepkg = 1
-                changed = 1
-                tmp.write("\\def\\t@a{microtype}\n")
-                tmp.write("\\let\\oldusepkg\\usepackage\n")
-                tmp.write("\\def\\usepackage{\\@ifnextchar[\\@usepkg{\\@usepkg[]}}\n")
-                tmp.write("\\def\\@usepkg[#1]#2{\\@ifnextchar[")
-                tmp.write("{\\@@usepkg[#1]{#2}}{\\@@usepkg[#1]{#2}[]}}\n")
-                tmp.write("\\def\@@usepkg[#1]#2[#3]{\\def\\t@b{#2}")
-                tmp.write("\\ifx\\t@a\\t@b\\else\\oldusepkg[#1]{#2}[#3]\\fi}\n")
-                tmp.write(line)
-                continue
-
-        match = def_re.match(line)
-        if match == None:
-            tmp.write(line)
-            continue
-
-        macroname = match.group(2)
-        if not macroname in macros:
-            macros.append(macroname)
-            tmp.write(line)
-            continue
-
-        definecmd = match.group(1)
-        if definecmd == "\\global\\long\\def":
-            tmp.write(line)
+        if not pdf_output and line.startswith("\\documentclass"):
+            changed = True
+            line += "\\PassOptionsToPackage{draft}{microtype}\n"
         else:
-            changed = 1
-            tmp.write("\\renewcommandx%s%s\n" % (match.group(2), match.group(3)))
+            match = def_re.match(line)
+            if match != None:
+                macroname = match.group(2)
+                if macroname in macros:
+                    definecmd = match.group(1)
+                    if definecmd == "\\newcommandx":
+                        changed = True
+                        line = line.replace(definecmd, "\\renewcommandx")
+                else:
+                    macros.append(macroname)
+        tmp.write(line)
 
     if changed:
         copyfileobj(tmp, open(latex_file,"wb"), 1)
@@ -432,8 +400,8 @@ def main(argv):
     progress("Preprocess through lilypond-book: %s" % lilypond)
     progress("Altering the latex file for font size and colors")
 
-    # Omit font size specification in latex file and make sure that multiple
-    # defined macros and the microtype package don't cause issues.
+    # Make sure that multiple defined macros and the microtype package
+    # don't cause issues in the latex file.
     fix_latex_file(latex_file, pdf_output)
 
     if lilypond:
@@ -478,9 +446,10 @@ def main(argv):
     # Compile the latex file.
     error_pages = []
     latex_status, latex_stdout = run_latex(latex, latex_file, bibtex)
+    latex_log = latex_file_re.sub(".log", latex_file)
     if latex_status:
         progress("Will try to recover from %s failure" % latex)
-        error_pages = check_latex_log(latex_file_re.sub(".log", latex_file))
+        error_pages = check_latex_log(latex_log)
 
     # The dvi output file name
     dvi_file = latex_file_re.sub(".dvi", latex_file)
@@ -516,9 +485,12 @@ def main(argv):
         return legacy_conversion_step1(latex_file, dpi, output_format, fg_color,
             bg_color, "pdflatex", True)
 
+    # Retrieve resolution
+    resolution = extract_resolution(latex_log, dpi)
+
     # Run the dvi file through dvipng.
     dvipng_call = '%s -Ttight -depth -height -D %d -fg "%s" -bg "%s" %s "%s"' \
-        % (dvipng, dpi, fg_color_dvipng, bg_color_dvipng, pages_parameter, dvi_file)
+        % (dvipng, resolution, fg_color_dvipng, bg_color_dvipng, pages_parameter, dvi_file)
     dvipng_status, dvipng_stdout = run_command(dvipng_call)
 
     if dvipng_status: