MathGroup Archive 2010

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

Search the Archive

Re: how to scale text to a bounding rectangle?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110427] Re: how to scale text to a bounding rectangle?
  • From: John Fultz <jfultz at wolfram.com>
  • Date: Thu, 17 Jun 2010 07:10:57 -0400 (EDT)

Try this...

TextRect[text_, {{left_, bottom_}, {right_, top_}}] :==
 Inset[Pane[text, {Scaled[1], Scaled[1]},
   ImageSizeAction -> "ResizeToFit"], {left, bottom}, {Left,
   Bottom}, {right - left, top - bottom}]


A tiny demonstration...

Manipulate[
 Graphics[{TextRect["some text", {{0, 0}, {1, 1}}],
   TextRect["more text", {{5, 5}, {7, 9}}]},
  PlotRange -> {{0, n}, {0, n}}, Axes -> True,
  GridLines -> Automatic], {n, 1, 100}]

A warning that ImageSizeAction->"ResizeToFit" might not be completely robust. 
The problem it's solving is a difficult one, and Mathematica may come up with
solutions which aren't pixel perfect or with slightly unpredictable properties. 
For example, the Manipulate demonstrates some slightly unexpected line breaking
properties.  But it does generally work, and it's the only way I can think of to
solve your problem without producing some sort of iterative resize-measure sort
of technique.

Sincerely,

John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.


On Thu, 17 Jun 2010 02:03:59 -0400 (EDT), Michael wrote:
> The problem with both of these solutions is that I need the bounding
> rectangle to be in a Graphics[] expression because I'm creating several
> of these text filled boxes at arbitrary offsets.  I'm trying to create
> something similar to Graph[] (hopefully using it's embedding functions)
> but instead of vertexes I want to create collections of boxes containing
> text.  However Mathematica seems to render text independently of the
> coordinate range of the graphic.  For example, these two expressions
> produce identical output:
>
> Graphics[{
> Text[Style["Test", 12]],
> Circle[{0, 0}, 1]
> }
> ]
>
> Graphics[{
> Text[Style["Test", 12]],
> Circle[{0, 0}, 100]
> }
> ]
>
> I found something close to what I want to do, but it's ugly because the
> graphics don't scale nicely:
>
> Graphics[{
> FaceForm[], EdgeForm[Black],
> Raster[Reverse[Rasterize["Hello", "Data"]], {{0, 0}, {100, 16}}],
> Rectangle[{0, 0}, {100, 16}]
> }
> ]
>
> But I would have to hope that surely there is some way to put Text[] in
> Graphics[] such that the size of the text is proportional to the visible
> coordinate range of the Graphics[]. Even if I could only find the size
> of the text in a given image I could then scale it as needed.  I tried
> using Rasterize[string,"RasterSize"] but I couldn't figure out how to
> make it work.  Any ideas along that line?  I'm hoping that I'm just
> missing something obvious that I've overlooked in Mathematica's big API..  .
>
> Thanks,
>
> Michael
>
>
>> On 6/15/2010 2:29 AM, Michael wrote:
>>> How can I automatically scale Text[] to fit into a bounding rectangle
>>> of
>>> a size that I specify?
>>>
>>> For instance, I can get a pretty close result here, but it is not
>>> perfect (the text doesn't touch the bottom edge of the rectangle), and
>>> "36", {0,0}, and {-1,-1} are all numbers I had to find through trial
>>> and
>>> error:
>>>
>>> Graphics[{
>>> FaceForm[], EdgeForm[Black],
>>> Rectangle[{0, 0}, {100, 10}],
>>> Text[Style["Hello", 36], {0, 0}, {-1, -1}]
>>> }
>>> ]
> On 6/16/2010 2:41 AM, Alexei Boulbitch wrote:
>> Hi, Michael,
>>
>> try this:
>>
>> Manipulate[
>>
>> Framed@Pane[Graphics[Text[Style["ABcdEG", n]]], {m1, m2},
>> Alignment -> Center],
>>
>> {n, 10, 30}, {m1, 50, 200}, {m2, 20, 200}]
>>
>> and play with parameters to chose those fitting your needs.
>> Have fun, Alexei
> On 6/16/2010 2:42 AM, Helen Read wrote:
>> Framed[Text[Style["Hello"]]]
>>
>> If you want the 36 point font, that's fine too.
>>
>> Framed[Text[Style["Hello", 36]]]
>>
>> You can set FrameMargins on any or all sides.
>>
>> Framed[Text[Style["Hello", 36]], FrameMargins -> {{10, 200}, {0, 0}}]
>>
>> Framed[Text[Style["Hello", 36]], FrameMargins -> 20]



  • Prev by Date: Re: how to scale text to a bounding rectangle?
  • Next by Date: possible bug with fraction box shortcut ctrl+/
  • Previous by thread: Re: how to scale text to a bounding rectangle?
  • Next by thread: Re: how to scale text to a bounding rectangle?