00001 // $Id: unicdman.cpp 788 2006-04-07 10:05:16Z luke $ 00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE 00003 ================================XARAHEADERSTART=========================== 00004 00005 Xara LX, a vector drawing and manipulation program. 00006 Copyright (C) 1993-2006 Xara Group Ltd. 00007 Copyright on certain contributions may be held in joint with their 00008 respective authors. See AUTHORS file for details. 00009 00010 LICENSE TO USE AND MODIFY SOFTWARE 00011 ---------------------------------- 00012 00013 This file is part of Xara LX. 00014 00015 Xara LX is free software; you can redistribute it and/or modify it 00016 under the terms of the GNU General Public License version 2 as published 00017 by the Free Software Foundation. 00018 00019 Xara LX and its component source files are distributed in the hope 00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the 00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00022 See the GNU General Public License for more details. 00023 00024 You should have received a copy of the GNU General Public License along 00025 with Xara LX (see the file GPL in the root directory of the 00026 distribution); if not, write to the Free Software Foundation, Inc., 51 00027 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00028 00029 00030 ADDITIONAL RIGHTS 00031 ----------------- 00032 00033 Conditional upon your continuing compliance with the GNU General Public 00034 License described above, Xara Group Ltd grants to you certain additional 00035 rights. 00036 00037 The additional rights are to use, modify, and distribute the software 00038 together with the wxWidgets library, the wxXtra library, and the "CDraw" 00039 library and any other such library that any version of Xara LX relased 00040 by Xara Group Ltd requires in order to compile and execute, including 00041 the static linking of that library to XaraLX. In the case of the 00042 "CDraw" library, you may satisfy obligation under the GNU General Public 00043 License to provide source code by providing a binary copy of the library 00044 concerned and a copy of the license accompanying it. 00045 00046 Nothing in this section restricts any of the rights you have under 00047 the GNU General Public License. 00048 00049 00050 SCOPE OF LICENSE 00051 ---------------- 00052 00053 This license applies to this program (XaraLX) and its constituent source 00054 files only, and does not necessarily apply to other Xara products which may 00055 in part share the same code base, and are subject to their own licensing 00056 terms. 00057 00058 This license does not apply to files in the wxXtra directory, which 00059 are built into a separate library, and are subject to the wxWindows 00060 license contained within that directory in the file "WXXTRA-LICENSE". 00061 00062 This license does not apply to the binary libraries (if any) within 00063 the "libs" directory, which are subject to a separate license contained 00064 within that directory in the file "LIBS-LICENSE". 00065 00066 00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS 00068 ---------------------------------------------- 00069 00070 Subject to the terms of the GNU Public License (see above), you are 00071 free to do whatever you like with your modifications. However, you may 00072 (at your option) wish contribute them to Xara's source tree. You can 00073 find details of how to do this at: 00074 http://www.xaraxtreme.org/developers/ 00075 00076 Prior to contributing your modifications, you will need to complete our 00077 contributor agreement. This can be found at: 00078 http://www.xaraxtreme.org/developers/contribute/ 00079 00080 Please note that Xara will not accept modifications which modify any of 00081 the text between the start and end of this header (marked 00082 XARAHEADERSTART and XARAHEADEREND). 00083 00084 00085 MARKS 00086 ----- 00087 00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara 00089 designs are registered or unregistered trademarks, design-marks, and/or 00090 service marks of Xara Group Ltd. All rights in these marks are reserved. 00091 00092 00093 Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK. 00094 http://www.xara.com/ 00095 00096 =================================XARAHEADEREND============================ 00097 */ 00098 // 00099 00100 #include "camtypes.h" 00101 #include "unicdman.h" 00102 00103 #include "camelot.h" 00104 00105 // We want better memory tracking 00106 // Declare smart memory handling in Debug builds 00107 #define new CAM_DEBUG_NEW 00108 00109 00110 /******************************************************************************************** 00111 > static inline BOOL UnicodeManager::IsDBCSLeadByte(BYTE TestChar) 00112 00113 Author: Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com> 00114 Created: 26/7/96 00115 Inputs: TestChar - the character to test 00116 Outputs: - 00117 Returns: TRUE if the character is a DBCS lead character, FALSE if not 00118 Purpose: Platform independent way of testing for lead bytes. 00119 Note: It's inline, in the header 00120 ********************************************************************************************/ 00121 00122 /******************************************************************************************** 00123 > static inline UINT32 UnicodeManager::ComposeMultiBytes(BYTE LeadChar, BYTE TrailChar) 00124 00125 Author: Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com> 00126 Created: 25/7/96 00127 Inputs: LeadChar - Lead multichar byte 00128 TrailChar - Trail multichar byte 00129 Outputs: - 00130 Returns: A composed UINT32 reperesenting the multibyte character 00131 Purpose: One standard (correct!) way for composing the lead and trail bytes 00132 Note: It's inline, in the header 00133 ********************************************************************************************/ 00134 00135 /******************************************************************************************** 00136 > static void UnicodeManager::DecomposeMultiBytes(UINT32 MBChar, BYTE* LeadChar, BYTE* TrailChar); 00137 00138 Author: Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com> 00139 Created: 25/7/96 00140 Inputs: MBChar - the composed multibyte character 00141 Outputs: LeadChar - Lead multichar byte 00142 TrailChar - Trail multichar byte 00143 Returns: - 00144 Purpose: One standard (correct!) way for decomposing the lead and trail bytes 00145 ********************************************************************************************/ 00146 void UnicodeManager::DecomposeMultiBytes(UINT32 MBChar, BYTE* LeadChar, BYTE* TrailChar) 00147 { 00148 ERROR3IF(LeadChar==NULL || TrailChar==NULL, "Output pointers were NULL"); 00149 00150 if (LeadChar!=NULL || TrailChar!=NULL) 00151 { 00152 *LeadChar = (MBChar >> 8) & 0xFF; 00153 *TrailChar = MBChar & 0xFF; 00154 } 00155 } 00156 00157 00158 00159 /******************************************************************************************** 00160 > static UINT32 UnicodeManager::UnicodeToMultiByte(WCHAR UnicodeChar) 00161 00162 Author: Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com> 00163 Created: 19/10/95 00164 Inputs: UnicodeChar - a single unicode character 00165 Ouptuts: - 00166 Returns: A single multi-byte character value that represents UnicodeChar 00167 Purpose: Converts a single unicode character into it's multi-byte equivelent. 00168 BUT what about two composed characters? Oh... 00169 Errors: If the conversion fails then character 128 (box), or the system defualt is 00170 returned. 00171 ********************************************************************************************/ 00172 UINT32 UnicodeManager::UnicodeToMultiByte(WCHAR UnicodeChar) 00173 { 00174 #if defined(__WXMSW__) 00175 if (UnicodeChar < 128) 00176 return UnicodeChar; 00177 00178 CHAR Return[20]; 00179 LPSTR lpReturn = Return; 00180 INT32 sizeReturn = sizeof lpReturn; 00181 00182 // Get the active code page. 00183 // This is for Win95/98 systems which do not support unicode. 00184 UINT32 nCodePage = GetACP(); 00185 ERROR3IF( (!IsValidCodePage(nCodePage)), "UnicodeManager::UnicodeToMultiByte - not a valid codepage" ); 00186 00187 // COnvert from Unicode 00188 if ( ::WideCharToMultiByte(nCodePage, WC_COMPOSITECHECK | WC_DEFAULTCHAR, &UnicodeChar, 1, 00189 lpReturn, sizeReturn, NULL, NULL) ) 00190 { 00191 if (IsDBCSLeadByte(Return[0])) 00192 return ((unsigned char)Return[0] << 8) | (unsigned char)Return[1]; 00193 else 00194 return (unsigned char)Return[0]; 00195 } 00196 else 00197 { 00198 ERROR3("MultiByteToWideChar failed"); 00199 return 128; 00200 } 00201 #else 00202 char psz[MB_CUR_MAX]; 00203 switch( wctomb( psz, UnicodeChar ) ) 00204 { 00205 case 1: 00206 return (unsigned char)psz[0]; 00207 00208 case 2: 00209 return ( (unsigned char)psz[0] << 8 ) | (unsigned char)psz[1]; 00210 00211 case 4: 00212 printf( "UniCh = %08x\n", ( (unsigned char)psz[0] << 24 ) | 00213 ( (unsigned char)psz[1] << 16 ) | ( (unsigned char)psz[2] << 8 ) | 00214 (unsigned char)psz[3] ); 00215 return( (unsigned char)psz[0] << 24 ) | ( (unsigned char)psz[1] << 16 ) | 00216 ( (unsigned char)psz[2] << 8 ) | (unsigned char)psz[3]; 00217 00218 default: 00219 ERROR3("MultiByteToWideChar failed"); 00220 return 128; 00221 } 00222 #endif 00223 } 00224 00225 00226 /******************************************************************************************** 00227 > static WCHAR UnicodeManager::MultiByteToUnicode(UINT32 MBChar) 00228 00229 Author: Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com> 00230 Created: 19/10/95 00231 Inputs: MBChar - a single multi-byte character 00232 Ouptuts: - 00233 Returns: The unicode value that represents MBChar 00234 Purpose: Converts a single multi-byte character into it's unicode equivelent. 00235 BUT what about two composed characters? Oh... Unimplemented as no one will 00236 cope with two chars being returned... 00237 Errors: If the conversion fails then character 128 (box) is returned. 00238 ********************************************************************************************/ 00239 WCHAR UnicodeManager::MultiByteToUnicode( UINT32 MBChar ) 00240 { 00241 CHAR MBArray[3]; 00242 WCHAR ReturnArray[4]; 00243 size_t cch; 00244 00245 // Convert UINT32 to DBCS 00246 if( IsDBCSLeadByte( ( MBChar >> 8 ) & 0xFF ) ) 00247 { 00248 MBArray[0] = (MBChar>>8) & 0xFF; 00249 MBArray[1] = MBChar & 0xFF; 00250 MBArray[2] = 0; 00251 cch = 2; 00252 } 00253 else 00254 { 00255 MBArray[0] = MBChar & 0xFF; 00256 MBArray[1] = 0; 00257 cch = 1; 00258 } 00259 00260 #if defined(__WXMSW__) 00261 // Get the active code page. 00262 // This is for Win95/98 systems which do not support unicode. 00263 UINT32 nCodePage = GetACP(); 00264 ERROR3IF( (!IsValidCodePage(nCodePage)), "UnicodeManager::MultiByteToUnicode - not a valid codepage" ); 00265 00266 // Convert to Unicode 00267 if ( ::MultiByteToWideChar(nCodePage, MB_PRECOMPOSED, MBArray, -1, ReturnArray, 4) ) 00268 { 00269 return ReturnArray[0]; // Composed chars then? 00270 } 00271 else 00272 { 00273 ERROR3("MultiByteToWideChar failed"); 00274 /*DWORD dwError = GetLastError(); 00275 //ERROR3_PF ("Error=%lx", dwError); 00276 char sError[20]; 00277 sprintf (sError, "Error=%lx", dwError); 00278 ERROR3 (sError);*/ 00279 return 128; 00280 } 00281 #else 00282 mbstate_t state; 00283 memset(&state, 0, sizeof(mbstate_t)); 00284 mbrtowc( ReturnArray, MBArray, cch, &state ); 00285 return ReturnArray[0]; 00286 #endif 00287 } 00288 00289 00290 00291 /******************************************************************************************** 00292 > BOOL UnicodeManager::IsUnicodeCompleteOS() 00293 00294 Author: Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com> 00295 Created: 19/10/95 00296 Returns: TRUE if running on a Unicode Complete OS 00297 ********************************************************************************************/ 00298 BOOL UnicodeManager::IsUnicodeCompleteOS() 00299 { 00300 #if defined(__WXMSW__) 00301 return IsWin32NT(); 00302 #elif defined(__WXGTK__) 00303 return true; 00304 #endif 00305 } 00306 00307 00308 00309 /******************************************************************************************** 00310 > BOOL UnicodeManager::IsDBCSOS() 00311 00312 Author: Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com> 00313 Created: 27/6/99 00314 Returns: TRUE if running on a DBCS enabled OS 00315 ********************************************************************************************/ 00316 BOOL UnicodeManager::IsDBCSOS() 00317 { 00318 #if defined(__WXMSW__) 00319 return ::GetSystemMetrics(SM_DBCSENABLED); 00320 #elif defined(__WXGTK__) 00321 return true; 00322 #endif 00323 } 00324 00325 00326 00327 /******************************************************************************************** 00328 > static UINT32 UnicodeManager::GetFontCharSet() 00329 00330 Author: Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com> 00331 Created: 31/7/96 00332 Inputs: - 00333 Ouptuts: - 00334 Returns: UINT32 representing the font character set of the system 00335 Purpose: Reads what value the system is using for the character sets of its fonts. 00336 ********************************************************************************************/ 00337 UINT32 UnicodeManager::GetFontCharSet() 00338 { 00339 if( IsDBCSOS() ) 00340 return SHIFTJIS_CHARSET; 00341 else 00342 return ANSI_CHARSET; 00343 00344 // UINT32 CharSet = 0; 00345 // 00346 // // This appears to be the only way of doing this. It's ugly as we have to create a DC. 00347 // if (!IsWindows31()) 00348 // { 00349 // CWnd* pWnd = CWnd::GetDesktopWindow(); 00350 // if (pWnd!=NULL && pWnd->GetDC()!=NULL) 00351 // CharSet = ::GetTextCharset(pWnd->GetDC()->GetSafeHdc()); 00352 // } 00353 // 00354 // return CharSet; 00355 }