Skip to content

Commit aef3d66

Browse files
authored
fix(gui): use loaded fonts in gui tests, to be consistent across OS (#2416)
* fix(gui): use loaded fonts in gui tests, to be consistent across OS * fix(gui): more controll for dpi and allow UITextArea to be bold or italic
1 parent 959d796 commit aef3d66

File tree

5 files changed

+43
-33
lines changed

5 files changed

+43
-33
lines changed

arcade/examples/gui/6_size_hints.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def __init__(self):
7979
width=800, # give text enough space to not wrap
8080
font_size=14,
8181
size_hint=(1, 1),
82+
bold=True,
8283
)
8384
)
8485
text.with_padding(top=10)
@@ -97,11 +98,11 @@ def __init__(self):
9798
)
9899

99100
width_slider_box = center_box.add(UIBoxLayout(vertical=False, size_hint=(1, 0)))
100-
width_slider_box.add(UILabel("Modify size_hint:"))
101+
width_slider_box.add(UILabel("Modify size_hint:", bold=True))
101102
width_slider = width_slider_box.add(
102103
arcade.gui.UISlider(min_value=0, max_value=10, value=0, size_hint=None, height=30)
103104
)
104-
width_value = width_slider_box.add(UILabel())
105+
width_value = width_slider_box.add(UILabel(bold=True))
105106

106107
content_anchor.add(UISpace(height=50))
107108

@@ -110,9 +111,9 @@ def __init__(self):
110111
demo_box.with_background(color=arcade.uicolor.GRAY_ASBESTOS)
111112

112113
# create a dummy widget to show the effect of the sliders
113-
dummy1 = demo_box.add(UILabel())
114+
dummy1 = demo_box.add(UILabel(bold=True))
114115
dummy1.with_background(color=arcade.uicolor.YELLOW_ORANGE)
115-
dummy2 = demo_box.add(UILabel())
116+
dummy2 = demo_box.add(UILabel(bold=True))
116117
dummy2.with_background(color=arcade.uicolor.GREEN_EMERALD)
117118

118119
def update_size_hint_value(value: float):
@@ -145,4 +146,3 @@ def main():
145146

146147
if __name__ == "__main__":
147148
main()
148-

arcade/gui/widgets/text.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,8 @@ class UITextArea(UIWidget):
627627
success.
628628
font_size: Font size of font.
629629
text_color: Color of the text.
630+
bold: If enabled, the label's text will be in a **bold** style.
631+
italic: If enabled, the label's text will be in an *italic*
630632
multiline: If enabled, a ``\\n`` will start a new line.
631633
scroll_speed: Speed of mouse scrolling.
632634
size_hint: A tuple of floats between 0 and 1 defining the amount
@@ -650,6 +652,8 @@ def __init__(
650652
text: str = "",
651653
font_name=("arial", "calibri"),
652654
font_size: float = 12,
655+
bold=False,
656+
italic=False,
653657
text_color: RGBA255 = arcade.color.WHITE,
654658
multiline: bool = True,
655659
scroll_speed: Optional[float] = None,
@@ -689,6 +693,8 @@ def __init__(
689693
font_name=font_name,
690694
font_size=font_size,
691695
color=Color.from_iterable(text_color),
696+
bold=bold,
697+
italic=italic,
692698
),
693699
)
694700

arcade/text.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def __init__(
219219
batch: pyglet.graphics.Batch | None = None,
220220
group: pyglet.graphics.Group | None = None,
221221
z: float = 0,
222+
**kwargs,
222223
):
223224
# Raises a RuntimeError if no window for better user feedback
224225
arcade.get_window()
@@ -255,6 +256,7 @@ def __init__(
255256
rotation=rotation, # type: ignore # pending https://github.com/pyglet/pyglet/issues/843
256257
batch=batch,
257258
group=group,
259+
**kwargs,
258260
)
259261

260262
def __enter__(self):

tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
WINDOW = None
2929
OFFSCREEN = None
3030

31+
arcade.resources.load_system_fonts()
3132

32-
def make_window_caption(request=None, prefix='Testing', sep=' - ') -> str:
33+
34+
def make_window_caption(request=None, prefix="Testing", sep=" - ") -> str:
3335
"""Centralizes test name customization.
3436
3537
It helps with:
@@ -179,11 +181,11 @@ def size(self):
179181
@size.setter
180182
def size(self, size):
181183
self.window.size = size
182-
184+
183185
@property
184186
def center_x(self):
185187
return self.window.center_x
186-
188+
187189
@property
188190
def center_y(self):
189191
return self.window.center_y

tests/unit/gui/test_uilabel.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99

1010
def test_constructor_only_text_no_size(window):
1111
"""Should fit text"""
12-
label = UILabel(text="Example")
12+
label = UILabel(text="Example", font_name="Kenney Pixel")
1313

14-
assert label.rect.width == pytest.approx(
15-
63, abs=10
16-
) # on windows the width differs about 6 pixel
17-
assert label.rect.height == pytest.approx(19, abs=1)
14+
assert label.rect.width == 43
15+
assert label.rect.height == 12
1816

1917

2018
def test_constructor_text_and_size(window):
@@ -28,11 +26,11 @@ def test_constructor_size_smaller_then_text(window):
2826

2927

3028
def test_constructor_fix_width_and_multiline(window):
31-
label = UILabel(text="E x a m p l e", width=10, multiline=True)
32-
assert label.rect.left == pytest.approx(0, abs=2)
33-
assert label.rect.bottom == pytest.approx(0, abs=2)
34-
assert label.rect.width == pytest.approx(10, abs=2)
35-
assert label.rect.height == pytest.approx(133, abs=7)
29+
label = UILabel(text="E x a m p l e", width=10, multiline=True, font_name="Kenney Pixel")
30+
assert label.rect.left == 0
31+
assert label.rect.bottom == 0
32+
assert label.rect.width == 10
33+
assert label.rect.height == 84
3634

3735

3836
def test_constructor_adaptive_width_support_for_multiline_text(window):
@@ -47,23 +45,23 @@ def test_constructor_adaptive_width_support_for_multiline_text(window):
4745

4846

4947
def test_with_border_keeps_previous_size(window):
50-
label = UILabel(text="Example")
51-
assert label.rect.width == pytest.approx(63, abs=10)
52-
assert label.rect.height == pytest.approx(19, abs=6)
48+
label = UILabel(text="Example", font_name="Kenney Pixel")
49+
assert label.rect.width == 43
50+
assert label.rect.height == 12
5351

5452
label.with_border()
55-
assert label.rect.width == pytest.approx(63, abs=10)
56-
assert label.rect.height == pytest.approx(19, abs=6)
53+
assert label.rect.width == 43
54+
assert label.rect.height == 12
5755

5856

5957
def test_with_padding_keeps_previous_size(window):
60-
label = UILabel(text="Example")
61-
assert label.rect.width == pytest.approx(63, abs=10)
62-
assert label.rect.height == pytest.approx(19, abs=6)
58+
label = UILabel(text="Example", font_name="Kenney Pixel")
59+
assert label.rect.width == 43
60+
assert label.rect.height == 12
6361

6462
label.with_padding(all=2)
65-
assert label.rect.width == pytest.approx(63, abs=10)
66-
assert label.rect.height == pytest.approx(19, abs=6)
63+
assert label.rect.width == 43
64+
assert label.rect.height == 12
6765

6866

6967
def test_internals_text_placed_at_0_0(window):
@@ -187,21 +185,22 @@ def test_integration_with_layout_fit_to_content(ui):
187185
label = UILabel(
188186
text="Example",
189187
size_hint=(0, 0), # default, enables auto size
188+
font_name="Kenney Pixel",
190189
)
191190

192191
ui.add(label)
193192
ui.execute_layout()
194193

195194
# auto size should fit the text
196-
assert label.rect.width == pytest.approx(63, abs=10)
197-
assert label.rect.height == pytest.approx(19, abs=6)
195+
assert label.rect.width == 44
196+
assert label.rect.height == 12
198197

199198
# even when text changed
200199
label.text = "Example, which is way longer"
201200
ui.execute_layout()
202201

203202
assert label.rect.width > 63
204-
assert label.rect.height == pytest.approx(19, abs=6)
203+
assert label.rect.height == 12
205204

206205
# or font
207206
label.text = "Example"
@@ -217,12 +216,13 @@ def test_fit_content_overrides_width(ui):
217216
text="Example",
218217
width=100,
219218
height=50,
219+
font_name="Kenney Pixel",
220220
)
221221

222222
label.fit_content()
223223

224-
assert label.rect.width == pytest.approx(63, abs=10)
225-
assert label.rect.height == pytest.approx(19, abs=6)
224+
assert label.rect.width == 44
225+
assert label.rect.height == 12
226226

227227

228228
def test_fit_content_uses_adaptive_multiline_width(ui):

0 commit comments

Comments
 (0)