1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 * A character set converter from Unicode to HZ.
41 * @created 08/Sept/1999
42 * @author Yueheng Xu, Yueheng.Xu@intel.com
44 * 04/Oct/1999. Yueheng Xu: Fixed line continuation problem when line
46 * Used table UnicodeToGBK[] to speed up the mapping.
48 #include "nsUnicodeToHZ.h"
49 #include "nsUCvCnDll.h"
51 //----------------------------------------------------------------------
52 // Class nsUnicodeToGBK [implementation]
54 #define HZ_STATE_ASCII 2
55 #define HZ_STATE_TILD 3
59 #define UNICODE_TILD 0x007E
60 nsUnicodeToHZ::nsUnicodeToHZ() : nsEncoderSupport(6)
62 mUtil
.InitToGBKTable();
63 mHZState
= HZ_STATE_ASCII
; // per HZ spec, default to HZ mode
65 NS_IMETHODIMP
nsUnicodeToHZ::ConvertNoBuff(
66 const PRUnichar
* aSrc
,
69 PRInt32
* aDestLength
)
72 PRInt32 iSrcLength
= *aSrcLength
;
73 PRInt32 iDestLength
= 0;
75 for (i
=0;i
< iSrcLength
;i
++)
79 // hi byte has something, it is not ASCII, process as a GB
80 if ( mHZState
!= HZ_STATE_GB
)
82 // we are adding a '~{' ESC sequence to star a HZ string
83 mHZState
= HZ_STATE_GB
;
86 aDest
+= 2; // increment 2 bytes
89 if(mUtil
.UnicodeToGBKChar(*aSrc
, PR_TRUE
, &aDest
[0], &aDest
[1])) {
90 aDest
+= 2; // increment 2 bytes
93 // some thing that we cannot convert
95 // error handling here
100 // if we are in HZ mode, end it by adding a '~}' ESC sequence
101 if ( mHZState
== HZ_STATE_GB
)
103 mHZState
= HZ_STATE_ASCII
;
106 aDest
+= 2; // increment 2 bytes
110 // if this is a regular char '~' , convert it to two '~'
111 if ( *aSrc
== UNICODE_TILD
)
115 aDest
+= 2; // increment 2 bytes
118 // other regular ASCII chars convert by normal ways
120 // Is this works for both little endian and big endian machines ?
121 *aDest
= (char) ( (PRUnichar
)(*aSrc
) );
122 aDest
++; // increment 1 byte
126 aSrc
++; // increment 2 bytes
127 if ( iDestLength
>= (*aDestLength
) )
132 *aDestLength
= iDestLength
;
137 NS_IMETHODIMP
nsUnicodeToHZ::FinishNoBuff(char * aDest
, PRInt32
* aDestLength
)
139 if ( mHZState
== HZ_STATE_GB
)
141 // if we are in HZ mode, end it by adding a '~}' ESC sequence
142 mHZState
= HZ_STATE_ASCII
;
152 NS_IMETHODIMP
nsUnicodeToHZ::FillInfo(PRUint32
*aInfo
)
154 mUtil
.FillGB2312Info(aInfo
);
155 //GB2312 font lib also have single byte ASCII characters, set them here
156 for ( PRUint16 u
= 0x0000; u
<= 0x007F; u
++)
157 SET_REPRESENTABLE(aInfo
, u
);