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>
<table>
<cfoutput>
<cfset failedFonts = [] >
<cfloop array="#allFamilies#" index="family">
<cftry>
<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>
<cfcatch>
<cfset arrayAppend( failedFonts, family )>
<cfset wasRendered = false>
</cfcatch>
</cftry>
<cfif wasRendered>
<tr><td><cfimage action="writeToBrowser" source="#img#"></td></tr>
</cfif>
</cfloop>
</cfoutput>
</table>
<br>
<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
<cfscript>
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) );
</cfscript>
<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