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

x/image/font: make it easier to measure a string's bounds and draw it in a bounding box #14436

josharian opened this Issue Feb 20, 2016 · 1 comment


None yet
2 participants
Copy link

josharian commented Feb 20, 2016

This issue applies to, if that is still under development.

I want to accomplish the following (superficially) easy tasks:

  • Given a string and a fully specified font/context, determine the rendered string's bounds, taking into account ascenders, descenders, etc.; something along the lines of NSString's -sizeWithAttributes: method.
  • Given an appropriately sized rect, render a string in that rect, compensating for ascenders, descenders, etc; something like NSString's -drawInRect:withAttributes: method.

I will use this to render a simple, short string into a texture of just the right size, and then upload that texture onto a window for use as a label at a particular origin.

MeasureString looks close, but it only provides the width, not the height. (And I don't see a way to get the line height / leading from a font.) And DrawString is close, but the dot's y offset is the baseline of the text, which I don't see a handy way to calculate.

The fact that these are top-level APIs in Cocoa suggests that these are common needs and that they cover a large portion of use cases. Maybe this should be handled with examples, or maybe with new fundamental APIs, or maybe with convenience APIs. I don't know enough to say.

/cc @nigeltao

@josharian josharian added this to the Unreleased milestone Feb 20, 2016


This comment has been minimized.

Copy link

nigeltao commented Feb 21, 2016

Yes, this is part of the "TODO: per-font Metrics" in font.go.

BTW, the project (not the top-level package) is still under development. See also golang/freetype#15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.