[reportlab-users] vertically centered text

Aaron Spike aaron at ekips.org
Mon Jan 28 18:16:07 EST 2008


Robin Becker wrote:

> The y coordinate of a drawString type operation ends up as being the

> baseline of the text. Typically text is not vertically symmetric so is

> not visually centered.


Yes, and this works great because it is the natural visual anchor point
for most uses of text. But if you want to center text in a tightly
containing shape it is difficult. In my particular application I want to
center text in a circle. If it isn't close to exactly centered it
distracts the eye.


> A good rule of thumb is that the part below the

> base line is 20% of the point size ie the vertical middle of the text is

> 40% of the pointsize above the normal base line.


I tried using some magic numbers like these and found the results
unsatisfactory.


> However, these

> assumptions are clearly dependent on exactly which glyphs are being

> used. I know this is not a decent answer, but you can obtain the curves

> being used for a particular string using

> reportlab.graphics.charts.textlabels._text2path

>

> def _text2Path(text, x=0, y=0, fontName='Times-Roman', fontSize=1000,

> anchor='start', truncate=1, pathReverse=0):

>

>

> the returned path can then be used to obtain a bounding box ie

>

> >>> from reportlab.graphics.charts.textlabels import _text2Path

> >>> p=_text2Path('a min')

> >>> p.getBounds()

> (34, -14, 2246, 694)

> >>> p=_text2Path('A min')

> >>> p.getBounds()

> (10, 0, 2524, 694)

> >>>

>

> so you can see that A is taller than a


_text2path looks like exactly what I need. When I'm working with Cairo I
use text_extents to find the bounds of a particular piece of text:

http://cairographics.org/manual/cairo-Scaled-Fonts.html#cairo-text-extents-t



My output with Cairo looks like:

http://www.ekips.org/misc/arpeggio/all.pdf

I'd like to make my diagrams flowable and use reportlab's layout
features for titles and annotations.

Should I be afraid that _text2path looks like a private part of the API?
or am I free to use that? Is there no public way to get this info? Maybe
something which can look into the current text rendering context?

Aaron Spike



More information about the reportlab-users mailing list