PetaMem has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have this HTML-table of a given size. Now I want to make sure, the text I will fill into it, will not expand it. Truncating text is ok.

However, as proportional font is used in display, I cannot simply say "truncate after n chars", because then I'd had to take n for the worst case (some MWMWMWMW string) and in that case the other extreme (some IIIIIIIII string) would get truncated too early.

So I thought of some heuristics, where every character would be given some units of width it has. The most narrow character would get 1 unit, whereas the most wide character would get some multiple of that value to approximate better than the one character = one width computation.

These values could be stored in some hash, where the char is the key, and the number of units is the value, and computation would be simply splitting the string to chars, iterating over that list and adding values. And truncating over some given threshold.

Now the questions:

Many thanks for your suggestions.

Bye
 PetaMem
    All Perl:   MT, NLP, NLU

Replies are listed 'Best First'.
Re: Text width metrics/heuristics
by Fang (Pilgrim) on Jun 09, 2005 at 08:54 UTC

    This may sound like an off-topic answer, but wouldn't that be the job of some CSS declaration? Some combination of overflow and white-space could be the answer to your problem.

    Just a thought...

Re: Text width metrics/heuristics
by muntfish (Chaplain) on Jun 09, 2005 at 08:54 UTC

    Tricky to do this accurately, since you can't guarantee which font or fontsize your browser will choose.

    Searching for font on CPAN turns up a pile of stuff that may be useful.

    Also, there may be a way to enforce this on the client side, with CSS - check out the bit in the tables spec about the "fixed" format.


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
Re: Text width metrics/heuristics
by ww (Archbishop) on Jun 09, 2005 at 14:49 UTC
    good thing to think about; not so good to implement:
    • Unless you're willing to also do the jiggeryjoggery of learning the user's screen size, you're going to be shooting in the dark (Your specified table-size may be a bad match for the user's device: 2400x1800, 800x640, 320x200, WAP....)
    • Browsers' font-sizes (in pixels) are inconsistent. Code a simple screenfilling line (with a mix of chars or real words) for one -- say, IE 5 or 6, and then open the same .html with FF or Moz or Konqueror. Then spec a different font-family and look again
        or....
    • Proportional fonts -- even in the same nominal size -- do not necessarily allocate the same width in pixels for each char across fonts.
    • and arguably worst implementing this except with css (at least, by any means I know) is going to royally screw those with vision impairment, who use local style sheets to cause rendered text to appear in larger and/or more contrasty fonts.

    That said, if you have the option of using a fixed-width font, that could provide a comparatively easy and (to those who are vision-challenged) perhaps_acceptable solution.