8000 Refactor URL handling in PDF backend by oscargus · Pull Request #23286 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Refactor URL handling in PDF backend #23286

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 1 commit into from
Jun 20, 2022
Merged
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
56 changes: 23 additions & 33 deletions lib/matplotlib/backends/backend_pdf.py
96BF
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,23 @@ def _datetime_to_pdf(d):
return r


def _get_link_annotation(gc, x, y, width, height):
"""
Create a link annotation object for embedding URLs.
"""
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(),
},
}
return link_annotation


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

Expand Down Expand Up @@ -2154,17 +2171,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))

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

Expand Down Expand Up @@ -2220,17 +2228,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))

# Gather font information and do some setup for combining
# characters into strings. The variable seq will contain a
Expand Down Expand Up @@ -2330,17 +2329,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))

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