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
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