Back to index

lightning-sunbird  0.9+nobinonly
Static Public Member Functions
nsMacNativeUnicodeConverter Class Reference

#include <nsMacNativeUnicodeConverter.h>

List of all members.

Static Public Member Functions

static nsresult ConvertScripttoUnicode (ScriptCode aScriptCode, const char *aMultibyteStr, PRInt32 aMultibyteStrLen, PRUnichar **aUnicodeStr, PRInt32 *aUnicodeStrLen)
static nsresult ConvertUnicodetoScript (const PRUnichar *aUnicodeStr, PRInt32 aUnicodeStrLen, char **aMultibyteStr, PRInt32 *aMultibyteStrlen, ScriptCodeRun **aScriptCodeRuns, PRInt32 *aScriptCodeRunLen)

Detailed Description

Definition at line 49 of file nsMacNativeUnicodeConverter.h.


Member Function Documentation

nsresult nsMacNativeUnicodeConverter::ConvertScripttoUnicode ( ScriptCode  aScriptCode,
const char *  aMultibyteStr,
PRInt32  aMultibyteStrLen,
PRUnichar **  aUnicodeStr,
PRInt32 aUnicodeStrLen 
) [static]

Definition at line 49 of file nsMacNativeUnicodeConverter.cpp.

{
  NS_ENSURE_ARG(aUnicodeStr);
  NS_ENSURE_ARG(aMultibyteStr);

  *aUnicodeStr = nsnull;
  *aUnicodeStrLen = 0;
       
  TextEncoding textEncodingFromScript;
  OSErr err = ::UpgradeScriptInfoToTextEncoding(aScriptCode, 
                                                kTextLanguageDontCare, 
                                                kTextRegionDontCare, 
                                                nsnull,
                                                &textEncodingFromScript);
  NS_ENSURE_TRUE(err == noErr, NS_ERROR_FAILURE);
       
  TextToUnicodeInfo  textToUnicodeInfo;
  err = ::CreateTextToUnicodeInfoByEncoding(textEncodingFromScript, &textToUnicodeInfo);
  NS_ENSURE_TRUE(err == noErr, NS_ERROR_FAILURE);

  // for MacArabic and MacHebrew, the corresponding Unicode string could be up to
  // six times as big.
  UInt32 factor;
  if (aScriptCode == smArabic ||
      aScriptCode == smHebrew ||
      aScriptCode == smExtArabic)
    factor = 6;
  else
    factor = 2;
    
  UniChar *unicodeStr = (UniChar *) nsMemory::Alloc(aMultibyteStrLen * factor);
  if (!unicodeStr) {
    ::DisposeTextToUnicodeInfo(&textToUnicodeInfo);
    return NS_ERROR_OUT_OF_MEMORY;
  }

  ByteCount sourceRead;
  ByteCount unicodeLen;
  
  err = ::ConvertFromTextToUnicode(textToUnicodeInfo,
                                   aMultibyteStrLen,
                                   (ConstLogicalAddress) aMultibyteStr,
                                   kUnicodeUseFallbacksMask | 
                                   kUnicodeLooseMappingsMask,
                                   0,nsnull,nsnull,nsnull,
                                   aMultibyteStrLen * factor,
                                   &sourceRead,
                                   &unicodeLen,
                                   unicodeStr);
                            
  if (err == noErr)
  {
    *aUnicodeStr = (PRUnichar *) unicodeStr;
    *aUnicodeStrLen = unicodeLen / sizeof(UniChar); // byte count -> char count
  }
                                                                      
  ::DisposeTextToUnicodeInfo(&textToUnicodeInfo);
                                                                                                
  return err == noErr ? NS_OK : NS_ERROR_FAILURE;                                  
}

Here is the call graph for this function:

nsresult nsMacNativeUnicodeConverter::ConvertUnicodetoScript ( const PRUnichar aUnicodeStr,
PRInt32  aUnicodeStrLen,
char **  aMultibyteStr,
PRInt32 aMultibyteStrlen,
ScriptCodeRun **  aScriptCodeRuns,
PRInt32 aScriptCodeRunLen 
) [static]

Definition at line 114 of file nsMacNativeUnicodeConverter.cpp.

{
  NS_ENSURE_ARG(aUnicodeStr);
  NS_ENSURE_ARG(aMultibyteStr);
  NS_ENSURE_ARG(aMultibyteStrlen);
  NS_ENSURE_ARG(aScriptCodeRuns);
  NS_ENSURE_ARG(aScriptCodeRunLen);
  
  *aMultibyteStr = nsnull;
  *aMultibyteStrlen = 0;
  *aScriptCodeRuns = nsnull;
  *aScriptCodeRunLen = 0;
  
  // Get a list of installed script.
  ItemCount numberOfScriptCodes = ::GetScriptManagerVariable(smEnabled);
  ScriptCode *scriptArray = (ScriptCode *) nsMemory::Alloc(sizeof(ScriptCode) * numberOfScriptCodes);
  NS_ENSURE_TRUE(scriptArray, NS_ERROR_OUT_OF_MEMORY);
       
  for (ScriptCode i = 0, j = 0; i <= smUninterp && j < numberOfScriptCodes; i++)
  {
    if (::GetScriptVariable(i, smScriptEnabled))
      scriptArray[j++] = i;
  }

  OSErr err;
  UnicodeToTextRunInfo unicodeToTextInfo;
  err = ::CreateUnicodeToTextRunInfoByScriptCode(numberOfScriptCodes,
                                                 scriptArray,
                                                 &unicodeToTextInfo); 
  nsMemory::Free(scriptArray);                                                 
  NS_ENSURE_TRUE(err == noErr, NS_ERROR_FAILURE);
                                              
  ByteCount inputRead;
  ItemCount scriptRunOutLen;
  ScriptCodeRun *scriptCodeRuns = NS_REINTERPRET_CAST(ScriptCodeRun*,
                                                      nsMemory::Alloc(sizeof(ScriptCodeRun) * 
                                                      aUnicodeStrLen));
  if (!scriptCodeRuns) {
    ::DisposeUnicodeToTextRunInfo(&unicodeToTextInfo);
    return NS_ERROR_OUT_OF_MEMORY;
  }
       
  // Allocate 2 bytes per character. 
  // I think that would be enough for charset encodings used by legacy Macintosh apps.
  LogicalAddress outputStr = (LogicalAddress) nsMemory::Alloc(aUnicodeStrLen * 2);
  if (!outputStr)
  {
    ::DisposeUnicodeToTextRunInfo(&unicodeToTextInfo);
    nsMemory::Free(scriptCodeRuns);
    return NS_ERROR_OUT_OF_MEMORY;
  }
  ByteCount outputLen = 0;

  err = ::ConvertFromUnicodeToScriptCodeRun(unicodeToTextInfo,
                                            aUnicodeStrLen * sizeof(UniChar),
                                            (UniChar *) aUnicodeStr,
                                            kUnicodeUseFallbacksMask | 
                                            kUnicodeLooseMappingsMask |
                                            kUnicodeTextRunMask,
                                            0,
                                            nsnull,
                                            nsnull,
                                            nsnull,
                                            aUnicodeStrLen * sizeof(PRUnichar), // char count -> byte count
                                            &inputRead,
                                            &outputLen,
                                            outputStr,
                                            aUnicodeStrLen,
                                            &scriptRunOutLen,
                                            scriptCodeRuns);
                                                   
  if (outputLen > 0 &&
      (err == noErr ||
       err == kTECUnmappableElementErr ||
       err == kTECOutputBufferFullStatus))
  {
    // set output as long as something got converted
    // since this function is called as a fallback
    *aMultibyteStr = (char *) outputStr;
    *aMultibyteStrlen = outputLen;
    *aScriptCodeRuns = scriptCodeRuns;
    *aScriptCodeRunLen = scriptRunOutLen;
    err = noErr;
  }

  ::DisposeUnicodeToTextRunInfo(&unicodeToTextInfo);
  
  return err == noErr ? NS_OK : NS_ERROR_FAILURE;                                  
}

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following files: