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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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
Course of the Month12 days, 15 hours left to enroll

777 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