]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/lyxpreview2bitmap.py
*lib/scripts/layout2layout.py: Fix section labelling
[lyx.git] / lib / scripts / lyxpreview2bitmap.py
index 7be31537bf4f11ecb371feebf1f716117e35bf4f..245848fee230bfd3c4d7b422ca9f1908d5e42a62 100755 (executable)
@@ -78,25 +78,41 @@ def make_texcolor(hexcolor):
 
 def extract_metrics_info(dvipng_stdout, metrics_file):
     metrics = open(metrics_file, 'w')
-    metrics_re = re.compile("\[([0-9]+) depth=(-?[0-9]+) height=(-?[0-9]+)")
+# "\[[0-9]+" can match two kinds of numbers: page numbers from dvipng
+# and glyph numbers from mktexpk. The glyph numbers always match
+# "\[[0-9]+\]" while the page number never is followed by "\]". Thus:
+    page_re = re.compile("\[([0-9]+)[^]]");
+    metrics_re = re.compile("depth=(-?[0-9]+) height=(-?[0-9]+)")
 
     success = 0
+    page = ""
     pos = 0
     while 1:
+        match = page_re.search(dvipng_stdout, pos)
+        if match == None:
+            break
+        page = match.group(1)
+        pos = match.end()
         match = metrics_re.search(dvipng_stdout, pos)
         if match == None:
             break
         success = 1
 
         # Calculate the 'ascent fraction'.
-        descent = string.atof(match.group(2))
-        ascent  = string.atof(match.group(3))
+        descent = string.atof(match.group(1))
+        ascent  = string.atof(match.group(2))
+
         frac = 0.5
-        if abs(ascent + descent) > 0.1:
-            frac = ascent / (ascent + descent)
+        if ascent >= 0 or descent >= 0:
+            if abs(ascent + descent) > 0.1:
+                frac = ascent / (ascent + descent)
+
+           # Sanity check
+            if frac < 0:
+                frac = 0.5
 
-        metrics.write("Snippet %s %f\n" % (match.group(1), frac))
-        pos = match.end(3) + 2
+        metrics.write("Snippet %s %f\n" % (page, frac))
+        pos = match.end() + 2
 
     return success
 
@@ -107,7 +123,7 @@ def convert_to_ppm_format(pngtopnm, basename):
     for png_file in glob.glob("%s*.png" % basename):
         ppm_file = png_file_re.sub(".ppm", png_file)
 
-        p2p_cmd = "'%s' '%s'" % (pngtopnm, png_file)
+        p2p_cmd = '%s "%s"' % (pngtopnm, png_file)
         p2p_status, p2p_stdout = run_command(p2p_cmd)
         if p2p_status != None:
             error("Unable to convert %s to ppm format" % png_file)
@@ -133,7 +149,7 @@ def main(argv):
     bg_color = make_texcolor(argv[5])
 
     # External programs used by the script.
-    path = string.split(os.getenv("PATH"), os.pathsep)
+    path = string.split(os.environ["PATH"], os.pathsep)
     latex = find_exe_or_terminate(["pplatex", "latex2e", "latex"], path)
 
     # This can go once dvipng becomes widespread.
@@ -154,7 +170,7 @@ def main(argv):
         pngtopnm = find_exe_or_terminate(["pngtopnm"], path)
 
     # Compile the latex file.
-    latex_call = "'%s' '%s'" % (latex, latex_file)
+    latex_call = '%s "%s"' % (latex, latex_file)
 
     latex_status, latex_stdout = run_command(latex_call)
     if latex_status != None:
@@ -163,7 +179,7 @@ def main(argv):
 
     # Run the dvi file through dvipng.
     dvi_file = latex_file_re.sub(".dvi", latex_file)
-    dvipng_call = "'%s' -Ttight -depth -height -D %d -fg '%s' -bg '%s' '%s'" \
+    dvipng_call = '%s -Ttight -depth -height -D %d -fg "%s" -bg "%s" "%s"' \
                   % (dvipng, dpi, fg_color, bg_color, dvi_file)
 
     dvipng_status, dvipng_stdout = run_command(dvipng_call)