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

Skip to content

Commit 47c96df

Browse files
authored
Merge pull request #26131 from tacaswell/tst/restore_old_tests
Tst/restore old tests
2 parents 149720c + d96d68c commit 47c96df

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

lib/matplotlib/tests/test_text.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import matplotlib as mpl
1212
from matplotlib.backend_bases import MouseEvent
13+
from matplotlib.backends.backend_agg import RendererAgg
14+
from matplotlib.figure import Figure
1315
from matplotlib.font_manager import FontProperties
1416
import matplotlib.patches as mpatches
1517
import matplotlib.pyplot as plt
@@ -965,3 +967,166 @@ def test_annotate_and_offsetfrom_copy_input(fig_test, fig_ref):
965967
an_xy = np.array([.5, .5])
966968
ax.annotate("foo", xy=an_xy, xycoords=l, xytext=(10, 0), textcoords="offset points")
967969
an_xy[:] = 2
970+
971+
972+
@check_figures_equal()
973+
def test_text_antialiased_off_default_vs_manual(fig_test, fig_ref):
974+
fig_test.text(0.5, 0.5, '6 inches x 2 inches',
975+
antialiased=False)
976+
977+
mpl.rcParams['text.antialiased'] = False
978+
fig_ref.text(0.5, 0.5, '6 inches x 2 inches')
979+
980+
981+
@check_figures_equal()
982+
def test_text_antialiased_on_default_vs_manual(fig_test, fig_ref):
983+
fig_test.text(0.5, 0.5, '6 inches x 2 inches', antialiased=True)
984+
985+
mpl.rcParams['text.antialiased'] = True
986+
fig_ref.text(0.5, 0.5, '6 inches x 2 inches')
987+
988+
989+
def test_text_annotation_get_window_extent():
990+
figure = Figure(dpi=100)
991+
renderer = RendererAgg(200, 200, 100)
992+
993+
# Only text annotation
994+
annotation = Annotation('test', xy=(0, 0), xycoords='figure pixels')
995+
annotation.set_figure(figure)
996+
997+
text = Text(text='test', x=0, y=0)
998+
text.set_figure(figure)
999+
1000+
bbox = annotation.get_window_extent(renderer=renderer)
1001+
1002+
text_bbox = text.get_window_extent(renderer=renderer)
1003+
assert bbox.width == text_bbox.width
1004+
assert bbox.height == text_bbox.height
1005+
1006+
_, _, d = renderer.get_text_width_height_descent(
1007+
'text', annotation._fontproperties, ismath=False)
1008+
_, _, lp_d = renderer.get_text_width_height_descent(
1009+
'lp', annotation._fontproperties, ismath=False)
1010+
below_line = max(d, lp_d)
1011+
1012+
# These numbers are specific to the current implementation of Text
1013+
points = bbox.get_points()
1014+
assert points[0, 0] == 0.0
1015+
assert points[1, 0] == text_bbox.width
1016+
assert points[0, 1] == -below_line
1017+
assert points[1, 1] == text_bbox.height - below_line
1018+
1019+
1020+
def test_text_with_arrow_annotation_get_window_extent():
1021+
headwidth = 21
1022+
fig, ax = plt.subplots(dpi=100)
1023+
txt = ax.text(s='test', x=0, y=0)
1024+
ann = ax.annotate(
1025+
'test',
1026+
xy=(0.0, 50.0),
1027+
xytext=(50.0, 50.0), xycoords='figure pixels',
1028+
arrowprops={
1029+
'facecolor': 'black', 'width': 2,
1030+
'headwidth': headwidth, 'shrink': 0.0})
1031+
1032+
plt.draw()
1033+
renderer = fig.canvas.renderer
1034+
# bounding box of text
1035+
text_bbox = txt.get_window_extent(renderer=renderer)
1036+
# bounding box of annotation (text + arrow)
1037+
bbox = ann.get_window_extent(renderer=renderer)
1038+
# bounding box of arrow
1039+
arrow_bbox = ann.arrow_patch.get_window_extent(renderer)
1040+
# bounding box of annotation text
1041+
ann_txt_bbox = Text.get_window_extent(ann)
1042+
1043+
# make sure annotation width is 50 px wider than
1044+
# just the text
1045+
assert bbox.width == text_bbox.width + 50.0
1046+
# make sure the annotation text bounding box is same size
1047+
# as the bounding box of the same string as a Text object
1048+
assert ann_txt_bbox.height == text_bbox.height
1049+
assert ann_txt_bbox.width == text_bbox.width
1050+
# compute the expected bounding box of arrow + text
1051+
expected_bbox = mtransforms.Bbox.union([ann_txt_bbox, arrow_bbox])
1052+
assert_almost_equal(bbox.height, expected_bbox.height)
1053+
1054+
1055+
def test_arrow_annotation_get_window_extent():
1056+
dpi = 100
1057+
dots_per_point = dpi / 72
1058+
figure = Figure(dpi=dpi)
1059+
figure.set_figwidth(2.0)
1060+
figure.set_figheight(2.0)
1061+
renderer = RendererAgg(200, 200, 100)
1062+
1063+
# Text annotation with arrow; arrow dimensions are in points
1064+
annotation = Annotation(
1065+
'', xy=(0.0, 50.0), xytext=(50.0, 50.0), xycoords='figure pixels',
1066+
arrowprops={
1067+
'facecolor': 'black', 'width': 8, 'headwidth': 10, 'shrink': 0.0})
1068+
annotation.set_figure(figure)
1069+
annotation.draw(renderer)
1070+
1071+
bbox = annotation.get_window_extent()
1072+
points = bbox.get_points()
1073+
1074+
assert bbox.width == 50.0
1075+
assert_almost_equal(bbox.height, 10.0 * dots_per_point)
1076+
assert points[0, 0] == 0.0
1077+
assert points[0, 1] == 50.0 - 5 * dots_per_point
1078+
1079+
1080+
def test_empty_annotation_get_window_extent():
1081+
figure = Figure(dpi=100)
1082+
figure.set_figwidth(2.0)
1083+
figure.set_figheight(2.0)
1084+
renderer = RendererAgg(200, 200, 100)
1085+
1086+
# Text annotation with arrow
1087+
annotation = Annotation(
1088+
'', xy=(0.0, 50.0), xytext=(0.0, 50.0), xycoords='figure pixels')
1089+
annotation.set_figure(figure)
1090+
annotation.draw(renderer)
1091+
1092+
bbox = annotation.get_window_extent()
1093+
points = bbox.get_points()
1094+
1095+
assert points[0, 0] == 0.0
1096+
assert points[1, 0] == 0.0
1097+
assert points[1, 1] == 50.0
1098+
assert points[0, 1] == 50.0
1099+
1100+
1101+
@image_comparison(baseline_images=['basictext_wrap'],
1102+
extensions=['png'])
1103+
def test_basic_wrap():
1104+
fig = plt.figure()
1105+
plt.axis([0, 10, 0, 10])
1106+
t = "This is a really long string that I'd rather have wrapped so that" \
1107+
" it doesn't go outside of the figure, but if it's long enough it" \
1108+
" will go off the top or bottom!"
1109+
plt.text(4, 1, t, ha='left', rotation=15, wrap=True)
1110+
plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
1111+
plt.text(5, 5, t, ha='right', rotation=-15, wrap=True)
1112+
plt.text(5, 10, t, fontsize=18, style='oblique', ha='center',
1113+
va='top', wrap=True)
1114+
plt.text(3, 4, t, family='serif', style='italic', ha='right', wrap=True)
1115+
plt.text(-1, 0, t, ha='left', rotation=-15, wrap=True)
1116+
1117+
1118+
@image_comparison(baseline_images=['fonttext_wrap'],
1119+
extensions=['png'])
1120+
def test_font_wrap():
1121+
fig = plt.figure()
1122+
plt.axis([0, 10, 0, 10])
1123+
t = "This is a really long string that I'd rather have wrapped so that" \
1124+
" it doesn't go outside of the figure, but if it's long enough it" \
1125+
" will go off the top or bottom!"
1126+
plt.text(4, -1, t, fontsize=18, family='serif', ha='left', rotation=15,
1127+
wrap=True)
1128+
plt.text(6, 5, t, family='sans serif', ha='left', rotation=15, wrap=True)
1129+
plt.text(5, 10, t, weight='heavy', ha='center', va='top', wrap=True)
1130+
plt.text(3, 4, t, family='monospace', ha='right', wrap=True)
1131+
plt.text(-1, 0, t, fontsize=14, style='italic', ha='left', rotation=-15,
1132+
wrap=True)

0 commit comments

Comments
 (0)