Back to index

lightning-sunbird  0.9+nobinonly
Public Types | Public Member Functions | Protected Attributes | Private Member Functions
nsUnicodeToJamoTTF Class Reference

#include <nsUnicodeToJamoTTF.h>

Inheritance diagram for nsUnicodeToJamoTTF:
Inheritance graph
[legend]
Collaboration diagram for nsUnicodeToJamoTTF:
Collaboration graph
[legend]

List of all members.

Public Types

enum  { kOnError_Signal, kOnError_CallBack, kOnError_Replace }

Public Member Functions

 nsUnicodeToJamoTTF ()
 Class constructor.
virtual ~nsUnicodeToJamoTTF ()
NS_IMETHOD Convert (const PRUnichar *aSrc, PRInt32 *aSrcLength, char *aDest, PRInt32 *aDestLength)
 Converts the data from Unicode to a Charset.
NS_IMETHOD Finish (char *aDest, PRInt32 *aDestLength)
 Finishes the conversion.
NS_IMETHOD GetMaxLength (const PRUnichar *aSrc, PRInt32 aSrcLength, PRInt32 *aDestLength)
 Returns a quick estimation of the size of the buffer needed to hold the converted data.
NS_IMETHOD Reset ()
 Resets the charset converter so it may be recycled for a completely different and urelated buffer of data.
NS_IMETHOD SetOutputErrorBehavior (PRInt32 aBehavior, nsIUnicharEncoder *aEncoder, PRUnichar aChar)
 Specify what to do when a character cannot be mapped into the dest charset.
NS_IMETHOD FillInfo (PRUint32 *aInfo)

Protected Attributes

PRUnicharmJamos
PRUnichar mJamosStatic [9]
PRInt32 mJamoCount
PRInt32 mJamosMaxLength
PRInt32 mByteOff
PRInt32 mErrBehavior
PRUnichar mErrChar
nsCOMPtr< nsIUnicharEncodermErrEncoder

Private Member Functions

NS_IMETHOD composeHangul (char *output)
int RenderAsPrecompSyllable (PRUnichar *aSrc, PRInt32 *aSrcLength, char *aResult)

Detailed Description

Definition at line 49 of file nsUnicodeToJamoTTF.h.


Member Enumeration Documentation

anonymous enum [inherited]
Enumerator:
kOnError_Signal 
kOnError_CallBack 
kOnError_Replace 

Definition at line 136 of file nsIUnicodeEncoder.h.

       {
    kOnError_Signal,        // on an error, stop and signal
    kOnError_CallBack,      // on an error, call the error handler
    kOnError_Replace       // on an error, replace with a different character
  };

Constructor & Destructor Documentation

Class constructor.

Definition at line 179 of file nsUnicodeToJamoTTF.cpp.

{
  mJamos = nsnull;
  Reset();
}

Here is the call graph for this function:

Definition at line 185 of file nsUnicodeToJamoTTF.cpp.


Member Function Documentation

NS_IMETHODIMP nsUnicodeToJamoTTF::composeHangul ( char *  output) [private]

Definition at line 499 of file nsUnicodeToJamoTTF.cpp.

{
  PRInt32 length = mJamoCount, i;
  nsresult rv = NS_OK;

  if (!length)
  {
    NS_WARNING("composeHangul() : zero length string comes in ! \n");
    return NS_ERROR_UNEXPECTED;
  }

  if (!aResult) 
    return NS_ERROR_NULL_POINTER;

  // Put Hangul tone mark first as it should be to the left of 
  // the character it follows.
  // XXX : What should we do when a tone mark come by itself?
  
  if (IS_TONE(mJamos[length - 1])) 
  {
    aResult[mByteOff++] = PRUint8(mJamos[length - 1] >> 8);
    aResult[mByteOff++] = PRUint8(mJamos[length - 1] & 0xff); 
    if (--length == 0)
      return rv;
  }

  // no more processing is necessary for precomposed modern Hangul syllables.
  if (length == 1 && IS_SYL(mJamos[0])) 
  {
    aResult[mByteOff++] = PRUint8(mJamos[0] >> 8);
    aResult[mByteOff++] = PRUint8(mJamos[0] & 0xff); 
    return rv;
  }

  if (CHAR_CLASS(mJamos[0]) == KO_CHAR_CLASS_NOHANGUL) 
  {
    NS_ASSERTION(length == 1, "A non-Hangul should come by itself !!\n");
    aResult[mByteOff++] = PRUint8(mJamos[0] >> 8);
    aResult[mByteOff++] = PRUint8(mJamos[0] & 0xff); 
    return rv;
  }

  nsXPIDLString buffer;

  rv =  JamoNormalize(mJamos, getter_Copies(buffer), &length);

  // safe to cast away const.
  PRUnichar* text = buffer.BeginWriting();
  NS_ENSURE_SUCCESS(rv, rv);

  text += RenderAsPrecompSyllable(text, &length, aResult);

  if (!length)
    return rv;

  // convert to extended Jamo sequence
  JamosToExtJamos(text, &length);


  // Check if not in LV or LVT form after the conversion
  if (length != 2 && length != 3 ||
      (!IS_LC_EXT(text[0]) || !IS_VO_EXT(text[1]) ||
       (length == 3 && !IS_TC_EXT(text[2]))))
    goto fallback;

//  Now that text[0..2] are identified as L,V, and T, it's safe to 
//  shift them back to U+1100 block although their ranges overlap each other.
  
  text[0] -= LC_OFFSET; 
  text[1] -= VO_OFFSET; 
  if (length == 3)
    text[2] -= TC_OFFSET;

  if (length != 3)
  {
    text[0] = gUnParkLcGlyphMap[text[0] - LBASE] * 6 + 
              gUnParkVo2LcMap[text[1] - VFILL] + UP_LBASE;
    text[1] = gUnParkVoGlyphMap[text[1] - VFILL] * 2 + UP_VBASE;
  }
  else 
  {
    text[0] = gUnParkLcGlyphMap[text[0] - LBASE] * 6 + 
              gUnParkVo2LcMap2[text[1] - VFILL] + UP_LBASE;
    text[2] = gUnParkTcGlyphMap[text[2] - TSTART] * 4 + 
              gUnParkVo2TcMap[text[1] - VFILL] + UP_TBASE; 
    text[1] = gUnParkVoGlyphMap[text[1] - VFILL] * 2 + UP_VBASE + 1; 
  }

  // Xft doesn't like blank glyphs at code points other than listed in 
  // the blank glyph list. Replace Lfill glyph code points of UnPark
  // fonts with standard LFILL code point (U+115F).
    
  if (UP_LBASE <= text[0] && text[0] < UP_LBASE + 6)
    text[0] = LFILL;

  // The same is true of glyph code points corresponding to VFILL
  // in UnBatang-like fonts. VFILL is not only blank but also non-advancing
  // so that we can just skip it. 
  if (UP_VBASE <= text[1] && text[1] < UP_VBASE + 2)
  {
    --length;
    if (length == 2) 
      text[1] = text[2]; 
  }

  for (i = 0 ; i < length; i++)
  {
    aResult[mByteOff++] = PRUint8(text[i] >> 8);
    aResult[mByteOff++] = PRUint8(text[i] & 0xff);
  }

  return rv;


  /* If jamo sequence is not convertible to a jamo cluster,
   * just enumerate stand-alone jamos. Prepend V and T with  Lf.
   *
   * XXX: It might be better to search for a sub-sequence (not just at the
   * beginning of a cluster but also in the middle or at the end.) 
   * that can be rendered as precomposed and render it as such and enumerate
   * jamos in the rest. This approach is useful when a simple Xkb-based input
   * is used. 
   */

fallback: 
  for (i = 0; i < length; i++)
  {
    PRUnichar wc=0, wc2=0;
    /* skip Lfill and Vfill if they're not the sole char. in a cluster */
    if (length > 1 && 
         (text[i] - LC_OFFSET == LFILL || text[i] - VO_OFFSET == VFILL))
      continue;
    else if (IS_LC_EXT (text[i]))
       wc = gUnParkLcGlyphMap[text[i] - LC_OFFSET - LBASE] * 6 + UP_LBASE;
    else 
    {
  /* insert Lfill glyph to advance cursor pos. for V and T */
      wc = LBASE;
  /* don't have to draw Vfill. Drawing Lfill is sufficient. */ 
      if (text[i] - VO_OFFSET != VFILL) 
        wc2 = IS_VO_EXT (text[i]) ? 
        gUnParkVoGlyphMap[text[i] - VO_OFFSET - VFILL] * 2 + UP_VBASE:
        gUnParkTcGlyphMap[text[i] - TC_OFFSET - TSTART] * 4 + UP_TBASE + 3;
    }
    aResult[mByteOff++] = PRUint8(wc >> 8);
    aResult[mByteOff++] = PRUint8(wc & 0xff);

    if (wc2) 
    {
      aResult[mByteOff++] = wc2 >> 8;
      aResult[mByteOff++] = wc2 & 0xff; 
    }
  }

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP nsUnicodeToJamoTTF::Convert ( const PRUnichar aSrc,
PRInt32 aSrcLength,
char *  aDest,
PRInt32 aDestLength 
) [virtual]

Converts the data from Unicode to a Charset.

About the byte ordering:

  • The input stream is Unicode, having the byte order which is internal for the machine on which the converter is running on.
  • For output, if the converter cares (that depends of the charset, for example a singlebyte will ignore the byte ordering) it should assume network order. If necessary and requested, we can add a method SetOutputByteOrder() so that the reverse order can be used, too. That method would have as default the assumed network order.

Unless there is not enough output space, this method must consume all the available input data! We don't have partial input for the Unicode charset. And for the last converted char, even if there is not enought output space, a partial ouput must be done until all available space will be used. The rest of the output should be buffered until more space becomes available. But this is not also true about the error handling method!!! So be very, very careful...

Parameters:
aSrc[IN] the source data buffer
aSrcLength[IN/OUT] the length of source data buffer; after conversion will contain the number of Unicode characters read
aDest[OUT] the destination data buffer
aDestLength[IN/OUT] the length of the destination data buffer; after conversion will contain the number of bytes written
Returns:
NS_OK_UENC_MOREOUTPUT if only a partial conversion was done; more output space is needed to continue NS_ERROR_UENC_NOMAPPING if character without mapping was encountered and the behavior was set to "signal".

Implements nsIUnicodeEncoder.

Definition at line 226 of file nsUnicodeToJamoTTF.cpp.

{
  nsresult rv = NS_OK;
  mByteOff = 0;

  // This should never happen, but it happens under MS Windows, somehow...
  if (mJamoCount > mJamosMaxLength) 
  {
    NS_WARNING("mJamoCount > mJamoMaxLength on entering Convert()");
    Reset();
  }

  for (PRInt32 charOff = 0; charOff < *aSrcLength; charOff++)
  {
    PRUnichar ch = aSrc[charOff];

    // Syllable boundary check. Ref. : Unicode 3.2 section 3.11 
    if (mJamoCount != 0 &&
        gIsBoundary[CHAR_CLASS(mJamos[mJamoCount - 1])][CHAR_CLASS(ch)])
    {
      composeHangul(aDest);
      mJamoCount = 0;
    }
    // Ignore tone marks other than the first in a sequence of tone marks.
    else if (mJamoCount != 0 && IS_TONE(mJamos[mJamoCount - 1]) && IS_TONE(ch))
    {
      --mJamoCount; 
      composeHangul(aDest);
      mJamoCount = 0;

      // skip over tone marks from the second on in a series.
      while (IS_TONE(ch) && ++charOff < *aSrcLength)
        ch = aSrc[charOff]; 

      if (!IS_TONE(ch)) 
      {
        mJamos[mJamoCount++] = ch; 
        continue;
      }
      else
        break;
    }

    if (mJamoCount == mJamosMaxLength)
    {
      mJamosMaxLength++;
      if (mJamos == mJamosStatic)
      {
        mJamos = (PRUnichar *) PR_Malloc(sizeof(PRUnichar) * mJamosMaxLength);
        if (!mJamos)
          return  NS_ERROR_OUT_OF_MEMORY;
        memcpy(mJamos, mJamosStatic, sizeof(PRUnichar) * mJamoCount);
      }
      else
      {
        mJamos = (PRUnichar *) PR_Realloc(mJamos, 
                               sizeof(PRUnichar) * mJamosMaxLength);
        if (!mJamos)
          return  NS_ERROR_OUT_OF_MEMORY;
      }
    }

    mJamos[mJamoCount++] = ch;
  }
    
  if (mJamoCount != 0)
    composeHangul(aDest);
  mJamoCount = 0;
  *aDestLength = mByteOff;

  return rv;
}

Here is the call graph for this function:

Implements nsICharRepresentable.

Definition at line 343 of file nsUnicodeToJamoTTF.cpp.

{
  FillInfoRange(aInfo, SBASE, SEND);

  PRUnichar i;

  // Hangul Conjoining Jamos
  for(i = 0x1100; i<= 0x1159; i++)
     SET_REPRESENTABLE(aInfo, i);
  SET_REPRESENTABLE(aInfo, 0x115f);
  for(i = 0x1160; i <= 0x11a2; i++)
     SET_REPRESENTABLE(aInfo, i);
  for(i = 0x11a8; i <= 0x11f9; i++)
     SET_REPRESENTABLE(aInfo, i);

  // Hangul Tone marks
  SET_REPRESENTABLE(aInfo, HTONE1);
  SET_REPRESENTABLE(aInfo, HTONE2);

  // UnPark  fonts have US-ASCII chars.
  for(i=0x20; i < 0x7f; i++)
     SET_REPRESENTABLE(aInfo, i);

  nsresult rv;

  // UnPark fonts have Hanjas and symbols defined in KS X 1001 as well.
  
  // XXX: Do we need to exclude Cyrillic, Greek letters and some Latin letters 
  // included in KS X 1001 as 'symbol characters'? 
  // KS X 1001 has only a subset of Greek and Cyrillic alphabets and
  // Latin letters with diacritic marks so that including them may
  // result in ransom-note like effect if it is listed *before*
  // any genuine Greek/Russian/Latin fonts in CSS. 
    
  // Lead byte range for symbol chars. in EUC-KR : 0xA1 - 0xAF
  rv = FillInfoEUCKR(aInfo, 0xA1, 0xAF); 
  NS_ENSURE_SUCCESS(rv, rv);

  // Lead byte range for Hanja in EUC-KR : 0xCA - 0xFD.
  return FillInfoEUCKR(aInfo, 0xCA, 0xFD); 
}

Here is the call graph for this function:

NS_IMETHODIMP nsUnicodeToJamoTTF::Finish ( char *  aDest,
PRInt32 aDestLength 
) [virtual]

Finishes the conversion.

The converter has the possibility to write some extra data and flush its final state.

Parameters:
aDest[OUT] the destination data buffer
aDestLength[IN/OUT] the length of destination data buffer; after conversion it will contain the number of bytes written
Returns:
NS_OK_UENC_MOREOUTPUT if only a partial conversion was done; more output space is needed to continue

Implements nsIUnicodeEncoder.

Definition at line 302 of file nsUnicodeToJamoTTF.cpp.

{
  mByteOff = 0;
  if (mJamoCount != 0)
    composeHangul(aDest);

  *aDestLength = mByteOff;

  mByteOff = 0;
  mJamoCount = 0;
  return NS_OK;
}

Here is the call graph for this function:

NS_IMETHODIMP nsUnicodeToJamoTTF::GetMaxLength ( const PRUnichar aSrc,
PRInt32  aSrcLength,
PRInt32 aDestLength 
) [virtual]

Returns a quick estimation of the size of the buffer needed to hold the converted data.

Remember: this estimation is >= with the actual size of the buffer needed. It will be computed for the "worst case"

Parameters:
aSrc[IN] the source data buffer
aSrcLength[IN] the length of source data buffer
aDestLength[OUT] the needed size of the destination buffer
Returns:
NS_OK_UENC_EXACTLENGTH if an exact length was computed NS_OK if all we have is an approximation

Implements nsIUnicodeEncoder.

Definition at line 332 of file nsUnicodeToJamoTTF.cpp.

{
  // a precomposed Hangul syllable can be decomposed into 3 Jamos, each of
  // which takes 2bytes. 
  *aDestLength = aSrcLength *  6;
  return NS_OK;
}
int nsUnicodeToJamoTTF::RenderAsPrecompSyllable ( PRUnichar aSrc,
PRInt32 aSrcLength,
char *  aResult 
) [private]

Definition at line 657 of file nsUnicodeToJamoTTF.cpp.

{

  int composed = 0;

  if (*aSrcLength == 3 && IS_SYL_LC(aSrc[0]) && IS_SYL_VO(aSrc[1]) && 
      IS_SYL_TC(aSrc[2]))
    composed = 3;
  else if (*aSrcLength == 2 && IS_SYL_LC(aSrc[0]) && IS_SYL_VO(aSrc[1]))
    composed = 2;
  else
    composed = 0;

  if (composed)
  {
    PRUnichar wc;
    if (composed == 3)
      wc = SYL_FROM_LVT(aSrc[0], aSrc[1], aSrc[2]);
    else
      wc = SYL_FROM_LVT(aSrc[0], aSrc[1], TBASE);
    aResult[mByteOff++] = PRUint8(wc >> 8);
    aResult[mByteOff++] = PRUint8(wc & 0xff);
  }

  *aSrcLength -= composed;

  return composed;
}

Here is the caller graph for this function:

Resets the charset converter so it may be recycled for a completely different and urelated buffer of data.

Implements nsIUnicodeEncoder.

Definition at line 317 of file nsUnicodeToJamoTTF.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

Specify what to do when a character cannot be mapped into the dest charset.

Parameters:
aOrder[IN] the behavior; taken from the enum

Implements nsIUnicodeEncoder.

Definition at line 165 of file nsUnicodeToJamoTTF.cpp.

{
  if (aBehavior == kOnError_CallBack && aEncoder == nsnull)
    return NS_ERROR_NULL_POINTER;
  mErrEncoder = aEncoder;
  mErrBehavior = aBehavior;
  mErrChar = aChar;
  return NS_OK;
}

Member Data Documentation

Definition at line 86 of file nsUnicodeToJamoTTF.h.

Definition at line 88 of file nsUnicodeToJamoTTF.h.

Definition at line 89 of file nsUnicodeToJamoTTF.h.

Definition at line 90 of file nsUnicodeToJamoTTF.h.

Definition at line 84 of file nsUnicodeToJamoTTF.h.

Definition at line 82 of file nsUnicodeToJamoTTF.h.

Definition at line 85 of file nsUnicodeToJamoTTF.h.

Definition at line 83 of file nsUnicodeToJamoTTF.h.


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