textinput/peninputhwrboxjp/src/peninputhwrboxrecognizer.cpp
changeset 0 eb1f2e154e89
equal deleted inserted replaced
-1:000000000000 0:eb1f2e154e89
       
     1 /*
       
     2 * Copyright (c) 2009 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:  Implementation of peninput handwriting recognition
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <AknFepGlobalEnums.h>  // ELowerCase
       
    20 #include <aknfeppeninputenums.h>    // ERangeHiraganaKanji, ...
       
    21 #include <PtiHwrRecognizer.h>
       
    22 #include <PtiEngine.h>
       
    23 
       
    24 #include "peninputhwrboxrecognizer.h"
       
    25 
       
    26 // constant definition
       
    27 const TInt KInvalidStrokeEndMarkX = -65536;
       
    28 const TInt KInvalidStrokeEndMarkY = -65536;
       
    29 
       
    30 // ---------------------------------------------------------
       
    31 // Symbian constructor
       
    32 // ---------------------------------------------------------
       
    33 //
       
    34 CPeninputHwrBoxRecognizer* CPeninputHwrBoxRecognizer::NewL(CPtiEngine* aPtiEngine)
       
    35     {
       
    36     CPeninputHwrBoxRecognizer* self = new (ELeave) CPeninputHwrBoxRecognizer();
       
    37 
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL(aPtiEngine);
       
    40     CleanupStack::Pop(self);//self
       
    41 
       
    42     return self;
       
    43     }
       
    44 
       
    45 // ---------------------------------------------------------
       
    46 // destructor
       
    47 // ---------------------------------------------------------
       
    48 //
       
    49 CPeninputHwrBoxRecognizer::~CPeninputHwrBoxRecognizer()
       
    50     {
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------
       
    54 // Do recoginize by engine
       
    55 // ---------------------------------------------------------
       
    56 //
       
    57 void CPeninputHwrBoxRecognizer::DoRecognize(const RArray<TPoint>& aTraceData, RPointerArray<HBufC>& aCandidates, RArray<TInt>& aRangeStartPos)
       
    58     {
       
    59     if (iRecognizer != NULL)
       
    60         {
       
    61         aCandidates.ResetAndDestroy();
       
    62 
       
    63         TInt primaryCount = iRecognizer->Recognize(aTraceData, aCandidates);
       
    64 
       
    65         // filter recognized candidate, remove unnecessary gesture when only number is permitted
       
    66 
       
    67         // filter recognized candidate, set start position for all ranges
       
    68         aRangeStartPos.Reset();
       
    69 
       
    70         _LIT(KSeparator,"\0");
       
    71         TPtrC ptr(KSeparator);
       
    72 
       
    73         if (primaryCount > 0)
       
    74             {
       
    75             // the start position of primary range is 0
       
    76             aRangeStartPos.Append(0);
       
    77 
       
    78             // remove uncessary primary candidate
       
    79             TInt removePos = iTotalCandidateNum - (iRangeCount - 1);
       
    80             TInt removeCount = primaryCount - removePos;
       
    81 
       
    82             if (removePos >= 0)
       
    83                 {
       
    84                 for (TInt i = 0; i < removeCount; i++)
       
    85                     {
       
    86                     delete aCandidates[removePos];
       
    87                     aCandidates.Remove(removePos);
       
    88                     }
       
    89                 }
       
    90 
       
    91             // remove range separator
       
    92             for (TInt i=0; i<aCandidates.Count(); i++)
       
    93                 {
       
    94                 if (aCandidates[i]->CompareC(ptr) == 0)
       
    95                     {
       
    96                     // remove separator from candidate list
       
    97                     delete aCandidates[i];
       
    98                     aCandidates.Remove(i);
       
    99                     // the start position of next range is i
       
   100                     aRangeStartPos.Append(i);
       
   101                     }
       
   102                 }
       
   103             }
       
   104         }
       
   105     }
       
   106 
       
   107 
       
   108 // ---------------------------------------------------------
       
   109 // Set primary and auxiliary ranges for hwr engine
       
   110 // ---------------------------------------------------------
       
   111 //
       
   112 void CPeninputHwrBoxRecognizer::SetRanges(const RArray<TInt>& aPermittedRanges)
       
   113     {
       
   114     if ((aPermittedRanges.Count() > 0) && (iRecognizer != NULL))
       
   115         {
       
   116         iPremaryRange = aPermittedRanges[0];
       
   117         iRangeCount = aPermittedRanges.Count();
       
   118 
       
   119         TRecognitionRange range;
       
   120 
       
   121         SetRecognitionRange(aPermittedRanges[0], range);
       
   122         iRecognizer->SetRange(range);
       
   123 
       
   124         // set auxiliary ranges for hwr engine
       
   125         for (TInt i=1; i<aPermittedRanges.Count(); i++)
       
   126             {
       
   127             SetRecognitionRange(aPermittedRanges[i], range);
       
   128 
       
   129             iRecognizer->AddAuxiliaryRange(range);
       
   130             }
       
   131 
       
   132         SetCase(iCase);
       
   133         }
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------
       
   137 // Set case
       
   138 // ---------------------------------------------------------
       
   139 //
       
   140 void CPeninputHwrBoxRecognizer::SetCase(TInt aCase)
       
   141     {
       
   142     // set letter to lower first when LowerCase
       
   143     // set letter to upper first when UpperCase and TextCase
       
   144     if (iRecognizer != NULL)
       
   145         {
       
   146         if (ECaseLower == aCase || ECaseInverseText == aCase)
       
   147             {
       
   148             iRecognizer->SetFirstLetterOrder(ELowerFirst);
       
   149             }
       
   150         else if (ECaseUpper == aCase || ECaseText == aCase)
       
   151             {
       
   152             iRecognizer->SetFirstLetterOrder(EUpperFirst);
       
   153             }
       
   154 
       
   155         iCase = aCase;
       
   156         }
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------
       
   160 // Set number mode for hwr engine
       
   161 // ---------------------------------------------------------
       
   162 //
       
   163 void CPeninputHwrBoxRecognizer::SetNumberMode(const TAknEditorNumericKeymap& aNumberMode)
       
   164     {
       
   165     if (iRecognizer != NULL)
       
   166         {
       
   167         iRecognizer->SetNumberMode(aNumberMode);
       
   168         }
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------
       
   172 // Get stroke end mark from hwr engine
       
   173 // ---------------------------------------------------------
       
   174 //
       
   175 TPoint CPeninputHwrBoxRecognizer::StrokeEndMark() const
       
   176     {
       
   177     if (iRecognizer != NULL)
       
   178         {
       
   179         return iRecognizer->StrokeEndMark();
       
   180         }
       
   181     else
       
   182         {
       
   183         return TPoint(KInvalidStrokeEndMarkX, KInvalidStrokeEndMarkY);
       
   184         }
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------
       
   188 // Set primary candidate num for hwr engine
       
   189 // ---------------------------------------------------------
       
   190 //
       
   191 TInt CPeninputHwrBoxRecognizer::SetPrimaryCandidateNum(TInt aNum)
       
   192     {
       
   193     if (iRecognizer != NULL)
       
   194         {
       
   195         return iRecognizer->SetCandidateNum(aNum);
       
   196         }
       
   197     else
       
   198         {
       
   199         return KErrGeneral;
       
   200         }
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------
       
   204 // Set total candidate num that should be shown
       
   205 // ---------------------------------------------------------
       
   206 //
       
   207 TInt CPeninputHwrBoxRecognizer::SetCandidateNum(TInt aNum)
       
   208     {
       
   209     if (aNum > 0)
       
   210         {
       
   211         iTotalCandidateNum = aNum;
       
   212         return KErrNone;
       
   213         }
       
   214     else
       
   215         {
       
   216         return KErrGeneral;
       
   217         }
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------
       
   221 // Set language
       
   222 // ---------------------------------------------------------
       
   223 //
       
   224 void CPeninputHwrBoxRecognizer::SetLanguageL(TInt aLanguage)
       
   225     {
       
   226 
       
   227     iLanguage = aLanguage;
       
   228 
       
   229     // select suitable hwr recognizer according to language
       
   230     iRecognizer = iPtiEngine->GetHwrRecognizerL(TLanguage(iLanguage));
       
   231     }
       
   232 
       
   233 // --------------------------------------------------------------------------
       
   234 // CPeninputHwrBoxRecognizer::SetInputAreaSize
       
   235 // (other items were commented in a header)
       
   236 // --------------------------------------------------------------------------
       
   237 //
       
   238 void CPeninputHwrBoxRecognizer::SetInputAreaSize( TSize aSize )
       
   239     {
       
   240     if ( iRecognizer )
       
   241         {
       
   242         iRecognizer->SetInputAreaSize( aSize );
       
   243         }
       
   244     }
       
   245 
       
   246 // ---------------------------------------------------------
       
   247 // C++ constructor
       
   248 // ---------------------------------------------------------
       
   249 //
       
   250 CPeninputHwrBoxRecognizer::CPeninputHwrBoxRecognizer()
       
   251     {
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------
       
   255 // Second phase constructor
       
   256 // ---------------------------------------------------------
       
   257 //
       
   258 void CPeninputHwrBoxRecognizer::ConstructL(CPtiEngine* aPtiEngine)
       
   259     {
       
   260     if (!aPtiEngine)
       
   261         {
       
   262         User::Leave(KErrArgument);
       
   263         }
       
   264     iPtiEngine = aPtiEngine;
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------
       
   268 // Set recognition range for hwr engine
       
   269 // ---------------------------------------------------------
       
   270 //
       
   271 void CPeninputHwrBoxRecognizer::SetRecognitionRange(const TInt aRange, TRecognitionRange& aRecognitionRange) const
       
   272     {
       
   273     aRecognitionRange.iLanguage = TLanguage(iLanguage);
       
   274 
       
   275     switch (aRange)
       
   276         {
       
   277         case ERangeNative:
       
   278         case ERangeHiraganaKanji:
       
   279             aRecognitionRange.iSubRange = EPtiRangeHiraganaKanji;
       
   280             break;
       
   281         case ERangeKatakana:
       
   282             aRecognitionRange.iSubRange = EPtiRangeKatakana;
       
   283             break;
       
   284         case ERangeFullWidthKatakana:
       
   285             aRecognitionRange.iSubRange = EPtiRangeFullWidthKatakana;
       
   286             break;
       
   287         case ERangeFullWidthEnglish:
       
   288             aRecognitionRange.iSubRange = EPtiRangeFullWidthEnglish;
       
   289             break;
       
   290         case ERangeFullWidthNumeric:
       
   291             aRecognitionRange.iSubRange = EPtiRangeFullWidthNumeric;
       
   292             break;
       
   293         case ERangeHiraganaOnly:
       
   294             aRecognitionRange.iSubRange = EPtiRangeHiraganaOnly;
       
   295             break;
       
   296         case ERangeEnglish:
       
   297             aRecognitionRange.iSubRange = EPtiRangeLatin;
       
   298             break;
       
   299         case ERangeNumber:
       
   300             aRecognitionRange.iSubRange = EPtiRangeNumber;
       
   301             break;
       
   302         case ERangeSymbol:
       
   303             if (iPremaryRange == ERangeEnglish)
       
   304                 {
       
   305                 aRecognitionRange.iLanguage = ELangEnglish;
       
   306                 }
       
   307             else
       
   308                 {
       
   309                 aRecognitionRange.iLanguage = TLanguage(iLanguage);
       
   310                 }
       
   311             aRecognitionRange.iSubRange = EPtiRangeSymbol;
       
   312             break;
       
   313         default:
       
   314             break;
       
   315         }
       
   316     }
       
   317 
       
   318 //End Of File