Tuesday, March 25, 2008

ImageDrawText - Negative font sizes give text a new spin

I discovered today (completely by accident) that using a negative font size gives image text a whole new spin. It rotates the text 180 degrees. Similar code in java produces the same results. So it must be related to the underlying java classes.


The following ColdFusion code produces the image below.

Negative Font Size Example


<cfset img = ImageNew("", 200, 100)>
<cfset attr = { font="Arial", style="bold", size="-20" }>
<cfset ImageDrawText(img, "Transform Me", 190, 10, attr)>
<cfimage action="writeToBrowser" source="#img#">




The one code gotcha is the x coordinate. Since the text is rotated 180 degrees, the x coordinate is now relative to the right hand side. So if the desired x position is 10, you must use x = ImageGetWidth(img)-10 instead of x = 10.

Now I have not seen this documented anywhere. So I would use ImageRotateDrawingAxis instead. But it was still an interesting find ;)

Update: I corrected a typo in example/function name.

ImageRotateDrawingAxis

<cfset img = ImageNew("", 200, 100)>
<cfset ImageRotateDrawingAxis(img, 180, 100, 10)>
<cfset attr = { font="Arial", style="bold", size="20" }>
<cfset ImageDrawText(img, "Transform Me", 10, 10, attr)>
<cfimage action="writeToBrowser" source="#img#">

5 comments:

Lemonhead March 27, 2008 at 9:32 PM  

You have a great blog here! I've been using iText for a couple years in a java environment. Over the past few weeks I've been doing a CF project that required a little iText work, and your blog has been invaluable!!

I have a question for you. Have you tackled extending PdfPageEventHelper in CF?

cfSearching March 28, 2008 at 7:54 AM  

@Lemonhead,

Thanks. I am very glad the CF examples were helpful :)

No, I have not. But you have piqued my curiosity. What are you trying to do with page events?

Lemonhead March 28, 2008 at 12:47 PM  

I use it to create page headers and footers.

Bruno has a tutorial at http://itextdocs.lowagie.com/tutorial/directcontent/pageevents/index.php

There is an example on the right hand side showing how to use the onEndPage to create headers and footers.

cfSearching March 28, 2008 at 12:53 PM  

Yes, I was trying something similar right before I saw your comment. The example I created seems to work so far. I will post in a few .. for posterity ;)

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep