Friday, July 11, 2008

HTTP is a Built-In ColdFusion Scope?

While using cfhttp yesterday, I noticed something interesting.

I was running a small test page that made a cfhttp calls and then used cfdump to display the results to the screen. Though the script executed without error, the results seemed wrong... and yet very familiar. It took me a second before I realized why. The results looked exactly like a dump of the CGI scope. So I went back and checked my code and found that I had accidentally dumped HTTP instead of CFHTTP:

<cfdump var="#HTTP#">

Based on the results, HTTP seems to be a built-in scope. I was a little surprised because I could not recall reading anything about a scope by that name. Curious, I used a few of the underlying java methods to examine the HTTP and CGI objects more closely. Sure enough java reported the two objects were both instances of the class coldfusion.runtime.CgiScope and that the two were equal.

HTTP.equals(CGI) = #HTTP.equals(CGI)#<br>
HTTP.getClass() = #HTTP.getClass().getName()#<br>
CGI.getClass() = #CGI.getClass().getName()#

So apparently HTTP is another another way of referencing the CGI scope. Now while I strongly suspect that is _not_ new to ColdFusion, it was certainly news to me ;-)

...Read More

Friday, July 4, 2008

What Fonts are Available in myJVM?

A while back I was looking for a quick way to determine what fonts were available in my JVM. I came across the java GraphicsEnvironment class, which returns an array of font families available in the current graphics environment. So I threw together a quick loop, and presto .. I had sample images of the available fonts in my JVM.

<cfset environ = createObject("java", "java.awt.GraphicsEnvironment").getLocalGraphicsEnvironment()>
<cfset allFamilies = environ.getAvailableFontFamilyNames()>

<h3>Display Available Font Families</h3>

<cfset failedFonts = [] >
<cfloop array="#allFamilies#" index="family">
<cfset img = ImageNew("", 200, 35, "argb", "##dcdcdc")>
<cfset prop = { font=family, size="14", style="plain" }>
<cfset ImageSetAntiAliasing( img, "on" )>
<cfset ImageSetDrawingColor( img, "##000000" )>
<cfset ImageDrawText( img, family, 15, 15, prop)>
<cfset wasRendered = true>

<cfset arrayAppend( failedFonts, family )>
<cfset wasRendered = false>

<cfif wasRendered>
<tr><td><cfimage action="writeToBrowser" source="#img#"></td></tr>

<h3>Unable to display fonts:</h3>
<cfdump var="#failedFonts#">

One thing that surprised me was that a few of the fonts could not be displayed. Namely the five (5) logical fonts and a few of the Lucida fonts. If you are not familiar with the term logical fonts, one of the old sun api's describes them as:

" .. not actual font libraries that are installed anywhere on your system. They are merely font-type names recognized by the Java runtime which must be mapped to some physical font that is installed on your system."

While I understand the difference between logical and physical fonts, I was still surprised logical font names were not accepted by the ImageDrawText function. When I created a java Font object directly, it worked fine. So I guess I was expecting ImageDrawText to behave the same way.

Using Java Font Object

img = ImageNew("", 100, 50, "argb");
Color = createObject("java", "java.awt.Color");
Font = createObject("java", "java.awt.Font");
textFont = Font.init( "Dialog", Font.PLAIN, javacast("int", 12));
graphics = ImageGetBufferedImage( img ).getGraphics();
graphics.setColor( Color.BLACK );
graphics.setFont( textFont );
graphics.drawString( textFont.getFamily(), javacast("int", 10), javacast("int", 10) );

<cfimage action="writeToBrowser" source="#img#">

As the documentation on ImageDrawText does not say much about the "font" attribute, that may be a misunderstanding on my part. If anyone can shed some light on this, I am eager to be enlightened ;-)

...Read More

  © Blogger templates The Professional Template by 2008

Header image adapted from atomicjeep