// Code.cpp: implementation of the CCode class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kssm.h" // KSC5601 -> 炼钦屈 内靛 函券钎
#include "ksc5601.h" // 炼钦屈 -> KSC5601 内靛 函券钎
#include "cjz.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCode::CCode()
{

}

CCode::~CCode()
{

}
// convert KSC5601 to C,J,Z
/*
________________________________________________________________
  
Function Name	: GetKoreanType
	
Purpose			: 涝仿等 内靛肺何磐 硅摹痹蘑阑 舅酒郴绰巴
		
Input			:
	code		: 内靛

Output			: No	

Return Value	: 硅摹痹蘑锅龋 ( 0 ~ 5 )
dl
Description		: 

Ref Journal		: "Bayesian Network Modeling of Hangul Characters for On-line Handwriting Recognition"  
					Sung-Jung Cho*, Jin H. Kim   ICDAR (2003), pp. 207-211 vol.1
	
date			: 2009/06/5
________________________________________________________________
*/
int CCode::GetKoreanType(WORD code)
{
	int type;
	int CJZid[3];
	int r = Code2CJZid(code, CJZid);
	if(r== -1){
		return -1;
	}
	if(r == 2 ){
		type =toJungType[CJZid[1]];
		
	}else{ // r == 3
		type =toJungType[CJZid[1]]+3;
	}
	return type;
}
/*
________________________________________________________________
  
Function Name	: GetJungType
	
Purpose			: 涝仿等 吝己Id肺何磐 吝己辆幅(急 吝己/穿款 吝己/般吝己)甫 舅酒郴绰巴
		
Input			:
	Jid			: 吝己Id

Output			: No	

Return Value	: 吝己辆幅(0:急 吝己,1:穿款 吝己,2:般吝己)

Description		: No

Ref Journal		: No
	
date			: 2009/06/5
________________________________________________________________
*/
int CCode::GetJungType(int Jid)
{
	if(Jid == -1) return -1;
	int type =toJungType[Jid];
	return type;
}
/*
________________________________________________________________
  
Function Name	: GetKoreanType
	
Purpose			: 涝仿等 檬吝辆己 锅龋肺何磐 硅摹痹蘑阑 舅酒郴绰巴
		
Input			:
	CJZid		: 檬吝辆己锅龋

Output			: No	

Return Value	: 硅摹痹蘑锅龋

Description		: 

Ref Journal		: "Bayesian Network Modeling of Hangul Characters for On-line Handwriting Recognition"  
					Sung-Jung Cho*, Jin H. Kim   ICDAR (2003), pp. 207-211 vol.1
	
date			: 2009/06/5
________________________________________________________________
*/
int CCode::GetKoreanType(int CJZid[3])
{
	int type;
	if(CJZid[0]== -1 || CJZid[1] == -1){
		return -1;
	}
	if(CJZid[2]== -1 ){
		type =toJungType[CJZid[1]];
		
	}else{ // r == 3
		type =toJungType[CJZid[1]]+3;
	}
	return type;
}
/*
________________________________________________________________
  
Function Name	: GetCJZNum
	
Purpose			: 涝仿等 檬吝辆Id肺何磐 磊葛俺荐 掘扁
		
Input			:
	CJZid		: 檬吝辆己锅龋

Output			: No	

Return Value	: 磊葛俺荐

Description		: 父老 '啊'扼搁 辆己Id绰 -1,

Ref Journal		: No
	
			  
date			: 2009/06/5
________________________________________________________________
*/
int CCode::GetCJZNum(int CJZid[3])
{
	int num = 0;
	if(CJZid[0]== -1 || CJZid[1] == -1){
		return 1;
	}
	if(CJZid[2]== -1 ){
		num = 2;
		
	}else{ // r == 3
		num = 3;
	}
	return num;
}

// convert KSC5601 to C,J,Z
/*
________________________________________________________________
  
Function Name	: Code2CJZid
	
Purpose			: 涝仿等 巩磊内靛肺何磐 檬吝辆Id甫 掘扁
		
Input			:
	code		: 巩磊内靛

Output			: 
	CJZid		: 掘绢柳 檬吝辆Id	

Return Value	: 磊葛俺荐

Description		: 父老 '啊'扼搁 辆己Id绰 -1,

Ref Journal		: No
	
date			: 2009/06/5
________________________________________________________________
*/
int CCode::Code2CJZid(WORD code, int CJZid[3])
{
	int len = 0;
	unsigned int msb, lsb;
	WORD codekssm;
	lsb = HIBYTE(code); msb = LOBYTE(code);
	codekssm = toKSSM[msb-0xb0][lsb-0xa1];
	CJZid[0] = toCId[CHO_SUNG(codekssm)];
	CJZid[1] = toJId[JUNG_SUNG(codekssm)];
	CJZid[2] = toZId[ZONG_SUNG(codekssm)];
	if(CJZid[0]== -1 || CJZid[1] == -1){
		return -1;
	}
	if(CJZid[2]== -1 ){
		return 2;
	}
	return 3;
}
// convert C,J,Z to KSC5601
/*
________________________________________________________________
  
Function Name	: CJZid2Code
	
Purpose			: 涝仿等 檬吝辆Id肺何磐 巩磊内靛甫 掘扁
		
Input			:
	CJZid		: 檬吝辆Id		

Output			: 
	code		: 巩磊内靛

Return Value	: 积己等 巩磊内靛啊 2350磊吝俊 乐栏搁 TRUE,绝栏搁 FALSE

Description		: 

Ref Journal		: No
	
date			: 2009/06/5
________________________________________________________________
*/
BOOL CCode::CJZid2Code(int CJZid[3],WORD& code )
{
	int len = 0;
	WORD codekssm=0x0001;
	codekssm = codekssm<<5;
	code = NONCODE;
	if(CJZid[0]== -1 || CJZid[1] == -1){
		return FALSE;
	}
	codekssm += toCkssm[CJZid[0]];
	codekssm = codekssm<<5;
	codekssm += toJkssm[CJZid[1]];
	codekssm = codekssm<<5;
	if(CJZid[2] == -1)
		codekssm += 0x01;
	else
		codekssm += toZkssm[CJZid[2]];

	int index = codekssm-0x8861;
	if(index>19286 ||index<0  )return FALSE;
	code = toKSC5601[index];
	if(code == NONCODE) return FALSE;
	unsigned int msb, lsb;
	lsb = LOBYTE(code); msb = HIBYTE(code);
	code = MAKEWORD(msb,lsb);
	return TRUE;
}

/*
________________________________________________________________
  
Function Name	: CJZid2CJZCode
	
Purpose			: 涝仿等 檬吝辆Id肺何磐 弊俊 措览茄 檬吝辆己 内靛甫 掘扁
		
Input			:
	CJZid		: 檬吝辆Id		

Output			: 
	CJZcd		: 檬吝辆内靛

Return Value	: No

Description		: 

Ref Journal		: No
	
date			: 2009/06/5
________________________________________________________________
*/
void CCode::CJZid2CJZCode(int CJZid[3],WORD CJZcd[3] )
{
	if(CJZid[0]!=-1)
		CJZcd[0] = toChok5601[CJZid[0]];
	else
		CJZcd[0] = SPACETWO;
	if(CJZid[1]!=-1)
		CJZcd[1] = toJungk5601[CJZid[1]];
	else
		CJZcd[1] = SPACETWO;
	if(CJZid[2]!=-1)
		CJZcd[2] = toZongk5601[CJZid[2]];
	else
		CJZcd[2] = SPACETWO;
	return ;
}
/*
________________________________________________________________
  
Function Name	: Cid2CCode
	
Purpose			: 涝仿等 檬己Id肺何磐 弊俊 措览茄 檬己 内靛甫 掘扁
		
Input			:
	Cid			: 檬己Id		

Output			: No	

Return Value	: 檬己内靛

Description		: 

Ref Journal		: No
	
date			: 2009/06/5
________________________________________________________________
*/
WORD CCode::Cid2CCode(int Cid )
{
	WORD CCd;
	if(Cid>= 0 && Cid < CHONum)
		CCd = toChok5601[Cid];
	else
		CCd = SPACETWO;
	return CCd;
}
/*
________________________________________________________________
  
Function Name	: Jid2JCode
	
Purpose			: 涝仿等 吝己Id肺何磐 弊俊 措览茄 吝己 内靛甫 掘扁
		
Input			:
	Jid			: 吝己Id		

Output			: No	

Return Value	: 吝己内靛

Description		: 

Ref Journal		: No
	
date			: 2009/06/5
________________________________________________________________
*/
WORD CCode::Jid2JCode(int Jid )
{
	WORD JCd;
	if(Jid>= 0 && Jid < JUNGNum)
		JCd = toJungk5601[Jid];
	else
		JCd = SPACETWO;
	return JCd;
}
/*
________________________________________________________________
  
Function Name	: Zid2ZCode
	
Purpose			: 涝仿等 辆己Id肺何磐 弊俊 措览茄 辆己 内靛甫 掘扁
		
Input			:
	Zid			: 辆己Id		

Output			: No	

Return Value	: 辆己内靛

Description		: 

Ref Journal		: No
	
date			: 2009/06/5
________________________________________________________________
*/
WORD CCode::Zid2ZCode(int Zid )
{
	WORD ZCd;
	if(Zid>= 0 && Zid < ZONGNum)
		ZCd = toZongk5601[Zid];
	else
		ZCd = SPACETWO;
	return ZCd;
}
/*
________________________________________________________________
  
Function Name	: Sid2SCode
	
Purpose			: 涝仿等 康绢 肚绰 箭磊Id肺何磐 弊俊 措览茄 内靛甫 掘扁
		
Input			:
	SymId		: 康绢 肚绰 箭磊Id		

Output			: No	

Return Value	: 康绢 肚绰 箭磊内靛

Description		: 

Ref Journal		: No
	
date			: 2009/06/5
________________________________________________________________
*/
WORD CCode::Sid2SCode(int SymId )
{
	static char TwoCode[]="０１２３４５６７８９ＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ";
	WORD SCd;
	if(SymId>= 0 && SymId < SymbolNum)
		SCd = MAKEWORD(TwoCode[SymId*2],TwoCode[SymId*2+1]);
	else
		SCd = SPACETWO;
	return SCd;
}

/*
________________________________________________________________
  
Function Name	: MakeKoreanCodeTable
	
Purpose			: 2350磊辆俊 措览茄 内靛钎积己
		
Input			: No		

Output			: No	

Return Value	: 2350磊辆俊 措览茄 内靛钎

Description		: 

Ref Journal		: No
	
date			: 2009/06/5
________________________________________________________________
*/
WORD* CCode::MakeKoreanCodeTable()
{
	WORD * code = (WORD*)malloc(KNum*sizeof(WORD));
	int i=0;
	for (char msb = (char)0xb0; msb <= (char) 0xc8; msb++){
		for (char lsb = (char)0xa1; lsb <= (char) 0xfe; lsb++){
			code[i++] = MAKEWORD(msb,lsb);
		}
	}

	return code;
}

/*
________________________________________________________________
  
Function Name	: Half2TwoCode
	
Purpose			: 馆阿栏肺 等 康绢 肚绰 箭磊内靛甫 罐酒辑  傈阿内靛肺 函券
		
Input			: 
	b			: 馆阿内靛	

Output			: 
	w			: 函券等 傈阿内靛

Return Value	: 函券己傍捞搁 TRUE,角菩捞搁 FALSE

Description		: 

Ref Journal		: No
	
date			: 2009/06/5
________________________________________________________________
*/
BOOL CCode::Half2TwoCode(char b,WORD& w)
{
	static char HalfCode[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	static char TwoCode[]="０１２３４５６７８９ＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ";
	int i=0;
	BOOL flag = FALSE;
	for (i = 0; i < SymbolNum; i++){
		if(HalfCode[i] == b){
			flag = TRUE;
			w = MAKEWORD(TwoCode[i*2],TwoCode[i*2+1]);
			break;
		}
	}
	return flag;
}

