Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97fc115

Browse files
authored
Merge pull request #23286 from oscargus/pdfurlrefactor
Refactor URL handling in PDF backend
2 parents 72156e2 + 02c7ae2 commit 97fc115

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,23 @@ def _datetime_to_pdf(d):
250250
return r
251251

252252

253+
def _get_link_annotation(gc, x, y, width, height):
254+
"""
255+
Create a link annotation object for embedding URLs.
256+
"""
257+
link_annotation = {
258+
'Type': Name('Annot'),
259+
'Subtype': Name('Link'),
260+
'Rect': (x, y, x + width, y + height),
261+
'Border': [0, 0, 0],
262+
'A': {
263+
'S': Name('URI'),
264+
'URI': gc.get_url(),
265+
},
266+
}
267+
return link_annotation
268+
269+
253270
def pdfRepr(obj):
254271
"""Map Python objects to PDF syntax."""
255272

@@ -2156,17 +2173,8 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
21562173
self._text2path.mathtext_parser.parse(s, 72, prop)
21572174

21582175
if gc.get_url() is not None:
2159-
link_annotation = {
2160-
'Type': Name('Annot'),
2161-
'Subtype': Name('Link'),
2162-
'Rect': (x, y, x + width, y + height),
2163-
'Border': [0, 0, 0],
2164-
'A': {
2165-
'S': Name('URI'),
2166-
'URI': gc.get_url(),
2167-
},
2168-
}
2169-
self.file._annotations[-1][1].append(link_annotation)
2176+
self.file._annotations[-1][1].append(_get_link_annotation(
2177+
gc, x, y, width, height))
21702178

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

@@ -2222,17 +2230,8 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
22222230
page, = dvi
22232231

22242232
if gc.get_url() is not None:
2225-
link_annotation = {
2226-
'Type': Name('Annot'),
2227-
'Subtype': Name('Link'),
2228-
'Rect': (x, y, x + page.width, y + page.height),
2229-
'Border': [0, 0, 0],
2230-
'A': {
2231-
'S': Name('URI'),
2232-
'URI': gc.get_url(),
2233-
},
2234-
}
2235-
self.file._annotations[-1][1].append(link_annotation)
2233+
self.file._annotations[-1][1].append(_get_link_annotation(
2234+
gc, x, y, page.width, page.height))
22362235

22372236
# Gather font information and do some setup for combining
22382237
# characters into strings. The variable seq will contain a
@@ -2332,17 +2331,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23322331
if gc.get_url() is not None:
23332332
font.set_text(s)
23342333
width, height = font.get_width_height()
2335-
link_annotation = {
2336-
'Type': Name('Annot'),
2337-
'Subtype': Name('Link'),
2338-
'Rect': (x, y, x + width / 64, y + height / 64),
2339-
'Border': [0, 0, 0],
2340-
'A': {
2341-
'S': Name('URI'),
2342-
'URI': gc.get_url(),
2343-
},
2344-
}
2345-
self.file._annotations[-1][1].append(link_annotation)
2334+
self.file._annotations[-1][1].append(_get_link_annotation(
2335+
gc, x, y, width / 64, height / 64))
23462336

23472337
# If fonttype is neither 3 nor 42, emit the whole string at once
23482338
# without manual kerning.

0 commit comments

Comments
 (0)