From ad41fc09cdf4efa66b17bd65c3f0084a2c343a2f Mon Sep 17 00:00:00 2001 From: Maic Siemering Date: Tue, 15 Oct 2024 10:25:12 +0200 Subject: [PATCH 1/2] fix(gui): use loaded fonts in gui tests, to be consistent across OS --- tests/conftest.py | 8 ++++-- tests/unit/gui/test_uilabel.py | 50 +++++++++++++++++----------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 10edb1265..8e46b39d8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -28,8 +28,10 @@ WINDOW = None OFFSCREEN = None +arcade.resources.load_system_fonts() -def make_window_caption(request=None, prefix='Testing', sep=' - ') -> str: + +def make_window_caption(request=None, prefix="Testing", sep=" - ") -> str: """Centralizes test name customization. It helps with: @@ -179,11 +181,11 @@ def size(self): @size.setter def size(self, size): self.window.size = size - + @property def center_x(self): return self.window.center_x - + @property def center_y(self): return self.window.center_y diff --git a/tests/unit/gui/test_uilabel.py b/tests/unit/gui/test_uilabel.py index 0753ff68f..98fbd4729 100644 --- a/tests/unit/gui/test_uilabel.py +++ b/tests/unit/gui/test_uilabel.py @@ -9,12 +9,10 @@ def test_constructor_only_text_no_size(window): """Should fit text""" - label = UILabel(text="Example") + label = UILabel(text="Example", font_name="Kenney Pixel") - assert label.rect.width == pytest.approx( - 63, abs=10 - ) # on windows the width differs about 6 pixel - assert label.rect.height == pytest.approx(19, abs=1) + assert label.rect.width == 43 + assert label.rect.height == 12 def test_constructor_text_and_size(window): @@ -28,11 +26,11 @@ def test_constructor_size_smaller_then_text(window): def test_constructor_fix_width_and_multiline(window): - label = UILabel(text="E x a m p l e", width=10, multiline=True) - assert label.rect.left == pytest.approx(0, abs=2) - assert label.rect.bottom == pytest.approx(0, abs=2) - assert label.rect.width == pytest.approx(10, abs=2) - assert label.rect.height == pytest.approx(133, abs=7) + label = UILabel(text="E x a m p l e", width=10, multiline=True, font_name="Kenney Pixel") + assert label.rect.left == 0 + assert label.rect.bottom == 0 + assert label.rect.width == 10 + assert label.rect.height == 84 def test_constructor_adaptive_width_support_for_multiline_text(window): @@ -47,23 +45,23 @@ def test_constructor_adaptive_width_support_for_multiline_text(window): def test_with_border_keeps_previous_size(window): - label = UILabel(text="Example") - assert label.rect.width == pytest.approx(63, abs=10) - assert label.rect.height == pytest.approx(19, abs=6) + label = UILabel(text="Example", font_name="Kenney Pixel") + assert label.rect.width == 43 + assert label.rect.height == 12 label.with_border() - assert label.rect.width == pytest.approx(63, abs=10) - assert label.rect.height == pytest.approx(19, abs=6) + assert label.rect.width == 43 + assert label.rect.height == 12 def test_with_padding_keeps_previous_size(window): - label = UILabel(text="Example") - assert label.rect.width == pytest.approx(63, abs=10) - assert label.rect.height == pytest.approx(19, abs=6) + label = UILabel(text="Example", font_name="Kenney Pixel") + assert label.rect.width == 43 + assert label.rect.height == 12 label.with_padding(all=2) - assert label.rect.width == pytest.approx(63, abs=10) - assert label.rect.height == pytest.approx(19, abs=6) + assert label.rect.width == 43 + assert label.rect.height == 12 def test_internals_text_placed_at_0_0(window): @@ -187,21 +185,22 @@ def test_integration_with_layout_fit_to_content(ui): label = UILabel( text="Example", size_hint=(0, 0), # default, enables auto size + font_name="Kenney Pixel", ) ui.add(label) ui.execute_layout() # auto size should fit the text - assert label.rect.width == pytest.approx(63, abs=10) - assert label.rect.height == pytest.approx(19, abs=6) + assert label.rect.width == 44 + assert label.rect.height == 12 # even when text changed label.text = "Example, which is way longer" ui.execute_layout() assert label.rect.width > 63 - assert label.rect.height == pytest.approx(19, abs=6) + assert label.rect.height == 12 # or font label.text = "Example" @@ -217,12 +216,13 @@ def test_fit_content_overrides_width(ui): text="Example", width=100, height=50, + font_name="Kenney Pixel", ) label.fit_content() - assert label.rect.width == pytest.approx(63, abs=10) - assert label.rect.height == pytest.approx(19, abs=6) + assert label.rect.width == 44 + assert label.rect.height == 12 def test_fit_content_uses_adaptive_multiline_width(ui): From 2876d74a79d2a3ab45e98da28b6b79b5b6d89ebb Mon Sep 17 00:00:00 2001 From: Maic Siemering Date: Tue, 15 Oct 2024 16:18:22 +0200 Subject: [PATCH 2/2] fix(gui): more controll for dpi and allow UITextArea to be bold or italic --- arcade/examples/gui/6_size_hints.py | 10 +++++----- arcade/gui/widgets/text.py | 6 ++++++ arcade/text.py | 2 ++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/arcade/examples/gui/6_size_hints.py b/arcade/examples/gui/6_size_hints.py index 7dfa1fdaf..7a0c1721a 100644 --- a/arcade/examples/gui/6_size_hints.py +++ b/arcade/examples/gui/6_size_hints.py @@ -79,6 +79,7 @@ def __init__(self): width=800, # give text enough space to not wrap font_size=14, size_hint=(1, 1), + bold=True, ) ) text.with_padding(top=10) @@ -97,11 +98,11 @@ def __init__(self): ) width_slider_box = center_box.add(UIBoxLayout(vertical=False, size_hint=(1, 0))) - width_slider_box.add(UILabel("Modify size_hint:")) + width_slider_box.add(UILabel("Modify size_hint:", bold=True)) width_slider = width_slider_box.add( arcade.gui.UISlider(min_value=0, max_value=10, value=0, size_hint=None, height=30) ) - width_value = width_slider_box.add(UILabel()) + width_value = width_slider_box.add(UILabel(bold=True)) content_anchor.add(UISpace(height=50)) @@ -110,9 +111,9 @@ def __init__(self): demo_box.with_background(color=arcade.uicolor.GRAY_ASBESTOS) # create a dummy widget to show the effect of the sliders - dummy1 = demo_box.add(UILabel()) + dummy1 = demo_box.add(UILabel(bold=True)) dummy1.with_background(color=arcade.uicolor.YELLOW_ORANGE) - dummy2 = demo_box.add(UILabel()) + dummy2 = demo_box.add(UILabel(bold=True)) dummy2.with_background(color=arcade.uicolor.GREEN_EMERALD) def update_size_hint_value(value: float): @@ -145,4 +146,3 @@ def main(): if __name__ == "__main__": main() - diff --git a/arcade/gui/widgets/text.py b/arcade/gui/widgets/text.py index c889443b9..52847951f 100644 --- a/arcade/gui/widgets/text.py +++ b/arcade/gui/widgets/text.py @@ -627,6 +627,8 @@ class UITextArea(UIWidget): success. font_size: Font size of font. text_color: Color of the text. + bold: If enabled, the label's text will be in a **bold** style. + italic: If enabled, the label's text will be in an *italic* multiline: If enabled, a ``\\n`` will start a new line. scroll_speed: Speed of mouse scrolling. size_hint: A tuple of floats between 0 and 1 defining the amount @@ -650,6 +652,8 @@ def __init__( text: str = "", font_name=("arial", "calibri"), font_size: float = 12, + bold=False, + italic=False, text_color: RGBA255 = arcade.color.WHITE, multiline: bool = True, scroll_speed: Optional[float] = None, @@ -689,6 +693,8 @@ def __init__( font_name=font_name, font_size=font_size, color=Color.from_iterable(text_color), + bold=bold, + italic=italic, ), ) diff --git a/arcade/text.py b/arcade/text.py index 6e74ee906..a060d686c 100644 --- a/arcade/text.py +++ b/arcade/text.py @@ -219,6 +219,7 @@ def __init__( batch: pyglet.graphics.Batch | None = None, group: pyglet.graphics.Group | None = None, z: float = 0, + **kwargs, ): # Raises a RuntimeError if no window for better user feedback arcade.get_window() @@ -255,6 +256,7 @@ def __init__( rotation=rotation, # type: ignore # pending https://github.com/pyglet/pyglet/issues/843 batch=batch, group=group, + **kwargs, ) def __enter__(self):