8000 Correct URL area with rotated texts in PDFs by eindH · Pull Request #23288 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Correct URL area with rotated texts in PDFs #23288

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

Merged
merged 27 commits into from
Jun 23, 2022
Merged
Changes from 11 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
02c7ae2
Refactor URL handling
oscargus Jun 16, 2022
afdda57
Init
eindH Jun 16, 2022
d5dda49
Fixed lint issues
eindH Jun 16, 2022
7620ed8
Calculates vertices
eindH Jun 16, 2022
13b48f7
Vertices of outlining rectangle
eindH Jun 16, 2022
8ef3740
Correct trigonometry
eindH Jun 16, 2022
ed8b1ca
Update lib/matplotlib/backends/backend_pdf.py
eindH Jun 16, 2022
c47e089
Update lib/matplotlib/backends/backend_pdf.py
eindH Jun 16, 2022
31f9b3b
Update lib/matplotlib/backends/backend_pdf.py
eindH Jun 16, 2022
6da81cd
Update lib/matplotlib/backends/backend_pdf.py
eindH Jun 16, 2022
9eae986
Changed variable name
eindH Jun 16, 2022
0ce7e87
Update lib/matplotlib/backends/backend_pdf.py
eindH Jun 16, 2022
760267b
Spellcheck error
eindH Jun 16, 2022
a99c3ec
Added negative sign and compute sin and cos once
eindH Jun 17, 2022
336c070
Added tests
Jun 17, 2022
4c193bc
Lint errors
eindH Jun 17, 2022
076fc32
Lint
eindH Jun 17, 2022
4b3fa6b
New feature documentation
eindH Jun 18, 2022
467a876
Lint
eindH Jun 18, 2022
649779b
Fixed tests
oscargus Jun 18, 2022
0e72d7a
Merge pull request #1 from oscargus/urlrotationfixes
eindH Jun 18, 2022
8c97b61
Update lib/matplotlib/backends/backend_pdf.py
eindH Jun 18, 2022
ff03152
Update lib/matplotlib/backends/backend_pdf.py
eindH Jun 18, 2022
d9cbfbc
Update lib/matplotlib/backends/backend_pdf.py
eindH Jun 18, 2022
e2b8ca0
Update lib/matplotlib/backends/backend_pdf.py
eindH Jun 18, 2022
c9b4f26
Update lib/matplotlib/tests/test_backend_pdf.py
eindH Jun 18, 2022
7600521
Lint
eindH Jun 18, 2022
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
90 changes: 57 additions & 33 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,57 @@ def _datetime_to_pdf(d):
return r


def _calculate_quad_point_coordinates(x, y, width, height, angle=0):
"""
Uses matrix maths to calculate the coordinates of a
rectangle when rotated by angle around x, y
"""

angle = math.radians(angle)
a = x + height * math.sin(angle)
b = y + height * math.cos(angle)
c = x + width * math.cos(angle) + height * math.sin(angle)
d = y - width * math.sin(angle) + height * math.cos(angle)
e = x + width * math.cos(angle)
f = y - width * math.sin(angle)
return ((x, y), (a, b), (c, d), (e, f))


def _get_coordinates_of_block(x, y, width, height, angle=0):
"""
Get the coordinates of a rectange that contains the URL text.
This is for use in PDF < v1.6
"""

vertices = _calculate_quad_point_coordinates(x, y, width,
height, angle)

min_x = min(vertices[0][0], vertices[1][0], vertices[2][0], vertices[3][0])
min_y = min(vertices[0][1], vertices[1][1], vertices[2][1], vertices[3][1])
max_x = max(vertices[0][0], vertices[1][0], vertices[2][0], vertices[3][0])
max_y = max(vertices[0][1], vertices[1][1], vertices[2][1], vertices[3][1])
return vertices, (min_x, min_y, max_x, max_y)


def _get_link_annotation(gc, x, y, width, height, angle=0):
"""
Create a link annotation object for embedding URLs.
"""
quadpoints, rect = _get_coordinates_of_block(x, y, width, height, angle)
link_annotation = {
'Type': Name('Annot'),
'Subtype': Name('Link'),
'Rect': rect,
'QuadPoint': quadpoints,
'Border': [0, 0, 0],
'A': {
'S': Name('URI'),
'URI': gc.get_url(),
},
}
return link_annotation


def pdfRepr(obj):
"""Map Python objects to PDF syntax."""

Expand Down Expand Up @@ -2154,17 +2205,8 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
self._text2path.mathtext_parser.parse(s, 72, prop)

if gc.get_url() is not None:
link_annotation = {
'Type': Name('Annot'),
'Subtype': Name('Link'),
'Rect': (x, y, x + width, y + height),
'Border': [0, 0, 0],
'A': {
'S': Name('URI'),
'URI': gc.get_url(),
},
}
self.file._annotations[-1][1].append(link_annotation)
self.file._annotations[-1][1].append(_get_link_annotation(
gc, x, y, width, height, angle))

fonttype = mpl.rcParams['pdf.fonttype']

Expand Down Expand Up @@ -2220,17 +2262,8 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
page, = dvi

if gc.get_url() is not None:
link_annotation = {
'Type': Name('Annot'),
'Subtype': Name('Link'),
'Rect': (x, y, x + page.width, y + page.height),
'Border': [0, 0, 0],
'A': {
'S': Name('URI'),
'URI': gc.get_url(),
},
}
self.file._annotations[-1][1].append(link_annotation)
self.file._annotations[-1][1].append(_get_link_annotation(
gc, x, y, page.width, page.height, angle))

# Gather font information and do some setup for combining
# characters into strings. The variable seq will contain a
Expand Down Expand Up @@ -2330,17 +2363,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
if gc.get_url() is not None:
font.set_text(s)
width, height = font.get_width_height()
link_annotation = {
'Type': Name('Annot'),
'Subtype': Name('Link'),
'Rect': (x, y, x + width / 64, y + height / 64),
'Border': [0, 0, 0],
'A': {
'S': Name('URI'),
'URI': gc.get_url(),
},
}
self.file._annotations[-1][1].append(link_annotation)
self.file._annotations[-1][1].append(_get_link_annotation(
gc, x, y, width / 64, height / 64, angle))

# If fonttype is neither 3 nor 42, emit the whole string at once
# without manual kerning.
Expand Down
0