#include "stdafx.h" #ifdef _COUNTRY_ME_ #include "KTextParagraph.h" #include "KTextParser.h" #include "SGameOperationManager.h" #include "KTextLayout.h" #define MAX_STRING_SIZE 2048 DWORD KTextParagraph::GetAttrHash() { KTextAttrBase* pAttr; std::string strTemp; DWORD dwHash = 0; for ( ilistAttr itor = m_listAttr.begin(); itor != m_listAttr.end(); ++itor ) { pAttr = *itor; switch ( pAttr->type ) { case KTextAttrBase::TA_FONT : strTemp = pAttr->GetAttr (); for ( int i = 0; i < strTemp.size(); ++i ) dwHash = dwHash * 32 - dwHash + strTemp[i]; dwHash = dwHash * 32 - dwHash + pAttr->nIndex; break; case KTextAttrBase::TA_SIZE : dwHash = dwHash * 32 - dwHash + pAttr->GetAttr (); dwHash = dwHash * 32 - dwHash + pAttr->nIndex; break; case KTextAttrBase::TA_FCOLOR : case KTextAttrBase::TA_BCOLOR : dwHash = dwHash * 32 - dwHash + pAttr->GetAttr (); dwHash = dwHash * 32 - dwHash + pAttr->nIndex; break; case KTextAttrBase::TA_BOLD : case KTextAttrBase::TA_INVERSE : case KTextAttrBase::TA_STRIKE : case KTextAttrBase::TA_UNDERLINE : dwHash = dwHash * 32 - dwHash + pAttr->GetAttr () + pAttr->type; dwHash = dwHash * 32 - dwHash + pAttr->nIndex; break; } } return dwHash; } PangoAttrList* KTextParagraph::GetPangoAttrList() { PangoAttrList* pList = pango_attr_list_new(); KTextAttrBase* pAttr; PangoAttribute* pPangoAttr; DWORD dwForeColor = 0xFFFFFF; DWORD dwBackColor = 0x000000; DWORD r, g, b; for ( ilistAttr itor = m_listAttr.begin(); itor != m_listAttr.end(); ++itor ) { pAttr = *itor; switch ( pAttr->type ) { case KTextAttrBase::TA_FONT : pPangoAttr = pango_attr_family_new( pAttr->GetAttr ().c_str() ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; case KTextAttrBase::TA_SIZE : pPangoAttr = pango_attr_size_new( pAttr->GetAttr () * PANGO_SCALE ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; case KTextAttrBase::TA_FCOLOR : dwForeColor = pAttr->GetAttr (); r = ((dwForeColor & 0xff0000) >> 16) * 65535.0 / 255; g = ((dwForeColor & 0x00ff00) >> 8) * 65535.0 / 255; b = ((dwForeColor & 0x0000ff) >> 0) * 65535.0 / 255; pPangoAttr = pango_attr_foreground_new( r, g, b ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; case KTextAttrBase::TA_BCOLOR : dwBackColor = pAttr->GetAttr (); r = ((dwBackColor & 0xff0000) >> 16) * 65535.0 / 255; g = ((dwBackColor & 0x00ff00) >> 8) * 65535.0 / 255; b = ((dwBackColor & 0x0000ff) >> 0) * 65535.0 / 255; pPangoAttr = pango_attr_background_new( r, g, b ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; case KTextAttrBase::TA_BOLD : pPangoAttr = pango_attr_weight_new( pAttr->GetAttr () ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; case KTextAttrBase::TA_INVERSE : if ( pAttr->GetAttr () ) { pPangoAttr = pango_attr_foreground_new( 0, 0, 0 ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); r = ((dwForeColor & 0xff0000) >> 16) * 65535 / 255; g = ((dwForeColor & 0x00ff00) >> 8) * 65535 / 255; b = ((dwForeColor & 0x0000ff) >> 0) * 65535 / 255; pPangoAttr = pango_attr_background_new( r, g, b); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); } else { r = ((dwBackColor & 0xff0000) >> 16) * 65535 / 255; g = ((dwBackColor & 0x00ff00) >> 8) * 65535 / 255; b = ((dwBackColor & 0x0000ff) >> 0) * 65535 / 255; pPangoAttr = pango_attr_background_new( r, g, b ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); r = ((dwForeColor & 0xff0000) >> 16) * 65535 / 255; g = ((dwForeColor & 0x00ff00) >> 8) * 65535 / 255; b = ((dwForeColor & 0x0000ff) >> 0) * 65535 / 255; pPangoAttr = pango_attr_foreground_new( r, g, b ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); } break; case KTextAttrBase::TA_STRIKE : pPangoAttr = pango_attr_strikethrough_new( pAttr->GetAttr () ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; case KTextAttrBase::TA_UNDERLINE : pPangoAttr = pango_attr_underline_new( PANGO_UNDERLINE_SINGLE ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; } } return pList; } PangoAttrList* KTextParagraph::GetPangoAttrListWithoutColor() { PangoAttrList* pList = pango_attr_list_new(); KTextAttrBase* pAttr; PangoAttribute* pPangoAttr; DWORD dwForeColor = 0xFFFFFF; DWORD dwBackColor = 0x000000; DWORD r, g, b; for ( ilistAttr itor = m_listAttr.begin(); itor != m_listAttr.end(); ++itor ) { pAttr = *itor; switch ( pAttr->type ) { case KTextAttrBase::TA_FONT : pPangoAttr = pango_attr_family_new( pAttr->GetAttr ().c_str() ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; case KTextAttrBase::TA_SIZE : pPangoAttr = pango_attr_size_new( pAttr->GetAttr () * PANGO_SCALE ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; case KTextAttrBase::TA_BOLD : pPangoAttr = pango_attr_weight_new( pAttr->GetAttr () ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; case KTextAttrBase::TA_INVERSE : if ( pAttr->GetAttr () ) { pPangoAttr = pango_attr_foreground_new( 0, 0, 0 ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); r = ((dwForeColor & 0xff0000) >> 16) * 65535 / 255; g = ((dwForeColor & 0x00ff00) >> 8) * 65535 / 255; b = ((dwForeColor & 0x0000ff) >> 0) * 65535 / 255; pPangoAttr = pango_attr_background_new( r, g, b); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); } else { r = ((dwBackColor & 0xff0000) >> 16) * 65535 / 255; g = ((dwBackColor & 0x00ff00) >> 8) * 65535 / 255; b = ((dwBackColor & 0x0000ff) >> 0) * 65535 / 255; pPangoAttr = pango_attr_background_new( r, g, b ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); r = ((dwForeColor & 0xff0000) >> 16) * 65535 / 255; g = ((dwForeColor & 0x00ff00) >> 8) * 65535 / 255; b = ((dwForeColor & 0x0000ff) >> 0) * 65535 / 255; pPangoAttr = pango_attr_foreground_new( r, g, b ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); } break; case KTextAttrBase::TA_STRIKE : pPangoAttr = pango_attr_strikethrough_new( pAttr->GetAttr () ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; case KTextAttrBase::TA_UNDERLINE : pPangoAttr = pango_attr_underline_new( PANGO_UNDERLINE_SINGLE ); pPangoAttr->start_index = pAttr->nIndex; pango_attr_list_insert( pList, pPangoAttr ); break; } } return pList; } void KTextParagraph::AddString( const std::string& strText ) { m_strText += multibyte_to_utf8( strText ); } std::string KTextParagraph::GetMultibyteString() { return utf8_to_multibyte( m_strText ); } std::wstring multibyte_to_wide( const std::string& strMultibyte ) { wchar_t wszTemp[MAX_STRING_SIZE]; int nSize = 0; nSize = MultiByteToWideChar( g_pGameOperationManager->GetDefaultCodePage(), 0, strMultibyte.c_str(), -1, wszTemp, MAX_STRING_SIZE ); return std::wstring( wszTemp ); } std::string wide_to_multibyte( const std::wstring& strWide ) { char szTemp[MAX_STRING_SIZE]; int nSize = 0; nSize = WideCharToMultiByte( g_pGameOperationManager->GetDefaultCodePage(), 0, strWide.c_str(), -1, szTemp, MAX_STRING_SIZE, NULL, NULL ); return std::string( szTemp ); } std::string utf8_to_multibyte( const std::string& strUTF8 ) { wchar_t wszTemp[MAX_STRING_SIZE]; char szTemp[MAX_STRING_SIZE]; int nSize = 0; nSize = MultiByteToWideChar( CP_UTF8, 0, strUTF8.c_str(), -1, wszTemp, MAX_STRING_SIZE ); nSize = WideCharToMultiByte( g_pGameOperationManager->GetDefaultCodePage(), 0, wszTemp, -1, szTemp, MAX_STRING_SIZE, NULL, NULL ); return std::string( szTemp ); } std::string multibyte_to_utf8( const std::string& strMultibyte) { wchar_t wszTemp[MAX_STRING_SIZE]; char szTemp[MAX_STRING_SIZE]; int nSize = 0; nSize = MultiByteToWideChar( g_pGameOperationManager->GetDefaultCodePage(), 0, strMultibyte.c_str(), -1, wszTemp, MAX_STRING_SIZE ); nSize = WideCharToMultiByte( CP_UTF8, 0, wszTemp, -1, szTemp, MAX_STRING_SIZE, NULL, NULL ); return std::string( szTemp ); } #endif