Skip to content

Add windows CSS keyword support and tests #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 4, 2025
1 change: 1 addition & 0 deletions changes/1814.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support for CSS keywords in Windows.
10 changes: 10 additions & 0 deletions core/tests/style/pack/test_css.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,16 @@
"flex-direction: row; flex: 0.0 0 auto; font-size: 42pt;",
id="font-size",
),
pytest.param(
Pack(font_size="smaller"),
"flex-direction: row; flex: 0.0 0 auto; font-size: smaller;",
id="font-size-smaller",
),
pytest.param(
Pack(font_size="large"),
"flex-direction: row; flex: 0.0 0 auto; font-size: large;",
id="font-size-large",
),
pytest.param(
Pack(font_size=SYSTEM_DEFAULT_FONT_SIZE),
"flex-direction: row; flex: 0.0 0 auto;",
Expand Down
11 changes: 10 additions & 1 deletion testbed/tests/test_fonts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from importlib import import_module

import pytest
from travertino.constants import (
ABSOLUTE_FONT_SIZES,
RELATIVE_FONT_SIZES,
)

import toga
from toga.fonts import (
Expand Down Expand Up @@ -53,7 +57,12 @@ async def test_use_system_font_fallback(
async def test_font_options(widget: toga.Label, font_probe):
"""Every combination of weight, style and variant can be used on a font."""
for font_family in SYSTEM_DEFAULT_FONTS:
for font_size in [20, SYSTEM_DEFAULT_FONT_SIZE]:
for font_size in [
20,
SYSTEM_DEFAULT_FONT_SIZE,
*ABSOLUTE_FONT_SIZES,
*RELATIVE_FONT_SIZES,
]:
for font_weight in FONT_WEIGHTS:
for font_style in FONT_STYLES:
for font_variant in FONT_VARIANTS:
Expand Down
20 changes: 19 additions & 1 deletion winforms/src/toga_winforms/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
from System.Drawing.Text import PrivateFontCollection
from System.IO import FileNotFoundException
from System.Runtime.InteropServices import ExternalException
from travertino.constants import (
ABSOLUTE_FONT_SIZES,
FONT_SIZE_SCALE,
RELATIVE_FONT_SIZE_SCALE,
RELATIVE_FONT_SIZES,
SYSTEM_DEFAULT_FONT_SIZE,
)

from toga.fonts import (
_REGISTERED_FONT_CACHE,
Expand All @@ -18,7 +25,6 @@
SANS_SERIF,
SERIF,
SYSTEM,
SYSTEM_DEFAULT_FONT_SIZE,
)

_FONT_CACHE = {}
Expand Down Expand Up @@ -93,6 +99,18 @@ def __init__(self, interface):
# Convert font size to Winforms format
if self.interface.size == SYSTEM_DEFAULT_FONT_SIZE:
font_size = DEFAULT_FONT.Size
elif (
isinstance(self.interface.size, str)
and self.interface.size in ABSOLUTE_FONT_SIZES
):
font_size = DEFAULT_FONT.Size
font_size *= FONT_SIZE_SCALE.get(self.interface.size, 1.0)
elif (
isinstance(self.interface.size, str)
and self.interface.size in RELATIVE_FONT_SIZES
):
font_size = getattr(self.interface, "_parent_size", DEFAULT_FONT.Size)
font_size *= RELATIVE_FONT_SIZE_SCALE.get(self.interface.size, 1.0)
else:
font_size = self.interface.size

Expand Down
16 changes: 14 additions & 2 deletions winforms/tests_backend/fonts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from System.Drawing import FontFamily, SystemFonts
from travertino.constants import (
FONT_SIZE_SCALE,
RELATIVE_FONT_SIZE_SCALE,
RELATIVE_FONT_SIZES,
)

from toga.fonts import (
BOLD,
Expand Down Expand Up @@ -45,8 +50,15 @@ def font_size(self):

def assert_font_size(self, expected):
if expected == SYSTEM_DEFAULT_FONT_SIZE:
expected = 9
assert self.font_size == expected
expected = 9.0
elif isinstance(expected, str):
base_size = 9.0
if expected in RELATIVE_FONT_SIZES:
parent_size = getattr(self, "_parent_size", base_size)
expected = parent_size * RELATIVE_FONT_SIZE_SCALE.get(expected, 1.0)
else:
expected = base_size * FONT_SIZE_SCALE.get(expected, 1.0)
assert abs(self.font.SizeInPoints - expected) < 0.1

def assert_font_family(self, expected):
assert str(self.font.Name) == {
Expand Down
Loading