[reportlab-users] Height of a String
Andy Robinson
andy at reportlab.com
Thu Mar 12 10:16:24 EDT 2009
2009/3/12 Andrea Cappelli <a.cappelli at gmail.com>:
> Hi all,
> it's possible to get teh height of a string? (given font and size,
> obviously).
Not with perfect accuracy. For our own purposes, we assume text
should fit in a box which extends by [fontsize] up from the baseline,
and [leading - fontsize] downwards, but this box will have some
extra white space.
The font face objects have an 'ascent' and 'descent' specified by the
designer of the font, which is in em-square units (1/1000th of
the width of a letter 'm', the widest character. You can query this
like so...
>>> from reportlab.pdfbase import pdfmetrics
>>> f = pdfmetrics.getFont('Helvetica')
>>> f.face.ascent
718
>>> f.face.descent
-207
>>>
This means that if you drew a string in Helvetica 10 points it would
mostly fit in 7.18 points above the baseline and 2.07 points below.
However, this is the designer's metrics for the whole font. It does
not cover each individual glyph in YOUR string, which may vary.
If working with type 1 files, you might have an 'afm' file with readable
text metrics. Otherwise, getting the precise size of a string requires
libraries to read the detailed glyph sizes (Just van Rossum's fonttools
is good). Our library doesn't go to this level of detail, sadly.
- Andy
More information about the reportlab-users
mailing list