Fitting a Text Box to the Text

S

Stanley Wileman

I've almost finished a version of PIC (of TROFF ancestry) that emits RTF,
but still have a problem fitting a text box to a given string of text (to
control the placement of the text in the picture). I'm using the
GetTextExtentPoint32 API to calculate the text extent, and a text box with 0
internal margins. Does anyone reading this know how Word handles this? How
does the width of the text box lines affect the maximum text extent? Here's
a little sample that (tries to) display 20 capital Ws and a period in a text
box. GetTextExtentPoint32 returns 4560 twips (after appropriate conversion),
and that's what's used in the RTF. Thanks in advance for any assistance!

--Stan Wileman

{\rtf1\ansi\deff0
{\fonttbl
{\f0 Times New Roman;}
}
{\stylesheet
{\s1 \i\fs12\f0 \sbasedon0 My Style;}}
{\pard \par}
{\*\do\dobxcolumn\dobypara\dodhgt9000
\dptxbx\dptxlrtb{\dptxbxtext\pard\plain\ql
\li0
\ri0
\widctlpar
\aspalpha
\aspnum
\faauto
\adjustright
\rin0
\lin0
\itap0
\fs24
\cgrid
{WWWWWWWWWWWWWWWWWWWW.\par}
}\dpx9\dpy420\dpxsize4560\dpysize210\dplinew10
 
C

Cindy M -WordMVP-

Hi Stanley,

My suggestion would be to use a FRAME, rather than a text box. You can set the
width to "auto" so that it would always match the length of the text it
contains...
I've almost finished a version of PIC (of TROFF ancestry) that emits RTF,
but still have a problem fitting a text box to a given string of text (to
control the placement of the text in the picture). I'm using the
GetTextExtentPoint32 API to calculate the text extent, and a text box with 0
internal margins. Does anyone reading this know how Word handles this? How
does the width of the text box lines affect the maximum text extent? Here's
a little sample that (tries to) display 20 capital Ws and a period in a text
box. GetTextExtentPoint32 returns 4560 twips (after appropriate conversion),
and that's what's used in the RTF. Thanks in advance for any assistance!

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

Stanley Wileman

Thanks for the suggestion, Cindy. I may give it some thought. As I
mentioned, the text boxes are "invisible" (hollow lines) and are used only
to allow precise positioning of text relative to other picture elements. I
therefore need to know -- during picture "creation" -- exactly how long the
text will be. PIC allows me to do things like this:

A: "Hello, world!"
circle rad 0.5 with .s at A.nw

This says to display the text "Hello, world!" centered at the current
location and then to draw a circle with a 0.5-inch radius with its "south
end" (bottom) at the northwest corner of the text labeled "A". This location
should be precisely the upper left corner of the smallest invisible text box
that will completely enclose "Hello, world!" But how in the world do I
generate the appropriate RTF for the circle unless I know precisely where
the northwest corner of the text box will be? It's all well and good to have
Word dynamically resize things, but *I* need to know *HOW* it's going to do
that dynamic resizing. And if I know that, then I don't need (and probably
don't want) any dynamic resizing going on anyway.

What makes things even worse is that the dynamic resizing doesn't work in
all cases, either. I saw in one of the FAQs a bit of VBA code that
"dynamically" resized text to fit a given text box by starting at 2.0 points
for the font size and working upward in 0.5-point increments until the
"Overflowing" property for the textbox was true, and then backing up 0.5
point. I tried doing something similar, but instead of changing the size of
the text, I changed the size of the text box using a binary-search-like
approach to "zero in" on the text box width that just encloses the text.
Well, in some cases when the Overflowing property is false, the text is
still clipped by the textbox. Bah, humbug! The first strings I was trying
were sequences of capital Ws followed by a period. I thought perhaps Word
was doing something special with the period, so I then tried a lowercase "i"
followed by sequences of capital Ws. Same inconsistent behavior -- sometimes
success, sometimes failure.

So, I'm still looking for "the truth about text boxes," and doing ad-hoc
things to make the text boxes large enough. It's very dissatisfying,
though...

Again, thanks for the help!

--Stan Wileman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top