Hi Brian & Joe,

I had a few comments on the string truncation code I recently saw posted...

1. The PalmOSGlue.lib that comes with the Palm OS 3.5 SDK includes a 
WinGlueDrawTruncChars function that will work correctly on 2.0 and 
3.0 versions of the OS.

2. TrimStringToFitCell won't work properly with multi-byte character 
encodings such as Japanese, since it assumes each character only 
takes one byte of storage in the string. It might also be slow if a 
significant amount of text needed to be truncated from the end of a 
string, since it calls StrLen & FntCharsWidth repeatedly in the loop.

3. TrimString will truncate the string unnecessarily, if the length 
of the string is within six pixels of the limit.

4. Both routines assume the width of "..." is always six pixels, 
which isn't a good assumption to make. In addition they assume that 
the passed string buffer can safely hold an extra two bytes (worst 
case, when one byte is removed but "..." is added).

Currently there isn't a TxtGlueTrimString routine, but it seems like 
such a call would be useful (versus just supporting truncated 
drawing), so I'll add that to my list.

-- Ken

At 10:56pm -0800 99-12-23, Palm Developers Forum List wrote:
>Date: 23 Dec 1999 06:12:20 -0800
>From: "Brian O'Grady" <[EMAIL PROTECTED]>
>Subject: Re: Working Code for String Truncate: For OS 3.0
>
>Thanks Joe, someone else recommended this code below which works fine but I
>am guessing your code is somewhat faster because you do not have the
>iteration. Thanks Again,
>
>
>Brian
>
>void TrimStringToFitCell(char *string, int cell_width_pixels)
>{
>  if (FntCharsWidth(string, StrLen(string)) > cell_width_pixels)
>  {
>    cell_width_pixels -= 6;
>    do
>    {
>      string[StrLen(string) - 1] = 0;
>    } while (FntCharsWidth(string, StrLen(string)) >= cell_width_pixels);
>  StrCat(string, "...");
>  }
>}

>- ----- Original Message -----
>From: Oguri, Joe <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>
>Cc: 'PalmDevList' <[EMAIL PROTECTED]>; Joe Oguri
><[EMAIL PROTECTED]>
>Sent: Wednesday, December 22, 1999 5:15 PM
>Subject: Working Code for String Truncate: For OS 3.0
>
>
> >
> >    I must have 3.0 also.. there is no WinDrawTruncChars on my system. So,
> > from reading the other
> >   postings on how to write your own, I wrote one that works for me. This
> > should save you about 45
> >   seconds (120 if you have to look up the API like I did).
> >
> > void
> > TrimString(CharPtr Str, Int Pixels)
> > {
> >     Int   sWid, sLen;
> >     Boolean fit;
> >
> >     sWid = Pixels - 6;
> >     sLen = StrLen(Str);
> >     FntCharsInWidth(Str, &sWid, &sLen, &fit);
> >     if (!fit) {
> >        Str[sLen] = 0;
> >        StrCat(Str, "...");
> >        }
> > }

Ken Krugler
TransPac Software, Inc.
<http://www.transpac.com>
+1 530-470-9200 (direct) +1 408-261-7550 (main)

Reply via email to