Back to index

lightning-sunbird  0.9+nobinonly
nsUnicodeToTIS620.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  * ucvth : nsUnicodeToTIS620.h
00003  *
00004  * ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is mozilla.org code. The Initial Developer of the Original Code is Sun Microsystems, Inc.  Portions created by SUN are Copyright (C) 2000 SUN Microsystems, Inc. All Rights Reserved.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Sun Microsystems, Inc.
00021  * Portions created by the Initial Developer are Copyright (C) 2000
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Prabhat Hegde (prabhat.hegde@sun.com)
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 #include "nsCOMPtr.h"
00041 #include "nsIServiceManager.h"
00042 #include "nsICharsetConverterManager.h"
00043 #include "nsILanguageAtomService.h"
00044 #include "nsCtlCIID.h"
00045 #include "nsILE.h"
00046 #include "nsULE.h"
00047 #include "nsUnicodeToTIS620.h"
00048 
00049 static NS_DEFINE_CID(kCharSetManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
00050 
00051 NS_IMPL_ISUPPORTS2(nsUnicodeToTIS620, nsIUnicodeEncoder, nsICharRepresentable)
00052 
00053 NS_IMETHODIMP nsUnicodeToTIS620::SetOutputErrorBehavior(PRInt32 aBehavior,
00054                                                         nsIUnicharEncoder * aEncoder, 
00055                                                         PRUnichar aChar)
00056 {
00057   return NS_ERROR_NOT_IMPLEMENTED;
00058 }
00059 
00060 nsUnicodeToTIS620::nsUnicodeToTIS620()
00061 {
00062   static   NS_DEFINE_CID(kLECID, NS_ULE_CID);
00063   nsresult rv;
00064 
00065   mCtlObj = do_CreateInstance(kLECID, &rv);
00066   if (NS_FAILED(rv)) {
00067 #ifdef DEBUG_prabhath
00068     // No other error handling needed here since we
00069     // handle absence of mCtlObj in Convert
00070     printf("ERROR: Cannot create instance of component " NS_ULE_PROGID " [%x].\n", rv);
00071 #endif
00072     NS_WARNING("Thai Text Layout Will Not Be Supported\n");
00073     mCtlObj = nsnull;
00074   }
00075 }
00076 
00077 nsUnicodeToTIS620::~nsUnicodeToTIS620()
00078 {
00079   // Maybe convert nsILE to a service
00080   // No NS_IF_RELEASE(mCtlObj) of nsCOMPtr;
00081 }
00082 
00083 /*
00084  * This method converts the unicode to this font index.
00085  * Note: ConversionBufferFullException is not handled
00086  *       since this class is only used for character display.
00087  */
00088 NS_IMETHODIMP nsUnicodeToTIS620::Convert(const PRUnichar* input,
00089                                          PRInt32*         aSrcLength,
00090                                          char*            output,
00091                                          PRInt32*         aDestLength)
00092 {
00093   PRSize outLen = 0;
00094 #ifdef DEBUG_prabhath_no_shaper
00095   printf("Debug/Test Case of No thai pango shaper Object\n");
00096   // Comment out mCtlObj == nsnull for test purposes
00097 #endif
00098 
00099   if (mCtlObj == nsnull) {
00100 #ifdef DEBUG_prabhath
00101     printf("ERROR: No CTL IMPLEMENTATION - Default Thai Conversion");
00102     // CP874 is the default converter for thai ; 
00103     // In case mCtlObj is absent (no CTL support), use it to convert.
00104 #endif
00105 
00106     nsCOMPtr<nsICharsetConverterManager> charsetMgr =
00107         do_GetService(kCharSetManagerCID);
00108     if (!charsetMgr)
00109       return NS_ERROR_FAILURE;
00110     
00111     nsCOMPtr<nsIUnicodeEncoder> encoder;
00112     charsetMgr->GetUnicodeEncoderRaw("TIS-620", getter_AddRefs(encoder));
00113     if (!encoder) {
00114       NS_WARNING("cannot get default converter for tis-620");
00115       return NS_ERROR_FAILURE;
00116     }
00117 
00118     encoder->Convert(input, aSrcLength, output, aDestLength);
00119     return NS_OK;
00120   }
00121 
00122   // CTLized shaping conversion starts here
00123   // No question of starting the conversion from an offset
00124   mCharOff = mByteOff = 0;
00125 
00126   // Charset tis620-0, tis620.2533-1, tis620.2529-1 & iso8859-11
00127   // are equivalent and have the same presentation forms
00128 
00129   // tis620-2 is hard-coded since we only generate presentation forms
00130   // in Windows-Stye as it is the current defacto-standard for the
00131   // presentation of thai content
00132   mCtlObj->GetPresentationForm(input, *aSrcLength, "tis620-2",
00133                                &output[mByteOff], &outLen);
00134 
00135   *aDestLength = outLen;
00136   return NS_OK;
00137 }
00138 
00139 NS_IMETHODIMP nsUnicodeToTIS620::Finish(char * output, PRInt32 * aDestLength)
00140 {
00141   // Finish does'nt have to do much as Convert already flushes
00142   // to output buffer
00143   mByteOff = mCharOff = 0;
00144   return NS_OK;
00145 }
00146 
00147 //================================================================
00148 NS_IMETHODIMP nsUnicodeToTIS620::Reset()
00149 {
00150   mByteOff = mCharOff = 0;
00151   return NS_OK;
00152 }
00153 
00154 //================================================================
00155 NS_IMETHODIMP nsUnicodeToTIS620::GetMaxLength(const PRUnichar * aSrc,
00156                                               PRInt32 aSrcLength,
00157                                               PRInt32 * aDestLength)
00158 {
00159   *aDestLength = (aSrcLength + 1) * 2; // Each Thai character can generate
00160                                         // atmost two presentation forms
00161   return NS_OK;
00162 }
00163 
00164 //================================================================
00165 NS_IMETHODIMP nsUnicodeToTIS620::FillInfo(PRUint32* aInfo)
00166 {
00167   PRUint16 i;
00168 
00169   // 00-0x7f
00170   for (i = 0;i <= 0x7f; i++)
00171     SET_REPRESENTABLE(aInfo, i);
00172 
00173   // 0x0e01-0x0e7f
00174   for (i = 0x0e01; i <= 0xe3a; i++)
00175     SET_REPRESENTABLE(aInfo, i);
00176 
00177   // U+0E3B - U+0E3E is undefined
00178   // U+0E3F - U+0E5B
00179   for (i = 0x0e3f; i <= 0x0e5b; i++)
00180     SET_REPRESENTABLE(aInfo, i);
00181 
00182   // U+0E5C - U+0E7F Undefined
00183   return NS_OK;
00184 }