-
-
Notifications
You must be signed in to change notification settings - Fork 781
Support CSS font size keywords #3242
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
base: main
Are you sure you want to change the base?
Changes from all commits
0f81681
5935112
849e245
35d8379
e29909d
f5bdde2
d62838d
81b2609
07b4706
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -4,6 +4,9 @@ | |||||||
from android.graphics import Typeface | ||||||||
from android.util import TypedValue | ||||||||
from org.beeware.android import MainActivity | ||||||||
from travertino.constants import ( | ||||||||
FONT_SIZE_SCALE, | ||||||||
) | ||||||||
|
||||||||
from toga.fonts import ( | ||||||||
_REGISTERED_FONT_CACHE, | ||||||||
|
@@ -98,6 +101,7 @@ def typeface(self, *, default=Typeface.DEFAULT): | |||||||
def size(self, *, default=None): | ||||||||
"""Return the font size in physical pixels.""" | ||||||||
context = MainActivity.singletonThis | ||||||||
base_size = 14 | ||||||||
if self.interface.size == SYSTEM_DEFAULT_FONT_SIZE: | ||||||||
if default is None: | ||||||||
typed_array = context.obtainStyledAttributes( | ||||||||
|
@@ -106,12 +110,15 @@ def size(self, *, default=None): | |||||||
default = typed_array.getDimension(0, 0) | ||||||||
typed_array.recycle() | ||||||||
return default | ||||||||
|
||||||||
elif isinstance(self.interface.size, str): | ||||||||
default = base_size * FONT_SIZE_SCALE[self.interface.size] | ||||||||
return default | ||||||||
Comment on lines
+114
to
+115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
else: | ||||||||
# Using SP means we follow the standard proportion between CSS pixels and | ||||||||
# points by default, but respect the system text scaling setting. | ||||||||
return TypedValue.applyDimension( | ||||||||
default = TypedValue.applyDimension( | ||||||||
TypedValue.COMPLEX_UNIT_SP, | ||||||||
self.interface.size * (96 / 72), | ||||||||
context.getResources().getDisplayMetrics(), | ||||||||
) | ||||||||
return default |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added support for absolute CSS font size keywords in Android, Cocoa, GTK, iOS, and Windows. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,9 @@ | ||||||||
from pathlib import Path | ||||||||
|
||||||||
from fontTools.ttLib import TTFont | ||||||||
from travertino.constants import ( | ||||||||
FONT_SIZE_SCALE, | ||||||||
) | ||||||||
|
||||||||
from toga.fonts import ( | ||||||||
_REGISTERED_FONT_CACHE, | ||||||||
|
@@ -88,6 +91,9 @@ def __init__(self, interface): | |||||||
|
||||||||
if self.interface.size == SYSTEM_DEFAULT_FONT_SIZE: | ||||||||
font_size = NSFont.systemFontSize | ||||||||
elif isinstance(self.interface.size, str): | ||||||||
base_size = NSFont.systemFontSize | ||||||||
font_size = base_size * FONT_SIZE_SCALE[self.interface.size] | ||||||||
Comment on lines
+95
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
else: | ||||||||
# A "point" in Apple APIs is equivalent to a CSS pixel, but the Toga | ||||||||
# public API works in CSS points, which are slightly larger | ||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This size will need to be scaled in a similar way to the
TypedValue.applyDimension
code below. Otherwise, the text will be too small on high DPI devices (which is virtually all of them). For example, if I editexamples/font_size
to use the font size keywords:As mentioned at #1814 (comment), on Android, as far as I know, "medium" should be the same as the default for every widget except the TextInput, where the default should be larger.
Please make a similar visual check on as many other platforms as you can, and let me know which ones you've checked. Windows in high DPI mode is the most likely one to have a problem.