Skip to content

Figure out how to pass Text to Renderer.draw_text always #30282

Open
@QuLogic

Description

@QuLogic

For font features/languages, I ran into an issue in that the Text object is only supplied to Renderer.draw_text if the string is not multiline:

mtext = self if len(info) == 1 else None

I tried a small change:

diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py
index 3b0de58814..b255a93c52 100644
--- a/lib/matplotlib/text.py
+++ b/lib/matplotlib/text.py
@@ -800,9 +800,7 @@ class Text(Artist):
 
             angle = self.get_rotation()
 
             for line, wh, x, y in info:
-
-                mtext = self if len(info) == 1 else None
                 x = x + posx
                 y = y + posy
                 if renderer.flipy():
@@ -816,14 +814,19 @@ class Text(Artist):
                 else:
                     textrenderer = renderer
 
-                if self.get_usetex():
-                    textrenderer.draw_tex(gc, x, y, clean_line,
-                                          self._fontproperties, angle,
-                                          mtext=mtext)
-                else:
-                    textrenderer.draw_text(gc, x, y, clean_line,
-                                           self._fontproperties, angle,
-                                           ismath=ismath, mtext=mtext)
+                xt, yt = self.get_transform().inverted().transform((x, y))
+                with cbook._setattr_cm(self, _x=xt, _y=yt, _text=clean_line,
+                                       convert_xunits=lambda x: x,
+                                       convert_yunits=lambda y: y,
+                                       _horizontalalignment='left', _verticalalignment='bottom'):
+                    if self.get_usetex():
+                        textrenderer.draw_tex(gc, x, y, clean_line,
+                                              self._fontproperties, angle,
+                                              mtext=self)
+                    else:
+                        textrenderer.draw_text(gc, x, y, clean_line,
+                                               self._fontproperties, angle,
+                                               ismath=ismath, mtext=self)
 
         gc.restore()
         renderer.close_group('text')

AFAICT, only the PGF backend uses mtext and under certain conditions will place the text using the original position and alignment instead of the x/y passed to draw_text. Unfortunately, these don't seem to match (I assume the x/y passed in accounts for the descenders and other flourishes), so this breaks the PGF tests. I wonder if there's a better condition to be put in here:

if mtext and (
(angle == 0 or
mtext.get_rotation_mode() == "anchor") and
mtext.get_verticalalignment() != "center_baseline"):

Originally posted by @QuLogic in #29695 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Waiting for other PR

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy