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) 1999
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 #include "nsITextTransform.h"
41 #include "nsTextTransformFactory.h"
43 // Basic mapping from Hankaku to Zenkaku
44 // Nigori and Maru is take care out side this basic mapping
45 static const PRUnichar gBasicMapping
[0x40] =
48 0xff60,0x3002,0x300c,0x300d,0x3001,0x30fb,0x30f2,0x30a1,
50 0x30a3,0x30a5,0x30a7,0x30a9,0x30e3,0x30e5,0x30e7,0x30c3,
52 0x30fc,0x30a2,0x30a4,0x30a6,0x30a8,0x30aa,0x30ab,0x30ad,
54 0x30af,0x30b1,0x30b3,0x30b5,0x30b7,0x30b9,0x30bb,0x30bd,
56 0x30bf,0x30c1,0x30c4,0x30c6,0x30c8,0x30ca,0x30cb,0x30cc,
58 0x30cd,0x30ce,0x30cf,0x30d2,0x30d5,0x30d8,0x30db,0x30de,
60 0x30df,0x30e0,0x30e1,0x30e2,0x30e4,0x30e6,0x30e8,0x30e9,
62 0x30ea,0x30eb,0x30ec,0x30ed,0x30ef,0x30f3,0x309b,0x309c
65 // Do we need to check for Nigori for the next unicode ?
66 #define NEED_TO_CHECK_NIGORI(u) (((0xff76<=(u))&&((u)<=0xff84))||((0xff8a<=(u))&&((u)<=0xff8e)))
68 // Do we need to check for Maru for the next unicode ?
69 #define NEED_TO_CHECK_MARU(u) ((0xff8a<=(u))&&((u)<=0xff8e))
71 // The unicode is in Katakana Hankaku block
72 #define IS_HANKAKU(u) (0xff60==((u) & 0xffe0)) || (0xff80==((u)&0xffe0))
73 #define IS_NIGORI(u) (0xff9e == (u))
74 #define IS_MARU(u) (0xff9f == (u))
75 #define NIGORI_MODIFIER 1
76 #define MARU_MODIFIER 2
79 void HankakuToZenkaku (
80 const PRUnichar
* aSrc
, PRInt32 aLen
,
81 PRUnichar
* aDest
, PRInt32 aDestLen
, PRInt32
* oLen
);
83 void HankakuToZenkaku (
84 const PRUnichar
* aSrc
, PRInt32 aLen
,
85 PRUnichar
* aDest
, PRInt32 aDestLen
, PRInt32
* oLen
)
87 // XXX aDestLen is never checked, assumed to be as long as aLen
88 NS_ASSERTION(aDestLen
>= aLen
, "aDest must be as long as aSrc");
95 // loop from the first to the last char except the last one.
96 for(i
= j
= 0; i
< (aLen
-1); i
++,j
++,aSrc
++, aDest
++)
98 if(IS_HANKAKU(*aSrc
)) {
99 // if it is in hankaku - do basic mapping first
100 *aDest
= gBasicMapping
[(*aSrc
) - 0xff60];
102 // if is some char could be modifier, and the next char
103 // is a modifier, modify it and eat one byte
105 if(IS_NIGORI(*(aSrc
+1)) && NEED_TO_CHECK_NIGORI(*aSrc
))
107 *aDest
+= NIGORI_MODIFIER
;
110 else if(IS_MARU(*(aSrc
+1)) && NEED_TO_CHECK_MARU(*aSrc
))
112 *aDest
+= MARU_MODIFIER
;
118 // not in hankaku block, just copy
123 // handle the last character
124 if(IS_HANKAKU(*aSrc
))
125 *aDest
= gBasicMapping
[(*aSrc
) - 0xff60];
134 class nsHankakuToZenkaku
: public nsITextTransform
{
139 nsHankakuToZenkaku() ;
140 virtual ~nsHankakuToZenkaku() ;
141 NS_IMETHOD
Change( const PRUnichar
* aText
, PRInt32 aTextLength
, nsString
& aResult
);
142 NS_IMETHOD
Change( nsString
& aText
, nsString
& aResult
);
146 NS_IMPL_ISUPPORTS1(nsHankakuToZenkaku
, nsITextTransform
)
148 nsHankakuToZenkaku::nsHankakuToZenkaku()
151 nsHankakuToZenkaku::~nsHankakuToZenkaku()
155 NS_IMETHODIMP
nsHankakuToZenkaku::Change( const PRUnichar
* aText
, PRInt32 aTextLength
, nsString
& aResult
)
158 if (!EnsureStringLength(aResult
, aTextLength
))
159 return NS_ERROR_OUT_OF_MEMORY
;
161 HankakuToZenkaku ( aText
, aTextLength
, aResult
.BeginWriting(), aTextLength
, &ol
);
162 aResult
.SetLength(ol
);
167 NS_IMETHODIMP
nsHankakuToZenkaku::Change( nsString
& aText
, nsString
& aResult
)
170 const PRUnichar
* u
= aResult
.get();
171 PRUnichar
* ou
= (PRUnichar
*) u
;
172 PRInt32 l
= aResult
.Length();
175 HankakuToZenkaku ( u
, l
, ou
, l
, &ol
);
176 aResult
.SetLength(ol
);
181 nsresult
NS_NewHankakuToZenkaku(nsISupports
** oResult
)
184 return NS_ERROR_NULL_POINTER
;
185 *oResult
= new nsHankakuToZenkaku();
188 return (*oResult
) ? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;