// tango.zh
// Beta 2


// Functions relating to glyph and word sizes.


bool __Tango_CharacterWillFit(int character, int font, int left, int right)
{
    return right-left>=__Tango_GetCharacterWidth(character, font);
}

bool __Tango_WordWillFit(int ptr, int font, int left, int right)
{
    return (right-left>=__Tango_GetWordWidth(ptr, font));
}

// How far the textbox should be scrolled down to display the bottom line.
int __Tango_GetTargetOffset()
{
    return __Tango_GetCurrSlotData(__TSDIDX_CHAR_Y)-
           __Tango_GetCurrSlotDefData(__TSDEF_Y)+
           __Tango_GetCurrFontData(__TANGO_FONT_CHAR_HEIGHT)-
           __Tango_GetCurrStyleData(TANGO_STYLE_TEXT_HEIGHT);
}

// Get the total width of a string. Functions are not evaluated, and
// line breaks are ignored.
int Tango_GetStringWidth(int str, int font)
{
    int width=0;
    int character;
    
    for(int i=0; str[i]!=0; i++)
    {
        character=str[i];
        
        if(character<' ')
            continue;
        
        if(font[__TANGO_FONT_PROPORTION]==TANGO_FONT_MONOSPACE)
            width+=font[__TANGO_FONT_WIDTH];
        else
            width+=font[character+__TANGO_FONT_ASCII_TO_TANGO];
    }
    
    return width;
}

// Get the total width of a series of printable characters in the given font.
// Functions are not evaluated, so any text they would produce is not taken
// into account.
int __Tango_GetWordWidth(int ptr, int font)
{
    int width=0;
    int character;
    
    while(true)
    {
        character=__Tango_Buffer[ptr];
        
        if(character==NULL ||
           character==' ' ||
           character==TANGO_CHAR_NEWLINE)
            break;
        
        if(character>=' ')
            width+=__Tango_GetCharacterWidth(character, font);
        
        if(__Tango_IsSpecialDataMarker(character))
            ptr++; // Skip it
        
        ptr++;
    }
    
    return width;
}

// Get the width of a character. The character is assumed to be a space
// or a valid printable character.
int __Tango_GetCharacterWidth(int character, int font)
{
    if(font[__TANGO_FONT_PROPORTION]==TANGO_FONT_MONOSPACE)
        return font[__TANGO_FONT_WIDTH];
    else
        return font[character+__TANGO_FONT_ASCII_TO_TANGO];
}

