Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text extents #5

Open
GoogleCodeExporter opened this issue Jun 18, 2015 · 16 comments
Open

Text extents #5

GoogleCodeExporter opened this issue Jun 18, 2015 · 16 comments

Comments

@GoogleCodeExporter
Copy link

With freetype-go, is there a way to compute the text extents (width and height) 
of a string given a particular font and size *before* rendering the text?

I've looked around in the source code, and it *appears* that the right 
information is there, but I can't quite seem to figure out how to do it.

As of right now, if I want an image that "snugly" fits some string, I do the 
following:

Over estimate the extents by multiplying the pixel size of one em unit by the 
length of the string. I get the width/height this way (assuming one line of 
text).

I allocate an image with a rectangle of two points: (0, 0) and (width, height).

After rendering the text to the image using DrawText, I use the point returned 
from DrawText to take a sub-image of the initial image. (I convert this point 
to a (x, y) position by simply dividing the X and Y of the point by 256. It 
seems to work OK, but I have no idea if this is correct.)

--------------

And one last question: would the extents include the entire bounding box of the 
string? The (x,y) position returned by DrawText doesn't seem to cover the parts 
of the text the dip below the text's base line. Like the tail in the 'y'. My 
solution has just been to pad the 'y' with a few pixels, but I know this is not 
optimal since it will break if the font size changes too much.

(My apologies if my terminology is off. I don't have much experience in font 
rendering.)

I would happily submit a patch if I was nudged in the right direction :-)

Thanks!

Original issue reported on code.google.com by [email protected] on 2 Jun 2012 at 10:44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant