DrawStrWidthP Function (ROM Call 0x3EE)

AMS 2.00 or higher graph.h

unsigned short DrawStrWidthP (const char *str, short len, short Font);

Returns the width of the first len characters of a string.

DrawStrWidthP returns the length in pixels of the first len characters of the string str displayed with font Font. The difference between DrawStrWidthP and DrawStrWidth is that DrawStrWidth gives the length in pixels of the full string str displayed with font Font.
Be careful: len must be less or equal to strlen (str)!

This function is interesting only for the F_4x6 font; for the F_6x8 and F_8x10 fonts, DrawStrWidthP will return 6*len and 8*len, respectively. Also, using

DrawStrWidthP (str, strlen (str), font);
is slower than using
DrawStrWidth (str, font);
Example:
// This line is equivalent to:
// printf_xy (0, 0, "%hu", DrawStrWidth ("DEFGHIJK", F_4x6));
printf_xy (0, 0, "%hu",
  DrawStrWidthP ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + 3, 8, F_4x6));
On AMS versions lower than 2.00, you can emulate DrawStrWidthP with:
unsigned short DrawStrWidthP(const char *str, short len, short Font)
{
  char s[len+1];
  short i;
  // Calling memcpy is slower than copying the string by-hand...
  for (i = 0; i < len; i++)
    s[i] = str[i];
  s[len] = 0;
  return DrawStrWidth (s, font);
}
or the following assembly code provided by Lionel Debroux:
unsigned short DrawStrWidthP(register const char *str asm("a0"), register short len asm("d0"), register short Font asm("d2"));
asm("DrawStrWidthP:
    move.w   %d0,%d1
    beq.s    __end_DrawStrWidthP__
    btst     #0,%d0
    beq.s    __allocate_on_the_stack_DSWP__
    addq.w   #1,%d1
__allocate_on_the_stack_DSWP__:
    suba.w   %d1,%sp
    
    movea.l  %sp,%a1
    subq.w   #1,%d0
__loop_copy_DrawStrWidthP__:
    move.b   (%a0)+,(%a1)+
    dbf      %d0,__loop_copy_DrawStrWidthP__
    clr.b    (%a1)
    
    addq.w   #8,%d1
    move.w   %d1,-(%sp) | save d1 (DrawStrWidth can destroy it)...
    move.w   %d2,-(%sp)
    pea      4(%sp)
    movea.l  0xC8.w,%a0
    movea.l  0x197*4(%a0),%a0 | DrawStrWidth
    jsr      (%a0)
    
    adda.w   6(%sp),%sp
__end_DrawStrWidthP__:
    rts
");
 
The C function is about 1% slower than the original implementation. The assembly implementation is faster.


Uses: sf_width
Used by: Dialog, WinStrXYWrap


See also: DrawStrWidth