Skip to content

Commit 02c7ae2

Browse files
committed
Refactor URL handling
1 parent 00cdf28 commit 02c7ae2

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

lib/matplotlib/backends/backend_pdf.py

+23-33
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

@@ -2154,17 +2171,8 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
21542171
self._text2path.mathtext_parser.parse(s, 72, prop)
21552172

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

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

@@ -2220,17 +2228,8 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
22202228
page, = dvi
22212229

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

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

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

0 commit comments

Comments
 (0)