Pixel coordinates/length of a string in legend

Hi. I wanted to know if there is any way to get the pixel coordinates of a legend’s label text. I wanted to draw bounding boxes around the text like this:

I could not get the get_window_extent() to work here unlike the other chart elements.

If getting the coordinates is not possible, can I get the length of a string in pixels, given the fontfamily and fontsize?

Thanks