Skip to content

Commit 3f9740c

Browse files
Font Manger: improve baseline
1 parent e9fac33 commit 3f9740c

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

internal_filesystem/builtin/apps/com.micropythonos.about/assets/about.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import lvgl as lv
66

7-
from mpos import Activity, DisplayMetrics, BuildInfo, DeviceInfo
7+
from mpos import Activity, DisplayMetrics, BuildInfo, DeviceInfo, FontManager
88
import mpos
99

1010
class About(Activity):
@@ -15,6 +15,8 @@ class About(Activity):
1515
def onCreate(self):
1616
self._uptime_label = None
1717
self._timer = None
18+
self._header_font = FontManager.getFont(size=14, family="Montserrat", emoji=False)
19+
self._body_font = FontManager.getFont(size=12, family="Montserrat", emoji=False)
1820
screen = lv.obj()
1921
screen.set_style_border_width(0, lv.PART.MAIN)
2022
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
@@ -233,11 +235,11 @@ def _add_label(self, parent, text, is_header=False, margin_top=DisplayMetrics.pc
233235
if is_header:
234236
primary_color = lv.theme_get_color_primary(None)
235237
label.set_style_text_color(primary_color, lv.PART.MAIN)
236-
label.set_style_text_font(lv.font_montserrat_14, lv.PART.MAIN)
238+
label.set_style_text_font(self._header_font, lv.PART.MAIN)
237239
label.set_style_margin_top(margin_top, lv.PART.MAIN)
238240
label.set_style_margin_bottom(DisplayMetrics.pct_of_height(2), lv.PART.MAIN)
239241
else:
240-
label.set_style_text_font(lv.font_montserrat_12, lv.PART.MAIN)
242+
label.set_style_text_font(self._body_font, lv.PART.MAIN)
241243
label.set_style_margin_bottom(2, lv.PART.MAIN)
242244
return label
243245

internal_filesystem/lib/mpos/ui/font_manager.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ def _get_composed_font(cls, base_font, size=None):
157157
try:
158158
# Do not mutate builtin font fallback: builtins may live in readonly memory.
159159
emoji_font.fallback = base_font
160+
emoji_font.base_line = cls._font_base_line(base_font)
161+
emoji_font.underline_position = cls._font_underline_position(base_font)
162+
emoji_font.underline_thickness = cls._font_underline_thickness(base_font)
160163
except Exception as err:
161164
cls._debug("compose fallback set failed: " + repr(err))
162165
return base_font
@@ -308,13 +311,14 @@ def _safe_getcwd(cls):
308311

309312
@classmethod
310313
def _imgfont_path_cb(cls, font, unicode_cp, unicode_next, offset_y, user_data):
314+
baseline = cls._font_base_line(font)
311315
if unicode_cp == CP_VARIATION_SELECTOR_TEXT or unicode_cp == CP_VARIATION_SELECTOR_EMOJI:
312-
offset_y.__dereference__(0)
316+
offset_y.__dereference__(-baseline)
313317
return cls._get_empty_imgfont_src(cls._font_pixel_height(font))
314318

315319
src = cls._get_emoji_src(unicode_cp, cls._font_pixel_height(font))
316320
if src is not None:
317-
offset_y.__dereference__(0)
321+
offset_y.__dereference__(-baseline)
318322
return cls._get_scaled_imgfont_src(src, cls._font_pixel_height(font))
319323

320324
cls._log_unknown_emoji_codepoint(unicode_cp)
@@ -380,6 +384,27 @@ def _font_pixel_height(cls, font):
380384
except Exception:
381385
return 1
382386

387+
@classmethod
388+
def _font_base_line(cls, font):
389+
try:
390+
return int(font.base_line)
391+
except Exception:
392+
return 0
393+
394+
@classmethod
395+
def _font_underline_position(cls, font):
396+
try:
397+
return int(font.underline_position)
398+
except Exception:
399+
return 0
400+
401+
@classmethod
402+
def _font_underline_thickness(cls, font):
403+
try:
404+
return int(font.underline_thickness)
405+
except Exception:
406+
return 0
407+
383408
@classmethod
384409
def _get_scaled_imgfont_src(cls, src, target_height):
385410
key = (src, target_height)

0 commit comments

Comments
 (0)