15
$\begingroup$

Is there a way to get the dimensions that the results of a Text[] will occupy within a Graphics[]? Specifically, if I run something like

Graphics[Text[
  Style["how quickly daft jumping zebras vex", 
   FontFamily -> "Verdana", FontSize -> 20]], PlotRange -> 1]

how can I get the dimensions relative to the coordinate system of the text?

edit: I have the PlotRange->1 in there because I'm using the text on a larger field and manipulating the text and other graphics using transformations that depend on the coordinate system. The text also is likely to change (hence my using sample text), so I'd like to have the transformations depend on measurements derived from the text (taking into account the styling of it).

$\endgroup$
8
  • $\begingroup$ Just a point, PlotRange is irrelevant in this object, even though it is visible in the AbsoluteOptions. You probably want Graphics[Text[ Style["how quickly daft jumping zebras vex", FontFamily -> "Verdana", FontSize -> 20]], ImageSize -> 400, AspectRatio -> 0.1] $\endgroup$
    – Verbeia
    Commented Feb 12, 2012 at 23:21
  • 1
    $\begingroup$ @Verbeia: but why 400 and 0.1? I had the PlotRange->1 in there because I'm using text on a larger field and manipulating the text and other graphics using transformations that depend on the coordinate system... and I suppose I should edit that information into the question. $\endgroup$
    – Isaac
    Commented Feb 12, 2012 at 23:25
  • $\begingroup$ Isaac, they were the round numbers that fit nicely on my system. I should probably have said "something like" that code. By the say, Chris' answer is a much better approach for your particular problem so I will delete my partial answer. Hopefully someone poses a question soon where these CurrentValue options are the answer, because I think they are worth documenting. $\endgroup$
    – Verbeia
    Commented Feb 13, 2012 at 0:27
  • 1
    $\begingroup$ @Verbeia please pose a question yourself even if you know the answer. I would like to read more about CurrentValue and fonts. $\endgroup$ Commented Feb 13, 2012 at 4:39
  • $\begingroup$ @MikeHoneychurch Ok :) mathematica.stackexchange.com/q/1684/8 and I actually don't know the answer $\endgroup$
    – Verbeia
    Commented Feb 13, 2012 at 4:56

2 Answers 2

9
$\begingroup$

Here you go:

t = Text[Style["how quickly daft jumping zebras vex", 
    FontFamily -> "Verdana", FontSize -> 20]];
{l, h} = d = Rasterize[t, "RasterSize"];
Graphics[{Green, Rectangle[{0, 0}, d], Black,
  Inset[t]}, PlotRange -> {{0, l}, {0, h}}, ImageSize -> l]

enter image description here

$\endgroup$
13
  • 2
    $\begingroup$ I can definitely use this to get the job done, but it's in terms of image size/pixels, rather than coordinates... $\endgroup$
    – Isaac
    Commented Feb 13, 2012 at 0:22
  • 1
    $\begingroup$ Actually... it looks like it's rasterizing the output form of the Text[] command: i.sstatic.net/YkeeK.png is the output of Rasterize[Text[Style["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris varius imperdiet lectus nec bibendum. Maecenas felis ipsum, elementum mattis tristique id, mollis a nisl. Nulla magna dolor, ullamcorper eleifend posuere non, vehicula ut augue.", FontFamily -> "Hypatia Sans Pro", FontSize -> 20], {0, 0}, {0, 0}]], so Rasterize[..,"RasterSize"] gives {724, 104} which isn't the length of the text on a single line... $\endgroup$
    – Isaac
    Commented Feb 13, 2012 at 0:34
  • $\begingroup$ @ Isaac - You can inset the resultant graphics into another graphic using coordinates, e.g. Graphics[{Red, Rectangle[{0, 0}, {1, 0.2}], Inset[%, {0, 0}, {0, 0}]}, PlotRange -> {{0, 1}, {0, 0.2}}, ImageSize -> 400] $\endgroup$ Commented Feb 13, 2012 at 0:44
  • $\begingroup$ The problem is the line wrapping—I want the width of the rendered text all on one line, but Rasterize[] is wrapping the text at the width of the window. $\endgroup$
    – Isaac
    Commented Feb 13, 2012 at 0:46
  • 4
    $\begingroup$ You can also use the option LineBreakWithin -> False in the options passed to Style if line wrapping is what you are trying to avoid. $\endgroup$
    – Andy Ross
    Commented Feb 13, 2012 at 2:35
3
$\begingroup$

Faced with the same problem, I found this solution:

a = Text[Style["vvddfhfh", 40], {0, 0}];
{width,height} = ImageDimensions[ImageCrop[Graphics[a]]]

which gives me the size of the actual rendering of the styled text, as opposed to the render of the output form of the Text command seen in the other answer

$\endgroup$

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.