Friday, February 8, 2008

Weird behavior with ImageDrawText and optional Font Attributes

A few weeks ago I noticed some strange behavior when using ImageDrawText without supplying all three of the major text attributes: font, size and style. According to the documentation, the default font size is 10 pt. If you do not supply a font when using the function, text will be drawn in the default system font.

The behavior I observed on windows is a bit different:


  • If no font attributes were supplied, the text looks like Arial 12pt

  • If only a size was specified, the text looks like Times New Roman

  • If only a style was specified, the text looks like Times New Roman 10pt

  • If only a font was specified, the text looks like 10pt





As I understand it, this behavior is bug related. I did submit a question about this to livedocs. Unfortunately it was not approved due to a policy about bug information in the comments. So I thought I would post it here in case it is helpful to anyone. My conclusion: for the most consistent results, its probably best to always specify all three text characteristics: font, size, style.

Test Code

<!--- no attributes produces > Arial 12pt --->
<cfset img = ImageNew( "", 300, 80, "rgb", "lightgray" )>
<cfset ImageSetDrawingColor( img, "black" )>
<cfset ImageDrawText( img, "A string with no font attributes", 15, 15)>
<cfset ImageSetDrawingColor( img, "##006633" )>
<cfset attr = { font="arial", style="plain", size="12" }>
<cfset ImageDrawText( img, "Looks like Arial 12pt", 15, 35, attr)>
<cfimage action="writeToBrowser" source="#img#">

<!--- size only produces > Times New Roman 12pt --->
<cfset img = ImageNew( "", 300, 80, "rgb", "lightgray" )>
<cfset ImageSetDrawingColor( img, "black" )>
<cfset attr = { size=12 } >
<cfset ImageDrawText( img, "A string with size=12 only", 15, 15, attr)>
<cfset ImageSetDrawingColor( img, "##006633" )>
<cfset attr = { font="Times New Roman", style="plain", size="12" }>
<cfset ImageDrawText( img, "Looks like Times New Roman 12pt", 15, 35, attr)>
<cfimage action="writeToBrowser" source="#img#">

<!--- style only produces > Times New Roman 10pt --->
<cfset img = ImageNew( "", 300, 80, "rgb", "lightgray" )>
<cfset ImageSetDrawingColor( img, "black" )>
<cfset attr = { style="plain" } >
<cfset ImageDrawText( img, "A string with style='plain'", 15, 15, attr)>
<cfset ImageSetDrawingColor( img, "##006633" )>
<cfset attr = { font="Times New Roman", style="plain", size="10" }>
<cfset ImageDrawText( img, "Looks like Times New Roman 10pt", 15, 35, attr)>
<cfimage action="writeToBrowser" source="#img#">

<!--- font only produces > correct font size 10pt --->
<cfset img = ImageNew( "", 300, 80, "rgb", "lightgray" )>
<cfset ImageSetDrawingColor( img, "black" )>
<cfset attr = { font="Arial" } >
<cfset ImageDrawText( img, "A string with font='Arial'", 15, 15, attr)>
<cfset ImageSetDrawingColor( img, "##006633" )>
<cfset attr = { font="Arial", style="plain", size="10" }>
<cfset ImageDrawText( img, "Looks like Arial 10pt", 15, 35, attr)>
<cfimage action="writeToBrowser" source="#img#">

<!--- size and style produces > Times New Roman (size) (style)--->
<cfset img = ImageNew( "", 300, 80, "rgb", "lightgray" )>
<cfset ImageSetDrawingColor( img, "black" )>
<cfset attr = { size="12", style="bold" } >
<cfset ImageDrawText( img, "A string with size=12, style='plain'", 15, 15, attr)>
<cfset ImageSetDrawingColor( img, "##006633" )>
<cfset attr = { font="Times New Roman", style="bold", size="12" }>
<cfset ImageDrawText( img, "Looks like Times New Roman 12pt", 15, 35, attr)>
<cfimage action="writeToBrowser" source="#img#">

<!--- font and style produces > Arial (style) 10pt --->
<cfset img = ImageNew( "", 300, 80, "rgb", "lightgray" )>
<cfset ImageSetDrawingColor( img, "black" )>
<cfset attr = { font="Arial", style="plain" } >
<cfset ImageDrawText( img, "A string with font='arial', style='plain'", 15, 15, attr)>
<cfset ImageSetDrawingColor( img, "##006633" )>
<cfset attr = { font="Arial", style="plain", size="10" }>
<cfset ImageDrawText( img, "Looks like Arial 10pt", 15, 35, attr)>
<cfimage action="writeToBrowser" source="#img#">

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep