[Main]
Name=DrawStrWidthP
Type=Function
Subtype=ROM Call
Header Files=graph.h
Definition=unsigned short DrawStrWidthP (const char *str, short len, short Font);
MinAMS=2.00
See Also=graph.h/DrawStrWidth

[ROM Call]
Index=$3EE

[Description]
Returns the width of the first <I>len</I> characters of a string.

[Explanation]
DrawStrWidthP returns the length in pixels of the first <I>len</I> characters of the string <I>str</I> displayed with font <I>Font</I>.
The difference between DrawStrWidthP and <A HREF="$$LINK(graph.h/DrawStrWidth)">DrawStrWidth</A> is that DrawStrWidth gives the length in pixels of the full string <I>str</I> displayed with font <I>Font</I>.
<BR>
Be careful: <I>len</I> must be less or equal to <CODE>strlen (str)</CODE>!
<BR><BR>
This function is interesting only for the <A HREF="$$LINK(graph.h/Fonts)">F_4x6</A>
font; for the <A HREF="$$LINK(graph.h/Fonts)">F_6x8</A> and
<A HREF="$$LINK(graph.h/Fonts)">F_8x10</A> fonts, DrawStrWidthP will return
6*len and 8*len, respectively.
Also, using 
<PRE>DrawStrWidthP (str, strlen (str), font);</PRE>
is slower than using
<PRE>DrawStrWidth (str, font);</PRE>
Example:
<PRE>// 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));
</PRE>
On AMS versions lower than 2.00, you can emulate DrawStrWidthP with:
<PRE>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 &lt; len; i++)
    s[i] = str[i];
  s[len] = 0;
  return DrawStrWidth (s, font);
}
</PRE>
or the following assembly code provided by Lionel Debroux:
<PRE>
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
");
 </PRE>
The C function is about 1% slower than the original implementation.
The assembly implementation is faster.

[References]
In=dialogs.h/Dialog, wingraph.h/WinStrXYWrap
Out=graph.h/sf_width
