textrendering/textformatting/tagma/TmChunk.cpp
changeset 24 71313a964664
parent 0 1fb32624e06b
child 49 4d76f1414957
equal deleted inserted replaced
21:f2f7b3284356 24:71313a964664
    17 
    17 
    18 
    18 
    19 #include "TmLayoutImp.h"
    19 #include "TmLayoutImp.h"
    20 #include "InlineText.h"
    20 #include "InlineText.h"
    21 #include "frmUtils.h"
    21 #include "frmUtils.h"
    22 
       
    23 inline TBool IsSurrogate(TText a) { return 0xD800 == (a & 0xF800); }
       
    24 inline TBool IsHighSurrogate(TText a) { return 0xD800 == (a & 0xFC00); }
       
    25 inline TBool IsLowSurrogate(TText a) { return 0xDC00 == (a & 0xFC00); }
       
    26 inline TChar JoinSurrogates(TText aHigh, TText aLow)
       
    27 	{
       
    28 	return ((aHigh - 0xd7f7) << 10) + aLow;
       
    29 	}
       
    30 
       
    31 
       
    32 inline TText16 GetHighSurrogate(TUint aChar)
       
    33 	{
       
    34 	return STATIC_CAST(TText16, 0xD7C0 + (aChar >> 10));
       
    35 	}
       
    36 
       
    37 inline TText16 GetLowSurrogate(TUint aChar)
       
    38 	{
       
    39 	return STATIC_CAST(TText16, 0xDC00 | (aChar & 0x3FF));
       
    40 	}
       
    41 
    22 
    42 
    23 
    43 TTmChunk::TTmChunk():
    24 TTmChunk::TTmChunk():
    44 	iDocPos(0),
    25 	iDocPos(0),
    45 	iTextLength(0),
    26 	iTextLength(0),
   105 					continue;
    86 					continue;
   106 					}
    87 					}
   107 				}
    88 				}
   108 			}
    89 			}
   109 
    90 
   110 		if ( IsLowSurrogate( *current_character ) )
    91 		if ( TChar::IsLowSurrogate( *current_character ) )
   111 			{
    92 			{
   112 			// skip it
    93 			// skip it
   113 			RDebug::Print(_L("Error: Should not be low surrogate. Skip corrupt low surrogate %X."), *current_character);
    94 			RDebug::Print(_L("Error: Should not be low surrogate. Skip corrupt low surrogate %X."), *current_character);
   114 			current_character++;
    95 			current_character++;
   115 			continue;
    96 			continue;
   116 			}
    97 			}
   117 		
    98 		
   118 		const TText *previous_character = current_character;
    99 		const TText *previous_character = current_character;
   119 		TUint character_to_process = *current_character++;
   100 		TUint character_to_process = *current_character++;
   120 		// If it's surrogate, join the high and low together
   101 		// If it's surrogate, join the high and low together
   121 		if( IsHighSurrogate( character_to_process ) )
   102 		if( TChar::IsHighSurrogate( character_to_process ) )
   122 			{
   103 			{
   123 			TInt high = character_to_process;
   104 			TInt high = character_to_process;
   124 			if ( current_character < last_character )
   105 			if ( current_character < last_character )
   125 			    {
   106 			    {
   126 	            TInt low = *current_character++;
   107 	            TInt low = *current_character++;
   127 	            if ( !IsLowSurrogate( low ) )
   108 	            if ( !TChar::IsLowSurrogate( low ) )
   128 	                {
   109 	                {
   129 	                // should be low surrogate
   110 	                // should be low surrogate
   130 	                // skip the high surrogate
   111 	                // skip the high surrogate
   131 	                RDebug::Print(_L("Error: Should be low surrogate. Skip corrupt high surrogate %X."), high);
   112 	                RDebug::Print(_L("Error: %X should be low surrogate. Skip corrupt high surrogate %X."), low, high);
   132 	                current_character--;
   113 	                current_character--;
   133 	                continue;
   114 	                continue;
   134 	                }
   115 	                }
   135 	            character_to_process = JoinSurrogates( high, low );			    
   116 	            character_to_process = TChar::JoinSurrogate( high, low );			    
   136 			    }
   117 			    }
   137 			}
   118 			}
   138 
   119 
   139 		TBool quit = FALSE;
   120 		TBool quit = FALSE;
   140 		TBool append_char = TRUE;
   121 		TBool append_char = TRUE;
   215 				bd_cat = aInfo.iBdCat;
   196 				bd_cat = aInfo.iBdCat;
   216 			else
   197 			else
   217 				{
   198 				{
   218 				TUint next_character_to_process = *current_character;
   199 				TUint next_character_to_process = *current_character;
   219 				// If the next character is surrogate
   200 				// If the next character is surrogate
   220 		        if( IsHighSurrogate( next_character_to_process ) )
   201 		        if( TChar::IsHighSurrogate( next_character_to_process ) )
   221 		            {
   202 		            {
   222 		            TInt high = next_character_to_process;
   203 		            TInt high = next_character_to_process;
   223 		            if ( current_character < last_character )
   204 		            if ( current_character < last_character )
   224 		                {
   205 		                {
   225 	                    TInt low = *(++current_character);
   206 	                    TInt low = *(++current_character);
   226 	                    
   207 	                    
   227 	                    if ( !IsLowSurrogate( low ) )
   208 	                    if ( !TChar::IsLowSurrogate( low ) )
   228 	                        {
   209 	                        {
   229 	                        // should be low surrogate
   210 	                        // should be low surrogate
   230 	                        RDebug::Print(_L("Error: Should be low surrogate. See TmChunk.cpp, TruncateIfNeeded()."));
   211 	                        RDebug::Print(_L("Error: %X should be low surrogate. See TmChunk.cpp, TruncateIfNeeded()."), low);
   231                             // The category of the character is determined by what it's mapped
   212                             // The category of the character is determined by what it's mapped
   232 	                        next_character_to_process = aContext.iSource.Map( high );
   213 	                        next_character_to_process = aContext.iSource.Map( high );
   233 	                        }
   214 	                        }
   234 	                    else
   215 	                    else
   235 	                        {
   216 	                        {
   236 	                        next_character_to_process = JoinSurrogates( high, low );
   217 	                        next_character_to_process = TChar::JoinSurrogate( high, low );
   237 	                        }
   218 	                        }
   238 	                    --current_character;		                
   219 	                    --current_character;		                
   239 		                }
   220 		                }
   240 		            }
   221 		            }
   241 				TChar::TBdCategory next_cat = BdCategory(next_character_to_process);				
   222 				TChar::TBdCategory next_cat = BdCategory(next_character_to_process);