Posted February 11, 2008 at
7:13 AM
Tags:
ColdFusion
I have added a user defined function to the imageUtils.cfc ColdFusion image manipulation component. This function, GetTextDimensions(), takes a string and a font-properties struct and determines the height and width dimensions of the rendered text in a ColdFusion image. This can be used to more precisely layout text elements of a generated ColdFusion image. In fact, this functionality is what is powering my ImageDrawTextarea() and the imageUtils.cfc DrawTextarea() functions.
Launch code in new window » Download code as text file »
- <cffunction
- name="GetTextDimensions"
- access="public"
- returntype="struct"
- output="false"
- hint="Give the string and the font properties, the width and height of the text is calculated. If the font properties struct is missing any values, ColdFusion's default values will be used.">
-
- <cfargument
- name="Text"
- type="string"
- required="true"
- hint="The text whose dimensions we are going to calculate."
- />
-
- <cfargument
- name="FontProperties"
- type="struct"
- required="true"
- hint="The font properties used to calculate the text dimensions."
- />
-
- <cfset var LOCAL = {} />
-
- <cfset LOCAL.Image = ImageNew( "", 1, 1, "rgb" ) />
-
- <cfset LOCAL.Graphics = ImageGetBufferedImage( LOCAL.Image )
- .GetGraphics()
- />
-
- <cfset LOCAL.CurrentFont = LOCAL.Graphics.GetFont() />
-
-
-
- <cfif NOT StructKeyExists( ARGUMENTS.FontProperties, "Size" )>
-
- <cfset ARGUMENTS.FontProperties.Size = LOCAL.CurrentFont.GetSize() />
-
- </cfif>
-
-
- <cfif NOT StructKeyExists( ARGUMENTS.FontProperties, "Font" )>
-
- <cfset ARGUMENTS.FontProperties.Font = LOCAL.CurrentFont.GetFontName() />
-
- </cfif>
-
-
- <cfif NOT StructKeyExists( ARGUMENTS.FontProperties, "Style" )>
-
- <cfif (
- LOCAL.CurrentFont.IsBold() AND
- LOCAL.CurrentFont.IsItalic()
- )>
-
- <cfset ARGUMENTS.FontProperties.Style = "bolditalic" />
-
- <cfset LOCAL.FontStyleMask = BitOR(
- LOCAL.CurrentFont.BOLD,
- LOCAL.CurrentFont.ITALIC
- ) />
-
- <cfelseif LOCAL.CurrentFont.IsBold()>
-
- <cfset ARGUMENTS.FontProperties.Style = "bold" />
-
- <cfset LOCAL.FontStyleMask = LOCAL.CurrentFont.BOLD />
-
- <cfelseif LOCAL.CurrentFont.IsItalic()>
-
- <cfset ARGUMENTS.FontProperties.Style = "italic" />
-
- <cfset LOCAL.FontStyleMask = LOCAL.CurrentFont.ITALIC />
-
- <cfelse>
-
- <cfset ARGUMENTS.FontProperties.Style = "plain" />
-
- <cfset LOCAL.FontStyleMask = LOCAL.CurrentFont.PLAIN />
-
- </cfif>
-
- <cfelse>
-
- <cfset LOCAL.FontStyleMask = LOCAL.CurrentFont.PLAIN />
-
- </cfif>
-
-
-
-
- <cfset LOCAL.NewFont = CreateObject(
- "java",
- "java.awt.Font"
- ).Init(
- JavaCast( "string", ARGUMENTS.FontProperties.Font ),
- JavaCast( "int", LOCAL.FontStyleMask ),
- JavaCast( "int", ARGUMENTS.FontProperties.Size )
- )
- />
-
- <cfset LOCAL.FontMetrics = LOCAL.Graphics.GetFontMetrics(
- LOCAL.NewFont
- ) />
-
- <cfset LOCAL.TextBounds = LOCAL.FontMetrics.GetStringBounds(
- JavaCast( "string", ARGUMENTS.Text ),
- LOCAL.Graphics
- ) />
-
-
- <cfset LOCAL.Return = {
- Width = Ceiling( LOCAL.TextBounds.GetWidth() ),
- Height = Ceiling( LOCAL.TextBounds.GetHeight() )
- } />
-
- <cfreturn LOCAL.Return />
- </cffunction>
It returns a structure that has Width and Height keys. Here, you can see it in action:
Launch code in new window » Download code as text file »
- <cfset strText = "Hey there, hot momma!" />
-
- <cfset objProperties = {
- Font = "Times New Roman",
- Size = "18"
- } />
-
- <cfset objDimensions = GetTextDimensions(
- strText,
- objProperties
- ) />
-
- <cfdump
- var="#objDimensions#"
- label="Dimensions for: #strText#"
- />
Running the above code, we get the following CFDump output:
Download Code Snippet ZIP File
Comments (0) |
Post Comment |
Ask Ben |
Permalink |
Other Searches |
Print Page
What Other People Are Searching For
[ local search ]
coldfusion get image text width
[ local search ]
coldfusion get image text height
[ local search ]
colfdusion get image text dimensions
[ local search ]
get text width coldfusion image
[ local search ]
get text height coldfusion image
There are no comments posted for this web log entry.
Post Comment |
Ask Ben