Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Font Sizing

Posted on 2003-03-17
Medium Priority
Last Modified: 2013-12-02
I am trying to size Text to fit a box. I want the text to fit as closely as possible. For example I have the word "Super" and I want to fit it in a box 40 pixels X 60 pixels. What do I need to tweak in order to accomplish this. I have tried messig with the point size but that dosn't give the disired effect.

Apps like corel draw, arts and letters, openoffice, etc. do this sort of thing. I just thought that maybe someone had some experience with tweaking fonts. The fonts have to be tweaked in many different ways. Some situations will produce short fat letters and others will produce tall skinny letters. I realize that it may be impossible to get a perfect fit within the box but some reasonable estimate should be possible.

Question by:qrstuvlmnop

Expert Comment

ID: 8155122
My best guess, and it is a guess, would be to draw the text to a bitmap of size TextWidth(Text) x TextHeight(Text) and then StretchDraw that bitmap to the desired rectangle.
LVL 34

Accepted Solution

Slick812 earned 60 total points
ID: 8155991
hello qrstuvlmnop , there are several ways to size fonts for a word to fit a certain width, but to fit both the width and height, might be a problem, I would use a TMetaFile, and the suggestion of SimesA, ,  of resizing it to the dimentions you need. maybe like this -

procedure TForm1.button_FitTextClick(Sender: TObject);
Emf1: TMetaFile;
Emf1 := TMetafile.Create;
  Emf1.Enhanced := True;
  Emf1.Transparent := True;
  Canvas.Font.Name := 'Times New Roman';
  Canvas.Font.Height := -20;
  Emf1.Width := Canvas.TextWidth('Super');
  Emf1.Height := Canvas.TextHeight('Super');
{set the Emf height and width to the textHeight and textWidth}
  with TMetafileCanvas.Create(Emf1, 0) do
      SetBkMode(Handle, 1);
  {set the background Mode to Transparent}
  Canvas.Draw(100,200, Emf1);
  Emf1.Width := 60;
  Emf1.Height := 40;
{unlike a Bitmap, when you change the size of a MetaFile
everything on the metafile changes size to match the new dimentions
also there is No pixelization, when you make it larger, even
very much larger}
  Canvas.Draw(120,260, Emf1);
  Emf1.Width := 340;
  Emf1.Height := 280;
  Canvas.Draw(20,240, Emf1);


 - - - - - - - - -  - - - - - - - - - - - - - -
you can resize the Emf1, by just changing the with and height, and when you draw it on a canvas it will appear like a text draw. . .  ask questions if you need more

Expert Comment

ID: 8157646
How about to change the font size of the textbox in order  to either get the TextWidth=60 or TextHeight=40?

Author Comment

ID: 8182468
Thank You, Slick812.
This Solution Works quite well.
I am also working on a solution that will envolve converting the font outline to a bezier curve and then resizing the curve to fit the box.  I'll post my solution if I ever get it to work.
LVL 34

Expert Comment

ID: 8182959
I have tried that, but it takes more than I could do to get the font drawing functions from the font file. . .  Good Luck, and Post the code if you are able to do that

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question