Closed jhanarato closed 1 year ago
Just to remind myself, this is the output from glyphtools.get_glyph_metrics()
:
{'fullwidth': 1186,
'lsb': 130,
'rise': 0,
'rsb': 132,
'run': 1448,
'width': 1448,
'xMax': 1316,
'xMin': 130,
'yMax': 1456,
'yMin': 0}
Given the width of the glyph (fullwidth
above, width
is the advance width) the left side bearing (lsb
) and a bounding box, this is how we calculate the x coordinate for drawing the glyph:
left_side_of_glyph = (bbox_width - glyph_width) // 2
fonts.py
module is greatly refactored. All that remains is the glyph_centered_x()
function that can be moved to the DayOfWeekIcon
class.
We should benchmark this code!
Glyphs probably look better centered vertically than when their baselines are aligned.
Use yMin
and yMax
to get the glyph height. Have to figure out the top side bearing.
For vertical centering this is helpful:
https://developer.android.com/reference/android/graphics/Paint.FontMetrics.html
So, when we draw a Pillow font, is zero the top or the ascent?
Oh bugger, just found this:
https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html
Looks like I just need to set the anchor to "mm"?
A simple test shows a baseline of 28 pixels below x/y for 30pt RobotoBold. The BBox code may actually be one pixel below, with the lowest at position 27. That is the same as the font ascent. That's what we should use!
Drawing text with an anchor solves the problem.
This combines issues #9 and #21.
We now scale up the font size with the icon size and have a couple of demos to eyeball the icons. At the moment they're off center horizontally and the bases are also off.
I think
countdown.Icons
is the place to put the centering code. If not, we can letcomponents.DayOfWeekIcon
do it independently.