This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

HOWTO: Automatically Fit Text Size in Rectangle

This sample is for Graphics Mill 5 and is not compatible with the recent version of SDK.

This article describes how to calculate the font size so that the text string fit completely into the specified rectangle.

It may turn out that you need to draw arbitrary text string on a piece of image with fixed dimensions. For example, you may need to draw a string entered by the user in some placeholder on template image. Most likely you do not care how the text string will be looking like. More important would be to fit entire string into the specified placeholder.

The most obvious way to adjust font size depending on the text length. It is evident that the longer is the text string, the smaller text size should be, and vice versa.

Let's examine how to do it.

  1. Initialize the font settings with some value. The font size should be the maximum the placeholder may fit (or even larger).
  2. Measure the text size using these font settings. The Aurigma.GraphicsMill.Drawing.Font class features the MeasureText method which returns the bounding rectangle of the text (i.e. the rectangle occupied by the text when it will be drawn with certain font settings).
  3. If the height of the bounding rectangle is larger than the placeholder size, we need to correct it. To do it, just set the same font size as placeholder rectangle height minus line spacing (internal leading).
  4. Measure the text with new font size again. Obviously the text height will be fine, but it is possible the length of the text exceeds the placeholder rectangle width. To check it, just compare the width of the bounding rectangle with placeholder width. If the string is too long, reduce the font size proportionally to the difference between these two widths.

The code sample below demonstrates how to do it.

// Source string and rectangle.
System.String text = "Aurigma Inc.";
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(50, 50, 100, 50);
 
// Bitmap onto which to render text.
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(Aurigma.GraphicsMill.RgbColor.Green,
 	300, 300, Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);
 
// Initialize font settings.
Aurigma.GraphicsMill.Drawing.Font font = new Aurigma.GraphicsMill.Drawing.Font("Arial", 50);
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Center;
font.VerticalAlignment = Aurigma.GraphicsMill.Drawing.VerticalAlignment.Center;
 
System.Drawing.SizeF size = System.Drawing.SizeF.Empty;
 
// Vertical scale.
size = font.MeasureString(text);
if(size.Height > rect.Height)
{
	font.Size = rect.Height - font.InternalLeading;
}

// Horizontal scale.
size = font.MeasureString(text);
if(size.Width > rect.Width)
{
	int dif = (int)(size.Width - rect.Width);
	font.Size -= dif * size.Height / size.Width;
}

// Text string output.
Aurigma.GraphicsMill.Drawing.GdiGraphics graphics = bitmap.GetGdiGraphics();
try
{
	Aurigma.GraphicsMill.Drawing.SolidBrush brushText = new Aurigma.GraphicsMill.Drawing.SolidBrush(
		Aurigma.GraphicsMill.RgbColor.Black);
	Aurigma.GraphicsMill.Drawing.Pen penRect = new Aurigma.GraphicsMill.Drawing.Pen(
		Aurigma.GraphicsMill.RgbColor.White, 1);

	graphics.DrawString(text, font, brushText, new System.Drawing.Point(rect.Left + rect.Width / 2, 
		rect.Top + rect.Height / 2 + (int)font.InternalLeading));

	graphics.DrawRectangle(penRect, rect);
}
finally
{
	graphics.Dispose();
}

bitmap.Save(@"c:\1.png");
bitmap.Dispose();