Back to index

lightning-sunbird  0.9+nobinonly
nsHankakuToZenkaku.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsITextTransform.h"
00039 #include "pratom.h"
00040 #include "nsUUDll.h"
00041 #include "nsTextTransformFactory.h"
00042 
00043 // Basic mapping from Hankaku to Zenkaku
00044 // Nigori and Maru is take care out side this basic mapping
00045 static const PRUnichar gBasicMapping[0x40] =
00046 {
00047 // 0xff60
00048 0xff60,0x3002,0x300c,0x300d,0x3001,0x30fb,0x30f2,0x30a1,        
00049 // 0xff68
00050 0x30a3,0x30a5,0x30a7,0x30a9,0x30e3,0x30e5,0x30e7,0x30c3,        
00051 // 0xff70
00052 0x30fc,0x30a2,0x30a4,0x30a6,0x30a8,0x30aa,0x30ab,0x30ad,        
00053 // 0xff78
00054 0x30af,0x30b1,0x30b3,0x30b5,0x30b7,0x30b9,0x30bb,0x30bd,        
00055 // 0xff80
00056 0x30bf,0x30c1,0x30c4,0x30c6,0x30c8,0x30ca,0x30cb,0x30cc,        
00057 // 0xff88
00058 0x30cd,0x30ce,0x30cf,0x30d2,0x30d5,0x30d8,0x30db,0x30de,        
00059 // 0xff90
00060 0x30df,0x30e0,0x30e1,0x30e2,0x30e4,0x30e6,0x30e8,0x30e9,        
00061 // 0xff98
00062 0x30ea,0x30eb,0x30ec,0x30ed,0x30ef,0x30f3,0x309b,0x309c
00063 };
00064 
00065 // Do we need to check for Nigori for the next unicode ?
00066 #define NEED_TO_CHECK_NIGORI(u) (((0xff76<=(u))&&((u)<=0xff84))||((0xff8a<=(u))&&((u)<=0xff8e)))
00067 
00068 // Do we need to check for Maru for the next unicode ?
00069 #define NEED_TO_CHECK_MARU(u) ((0xff8a<=(u))&&((u)<=0xff8e))
00070 
00071 // The  unicode is in Katakana Hankaku block
00072 #define IS_HANKAKU(u) (0xff60==((u) & 0xffe0)) || (0xff80==((u)&0xffe0))
00073 #define IS_NIGORI(u) (0xff9e == (u))
00074 #define IS_MARU(u)   (0xff9f == (u))
00075 #define NIGORI_MODIFIER 1
00076 #define MARU_MODIFIER   2
00077  
00078 // function prototype 
00079 void HankakuToZenkaku (
00080     const PRUnichar* aSrc, PRInt32 aLen, 
00081     PRUnichar* aDest, PRInt32 aDestLen, PRInt32* oLen);
00082 
00083 void HankakuToZenkaku (
00084     const PRUnichar* aSrc, PRInt32 aLen, 
00085     PRUnichar* aDest, PRInt32 aDestLen, PRInt32* oLen)
00086 {
00087     // XXX aDestLen is never checked, assumed to be as long as aLen
00088     NS_ASSERTION(aDestLen >= aLen, "aDest must be as long as aSrc");
00089 
00090     PRInt32 i,j;
00091     if ( aLen == 0) {
00092       *oLen = 0;
00093       return;
00094     }
00095     // loop from the first to the last char except the last one.
00096     for(i = j = 0; i < (aLen-1); i++,j++,aSrc++, aDest++)
00097     {
00098        if(IS_HANKAKU(*aSrc)) {
00099          // if it is in hankaku - do basic mapping first
00100          *aDest = gBasicMapping[(*aSrc) - 0xff60];
00101          
00102          // if is some char could be modifier, and the next char
00103          // is a modifier, modify it and eat one byte
00104 
00105          if(IS_NIGORI(*(aSrc+1)) && NEED_TO_CHECK_NIGORI(*aSrc))
00106          {
00107             *aDest += NIGORI_MODIFIER;
00108             i++; aSrc++;
00109          }
00110          else if(IS_MARU(*(aSrc+1)) && NEED_TO_CHECK_MARU(*aSrc)) 
00111          {
00112             *aDest += MARU_MODIFIER;
00113             i++; aSrc++;
00114          }
00115        }
00116        else 
00117        {
00118          // not in hankaku block, just copy 
00119          *aDest = *aSrc;
00120        }
00121     }
00122 
00123     // handle the last character
00124     if(IS_HANKAKU(*aSrc)) 
00125          *aDest = gBasicMapping[(*aSrc) - 0xff60];
00126     else
00127          *aDest = *aSrc;
00128 
00129     *oLen = j+1;
00130 }
00131 
00132 
00133 
00134 class nsHankakuToZenkaku : public nsITextTransform {
00135   NS_DECL_ISUPPORTS
00136 
00137 public: 
00138 
00139   nsHankakuToZenkaku() ;
00140   virtual ~nsHankakuToZenkaku() ;
00141   NS_IMETHOD Change( const PRUnichar* aText, PRInt32 aTextLength, nsString& aResult);
00142   NS_IMETHOD Change( nsString& aText, nsString& aResult);
00143 
00144 };
00145 
00146 NS_IMPL_ISUPPORTS1(nsHankakuToZenkaku, nsITextTransform)
00147 
00148 nsHankakuToZenkaku::nsHankakuToZenkaku()
00149 {
00150 }
00151 nsHankakuToZenkaku::~nsHankakuToZenkaku()
00152 {
00153 }
00154 
00155 NS_IMETHODIMP nsHankakuToZenkaku::Change( const PRUnichar* aText, PRInt32 aTextLength, nsString& aResult)
00156 {
00157   PRInt32 ol;
00158   if (!EnsureStringLength(aResult, aTextLength))
00159     return NS_ERROR_OUT_OF_MEMORY;
00160 
00161   HankakuToZenkaku ( aText, aTextLength, aResult.BeginWriting(), aTextLength, &ol);
00162   aResult.SetLength(ol);
00163 
00164   return NS_OK;
00165 }
00166 
00167 NS_IMETHODIMP nsHankakuToZenkaku::Change( nsString& aText, nsString& aResult)
00168 {
00169    aResult = aText;
00170    const PRUnichar* u = aResult.get();
00171    PRUnichar* ou = (PRUnichar*) u;
00172    PRInt32 l = aResult.Length();
00173    PRInt32 ol;
00174    
00175    HankakuToZenkaku ( u, l, ou, l, &ol);
00176    aResult.SetLength(ol);
00177 
00178    return NS_OK;
00179 }
00180 
00181 nsresult NS_NewHankakuToZenkaku(nsISupports** oResult)
00182 {
00183   if(!oResult)
00184     return NS_ERROR_NULL_POINTER;
00185   *oResult = new nsHankakuToZenkaku();
00186   if(*oResult)
00187      NS_ADDREF(*oResult);
00188   return (*oResult) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00189 }