8000 I changed the offset code for draw_text for RendererAgg fixing issue #13044 by safdarzareef · Pull Request #20057 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

I changed the offset code for draw_text for RendererAgg fixing issue #13044 #20057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
except ImportError:
import dummy_threading as threading
from contextlib import nullcontext
from math import radians, cos, sin
from math import radians, cos, sin, sqrt

import numpy as np
from PIL import Image
Expand Down Expand Up @@ -208,10 +208,13 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
xo, yo = font.get_bitmap_offset()
xo /= 64.0
yo /= 64.0
h = sqrt(xo**2 + yo**2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I prefer using hypot() instead, but I don't know if it really matters in the context of bounding box calculations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it will matter, but we could still change it.

xd = d * sin(radians(angle))
yd = d * cos(radians(angle))
xo = h * cos(radians(angle))
yo = h * sin(radians(angle))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to calculate radians(angle) four times, might as well just do it once and store it in a variable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, these four lines here would greatly benefit from a comment explaining the trigonometry. Usually, cos() goes with x, and y goes with sin(). Seeing it two different ways here for the offset and the descent is confusing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can explain the offset trigonometry, but I just left the code for the descent as it was before. The descent seems wrong - but I am unaware of the reason why it was like that before so I left it there. Basic trigonometry suggests that x components should be calculated by multiplying a number by the cosine of its angle and y components should be calculated by multiplying a number by the sine of its angle - so that is why I used that for the offset but I do think the descent should be in the same form too.

x = round(x + xo + xd)
y = round(y + yo + yd)
y = round(y + yo - yd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much like the trig above, I think this will also need a quick proof in the comments.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I have a proof for this at the moment except that in the draw_mathtext function the y is calculated in a similar manner and by subtracting the y offset we get a satisfactory result.

self._renderer.draw_text_image(font, x, y + 1, angle, gc)

def get_text_width_height_descent(self, s, prop, ismath):
Expand Down
0