Skip to content
Prev 61907 / 63424 Next

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: