[reportlab-users] vertically centered text

Robin Becker robin at reportlab.com
Mon Jan 28 17:05:41 EST 2008


Aaron Spike wrote:

> I am able to center text horizontally with the drawCenteredString()

> function that the api provides. I haven't been able to find any way to

> center text vertically (or both vertically and horizontally at the same

> time). Is there any font metric I can access to adjust the vertical

> placement of text so that I can center it on a point?

>

> Aaron Spike

.......
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. 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. 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
--
Robin Becker


More information about the reportlab-users mailing list