Tight bounding box around text in graphics?
Hi
strheight(), which is based GEStrHeight(), is pretty crude, not only
ignoring descenders, but also only considering the ascent of the overall
font (capital "M").
There is a GEStrMetric(), which returns character-specific ascent and
descent, but that is only currently exposed via grid::stringAscent() and
grid::stringDescent(). There is also grid::stringHeight(), which is as
unsubtle as strheight().
For example, these are all the same (just font ascent) ...
> strheight("y", "in")
[1] 0.1248031
> strheight("x", "in")
[1] 0.1248031
> strheight("M", "in")
[1] 0.1248031
... and these are all the same ...
> convertHeight(stringHeight("y"), "in")
[1] 0.124803149606299inches
> convertHeight(stringHeight("x"), "in")
[1] 0.124803149606299inches
> convertHeight(stringAscent("M"), "in")
[1] 0.124803149606299inches
... but these have more detail ...
> convertHeight(stringAscent("y"), "in")
[1] 0.0936023622047244inches
> convertHeight(stringDescent("y"), "in")
[1] 0.0416010498687664inches
> convertHeight(stringAscent("x"), "in")
[1] 0.0936023622047244inches
> convertHeight(stringDescent("x"), "in")
[1] 0inches
> convertHeight(stringHeight("M"), "in")
[1] 0.124803149606299inches
> convertHeight(stringDescent("M"), "in")
[1] 0inches
In theory, it should not be difficult to add a graphics::strascent() and
graphics::strdescent() if that would help.
Paul
On 26/09/23 08:06, Duncan Murdoch wrote:
I've mentioned in previous messages that I'm trying to redo rgl text. Part of what I need is to measure the size of strings in pixels when they are drawn by base graphics. It appears that strwidth(texts, "user", cex = cex, font = font, family = family) gives accurate measurements of the width in user coordinates. I've got those set up to match pixels, so I'm fine here. However, the equivalent call for strheight() only measures height above the baseline according to the docs, and indeed the number is smaller than the size of what's displayed. Descenders (e.g. the tail of "y") aren't counted. Is there a way to measure how far a character might descend? Is it valid to assume it won't descend more than a line height below the top of the char? I have a partial solution -- textshaping::shape_text gives a "height" value that includes lots of space below the character, and a "top_border" value that measures from the top of the textbox to the baseline. So I think `height - top_border` would give me what I'm asking for. But this only works with graphics devices in the ragg package. Is there a general solution? Duncan Murdoch
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel <https://stat.ethz.ch/mailman/listinfo/r-devel>
Dr Paul Murrell Te Kura Tatauranga | Department of Statistics Waipapa Taumata Rau | The University of Auckland Private Bag 92019, Auckland 1142, New Zealand 64 9 3737599 x85392 paul at stat.auckland.ac.nz www.stat.auckland.ac.nz/~paul/