# Computing the bounding boxes for Text objects

Regardless of AspectRatio, the Text boxes overlap.

TreeForm[#, AspectRatio -> 1/3, ImageSize -> 700]& @
Cases[
Import["http://www.weather.gov/data/current_obs/KOAK.xml", "XMLObject"],
XMLElement[tag:_, _, value:_] :> (tag -> value), Infinity
]


More generally, I've asked Wolfram Research, Inc. about exposing bounding box coordinates for Text objects so that layout manager or engine can test overlap or compute tilings or disjoint positioning, but it appears to be complicated by the various Text options.

Does anyone know how to at least conservatively approximate the Text bounding box?

-
Consider this answer stackoverflow.com/a/4137213/353410 –  belisarius Apr 11 '12 at 20:50
In the Word Cloud question you'll probably find some methods to detemine bounding boxes of Text. –  Sjoerd C. de Vries Apr 11 '12 at 21:36
You may also be interested in using the function stretchText I applied in [mathematica.stackexchange.com/questions/4374/… answer). It's sort of the reverse, in that it takes dimensions that you specify and then deforms the text to fit into that prescribed area. –  Jens Apr 18 '12 at 0:02

Here a test text. I use a gray background to show how large the bounding box actually is.

t = Graphics[Text["Test", BaseStyle -> {128}, Background -> Gray]]


d = ImageDimensions[ImageCrop[t]];
Graphics[{
FaceForm[], EdgeForm[Red],
Text["Test", {0, 0}, {-1, -1}, BaseStyle -> {128}],
Rectangle[{0, 0}, Offset[d, {0, 0}]],
PointSize[0.05], Point[{0, 0}]
}]


If you want a closer cut for your bounding box, just remove the Background option and ImageCrop again. However, note that offsets in the Text function are with respect to the bounding box Mathematica uses (indicated by the gray box).

t = Graphics[Text["Test", BaseStyle -> {128}]];
d = ImageDimensions[ImageCrop[t]];
Graphics[{
FaceForm[], EdgeForm[Red],
Text["Test", {0, 0}, {-.97, -0.6}, BaseStyle -> {128}],
Rectangle[{0, 0}, Offset[d, {0, 0}]],
PointSize[0.05], Point[{0, 0}]
}]


Note the {-.97, -0.6} offset?

-
97 = 2 * 42 + 13. // 42 and 13 are two known universal constants. The "2" could come from the renormalization process. –  belisarius Apr 12 '12 at 5:55
@belisarius ;-D –  Sjoerd C. de Vries Apr 12 '12 at 8:50
What happens to these "universal constants" once Style options are applied to Text? –  alancalvitti Apr 12 '12 at 14:34
@alancalvitti Belisarius was joking, of course. The offset values for the close-shave bounding box depend on the size of the bounding box MMA works with, and that box depends on the Style options. –  Sjoerd C. de Vries Apr 12 '12 at 19:23

If you are willing to settle for offset coordinates, you can use Rasterize to get the size of the text box.

text = Style["Text box box", Bold, Large, FontFamily -> "Times"]


textbox = Rasterize[text, "RasterSize"]

pos = {5, 10}

Graphics[{Text[text, pos, {-1, -1}], FaceForm[None], EdgeForm[Black],
Rectangle[pos, Offset[textbox, pos]]}, Frame -> True]


The key was the "RasterSize" element in Rasterize. You can use "BoundingBox" too, which will give you the baseline of the text.

You will notice that I drew the bounding box of the text (the Rectangle) using Offset coordinates, not in plot coordinates. You could use offset coordinates for every measurement and position specification of your text primitives to align them properly, but this will be inconvenient. It would be much better if we could easily convert between plot coordinates and offset coordinates. This is unfortunately quite messy and it will depend on the size of the graphic.

Offset coordinates are in printer's points. By default, Mathematica graphics are 360 points wide. If you know the exact PlotRange of your figure, and you can eliminate all padding outside the actual plot range (PlotRangePadding and ImagePadding, I'm not sure about ImageMargins), then you can use the known plot width to convert between offset and plot coordinates. Unfortunately, this conversion will only be valid for a given size of the graphic (the default size). It will not be valid any more if you resize it.

-
Thank you for the effort. Unfortunately we can't rasterize as a key goal is to develop layout engines for real time ZUIs for Health IT (partially using W3C tech standards like HTML5/CSS/SVG, in a way similar to or ever using Stanford's Data Driven Documents (D3) where the text labels, like other graphics objects, are going to get zoomed in and out of.) –  alancalvitti May 28 '12 at 1:58
Just wanted to say these are all good answers. But WRI can't come up w/ a satisfactory solution either. Likely only RWI has the resources to map out all the variations in Style for text. By the way, although bounding box coordinates are not exposed, obviously the boxes generated by expressions like TreeForm do in fact "bound tightly" around Text. –  alancalvitti May 28 '12 at 2:09
@alancalvitti I wrote this answer in a rush yesterday, and it needs some improvement, which I'll do shortly ... but I wanted to note that this solution does not rasterize the text, it merely retrieves its size in pixels. –  Szabolcs May 28 '12 at 6:48
ok excellent. Will review more in-depth then. –  alancalvitti May 28 '12 at 16:41