fep/aknfep/src/aknfepzhuyinconvertion.cpp
changeset 36 a7632c26d895
parent 35 0f326f2e628e
child 42 b3eaa440ab06
equal deleted inserted replaced
35:0f326f2e628e 36:a7632c26d895
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0""
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:           
       
    15  *      Provides the TZhuyinSymbolsMethods definition 
       
    16  *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 
       
    28 
       
    29 
       
    30 #include "aknfepzhuyinconvertion.h"
       
    31 
       
    32 _LIT(KFullStop, "\x002E");
       
    33 _LIT(KToneMark0, "\x0020");
       
    34 _LIT(KToneMark1, "\x02CA");
       
    35 _LIT(KToneMark2, "\x02C7");
       
    36 _LIT(KToneMark3, "\x02CB");
       
    37 _LIT(KToneMark4, "\x02D9");
       
    38 
       
    39 const TInt KMinZhuyinUnicode = 0x3105;
       
    40 const TInt KMaxZhuyinUnicode = 0x3129;
       
    41 const TInt KMaxBufLen = 100;
       
    42 const TInt KMaxChineseCount = 7;
       
    43 
       
    44 // ---------------------------------------------------------
       
    45 // Convert aArray to a descriptor outputed through aOutDes.
       
    46 // ---------------------------------------------------------
       
    47 //
       
    48 TInt TZhuyinSymbolsMethods::Convert2Des ( CDesCArrayFlat* aArray, TDes& aOutDes )
       
    49     {
       
    50     TInt bufLen = 0;
       
    51     TInt i = 0;
       
    52     for ( ; i < aArray->Count(); i++ )
       
    53         {
       
    54         bufLen = bufLen + aArray->MdcaPoint(i).Length();
       
    55         if ( bufLen > KMaxBufLen ) 
       
    56             {
       
    57             return i;
       
    58             }
       
    59         aOutDes.Append(aArray->MdcaPoint(i));
       
    60         }
       
    61     return i;
       
    62     }
       
    63 
       
    64 // ---------------------------------------------------------
       
    65 // Update the content in eep control.
       
    66 // ---------------------------------------------------------
       
    67 //
       
    68 TBool TZhuyinSymbolsMethods::UpdateEEPContent( 
       
    69     CPtiEngine* aPtiengine,
       
    70     CAknFepZhuyinAnalyser* aAnalyser,
       
    71     CDesCArrayFlat* aZhuyinSymbols, 
       
    72     CDesCArrayFlat* aChineseDisped,
       
    73     CDesCArrayFlat* aCandidateList,
       
    74     TDes& aOutDes,
       
    75     TBool& aPhraseCreation)
       
    76     {
       
    77     TBool rnt = EFalse;
       
    78     TRAPD( ret, rnt = UpdateEEPContentL( aPtiengine, 
       
    79                                          aAnalyser, 
       
    80                                          aZhuyinSymbols, 
       
    81                                          aChineseDisped, 
       
    82                                          aCandidateList, 
       
    83                                          aOutDes, 
       
    84                                          aPhraseCreation ) );
       
    85     if ( KErrNone != ret )
       
    86         {
       
    87         return EFalse;
       
    88         }
       
    89     
       
    90     return rnt;
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------
       
    94 // clean.
       
    95 // ---------------------------------------------------------
       
    96 //
       
    97 void CleanArray( TAny* aAny )
       
    98     {
       
    99     RPointerArray<HBufC>* implArray = 
       
   100         reinterpret_cast< RPointerArray<HBufC>*> ( aAny );
       
   101     implArray->ResetAndDestroy();
       
   102     implArray->Close();
       
   103     }
       
   104 
       
   105 
       
   106 // ---------------------------------------------------------
       
   107 // Update the content in eep control.
       
   108 // ---------------------------------------------------------
       
   109 //
       
   110 TBool TZhuyinSymbolsMethods::UpdateEEPContentL( 
       
   111     CPtiEngine* aPtiengine,
       
   112     CAknFepZhuyinAnalyser* aAnalyser,
       
   113     CDesCArrayFlat* aZhuyinSymbols, 
       
   114     CDesCArrayFlat* aChineseDisped,
       
   115     CDesCArrayFlat* aCandidateList,
       
   116     TDes& aOutDes,
       
   117     TBool& aPhraseCreation)
       
   118     {
       
   119     TBuf<KMaxBufLen> buf;
       
   120     Convert2Des( aZhuyinSymbols, buf );
       
   121     RPointerArray<HBufC> list;
       
   122     TCleanupItem cleanitem( CleanArray, &list );
       
   123     CleanupStack::PushL( cleanitem );
       
   124     
       
   125     HBufC* retString = aPtiengine->GetCandidatesByInputString( buf, list,
       
   126         EFalse );
       
   127     if ( buf.Length() == 0 )
       
   128         {
       
   129         Convert2Des( aChineseDisped, aOutDes );
       
   130         delete retString;
       
   131         aCandidateList->Reset();
       
   132         aCandidateList->Compress();
       
   133         CleanupStack::PopAndDestroy();
       
   134         return ETrue;
       
   135         }
       
   136     
       
   137     CleanupStack::PushL( retString );
       
   138     TBool valid = aAnalyser->AnalyzeL( buf );
       
   139        
       
   140     HBufC* showbuf = aAnalyser->LastAnalysisResult();
       
   141     aCandidateList->Reset();
       
   142     if ( retString->Compare( buf ) != 0 || 
       
   143         retString->Compare( buf ) == 0 && aChineseDisped->Count() != 0 )
       
   144         {
       
   145         // Display the candidates for the 1st chinese character group.
       
   146         aPhraseCreation = ETrue;
       
   147         list.ResetAndDestroy();
       
   148         TInt length = 0;
       
   149         TInt i = 0;
       
   150         for ( i = 0; i < showbuf->Length(); i++ )
       
   151             {
       
   152             if ( showbuf->Mid(i, 1).Compare(KFullStop) == 0 )
       
   153                 {
       
   154                 length = i;
       
   155                 break;
       
   156                 }
       
   157             if ( showbuf->Mid(i, 1).Compare(KToneMark0) == 0 
       
   158                 || showbuf->Mid(i, 1).Compare(KToneMark1) == 0 
       
   159                 || showbuf->Mid(i, 1).Compare(KToneMark2) == 0
       
   160                 || showbuf->Mid(i, 1).Compare(KToneMark3) == 0
       
   161                 || showbuf->Mid(i, 1).Compare(KToneMark4) == 0 )
       
   162                 {
       
   163                 length = i + 1;
       
   164                 break;
       
   165                 }
       
   166             }
       
   167         if ( i == showbuf->Length())
       
   168             {
       
   169             length = i;
       
   170             }
       
   171         CleanupStack::PopAndDestroy( retString );
       
   172         TPtrC tmp = showbuf->Left( length );
       
   173         retString = aPtiengine->GetCandidatesByInputString( tmp, list, EFalse );
       
   174         CleanupStack::PushL( retString );
       
   175         for (TInt ii = 0; ii < list.Count(); ii++ )
       
   176             {
       
   177             aCandidateList->AppendL( *(list[ii]) );
       
   178             }
       
   179         }
       
   180     else
       
   181         {
       
   182         aPhraseCreation = EFalse;
       
   183         for (TInt i = 0; i < list.Count(); i++ )
       
   184             {
       
   185             aCandidateList->AppendL( *(list[i]) );
       
   186             }
       
   187         }
       
   188     CleanupStack::Pop( retString );
       
   189     CleanupStack::PopAndDestroy();
       
   190     Convert2Des( aChineseDisped, aOutDes );
       
   191     if ( !valid )
       
   192         {
       
   193         aOutDes.Append(buf);
       
   194         }
       
   195     else
       
   196         aOutDes.Append(*showbuf);
       
   197     delete retString;
       
   198     
       
   199     if ( aZhuyinSymbols->Count() > 0
       
   200         && ( aZhuyinSymbols->MdcaPoint(0).Compare(KToneMark0) == 0 
       
   201         || aZhuyinSymbols->MdcaPoint(0).Compare(KToneMark1) == 0 
       
   202         || aZhuyinSymbols->MdcaPoint(0).Compare(KToneMark2) == 0
       
   203         || aZhuyinSymbols->MdcaPoint(0).Compare(KToneMark3) == 0
       
   204         || aZhuyinSymbols->MdcaPoint(0).Compare(KToneMark4) == 0 ) )
       
   205         {
       
   206         valid = EFalse;
       
   207         }
       
   208     
       
   209     if ( aCandidateList->Count() != 0 && aOutDes.Length() == 1 )
       
   210         {
       
   211         valid = ETrue;
       
   212         }
       
   213     return valid;
       
   214     }
       
   215 
       
   216 // ---------------------------------------------------------
       
   217 // Commit a chinese character from candidate list.
       
   218 // ---------------------------------------------------------
       
   219 //
       
   220 TBool TZhuyinSymbolsMethods::AppendChineseChar( 
       
   221     const TDes& aChinese,
       
   222     CAknFepZhuyinAnalyser* aAnalyser,
       
   223     CDesCArrayFlat* aZhuyinSymbols, 
       
   224     CDesCArrayFlat* aChineseDisped,
       
   225     CDesCArrayFlat* aZhuyinSymbolsForChinese )
       
   226     {
       
   227     TBuf<KMaxBufLen> buf;
       
   228     buf.Copy(aChinese);
       
   229     if ( aChineseDisped->Count() >= KMaxChineseCount )
       
   230         {
       
   231         return ETrue;
       
   232         }
       
   233     buf.Zero();
       
   234     Convert2Des(aZhuyinSymbols, buf);
       
   235     TBool valid = EFalse;
       
   236     TRAPD( err, valid = aAnalyser->AnalyzeL( buf ) );
       
   237     if ( KErrNone != err )
       
   238         {
       
   239         return EFalse;
       
   240         }
       
   241     
       
   242     if ( !valid )
       
   243         {
       
   244         return EFalse;
       
   245         }
       
   246     else
       
   247         {
       
   248         HBufC* showbuf = aAnalyser->LastAnalysisResult();
       
   249         TInt length = 0;
       
   250         TInt i = 0;
       
   251         for ( i = 0; i < showbuf->Length(); i++ )
       
   252             {
       
   253             if ( showbuf->Mid(i, 1).Compare(KFullStop) == 0 )
       
   254                 {
       
   255                 length = i;
       
   256                 break;
       
   257                 }
       
   258             if ( showbuf->Mid(i, 1).Compare(KToneMark0) == 0 
       
   259                 || showbuf->Mid(i, 1).Compare(KToneMark1) == 0 
       
   260                 || showbuf->Mid(i, 1).Compare(KToneMark2) == 0
       
   261                 || showbuf->Mid(i, 1).Compare(KToneMark3) == 0
       
   262                 || showbuf->Mid(i, 1).Compare(KToneMark4) == 0 )
       
   263                 {
       
   264                 length = i + 1;
       
   265                 break;
       
   266                 }
       
   267             }
       
   268         if ( i == showbuf->Length())
       
   269             {
       
   270             length = i;
       
   271             }
       
   272         // update arrays
       
   273         TRAPD( ret, aChineseDisped->AppendL(aChinese) );
       
   274         if ( KErrNone != ret )
       
   275             {
       
   276             return EFalse;
       
   277             }
       
   278         
       
   279         TBuf<KMaxBufLen> tmpbuf;
       
   280         for ( TInt ii = 0; ii < length; ii++ )
       
   281             {
       
   282             tmpbuf.Append(aZhuyinSymbols->MdcaPoint(ii));
       
   283             }
       
   284         TRAPD( ret1, aZhuyinSymbolsForChinese->AppendL(tmpbuf) );
       
   285         if ( KErrNone != ret1 )
       
   286             {
       
   287             return EFalse;
       
   288             }
       
   289         aZhuyinSymbols->Delete(0, length);
       
   290         if ( aChineseDisped->Count() >= KMaxChineseCount 
       
   291             || aZhuyinSymbols->Count() == 0 )
       
   292             {
       
   293             return ETrue;
       
   294             }
       
   295         else
       
   296             {
       
   297             return EFalse;
       
   298             }
       
   299         }
       
   300     }
       
   301 
       
   302 // ---------------------------------------------------------
       
   303 // append the zhuyin symbol to the zhuyin array form zhuyin buf.
       
   304 // ---------------------------------------------------------
       
   305 //
       
   306 void TZhuyinSymbolsMethods::GetZhuyinSymbolsL( 
       
   307     CDesCArrayFlat* aZhuyinSymbols,  
       
   308     const TDesC& aInputBuf )
       
   309     {
       
   310     for ( TInt i = 0; i < aInputBuf.Length(); i++ )
       
   311         {
       
   312         aZhuyinSymbols->InsertL(i, aInputBuf.Mid(i,1));
       
   313         }
       
   314     }
       
   315 
       
   316 // ---------------------------------------------------------
       
   317 // Delete a chinese character from aChineseDisped array
       
   318 // and update related array in the meantime.
       
   319 // ---------------------------------------------------------
       
   320 //
       
   321 TBool TZhuyinSymbolsMethods::DeleteChineseChar(
       
   322     CDesCArrayFlat* aZhuyinSymbols, 
       
   323     CDesCArrayFlat* aChineseDisped,
       
   324     CDesCArrayFlat* aZhuyinSymbolsForChinese )
       
   325     {
       
   326     if ( aChineseDisped->Count() == 0 )
       
   327         {
       
   328         return EFalse;
       
   329         }
       
   330     TBuf<KMaxBufLen> buf;
       
   331     buf.Append( aZhuyinSymbolsForChinese->MdcaPoint( 
       
   332         aZhuyinSymbolsForChinese->Count() - 1 ) );
       
   333     
       
   334     TRAPD( ret, GetZhuyinSymbolsL( aZhuyinSymbols, buf ) );
       
   335     if ( KErrNone != ret )
       
   336         {
       
   337         return EFalse;
       
   338         }
       
   339     
       
   340     aChineseDisped->Delete(aChineseDisped->Count() - 1);
       
   341     aZhuyinSymbolsForChinese->Delete(aZhuyinSymbolsForChinese->Count() - 1);
       
   342     return ETrue;
       
   343     }
       
   344 
       
   345 // ---------------------------------------------------------
       
   346 // Delete a chinese character from aChineseDisped array
       
   347 // and update related array in the meantime.
       
   348 // ---------------------------------------------------------
       
   349 //
       
   350 TBool TZhuyinSymbolsMethods::DeleteZhuyinSymbol(
       
   351     CDesCArrayFlat* aZhuyinSymbols, 
       
   352     TInt aKeyStroke
       
   353     )
       
   354     {
       
   355     TInt delCount = 0;
       
   356     if ( aKeyStroke == aZhuyinSymbols->Count() )
       
   357         {
       
   358         delCount++;
       
   359         }
       
   360     else if ( aKeyStroke < aZhuyinSymbols->Count() )
       
   361         {
       
   362         delCount++;
       
   363         }
       
   364     else
       
   365         {
       
   366         return EFalse;
       
   367         }
       
   368     aZhuyinSymbols->Delete( aKeyStroke-1, delCount );
       
   369     return ETrue;
       
   370     }
       
   371 
       
   372 // ---------------------------------------------------------
       
   373 // Refresh the cursor position according to the input parameters.
       
   374 // ---------------------------------------------------------
       
   375 //
       
   376 TInt TZhuyinSymbolsMethods::RefreshCursorPos (
       
   377     TInt aChineseNum,
       
   378     const TDes& aShownContent,
       
   379     TInt aKeyStrokePos )
       
   380     {
       
   381     TInt cursorPos = aChineseNum;
       
   382     TInt index = 0;
       
   383     TInt count = 0;
       
   384     if ( aShownContent.Length() - aChineseNum <= 0 )
       
   385         {
       
   386         return aChineseNum;
       
   387         }
       
   388     
       
   389     if ( 0 == aKeyStrokePos )
       
   390         {
       
   391         return aChineseNum;
       
   392         }
       
   393     
       
   394     TPtrC zhuyinShown = aShownContent.Right( aShownContent.Length() - 
       
   395         aChineseNum );
       
   396     
       
   397     if ( zhuyinShown.Length() == 0 )
       
   398         {
       
   399         return aChineseNum;
       
   400         }
       
   401     
       
   402     for ( TInt i = 0; i < zhuyinShown.Length(); ++i )
       
   403         {
       
   404         if ( 0 != zhuyinShown.Mid( i, 1 ).Compare( KFullStop ) )
       
   405             {
       
   406             count = count + 1;
       
   407             }
       
   408         if ( aKeyStrokePos == count )
       
   409             {
       
   410             index = i + 1;
       
   411             break;
       
   412             }
       
   413         }
       
   414 
       
   415     return cursorPos + index;
       
   416     
       
   417     }
       
   418 
       
   419 // ---------------------------------------------------------
       
   420 // Get the number of zhuyin symbol group.
       
   421 // ---------------------------------------------------------
       
   422 //
       
   423 TInt TZhuyinSymbolsMethods::GetChineseCount(const TDes& aShown)
       
   424     {
       
   425     TInt i = 0;
       
   426     TInt count = 0;
       
   427     for ( i = 0; i < aShown.Length(); i++ )
       
   428         {
       
   429         if ( aShown.Mid(i, 1).Compare(KFullStop) == 0
       
   430             || aShown.Mid(i, 1).Compare(KToneMark0) == 0 
       
   431             || aShown.Mid(i, 1).Compare(KToneMark1) == 0 
       
   432             || aShown.Mid(i, 1).Compare(KToneMark2) == 0
       
   433             || aShown.Mid(i, 1).Compare(KToneMark3) == 0
       
   434             || aShown.Mid(i, 1).Compare(KToneMark4) == 0 )
       
   435             {
       
   436             count++;
       
   437             }
       
   438         }
       
   439     if ( aShown.Right(1).Compare(KToneMark0) == 0 
       
   440         || aShown.Right(1).Compare(KToneMark1) == 0 
       
   441         || aShown.Right(1).Compare(KToneMark2) == 0
       
   442         || aShown.Right(1).Compare(KToneMark3) == 0
       
   443         || aShown.Right(1).Compare(KToneMark4) == 0 )
       
   444         {
       
   445         return count;
       
   446         }
       
   447     else
       
   448         {
       
   449         return count + 1;
       
   450         }
       
   451     }
       
   452 
       
   453 // ---------------------------------------------------------
       
   454 // Whether the aFirstPhrase is a Zhuyin symbol or not.
       
   455 // ---------------------------------------------------------
       
   456 //
       
   457 TBool TZhuyinSymbolsMethods::IsZhuyinSymbol( const TDesC& aFirstPhrase )
       
   458     {
       
   459     if ( aFirstPhrase.Length() > 1 )
       
   460         {
       
   461         return EFalse;
       
   462         }
       
   463     
       
   464     if ( aFirstPhrase[0] >= KMinZhuyinUnicode &&
       
   465          aFirstPhrase[0] <= KMaxZhuyinUnicode )
       
   466         {
       
   467         return ETrue;
       
   468         }
       
   469     else
       
   470         {
       
   471         return EFalse;
       
   472         }
       
   473     }
       
   474 
       
   475 // ---------------------------------------------------------
       
   476 // Whether the aFirstPhrase is a Zhuyin symbol or not.
       
   477 // ---------------------------------------------------------
       
   478 //
       
   479 TBool TZhuyinSymbolsMethods::IsZhuyinSpelling(
       
   480                                     const TDesC& /*aSymbol*/,
       
   481                                     const TInt /*index*/,
       
   482                                     CDesCArrayFlat* /*aZhuyinSymbols*/ ) 
       
   483     {
       
   484     return ETrue;
       
   485     }
       
   486 
       
   487 // End of file