[comp.sys.amiga] How to find width: ANSWERED

Lee_Robert_Willis@cup.portal.com (10/28/90)

Previously, I posted:

>Does anyone know how to find the width of a single character within 
>a proportional font, without using 'TextLength()'?  (The font/character 
>pair may not be the current font of the RastPort.) 
>
>Using the RKM section on Text (1.3 Chapter 24), I constructed the
>following function.  It checks if the font is proportional or not,
>If it is, it then returns the sum of 'tf_CharSpace' + 'tf_CharKern' 
>for the supplied character 'c'.  
>
>Unfortunately, this does not appear to be correct.  It comes close,
>but I appear to be short a pixel or two with each character.  I'm stuck.

Thanks to everyone who emailed or posted responses.  I got a number of
suggestions which fell into 2 basic categores:
        1) Use IntuiTextLength()

        2) Keep a spare RastPort structure for TextLength().

Both of these methods require more performance overhead than I am willing 
to pay.  (They both require too much data-structure massaging.)  I'm
working on a multi-font text editor, so I need to be able to find the width
of a character upon each keystroke.

Happily, it turns out that the method I posted *does* work! The 1-2 pixel
error I was having was a different bug.  I wasn't passing the correct
character into the routine, and thus was not getting the correct width
back.  (Damn array indices!)

I'm reposting the function for those that might be interested.

Thanx again,
   Lee

   (Lee_Robert_Willis@cup.portal.com)

-----------------------------------------------------------------------
                           Source Code
-----------------------------------------------------------------------

#define FONT_IS_PROPORTIONAL(f) (f->tf_Flags & FPF_PROPORTIONAL)

short Width_of_Char( unsigned char c, struct TextFont *Font )
{
   short  width; /* pixels */
   USHORT char_index;
   UWORD *CharSpacing, *CharKerning;
   

   if FONT_IS_PROPORTIONAL( Font ) 
   {
      /* Make sure 'c' is within the legal character set 
       * for 'Font'.
       */
      if ((c >= Font->tf_LoChar) && (c <= Font->tf_HiChar))
      {
         /* map 'c' into the appropriate index for the
          * 'Font->tf_CharSpace' array.
          */
         char_index  = c - Font->tf_LoChar;
         
         CharSpacing = (UWORD *) Font->tf_CharSpace;
         width       = CharSpacing[ char_index ];
         
         /* Add in kerning, if applicable. */
         if ((CharKerning = (UWORD *) Font->tf_CharKern))
            width += CharKerning[ char_index ];
      }
      else
         width = Font->tf_XSize; /* ERROR: 'c' is not in range. */
   }
   else  /* All font characters are the same size. */
      width = Font->tf_XSize;

   return width;
}