#include "read.h"
#include "Graphics.h"
//UINT32 show_txt( char*txt,UINT16 start,UINT16 len,UINT8 x,UINT8 y);
//UINT32 Pixel_txt(UINT8 x,UINT8 y);
//UIS_DRAWING_BUFFER_T		bufd;
#define bmap                 0x10d0aea0
UINT16 GBCodeToBig5Code(char *str);
//UINT16 Big5toUCS2(char *str);
UINT8 max_h;
WCHAR text[25];
//WCHAR text1[25];
UINT8 topoffset;
UINT8 gboff; //GB是否缺位
UINT8 r_type;
WCHAR r_start[]={0x7FFB,0x9875,0};
WCHAR r_start1[]={0x6EDA,0x52A8,0};
WCHAR r_stop[]={0x505C,0x6B62,0};
UINT8 SET_A=16;
UINT8 MODE_AUTO=0;
BOOL X_TIME=FALSE;
//UINT8 r_key=99;
//WCHAR tem_gb[]={0xCED2,0xB0AE,0xC4E3,0xCED2,0xB5C4,0xC9B5,0xB9CF,0xD1BD,0x31,0x32,0x33,0x34,0x35,0x36,0x34,0x36,0x35,0x34,0};
char tem_u[LEN];
//UINT8 gboff1;
//int  num;
const EVENT_HANDLER_ENTRY_T read_state_handlers[]  =
{

	{ EV_TIMER_EXPIRED,       		         rdTimer },
    { CANVAS_EV_KEY_PRESS,             rdKeypress },
	//{ CANVAS_EV_KEY_RELEASE,             rdKeypress },
	//{ EV_KEY_PRESS,								rKeypress	},
    { STATE_HANDLERS_END,           		      	  NULL },
};
UINT32 rdTimer( EVENT_STACK_T *ev_st,  void *app )
{
    APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
	UINT32                	    status=RESULT_OK;
	if(((DL_TIMER_DATA_T*)(AFW_GetEv(ev_st)->attachment))->ID==20)
	{
	//if(papp->R_TIME)
	//{

	APP_UtilStopTimer(app);
	//DL_KeyInjectKeyPress(99,0,0);
	DL_KeyInjectKeyPress(99,1,0);
	//if(papp->tem_j+max_h<=ROWNUM)
	//{
	if(MODE_AUTO==1)
	papp->tem_j+=SET_H;
	else if(MODE_AUTO==2)
	{
		SET_A+=SET_X;
		if(SET_A>=SET_Y)
		{
		SET_A-=SET_Y;
		papp->tem_j+=1;
		}
	}

	status = showTXT(ev_st,app);	
	//}
	//else
	//papp->R_TIME=FALSE;
	//}
	}
	
	return status;
}
/*UINT32 rKeypress( EVENT_STACK_T *ev_st,  void *app )
{
	APP_ConsumeEv(ev_st, app);
	APP_UtilStopTimer(app);
	return RESULT_OK;
}*/
UINT32 rdKeypress( EVENT_STACK_T *ev_st,  void *app )
{	
    APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
	UINT32                	    status=RESULT_OK;
	EVENT_T     *event = AFW_GetEv(ev_st);
	UINT32		KP=event->data.key_pressed;
	APP_ConsumeEv(ev_st, app);
	APP_UtilStopTimer(app);
	//if(papp->setmode==MAIN_SET_C||papp->mode==MODE_P)
	if(papp->mode==MODE_P)
	{
	papp->d_true=TRUE;
	papp->mode=MODE_NULL;

	status = APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );	
	return status;
	}
	switch(KP)
	{
	case KEY_3:		//跳转
	papp->mode=MODE_NULL;
	status = APP_UtilChangeState(HE_STATE_GOTO, ev_st, app );
   	break;
	case KEY_RED:
	APP_ConsumeEv(ev_st, app);
	status = destroyApp(ev_st, app );
	break;
	case KEY_SOFT_LEFT: //退出 返回
	if(cmd_file)
	status = destroyApp(ev_st, app );
	else
	{
	papp->mode=MODE_NULL;
	papp->d_true=TRUE;
	status = APP_UtilChangeState(HE_STATE_FB, ev_st, app );
	//status = APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );
	}
	break;
	
	case KEY_SOFT_RIGHT: //编码
	papp->setmode=MAIN_TYPE;
	papp->mode=MODE_EDIT;
	papp->set=TRUE;
	papp->d_true=TRUE;
	status = APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );	
	break;
	
	case KEY_MENU: //菜单
	papp->mode=MODE_MENU;
	status = APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );
	break;
	
	case KEY_JOY_OK: //编辑
	case KEY_5:
	status = APP_UtilChangeState(HE_STATE_EDIT, ev_st, app );
	break;
	
	case KEY_LEFT:  //后退一页
	case KEY_4:
	//r_key=KEY_4;
	papp->tem_j-=max_h;	
	showTXT(ev_st,app);	
   	break;
	
	case KEY_RIGHT: //前进一页
	case KEY_6:
	//r_key=KEY_6;
	if(papp->tem_j+max_h<=ROWNUM)
	{
	papp->tem_j+=max_h;
	showTXT(ev_st,app);	
	}
   	break;
	
	case KEY_UP:  //后退一行
	case KEY_2:
	//r_key=KEY_2;
	papp->tem_j-=1;	
	showTXT(ev_st,app);	
   	break;
	
	case KEY_DOWN: //前进一行
	case KEY_8:
	//r_key=KEY_8;
	if(papp->tem_j+1<=ROWNUM)
	{
	papp->tem_j+=1;
	showTXT(ev_st,app);	
	}
	break;
	
	case KEY_GREEN:	//书签
	case KEY_1:
	papp->mode=MODE_MARK;
	status = APP_UtilChangeState(HE_STATE_MARK, ev_st, app );
   	break;
	
	case KEY_0: //自动翻页
	//APP_UtilStopTimer(app);

	if(MODE_AUTO==2)
	MODE_AUTO=0;
	else
	MODE_AUTO++;
	
	/*if(papp->R_TIME&&X_TIME)
	{
	papp->R_TIME=FALSE;
	X_TIME=FALSE;
	}
	else if(papp->R_TIME)
	X_TIME=TRUE;
	else
	papp->R_TIME=TRUE;
	if(papp->R_TIME)*/	

	KP=MODE_AUTO;
	MODE_AUTO=0;
	showTXT(ev_st,app);
	MODE_AUTO=KP;	
	show_auto(MODE_AUTO);
	APP_UtilStartTimer(1000, 20, app);
	//showTXT(ev_st,app);
	//show_auto(MODE_AUTO);
	break;
	
	case KEY_STAR: //自动翻页
	if(SET_T>100)
	{
	SET_T-=100;
	}
	else
	{SET_T=100;}
	status=showTXT(ev_st,app);
	break;
	
	case KEY_POUND: //自动翻页
	SET_T+=100;
	status=showTXT(ev_st,app);
	break;
	}
	return status;
}
UINT32 ReadStateEnter( EVENT_STACK_T *ev_st,  void *app,  ENTER_STATE_TYPE_T type )
{
    APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
    SU_PORT_T               port = papp->apt.port;
	int tmp;
	max_h=(display_size.y+1)/SET_Y;
	papp->READ=TRUE;
	if(type!=ENTER_STATE_ENTER) return RESULT_OK;

      bufd.w = display_size.x+1;
      bufd.h = display_size.y+1;
      bufd.p_buf = NULL;

	//dialog = UIS_CreateColorCanvas ( &port, &bufd, TRUE ); // 耦玟喔?赅礅囫

	dialog = UIS_CreateColorCanvas ( &port, &bufd, TRUE ); // 耦玟喔?赅礅囫
    if(dialog == 0) return RESULT_FAIL;
	
	papp->apt.dialog = dialog;
	Background();
	if(papp->mode==MODE_P)
	show_c();
	else if(!DL_FsFFileExist(papp->openfile))
	{
	show_file(papp->openfile);
	papp->mode=MODE_P;
	}
	else 
	{
			if(papp->mode==MODE_FB)
			{
			papp->tem_x=0;
			papp->offset=0;
			OpenFile(ev_st,app);
			PrintTXT(ev_st,app);
			}else
			if(papp->mode==MODE_EDIT)
			{
			//papp->tem_j=papp->tem_j-papp->tem_x;
			papp->tem_x=0;	
			tmp=papp->offset;
			OpenFile(ev_st,app);
			papp->offset=tmp;
			tmp=papp->tem_j;
			PrintTXT(ev_st,app);
			papp->tem_j=tmp;
			}else if(papp->mode==MODE_CMD)
			{
			OpenFile(ev_st,app);
			PrintTXT(ev_st,app);
			}
			showTXT(ev_st, app);
	}
    return RESULT_OK;
}
UINT32 Background(void)
{	
	if(r_pic)
	DrawImageId(0,0);
	else
	{
	SetFillColor(SET_C_I[3],SET_C_I[4],SET_C_I[5],0);
	FillRect(0,0,display_size.x+1,display_size.y+1); 
	}
	SetForegroundColor(SET_C_I[0],SET_C_I[1],SET_C_I[2],0);
	
	//show_txt((char*)r_stop,0,4,0,0);
	return RESULT_OK;
}
UINT32 show_c(void)
{	
	SetForegroundColor(SET_C_I[0],SET_C_I[1],SET_C_I[2],0);
	DrawText(L"Welcome to TxtEdit",display_size.x/2-56,display_size.y/2-8,ANCHOR_LEFT, ANCHOR_TOP);  
	DrawText(L"TxtEdit",display_size.x/2-20,24,ANCHOR_LEFT, ANCHOR_TOP);
	DrawText(L"by:e314521",display_size.x/2-32,display_size.y-40,ANCHOR_LEFT, ANCHOR_TOP);
	return RESULT_OK;
}
UINT32 show_auto(UINT8 mode)
{
	//SetFillColor(255,255,255,0);
	//FillRect(display_size.x/2-22,display_size.y/2-8,40,16); 
	SetForegroundColor(255,0,0,0);
	if(mode==0)
	DrawText(r_stop,display_size.x/2-16,display_size.y/2-8,ANCHOR_LEFT, ANCHOR_TOP); 	
	else if(mode==1)
	DrawText(r_start,display_size.x/2-16,display_size.y/2-8,ANCHOR_LEFT, ANCHOR_TOP); 
	else DrawText(r_start1,display_size.x/2-16,display_size.y/2-8,ANCHOR_LEFT, ANCHOR_TOP); 
	return RESULT_OK;
}
UINT32 show_file(WCHAR *uir)
{

	SetForegroundColor(255,0,0,0);
	DrawText(uir,4,display_size.y/2-24,ANCHOR_LEFT, ANCHOR_TOP); 
	DrawText(L"THE FILE NOT FOUND >>>",4,display_size.y/2+8,ANCHOR_LEFT, ANCHOR_TOP); 
	

	return RESULT_OK;
}
UINT32 show_Percent(UINT8 i)
{
	SetFillColor(255,0,0,0);
	FillRect(display_size.x,0,1,i); 
	return RESULT_OK;
}
/*UINT32 ReadStateExit( EVENT_STACK_T * ev_st, void * app, EXIT_STATE_TYPE_T type )
{
	APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
	UINT32			status;
	
	dbg("FBStateExit: Enter, type = %d", type);
	if ( type != EXIT_STATE_EXIT )
		return RESULT_OK;
	
	status = RESULT_OK;
	status = APP_UtilUISDialogDelete( &papp->apt.dialog );
	
	dbg("FBStateExit: END, status = %d", status);
	return status;
}*/
GRAPHIC_METRIC_T getfontsize(WCHAR *str,	UINT8 font_id)
{
	GRAPHIC_METRIC_T string_size;

	UIS_CanvasGetStringSize(str,&string_size,font_id);
	return string_size;
}
int getCodeType(char* str) 
{		
		//int result = TYPE_ANSI;
		int result = atype;	//ansi
	  
  if (!str || (str[0] == 0 && str[1] == 0)) return 0;

  
  if (str[0] == 0xff && str[1] == 0xfe) {
    result = TYPE_UCL; //unicode le
  }
  
  else if (str[0] == 0xfe && str[1] == 0xff) {
    result = TYPE_UCS; //unicode be
  }
  
  else if (str[0] == 0xef && str[1] == 0xbb && str[2] == 0xbf) {
    result = TYPE_UTF8; //utf8
  }
  return result;
}
UINT32 OpenFile(EVENT_STACK_T * ev_st, void * app)
{
    APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
	//udbg("openfile=%s",papp->openfile);
	
	//if(DL_FsFFileExist(papp->openfile))
	//{
		char buf3[3];
		FILE_HANDLE_T fd;
		size_t readen;
		//if(!DL_FsFFileExist(papp->openfile))
		//return RESULT_OK;
		fd = DL_FsOpenFile( papp->openfile,FILE_READ_PLUS_MODE,0);
		if (fd == FILE_HANDLE_INVALID  ) return RESULT_FAIL;
		papp->file_size = DL_FsGetFileSize( fd );
		//dbg("papp->file_size=%d",papp->file_size);
		if(papp->file_size==0)
			{
			DL_FsCloseFile(fd);
			return RESULT_OK;
			}
		if(papp->type!=TYPE_NULL)
		{
		r_type=papp->type;
		topoffset=0;
		}
		else
		r_type=TYPE_AUTO;
		if(r_type==TYPE_AUTO)
		{
		DL_FsReadFile(buf3, 3, 1, fd, &readen );
		r_type = getCodeType(buf3);				
		//dbg("type=%d",r_type);
				switch (r_type) 
					{
						case TYPE_BIG:
						case TYPE_ANSI:
						case TYPE_XBIG5:
						topoffset=0;
						break;
				
						case TYPE_UCS:			
						case TYPE_UCL:
						topoffset=2;
						break;
  			
						case TYPE_UTF8:
						topoffset=3; 
						break;
					}
		}	
		DL_FsCloseFile(fd);

					//topoffset=0;
		////背景
		//paint();
		//paint();
		gboff=0;

	//}
	return RESULT_OK;
}


UINT32 showTXT(EVENT_STACK_T * ev_st, void * app)
{
    GRAPHIC_POINT_T anchor_point;
	APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
    COLOR_T                 color;
	UINT8 h=max_h;
	//UINT8 h=(display_size.y+1)/SET_Y;//MODE=0;
	UINT16 i,temoff=0;
	int j=papp->tem_j;
    color.transparent = 255;
	//dbg("showTXT:ENTER",NULL);
	//dbg("showTXT:papp->tem_j=%d",papp->tem_j);

	if(papp->tem_j<0)
	{

		if(papp->offset>topoffset)
		{
		if(papp->offset-LEN>topoffset)
		papp->offset-=LEN;
		else
		papp->offset=0;	

		//dbg("showTXT:papp->offset=%d",papp->offset);	

		PrintTXT(ev_st,app);
			if(papp->tem_x==0)
			papp->tem_j=ROWNUM+j;
			else
			{
			papp->tem_j=ROWNUM-h-papp->tem_x;
			papp->tem_x=0;
			}
		}
		else
		{

		papp->tem_j=0;
		return RESULT_OK;		
		}
	}
	//dbg("showTXT:papp->tem_j=%d",papp->tem_j);
	//dbg("showTXT:ROWNUM=%d",ROWNUM);


	

	
	j=papp->tem_j;	
	//dbg("showTXT:ENTER2",NULL);
	if(j+h>ROWNUM)
	{
		papp->tem_x=ROWNUM-j;

		//dbg("showTXT:SHOW[j].bufpos=%d",SHOW[j].bufpos);	
		//dbg("showTXT:SHOW[ROWNUM].bufpos=%d",SHOW[ROWNUM].bufpos);
		if(papp->file_size > papp->offset+topoffset+LEN)
		{		
		for(i=j;i<=ROWNUM;i++)
		tem_bufpos[i-j]=SHOW[i].bufpos-SHOW[j].bufpos;
		temoff=SHOW[ROWNUM].off-SHOW[j].off;

		papp->offset=papp->offset+LEN-gboff;
		
		//dbg("papp->offset=%d",papp->offset);
		rowend=TRUE;

		PrintTXT(ev_st,app);
		if(ROWNUM<10)
		h=ROWNUM;
		rowend=FALSE;
		j=0;
		}else
		h=papp->tem_x;
		

		
	}
		Background();
		
		for(i=0;i<h;i++)		
		{

		anchor_point.x = 0;
		if(MODE_AUTO==2)
		anchor_point.y =(i+1)*SET_Y-1-SET_A;		
		else
		anchor_point.y =i*SET_Y-1;
		UIS_CanvasSetFont(1, dialog);
		UIS_CanvasSetBackgroundColor( color);		
		//dbg("SHOW[j+1].bufpos=%d SHOW[j].bufpos=%d",SHOW[j+1].bufpos,SHOW[j].bufpos);		

		UIS_CanvasDrawColorText (showtxt,
								SHOW[j].bufpos,
								(UINT16)(SHOW[j+1].bufpos-SHOW[j].bufpos), 				
								anchor_point,    
								(UINT16)(ANCHOR_LEFT | ANCHOR_TOP),	    
								dialog);							
								j++;	
								

									
		}	
		if(h!=max_h)
		{
		MODE_AUTO=0;
		SetForegroundColor(255,0,0,0);
		DrawText(L"FILE IS END>>>>>>>",1,h*SET_Y-1,ANCHOR_LEFT, ANCHOR_TOP);
		}
	
	//dbg("showTXT:tem_x=%d",papp->tem_x);	
	//dbg("papp->tem_j=%d",papp->tem_j);	
	papp->startoff=papp->offset+SHOW[papp->tem_j].off-temoff+topoffset;
	papp->endoff=SHOW[j].off-SHOW[papp->tem_j].off+temoff;

	//dbg("papp->offset=%d",papp->offset);
	//dbg("papp->startoff=%d",papp->startoff);
	//dbg("papp->endoff=%d",papp->endoff);
	show_Percent(papp->startoff*(display_size.y+1)/papp->file_size);//更加准确
	if(MODE_AUTO!=0)
	{
	APP_UtilStartTimer(SET_T, 20, app);
	}
	

	return RESULT_OK;
}

UINT32 PrintTXT(EVENT_STACK_T * ev_st, void * app)
{
	APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
	//char nbuf[2];
	char buf[LEN+1];
	int tem_i=0;
	//UINT8 g=0;
	//char 	buf1[1];
	WCHAR buff[2]={0,0};//,ad;
	char cbuf[2];//,ad;
	int j=0,cr=0,num1=0;
	int w=0,
	//WCHAR tem_1[]={0x9954,0};
	row=0;
	FILE_HANDLE_T fd;//文件指针 句柄
	size_t readen;
	int i=0;
	//dbg("PrintTXT:ENTER",NULL);
	//gboff1=gboff;

	//dbg("gboff[0]=%d",gboff);
	fd=DL_FsOpenFile(papp->openfile, FILE_READ_MODE, 0);
	DL_FsFSeekFile( fd , papp->offset+topoffset , 0);
	//dbg("PrintTXT:papp->offset=%d",papp->offset+topoffset);
  if(papp->offset+topoffset+LEN<=papp->file_size)
  	{                                                  
		DL_FsReadFile(buf,LEN,1,fd, &readen );
		DL_FsCloseFile(fd);                                                         
		num1=LEN;
    }
  else
  	{			                             
		DL_FsReadFile(buf,1,papp->file_size-topoffset-papp->offset,fd, &readen );
		DL_FsCloseFile(fd);                                                              
		num1=papp->file_size-papp->offset-topoffset;
    }

		if(rowend&&papp->tem_x!=0)
		{
			for(j=0;j<SHOW[ROWNUM].bufpos-SHOW[ROWNUM-papp->tem_x].bufpos;j++)//读取上次最后几行文本
			showtxt[j]=showtxt[j+SHOW[ROWNUM-papp->tem_x].bufpos];

			
		}

	cr=num1/10+2+4;
	if ( SHOW ) {
		suFreeMem(SHOW);
		SHOW = NULL;
	}
	if(SHOW == NULL)
	SHOW=suAllocMem(cr*sizeof(RD_T), NULL);
	SHOW[0].bufpos=0;
	if(rowend&&papp->tem_x!=0)
	{
		/*for(j=0;j<tem_bufpos[tem_x];j++)
		showtxt[j]=tem_showtxt[j];*/

		for(i=0;i<=papp->tem_x;i++)
		{
		SHOW[i].bufpos=tem_bufpos[i];//读取上次几行的位置
		//dbg("tem_bufpos[i]=%d",tem_bufpos[i]);
		}
		i=0;
		
		row+=papp->tem_x;

	}
	
	//dbg("PrintTXT:while",NULL);
	//dbg("PrintTXT:i=%d",i);
	gboff=0;
/*	switch(r_type)
	{		
			case TYPE_ANSI:
			case TYPE_BIG:
			if(buf[i]==0x0a)
			{
			i++;
			SHOW[row].off=1;
			}
			break;
			case TYPE_UCL:
			case TYPE_UCS:
			if(buf[i]==0&&buf[i+1]==0x0a)
			{
			i+=2;
			SHOW[row].off=2;
			}
			break;
	}*/
	switch(r_type)
	{
			case TYPE_ANSI:
			case TYPE_BIG:
			case TYPE_XBIG5:
			case TYPE_BIG5:
			
			while(i< num1) //防止越界
			{	
				//dbg("PrintTXT:buf[i]=%d",buf[i]);
				cbuf[0]=buf[i];
				cbuf[1]=buf[i+1];
				if(buf[i]<0x81)
				//if(buf[i]<0xA1)
				{
					
					if(buf[i]==0x0d)
					{					
					w=0;
					row++;
					SHOW[row].bufpos=j;
					SHOW[row].off=i;
					i+=2;//++;
					}else if(buf[i]==0x0a)		
					{
					w=0;
					row++;
					SHOW[row].bufpos=j;
					SHOW[row].off=i;
					i++;
					}					
					else
					{
					//buff[0]=GBtoU(cbuf,1);
					//buff[0]=0;
					//u_atou(cbuf,buff);
					buff[0]=buf[i];
					buff[1]=0;
					showtxt[j]=buff[0];
					showtxt[j+1]=0;

						if((w+=getfontsize(buff,1).w )>display_size.x+1)
						{
						row++;
						SHOW[row].bufpos=j;
						SHOW[row].off=i-tem_i;
						w=getfontsize(buff,1).w;
						}
					//dbg("PrintTXT:w=%d",getfontsize(buff,1).w);
					i++;
					j++;
					}
				}else if(i!=num1-1)
				{
							
				buff[0]=GBtoU(cbuf,r_type);
				//dbg("PrintTXT:cbuf=%d%d",cbuf[0],cbuf[1]);
				//buff[0]=GBCodeToBig5Code(cbuf);
				dbg("PrintTXT:buff[0]=%d",buff[0]);
				showtxt[j+1]=0;
				showtxt[j]=buff[0];	
					if((w+= getfontsize(buff,1).w )>display_size.x+1)
						{
						row++;
						SHOW[row].bufpos=j;
						SHOW[row].off=i;
						w=getfontsize(buff,1).w;
						}
						j++;
						i+=2;
				}else 
				{
				//gboff=buf[i];
				gboff=1;					
				break;
				}

			}			
			//i-=gboff;
			break;
		case TYPE_UCL:
			while(i< (num1-1)) //防止越界
			{	
				//dbg("PrintTXT:i=%d",i);
				//buff[0]=Char16(buf[i],buf[i+1]);
				//buff[0]=(buf[i+1]<<8)|buf[i];
				buff[0]=(buf[i+1]<<8)|buf[i];
				i+=2;

				if(buff[0]==0x0d)
				{
				i+=2;
				w=0;
				row++;
				SHOW[row].bufpos=j;
				SHOW[row].off=i-4;	
				}else if(buff[0]==0x0a)		
					{
					w=0;
					row++;
					SHOW[row].bufpos=j;
					SHOW[row].off=i-2;
					}else
							{
							showtxt[j+1]=0;
							showtxt[j]=buff[0];					  							  		
							if((w+= getfontsize(buff,1).w )>display_size.x+1)
							{
							row++;
							SHOW[row].bufpos=j;
							SHOW[row].off=i-2;
							w=getfontsize(buff,1).w;
							}
							//udbg("buff=%s",buff);
							j++;
							}
			}
			break;
			case TYPE_UCS:
			while(i< (num1-1)) //防止越界
			{	
				//dbg("PrintTXT:i=%d",i);
				//buff[0]=Char16(buf[i],buf[i+1]);
				buff[0]=(buf[i]<<8)|buf[i+1];
				i+=2;

				if(buff[0]==0x0d)
				{
				i+=2;
				w=0;
				row++;
				SHOW[row].bufpos=j;
				SHOW[row].off=i-4;	
				}else if(buff[0]==0x0a)
				{
				w=0;
				row++;
				SHOW[row].bufpos=j;
				SHOW[row].off=i-2;	
				}
				else
							{
							showtxt[j+1]=0;
							showtxt[j]=buff[0];					  							  		
							if((w+= getfontsize(buff,1).w )>display_size.x+1)
							{
							row++;
							SHOW[row].bufpos=j;
							SHOW[row].off=i-2;
							w=getfontsize(buff,1).w;
							}
							//udbg("buff=%s",buff);
							j++;
							}
			}
			break;
			case TYPE_UTF8:
			while(i< num1) 
			{
				tem_i=i;
				if ((buf[i] & 0x80) == 0)
				{
				buff[0]=buf[i];
				i++;
				//j++;
				}else if((buf[i] & 0xE0) == 0xC0) 
				{
				buff[0]=(buf[i] & 0x3F) << 6|(buf[i+1] & 0x3F);
				//j++;
				i+=2;
				}else if((buf[i] & 0xF0) == 0xE0) 
				{
				buff[0]=( (buf[i] & 0x1F) << 12|(buf[i+1] & 0x3F) << 6|buf[i+2] & 0x3F);
				//j++;
				i+=3;
				}else if((buf[i] & 0xF8) == 0xF0) 
				{
				buff[0]= (buf[i] & 0x0F) << 18 |(buf[i+1] & 0x3F) << 12 | (buf[i+2] & 0x3F) << 6| (buf[i+3] & 0x3F);
				//j++;
				i+=4;
				}else
				{
				i++;
				continue;
				}
				
				if(buff[0]==0x0d)
				{
				w=0;
				row++;
				SHOW[row].bufpos=j;
				SHOW[row].off=i;
				i++;
				}else if(buff[0]==0x0a)
				{
				w=0;
				row++;
				SHOW[row].bufpos=j;
				SHOW[row].off=i;
				}
				else 
				{
				showtxt[j+1]=0;
				showtxt[j]=buff[0];	
				if((w+= getfontsize(buff,1).w )>display_size.x+1)
							{
							row++;
							SHOW[row].bufpos=j;
							SHOW[row].off=tem_i;
							w=getfontsize(buff,1).w;
							}
				j++;
				}
			}
			break;
	}
	// if(row==0)
	// {
   	// SHOW[++row].bufpos=j;
	// SHOW[row].off=i;
	// }
   //else if(buff[0]!=0x0d)
   //{
    	SHOW[++row].bufpos=j;
		SHOW[row].off=i;
	//}
   //else
   //{
   // 	SHOW[row].bufpos=j;
	//	SHOW[row].off=i;
	//}
	/*
   if(row==0)
   	 bufpos[++row]=j;	
	row++;
	SHOW[row].bufpos=j-1;
	SHOW[row].off=i;*/

	
	//dbg("gboff=%d",gboff);
	//rowoffset[0]= offset-LEN;
	//rowoffset[1]= i;
	//rowoffset[2]= offset+num;
	//dbg("PrintTXT:END",NULL);
    ROWNUM=row;
	papp->tem_j=0;
	//UtoGB(tem_gb,tem_u);
	return RESULT_OK;
}

UINT16 GBtoU(char *str,UINT8 i)
{

	text[0]=0;
	/*if(i==TYPE_ANSI)
	{
	//DL_Char_convBig5toUCS2Buf((UINT8*)str,2,text,4);
	DL_Char_convGB2312toUCS2Buf((UINT8*)str,2,text,4);
	return text[0];
	}else if(i==TYPE_BIG5)
	{
	DL_Char_convBig5toUCS2Buf((UINT8*)str,2,text,4);
	return text[0];
	}
	//return Big5toUCS2(str);
	else if(i==TYPE_XBIG5)
	return GBCodeToBig5Code(str);
	return GBKtoU(str,i);
	*/
	switch (i) 
	{
	case TYPE_ANSI:
	DL_Char_convGB2312toUCS2Buf((UINT8*)str,2,text,4);
	break;
	case TYPE_BIG5:
	DL_Char_convBig5toUCS2Buf((UINT8*)str,2,text,4);
	break;
	case TYPE_XBIG5:
	GBCodeToBig5Code(str);
	break;
	case TYPE_BIG:
	GBKtoU(str,i);
	break;
	}
	return text[0];
}
UINT16 GBKtoU(char *str,UINT8 i)
{
	UINT16 tmp=0;
	UINT8  temp[2];
			tmp = str[0] - 0x81;
			tmp = tmp * 0xBF + str[1] - 0x40;
			tmp = tmp << 1;
			if (tmp < 0xBC04)
			{
				temp[0] = *(UINT8*)(map_id+tmp);
				temp[1] = *(UINT8*)(map_id+tmp+1);
				text[0] = (temp[1] << 8) | temp[0];
			}else text[0]=0x220e;
			
	return text[0];
}
UINT16 GBCodeToBig5Code(char *str)
{
	UINT32 j=0;
	char tem1[2];
	dbg("GBCodeToBig5Code",NULL);
	//dbg("GBCodeToBig5Code:tem1=0x%p%p",tem1[0],tem1[1]);
   if ((str[1] >= 0xa1) && (str[1]  <= 0xfe))
   {
   	
      if ((str[0] >= 0xa1) && (str[0] <= 0xa9))
      {
         j = ((str[0] - 0xa1) * 94 + (str[1]  - 0xa1)) * 2; 
      } 
      else if ((str[0] >= 0xb0) && (str[0] <= 0xf7))
      {
         j = ((str[0] - 0xb0 + 9) * 94 + (str[1]  - 0xa1)) * 2;
      }
		 //tem1[0]=*(UINT8*)(bmap+j);
		 //tem1[1]=*(UINT8*)(bmap+j+1);
		tem1[0]=g2bTable[j];
		tem1[1]=g2bTable[j+1];
		GBtoU(tem1,TYPE_BIG5);
		text[1]=0;
		//text[0]=Big5toUCS2(tem1);
		if(text[0]<0x81) 
		text[0]=0x220e;
   }else
   text[0]=0x220e;
   //DL_Char_convBig5toUCS2Buf((UINT8*)tem,2,text,4);
   //Big5toUCS2(tem);
   //dbg("GBCodeToBig5Code:text[0]=%d",text[0]);
   //GBtoU(tem,3);
   
   //dbg("GBCodeToBig5Code:g2bTable[j]=0x%p%p",g2bTable[j],g2bTable[j+1]);
return text[0];
}
/*
UINT16 Big5toUCS2(char *str)
{
	text[0]=0;
	DL_Char_convBig5toUCS2Buf((UINT8*)str,2,text,4);
	text[1]=0;
	return text[0];
}*/