I think most of the slowness is in creating the objects not calculating
the width. So if you can somehow cache or reuse the objects, it is much
faster. You will notice that the following code is much faster than
calling the UDF. So what I would suggest is that if your going to be
using the same face, style and size that you just create the
FontMetrictsObject once, and reuse it:
<cfscript>
face = "Arial";
style = "Plain";
size = 12;
if (Style EQ "Plain" OR Style EQ "Normal") {
Style = CreateObject("java", "java.awt.Font").PLAIN;
} else if (Style EQ "Bold") {
Style = CreateObject("java", "java.awt.Font").BOLD;
} else if (Style EQ "Italic") {
Style = CreateObject("java", "java.awt.Font").ITALIC;
}
FontObject = CreateObject("java","java.awt.Font").init(Face, Style,Size);
FontMetricsObject =
CreateObject("java","java.awt.Toolkit").getDefaultToolkit().getFontMetrics(FontObject);
</cfscript>
<cfset tick = GetTickCount()>
<cfloop from="1" to="5000" index="i">
<cfset size = FontMetricsObject.stringWidth("this string width")>
</cfloop>
<cfset tock = GetTickCount()>
<cfoutput>#tock-tick#ms</cfoutput>
Dirk Sieber wrote:
> Hello again,
>
>
> Last week, I posted regarding finding the actual length of a text string
> (original question below), and, thanks to some helpful people, ended up
> with the idea of instansiating the Java FontObject, which we turned into
> a function, like this:
> <cffunction name="StringWidth" output="false">
> <cfargument name="String" required="true">
> <cfargument name="Face" default="Arial">
> <cfargument name="Style" default="Plain">
> <cfargument name="Size" default="12">
>
> <cfscript>
> if (Style EQ "Plain" OR Style EQ "Normal") {
> Style = CreateObject("java", "java.awt.Font").PLAIN;
> } else if (Style EQ "Bold") {
> Style = CreateObject("java", "java.awt.Font").BOLD;
> } else if (Style EQ "Italic") {
> Style = CreateObject("java", "java.awt.Font").ITALIC;
> }
>
> FontObject = CreateObject("java","java.awt.Font").init(Face, Style,
> Size);
> FontMetricsObject =
> CreateObject("java","java.awt.Toolkit").getDefaultToolkit().getFontMetri
> cs(FontObject);
>
> return(FontMetricsObject.stringWidth(String));
>
> </cfscript>
> </cffunction>
>
>
> Now, the problem - it works really well, but it's painfully slow - we're
> sometimes calling this 50 times on a page, and it's causing the page
> times to be in the 3-5 second range. Removing the call drops it to a
> few hundred milliseconds, so this seems to be the issue.
>
>
> It's not completely intolerable, and it's much nicer than the 'keep a
> table of all the character widths' way of doing things, so we'll stick
> with it, but if anyone's got any ideas on a way to speed this up, I'm
> all ears!
>
>
> Thanks,
> Dirk
>
> > From: Dirk Sieber [mailto:[EMAIL PROTECTED]
> > Sent: Wednesday, August 11, 2004 11:13 AM
> > To: CF-Talk
> > Subject: Determining physical length of text string (not char count)
> >
> >
> > Hello everyone,
> >
> > We're trying to figure out if there's a way in CF to figure out the
> > actual, physical length of a string of text - eg, in pixels, inches,
> > whatever.
> >
> > The issue - we need to be able to accept user data for a field, that
> > will eventually be printed to a PDF. That PDF has strict specs as to
> > how long a line of text can be. Of course, that'd be simple enough,
> > if it was printed in a fixed-width font... but it's not, and we don't
> > have the option of changing it.
> >
> > So... I need to find a way to detemine how long a string of text will
> > be, given the font type, style, & point size. Anyone have any ideas
> > how to do this from within CF? (This is under MX, BTW).
> >
> > Many thanks for any suggestions!
> > Dirk
> > _____
> >
> >
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

