MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: text bounding box size

  • To: mathgroup at smc.vnet.net
  • Subject: [mg64521] Re: text bounding box size
  • From: Frederick Soo <fsoo at u.washington.edu>
  • Date: Mon, 20 Feb 2006 06:29:58 -0500 (EST)
  • Organization: University of Washington
  • References: <dshfie$hna$1@smc.vnet.net> <dsk838$i3j$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

hi Brian,

Thanks for your suggestion. Your example helped me to figure out a 
(kludgy) way to parse the PostScript generated by 
DisplayString[Graphics[{Text["string"]}]].

Here's a function that (roughly) returns the dimensions {width, height} 
of Text[expr], in points.

TextBoxSize[str_] :=Module[{displaystr,dims},
       displaystr = 
DisplayString[Graphics[{Text[str,{0,0},{-1,-1},{1,0},Background\[Rule]RGBColor[1,0,0]]}]];
       dims = 
StringCases[displaystr,RegularExpression["(\\s+(\-*\\d+\\.\\d+|\-*\\d+)\\s+(\-*\\d+\\.\\d+|\-*\\d+)) 
Mabsadd"]\[RuleDelayed]{ToExpression["$2"],ToExpression["$3"]}];

       dims[[2]]
       ];

In[28]:=
TextBoxSize[StyleForm["blah",FontFamily\[Rule]Helvetica, FontSize\[Rule]16]]
Out[28]=
{34.125,16.125}

Note that this is done by adding a Background box to the text, feeding 
it to DisplayString, and grabbing the background box size from the 
generated PostScript string.  This Background box (at least in 5.2, both 
on Macs and PCs, at least) is not perfect - for font sizes smaller than 
about 10pts, it is too tall and too wide!

Show[Graphics[{Text[StyleForm["abcdefg",
          FontFamily -> "Courier", FontSize -> 4], {0, 0}, Background ->
             RGBColor[1, 0, 0]]}]]

The routine is also a little bit slow, as it both generates the PS and 
parses it.

If there are other bugs, or anyone has a solution that is any better, 
please let me know!

thanks,

Fred


bghiggins at ucdavis.edu wrote:
> Fred, Try this:
> 
> Suppose you have the graphic
> plt=Plot[Sin[x],{x,0,2p}];
> 
> Convert the graphic to a DisplayString with the "EPS" format.i.e.,
> 
> DisplayString[plt,"EPS"]
> 
> The size of the Bounding box info for the graphic is near the top of
> the String file and begins like:
> 
> %%BoundingBox: 88 4 376 182
> 
> You can use  StringCases with  an appropriate RegularExpression  to
> extract out the values:
> Here is an example:
> 
> res=Flatten[StringCases[
> 
> DisplayString[plt,"EPS"],RegularExpression["%%BoundingBox:(\\s+(\\d+)\\s+(\
> \\d+)\\s+(\\d+)\\s+(\\d+))"]:>{ToExpression["$2"],ToExpression["$\
> 3"],ToExpression["$4"].ToExpression["$5"]}]]
> 
> {88,4,376.182}
> 
> The format for the Bounding Box data is xo,yo,x1,y1, where (x0,y0) and
> (x1,y1) are the coordinates of the lower-left and upper-right corners,
> respectively.
> measured in points.
> 
> Hope this helps
> 
> Cheers,
> 
> Brian
> 
> 
> 
> Fred Soo wrote:
> 
>>hi,
>>
>>I know this has been asked before, but I haven't seen any response.  Has
>>anyone found an easy way to get the bounding box size (height, width) of
>>a Text expression for a given DisplayFunction?  Or for that matter, the
>>size of any Graphics object being displayed?
>>
>>I've tried a few things, but this is way too difficult, and there aren't
>>even any kludgy workarounds.
>>
>>For instance, to get text height, I tried to pull the FontSize->x out of
>>any Text expression.  This is ok, but of course, doesn't work if no
>>FontSize is specified.
>>
>>As for reading the default TextStyle, there doesn't seem to be a way
>>unless you use $TextStyle.  In addition, if you use Show[expr,
>>DefaultFont:>x] to change all of the fonts in a graphic, it doesn't
>>appear to modify $TextStyle or pass DefaultFont->x, so there is no way
>>to tell what font x is.
>>
>>Text width is kind of hopeless, as there doesn't seem (unless somebody
>>knows, please let me know) a way of getting a font metric.
>>
>>I've looked at the PostScript output from DisplayString, and can find
>>some bits and pieces which make some sense, but not being a PostScript
>>expert, this would take a lot of time to figure out.
>>
>>Any suggestions?
>>
>>Thanks!
>>
>>Fred
>>
>>Department of Physiology and Biophysics
>>University of Washington
>>Seattle, WA 98199
>>(206) 616-2510
> 
> 


  • Prev by Date: Re: Plot - LinearFilter/List Problem
  • Next by Date: Re: Re: Using a text editor like interface for Mathematica?
  • Previous by thread: Re: text bounding box size
  • Next by thread: Awesome PDF writer