Loading...
Logo
Processing Forum

glyph height?

in Core Library Questions  •  2 years ago  
it would be useful to know the height of text drawn with a given font.

font.getGlyph() seems like the route to go for this, using a standard character (e.g. 'A') to get metrics.  however, i can't access any of the fields of Glyph, even though Glyph is a public class, and its fields (e.g. height) are public as well.

maybe this is an inner class thing?  just wondering why i can't access Glyph's fields.

thanks!

Replies(7)

Re: glyph height?

2 years ago
Processing has inbuilt functions for that.
If the font is the current font selected with textFont(), then
textAscent() + textDescent() should be equivalent to the height of the text.

Re: glyph height?

2 years ago
ah.  weird those are not methods of PFont, but instead operate on the active (via textFont()) font.
thanks for pointing that out.

i'm still curious, tho at this point it's only curiosity, why i can't access those public fields of Glyph...

Re: glyph height?

2 years ago
Yes, the class Glyph is public but the array of Glyphs itself is protected (accessible only by package).

Re: Re: glyph height?

2 years ago
yeah but -- PFont.getGlyph() returns a Glyph.  how does the protected Glyph array have to do with accessing fields of a given Glyph?

Re: glyph height?

2 years ago
I don't have such problem:
Copy code
  1. PFont f = createFont("Arial", 48);
  2. PFont.Glyph g = f.getGlyph('p');
  3. println(g.width + " " + g.height);
shows 22 37

Re: Re: glyph height?

2 years ago
aha.  it *was* an inner class problem, in that i wasn't addressing the class properly, i was addressing it as just Glyph, not PFont.Glyph.  thanks phi.lho.

Re: glyph height?

2 years ago
It shows the usefulness of reporting full errors messages instead of vague indications like "it doesn't work" or "I can't access", particularly when not giving testable code...