Here's a poor man's (or simple man's) solution...

First display in a web page every character (numbers, letters, upper
case, lower case, etc) that might appear in your app.  Display the
characters using the exact same type properties, including font family,
size, weight, etc.  Make a screen capture of the page that displays
these characters.  Paste the screen capture into a Photoshop image and
zoom in on each character so that you can measure its width in pixels.
On paper or a text document, record the width in pixels that corresponds
with each character.  Once you've gathered all the data, create a
VBScript function for your ASP pages that computes the width of a
string.  The first thing the function must do is build a 2-dimensional
array to contain all the pairs of data--character and width.  Then the
function should parse through the string one character at a time, look
up each character in the array to find its width, and add the width to
an integer that represents the total width of the string.  The function
would then return the integer.

Better yet, you could put this function in an include file and include
it in any web page that needs to compute the length of a string.

If you want to get really fancy, you could have multiple arrays, one for
each font scenario that you want to support.  Then make your function
accept 2 parameters instead of 1.  The second parameter would be to
indicate the font scenario.  Just as an example, our website's style
sheet uses a lean collection of 5 different type styles, so I would
create 5 arrays to represent them and their respective characters'
widths.

This solution will work a lot better if you use CSS to set your type
properties, and even better if, in your CSS, you use pixels to represent
the size of your type, not points or inches.

One minor gotchya...  You must take into account the space between
characters.  You can do this on a character basis, or your function
could add a default number to the total width for each space.

One more gotchya...  You're probably going to get slightly different
results depending on what browser the visitor is using.  If all your
visitors use the same browser (intranet environment) or if you can
assume that all your visitors use IE 4+, then it's probably a non-issue.
Even if your visitors use a bunch of different browsers on different
platforms, the function would probably still provide a good
approximation, but it would not be perfectly reliable--something to take
into consideration.  ...on the other hand, you will have this issue no
matter what solution you use.

What do you think?

- Vieth

> -----Original Message-----
> From: Niels Bieze [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, September 29, 2004 4:51 AM
> To: [EMAIL PROTECTED]
> Subject: [ASP] Calculating the length of a string in pixels
> 
> 
> Hi all
> 
>  
> 
> Is there someone that knows of a function/component that will 
> calculate the length of a string in pixels?
> 
>  
> 
> Say I have a string = "Brown Fox" the font is Arial, and the 
> font-size = 10
> 
>  
> 
> Is it possible to calculate how long that string would be in pixels??
> 
>  
> 
> Any and all help is greatly appreciated
> 
>  
> 
> Greetings
> 
>  
> 
>  
> 
>  
> 
>  
> 
> 
> 
> [Non-text portions of this message have been removed]
> 
> 
> 
> ------------------------ Yahoo! Groups Sponsor 
> --------------------~--> Make a clean sweep of pop-up ads. 
> Yahoo! Companion Toolbar.
> Now with Pop-Up Blocker. Get it for free!
> http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/17folB/TM
> --------------------------------------------------------------
> ------~-> 
> 
> --------------------------------------------------------------
> -------    
>  Home       : http://groups.yahoo.com/group/active-server-pages
> ---------------------------------------------------------------------
>  Post       : [EMAIL PROTECTED]
>  Subscribe  : [EMAIL PROTECTED]
>  Unsubscribe: [EMAIL PROTECTED]
> ---------------------------------------------------------------------
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 
> 
> 


------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/17folB/TM
--------------------------------------------------------------------~-> 

---------------------------------------------------------------------    
 Home       : http://groups.yahoo.com/group/active-server-pages
---------------------------------------------------------------------
 Post       : [EMAIL PROTECTED]
 Subscribe  : [EMAIL PROTECTED]
 Unsubscribe: [EMAIL PROTECTED]
--------------------------------------------------------------------- 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/active-server-pages/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 

Reply via email to