Revision: 7120
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7120&view=rev
Author:   leejjoon
Date:     2009-05-18 19:19:33 +0000 (Mon, 18 May 2009)

Log Message:
-----------
Fix the linespacing bug of multiline text (#1239682)

Modified Paths:
--------------
    trunk/matplotlib/CHANGELOG
    trunk/matplotlib/examples/pylab_examples/multiline.py
    trunk/matplotlib/lib/matplotlib/text.py

Modified: trunk/matplotlib/CHANGELOG
===================================================================
--- trunk/matplotlib/CHANGELOG  2009-05-18 17:25:46 UTC (rev 7119)
+++ trunk/matplotlib/CHANGELOG  2009-05-18 19:19:33 UTC (rev 7120)
@@ -1,3 +1,6 @@
+2009-05-18 Fix the linespacing bug of multiline text (#1239682). See 
+           examples/pylab_examples/multiline.py -JJL
+
 2009-05-18 Add *annotation_clip* attr. for text.Annotation class.
            If True, annotation is only drawn when the annotated point is
            inside the axes area. -JJL

Modified: trunk/matplotlib/examples/pylab_examples/multiline.py
===================================================================
--- trunk/matplotlib/examples/pylab_examples/multiline.py       2009-05-18 
17:25:46 UTC (rev 7119)
+++ trunk/matplotlib/examples/pylab_examples/multiline.py       2009-05-18 
19:19:33 UTC (rev 7120)
@@ -1,15 +1,46 @@
 #!/usr/bin/env python
 from pylab import *
+#from matplotlib.pyplot import *
+#from numpy import arange
 
-plot(arange(10))
-xlabel('this is a xlabel\n(with newlines!)')
-ylabel('this is vertical\ntest', multialignment='center')
-#ylabel('this is another!')
-text(2, 7,'this is\nyet another test',
-     rotation=45,
-     horizontalalignment = 'center',
-     verticalalignment   = 'top',
-     multialignment      = 'center')
+if 1:
+    figure(figsize=(7, 4))
+    ax = subplot(121)
+    ax.set_aspect(1)
+    plot(arange(10))
+    xlabel('this is a xlabel\n(with newlines!)')
+    ylabel('this is vertical\ntest', multialignment='center')
+    #ylabel('this is another!')
+    text(2, 7,'this is\nyet another test',
+         rotation=45,
+         horizontalalignment = 'center',
+         verticalalignment   = 'top',
+         multialignment      = 'center')
 
-grid(True)
+    grid(True)
+
+
+
+    subplot(122)
+    
+    text(0.29, 0.7, "Mat\nTTp\n123", size=18,
+         va="baseline", ha="right", multialignment="left",
+         bbox=dict(fc="none"))
+
+    text(0.34, 0.7, "Mag\nTTT\n123", size=18,
+         va="baseline", ha="left", multialignment="left",
+         bbox=dict(fc="none"))
+
+    text(0.95, 0.7, "Mag\nTTT$^{A^A}$\n123", size=18,
+         va="baseline", ha="right", multialignment="left",
+         bbox=dict(fc="none"))
+
+    xticks([0.2, 0.4, 0.6, 0.8, 1.],
+           ["Jan\n2009","Feb\n2009","Mar\n2009", "Apr\n2009", "May\n2009"])
+    
+    axhline(0.7)
+    title("test line spacing for multiline text")
+
+subplots_adjust(bottom=0.25, top=0.8)
+draw()
 show()

Modified: trunk/matplotlib/lib/matplotlib/text.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/text.py     2009-05-18 17:25:46 UTC (rev 
7119)
+++ trunk/matplotlib/lib/matplotlib/text.py     2009-05-18 19:19:33 UTC (rev 
7120)
@@ -8,15 +8,15 @@
 
 from matplotlib import cbook
 from matplotlib import rcParams
-import artist
-from artist import Artist
-from cbook import is_string_like, maxdict
-from font_manager import FontProperties
-from patches import bbox_artist, YAArrow, FancyBboxPatch, \
+import matplotlib.artist as artist
+from matplotlib.artist import Artist
+from matplotlib.cbook import is_string_like, maxdict
+from matplotlib.font_manager import FontProperties
+from matplotlib.patches import bbox_artist, YAArrow, FancyBboxPatch, \
      FancyArrowPatch, Rectangle
-import transforms as mtransforms
-from transforms import Affine2D, Bbox
-from lines import Line2D
+import matplotlib.transforms as mtransforms
+from matplotlib.transforms import Affine2D, Bbox
+from matplotlib.lines import Line2D
 
 import matplotlib.nxutils as nxutils
 
@@ -227,6 +227,11 @@
         self._linespacing = other._linespacing
 
     def _get_layout(self, renderer):
+        """
+        return the extent (bbox) of the text together with
+        multile-alignment information. Note that it returns a extent
+        of a rotated text when necessary.
+        """
         key = self.get_prop_tup()
         if key in self.cached: return self.cached[key]
 
@@ -242,9 +247,9 @@
 
         # Find full vertical extent of font,
         # including ascenders and descenders:
-        tmp, heightt, bl = renderer.get_text_width_height_descent(
+        tmp, lp_h, lp_bl = renderer.get_text_width_height_descent(
                 'lp', self._fontproperties, ismath=False)
-        offsety = heightt * self._linespacing
+        offsety = lp_h * self._linespacing
 
         baseline = None
         for i, line in enumerate(lines):
@@ -254,8 +259,22 @@
             if baseline is None:
                 baseline = h - d
             whs[i] = w, h
-            horizLayout[i] = thisx, thisy, w, h
-            thisy -= offsety
+
+            # For general multiline text, we will have a fixed spacing
+            # between the "baseline" of the upper line and "top" of
+            # the lower line (instead of the "bottom" of the upper
+            # line and "top" of the lower line)
+
+            # For multiline text, increase the line spacing when the
+            # text net-height(excluding baseline) is larger than that
+            # of a "l" (e.g., use of superscripts), which seems
+            # what TeX does. 
+
+            d_yoffset = max(0, (h-d)-(lp_h-lp_bl))
+
+            horizLayout[i] = thisx, thisy-(d + d_yoffset), \
+                             w, h
+            thisy -= offsety + d_yoffset
             width = max(width, w)
 
         ymin = horizLayout[-1][1]
@@ -1688,3 +1707,4 @@
 
 
 artist.kwdocd['Annotation'] = Annotation.__init__.__doc__
+


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables 
unlimited royalty-free distribution of the report engine 
for externally facing server and web deployment. 
http://p.sf.net/sfu/businessobjects
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to