Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaebb3c5

Browse files
committed
Add URL support for images in PDF backend
1 parent51ce677 commitaebb3c5

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

‎lib/matplotlib/backends/backend_pdf.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,12 +2012,13 @@ def draw_image(self, gc, x, y, im, transform=None):
20122012

20132013
self.check_gc(gc)
20142014

2015-
w=72.0*w/self.image_dpi
2016-
h=72.0*h/self.image_dpi
2017-
20182015
imob=self.file.imageObject(im)
20192016

20202017
iftransformisNone:
2018+
w=72.0*w/self.image_dpi
2019+
h=72.0*h/self.image_dpi
2020+
ifgc.get_url()isnotNone:
2021+
self._add_link_annotation(gc,x,y,w,h)
20212022
self.file.output(Op.gsave,
20222023
w,0,0,h,x,y,Op.concat_matrix,
20232024
imob,Op.use_xobject,Op.grestore)
@@ -2038,6 +2039,10 @@ def draw_path(self, gc, path, transform, rgbFace=None):
20382039
gc.get_sketch_params())
20392040
self.file.output(self.gc.paint())
20402041

2042+
def_add_link_annotation(self,gc,x,y,width,height,angle=0):
2043+
self.file._annotations[-1][1].append(
2044+
_get_link_annotation(gc,x,y,width,height,angle))
2045+
20412046
defdraw_path_collection(self,gc,master_transform,paths,all_transforms,
20422047
offsets,offsetTrans,facecolors,edgecolors,
20432048
linewidths,linestyles,antialiaseds,urls,
@@ -2206,8 +2211,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
22062211
self._text2path.mathtext_parser.parse(s,72,prop)
22072212

22082213
ifgc.get_url()isnotNone:
2209-
self.file._annotations[-1][1].append(_get_link_annotation(
2210-
gc,x,y,width,height,angle))
2214+
self._add_link_annotation(gc,x,y,width,height,angle)
22112215

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

@@ -2263,8 +2267,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
22632267
page,=dvi
22642268

22652269
ifgc.get_url()isnotNone:
2266-
self.file._annotations[-1][1].append(_get_link_annotation(
2267-
gc,x,y,page.width,page.height,angle))
2270+
self._add_link_annotation(gc,x,y,page.width,page.height,angle)
22682271

22692272
# Gather font information and do some setup for combining
22702273
# characters into strings. The variable seq will contain a
@@ -2364,8 +2367,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23642367
ifgc.get_url()isnotNone:
23652368
font.set_text(s)
23662369
width,height=font.get_width_height()
2367-
self.file._annotations[-1][1].append(_get_link_annotation(
2368-
gc,x,y,width/64,height/64,angle))
2370+
self._add_link_annotation(gc,x,y,width/64,height/64,angle)
23692371

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

‎lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,54 @@ def test_kerning():
360360
fig.text(0,.75,s,size=20)
361361

362362

363+
deftest_image_url():
364+
pikepdf=pytest.importorskip('pikepdf')
365+
366+
image_url='https://test_image_urls.matplotlib.org/'
367+
image_url2='https://test_image_urls2.matplotlib.org/'
368+
369+
X,Y=np.meshgrid(np.arange(-5,5,1),np.arange(-5,5,1))
370+
Z=np.sin(Y**2)
371+
fig,ax=plt.subplots()
372+
ax.imshow(Z,extent=[0,1,0,1],url=image_url)
373+
withio.BytesIO()asfd:
374+
fig.savefig(fd,format="pdf")
375+
withpikepdf.Pdf.open(fd)aspdf:
376+
annots=pdf.pages[0].Annots
377+
378+
# Iteration over Annots must occur within the context manager,
379+
# otherwise it may fail depending on the pdf structure.
380+
annot=next(
381+
(aforainannotsifa.A.URI==image_url),
382+
None)
383+
assertannotisnotNone
384+
# Positions in points (72 per inch.)
385+
assertannot.Rect== [decimal.Decimal('122.4'),
386+
decimal.Decimal('43.2'),
387+
468,
388+
decimal.Decimal('388.8')]
389+
ax.set_xlim(0,3)
390+
ax.imshow(Z[::-1],extent=[2,3,0,1],url=image_url2)
391+
# Must save as separate images
392+
plt.rcParams['image.composite_image']=False
393+
withio.BytesIO()asfd:
394+
fig.savefig(fd,format="pdf")
395+
withpikepdf.Pdf.open(fd)aspdf:
396+
annots=pdf.pages[0].Annots
397+
398+
# Iteration over Annots must occur within the context manager,
399+
# otherwise it may fail depending on the pdf structure.
400+
annot=next(
401+
(aforainannotsifa.A.URI==image_url2),
402+
None)
403+
assertannotisnotNone
404+
# Positions in points (72 per inch.)
405+
assertannot.Rect== [decimal.Decimal('369.6'),
406+
decimal.Decimal('141.6'),
407+
decimal.Decimal('518.64'),
408+
decimal.Decimal('290.64')]
409+
410+
363411
deftest_glyphs_subset():
364412
fpath=str(_get_data_path("fonts/ttf/DejaVuSerif.ttf"))
365413
chars="these should be subsetted! 1234567890"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp