Starting with Version 1.8, AspJpeg provides a more versatile method for text drawing than PrintText.
The new method, PrintTextEx, uses the FreeType™ open-source library to
produce high-quality anti-aliased text rendering on any background, in any color and size, and at any angle.
PrintTextEx offers the following new features:
- Text rendering with PrintTextEx is always anti-aliased regardless of background.
Font.Quality and Font.BkMode properties are ignored.
(the old PrintText requires the background to be solid for anti-aliasing to work.)
- The font to be used is specified via a physical path. Therefore, the font does not have to be properly
registered on the machine as long as a path to the font file is known.
Both TrueType/OpenType and Type 1 fonts are supported.
- Automatic word wrapping. CR/LF characters can be used in the text string for hard line breaks.
- Text alignment to the left, right, center, and justified.
- Adjustable opacity for image watermarking.
- Text can be rotated around the origin.
- For multi-line text, line spacing can be adjusted.
- The method returns the width of the string being drawn, and the height of the text
paragraph (the latter via a separare property Canvas.ParagraphHeight.)
PrintTextEx expects 4 parameters: the Unicode text string to draw, the x and y coordinates, and the
physical path to a TrueType/OpenType or Type 1 font to draw it with. The coordinates
specify the position of the lower-left corner of the first character of the first line of text to be drawn,
in the image coordinate system. Text size (in pixels) and color are specified via Font.Size and Font.Color
properties, respectively. For example:
Jpeg.Canvas.Font.Size = 20
Jpeg.Canvas.Font.Color = &HFFFF0000 'Red
Jpeg.Canvas.PrintTextEx "ABC", 10, 50, "c:\Windows\Fonts\Arial.ttf"
If the location of the Windows directory is not known in advance, the property Jpeg.WindowsDirectory
(added in Version 1.8) should be used, so the last line may be rewritten as follows:
...
Jpeg.Canvas.PrintTextEx "ABC", 10, 50, Jpeg.WindowsDirectory & "\Fonts\Arial.ttf"
As mentioned above, PrintTextEx supports automatic word wrapping. This
functionality is enabled by setting the property Font.Width to a non-zero value (in pixels).
The text to be drawn can also contain CR/LF characters for hard line breaks.
If Font.Width is specified, text alignment can also be specified via Font.Align. The supported values
are 0 (left, default), 1 (right), 2 (center) and 3 (justified). You can also adjust the distance (in pixels)
between the lines via Font.Spacing. A positive value increases the default
line spacing, a negative one decreases it.
The justified text on the image below was produced by the following code snippet:
Text = "There is some confusion about the number of ..."
jpeg.Canvas.Font.Color = &HFFFFFFFF ' white
jpeg.Canvas.Font.Align = 3 'justify
jpeg.Canvas.Font.Width = 300
jpeg.Canvas.Font.Size = 15
jpeg.Canvas.Font.Spacing = 2
jpeg.Canvas.PrintTextEx Text, 210, 160, "c:\Windows\Fonts\Arial.ttf"
For image watermarking purposes, text opacity can be modified by setting the Font.Opacity property
to a value between 0 (fully transparent) and 1 (fully opaque). The image below was created via
the following code:
Text = "Copyright © 2007 John Smith Stock Photography, Inc."
jpeg.Canvas.Font.Size = 18
jpeg.Canvas.Font.Opacity = 0.5
jpeg.Canvas.PrintTextEx Text, 160, 230, "c:\Windows\Fonts\Arial.ttf"
Text rotation is activated by setting the Font.Rotation property (in degrees). Text is rotated
around the point specified by the x and y coordinates described above. A positive Rotation value
produces a counter-clockwise rotation.
As of Version 2.1, PrintTextEx also supports text underlining.
To enable this feature, set the property Font.Underlining to True.
The width and color of the line used for the underlining are specified via the properties
Pen.Width and Pen.Color, respectively.
PrintTextEx returns the width of the text string being drawn (in pixels) or, in case of multi-line text,
the width of the longest line in the paragraph.
Starting with version 1.9, the height of the text paragraph rendered by
the most recent call to PrintTextEx
can be obtained from the read-only property Canvas.ParagraphHeight.
The following code sample displays a text string in a round-about manner.
VB Script:
<%
' Directory with images
Path = Server.MapPath("../images")
Set Jpeg = Server.CreateObject("Persits.Jpeg")
' Font path
FontPath = Jpeg.WindowsDirectory & "\fonts\courbd.ttf"
Jpeg.Open Path & "\apple.jpg"
' Text to draw
Txt = "* APPLES ARE A GREAT SOURCE OF VITAMINS"
Jpeg.Canvas.Font.Color = &HFFFFFF00
Jpeg.Canvas.Font.Size = 60
' Draw letters in a round-about manner
Angle = -275
For i = 1 To Len(Txt)
x = 280 * cos( Angle * 3.141592 / 180 ) + Jpeg.Width / 2
y = 280 * sin( Angle * 3.141592 / 180 ) + Jpeg.Height / 2
Jpeg.Canvas.Font.Rotation = 270 - Angle
w = Jpeg.Canvas.PrintTextEx(Mid(txt, i, 1), x, y, FontPath)
Angle = Angle + w / 3.85
Next
Jpeg.Save Server.MapPath("text.jpg")
%>
|
C#:
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="ASPJPEGLib" %>
<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
// Source directory with images
String strPath = Server.MapPath("../images");
IASPJpeg objJpeg;
objJpeg = new ASPJpeg();
// Font path
String strFontPath = objJpeg.WindowsDirectory + "\\fonts\\courbd.ttf";
objJpeg.Open( strPath + "\\apple.jpg" );
// Text to draw
String strTxt = "* APPLES ARE A GREAT SOURCE OF VITAMINS";
objJpeg.Canvas.Font.Color = 0xFFFF00;
objJpeg.Canvas.Font.Size = 60;
// Draw letters in a round-about manner
double fAngle = -275;
for( int i = 0; i < strTxt.Length; i++ )
{
int x = (int)(280 * Math.Cos( fAngle * Math.PI / 180.0 ) + objJpeg.Width / 2);
int y = (int)(280 * Math.Sin( fAngle * Math.PI / 180.0 ) + objJpeg.Height / 2);
objJpeg.Canvas.Font.Rotation = 270 - (int)fAngle;
float w = objJpeg.Canvas.PrintTextEx( strTxt.Substring(i, 1), (int)x, (int)y, strFontPath );
fAngle = fAngle + w / 3.85;
}
objJpeg.Save( Server.MapPath("text.jpg") );
TextImage.Src = "text.jpg";
}
</script>
|
Click the links below to run this code sample:
http://localhost/aspjpeg/manual_05/05_text.asp
http://localhost/aspjpeg/manual_05/05_text.aspx