#! /usr/bin/env python
-# -*- coding: iso-8859-1 -*-
+# -*- coding: utf-8 -*-
# file lyxpreview2bitmap.py
# This file is part of LyX, the document processor.
# author Angus Leeming
# with much advice from members of the preview-latex project:
# David Kastrup, dak@gnu.org and
-# Jan-Åke Larsson, jalar@mai.liu.se.
+# Jan-Åke Larsson, jalar@mai.liu.se.
# Full author contact details are available in file CREDITS
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