#include "xjtext.h"

xJTextWindow::xJTextWindow(int Width,int Height,char* FG,char*   SG,char* BG,
			   char* FNA,char* FNAT,char*FNK,char* FNKT)
  :xTextWindow(Width,Height,FG,SG,BG,FNA,FNAT,FNK,FNKT)
{
  MainLineWidth =CharWidthKANJI;
  RubyOffset    =MainLineWidth+2;
  RubyLeftOffset=-CharWidthKANJITINY-2;
  LineWidth     =RubyOffset+CharWidthKANJITINY-RubyLeftOffset+4;
  TopY          =CharHeightKANJI;
  BottomY       =TheHints.height-CharHeightKANJI*3;
  RightX        =TheHints.width-CharWidthKANJI*2; LeftX=CharWidthKANJI;
  CurrentX      =RightX;         CurrentY       =TopY;  
  CurrentColor  =ForegroundColor;CurrentButtonID=0;
  XCharTmp      =new xchar[MaxXCharTmp];XCharTmpCnt  =0;TmpY=0;
  TmpYRuby=0;   TmpYRubyLeft=0;
}

int xJTextWindow::newlineOK      (uchar Byte1,uchar Byte2)
{
  switch(Byte1)
    {
    case 0x21:
      switch(Byte2)
	{
	case 0x22:case 0x23:case 0x24:case 0x25:case 0x26:case 0x27:case 0x28:
	case 0x29:case 0x2a:case 0x30:case 0x31:case 0x32:case 0x33:case 0x34:
	case 0x35:case 0x36:case 0x37:case 0x39:case 0x3c:case 0x44:case 0x45:
	case 0x46:case 0x47:case 0x48:case 0x49:case 0x4b:case 0x4d:case 0x4f:
	case 0x51:case 0x53:case 0x55:case 0x57:case 0x59:case 0x5b:
	  return 0;
	default: return 1;
	}
    case 0x24: case 0x25:
      switch(Byte2)
	{
	case 0x21:case 0x23:case 0x25:case 0x27:case 0x29:case 0x43:case 0x63:
	case 0x65:case 0x67:case 0x6e:
	  return 0;
	default: return 1;
	}
    default: return 1;
    }
}

int xJTextWindow::putText        (char* Text)
{
  int TextLength=strlen(Text);uchar* UText=(uchar*)Text;int I=0;
  int NewPageFlag=0;
  while(I<TextLength)
    {
      if     (CurrentX<LeftX) newPage();
      if     (UText[I]=='\n'){ CurrentX-=LineWidth;CurrentY =TopY;I++;}
      else if(UText[I]=='\f'){ newPage();I++;NewPageFlag=1;}
      else if(UText[I]>=0x80&&UText[I+1]>=0x80)
	{
	  xchar XChar(CurrentColor,CurrentButtonID,CH_KANJI,
		      CurrentX,CurrentY,UText[I]-0x80,UText[I+1]-0x80);
	  addChar(XChar); CurrentY+=CharHeightKANJI;I+=2;
	}
      else if(UText[I]<0x80)
	{
	  xchar XChar(CurrentColor,CurrentButtonID,CH_ASCII,
		      CurrentX,CurrentY,Text[I]);
	  addChar(XChar); CurrentY+=CharHeightASCII;I++;
	}
    }
  return NewPageFlag;
}

int xJTextWindow::putAText       (char* AText)
{
  int TextLength=strlen(AText);int NewPageFlag=0;
  if(CurrentY==TopY)
    {
      if(CurrentX<LeftX){ NewPageFlag=1;newPage();}
      for(int I=0;I<TextLength;++I)
	{
	  xchar XChar(CurrentColor,CurrentButtonID,CH_ASCII,
		      CurrentX,CurrentY,AText[I]);
	  addChar(XChar);CurrentY+=CharHeightASCII;
	}
    }
  else
    {
      if(CurrentY+TextLength*CharHeightASCII+CharHeightASCII/2>BottomY)
	{ CurrentX-=LineWidth;CurrentY=TopY;}
      else CurrentY+=CharHeightASCII/2;
      if(CurrentX<LeftX){ newPage();NewPageFlag=1;}
      for(int I=0;I<TextLength;++I)
	{
	  xchar XChar(CurrentColor,CurrentButtonID,CH_ASCII,
		      CurrentX,CurrentY,AText[I]);
	  addChar(XChar);CurrentY+=CharHeightASCII;
	}
    }
  CurrentY+=CharHeightASCII/2; return NewPageFlag;
}

int xJTextWindow::putJText       (char* JText)
{
  int TextLength=strlen(JText)/2; uchar* UText=(uchar*)JText;int NewPageFlag=0;
  for(int I=0;I<TextLength;++I)
    {
      if(CurrentY>BottomY) if(newlineOK(UText[I*2]-0x80,UText[I*2+1]-0x80))
	{CurrentX-=LineWidth;CurrentY =TopY;}
      if(CurrentX<LeftX){newPage();NewPageFlag=1;}
      xchar XChar(CurrentColor,CurrentButtonID,CH_KANJI,
		  CurrentX,CurrentY,UText[I*2]-0x80,UText[I*2+1]-0x80);
      addChar(XChar);CurrentY+=CharHeightKANJI;
    }
  return NewPageFlag;
}

int xJTextWindow::putHText       (char* AText)
{
  int TextLength=strlen(AText);int NewPageFlag=0;
  if(CurrentY+CharHeightASCII>BottomY){ CurrentX-=LineWidth;CurrentY=TopY;}
  if(CurrentX<LeftX){ newPage();NewPageFlag=1;}
  int Xc=CurrentX+CharWidthKANJI/2-(CharWidthASCIIHOR*TextLength)/2;
  for(int I=0;I<TextLength;++I)
    {
      xchar XChar(CurrentColor,CurrentButtonID,CH_ASCII_HOR,
		  Xc,CurrentY,AText[I]);
      addChar(XChar);Xc+=CharWidthASCIIHOR;
    }
  CurrentY+=CharHeightASCIIHOR; return NewPageFlag;
};

int xJTextWindow::putKunten      (char* JText)
{
  int Direction; XCharStruct OverAll;int Ascent; int Descent;
  for(int I=0;I<strlen(JText)/2;++I)
    {
      XChar2b Char2b;
      Char2b.byte1=(uchar)JText[I*2]  -0x80;
      Char2b.byte2=(uchar)JText[I*2+1]-0x80;
      XTextExtents16(FontKTiny->fontStruct(),&Char2b,1,
		     &Direction,&Ascent,&Descent,&OverAll);
      xchar XChar(CurrentColor,CurrentButtonID,CH_KANJI_TINY,
		  CurrentX,CurrentY,
		  (uchar)JText[I*2]+0x80,(uchar)JText[I*2+1]-0x80);
      addChar(XChar);CurrentY+=CharHeightKANJITINY+OverAll.descent;
    }
  return 0;
}

int xJTextWindow::putKuntenB     (char* JText)
{
  int Direction; XCharStruct OverAll;int Ascent; int Descent;
  int YNow=0;
  for(int I=0;I<strlen(JText)/2;++I)
    {
      XChar2b Char2b;
      Char2b.byte1=(uchar)JText[I*2]  -0x80;
      Char2b.byte2=(uchar)JText[I*2+1]-0x80;
      XTextExtents16(FontKTiny->fontStruct(),&Char2b,1,
		     &Direction,&Ascent,&Descent,&OverAll);
      xchar XChar(CurrentColor,CurrentButtonID,CH_KANJI_TINY,
		  CurrentX,YNow+CurrentY,
		  (uchar)JText[I*2]-0x80,(uchar)JText[I*2+1]-0x80);
      addChar(XChar);YNow+=CharHeightKANJITINY+OverAll.descent;
    }
  if(YNow<CharHeightKANJITINY)
    {
      xchar Bar(CurrentColor,CurrentButtonID,CH_KANJI_TINY,
		CurrentX+CharWidthKANJITINY,CurrentY,0x21,0x3d);
      addChar(Bar);
      CurrentY+=CharHeightKANJITINY;
    }
  else
    {
      xchar Bar(CurrentColor,CurrentButtonID,CH_KANJI_TINY,
		CurrentX+CharWidthKANJITINY,
		CurrentY+(YNow-CharHeightKANJITINY)/2,0x21,0x3d);
      addChar(Bar);
      CurrentY+=YNow;
    }
  return 0;
}

int xJTextWindow::putTmpAText    (char* AText)
{
  if(TmpY!=0 && CurrentY!=0) TmpY+=CharHeightASCII/2;
  for(int I=0;I<strlen(AText);++I)
    {
      if(XCharTmpCnt==MaxXCharTmp) return 1;
      XCharTmp[XCharTmpCnt++]
	.set(CurrentColor,CurrentButtonID,CH_ASCII,0,TmpY,AText[I]);
      TmpY+=CharHeightASCII;
    }
  TmpY+=CharHeightASCII/2;
  return 0;
}

int xJTextWindow::putTmpJText    (char* JText)
{
  for(int I=0;I<strlen(JText)/2;++I)
    {
      if(XCharTmpCnt==MaxXCharTmp) return 1;
      XCharTmp[XCharTmpCnt++].set
	(CurrentColor,CurrentButtonID,CH_KANJI,0,TmpY,
	 (uchar)JText[I*2]-0x80,(uchar)JText[I*2+1]-0x80);
      TmpY+=CharHeightKANJI;
    }
  return 0;
}

int xJTextWindow::putTmpKunten   (char* JText)
{
  int Direction; XCharStruct OverAll;int Ascent; int Descent;
  for(int I=0;I<strlen(JText)/2;++I)
    {
      if(XCharTmpCnt==MaxXCharTmp) return 1;
      XChar2b Char2b;
      Char2b.byte1=(uchar)JText[I*2]  -0x80;
      Char2b.byte2=(uchar)JText[I*2+1]-0x80;
      XTextExtents16(FontKTiny->fontStruct(),&Char2b,1,
		     &Direction,&Ascent,&Descent,&OverAll);
      XCharTmp[XCharTmpCnt++].set(CurrentColor,CurrentButtonID,CH_KANJI_TINY,
				  0,TmpY,
				  (uchar)JText[I*2]  -0x80,
				  (uchar)JText[I*2+1]-0x80);
      TmpY+=CharHeightKANJITINY+OverAll.descent;
    }
  return 0;
}

int xJTextWindow::putTmpKuntenB  (char* JText)
{
  int Direction; XCharStruct OverAll;int Ascent; int Descent;
  int YNow=0;
  for(int I=0;I<strlen(JText)/2;++I)
    {
      if(XCharTmpCnt==MaxXCharTmp) return 1;
      XChar2b Char2b;
      Char2b.byte1=(uchar)JText[I*2]  -0x80;
      Char2b.byte2=(uchar)JText[I*2+1]-0x80;
      XTextExtents16(FontKTiny->fontStruct(),&Char2b,1,
		     &Direction,&Ascent,&Descent,&OverAll);
      XCharTmp[XCharTmpCnt++].set(CurrentColor,CurrentButtonID,CH_KANJI_TINY,
				  0,YNow+TmpY,
				  (uchar)JText[I*2]  -0x80,
				  (uchar)JText[I*2+1]-0x80);
      YNow+=CharHeightKANJITINY+OverAll.descent;
    }
  if(YNow<CharHeightKANJITINY)
    {
      XCharTmp[XCharTmpCnt++].set(CurrentColor,CurrentButtonID,CH_KANJI_TINY,
				  CharWidthKANJITINY,TmpY,0x21,0x3d);
      TmpY+=CharHeightKANJITINY;
    }
  else
    {
      XCharTmp[XCharTmpCnt++].set(CurrentColor,CurrentButtonID,CH_KANJI_TINY,
				  CharWidthKANJITINY,
				  TmpY+(YNow-CharHeightKANJITINY)/2,0x21,0x3d);
      TmpY+=YNow;
    }
  return 0;
}

int xJTextWindow::putTmpARuby    (char* AText)
{
  if(TmpYRuby!=0) TmpYRuby+=CharHeightASCIITINY/2;
  for(int I=0;I<strlen(AText);++I)
    {
      if(XCharTmpCnt==MaxXCharTmp) return 1;
      XCharTmp[XCharTmpCnt++].set(CurrentColor,CurrentButtonID,CH_ASCII_TINY,
				  RubyOffset,TmpYRuby,AText[I]);
      TmpYRuby+=CharHeightASCIITINY;
    }
  TmpYRuby+=CharHeightASCIITINY/2;
  return 0;
}

int xJTextWindow::putTmpJRuby    (char* JText)
{
  for(int I=0;I<strlen(JText)/2;++I)
    {
      if(XCharTmpCnt==MaxXCharTmp) return 1;
      XCharTmp[XCharTmpCnt++].
	set(CurrentColor,CurrentButtonID,CH_KANJI_TINY,RubyOffset,TmpYRuby,
	 (uchar)JText[I*2]-0x80,(uchar)JText[I*2+1]-0x80);
      TmpYRuby+=CharHeightKANJITINY;
    }
  return 0;
}

int xJTextWindow::putTmpARubyLeft(char* AText)
{
  if(TmpYRubyLeft!=0) TmpYRubyLeft+=CharHeightASCIITINY/2;
  for(int I=0;I<strlen(AText);++I)
    {
      if(XCharTmpCnt==MaxXCharTmp) return 1;
      XCharTmp[XCharTmpCnt++].set(CurrentColor,CurrentButtonID,CH_ASCII_TINY,
				  RubyLeftOffset,TmpYRubyLeft,AText[I]);
      TmpYRubyLeft+=CharHeightASCIITINY;
    }
  TmpYRubyLeft+=CharHeightASCIITINY/2;
  return 0;
}

int xJTextWindow::putTmpJRubyLeft(char* JText)
{
  for(int I=0;I<strlen(JText)/2;++I)
    {
      if(XCharTmpCnt==MaxXCharTmp) return 1;
      XCharTmp[XCharTmpCnt++].
	set(CurrentColor,CurrentButtonID,CH_KANJI_TINY,RubyLeftOffset,
	    TmpYRubyLeft,(uchar)JText[I*2]-0x80,(uchar)JText[I*2+1]-0x80);
      TmpYRubyLeft+=CharHeightKANJITINY;
    }
  return 0;
}

int xJTextWindow::setTmpToText (void)
{
  int NewPageFlag=0;
  int TextHeight=(TmpY>TmpYRuby?TmpY:TmpYRuby)>TmpYRubyLeft?
                 (TmpY>TmpYRuby?TmpY:TmpYRuby):TmpYRubyLeft;

  if     (TmpY==TextHeight)
    {
      for(int I=0;I<XCharTmpCnt;++I)
	{ 
	  if     (XCharTmp[I].x()==RubyOffset)
	    { XCharTmp[I].offsetXY(0,(TmpY-TmpYRuby)/2);}
	  else if(XCharTmp[I].x()==RubyLeftOffset)
	    { XCharTmp[I].offsetXY(0,(TmpY-TmpYRubyLeft)/2);}
	}
    }
  else if(TmpYRuby==TextHeight)
    {
      TextHeight=TmpYRuby;
      for(int I=0;I<XCharTmpCnt;++I)
	{ 
	  if     (XCharTmp[I].x()!=RubyOffset&&XCharTmp[I].x()!=RubyLeftOffset)
	    { XCharTmp[I].offsetXY(0,(TmpYRuby-TmpY)/2);}
	  else if(XCharTmp[I].x()==RubyLeftOffset)
	    { XCharTmp[I].offsetXY(0,(TmpYRuby-TmpYRubyLeft)/2);}
	}
    }
  else if(TmpYRubyLeft==TextHeight)
    {
      for(int I=0;I<XCharTmpCnt;++I)
	{ 
	  if     (XCharTmp[I].x()==RubyOffset)
	    { XCharTmp[I].offsetXY(0,(TmpYRubyLeft-TmpYRuby)/2);}
	  else if(XCharTmp[I].x()!=RubyLeftOffset)
	    { XCharTmp[I].offsetXY(0,(TmpYRubyLeft-TmpY)/2);}
	}
    }
  if(CurrentY==TopY)
    {
      if(CurrentX<LeftX){ NewPageFlag=1;newPage();}
      for(int I=0;I<XCharTmpCnt;++I)
	{ XCharTmp[I].offsetXY(CurrentX,CurrentY); addChar(XCharTmp[I]);}
    }
  else
    {
      if(CurrentY+TextHeight>BottomY){CurrentX-=LineWidth;CurrentY=TopY;}
      else if(XCharTmp[0].type()==CH_ASCII) CurrentY+=CharHeightASCII/2;
      if(CurrentX<LeftX){ NewPageFlag=1;newPage();}
      for(int I=0;I<XCharTmpCnt;++I)
	{ XCharTmp[I].offsetXY(CurrentX,CurrentY); addChar(XCharTmp[I]);}
    }
  CurrentY+=TextHeight;TmpY=TmpYRuby=TmpYRubyLeft=0;XCharTmpCnt=0;
  return NewPageFlag;
}














