Search Persits Software Knowledge Base

 

Article PS070123158

HOWTO: Render Anti-Aliased Text with Wrapping and Alignment

Problem Summary

Prior to Version 1.8, AspJpeg was unable to render high-quality antialiased text over an image. Word wrapping and text alignment were not supported. Also, text could only be rendered using system-registered fonts. Direct use of font files was not possible.

This article discusses the usage of the PrintTextEx method (introduced by Version 1.8) which supports antialiasing and many other features.

Solution

Anti-Aliasing & Font Usage

AspJpeg's PrintTextEx method is based on the FreeType™ open-source library which provides high-quality anti-aliased text rendering regardless of background. Previous versions of AspJpeg could only render anti-aliased text on solid opaque backgrounds. The following code snippet draws a 30-pixel-high text string in red color using the Arial TrueType font:

Jpeg.Canvas.Font.Size = 30
Jpeg.Canvas.Font.Color = &HFFFF0000
Jpeg.Canvas.PrintTextEx "Hello World!", 10, 20, "c:\Windows\Fonts\Arial.ttf"

The 1st argument specifies the string to be rendered in Unicode format.

The 2nd and 3rd arguments (X and Y) specify the lower-left corner of the first character of the text string relative to the upper-left corner of the image.

The 4th argument specifies the path to the font to be used for the rendering. Both TrueType/OpenType and Type 1 fonts are supported. The font does not need to be properly registered on the system as long as the font file is accessible to the script.

Word Wrapping & Text Alignment

PrintTextEx is capable of rendering a text string within a given horizontal space. The width of the space is specified via the Font.Width property in pixels. The method will automatically wrap the words, and also recognize the CR/LF characters within the string as hard line breaks.

In addition to word-wrapping, PrintTextEx also provides text alignment to the left (default mode), right, center and justified. Text alignment is specified via the Font.Align property. The valid values are: 0 (left), 1 (right), 2 (center) and 3 (justified). Note that if Font.Align is set to a value other than 0, Font.Width must also be specified.

The following code snippet displays a text string within a 300-pixel space and aligns it to the right:

Jpeg.Canvas.Font.Width = 300
Jpeg.Canvas.Font.Align = 1
Jpeg.Canvas.PrintTextEx "A long string", 10, 20, "c:\Windows\Fonts\Arial.ttf"

Opacity

For image watermarking purposes, text can be rendered on the image in a semi-transparent fashion. The degree of transparency is specified via the Font.Opacity property. The default value of 1 means full opacity (the background does not show through the text at all). A value less than 1 makes the text partially transparent, and the value of 0 makes it fully transparent (and thus invisible). The following code snippet displays text at 50% transparency:

Jpeg.Canvas.Font.Opacity = 0.5
Jpeg.Canvas.PrintTextEx "Watermark", 10, 20, "c:\Windows\Fonts\Arial.ttf"

Rotation

PrintTextEx can display a text string at any angle specified by Font.Rotation (in degrees). The default value of 0 means horizontal orientation (no rotation.) Text is rotated around the point specified by the (X, Y) arguments. A positive Rotation value means counter-clockwise rotation.

The following snippet displays a string vertically (rotated 90 degrees counter-clockwise):

Jpeg.Canvas.Font.Rotation = 90
Jpeg.Canvas.PrintTextEx "Vertical String", 10, 20, "c:\Windows\Fonts\Arial.ttf"

The latest version of AspJpeg can be downloaded from www.aspjpeg.com.

Created: 1/23/2007 6:43:59 PM
Last Modified: 1/25/2007 3:08:38 PM

Copyright © Persits Software, Inc. 1998 - 2018
For technical support, write to support@persits.com.