jhanarato / uposatha-inky

Display the uposatha on a Pimoroni Inky display
MIT License
0 stars 0 forks source link

Align glyphs in DayOfWeekIcon #22

Closed jhanarato closed 1 year ago

jhanarato commented 1 year ago

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 let components.DayOfWeekIcon do it independently.

jhanarato commented 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}
jhanarato commented 1 year ago

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:

jhanarato commented 1 year ago

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!

jhanarato commented 1 year ago

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.

jhanarato commented 1 year ago

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?

jhanarato commented 1 year ago

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

jhanarato commented 1 year ago

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!

jhanarato commented 1 year ago

Drawing text with an anchor solves the problem.