Skip to content

Commit

Permalink
Fix Android font size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsmith committed Feb 26, 2023
1 parent 73f0c5a commit 2ff0e95
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion android/src/toga_android/widgets/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def set_enabled(self, value):
self.native.setEnabled(value)

def set_font(self, font):
self.native.setTextSize(TypedValue.COMPLEX_UNIT_SP, font._impl.get_size())
self.native.setTextSize(TypedValue.COMPLEX_UNIT_PT, font._impl.get_size())
self.native.setTypeface(font._impl.get_typeface(), font._impl.get_style())

def set_on_press(self, handler):
Expand Down
2 changes: 1 addition & 1 deletion android/src/toga_android/widgets/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def set_text(self, value):
self.native.setText(value)

def set_font(self, font):
self.native.setTextSize(TypedValue.COMPLEX_UNIT_SP, font._impl.get_size())
self.native.setTextSize(TypedValue.COMPLEX_UNIT_PT, font._impl.get_size())
self.native.setTypeface(font._impl.get_typeface(), font._impl.get_style())

def set_color(self, color):
Expand Down
6 changes: 5 additions & 1 deletion android/tests_backend/widgets/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ def text(self):

@property
def font(self):
return toga_font(self.native.getTypeface(), self.native.getTextSize(), self.dpi)
return toga_font(
self.native.getTypeface(),
self.native.getTextSize(),
self.native.getResources(),
)

@property
def alignment(self):
Expand Down
12 changes: 7 additions & 5 deletions android/tests_backend/widgets/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from travertino.fonts import Font

from android.graphics import Color, Typeface
from android.util import TypedValue
from toga.colors import rgba
from toga.fonts import BOLD, ITALIC, MONOSPACE, NORMAL, SANS_SERIF, SERIF, SYSTEM

Expand All @@ -17,10 +18,11 @@ def toga_color(color_int):
)


def toga_font(typeface, size, dpi):
# Android provides font details in pixels; that size needs to be converted
# using the ratio between the display DPI and the baseline DPI (160).
size_in_points = size / (dpi * 160)
def toga_font(typeface, size, resources):
# Android provides font details in pixels; that size needs to be converted to points.
pixels_per_point = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PT, 1, resources.getDisplayMetrics()
)

return Font(
family={
Expand All @@ -31,7 +33,7 @@ def toga_font(typeface, size, dpi):
# : CURSIVE ??
# : FANTASY ??
}.get(typeface, "Unknown"),
size=size_in_points,
size=size / pixels_per_point,
style=ITALIC if typeface.isItalic() else NORMAL,
variant=NORMAL,
weight=BOLD if typeface.isBold() else NORMAL,
Expand Down

0 comments on commit 2ff0e95

Please sign in to comment.