[cairo] “text align center” Code Sample

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Tue Nov 17 13:38:57 PST 2015


This code sample <http://cairographics.org/samples/text_align_center/>
is supposed to show how to center text around a position both
horizontally and vertically. I don’t think the vertical centring looks
good.

If you want to do accurate vertical centring, the formula I came up
with is

    text_mid_y = (- 2 * text_extents.y_bearing - text_extents.height) / 2

However, I find that it looks a bit better overall to do the vertical
centring based on the font metrics, rather than that of the specific
text:

    font_mid_y = (font_extents.ascent - font_extents.descent) / 2

You can compare the different effects here
<http://default-cube.deviantart.com/art/Text-Vert-Align-520839470>, and
see if you agree or not.


More information about the cairo mailing list