Back to index

lightning-sunbird  0.9+nobinonly
nsMacNativeUnicodeConverter.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
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 the GNU General Public License Version 2 or later (the "GPL"), or
00026  * 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 "nsMemory.h"
00039 #include "nsMacNativeUnicodeConverter.h"
00040 
00041 #include <TextUtils.h>
00042 #include <UnicodeConverter.h>
00043 #include <Fonts.h>
00044 
00045 //-------------------------------------------------------------------------
00046 //
00047 //
00048 //-------------------------------------------------------------------------
00049 nsresult nsMacNativeUnicodeConverter::ConvertScripttoUnicode(ScriptCode aScriptCode, 
00050                                                              const char *aMultibyteStr,
00051                                                              PRInt32 aMultibyteStrLen,
00052                                                              PRUnichar **aUnicodeStr,
00053                                                              PRInt32 *aUnicodeStrLen)
00054 {
00055   NS_ENSURE_ARG(aUnicodeStr);
00056   NS_ENSURE_ARG(aMultibyteStr);
00057 
00058   *aUnicodeStr = nsnull;
00059   *aUnicodeStrLen = 0;
00060        
00061   TextEncoding textEncodingFromScript;
00062   OSErr err = ::UpgradeScriptInfoToTextEncoding(aScriptCode, 
00063                                                 kTextLanguageDontCare, 
00064                                                 kTextRegionDontCare, 
00065                                                 nsnull,
00066                                                 &textEncodingFromScript);
00067   NS_ENSURE_TRUE(err == noErr, NS_ERROR_FAILURE);
00068        
00069   TextToUnicodeInfo  textToUnicodeInfo;
00070   err = ::CreateTextToUnicodeInfoByEncoding(textEncodingFromScript, &textToUnicodeInfo);
00071   NS_ENSURE_TRUE(err == noErr, NS_ERROR_FAILURE);
00072 
00073   // for MacArabic and MacHebrew, the corresponding Unicode string could be up to
00074   // six times as big.
00075   UInt32 factor;
00076   if (aScriptCode == smArabic ||
00077       aScriptCode == smHebrew ||
00078       aScriptCode == smExtArabic)
00079     factor = 6;
00080   else
00081     factor = 2;
00082     
00083   UniChar *unicodeStr = (UniChar *) nsMemory::Alloc(aMultibyteStrLen * factor);
00084   if (!unicodeStr) {
00085     ::DisposeTextToUnicodeInfo(&textToUnicodeInfo);
00086     return NS_ERROR_OUT_OF_MEMORY;
00087   }
00088 
00089   ByteCount sourceRead;
00090   ByteCount unicodeLen;
00091   
00092   err = ::ConvertFromTextToUnicode(textToUnicodeInfo,
00093                                    aMultibyteStrLen,
00094                                    (ConstLogicalAddress) aMultibyteStr,
00095                                    kUnicodeUseFallbacksMask | 
00096                                    kUnicodeLooseMappingsMask,
00097                                    0,nsnull,nsnull,nsnull,
00098                                    aMultibyteStrLen * factor,
00099                                    &sourceRead,
00100                                    &unicodeLen,
00101                                    unicodeStr);
00102                             
00103   if (err == noErr)
00104   {
00105     *aUnicodeStr = (PRUnichar *) unicodeStr;
00106     *aUnicodeStrLen = unicodeLen / sizeof(UniChar); // byte count -> char count
00107   }
00108                                                                       
00109   ::DisposeTextToUnicodeInfo(&textToUnicodeInfo);
00110                                                                                                 
00111   return err == noErr ? NS_OK : NS_ERROR_FAILURE;                                  
00112 }
00113 
00114 nsresult nsMacNativeUnicodeConverter::ConvertUnicodetoScript(const PRUnichar *aUnicodeStr, 
00115                                                              PRInt32 aUnicodeStrLen,
00116                                                              char **aMultibyteStr,
00117                                                              PRInt32 *aMultibyteStrlen,
00118                                                              ScriptCodeRun **aScriptCodeRuns,
00119                                                              PRInt32 *aScriptCodeRunLen)
00120 {
00121   NS_ENSURE_ARG(aUnicodeStr);
00122   NS_ENSURE_ARG(aMultibyteStr);
00123   NS_ENSURE_ARG(aMultibyteStrlen);
00124   NS_ENSURE_ARG(aScriptCodeRuns);
00125   NS_ENSURE_ARG(aScriptCodeRunLen);
00126   
00127   *aMultibyteStr = nsnull;
00128   *aMultibyteStrlen = 0;
00129   *aScriptCodeRuns = nsnull;
00130   *aScriptCodeRunLen = 0;
00131   
00132   // Get a list of installed script.
00133   ItemCount numberOfScriptCodes = ::GetScriptManagerVariable(smEnabled);
00134   ScriptCode *scriptArray = (ScriptCode *) nsMemory::Alloc(sizeof(ScriptCode) * numberOfScriptCodes);
00135   NS_ENSURE_TRUE(scriptArray, NS_ERROR_OUT_OF_MEMORY);
00136        
00137   for (ScriptCode i = 0, j = 0; i <= smUninterp && j < numberOfScriptCodes; i++)
00138   {
00139     if (::GetScriptVariable(i, smScriptEnabled))
00140       scriptArray[j++] = i;
00141   }
00142 
00143   OSErr err;
00144   UnicodeToTextRunInfo unicodeToTextInfo;
00145   err = ::CreateUnicodeToTextRunInfoByScriptCode(numberOfScriptCodes,
00146                                                  scriptArray,
00147                                                  &unicodeToTextInfo); 
00148   nsMemory::Free(scriptArray);                                                 
00149   NS_ENSURE_TRUE(err == noErr, NS_ERROR_FAILURE);
00150                                               
00151   ByteCount inputRead;
00152   ItemCount scriptRunOutLen;
00153   ScriptCodeRun *scriptCodeRuns = NS_REINTERPRET_CAST(ScriptCodeRun*,
00154                                                       nsMemory::Alloc(sizeof(ScriptCodeRun) * 
00155                                                       aUnicodeStrLen));
00156   if (!scriptCodeRuns) {
00157     ::DisposeUnicodeToTextRunInfo(&unicodeToTextInfo);
00158     return NS_ERROR_OUT_OF_MEMORY;
00159   }
00160        
00161   // Allocate 2 bytes per character. 
00162   // I think that would be enough for charset encodings used by legacy Macintosh apps.
00163   LogicalAddress outputStr = (LogicalAddress) nsMemory::Alloc(aUnicodeStrLen * 2);
00164   if (!outputStr)
00165   {
00166     ::DisposeUnicodeToTextRunInfo(&unicodeToTextInfo);
00167     nsMemory::Free(scriptCodeRuns);
00168     return NS_ERROR_OUT_OF_MEMORY;
00169   }
00170   ByteCount outputLen = 0;
00171 
00172   err = ::ConvertFromUnicodeToScriptCodeRun(unicodeToTextInfo,
00173                                             aUnicodeStrLen * sizeof(UniChar),
00174                                             (UniChar *) aUnicodeStr,
00175                                             kUnicodeUseFallbacksMask | 
00176                                             kUnicodeLooseMappingsMask |
00177                                             kUnicodeTextRunMask,
00178                                             0,
00179                                             nsnull,
00180                                             nsnull,
00181                                             nsnull,
00182                                             aUnicodeStrLen * sizeof(PRUnichar), // char count -> byte count
00183                                             &inputRead,
00184                                             &outputLen,
00185                                             outputStr,
00186                                             aUnicodeStrLen,
00187                                             &scriptRunOutLen,
00188                                             scriptCodeRuns);
00189                                                    
00190   if (outputLen > 0 &&
00191       (err == noErr ||
00192        err == kTECUnmappableElementErr ||
00193        err == kTECOutputBufferFullStatus))
00194   {
00195     // set output as long as something got converted
00196     // since this function is called as a fallback
00197     *aMultibyteStr = (char *) outputStr;
00198     *aMultibyteStrlen = outputLen;
00199     *aScriptCodeRuns = scriptCodeRuns;
00200     *aScriptCodeRunLen = scriptRunOutLen;
00201     err = noErr;
00202   }
00203 
00204   ::DisposeUnicodeToTextRunInfo(&unicodeToTextInfo);
00205   
00206   return err == noErr ? NS_OK : NS_ERROR_FAILURE;                                  
00207 }
00208