I had a play with this myself, and found that the pixel width retruned
by the funtion is a LONG way out of the actual length of the string
rendered in a browser.


e.g.


<cfscript>
font_obj = createobject("java","java.awt.Font").init('Arial',1,8);
font_metrics_obj =
createobject("java","java.awt.Toolkit").getDefaultToolkit().getFontMetri
cs(font_obj);
Pixel_Width = font_metrics_obj.stringWidth('the pixel width is miles
out?');
</cfscript>


<cfoutput>
#Pixel_Width#
<br>
<img src="" width="<cfoutput>#Pixel_Width#</cfoutput>" height="1"
alt="" border="0">
<br>
<div align="left" style="font-family:Arial;font-size:8pt;">the pixel
width is miles out?</div>
</cfoutput>


You'll need a test gif of course.


Still, I'm not sure why, maybe java just renders fonts entirely
differently to browsers. I tried several fonts and many font sizes, none
of them are even close.


Craig.

-----Original Message-----
From: Dirk Sieber [mailto:[EMAIL PROTECTED]
Sent: 16 August 2004 20:04
To: CF-Talk
Subject: slow Java call - Determining physical length of string - Pt 2

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]

Reply via email to