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

Commitccaffe2

Browse files
committed
Added validation
Adjusted the error with the cacheThere was a bug you would get the same text even with different fontsets arguments because of the cache
1 parent38c362b commitccaffe2

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

‎lib/matplotlib/font_manager.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
frommatplotlibimportafm,cbook,ft2font,rcParams
4242
frommatplotlib.fontconfig_patternimport (
4343
parse_fontconfig_pattern,generate_fontconfig_pattern)
44+
frommatplotlib.rcsetupimport_validators
4445

4546
_log=logging.getLogger(__name__)
4647

@@ -694,7 +695,6 @@ def __init__(self,
694695
self._stretch=rcParams['font.stretch']
695696
self._size=rcParams['font.size']
696697
self._file=None
697-
self._mathtext_fontset=rcParams['mathtext.fontset']
698698

699699
ifisinstance(family,str):
700700
# Treat family as a fontconfig pattern if it is the only
@@ -743,7 +743,8 @@ def __hash__(self):
743743
self.get_weight(),
744744
self.get_stretch(),
745745
self.get_size_in_points(),
746-
self.get_file())
746+
self.get_file(),
747+
self.get_mathtext_fontset())
747748
returnhash(l)
748749

749750
def__eq__(self,other):
@@ -950,19 +951,15 @@ def set_mathtext_fontset(self, fontset):
950951
See Also
951952
--------
952953
.text.set_mathtext_fontset
953-
954-
TO DO: make the same validations as made when atributing a fontset
955-
to the rcParams[]
956954
"""
957-
958-
#if fontset not in _validators()
959-
960-
self._mathtext_fontset= \
961-
rcParams["mathtext.fontset"]iffontsetisNoneelsefontset
962-
963-
964-
965-
955+
iffontsetisNone:
956+
self._mathtext_fontset=rcParams['mathtext.fontset']
957+
return
958+
#this function validates the parameter fontset just as if it were
959+
#passed to rcParams['mathtext.fontset']
960+
_validators['mathtext.fontset'](fontset)
961+
self._mathtext_fontset=fontset
962+
966963
defcopy(self):
967964
"""Return a copy of self."""
968965
new=type(self)()

‎lib/matplotlib/tests/test_mathtext.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,59 @@ def test_mathtext_to_png(tmpdir):
364364
mt=mathtext.MathTextParser('bitmap')
365365
mt.to_png(str(tmpdir.join('example.png')),'$x^2$')
366366
mt.to_png(io.BytesIO(),'$x^2$')
367+
368+
369+
deftest_mathtext_fontset():
370+
#This is my first time doing this kinds of tests, so I am not sure if I am
371+
#doing it the right way, here's my plan for this testing:
372+
#
373+
#1) Create two figures, one without my implementation
374+
# (should produce 2 texts with the same font)
375+
# and one with (should create two texts with different fonts)
376+
#2) Save the figures on Disk
377+
#3) Load the figures and compare them, if they are equal, the test fails
378+
#4) Delete the figures
379+
#
380+
# I am making the test a bit slower but avoiding the burden of adding
381+
# more figures to the repo. On my computer at least, the time for this
382+
# test was of the order of a few seconds.
383+
384+
plt.rcParams["mathtext.fontset"]="dejavusans"
385+
386+
#figure 1 setup
387+
#this figure IS NOT using the 'fontset' argument, therefore,
388+
#the font should be equal to the font set in rcParams["mathtext.fontset"]
389+
f1,ax1=plt.subplots(figsize=(12,10))
390+
ax1.plot(range(11),color="0.9")
391+
ax1.text(0.2,9,r"$\mathit{This\ text\ should\ have\ a\ one\ font} $",
392+
size=18)
393+
394+
ax1.text(0.2,8,r"$\mathit{This\ text\ should\ have\ another} $",
395+
size=18)
396+
397+
#figure 2 setup
398+
#this figure IS using the 'fontset' parameter and must have two texts
399+
#with distinct fonts in the final image
400+
f2,ax2=plt.subplots(figsize=(12,10))
401+
ax2.plot(range(11),color="0.9")
402+
ax2.text(0.2,9,r"$\mathit{This\ text\ should\ have\ a\ one\ font} $",
403+
size=18,fontset='dejavusans' )
404+
405+
ax2.text(0.2,8,r"$\mathit{This\ text\ should\ have\ another} $",
406+
size=18,fontset='dejavuserif' )
407+
408+
f1.savefig('mathtext_fontset_image1.png')
409+
f2.savefig('mathtext_fontset_image2.png')
410+
411+
figureLoaded1=io.FileIO('mathtext_fontset_image1.png').readall()
412+
figureLoaded2=io.FileIO('mathtext_fontset_image2.png').readall()
413+
414+
figuresEqual=figureLoaded1==figureLoaded2
415+
416+
os.remove('mathtext_fontset_image1.png')
417+
os.remove('mathtext_fontset_image2.png')
418+
419+
#if the parameter fontset is not working, the figures will be equal and the
420+
#test will fail
421+
assertnotfiguresEqual
422+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp