17
$\begingroup$

Regardless of AspectRatio, the Text boxes overlap.

xmlObject = Uncompress[FromCharacterCode[
    Flatten[ImageData[Import["https://i.sstatic.net/AG4GB.png"], "Byte"]]]];
TreeForm[#, AspectRatio -> 1/3, ImageSize -> 700] &@
 Cases[xmlObject, XMLElement[tag : _, _, value : _] :> (tag -> value),
   Infinity]

Mathematica graphics

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?

$\endgroup$
3

2 Answers 2

10
$\begingroup$

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]]

Mathematica graphics

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}]
}]

Mathematica graphics

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}]
}]

Mathematica graphics

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

$\endgroup$
4
  • 3
    $\begingroup$ 97 = 2 * 42 + 13. // 42 and 13 are two known universal constants. The "2" could come from the renormalization process. $\endgroup$ Commented Apr 12, 2012 at 5:55
  • $\begingroup$ @belisarius ;-D $\endgroup$ Commented Apr 12, 2012 at 8:50
  • $\begingroup$ What happens to these "universal constants" once Style options are applied to Text? $\endgroup$ Commented Apr 12, 2012 at 14:34
  • 2
    $\begingroup$ @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. $\endgroup$ Commented Apr 12, 2012 at 19:23
7
$\begingroup$

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"]

Mathematica graphics

textbox = Rasterize[text, "RasterSize"]

pos = {5, 10}

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

Mathematica graphics

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.

$\endgroup$
4
  • $\begingroup$ 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.) $\endgroup$ Commented May 28, 2012 at 1:58
  • $\begingroup$ 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. $\endgroup$ Commented May 28, 2012 at 2:09
  • $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Commented May 28, 2012 at 6:48
  • $\begingroup$ ok excellent. Will review more in-depth then. $\endgroup$ Commented May 28, 2012 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.