view GUIFont.cpp @ 1160:502b8b2e36b4

font cleaning
author Gloval
date Tue, 04 Jun 2013 01:37:23 +0400
parents c0bfb386b15f
children 29a8defbad9e
line wrap: on
line source

#include <string>
#include <assert.h>

#include "LOD.h"
#include "GUIFont.h"
#include "GUIWindow.h"
#include "Render.h"

#include "mm7_data.h"


extern LODFile_IconsBitmaps *pIcons_LOD;


struct GUIFont *pAutonoteFont;
struct GUIFont *pSpellFont;
struct GUIFont *pFontArrus;
struct GUIFont *pFontLucida;
struct GUIFont *pBook2Font;
struct GUIFont *pBookFont;
struct GUIFont *pFontCreate;
struct GUIFont *pFontCChar;
struct GUIFont *pFontComic;
struct GUIFont *pFontSmallnum;

char temp_string[2048];

char pTmpBuf3[10000];

void DrawCharToBuff(unsigned short* uXpos,unsigned char* pCharPixels, int uCharWidth, int uCharHeight, unsigned __int16* pFontPalette, __int16 draw_color, int line_width);


//----- (0044C448) --------------------------------------------------------
GUIFont *LoadFont(const char *pFontFile, const char *pFontPalette, ...)
{	
    int pallete_index; // eax@3
    GUIFont *pFont;
    unsigned int palletes_count =0;
    va_list palettes_ptr;

    pFont = (GUIFont *)pIcons_LOD->LoadRaw(pFontFile, 0);
    va_start(palettes_ptr, pFontFile);

    while  (NULL!=(pFontPalette=va_arg(palettes_ptr, const char *)))
        {
        pallete_index =pIcons_LOD->LoadTexture(pFontPalette, TEXTURE_16BIT_PALETTE);
        if (pallete_index == -1)
            {
            wsprintfA(pTmpBuf, "Unable to open %s", pFontPalette);
            Abortf(pTmpBuf);
            }	
        pFont->pFontPalettes[palletes_count] = pIcons_LOD->pTextures[pallete_index].pPalette16;
        ++palletes_count;
        }
    va_end(palettes_ptr);
    pFont->palletes_count = palletes_count;
    return pFont;
}



//----- (0044D2FD) --------------------------------------------------------
void GUIFont::_44D2FD_prolly_draw_credits_entry(GUIFont *pFont, int Str, int a4, unsigned int w, unsigned int h, 
                                                unsigned __int16 a7, unsigned __int16 a8, const char *pString, 
                                                unsigned __int16 *pPixels, unsigned int uPixelsWidth)
{
  int v11; // ebx@1
  int v12; // edi@1
  char *v13; // eax@1
  char *v14; // eax@1
  unsigned __int16 *v15; // esi@1
  GUIFont *v16; // edi@4
  signed int v17; // ecx@4
  signed int v18; // eax@6
  int v19; // [sp+Ch] [bp-5Ch]@1
  int v20; // [sp+10h] [bp-58h]@1
  unsigned int v21; // [sp+14h] [bp-54h]@1
  unsigned int v22; // [sp+18h] [bp-50h]@1
  unsigned int v23; // [sp+1Ch] [bp-4Ch]@1
  unsigned int v24; // [sp+20h] [bp-48h]@1
  GUIFont *v25; // [sp+60h] [bp-8h]@1
  GUIFont *v26; // [sp+64h] [bp-4h]@1
  char *Stra; // [sp+70h] [bp+8h]@1
  int a4a; // [sp+74h] [bp+Ch]@4
  int a7a; // [sp+80h] [bp+18h]@2

  v11 = a7;
  v22 = h;
  v12 = Str;
  v26 = this;
  v24 = a4 + h - 1;
  v21 = w;
  v25 = pFont;
  v23 = Str + w - 1;
  ui_current_text_color = a7;
  v19 = Str;
  v20 = a4;
  v13 = GUIFont::_44C933(pString, this, pFont, (int)&v19, 0, 1);
  v14 = strtok(v13, "\n");
  Stra = v14;
  v15 = &pPixels[uPixelsWidth * a4];
  if ( v14 )
  {
    a7a = v12 >> 1;
    while ( 1 )
    {
      v16 = v26;
      ui_current_text_color = v11;
      v17 = 0;
      a4a = v11;
      if ( *v14 == '_' )
      {
        v16 = v25;
        a4a = a8;
        ui_current_text_color = a8;
        v17 = 1;
      }
      v18 = (signed int)(w - v16->GetLineWidth(&v14[v17])) >> 1;
      if ( v18 < 0 )
        v18 = 0;
      v16->DrawTextLineToBuff(a4a, a8, &v15[v18 + a7a], Stra, uPixelsWidth);
      v15 += uPixelsWidth * (LOBYTE(v16->uFontHeight) - 3);
      Stra = strtok(0, "\n");
      if ( !Stra )
        break;
      v14 = Stra;
    }
  }
}
// 5C6DB4: using guessed type int ui_current_text_color;


//----- (0044D1E7) --------------------------------------------------------
void GUIFont::DrawTextLine( unsigned int uDefaultColor, signed int uX, signed int uY, 
                            const char *text, int max_len_pix )
{
    signed int uX_pos; // edi@3
    unsigned char c; // cl@4
    unsigned __int16 draw_color; // cx@12
    unsigned __int8 *pCharPixels; // eax@12
    char color_code[20]; // [sp+Ch] [bp-1Ch]@16
    int text_length; // [sp+20h] [bp-8h]@2
    int text_color; // [sp+24h] [bp-4h]@1
    int uCharWidth; // [sp+30h] [bp+8h]@9

    if ( !text )
        return;
    text_color = ui_current_text_color;
    text_length = strlen(text);
    uX_pos=uX;
    for (int i=0; i<text_length; ++i )
        {
        c = text[i];
        if ( IsCharValid(c) )
            {
            switch (c)
                {
            case '\n':	//Line Feed 0A 10:
                return;
                break;
            case '\f':  //Form Feed, page eject  0C 12 
                strncpy(color_code, &text[i + 1], 5);
                color_code[5] = 0;
                text_color = atoi(color_code);
                ui_current_text_color = text_color;
                i += 5;	  
                break;
            case '\t':	// Horizontal tab 09
            case '\r':   //Carriage Return 0D 13                 
                break;
            default:
                uCharWidth = pMetrics[c].uWidth;
                if ( uCharWidth )
                    {
                    if ( i > 0 )
                        uX_pos += pMetrics[c].uLeftSpacing;
                    draw_color = text_color;
                    pCharPixels = &pFontData[font_pixels_offset[c]];
                    if ( !text_color )
                        draw_color = -1;
                    pRenderer->DrawText(uX_pos, uY, pCharPixels, uCharWidth, uFontHeight, pFontPalettes[0], draw_color, 0);
                    uX_pos += uCharWidth;
                    if ( i < text_length )
                        uX_pos += pMetrics[c].uRightSpacing;
                    }
                }
            }
        }
    
}

//----- (0040F845) --------------------------------------------------------
void DrawCharToBuff( unsigned short* uXpos,unsigned char* pCharPixels, int uCharWidth, int uCharHeight, 
                            unsigned __int16* pFontPalette, __int16 draw_color, int line_width )
    {
    unsigned __int16* draw_buff; // edi@1
    unsigned char* pPixels; // esi@1
    unsigned char char_pxl; // eax@3

    draw_buff = uXpos;
    pPixels = pCharPixels;
    for(int i=0; i<uCharHeight; ++i)
        {
        for(int j=0; j<uCharWidth; ++j)
            {
            char_pxl = *pPixels++;
            if ( char_pxl )
                {
                if ( char_pxl == 1 )
                    *draw_buff = pFontPalette[1];         
                else         
                    *draw_buff = draw_color;         
                }
            ++draw_buff;
            }
        draw_buff+=line_width-uCharWidth;
        }

}

//----- (0044D0B5) --------------------------------------------------------
void GUIFont::DrawTextLineToBuff( int uColor, int a3, unsigned short* uX_buff_pos, const char *text, int line_width )
    {
  
  unsigned short* uX_pos; // edi@3
  unsigned char c; // cl@4
  unsigned __int16 draw_color; // cx@12
  unsigned __int8 *pCharPixels; // eax@12
  char color_code[20]; // [sp+Ch] [bp-1Ch]@16
  int text_length; // [sp+20h] [bp-8h]@2
  int text_color; // [sp+24h] [bp-4h]@1
  int uCharWidth; // [sp+30h] [bp+8h]@9

  if ( !text )
      return;
  text_color = ui_current_text_color;
  text_length = strlen(text);
  uX_pos=uX_buff_pos;
  for (int i=0; i<text_length; ++i )
      {
      c = text[i];
      if ( IsCharValid(c) )
          {
          switch (c)
              {
          case '\n':	//Line Feed 0A 10:
              return;
              break;
          case '\f':  //Form Feed, page eject  0C 12 
              strncpy(color_code, &text[i + 1], 5);
              color_code[5] = 0;
              text_color = atoi(color_code);
              ui_current_text_color = text_color;
              i += 5;	  
              break;
          case '\t':	// Horizontal tab 09
          case '\r':   //Carriage Return 0D 13                 
              break;
          default:
              uCharWidth = pMetrics[c].uWidth;
              if ( uCharWidth )
                  {
                  if ( i > 0 )
                      uX_pos += pMetrics[c].uLeftSpacing;
                  draw_color = text_color;
                  pCharPixels = &pFontData[font_pixels_offset[c]];
                  if ( !text_color )
                      draw_color = -1;
                  DrawCharToBuff(uX_pos, pCharPixels, uCharWidth, uFontHeight, pFontPalettes[0], draw_color, line_width);
                  uX_pos += uCharWidth;
                  if ( i < text_length )
                      uX_pos += pMetrics[c].uRightSpacing;
                  }
              }
          }
      }
}



//----- (0044C933) --------------------------------------------------------
char * GUIFont::_44C933(const char *pString, GUIFont *pFont, GUIFont *a3, int a4, int a5, int a6)
{
  const char *v6; // esi@1
  size_t v8; // eax@3
  size_t v9; // edi@3
  int v10; // eax@3
  GUIFont *v11; // esi@3
  int v12; // ebx@3
  char v13; // cl@5
  int v14; // edx@11
  int v15; // ecx@11
  int v16; // edx@12
  int v17; // edx@13
  int v18; // edx@14
  int v19; // edx@15
  int v20; // edi@17
  int v21; // edx@17
  int v22; // ecx@17
  int v23; // edi@18
  char v24; // zf@18
  int v25; // ecx@19
  int v26; // eax@19
  char v27; // dl@24
  int v28; // eax@46
  std::string v29; // [sp-18h] [bp-48h]@2
  const char *v30; // [sp-8h] [bp-38h]@2
  int v31; // [sp-4h] [bp-34h]@2
  const char *v32; // [sp+Ch] [bp-24h]@1
  char Str; // [sp+10h] [bp-20h]@46
  char v34; // [sp+13h] [bp-1Dh]@46
  GUIFont *v35; // [sp+14h] [bp-1Ch]@1
  int v36; // [sp+18h] [bp-18h]@3
  int v37; // [sp+1Ch] [bp-14h]@3
  int v38; // [sp+20h] [bp-10h]@4
  int v39; // [sp+24h] [bp-Ch]@3
  int v40; // [sp+28h] [bp-8h]@3
  int i; // [sp+2Ch] [bp-4h]@17
  std::string *v42; // [sp+38h] [bp+8h]@2

  v6 = pString;
  v35 = pFont;
  v32 = pString;
  if ( !pString )
  {
    MessageBoxW(nullptr, L"Invalid string passed !", L"E:\\WORK\\MSDEV\\MM7\\MM7\\Code\\Font.cpp:558", 0);
    return 0;
  }
  v8 = strlen(pString);
  v9 = v8;
  v39 = v8;

  assert(strlen(v6) < sizeof(pTmpBuf3));
  strcpy(pTmpBuf3, v6);
  v10 = a5;
  v11 = v35;
  v12 = 0;
  v40 = 0;
  v37 = a5;
  v36 = 0;
  if ( (signed int)v9 > 0 )
  {
    v38 = v9 - 1;
    do
    {
      v13 = pTmpBuf3[v12];
      if ( (unsigned __int8)v13 >= v11->cFirstChar && (unsigned __int8)v13 <= v11->cLastChar
        || v13 == '\f'
        || v13 == '\r'
        || v13 == '\t'
        || v13 == '\n' )
      {
        v14 = (unsigned __int8)v13 - 9;
        v15 = (int)&v11->pMetrics[(unsigned __int8)v13];
        if ( v14 )
        {
          v16 = v14 - 1;
          if ( v16 )
          {
            v17 = v16 - 2;
            if ( v17 )
            {
              v18 = v17 - 1;
              if ( v18 )
              {
                v19 = v18 - 19;
                if ( v19 )
                {
                  if ( v19 == 63 )
                  {
                    v11 = a3;
                  }
                  else
                  {
                    v20 = *(int *)(v15 + 8);
                    v21 = *(int *)(v15 + 4);
                    v22 = *(int *)v15;
                    i = v20;
                    if ( (unsigned int)(v10 + v20 + v22 + v21) < *(int *)(a4 + 8) )
                    {
                      v12 = v36;
                      if ( v36 > v40 )
                        v10 += v22;
                      v10 += v21;
                      if ( v36 < v39 )
                        v10 += i;
                    }
                    else
                    {
                      v23 = v40;
                      v24 = v11 == a3;
                      pTmpBuf3[v40] = 10;
                      if ( v24 )
                      {
                        v25 = v38;
                        v26 = v23 + 1;
                        while ( v25 >= v26 )
                        {
                          pTmpBuf3[v25] = byte_5C45AF[v25];
                          --v25;
                        }
                        ++v39;
                        ++v38;
                        pTmpBuf3[v23++ + 1] = 95;
                        v40 = v26;
                      }
                      v12 = v36;
                      v10 = v37;
                      for ( i = v23; i <= v36; ++i )
                      {
                        v27 = pTmpBuf3[i];
                        if ( (unsigned __int8)v27 >= v11->cFirstChar && (unsigned __int8)v27 <= v11->cLastChar
                          || v27 == 12
                          || v27 == 13
                          || v27 == 9
                          || v27 == 10 )
                        {
                          if ( i > v23 )
                            v10 += v11->pMetrics[(unsigned __int8)v27].uLeftSpacing;
                          v10 += *((int *)&v11->cFirstChar + 3 * (unsigned __int8)v27 + 9);
                          if ( i < v36 )
                            v10 += v11->pMetrics[(unsigned __int8)v27].uRightSpacing;
                        }
                      }
                    }
                  }
                }
                else
                {
                  v10 += *(int *)(v15 + 4);
                  v40 = v12;
                }
              }
              else
              {
                if ( !a6 )
                  return (char *)v32;
              }
            }
            else
            {
              v12 += 5;
            }
          }
          else
          {
            v10 = v37;
            v11 = v35;
            v40 = v12;
          }
        }
        else
        {
          strncpy(&Str, &pTmpBuf3[v12 + 1], 3u);
          v34 = 0;
          v28 = atoi(&Str);
          v10 = a5 + v28;
          v12 += 3;
          v37 = v10;
        }
      }
      ++v12;
      v36 = v12;
    }
    while ( v12 < v39 );
  }
  return pTmpBuf3;
}


//----- (0044C6C2) --------------------------------------------------------
char* GUIFont::_44C6C2( const char *pInString, GUIWindow *pWindow, unsigned int uX, int a5 )
    {
  int text_height; // edi@1
  char *text_str; // ebx@3
  int i;
  unsigned char c; // cl@4
  int text_length; 

  text_height = 0;

  if ( !pInString )
    return 0;
  text_str = FitTextInAWindow(pInString, this, pWindow, uX, 0);
  text_length = strlen(text_str);
  //uX_pos=uX_buff_pos;
  for (i=0; i<text_length; ++i )
      {
      c = text_str[i];
      if ( IsCharValid(c) )
          {
          switch (c)
              {
          case '\n':	//Line Feed 0A 10:
              text_height = text_height + uFontHeight - 3;
              if ( text_height >= (signed int)(a5 * pWindow->uFrameHeight) )
                  return &text_str[i+1];
              break;
          case '\f':  //Form Feed, page eject  0C 12       
              i += 5;	  
              break;
          case '\t':	// Horizontal tab 09
          case '\r':   //Carriage Return 0D 13 
              i += 3;
              break;    
              }
          if ( text_height >= (signed int)(a5 * pWindow->uFrameHeight) )
              return &text_str[i];
          }
      }
  return &text_str[i];
}



//----- (0044C62E) --------------------------------------------------------
int GUIFont::GetStringHeight2(GUIFont *a2, const char *Str, int a4, int a5, int a6)
{
  GUIFont *v6; // edi@1
  int v8; // esi@3
  unsigned __int8 *v9; // ebx@3
  size_t v10; // eax@3
  unsigned __int8 v11; // cl@4
  signed int Stra; // [sp+Ch] [bp+8h]@3
  size_t v13; // [sp+14h] [bp+10h]@3

  auto a1 = this;
  v6 = a1;
  if ( !Str )
    return 0;
  v8 = LOBYTE(a1->uFontHeight) - 3;
  v9 = (unsigned __int8 *)GUIFont::_44C933(Str, a1, a2, a4, a5, 0);
  v10 = strlen(Str);
  Stra = 0;
  v13 = v10;
  while ( Stra < (signed int)v13 )
  {
    if ( GUIFont::IsCharValid(v11 = *v9) )
    {
      if ( v11 == 9 )
        goto LABEL_9;
      switch ( v11 )
      {
        case 0xAu:
          v8 = v8 + LOBYTE(v6->uFontHeight) - 3;
          break;
        case 0xCu:
          v9 += 5;
          Stra += 5;
          break;
        case 0xDu:
LABEL_9:
          if ( a6 != 1 )
          {
            v9 += 3;
            Stra += 3;
          }
          break;
      }
    }
    ++Stra;
    ++v9;
  }
  return v8;
}

//----- (0044C59D) --------------------------------------------------------
int GUIFont::CalcTextHeight( const char *pString, struct GUIWindow *pWindow, int uXOffset, int a5 )
    { 
	int uAllHeght; 
	int uStringLen; 
	unsigned char c; 
	char *test_string; 

	if (!pString)
		return 0;
	uAllHeght = this->uFontHeight - 3;
	test_string = FitTextInAWindow(pString, this, pWindow, uXOffset, 0);
	uStringLen = strlen(pString);
	for (int i = 0; i < uStringLen; ++i)
		{
		c = test_string[i];
		if (IsCharValid(c))
			{
			switch (c)
				{
			case '\n':	//Line Feed 0A 10:
				uAllHeght+= this->uFontHeight - 3;
				break;
			case '\f':  //Form Feed, page eject  0C 12 
				i += 5;		  
				break;
			case '\t':	// Horizontal tab 09
			case '\r':   //Carriage Return 0D 13
				if (a5 != 1)
					i += 3;
				break;
				}
			}
		}
	return uAllHeght;
	}


//----- (0044C51E) --------------------------------------------------------
int GUIFont::GetLineWidth(const char *pString)
	{
	int str_len; // ebp@3
	int string_line_width; // esi@3
	unsigned char c;

	if (!pString)
		return 0;
	str_len = strlen(pString);
	string_line_width = 0;
	for ( int i = 0; i < str_len; ++i )
		{
		c = pString[i];
		if (IsCharValid(c))
			{
			switch (c)
				{
			case '\t':
			case '\n':
			case '\r':
				return string_line_width;
			case '\f':
				i += 5;	  
				break;
			default:
				if (i > 0)
					string_line_width += this->pMetrics[c].uLeftSpacing;
				string_line_width += this->pMetrics[c].uWidth;
				if (i < str_len)
					string_line_width +=this->pMetrics[c].uRightSpacing;
				}
			}
		}
	return string_line_width;
	}


//----- (0044C502) --------------------------------------------------------
int GUIFont::AlignText_Center(unsigned int uCenterX, const char *pString)
{
  signed int position; // esi@1
 
  position = (signed int)(uCenterX - GetLineWidth(pString)) >> 1;
  if ( position >= 0 )
    return position;
  else
    return  0;

}

//----- (0044C768) --------------------------------------------------------
char * FitTextInAWindow( const char *pInString, GUIFont *pFont, GUIWindow *pWindow, signed int uX, int a5 )
    {
	unsigned char c;
	int uInStrLen;
	char digits[4];
	int possible_transition_point;
	int string_pixel_Width;
	int start_pixel_offset;

	if (!pInString)
		{
		MessageBoxW(nullptr, L"Invalid string passed !", L"E:\\WORK\\MSDEV\\MM7\\MM7\\Code\\Font.cpp:445", 0);
		return 0;
		}
	uInStrLen = strlen(pInString);
	strcpy(&temp_string[0], pInString);
	if (uInStrLen==0)
		return &temp_string[0];

	start_pixel_offset=string_pixel_Width=uX;
	possible_transition_point=0;
	for(int i=0; i<uInStrLen; ++i) 
		{
		c=temp_string[i];
		if (pFont->IsCharValid(c))
			{
			switch (c)
				{
			case '\t':	// Horizontal tab 09
				{
				strncpy(digits, &temp_string[i+1],3);
				digits[3]=0;
				string_pixel_Width= atoi(digits)+uX;
				i+=3;
				break;
				}
			case  '\n':	//Line Feed 0A 10
				{
				string_pixel_Width=start_pixel_offset;
				possible_transition_point=i;
				break;
				}
			case  '\f':   //Form Feed, page eject  0C 12
				{
				i+=5;  
				break;
				}
			case  '\r':   //Carriage Return 0D 13
				{
				if (!a5)
					return (char*)pInString;
				break;
				}
			case ' ' :
				{
				string_pixel_Width+=pFont->pMetrics[c].uWidth;
				possible_transition_point=i;
				break;
				}
			default:

				if ((string_pixel_Width+pFont->pMetrics[c].uWidth+ pFont->pMetrics[c].uLeftSpacing+
					pFont->pMetrics[c].uRightSpacing)<pWindow->uFrameWidth)
					{
					if(i>possible_transition_point)
						string_pixel_Width+=pFont->pMetrics[c].uLeftSpacing;
					string_pixel_Width+=pFont->pMetrics[c].uWidth;
					if (i<uInStrLen)
						string_pixel_Width+=pFont->pMetrics[c].uRightSpacing;
					}
				else
					{
					temp_string[possible_transition_point]='\n';
					string_pixel_Width=start_pixel_offset;
					if (i>possible_transition_point)
						{
						for(int j=possible_transition_point;j<i; ++j ) 
							{
							c=temp_string[j];
							if (pFont->IsCharValid(c))
								{
								if(j>possible_transition_point)
									string_pixel_Width+=pFont->pMetrics[c].uLeftSpacing;
								string_pixel_Width+=pFont->pMetrics[c].uWidth;
								if (j<i)
									string_pixel_Width+=pFont->pMetrics[c].uRightSpacing;

								}
							}  
						}
					}
				}
			}
		}
	return &temp_string[0];
	}