Here is a patch which uses a preview package. It uses a "showbox"
option in the preview package, with a slight tweak (this only patches
the texmanager.py. You still need to apply the agg backend patch in my
previous post). It would be good if this patch will be accepted, but
the extra requirement of the preview package may need some dicussion.
Although it seems that the preview package is commonly found with a
TeX installation, I guess it is not part of the major TeX distribution
(e.g. tetex, tex-live) yet. One way would be to make it as an optional
feature.
Regards,
-JJ
On Fri, Aug 29, 2008 at 12:04 PM, Jae-Joon Lee <[EMAIL PROTECTED]> wrote:
> Thanks,
>
> I quickly went through the code of the pngmath.py, and it seems that
> the depth(descent) of the dvi file is reported by "dvipng" (but the
> preview package must be used in the tex file for this to work
> correctly). Therefore, with this method, we need to run dvipng even if
> we use ps of pdf backend. Although this seems fine to me, I'll see if
> I can extract the depth of the text without running the dvipng.
>
> Regards,
>
> -JJ
>
>
>
>
> On Fri, Aug 29, 2008 at 7:59 AM, Michael Droettboom <[EMAIL PROTECTED]> wrote:
>> Sphinx contains one way to do this in its new "pngmath" extension. It uses
>> the LaTeX package "preview" which does all of this magic internally. And I
>> believe it's a little more general. If I recall, the approach you're taking
>> won't work with some LaTeX constructs such as:
>>
>> \begin{align}
>> x & = 2
>> y & = 2
>> \end{align}
>>
>> Plus, Sphinx is BSD-licensed, so it should be fine to copy-and-paste
>> whatever code is necessary.
>>
>> Of course, latex-preview is required to be installed, but I think it's a
>> pretty common package.
>>
>> See here:
>>
>> http://svn.python.org/projects/doctools/trunk/sphinx/ext/pngmath.py
>>
>> Cheers,
>> Mike
>>
>> Jae-Joon Lee wrote:
>>>
>>> On Thu, Aug 28, 2008 at 4:18 PM, John Hunter <[EMAIL PROTECTED]> wrote:
>>>
>>>>
>>>> On Thu, Aug 28, 2008 at 2:57 PM, Jae-Joon Lee <[EMAIL PROTECTED]>
>>>> wrote:
>>>>
>>>>
>>>>>
>>>>> First of all, I borrowed this idea from the PyX which is in GPL.
>>>>> Although there is little of copying, other than the basic idea, I'm
>>>>> not 100% sure if this could be BSD-compatible.
>>>>>
>>>>
>>>> I think it is fine to borrow the idea; what we need to do is a clean
>>>> room implementation with no copying. You can best answer that, so if
>>>> you tell us your patch is cleanly implemented, we can accept it.
>>>>
>>>> JDH
>>>>
>>>>
>>>
>>> Thanks for the response.
>>>
>>> Well, the only part I borrowed from PyX is TeX related commands they
>>> use (there is not much of implementation as far as TeX-related code is
>>> concerned). From their code, I learned the meaning and usage of the
>>> following TeX commands
>>>
>>> \newbox
>>> \setbox
>>> \immediate\write16
>>>
>>> And I used the same TeX commands in my code.
>>> But I personally think this is not a (code) copy.
>>>
>>> Other than this, the code is clean.
>>> Regards,
>>>
>>> -JJ
>>>
>>> -------------------------------------------------------------------------
>>> This SF.Net email is sponsored by the Moblin Your Move Developer's
>>> challenge
>>> Build the coolest Linux based applications with Moblin SDK & win great
>>> prizes
>>> Grand prize is a trip for two to an Open Source event anywhere in the
>>> world
>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
>>> _______________________________________________
>>> Matplotlib-devel mailing list
>>> Matplotlib-devel@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
>>>
>>
>>
>
Index: texmanager.py
===================================================================
--- texmanager.py (revision 6060)
+++ texmanager.py (working copy)
@@ -45,6 +45,7 @@
import matplotlib as mpl
from matplotlib import rcParams
from matplotlib._png import read_png
+import re
DEBUG = False
@@ -236,14 +237,30 @@
else:
unicode_preamble = ''
+
+
+ # A preview package is used with showbox option to report the
+ # height, depth and width of the box containg the given text.
+ # I'm not sure if we still need the geometry package.
+
+
s = r"""\documentclass{article}
%s
%s
%s
+\usepackage[active,showbox]{preview}
\usepackage[papersize={72in,72in}, body={70in,70in}, margin={1in,1in}]{geometry}
-\pagestyle{empty}
+
+%% we override the showbox command (which is excuted by shobox option
+%% in preview package) as it is treated as an error and makes the
+%% 'exitstatus' non-zero.
+
+\def\showbox#1{\immediate\write16{MatplotlibBox:(\the\ht#1+\the\dp#1)x\the\wd#1}}
+
\begin{document}
-\fontsize{%f}{%f}%s
+\begin{preview}
+{\fontsize{%f}{%f}%s}
+\end{preview}
\end{document}
""" % (self._font_preamble, unicode_preamble, custom_preamble,
fontsize, fontsize*1.25, tex)
@@ -262,6 +279,11 @@
return texfile
+
+ # a regular expression to extract the height, depth and width of the box
+ # e.g.) MatplotlibBox:(10.74074pt+0.0pt)x8.15964pt
+ _re_vbox = re.compile(r"MatplotlibBox:\(([\d.]+)pt\+([\d.]+)pt\)x([\d.]+)pt")
+
def make_dvi(self, tex, fontsize):
"""
generates a dvi file containing latex's layout of tex string
@@ -289,9 +311,16 @@
raise RuntimeError(('LaTeX was not able to process the following \
string:\n%s\nHere is the full report generated by LaTeX: \n\n'% repr(tex)) + report)
else: mpl.verbose.report(report, 'debug')
+
+ # find the box information from the latex output
+ # file and store them in a ".baseline" file
+ m = TexManager._re_vbox.search(report)
+ open(basefile+'.baseline',"w").write(" ".join(m.groups()))
+
for fname in glob.glob(basefile+'*'):
if fname.endswith('dvi'): pass
elif fname.endswith('tex'): pass
+ elif fname.endswith('baseline'): pass
else:
try: os.remove(fname)
except OSError: pass
@@ -441,3 +470,24 @@
self.rgba_arrayd[key] = Z
return Z
+
+
+ def get_text_width_height_descent(self, tex, fontsize):
+ """
+ return text size in point
+ """
+ basefile = self.get_basefile(tex, fontsize)
+ baselinefile = '%s.baseline'% basefile
+
+
+ if DEBUG or not os.path.exists(baselinefile):
+ dvifile = self.make_dvi(tex, fontsize)
+
+ # height, depth(= descent), width of the box.
+ l = open(baselinefile).read().split()
+ height, depth, width = [float(l1) for l1 in l]
+
+ # The height of the text reproted be the TeX is a height
+ # from the baseline, not from the bottom.
+
+ return width, height+depth, depth
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel