[Rd] Tight bounding box around text in graphics?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Tue Sep 26 11:57:41 CEST 2023


Thanks!  I said "base graphics" in my question, but I really have no 
objection to using grid graphics, so I'll explore those grid functions.

Duncan Murdoch

On 25/09/2023 3:53 p.m., Paul Murrell wrote:
> 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 using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>> <https://stat.ethz.ch/mailman/listinfo/r-devel>
>



More information about the R-devel mailing list