Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5389 lines
153 KiB

  1. // FILE: WispApis.c
  2. //
  3. #include <stdlib.h>
  4. #include "volcanop.h"
  5. #include "RecTypes.h"
  6. #include "RecApis.h"
  7. #include <Limits.h>
  8. #include <strsafe.h>
  9. #include "TpcError.h"
  10. #include "TpgHandle.h"
  11. #include "res.h"
  12. //#define ENABLE_CONFIDENCE_LEVEL
  13. #define LARGE_BREAKS 500
  14. // definitions of possible handle types that WISP used
  15. #define TPG_HRECOCONTEXT (1)
  16. #define TPG_HRECOGNIZER (2)
  17. #define TPG_HRECOALT (3)
  18. //Why cannot we include penwin.h???? I have to redefine everything I need....
  19. #define SYV_UNKNOWN 0x00000001L
  20. BOOL SymbolToCharacterW(SYV *pSyv, int cSyv, WCHAR *wsz, int *pConv);
  21. // If this pointer is non-NULL, then free input is enabled.
  22. extern BBOX_PROB_TABLE *g_pProbTable;
  23. // String identifying which language is loaded
  24. extern wchar_t *g_szRecognizerLanguage;
  25. extern HINSTANCE g_hInstanceDllCode;
  26. #define NUMBER_OF_ALTERNATES 10
  27. #define TAB_STROKE_INC 30
  28. // {7DFE11A7-FB5D-4958-8765-154ADF0D833F}
  29. static const GUID GUID_CONFIDENCELEVEL =
  30. { 0x7dfe11a7, 0xfb5d, 0x4958, { 0x87, 0x65, 0x15, 0x4a, 0xdf, 0x0d, 0x83, 0x3f } };
  31. // {8CC24B27-30A9-4b96-9056-2D3A90DA0727}
  32. static const GUID GUID_LINEMETRICS =
  33. { 0x8cc24b27, 0x30a9, 0x4b96, { 0x90, 0x56, 0x2d, 0x3a, 0x90, 0xda, 0x07, 0x27 } };
  34. // {6D4087D7-61D2-495f-9293-5B7B1C3FCEAB}
  35. static const CLSID JPN_CLSID =
  36. { 0x6D4087D7, 0x61D2, 0x495f, { 0x92, 0x93, 0x5B, 0x7B, 0x1C, 0x3F, 0xCE, 0xAB } };
  37. static const CLSID KOR_CLSID =
  38. { 0x6D5087D7, 0x61D2, 0x495f, { 0x92, 0x93, 0x5B, 0x7B, 0x1C, 0x3F, 0xCE, 0xAB } };
  39. static const CLSID CHS_CLSID =
  40. { 0x6D6087D7, 0x61D2, 0x495f, { 0x92, 0x93, 0x5B, 0x7B, 0x1C, 0x3F, 0xCE, 0xAB } };
  41. static const CLSID CHT_CLSID =
  42. { 0x6D7087D7, 0x61D2, 0x495f, { 0x92, 0x93, 0x5B, 0x7B, 0x1C, 0x3F, 0xCE, 0xAB } };
  43. //
  44. // Definitions of strucure which pointers are used
  45. // to define the WISP handles (HRECOGNIZER, HRECOCONTEXT
  46. // HRECOALTERNATE)
  47. ////////////////////////////////////////////////////////
  48. // This is the structure used for the WISP recognizer
  49. // There is no data, because we have nothing to store
  50. struct WispRec
  51. {
  52. long unused;
  53. };
  54. // This is the structure for WISP alternates
  55. // It contains an array of column used in the
  56. // lattice and an array of indexes used in
  57. // those columns.
  58. // We alse cache the reco context for which
  59. // this alternate is valis, the length of the
  60. // string this alternate corresponds to and
  61. // the original RECO_RANGE this alternate was
  62. // produced from (in a call to GetAlternateList
  63. // or other)
  64. struct WispAlternate
  65. {
  66. HRECOCONTEXT hrc;
  67. int *pIndexInColumn;
  68. int *pColumnIndex;
  69. int iNumberOfColumns;
  70. int iLength;
  71. RECO_RANGE OriginalRecoRange;
  72. };
  73. // This is the WISP structure for the reco context.
  74. // It contains information on the guide used, the
  75. // CAC modem the number of strokes currently
  76. // entered, the context (prefix)
  77. // It also contains the handle to the HWX reco
  78. // context
  79. // We store the lattice so that we not need to
  80. // recreate it every time we are asked for it
  81. struct WispContext
  82. {
  83. HRC hrc;
  84. RECO_GUIDE *pGuide;
  85. ULONG uiGuideIndex;
  86. BOOL bIsBoxed;
  87. BOOL bIsCAC;
  88. BOOL bCACEndInk;
  89. ULONG iCACMode;
  90. UINT uAbort;
  91. ULONG ulCurrentStrokeCount;
  92. BOOL bHasTextContext; // Whether any context has been set
  93. WCHAR *wszBefore; // Context before ink
  94. WCHAR *wszAfter; // Context after ink
  95. DWORD dwFlags; // Flags
  96. WCHAR *wszFactoid; // Factoid
  97. // Lattice for the automation code, with associated data structures
  98. RECO_LATTICE *pLattice;
  99. RECO_LATTICE_PROPERTY *pLatticeProperties;
  100. BYTE *pLatticePropertyValues;
  101. RECO_LATTICE_PROPERTY **ppLatticeProperties;
  102. };
  103. // Structure for the alternate list recursive call
  104. ////////////////////////////////////////////////
  105. typedef struct tagAltRank
  106. {
  107. struct WispAlternate *wispalt;
  108. FLOAT fScore;
  109. struct tagAltRank *next;
  110. BOOL bCurrentPath;
  111. } AltRank;
  112. typedef struct tagAltRankList
  113. {
  114. AltRank *pFirst;
  115. AltRank *pLast;
  116. ULONG ulSize;
  117. } AltRankList;
  118. typedef struct tagDiffBreakElement
  119. {
  120. int iColumn;
  121. int iIndex;
  122. struct tagDiffBreakElement *pNext;
  123. } DiffBreakElement;
  124. typedef struct tagDifBreakList
  125. {
  126. int iColumnCount;
  127. DiffBreakElement *pFirst;
  128. float score;
  129. BOOL bCurrentPath;
  130. } DifBreakList;
  131. typedef struct tagDifBreakAltStruct
  132. {
  133. VRC *vrc; // the recognizer data structure
  134. int iFirstStroke; // the first stroke in the original alternate
  135. ULONG ulMax; // Max alternates that we want to return
  136. int iLastChar; // This is to put in the original reco range of the alternate
  137. int iFirstChar; // This is to put in the original reco range of the alternate
  138. AltRankList *paltRankList; // List of Alternates
  139. int iMode; // Segmentation mode (DIFF_BREAK, ...)
  140. } DifBreakAltStruct;
  141. /////////////////////////////////////////////////////
  142. // Declare the GUIDs and consts of the Packet description
  143. /////////////////////////////////////////////////////
  144. const GUID g_guidx ={ 0x598a6a8f, 0x52c0, 0x4ba0, { 0x93, 0xaf, 0xaf, 0x35, 0x74, 0x11, 0xa5, 0x61 } };
  145. const GUID g_guidy = { 0xb53f9f75, 0x04e0, 0x4498, { 0xa7, 0xee, 0xc3, 0x0d, 0xbb, 0x5a, 0x90, 0x11 } };
  146. const PROPERTY_METRICS g_DefaultPropMetrics = { LONG_MIN, LONG_MAX, PROPERTY_UNITS_DEFAULT, 1.0 };
  147. /////////////////////////////////////////////////////
  148. // Helper function to bubble sort an array
  149. /////////////////////////////////////////////////////
  150. // I use a bubble sort because most likely if the
  151. // array is not already sorted, we probably have one or
  152. // two inversion. This is caused by the fact that
  153. // people usually write the letters in a word in
  154. // the correct order
  155. BOOL SlowSort(ULONG *pTab, ULONG ulSize)
  156. {
  157. ULONG i, j, temp;
  158. BOOL bPermut;
  159. // Stupid bubble sort
  160. for (i = 0; i<ulSize; i++)
  161. {
  162. bPermut = FALSE;
  163. for (j = 0; j < ulSize-1-i; j++)
  164. {
  165. if (pTab[j] > pTab[j+1])
  166. {
  167. bPermut = TRUE;
  168. temp = pTab[j];
  169. pTab[j] = pTab[j+1];
  170. pTab[j+1] = temp;
  171. }
  172. }
  173. if (!bPermut) return TRUE;
  174. }
  175. return TRUE;
  176. }
  177. /////////////////////////////////////////////////////
  178. // Implementation of the Wisp Reco Apis
  179. /////////////////////////////////////////////////////
  180. // CreateRecognizer
  181. // Returns a recognizer handle to the recognizer
  182. // corresponding to the passed CLSID. In the case
  183. // of this dll, we only support one CLSID so we will
  184. // not even check for the value of the clsid
  185. // (even if the clsid is null)
  186. //
  187. // Parameter:
  188. // pCLSID [in] : The pointer to the CLSID
  189. // that determines what recognizer we want
  190. // phrec [out] : The address of the returned recognizer
  191. // handle.
  192. //////////////////////////////////////////////////////////////////////
  193. HRESULT WINAPI CreateRecognizer(CLSID *pCLSID, HRECOGNIZER *phrec)
  194. {
  195. struct WispRec *pRec;
  196. // We might want to make NULL illegal later.
  197. if (pCLSID != NULL && IsBadReadPtr(pCLSID, sizeof(CLSID)))
  198. {
  199. return E_POINTER;
  200. }
  201. // validate the pointer
  202. if (IsBadWritePtr(phrec, sizeof(HRECOGNIZER)))
  203. {
  204. return E_POINTER;
  205. }
  206. // initialize the east asian recognizers
  207. #ifdef USE_RESOURCES
  208. if (!HwxConfig())
  209. {
  210. return E_FAIL;
  211. }
  212. #endif
  213. // We might want to make NULL illegal later.
  214. if (pCLSID != NULL)
  215. {
  216. if (wcscmp(g_szRecognizerLanguage, L"JPN") == 0 &&
  217. !IsEqualCLSID(pCLSID, &JPN_CLSID))
  218. {
  219. return E_INVALIDARG;
  220. }
  221. if (wcscmp(g_szRecognizerLanguage, L"CHS") == 0 &&
  222. !IsEqualCLSID(pCLSID, &CHS_CLSID))
  223. {
  224. return E_INVALIDARG;
  225. }
  226. if (wcscmp(g_szRecognizerLanguage, L"CHT") == 0 &&
  227. !IsEqualCLSID(pCLSID, &CHT_CLSID))
  228. {
  229. return E_INVALIDARG;
  230. }
  231. if (wcscmp(g_szRecognizerLanguage, L"KOR") == 0 &&
  232. !IsEqualCLSID(pCLSID, &KOR_CLSID))
  233. {
  234. return E_INVALIDARG;
  235. }
  236. }
  237. // We only have one CLSID per recognizer so always return an hrec...
  238. pRec = (struct WispRec*)ExternAlloc(sizeof(*pRec));
  239. if (NULL == pRec)
  240. {
  241. return E_OUTOFMEMORY;
  242. }
  243. (*phrec) = (HRECOGNIZER)CreateTpgHandle(TPG_HRECOGNIZER, pRec);
  244. if (0 == (*phrec))
  245. {
  246. ExternFree(pRec);
  247. return E_OUTOFMEMORY;
  248. }
  249. return S_OK;
  250. }
  251. // DestroyRecognizer
  252. // Destroys a recognizer handle. Free the associate memory
  253. //
  254. // Parameter:
  255. // hrec [in] : handle to the recognizer
  256. /////////////////////////////////////////////////////////////
  257. HRESULT WINAPI DestroyRecognizer(HRECOGNIZER hrec)
  258. {
  259. struct WispRec *pRec;
  260. // destroy the handle and return the corresponding pointer
  261. pRec = (struct WispRec*)DestroyTpgHandle((HANDLE)hrec, TPG_HRECOGNIZER);
  262. if (NULL == pRec)
  263. {
  264. return E_INVALIDARG;
  265. }
  266. #ifdef USE_RESOURCES
  267. if (!HwxUnconfig(TRUE))
  268. {
  269. return E_FAIL;
  270. }
  271. #endif
  272. ExternFree(pRec);
  273. return S_OK;
  274. }
  275. // GetRecoAttributes
  276. // This function returns the reco attributes corresponding
  277. // to a given recognizer. Since we only have one recognizer
  278. // type we always return the same things.
  279. //
  280. // Parameters:
  281. // hrc [in] : The handle to the recognizer we want the
  282. // the attributes for.
  283. // pRecoAttrs [out] : Address of the user allocated buffer
  284. // to hold the reco attributes.
  285. ///////////////////////////////////////////////////////////////////////////
  286. HRESULT WINAPI GetRecoAttributes(HRECOGNIZER hrec, RECO_ATTRS* pRecoAttrs)
  287. {
  288. HRESULT hr = S_OK;
  289. HRSRC hrsrc = NULL;
  290. HGLOBAL hg = NULL;
  291. LPBYTE pv = NULL;
  292. WORD wCurrentCount = 0;
  293. WORD wRecognizerCount = 0;
  294. DWORD dwRecoCapa;
  295. WORD wLanguageCount;
  296. WORD *aLanguages;
  297. WORD iLang;
  298. struct WispRec *pRec;
  299. if (IsBadWritePtr(pRecoAttrs, sizeof(RECO_ATTRS)))
  300. return E_POINTER;
  301. // Check the recognizer handle
  302. pRec = (struct WispRec*)FindTpgHandle((HANDLE)hrec, TPG_HRECOGNIZER);
  303. if (NULL == pRec)
  304. {
  305. return E_INVALIDARG;
  306. }
  307. ZeroMemory(pRecoAttrs, sizeof(RECO_ATTRS));
  308. // Update the global structure is necessary
  309. // Load the resources
  310. // Load the recognizer friendly name
  311. if (0 == LoadStringW(g_hInstanceDllCode, // handle to resource module
  312. RESID_WISP_FRIENDLYNAME, // resource identifier
  313. pRecoAttrs->awcFriendlyName, // resource buffer
  314. sizeof(pRecoAttrs->awcFriendlyName) / sizeof(WCHAR) // size of buffer
  315. ))
  316. {
  317. hr = E_FAIL;
  318. }
  319. // Load the recognizer vendor name
  320. if (0 == LoadStringW(g_hInstanceDllCode, // handle to resource module
  321. RESID_WISP_VENDORNAME, // resource identifier
  322. pRecoAttrs->awcVendorName, // resource buffer
  323. sizeof(pRecoAttrs->awcVendorName) / sizeof(WCHAR) // size of buffer
  324. ))
  325. {
  326. hr = E_FAIL;
  327. }
  328. if (SUCCEEDED(hr))
  329. {
  330. hrsrc = FindResource(g_hInstanceDllCode, // module handle
  331. (LPCTSTR)RESID_WISP_DATA, // resource name
  332. (LPCTSTR)RT_RCDATA // resource type
  333. );
  334. if (NULL == hrsrc)
  335. {
  336. // The resource is not found!
  337. ASSERT(NULL != hrsrc);
  338. hr = E_FAIL;
  339. }
  340. }
  341. if (SUCCEEDED(hr))
  342. {
  343. hg = LoadResource(
  344. g_hInstanceDllCode, // module handle
  345. hrsrc // resource handle
  346. );
  347. if (NULL == hg)
  348. {
  349. hr = E_FAIL;
  350. }
  351. }
  352. if (SUCCEEDED(hr))
  353. {
  354. pv = (LPBYTE)LockResource(
  355. hg // handle to resource
  356. );
  357. if (NULL == pv)
  358. {
  359. hr = E_FAIL;
  360. }
  361. }
  362. dwRecoCapa = *((DWORD*)pv);
  363. pv += sizeof(dwRecoCapa);
  364. wLanguageCount = *((WORD*)pv);
  365. pv += sizeof(wLanguageCount);
  366. aLanguages = (WORD*)pv;
  367. pv += wLanguageCount * sizeof(WORD);
  368. // Fill the reco attricute structure for this recognizer
  369. // Add the languages
  370. ASSERT(wLanguageCount < 64);
  371. for (iLang = 0; iLang < wLanguageCount; iLang++)
  372. {
  373. pRecoAttrs->awLanguageId[iLang] = aLanguages[iLang];
  374. }
  375. // End the list with a NULL
  376. pRecoAttrs->awLanguageId[wLanguageCount] = 0;
  377. // Add the recocapability flag
  378. pRecoAttrs->dwRecoCapabilityFlags = dwRecoCapa;
  379. return hr;
  380. }
  381. // CreateRecoContext
  382. // This function creates a reco context for a given recognizer
  383. // Since we only have one type of recognizers in this dll,
  384. // always return the same kind of reco context.
  385. //
  386. // Parameters:
  387. // hrec [in] : Handle to the recognizer we want to create a
  388. // reco context for.
  389. // phrc [out] : Pointer to the returned reco context's handle
  390. ////////////////////////////////////////////////////////////////////////
  391. HRESULT WINAPI CreateContext(HRECOGNIZER hrec, HRECOCONTEXT *phrc)
  392. {
  393. struct WispContext *pWispContext = NULL;
  394. struct WispRec *pRec;
  395. // Check the recognizer handle
  396. pRec = (struct WispRec*)FindTpgHandle((HANDLE)hrec, TPG_HRECOGNIZER);
  397. if (NULL == pRec)
  398. {
  399. return E_INVALIDARG;
  400. }
  401. // validate the pointer
  402. if (IsBadWritePtr(phrc, sizeof(HRECOCONTEXT)))
  403. {
  404. return E_POINTER;
  405. }
  406. pWispContext = (struct WispContext*)ExternAlloc(sizeof(struct WispContext));
  407. if (!pWispContext)
  408. return E_OUTOFMEMORY;
  409. pWispContext->pGuide = NULL;
  410. pWispContext->pLattice = NULL;
  411. pWispContext->pLatticeProperties = NULL;
  412. pWispContext->pLatticePropertyValues = NULL;
  413. pWispContext->ppLatticeProperties = NULL;
  414. pWispContext->bIsBoxed = FALSE;
  415. pWispContext->bIsCAC = FALSE;
  416. pWispContext->iCACMode = CAC_FULL;
  417. pWispContext->bCACEndInk = FALSE;
  418. pWispContext->uAbort = 0;
  419. pWispContext->ulCurrentStrokeCount = 0;
  420. pWispContext->hrc = NULL;
  421. pWispContext->bHasTextContext = FALSE;
  422. pWispContext->wszBefore = NULL;
  423. pWispContext->wszAfter = NULL;
  424. pWispContext->dwFlags = 0;
  425. pWispContext->wszFactoid = NULL;
  426. // create the handle
  427. *phrc = (HRECOCONTEXT)CreateTpgHandle(TPG_HRECOCONTEXT, pWispContext);
  428. if (NULL == (*phrc))
  429. {
  430. ExternFree(pWispContext);
  431. return E_OUTOFMEMORY;
  432. }
  433. return S_OK;
  434. }
  435. // creates an HRC by calling the appropriate hwx api
  436. HRESULT CreateHRCinContext(struct WispContext *pWispContext)
  437. {
  438. // are we in boxed mode
  439. if (pWispContext->bIsBoxed)
  440. {
  441. pWispContext->hrc = HwxCreate(NULL);
  442. }
  443. // free
  444. else
  445. {
  446. pWispContext->hrc = CreateCompatibleHRC(NULL, NULL);
  447. }
  448. // we failed
  449. if (pWispContext->hrc == NULL)
  450. {
  451. return E_FAIL;
  452. }
  453. // reco settings
  454. if (pWispContext->bHasTextContext)
  455. {
  456. if (!SetHwxCorrectionContext (pWispContext->hrc, pWispContext->wszBefore, pWispContext->wszAfter))
  457. {
  458. return E_FAIL;
  459. }
  460. }
  461. if (!SetHwxFlags(pWispContext->hrc, pWispContext->dwFlags))
  462. {
  463. return E_FAIL;
  464. }
  465. switch (SetHwxFactoid(pWispContext->hrc, pWispContext->wszFactoid))
  466. {
  467. case HRCR_OK:
  468. break;
  469. case HRCR_UNSUPPORTED:
  470. HwxDestroy(pWispContext->hrc);
  471. return TPC_E_INVALID_PROPERTY;
  472. case HRCR_CONFLICT:
  473. HwxDestroy(pWispContext->hrc);
  474. return TPC_E_OUT_OF_ORDER_CALL;
  475. case HRCR_ERROR:
  476. default:
  477. HwxDestroy(pWispContext->hrc);
  478. return E_FAIL;
  479. }
  480. return S_OK;
  481. }
  482. //
  483. // Frees a reco lattice
  484. //
  485. HRESULT FreeRecoLattice(struct WispContext *wisphrc)
  486. {
  487. ULONG i = 0;
  488. RECO_LATTICE *pRecoLattice = wisphrc->pLattice;
  489. if (pRecoLattice == NULL)
  490. {
  491. return S_OK;
  492. }
  493. // Free the Lattice column information
  494. if (pRecoLattice->pLatticeColumns)
  495. {
  496. // Free the array of strokes
  497. if (pRecoLattice->pLatticeColumns[0].pStrokes)
  498. {
  499. ExternFree(pRecoLattice->pLatticeColumns[0].pStrokes);
  500. }
  501. for (i = 0; i < pRecoLattice->ulColumnCount; i++)
  502. {
  503. if (pRecoLattice->pLatticeColumns[i].cpProp.apProps)
  504. {
  505. ExternFree(pRecoLattice->pLatticeColumns[i].cpProp.apProps);
  506. }
  507. }
  508. // Free the array of lattice elements
  509. if (pRecoLattice->pLatticeColumns[0].pLatticeElements)
  510. {
  511. ExternFree(pRecoLattice->pLatticeColumns[0].pLatticeElements);
  512. }
  513. ExternFree(pRecoLattice->pLatticeColumns);
  514. }
  515. // Free the the RecoLattice properties
  516. if (pRecoLattice->pGuidProperties)
  517. {
  518. ExternFree(pRecoLattice->pGuidProperties);
  519. }
  520. // Free the best result information
  521. if (pRecoLattice->pulBestResultColumns)
  522. {
  523. ExternFree(pRecoLattice->pulBestResultColumns);
  524. }
  525. if (pRecoLattice->pulBestResultIndexes)
  526. {
  527. ExternFree(pRecoLattice->pulBestResultIndexes);
  528. }
  529. if (wisphrc->pLatticeProperties)
  530. {
  531. ExternFree(wisphrc->pLatticeProperties);
  532. wisphrc->pLatticeProperties = NULL;
  533. }
  534. if (wisphrc->pLatticePropertyValues)
  535. {
  536. ExternFree(wisphrc->pLatticePropertyValues);
  537. wisphrc->pLatticePropertyValues = NULL;
  538. }
  539. if (wisphrc->ppLatticeProperties != NULL)
  540. {
  541. ExternFree(wisphrc->ppLatticeProperties);
  542. wisphrc->ppLatticeProperties = NULL;
  543. }
  544. // Free the RECO_LATTICE structure
  545. ExternFree(wisphrc->pLattice);
  546. wisphrc->pLattice = NULL;
  547. return S_OK;
  548. }
  549. // DestroyContextInternal
  550. // Destroy a reco context and free the associated memory.
  551. //
  552. // Parameters:
  553. // hrc [in] : pointer to the reco context to destroy
  554. //////////////////////////////////////////////////////////////
  555. HRESULT WINAPI DestroyContextInternal(struct WispContext *wisphrc)
  556. {
  557. HRESULT hr;
  558. // validate and destroy the handle & return the pointer
  559. if (NULL == wisphrc)
  560. {
  561. return E_INVALIDARG;
  562. }
  563. // free the contents of the context
  564. if (wisphrc->hrc)
  565. {
  566. if (wisphrc->bIsBoxed)
  567. HwxDestroy(wisphrc->hrc);
  568. else
  569. DestroyHRC(wisphrc->hrc);
  570. }
  571. if (wisphrc->pGuide)
  572. {
  573. ExternFree(wisphrc->pGuide);
  574. }
  575. if (wisphrc->pLattice)
  576. {
  577. hr = FreeRecoLattice(wisphrc);
  578. ASSERT(SUCCEEDED(hr));
  579. }
  580. wisphrc->pLattice = NULL;
  581. if (wisphrc->bHasTextContext)
  582. {
  583. ExternFree(wisphrc->wszBefore);
  584. ExternFree(wisphrc->wszAfter);
  585. }
  586. ExternFree(wisphrc->wszFactoid);
  587. ExternFree(wisphrc);
  588. return S_OK;
  589. }
  590. // DestroyContext
  591. // Destroy a reco context and free the associated memory.
  592. //
  593. // Parameters:
  594. // hrc [in] : handle to the reco context to destroy
  595. //////////////////////////////////////////////////////////////
  596. HRESULT WINAPI DestroyContext(HRECOCONTEXT hrc)
  597. {
  598. struct WispContext *wisphrc;
  599. // validate and destroy the handle & return the pointer
  600. wisphrc = (struct WispContext*)DestroyTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  601. if (NULL == wisphrc)
  602. {
  603. return E_INVALIDARG;
  604. }
  605. return DestroyContextInternal(wisphrc);
  606. }
  607. #ifdef ENABLE_CONFIDENCE_LEVEL
  608. const ULONG PROPERTIES_COUNT = 2;
  609. #else
  610. const ULONG PROPERTIES_COUNT = 1;
  611. #endif
  612. // IRecognizer::GetResultPropertyList
  613. HRESULT WINAPI GetResultPropertyList(HRECOGNIZER hrec, ULONG* pPropertyCount, GUID* pPropertyGuid)
  614. {
  615. HRESULT hr = S_OK;
  616. struct WispRec *pRec;
  617. // Check the recognizer handle
  618. pRec = (struct WispRec*)FindTpgHandle((HANDLE)hrec, TPG_HRECOGNIZER);
  619. if (NULL == pRec)
  620. {
  621. return E_INVALIDARG;
  622. }
  623. if (IsBadWritePtr(pPropertyCount, sizeof(ULONG)))
  624. {
  625. return E_POINTER;
  626. }
  627. if (!pPropertyGuid)
  628. {
  629. *pPropertyCount = PROPERTIES_COUNT; // For now we support only two GUID properties
  630. }
  631. else
  632. {
  633. // Check the array
  634. if (PROPERTIES_COUNT > *pPropertyCount)
  635. {
  636. return TPC_E_INSUFFICIENT_BUFFER;
  637. }
  638. if (IsBadWritePtr(pPropertyGuid, sizeof(GUID)*(*pPropertyCount)))
  639. {
  640. return E_POINTER;
  641. }
  642. pPropertyGuid[0] = GUID_LINEMETRICS;
  643. #ifdef ENABLE_CONFIDENCE_LEVEL
  644. pPropertyGuid[1] = GUID_CONFIDENCELEVEL;
  645. #endif
  646. *pPropertyCount = PROPERTIES_COUNT;
  647. }
  648. return hr;
  649. }
  650. // GetPreferredPacketDescription
  651. // Returns the preferred packet description for the recognizer
  652. // This is going to be x, y only for this recognizer
  653. //
  654. // Parameters:
  655. // hrec [in] : The recognizer we want the preferred
  656. // packet description for
  657. // pPacketDescription [out] : The packet description
  658. /////////////////////////////////////////////////////////////////////////////////
  659. HRESULT WINAPI GetPreferredPacketDescription(HRECOGNIZER hrec , PACKET_DESCRIPTION* pPacketDescription)
  660. {
  661. struct WispRec *pRec;
  662. // Check the recognizer handle
  663. pRec = (struct WispRec*)FindTpgHandle((HANDLE)hrec, TPG_HRECOGNIZER);
  664. if (NULL == pRec)
  665. {
  666. return E_INVALIDARG;
  667. }
  668. // validate the pointer
  669. if (IsBadWritePtr(pPacketDescription, sizeof(PACKET_DESCRIPTION)))
  670. {
  671. return E_POINTER;
  672. }
  673. // We can be called the first time with pPacketProperies
  674. // equal to NULL, just to get the size of those buffer
  675. // The second time we get called thoses buffers are allocated, so
  676. // we can fill them with the data.
  677. if (pPacketDescription->pPacketProperties)
  678. {
  679. // Make sure that the pPacketProperties is of a valid size
  680. // Set the packet size to the size of x and y
  681. pPacketDescription->cbPacketSize = 2 * sizeof(LONG);
  682. // We are only setting 2 properties (X and Y)
  683. if (pPacketDescription->cPacketProperties < 2)
  684. return TPC_E_INSUFFICIENT_BUFFER;
  685. pPacketDescription->cPacketProperties = 2;
  686. // We are not setting buttons
  687. pPacketDescription->cButtons = 0;
  688. // Make sure that the pPacketProperties is of a valid size
  689. if (IsBadWritePtr(pPacketDescription->pPacketProperties, 2 * sizeof(PACKET_PROPERTY)))
  690. {
  691. return E_POINTER;
  692. }
  693. // Fill in pPacketProperies
  694. // Add the GUID_X
  695. pPacketDescription->pPacketProperties[0].guid = g_guidx;
  696. pPacketDescription->pPacketProperties[0].PropertyMetrics = g_DefaultPropMetrics;
  697. // Add the GUID_Y
  698. pPacketDescription->pPacketProperties[1].guid = g_guidy;
  699. pPacketDescription->pPacketProperties[1].PropertyMetrics = g_DefaultPropMetrics;
  700. }
  701. else
  702. {
  703. // Just fill in the PacketDescription structure leavin NULL
  704. // pointers for the pguidButtons and pPacketProperies
  705. // Set the packet size to the size of x and y
  706. pPacketDescription->cbPacketSize = 2*sizeof(LONG);
  707. // We are only setting 2 properties (X and Y)
  708. pPacketDescription->cPacketProperties = 2;
  709. // We are not setting buttons
  710. pPacketDescription->cButtons = 0;
  711. // There are not guid buttons
  712. pPacketDescription->pguidButtons = NULL;
  713. }
  714. return S_OK;
  715. }
  716. #define FUZZ_GEN (1e-9) // general fuzz - nine decimal digits
  717. /**********************************************************************/
  718. // Convert double to int
  719. int RealToInt(double dbl)
  720. {
  721. // Add in the rounding threshold.
  722. // NOTE: The MAXWORD bias used in the floor function
  723. // below must not be combined with this line. If it
  724. // is combined the effect of FUZZ_GEN will be lost.
  725. dbl += 0.5 + FUZZ_GEN;
  726. // Truncate
  727. // The UINT_MAX bias in the floor function will cause
  728. // truncation (rounding toward minuse infinity) within
  729. // the range of a short.
  730. dbl = floor(dbl + UINT_MAX) - UINT_MAX;
  731. // Clip the result.
  732. return dbl > INT_MAX - 7 ? INT_MAX - 7 :
  733. dbl < INT_MIN + 7 ? INT_MIN + 7 : (int)dbl;
  734. }
  735. /**********************************************************************/
  736. // Transform POINT array in place
  737. void Transform(const XFORM *pXf, POINT * pPoints, ULONG cPoints)
  738. {
  739. ULONG iPoint = 0;
  740. LONG xp = 0;
  741. if(NULL != pXf)
  742. {
  743. for(iPoint = 0; iPoint < cPoints; ++iPoint)
  744. {
  745. xp = RealToInt(pPoints[iPoint].x * pXf->eM11 +
  746. pPoints[iPoint].y * pXf->eM21 + pXf->eDx);
  747. pPoints[iPoint].y = RealToInt(pPoints[iPoint].x * pXf->eM12 +
  748. pPoints[iPoint].y * pXf->eM22 + pXf->eDy);
  749. pPoints[iPoint].x = xp;
  750. }
  751. }
  752. }
  753. HRESULT WINAPI AddStroke(HRECOCONTEXT hrc, const PACKET_DESCRIPTION* pPacketDesc, ULONG cbPacket, const BYTE *pPacket, const XFORM *pXForm)
  754. {
  755. HRESULT hr = S_OK;
  756. ULONG ulPointCount = 0;
  757. STROKEINFO stInfo;
  758. POINT *ptArray = NULL;
  759. struct WispContext *wisphrc;
  760. ULONG ulXIndex = 0, ulYIndex = 0;
  761. BOOL bXFound = FALSE, bYFound = FALSE;
  762. ULONG ulPropIndex = 0;
  763. ULONG index = 0;
  764. int hres = 0;
  765. VRC *vrc = NULL;
  766. const LONG* pLongs = (const LONG *)(pPacket);
  767. int temp = 0;
  768. // find the handle and validate the correpsonding pointer
  769. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  770. if (NULL == wisphrc)
  771. {
  772. return E_INVALIDARG;
  773. }
  774. if (pPacketDesc != NULL && IsBadReadPtr(pPacketDesc, sizeof(PACKET_DESCRIPTION)))
  775. {
  776. return E_POINTER;
  777. }
  778. if (pXForm != NULL && IsBadReadPtr(pXForm, sizeof(XFORM)))
  779. {
  780. return E_POINTER;
  781. }
  782. // validate the data pointer
  783. if(IsBadReadPtr(pPacket, cbPacket))
  784. {
  785. return E_POINTER;
  786. }
  787. if (!wisphrc->hrc)
  788. {
  789. // If we have a free guide and this is not allowed by
  790. // the recognizer, then fail. Return an out of order
  791. // error because it probably means they forgot to
  792. // set the guide before adding ink.
  793. if (g_pProbTable == NULL && !wisphrc->bIsBoxed)
  794. {
  795. return TPC_E_OUT_OF_ORDER_CALL;
  796. }
  797. hr = CreateHRCinContext(wisphrc);
  798. if (FAILED(hr))
  799. {
  800. return E_FAIL;
  801. }
  802. }
  803. if (wisphrc->bCACEndInk)
  804. {
  805. hr = SetCACMode(hrc, wisphrc->iCACMode);
  806. if (FAILED(hr))
  807. return E_FAIL;
  808. }
  809. vrc = (VRC*)wisphrc->hrc;
  810. // Get the number of packets
  811. if (pPacketDesc)
  812. {
  813. ASSERT(!(cbPacket%(pPacketDesc->cbPacketSize)));
  814. ulPointCount = (cbPacket)/(pPacketDesc->cbPacketSize);
  815. }
  816. else
  817. {
  818. ulPointCount = (cbPacket)/(2*sizeof(LONG));
  819. }
  820. // Fill in the stroke info stucture
  821. // Should check it does not exceed the size of a UINT
  822. stInfo.cPnt = ulPointCount;
  823. // PLEASE FIND ANOTHER WAY TO STORE THE STROKE INDEX!!!
  824. stInfo.dwTick = wisphrc->ulCurrentStrokeCount*60*1000;
  825. stInfo.wPdk = 0x0001;
  826. stInfo.cbPnts = ulPointCount*sizeof(POINT);
  827. wisphrc->ulCurrentStrokeCount++;
  828. // Find the index of GUID_X and GUID_Y
  829. if (pPacketDesc)
  830. {
  831. for (ulPropIndex = 0; ulPropIndex < pPacketDesc->cPacketProperties; ulPropIndex++)
  832. {
  833. if (IsEqualGUID(&(pPacketDesc->pPacketProperties[ulPropIndex].guid), &g_guidx))
  834. {
  835. bXFound = TRUE;
  836. ulXIndex = ulPropIndex;
  837. }
  838. else
  839. if (IsEqualGUID(&(pPacketDesc->pPacketProperties[ulPropIndex].guid), &g_guidy))
  840. {
  841. bYFound = TRUE;
  842. ulYIndex = ulPropIndex;
  843. }
  844. if (bXFound && bYFound)
  845. {
  846. break;
  847. }
  848. }
  849. if (!bXFound || !bYFound)
  850. {
  851. // The coordinates are not part of the packet!
  852. // Remove the last stroke from the stroke array
  853. wisphrc->ulCurrentStrokeCount--;
  854. return TPC_E_INVALID_PACKET_DESCRIPTION;
  855. }
  856. // Allocate the memory for the stroke
  857. // Do it very poorly first (we could reuse the buffer)
  858. ptArray = (POINT*)ExternAlloc(ulPointCount*sizeof(POINT));
  859. if (!ptArray)
  860. {
  861. // Remove the last stroke from the stroke array
  862. wisphrc->ulCurrentStrokeCount--;
  863. return E_OUTOFMEMORY;
  864. }
  865. // Get the points from the packets
  866. for (index = 0; index < ulPointCount; index++, pLongs += (pPacketDesc->cbPacketSize)/sizeof(long))
  867. {
  868. // Feed the ptArray (array of points)
  869. ptArray[index].x = *(pLongs+ulXIndex);
  870. ptArray[index].y = *(pLongs+ulYIndex);
  871. }
  872. // TO DO, for now I transform the points so they
  873. // they are in the ink coordinates. It is up to
  874. // the recognizer team to decide what they should
  875. // use: raw ink or transformed ink
  876. Transform(pXForm, ptArray, ulPointCount);
  877. if (wisphrc->bIsBoxed)
  878. {
  879. if (HwxInput(wisphrc->hrc, ptArray, stInfo.cPnt, stInfo.dwTick))
  880. hres = HRCR_OK;
  881. else
  882. hres = HRCR_ERROR;
  883. }
  884. else
  885. {
  886. hres = AddPenInputHRC(wisphrc->hrc, ptArray, NULL, 0, &stInfo);
  887. }
  888. if ( hres != HRCR_OK)
  889. {
  890. hr = E_FAIL;
  891. // Remove the last stroke from the stroke array
  892. wisphrc->ulCurrentStrokeCount--;
  893. ExternFree(ptArray);
  894. return hr;
  895. }
  896. ExternFree(ptArray);
  897. temp = vrc->pLattice->nRealStrokes;
  898. InterlockedExchange(&(wisphrc->uAbort), temp);
  899. }
  900. else
  901. {
  902. if (wisphrc->bIsBoxed)
  903. {
  904. if (HwxInput(wisphrc->hrc, (POINT*)pPacket, stInfo.cPnt, stInfo.dwTick))
  905. hres = HRCR_OK;
  906. else
  907. hres = HRCR_ERROR;
  908. }
  909. else
  910. {
  911. hres = AddPenInputHRC(wisphrc->hrc, (POINT*)pPacket, NULL, 0, &stInfo);
  912. }
  913. if (hres != HRCR_OK)
  914. {
  915. hr = E_FAIL;
  916. // Remove the last stroke from the stroke array
  917. wisphrc->ulCurrentStrokeCount--;
  918. return hr;
  919. }
  920. temp = vrc->pLattice->nRealStrokes;
  921. InterlockedExchange(&(wisphrc->uAbort), temp);
  922. }
  923. ptArray = NULL;
  924. return hr;
  925. }
  926. HRESULT WINAPI GetBestResultString(HRECOCONTEXT hrc, ULONG *pcwSize, WCHAR* pszBestResult)
  927. {
  928. struct WispContext *wisphrc;
  929. HRESULT hr = S_OK;
  930. VRC *vrc = NULL;
  931. ULONG i = 0;
  932. // find the handle and validate the correpsonding pointer
  933. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  934. if (NULL == wisphrc)
  935. {
  936. return E_INVALIDARG;
  937. }
  938. if (IsBadWritePtr(pcwSize, sizeof(ULONG)))
  939. {
  940. return E_POINTER;
  941. }
  942. // check the string pointer if needed
  943. if ( pszBestResult &&
  944. IsBadWritePtr (pszBestResult, (*pcwSize) * sizeof (*pszBestResult))
  945. )
  946. {
  947. return E_POINTER;
  948. }
  949. vrc = (VRC*)wisphrc->hrc;
  950. if (!vrc)
  951. {
  952. *pcwSize = 0;
  953. return S_OK;
  954. }
  955. if (!vrc->pLatticePath)
  956. {
  957. *pcwSize = 0;
  958. return S_OK;
  959. }
  960. if (!pszBestResult)
  961. {
  962. *pcwSize = vrc->pLatticePath->nChars;
  963. return S_OK;
  964. }
  965. // Make the length realistic
  966. if (*pcwSize > (ULONG)vrc->pLatticePath->nChars)
  967. {
  968. *pcwSize = vrc->pLatticePath->nChars;
  969. }
  970. // Is the buffer too small?
  971. if (*pcwSize < (ULONG)vrc->pLatticePath->nChars)
  972. {
  973. hr = TPC_S_TRUNCATED;
  974. }
  975. for (i = 0; i < *pcwSize; i++)
  976. {
  977. pszBestResult[i] = vrc->pLatticePath->pElem[i].wChar;
  978. }
  979. return hr;
  980. }
  981. //
  982. // GetBestAlternate
  983. //
  984. // This function create the best alternate from the best segmentation
  985. //
  986. // Parameters:
  987. // hrc [in] : the reco context
  988. // pHrcAlt [out] : pointer to the handle of the alternate
  989. /////////////////
  990. HRESULT WINAPI GetBestAlternate(HRECOCONTEXT hrc, HRECOALT* pHrcAlt)
  991. {
  992. struct WispContext *wisphrc;
  993. HRESULT hr = S_OK;
  994. ULONG cbSize = 0;
  995. struct WispAlternate *pWispAlt = NULL;
  996. VRC *vrc = NULL;
  997. ULONG i = 0;
  998. // find the handle and validate the correpsonding pointer
  999. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  1000. if (NULL == wisphrc)
  1001. {
  1002. return E_INVALIDARG;
  1003. }
  1004. // First get the number of characters in the string
  1005. vrc = (VRC*)wisphrc->hrc;
  1006. if (!vrc)
  1007. {
  1008. return TPC_E_NOT_RELEVANT;
  1009. }
  1010. if (!vrc->pLatticePath)
  1011. {
  1012. // There is no ink
  1013. return TPC_E_NOT_RELEVANT;
  1014. }
  1015. cbSize = vrc->pLatticePath->nChars;
  1016. // Create the alternate
  1017. pWispAlt = (struct WispAlternate*)ExternAlloc(sizeof(struct WispAlternate));
  1018. if (!pWispAlt)
  1019. {
  1020. return E_OUTOFMEMORY;
  1021. }
  1022. ZeroMemory(pWispAlt, sizeof(struct WispAlternate));
  1023. pWispAlt->iNumberOfColumns = cbSize;
  1024. pWispAlt->iLength = cbSize;
  1025. pWispAlt->OriginalRecoRange.iwcBegin = 0;
  1026. pWispAlt->OriginalRecoRange.cCount = cbSize;
  1027. pWispAlt->hrc = hrc;
  1028. if (cbSize)
  1029. {
  1030. pWispAlt->pColumnIndex = ExternAlloc(sizeof(ULONG)*cbSize);
  1031. if (!pWispAlt->pColumnIndex)
  1032. {
  1033. ExternFree(pWispAlt);
  1034. return E_OUTOFMEMORY;
  1035. }
  1036. // Initialize the column index array
  1037. for (i = 0; i<cbSize; i++)
  1038. {
  1039. pWispAlt->pColumnIndex[i] = vrc->pLatticePath->pElem[i].iStroke;
  1040. }
  1041. pWispAlt->pIndexInColumn = ExternAlloc(sizeof(ULONG)*cbSize);
  1042. if (!pWispAlt->pIndexInColumn)
  1043. {
  1044. ExternFree(pWispAlt->pColumnIndex);
  1045. ExternFree(pWispAlt);
  1046. return E_OUTOFMEMORY;
  1047. }
  1048. // The best alternate doe not always have the index 0 in the alternate column
  1049. // Initialize the index in column array
  1050. for (i = 0; i<cbSize; i++)
  1051. {
  1052. pWispAlt->pIndexInColumn[i] = vrc->pLatticePath->pElem[i].iAlt;
  1053. }
  1054. }
  1055. // create a tpg handle
  1056. *pHrcAlt = (HRECOALT)CreateTpgHandle(TPG_HRECOALT, pWispAlt);
  1057. if (0 == *pHrcAlt)
  1058. {
  1059. ExternFree (pWispAlt->pIndexInColumn);
  1060. ExternFree (pWispAlt->pColumnIndex);
  1061. ExternFree (pWispAlt);
  1062. return E_OUTOFMEMORY;
  1063. }
  1064. return S_OK;
  1065. }
  1066. // internal implementation: destroy the wispalternate structure
  1067. HRESULT DestroyAlternateInternal(struct WispAlternate *wisphrcalt)
  1068. {
  1069. ExternFree(wisphrcalt->pColumnIndex);
  1070. ExternFree(wisphrcalt->pIndexInColumn);
  1071. ExternFree(wisphrcalt);
  1072. return S_OK;
  1073. }
  1074. //
  1075. // DestroyAlternate
  1076. //
  1077. // This function destroys an alternate, freeing the allocated memory
  1078. //
  1079. // Parameters:
  1080. // hrcalt [in] : handle of the alternate to be destroyed
  1081. /////////////////
  1082. HRESULT WINAPI DestroyAlternate(HRECOALT hrcalt)
  1083. {
  1084. struct WispAlternate *wisphrcalt;
  1085. wisphrcalt = (struct WispAlternate *) DestroyTpgHandle (hrcalt, TPG_HRECOALT);
  1086. if (NULL == wisphrcalt)
  1087. {
  1088. return E_INVALIDARG;
  1089. }
  1090. return DestroyAlternateInternal (wisphrcalt);
  1091. }
  1092. HRESULT WINAPI SetGuide(HRECOCONTEXT hrc, const RECO_GUIDE* pGuide, ULONG iIndex)
  1093. {
  1094. struct WispContext *wisphrc;
  1095. HWXGUIDE hwxGuide;
  1096. HRESULT hr = S_OK;
  1097. BOOL bGuideAlreadySet = FALSE;
  1098. RECO_GUIDE rgOldGuide;
  1099. ULONG uiOldIndex = 0;
  1100. BOOL bIsOldGuideBox = FALSE;
  1101. BOOL bIsHRCAlreadyCreated = FALSE;
  1102. // find the handle and validate the correpsonding pointer
  1103. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  1104. if (NULL == wisphrc)
  1105. {
  1106. return E_INVALIDARG;
  1107. }
  1108. if (pGuide != NULL && IsBadReadPtr(pGuide, sizeof(RECO_GUIDE)))
  1109. {
  1110. return E_POINTER;
  1111. }
  1112. if (pGuide != NULL)
  1113. {
  1114. if ((pGuide->cHorzBox < 0 || pGuide->cVertBox < 0) || // invalid
  1115. (pGuide->cHorzBox == 0 && pGuide->cVertBox > 0) || // horizontal lined mode
  1116. (pGuide->cHorzBox > 0 && pGuide->cVertBox == 0) || // vertical lined mode
  1117. (g_pProbTable == NULL && pGuide->cHorzBox == 0 && pGuide->cVertBox == 0)) // free mode not allowed sometimes
  1118. {
  1119. return E_INVALIDARG;
  1120. }
  1121. }
  1122. if (pGuide == NULL && g_pProbTable == NULL)
  1123. {
  1124. // Can't do free mode, but got a NULL guide, so return an error.
  1125. return E_INVALIDARG;
  1126. }
  1127. // Is there already an HRC
  1128. if (wisphrc->hrc)
  1129. {
  1130. bIsHRCAlreadyCreated = TRUE;
  1131. }
  1132. // Save the old values in case the call to the
  1133. // recognizer fails
  1134. if (wisphrc->pGuide)
  1135. {
  1136. bGuideAlreadySet = TRUE;
  1137. rgOldGuide = *(wisphrc->pGuide);
  1138. uiOldIndex = wisphrc->uiGuideIndex;
  1139. bIsOldGuideBox = wisphrc->bIsBoxed;
  1140. }
  1141. // If there was no guide already present, allocate one
  1142. if (!wisphrc->pGuide)
  1143. wisphrc->pGuide = ExternAlloc(sizeof(RECO_GUIDE));
  1144. if (!wisphrc->pGuide)
  1145. return E_OUTOFMEMORY;
  1146. // If the guide is NULL, then treat it as all zeros (free mode)
  1147. if (pGuide != NULL)
  1148. {
  1149. *(wisphrc->pGuide) = *pGuide;
  1150. }
  1151. else
  1152. {
  1153. ZeroMemory(wisphrc->pGuide, sizeof(RECO_GUIDE));
  1154. }
  1155. wisphrc->uiGuideIndex = iIndex;
  1156. // Check if we are in box mode or free input mode
  1157. if (wisphrc->pGuide->cHorzBox && wisphrc->pGuide->cVertBox)
  1158. {
  1159. // We are in the box api mode
  1160. // We need to have a proper conversion
  1161. ZeroMemory(&hwxGuide, sizeof(HWXGUIDE));
  1162. hwxGuide.cHorzBox = wisphrc->pGuide->cHorzBox;
  1163. hwxGuide.cVertBox = wisphrc->pGuide->cVertBox;
  1164. hwxGuide.cxBox = wisphrc->pGuide->cxBox;
  1165. hwxGuide.cyBox = wisphrc->pGuide->cyBox;
  1166. hwxGuide.xOrigin = wisphrc->pGuide->xOrigin;
  1167. hwxGuide.yOrigin = wisphrc->pGuide->yOrigin;
  1168. hwxGuide.cxOffset = wisphrc->pGuide->cxBase ;
  1169. hwxGuide.cyOffset = 0;
  1170. hwxGuide.cxWriting = wisphrc->pGuide->cxBox - (2 * wisphrc->pGuide->cxBase) ;
  1171. if (wisphrc->pGuide->cyBase > 0) {
  1172. hwxGuide.cyWriting = wisphrc->pGuide->cyBase ;
  1173. } else {
  1174. hwxGuide.cyWriting = wisphrc->pGuide->cyBox ;
  1175. }
  1176. hwxGuide.cyMid = 0 ;
  1177. hwxGuide.cyBase = 0 ;
  1178. hwxGuide.nDir = HWX_HORIZONTAL ;
  1179. // Is the hrc already created
  1180. if (bIsHRCAlreadyCreated)
  1181. {
  1182. // Are we already in box mode?
  1183. if (!wisphrc->bIsBoxed)
  1184. {
  1185. // We need to switch to a box hrc if possible
  1186. if (wisphrc->ulCurrentStrokeCount == 0)
  1187. {
  1188. // Destroy the previous context
  1189. DestroyHRC(wisphrc->hrc);
  1190. wisphrc->hrc = NULL;
  1191. }
  1192. else
  1193. {
  1194. hr = E_FAIL;
  1195. }
  1196. }
  1197. }
  1198. wisphrc->bIsBoxed = TRUE;
  1199. if (SUCCEEDED(hr) && !wisphrc->hrc)
  1200. {
  1201. hr = CreateHRCinContext(wisphrc);
  1202. if (FAILED(hr))
  1203. hr = E_FAIL;
  1204. }
  1205. if (SUCCEEDED(hr))
  1206. {
  1207. if (HwxSetGuide(wisphrc->hrc, &hwxGuide))
  1208. {
  1209. if (TRUE == HwxSetAbort(wisphrc->hrc, &(wisphrc->uAbort)))
  1210. {
  1211. return S_OK;
  1212. }
  1213. else
  1214. {
  1215. hr = E_FAIL;
  1216. }
  1217. }
  1218. else
  1219. {
  1220. hr = E_INVALIDARG;
  1221. }
  1222. }
  1223. }
  1224. else
  1225. {
  1226. wisphrc->bIsBoxed = FALSE;
  1227. if (!wisphrc->hrc)
  1228. {
  1229. // We need to switch to a free hrc if possible
  1230. if (wisphrc->ulCurrentStrokeCount == 0)
  1231. {
  1232. // Destroy the previous context
  1233. HwxDestroy(wisphrc->hrc);
  1234. wisphrc->hrc = NULL;
  1235. }
  1236. else
  1237. {
  1238. hr = E_FAIL;
  1239. }
  1240. }
  1241. hr = CreateHRCinContext(wisphrc);
  1242. if (FAILED(hr))
  1243. hr = E_FAIL;
  1244. // we are in the free api mode
  1245. if (SUCCEEDED(hr))
  1246. {
  1247. if (HRCR_OK == SetGuideHRC(wisphrc->hrc, (GUIDE *)wisphrc->pGuide, iIndex))
  1248. return S_OK;
  1249. hr = E_INVALIDARG;
  1250. }
  1251. }
  1252. // The calls did not succeed.
  1253. // If we allocated an hrc, destroy it
  1254. if (!bIsHRCAlreadyCreated && wisphrc->hrc)
  1255. {
  1256. if (wisphrc->bIsBoxed)
  1257. {
  1258. HwxDestroy(wisphrc->hrc);
  1259. }
  1260. else
  1261. {
  1262. DestroyHRC(wisphrc->hrc);
  1263. }
  1264. wisphrc->hrc = NULL;
  1265. }
  1266. // Set back the old guide
  1267. if (bGuideAlreadySet)
  1268. {
  1269. *(wisphrc->pGuide) = rgOldGuide;
  1270. wisphrc->bIsBoxed = bIsOldGuideBox;
  1271. wisphrc->uiGuideIndex = uiOldIndex;
  1272. }
  1273. else
  1274. {
  1275. ExternFree(wisphrc->pGuide);
  1276. wisphrc->pGuide = NULL;
  1277. wisphrc->bIsBoxed = FALSE;
  1278. }
  1279. return hr;
  1280. }
  1281. HRESULT WINAPI GetGuide(HRECOCONTEXT hrc, RECO_GUIDE* pGuide, ULONG *piIndex)
  1282. {
  1283. struct WispContext *wisphrc;
  1284. // find the handle and validate the correpsonding pointer
  1285. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  1286. if (NULL == wisphrc)
  1287. {
  1288. return E_INVALIDARG;
  1289. }
  1290. if (IsBadWritePtr(pGuide, sizeof(RECO_GUIDE)))
  1291. {
  1292. return E_POINTER;
  1293. }
  1294. if (IsBadWritePtr(piIndex, sizeof(ULONG)))
  1295. {
  1296. return E_POINTER;
  1297. }
  1298. if (!wisphrc->pGuide)
  1299. {
  1300. return S_FALSE;
  1301. }
  1302. if (wisphrc->pGuide)
  1303. {
  1304. *pGuide = *(wisphrc->pGuide);
  1305. }
  1306. if (piIndex)
  1307. {
  1308. *piIndex = wisphrc->uiGuideIndex;
  1309. }
  1310. return S_OK;
  1311. }
  1312. HRESULT WINAPI AdviseInkChange(HRECOCONTEXT hrc, BOOL bNewStroke)
  1313. {
  1314. struct WispContext *wisphrc;
  1315. // find the handle and validate the correpsonding pointer
  1316. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  1317. if (NULL == wisphrc)
  1318. {
  1319. return E_INVALIDARG;
  1320. }
  1321. InterlockedIncrement(&(wisphrc->uAbort));
  1322. return S_OK;
  1323. }
  1324. HRESULT WINAPI SetCACMode(HRECOCONTEXT hrc, int iMode)
  1325. {
  1326. HRESULT hr = S_OK;
  1327. struct WispContext *wisphrc;
  1328. VRC *vrc;
  1329. HRECOCONTEXT CloneHrc = NULL;
  1330. HRC OldHrc = NULL;
  1331. int i = 0, j = 0;
  1332. int iCACMode = 0;
  1333. // find the handle and validate the correpsonding pointer
  1334. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  1335. if (NULL == wisphrc)
  1336. {
  1337. return E_INVALIDARG;
  1338. }
  1339. if (!wisphrc->bIsBoxed)
  1340. {
  1341. return E_FAIL;
  1342. }
  1343. if (iMode != CAC_FULL && iMode != CAC_PREFIX && iMode != CAC_RANDOM)
  1344. {
  1345. return E_INVALIDARG;
  1346. }
  1347. vrc = (VRC*)wisphrc->hrc;
  1348. OldHrc = wisphrc->hrc;
  1349. wisphrc->hrc = NULL;
  1350. // Create the new context
  1351. wisphrc->hrc = HwxCreate(OldHrc);
  1352. if (!wisphrc->hrc)
  1353. {
  1354. wisphrc->hrc = OldHrc;
  1355. return E_FAIL;
  1356. }
  1357. if (FALSE == HwxSetAbort(wisphrc->hrc, &(wisphrc->uAbort)))
  1358. {
  1359. ASSERT(0);
  1360. }
  1361. // Set the CAC mode
  1362. if (iMode == CAC_FULL)
  1363. {
  1364. iCACMode = HWX_PARTIAL_ALL;
  1365. }
  1366. else
  1367. if (iMode == CAC_PREFIX)
  1368. {
  1369. iCACMode = HWX_PARTIAL_ORDER;
  1370. }
  1371. else
  1372. if (iMode == CAC_RANDOM)
  1373. {
  1374. iCACMode = HWX_PARTIAL_FREE;
  1375. }
  1376. if (!HwxSetPartial(wisphrc->hrc, iCACMode))
  1377. {
  1378. // Put things back together
  1379. HwxDestroy(wisphrc->hrc);
  1380. wisphrc->hrc = ((struct WispContext*)OldHrc)->hrc;
  1381. return E_FAIL;
  1382. }
  1383. wisphrc->bIsCAC = TRUE;
  1384. wisphrc->iCACMode = iMode;
  1385. wisphrc->bCACEndInk = FALSE;
  1386. // TO DO
  1387. // TO DO
  1388. //
  1389. // We probably need to store the original ink, not use the smoothed and merged ink that
  1390. // is store in the Lattice...
  1391. // We need to get the ink from the old context, if there was an old context
  1392. if (vrc != NULL)
  1393. {
  1394. for (i = 0; i < vrc->pLattice->nStrokes; i++)
  1395. {
  1396. // We need to reorder the strokes to have them in the same order
  1397. for (j = 0; j < vrc->pLattice->nStrokes; j++)
  1398. {
  1399. if (vrc->pLattice->pStroke[j].iOrder == i)
  1400. {
  1401. // Add the Stroke to the new context
  1402. if (!HwxInput(wisphrc->hrc, vrc->pLattice->pStroke[j].pts, vrc->pLattice->pStroke[j].nInk, vrc->pLattice->pStroke[j].timeStart))
  1403. {
  1404. hr = E_FAIL;
  1405. }
  1406. break;
  1407. }
  1408. }
  1409. }
  1410. wisphrc->uAbort = vrc->pLattice->nStrokes;
  1411. if (vrc->fBoxedInput)
  1412. HwxDestroy(OldHrc);
  1413. else
  1414. DestroyHRC(OldHrc);
  1415. }
  1416. else
  1417. {
  1418. wisphrc->uAbort = 0;
  1419. }
  1420. return hr;
  1421. }
  1422. HRESULT WINAPI EndInkInput(HRECOCONTEXT hrc)
  1423. {
  1424. struct WispContext *wisphrc;
  1425. // find the handle and validate the correpsonding pointer
  1426. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  1427. if (NULL == wisphrc)
  1428. {
  1429. return E_INVALIDARG;
  1430. }
  1431. if (wisphrc->bIsBoxed)
  1432. {
  1433. if (HwxEndInput(wisphrc->hrc))
  1434. return S_OK;
  1435. }
  1436. else
  1437. {
  1438. if (HRCR_OK == EndPenInputHRC(wisphrc->hrc))
  1439. return S_OK;
  1440. }
  1441. if (!wisphrc->ulCurrentStrokeCount)
  1442. {
  1443. return S_OK; // We do not have ink yet
  1444. }
  1445. return E_FAIL;
  1446. }
  1447. // Given a recognition context, create a new one which has no ink in it, but
  1448. // is otherwise identical. An error is returned if there are any allocation
  1449. // problems (which should be the only types of errors).
  1450. HRESULT WINAPI CloneContext(HRECOCONTEXT hrc, HRECOCONTEXT* pCloneHrc)
  1451. {
  1452. struct WispContext *pWispContext = NULL;
  1453. struct WispContext *wisphrc;
  1454. HRESULT hRes = S_OK, hr = S_OK;
  1455. // find the handle and validate the correpsonding pointer
  1456. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  1457. if (NULL == wisphrc)
  1458. {
  1459. return E_INVALIDARG;
  1460. }
  1461. if (IsBadWritePtr(pCloneHrc, sizeof(HRECOCONTEXT)))
  1462. {
  1463. return E_POINTER;
  1464. }
  1465. pWispContext = (struct WispContext*)ExternAlloc(sizeof(struct WispContext));
  1466. if (!pWispContext)
  1467. {
  1468. return E_OUTOFMEMORY;
  1469. }
  1470. // Did we already create a context???
  1471. if (!wisphrc->hrc)
  1472. {
  1473. // The context was not already created
  1474. memcpy(pWispContext, wisphrc, sizeof(struct WispContext));
  1475. // If a guide was created, then we would have a context
  1476. ASSERT(!wisphrc->pGuide);
  1477. pWispContext->pGuide = NULL;
  1478. // You can't get a lattice until after processing has been done (in a context)
  1479. ASSERT(!wisphrc->pLattice);
  1480. pWispContext->pLattice = NULL;
  1481. pWispContext->pLatticeProperties = NULL;
  1482. pWispContext->pLatticePropertyValues = NULL;
  1483. pWispContext->ppLatticeProperties = NULL;
  1484. // Copy the text context
  1485. if (wisphrc->bHasTextContext)
  1486. {
  1487. pWispContext->bHasTextContext = TRUE;
  1488. pWispContext->wszBefore = Externwcsdup(wisphrc->wszBefore);
  1489. pWispContext->wszAfter = Externwcsdup(wisphrc->wszAfter);
  1490. if (pWispContext->wszBefore == NULL || pWispContext->wszAfter == NULL)
  1491. {
  1492. ExternFree(pWispContext->wszBefore);
  1493. ExternFree(pWispContext->wszAfter);
  1494. ExternFree(pWispContext);
  1495. return E_OUTOFMEMORY;
  1496. }
  1497. }
  1498. else
  1499. {
  1500. pWispContext->bHasTextContext = FALSE;
  1501. pWispContext->wszBefore = NULL;
  1502. pWispContext->wszAfter = NULL;
  1503. }
  1504. // Copy factoid setting
  1505. if (wisphrc->wszFactoid)
  1506. {
  1507. pWispContext->wszFactoid = Externwcsdup(wisphrc->wszFactoid);
  1508. if (pWispContext->wszFactoid == NULL)
  1509. {
  1510. ExternFree(pWispContext->wszAfter);
  1511. ExternFree(pWispContext->wszBefore);
  1512. ExternFree(pWispContext);
  1513. return E_OUTOFMEMORY;
  1514. }
  1515. }
  1516. else
  1517. {
  1518. pWispContext->wszFactoid = NULL;
  1519. }
  1520. }
  1521. else
  1522. {
  1523. // Depending of whether we are in box mode create the hwx hrc
  1524. if (!wisphrc->bIsBoxed)
  1525. {
  1526. pWispContext->bIsBoxed = FALSE;
  1527. pWispContext->hrc = CreateCompatibleHRC(wisphrc->hrc, NULL);
  1528. }
  1529. else
  1530. {
  1531. pWispContext->bIsBoxed = TRUE;
  1532. pWispContext->hrc = HwxCreate(wisphrc->hrc);
  1533. }
  1534. if (!pWispContext->hrc)
  1535. {
  1536. hr = E_OUTOFMEMORY;
  1537. }
  1538. // Set the context variables
  1539. if (SUCCEEDED(hr) && wisphrc->bHasTextContext)
  1540. {
  1541. pWispContext->bHasTextContext = TRUE;
  1542. pWispContext->wszBefore = Externwcsdup(wisphrc->wszBefore);
  1543. pWispContext->wszAfter = Externwcsdup(wisphrc->wszAfter);
  1544. if (pWispContext->wszBefore == NULL || pWispContext->wszAfter == NULL)
  1545. {
  1546. hr = E_OUTOFMEMORY;
  1547. }
  1548. }
  1549. else
  1550. {
  1551. pWispContext->bHasTextContext = FALSE;
  1552. pWispContext->wszBefore = NULL;
  1553. pWispContext->wszAfter = NULL;
  1554. }
  1555. // Copy flags
  1556. pWispContext->dwFlags = wisphrc->dwFlags;
  1557. // Copy factoid setting
  1558. if (SUCCEEDED(hr) && wisphrc->wszFactoid)
  1559. {
  1560. pWispContext->wszFactoid = Externwcsdup(wisphrc->wszFactoid);
  1561. if (pWispContext->wszFactoid == NULL)
  1562. {
  1563. hr = E_OUTOFMEMORY;
  1564. }
  1565. }
  1566. else
  1567. {
  1568. pWispContext->wszFactoid = NULL;
  1569. }
  1570. // Set the guide for the Wisp structure
  1571. if (SUCCEEDED(hr) && wisphrc->pGuide)
  1572. {
  1573. pWispContext->pGuide = ExternAlloc(sizeof(RECO_GUIDE));
  1574. if (!pWispContext->pGuide)
  1575. {
  1576. hr = E_OUTOFMEMORY;
  1577. }
  1578. else
  1579. {
  1580. *(pWispContext->pGuide) = *(wisphrc->pGuide);
  1581. pWispContext->uiGuideIndex = wisphrc->uiGuideIndex;
  1582. }
  1583. }
  1584. else
  1585. {
  1586. pWispContext->pGuide = NULL;
  1587. }
  1588. // Set the abort for hwx
  1589. pWispContext->uAbort = 0;
  1590. if (SUCCEEDED(hr) && pWispContext->bIsBoxed)
  1591. {
  1592. if (!HwxSetAbort(pWispContext->hrc, &(pWispContext->uAbort)))
  1593. {
  1594. hr = E_FAIL;
  1595. }
  1596. }
  1597. pWispContext->ulCurrentStrokeCount = 0;
  1598. pWispContext->bCACEndInk = FALSE;
  1599. pWispContext->bIsCAC = FALSE;
  1600. // Set the CAC Mode
  1601. if (SUCCEEDED(hr) && wisphrc->bIsCAC)
  1602. {
  1603. pWispContext->bIsCAC = TRUE;
  1604. pWispContext->iCACMode = wisphrc->iCACMode;
  1605. }
  1606. }
  1607. // Clean the lattice
  1608. pWispContext->pLattice = NULL;
  1609. pWispContext->pLatticeProperties = NULL;
  1610. pWispContext->pLatticePropertyValues = NULL;
  1611. pWispContext->ppLatticeProperties = NULL;
  1612. if (SUCCEEDED(hr))
  1613. {
  1614. // create a tpg handle
  1615. *pCloneHrc = (HRECOCONTEXT)CreateTpgHandle(TPG_HRECOCONTEXT, pWispContext);
  1616. if (NULL == (*pCloneHrc))
  1617. {
  1618. hr = E_OUTOFMEMORY;
  1619. }
  1620. }
  1621. if (!SUCCEEDED(hr))
  1622. {
  1623. hRes = DestroyContextInternal(pWispContext);
  1624. ASSERT(SUCCEEDED(hRes));
  1625. }
  1626. return hr;
  1627. }
  1628. // ResetContext
  1629. // This function keeps the settings on the passed reco context
  1630. // but purges it of the ink it contains. If the EndInkInput
  1631. // had been called on the reco context, Reset context will
  1632. // now allow more ink to be entered.
  1633. //
  1634. // Parameter:
  1635. // hrc [in] : the handle to the reco context
  1636. /////////////////////////////////////////////////////////
  1637. HRESULT WINAPI ResetContext(HRECOCONTEXT hrc)
  1638. {
  1639. struct WispContext *wisphrc;
  1640. HRESULT hr = S_OK;
  1641. HRC hrcold = NULL;
  1642. // find the handle and validate the correpsonding pointer
  1643. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  1644. if (NULL == wisphrc)
  1645. {
  1646. return E_INVALIDARG;
  1647. }
  1648. if (!wisphrc->hrc)
  1649. {
  1650. return S_OK;
  1651. }
  1652. // Save the old HRC (in case of an error), and put in the new one
  1653. hrcold = wisphrc->hrc;
  1654. if (!wisphrc->bIsBoxed)
  1655. {
  1656. wisphrc->hrc = CreateCompatibleHRC(wisphrc->hrc, NULL);
  1657. }
  1658. else
  1659. {
  1660. wisphrc->hrc = HwxCreate(wisphrc->hrc);
  1661. }
  1662. if (!wisphrc->hrc)
  1663. {
  1664. wisphrc->hrc = hrcold;
  1665. return E_FAIL;
  1666. }
  1667. // If there was a guide, put it back
  1668. if (SUCCEEDED(hr) && wisphrc->pGuide)
  1669. {
  1670. hr = SetGuide(hrc, wisphrc->pGuide, wisphrc->uiGuideIndex);
  1671. }
  1672. if (SUCCEEDED(hr) && wisphrc->bIsCAC)
  1673. {
  1674. hr = SetCACMode(hrc, wisphrc->iCACMode);
  1675. }
  1676. if (SUCCEEDED(hr) && wisphrc->bHasTextContext)
  1677. {
  1678. hr = SetTextContext(hrc,
  1679. wcslen(wisphrc->wszBefore), wisphrc->wszBefore,
  1680. wcslen(wisphrc->wszAfter), wisphrc->wszAfter);
  1681. }
  1682. if (SUCCEEDED(hr))
  1683. {
  1684. hr = SetFlags(hrc, wisphrc->dwFlags);
  1685. }
  1686. if (SUCCEEDED(hr))
  1687. {
  1688. if (wisphrc->wszFactoid)
  1689. hr = SetFactoid(hrc, wcslen(wisphrc->wszFactoid), wisphrc->wszFactoid);
  1690. else
  1691. hr = SetFactoid(hrc, 0, wisphrc->wszFactoid);
  1692. }
  1693. if (FAILED(hr))
  1694. {
  1695. // Something went wrong. Restore the context to its original state.
  1696. if (!wisphrc->bIsBoxed)
  1697. {
  1698. DestroyHRC(wisphrc->hrc);
  1699. }
  1700. else
  1701. {
  1702. HwxDestroy(wisphrc->hrc);
  1703. }
  1704. wisphrc->hrc = hrcold;
  1705. return hr;
  1706. }
  1707. // These changes are done last, because they can't be undone easily.
  1708. // All error cases have already been taken care of above.
  1709. wisphrc->ulCurrentStrokeCount = 0;
  1710. wisphrc->uAbort = 0;
  1711. wisphrc->bCACEndInk = FALSE;
  1712. if (wisphrc->pLattice)
  1713. {
  1714. hr = FreeRecoLattice(wisphrc);
  1715. }
  1716. wisphrc->pLattice = NULL;
  1717. if (!wisphrc->bIsBoxed)
  1718. {
  1719. DestroyHRC(hrcold);
  1720. }
  1721. else
  1722. {
  1723. HwxDestroy(hrcold);
  1724. }
  1725. return hr;
  1726. }
  1727. // Sets the prefix and suffix context for the recognition context. Can
  1728. // return errors on memory allocation failure. Note that this function
  1729. // is called with the strings pointing at the strings already in the HRC,
  1730. // so we need to be careful not to free the old strings before copying them.
  1731. HRESULT WINAPI SetTextContext(HRECOCONTEXT hrc, ULONG cwcBefore, const WCHAR *pwcBefore, ULONG cwcAfter, const WCHAR *pwcAfter)
  1732. {
  1733. HRESULT hr = S_OK;
  1734. struct WispContext *wisphrc;
  1735. WCHAR *wszBefore, *wszAfter;
  1736. // find the handle and validate the correpsonding pointer
  1737. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  1738. if (NULL == wisphrc)
  1739. {
  1740. return E_INVALIDARG;
  1741. }
  1742. if ( IsBadReadPtr(pwcBefore, cwcBefore * sizeof(WCHAR)) ||
  1743. IsBadReadPtr(pwcAfter, cwcAfter * sizeof(WCHAR))
  1744. )
  1745. {
  1746. return E_POINTER;
  1747. }
  1748. wszBefore = ExternAlloc((cwcBefore + 1) * sizeof(WCHAR));
  1749. if (wszBefore == NULL)
  1750. {
  1751. return E_OUTOFMEMORY;
  1752. }
  1753. wszAfter = ExternAlloc((cwcAfter + 1) * sizeof(WCHAR));
  1754. if (wszAfter == NULL)
  1755. {
  1756. ExternFree(wszBefore);
  1757. return E_OUTOFMEMORY;
  1758. }
  1759. memcpy(wszBefore, pwcBefore, cwcBefore * sizeof(WCHAR));
  1760. wszBefore[cwcBefore] = 0;
  1761. memcpy(wszAfter, pwcAfter, cwcAfter * sizeof(WCHAR));
  1762. wszAfter[cwcAfter] = 0;
  1763. // If we have a context already, then try to set this context.
  1764. // The only errors are memory allocation errors, so we don't need
  1765. // to create a context here to make sure the call will succeed.
  1766. if (wisphrc->hrc)
  1767. {
  1768. if (!SetHwxCorrectionContext(wisphrc->hrc, wszBefore, wszAfter))
  1769. {
  1770. hr = E_FAIL;
  1771. }
  1772. }
  1773. // If everything went okay, then we can update the context.
  1774. if (SUCCEEDED(hr))
  1775. {
  1776. wisphrc->bHasTextContext = TRUE;
  1777. ExternFree(wisphrc->wszBefore);
  1778. ExternFree(wisphrc->wszAfter);
  1779. wisphrc->wszBefore = wszBefore;
  1780. wisphrc->wszAfter = wszAfter;
  1781. }
  1782. return hr;
  1783. }
  1784. // Create an alternate (returned by setting the pointer ppAlt). The contents of
  1785. // the alternate are determined by pAltStruct and pdbList.
  1786. HRESULT CreateDifBreakAlternate(DifBreakList* pdbList, DifBreakAltStruct *pAltStruct, struct WispAlternate **ppAlt)
  1787. {
  1788. int i = 0;
  1789. VRC *vrc = pAltStruct->vrc;
  1790. DiffBreakElement *pCur = NULL;
  1791. *ppAlt = (struct WispAlternate*)ExternAlloc(sizeof(struct WispAlternate));
  1792. if (!*ppAlt)
  1793. return E_OUTOFMEMORY;
  1794. ZeroMemory(*ppAlt, sizeof(struct WispAlternate));
  1795. // Initialize the new alternate structure
  1796. (*ppAlt)->iLength = (*ppAlt)->iNumberOfColumns = pdbList->iColumnCount;
  1797. (*ppAlt)->pColumnIndex = (int*)ExternAlloc(sizeof(int)*(*ppAlt)->iNumberOfColumns);
  1798. if (!(*ppAlt)->pColumnIndex)
  1799. {
  1800. ExternFree(*ppAlt);
  1801. return E_OUTOFMEMORY;
  1802. }
  1803. (*ppAlt)->pIndexInColumn = (int*)ExternAlloc(sizeof(int)*(*ppAlt)->iNumberOfColumns);
  1804. if (!(*ppAlt)->pIndexInColumn)
  1805. {
  1806. ExternFree((*ppAlt)->pColumnIndex);
  1807. ExternFree(*ppAlt);
  1808. return E_OUTOFMEMORY;
  1809. }
  1810. (*ppAlt)->OriginalRecoRange.iwcBegin = pAltStruct->iFirstChar;
  1811. (*ppAlt)->OriginalRecoRange.cCount = pAltStruct->iLastChar -
  1812. pAltStruct->iFirstChar + 1;
  1813. pCur = pdbList->pFirst;
  1814. for (i = 0; i < pdbList->iColumnCount; i++)
  1815. {
  1816. // Add the ColumnIndex
  1817. (*ppAlt)->pColumnIndex[i] = pCur->iColumn;
  1818. (*ppAlt)->pIndexInColumn[i] = pCur->iIndex;
  1819. pCur = pCur->pNext;
  1820. }
  1821. return S_OK;
  1822. }
  1823. BOOL AddToDefSegList(DifBreakList* pdbList, DifBreakAltStruct *pAltStruct)
  1824. {
  1825. AltRank *pAltRank = NULL;
  1826. HRESULT hr = S_OK, hRes = S_OK;
  1827. AltRankList *pAltRankList = pAltStruct->paltRankList;
  1828. AltRank *pPrev = NULL, *pCur;
  1829. UINT i = 0;
  1830. DiffBreakElement *pCurrent = NULL;
  1831. int j = 0;
  1832. // Doesn't make sense to add alternates to a list when no alternates are allowed.
  1833. ASSERT(pAltStruct->ulMax > 0);
  1834. if (pAltStruct->ulMax == 0)
  1835. {
  1836. return TRUE;
  1837. }
  1838. // Is the score even interesting?
  1839. if (pAltRankList->ulSize == pAltStruct->ulMax &&
  1840. !pdbList->bCurrentPath &&
  1841. pAltRankList->pLast != NULL &&
  1842. pAltRankList->pLast->fScore >= pdbList->score)
  1843. return TRUE;
  1844. // Is the decomposition interesting (depending on the recognition mode)
  1845. // For now yes, everyting is interesting!
  1846. // Create the alternate
  1847. pAltRank = (AltRank*)ExternAlloc(sizeof(AltRank));
  1848. if (!pAltRank)
  1849. return FALSE;
  1850. pAltRank->fScore = pdbList->score;
  1851. // All paths created here are not on the current path
  1852. pAltRank->bCurrentPath = pdbList->bCurrentPath;
  1853. pAltRank->next = NULL;
  1854. // Add the new alternate at the current location
  1855. hr = CreateDifBreakAlternate(pdbList, pAltStruct, &(pAltRank->wispalt));
  1856. if (FAILED(hr))
  1857. {
  1858. ExternFree(pAltRank);
  1859. return FALSE;
  1860. }
  1861. if (!pAltRankList->pFirst)
  1862. {
  1863. // Add at the start of the location
  1864. pAltRankList->pFirst = pAltRank;
  1865. pAltRankList->pLast = pAltRank;
  1866. pAltRankList->ulSize = 1;
  1867. return TRUE;
  1868. }
  1869. // If the new alternate is the current path, then it goes to the top of the list.
  1870. // If not, and the current top of list is not on the current path, then compare scores.
  1871. if (pdbList->bCurrentPath ||
  1872. (!pAltRankList->pFirst->bCurrentPath && pAltRankList->pFirst->fScore < pdbList->score))
  1873. {
  1874. // Add at the start of the location
  1875. pAltRank->next = pAltRankList->pFirst;
  1876. pAltRankList->pFirst = pAltRank;
  1877. if (pAltRankList->ulSize == pAltStruct->ulMax)
  1878. {
  1879. // Delete the last element
  1880. hRes = DestroyAlternateInternal(pAltRankList->pLast->wispalt);
  1881. ASSERT(SUCCEEDED(hRes));
  1882. ExternFree(pAltRankList->pLast);
  1883. // Get a pointer to the last element
  1884. pCur = pAltRankList->pFirst;
  1885. while(pCur->next != pAltRankList->pLast)
  1886. pCur = pCur->next;
  1887. pAltRankList->pLast = pCur;
  1888. pCur->next = NULL;
  1889. }
  1890. else
  1891. {
  1892. pAltRankList->ulSize++;
  1893. }
  1894. return TRUE;
  1895. }
  1896. pCur = pAltRankList->pFirst;
  1897. // Insert the link at the right location
  1898. for (i = 0; i < pAltRankList->ulSize - 1; i++)
  1899. {
  1900. pPrev = pCur;
  1901. pCur = pCur->next;
  1902. if (pCur->fScore < pdbList->score)
  1903. {
  1904. // insert at the pPrev
  1905. pAltRank->next = pCur;
  1906. pPrev->next = pAltRank;
  1907. if (pAltRankList->ulSize == pAltStruct->ulMax)
  1908. {
  1909. // Delete the last element
  1910. HRESULT hrDA = DestroyAlternateInternal(pAltRankList->pLast->wispalt);
  1911. ASSERT(SUCCEEDED(hrDA));
  1912. ExternFree(pAltRankList->pLast);
  1913. // Get a pointer to the last element
  1914. pCur = pAltRankList->pFirst;
  1915. while(pCur->next != pAltRankList->pLast)
  1916. pCur = pCur->next;
  1917. pAltRankList->pLast = pCur;
  1918. pCur->next = NULL;
  1919. }
  1920. else
  1921. {
  1922. pAltRankList->ulSize++;
  1923. }
  1924. return TRUE;
  1925. }
  1926. }
  1927. // We are actually adding at the end of the list
  1928. pAltRank->next = NULL;
  1929. pAltRankList->pLast->next = pAltRank;
  1930. pAltRankList->pLast = pAltRank;
  1931. pAltRankList->ulSize++; // obviously we still have room
  1932. return TRUE;
  1933. }
  1934. /*
  1935. static float GetScore(LATTICE *pLattice, int iStroke, int iAlt)
  1936. {
  1937. if (pLattice->fUseGuide)
  1938. {
  1939. return pLattice->pAltList[iStroke].alts[iAlt].logProb;
  1940. }
  1941. else
  1942. {
  1943. return pLattice->pAltList[iStroke].alts[iAlt].logProb * pLattice->pAltList[iStroke].alts[iAlt].nStrokes;
  1944. }
  1945. }
  1946. */
  1947. BOOL GetRecDifSegAltList(int iCurrentStroke, int iCurrentIndex, DifBreakList* pdbList, DifBreakAltStruct *pAltStruct)
  1948. {
  1949. int iNextStroke = 0;
  1950. int i = 0, j = 0, l = 0;
  1951. DiffBreakElement dbElement;
  1952. DiffBreakElement dbSpaceElement;
  1953. float fOriginalScore = pdbList->score;
  1954. BOOL bOriginalCurrentPath = pdbList->bCurrentPath;
  1955. BOOL bSomethingAdded = FALSE;
  1956. BOOL bMainSeg = FALSE;
  1957. int iNumberOfStrokes = 0;
  1958. BOOL bAllBreakSkip = FALSE;
  1959. int iNextNextStroke = 0;
  1960. BOOL bSameBreak = FALSE;
  1961. BOOL bOkay = TRUE;
  1962. iNextStroke = iCurrentStroke -
  1963. pAltStruct->vrc->pLattice->pAltList[iCurrentStroke].alts[iCurrentIndex].nStrokes;
  1964. // Check if we are at the end (we include uiFirstIndex)
  1965. if (pAltStruct->iMode == LARGE_BREAKS)
  1966. {
  1967. if (pAltStruct->vrc->pLattice->pAltList[iCurrentStroke].alts[iCurrentIndex].nStrokes
  1968. > iCurrentStroke-pAltStruct->iFirstStroke)
  1969. {
  1970. // Yes this is a last (or rather "first") stroke
  1971. // But does this stroke correspond to the start of a stroke from the main
  1972. // segmentation?
  1973. bMainSeg = FALSE;
  1974. for (j = pAltStruct->iLastChar; j>=0; j--)
  1975. {
  1976. if (pAltStruct->vrc->pLatticePath->pElem[j].iStroke -
  1977. pAltStruct->vrc->pLatticePath->pElem[j].nStrokes ==
  1978. iNextStroke)
  1979. {
  1980. bMainSeg = TRUE;
  1981. pAltStruct->iFirstChar = j;
  1982. break;
  1983. }
  1984. }
  1985. // Add this to the stroke list
  1986. if (bMainSeg)
  1987. return AddToDefSegList(pdbList, pAltStruct);
  1988. }
  1989. }
  1990. else
  1991. {
  1992. if (pAltStruct->vrc->pLattice->pAltList[iCurrentStroke].alts[iCurrentIndex].nStrokes
  1993. == iCurrentStroke-pAltStruct->iFirstStroke+1)
  1994. {
  1995. // We are at the end
  1996. for (j = pAltStruct->iLastChar; j>=0; j--)
  1997. {
  1998. if (pAltStruct->vrc->pLatticePath->pElem[j].iStroke -
  1999. pAltStruct->vrc->pLatticePath->pElem[j].nStrokes ==
  2000. iNextStroke)
  2001. {
  2002. pAltStruct->iFirstChar = j;
  2003. return AddToDefSegList(pdbList, pAltStruct);
  2004. }
  2005. }
  2006. // This is an error case
  2007. return FALSE;
  2008. }
  2009. if (pAltStruct->vrc->pLattice->pAltList[iCurrentStroke].alts[iCurrentIndex].nStrokes
  2010. > iCurrentStroke-pAltStruct->iFirstStroke+1)
  2011. {
  2012. // Not an error, we just stepped back too far in the lattice.
  2013. return TRUE;
  2014. }
  2015. }
  2016. // We are not at the end (or start) of the alternate, dig deeper
  2017. // First, see if we need a space
  2018. if (pAltStruct->vrc->pLattice->pAltList[iNextStroke].fSpaceAfterStroke)
  2019. {
  2020. dbSpaceElement.iColumn = iNextStroke;
  2021. dbSpaceElement.iIndex = SPACE_ALT_ID;
  2022. dbSpaceElement.pNext = pdbList->pFirst;
  2023. pdbList->iColumnCount++;
  2024. pdbList->pFirst = &dbSpaceElement;
  2025. }
  2026. // Then add on the placeholder for the current character
  2027. dbElement.iColumn = iNextStroke;
  2028. dbElement.pNext = pdbList->pFirst;
  2029. pdbList->iColumnCount++;
  2030. pdbList->pFirst = &dbElement;
  2031. // In the case of ALT_BREAKS_SAME, get the number of strokes of the best result's column
  2032. if (pAltStruct->iMode == ALT_BREAKS_SAME)
  2033. {
  2034. iNumberOfStrokes = -1;
  2035. for (i = 0; i < pAltStruct->vrc->pLattice->pAltList[iNextStroke].nUsed; i++)
  2036. {
  2037. if (pAltStruct->vrc->pLattice->pAltList[iNextStroke].alts[i].fCurrentPath)
  2038. {
  2039. iNumberOfStrokes = pAltStruct->vrc->pLattice->pAltList[iNextStroke].alts[i].nStrokes;
  2040. break;
  2041. }
  2042. }
  2043. ASSERT(iNumberOfStrokes>0);
  2044. // If we don't find the current path, something has gone wrong. The
  2045. // rest of the code will behave sensibly, though, so continue.
  2046. }
  2047. for (i = 0; i < pAltStruct->vrc->pLattice->pAltList[iNextStroke].nUsed; i++)
  2048. {
  2049. // TBD:
  2050. // Here we should have an optimization to know if
  2051. // we should continue processing the alternates with the
  2052. // same decomposition
  2053. if (pAltStruct->iMode == ALT_BREAKS_SAME)
  2054. if (pAltStruct->vrc->pLattice->pAltList[iNextStroke].alts[i].nStrokes != iNumberOfStrokes)
  2055. continue;
  2056. // If we have been on the current path so far and are still on it with this
  2057. // node, then we are staying on the current path
  2058. pdbList->bCurrentPath = bOriginalCurrentPath &&
  2059. pAltStruct->vrc->pLattice->pAltList[iNextStroke].alts[i].fCurrentPath;
  2060. // Only need to skip alternates if we are trying to get one of each
  2061. // segmentation. And we never need to skip the current path, since we
  2062. // always want to return that segmentation.
  2063. if (pAltStruct->iMode == ALT_BREAKS_UNIQUE && !pdbList->bCurrentPath)
  2064. {
  2065. bAllBreakSkip = FALSE;
  2066. // Did we already go over an alternate with the same number of strokes?
  2067. for (l = 0; l<i; l++)
  2068. {
  2069. if (pAltStruct->vrc->pLattice->pAltList[iNextStroke].alts[i].nStrokes ==
  2070. pAltStruct->vrc->pLattice->pAltList[iNextStroke].alts[l].nStrokes)
  2071. {
  2072. bAllBreakSkip = TRUE;
  2073. break;
  2074. }
  2075. }
  2076. if (bAllBreakSkip)
  2077. continue;
  2078. // If we have been on the current path so far, but are now considering a
  2079. // character off the current path,
  2080. // then skip it if there is a currrent path character later in the alt
  2081. // list with the same number of strokes.
  2082. if (!pdbList->bCurrentPath && bOriginalCurrentPath)
  2083. {
  2084. for (l = i + 1; l < pAltStruct->vrc->pLattice->pAltList[iNextStroke].nUsed; l++)
  2085. {
  2086. if (pAltStruct->vrc->pLattice->pAltList[iNextStroke].alts[l].fCurrentPath &&
  2087. pAltStruct->vrc->pLattice->pAltList[iNextStroke].alts[i].nStrokes ==
  2088. pAltStruct->vrc->pLattice->pAltList[iNextStroke].alts[l].nStrokes)
  2089. {
  2090. bAllBreakSkip = TRUE;
  2091. break;
  2092. }
  2093. }
  2094. }
  2095. if (bAllBreakSkip) continue;
  2096. }
  2097. dbElement.iIndex = i;
  2098. pdbList->score = fOriginalScore +
  2099. pAltStruct->vrc->pLattice->pAltList[iNextStroke].alts[i].logProb;
  2100. // GetScore(pAltStruct->vrc->pLattice, iNextStroke, i);
  2101. if (!GetRecDifSegAltList(iNextStroke, i, pdbList, pAltStruct))
  2102. bOkay = FALSE;
  2103. }
  2104. pdbList->iColumnCount--;
  2105. pdbList->pFirst = dbElement.pNext;
  2106. // Unwind the space if necessary
  2107. if (pAltStruct->vrc->pLattice->pAltList[iNextStroke].fSpaceAfterStroke)
  2108. {
  2109. pdbList->iColumnCount--;
  2110. pdbList->pFirst = dbSpaceElement.pNext;
  2111. }
  2112. pdbList->score = fOriginalScore;
  2113. pdbList->bCurrentPath = bOriginalCurrentPath;
  2114. return bOkay;
  2115. }
  2116. HRESULT GetDifSegAltList(VRC *vrc, int iLastStroke, int iFirstStroke, ULONG ulMax, AltRankList *pAltRankList, int iMode, int iFirstChar, int iLastChar)
  2117. {
  2118. HRESULT hr = S_OK;
  2119. int iStroke = 0;
  2120. int i = 0, j = 0, l = 0;
  2121. DifBreakList dbList;
  2122. DiffBreakElement dbElement;
  2123. BOOL bGoodStart = FALSE;
  2124. BOOL bAllBreakSkip = FALSE;
  2125. DifBreakAltStruct altStruct;
  2126. int iNumberOfStrokes = 0;
  2127. // Get the last Column alternates, they should contain uiLastStroke
  2128. // For each of these alternate, get the complete alternate list, stopping
  2129. // at uiFirstStroke.
  2130. // Initialize the data
  2131. altStruct.iFirstStroke = iFirstStroke;
  2132. altStruct.iFirstChar = iFirstChar;
  2133. altStruct.iLastChar = iLastChar;
  2134. altStruct.iMode = iMode;
  2135. altStruct.ulMax = ulMax;
  2136. altStruct.vrc = vrc;
  2137. altStruct.paltRankList = pAltRankList;
  2138. altStruct.paltRankList->pFirst = altStruct.paltRankList->pLast = NULL;
  2139. altStruct.paltRankList->ulSize = 0;
  2140. dbList.iColumnCount = 1;
  2141. dbList.pFirst = &dbElement;
  2142. dbList.score = 0.0;
  2143. // The starting last stroke should no be further away than 35 strokes from
  2144. // the uiLastStroke
  2145. if (iMode == LARGE_BREAKS)
  2146. iStroke = (vrc->pLattice->nStrokes > iLastStroke + 35 ? iLastStroke + 35 : vrc->pLattice->nStrokes - 1);
  2147. else
  2148. iStroke = iLastStroke;
  2149. while (iStroke >= iLastStroke)
  2150. {
  2151. // The stroke has to be one of the main decomposition
  2152. if (iMode == LARGE_BREAKS)
  2153. {
  2154. bGoodStart = FALSE;
  2155. for (i = 0; i < vrc->pLattice->pAltList[iStroke].nUsed; i++)
  2156. {
  2157. if (vrc->pLattice->pAltList[iStroke].alts[i].fCurrentPath)
  2158. {
  2159. bGoodStart = TRUE;
  2160. // Get the character number in the best alternate string
  2161. for (j = 0; j < vrc->pLatticePath->nChars; j++)
  2162. {
  2163. if (vrc->pLatticePath->pElem[j].iStroke == iStroke)
  2164. {
  2165. altStruct.iLastChar = j;
  2166. break;
  2167. }
  2168. }
  2169. break;
  2170. }
  2171. }
  2172. }
  2173. else
  2174. {
  2175. altStruct.iLastChar = iLastChar;
  2176. bGoodStart = TRUE;
  2177. }
  2178. if (bGoodStart)
  2179. {
  2180. // In the case of ALT_BREAKS_SAME, get the number of strokes of the best result's column
  2181. if (iMode == ALT_BREAKS_SAME)
  2182. {
  2183. iNumberOfStrokes = -1;
  2184. for (i = 0; i < vrc->pLattice->pAltList[iStroke].nUsed; i++)
  2185. {
  2186. if (vrc->pLattice->pAltList[iStroke].alts[i].fCurrentPath)
  2187. {
  2188. iNumberOfStrokes = vrc->pLattice->pAltList[iStroke].alts[i].nStrokes;
  2189. break;
  2190. }
  2191. }
  2192. ASSERT(iNumberOfStrokes>0);
  2193. if (iNumberOfStrokes <= 0)
  2194. hr = E_UNEXPECTED;
  2195. }
  2196. // Search if there is an alternate that contains uiLastStroke
  2197. for (i = 0; i < vrc->pLattice->pAltList[iStroke].nUsed; i++)
  2198. {
  2199. if (iMode == ALT_BREAKS_SAME)
  2200. if (vrc->pLattice->pAltList[iStroke].alts[i].nStrokes != iNumberOfStrokes)
  2201. continue;
  2202. if (iMode == ALT_BREAKS_UNIQUE && !vrc->pLattice->pAltList[iStroke].alts[i].fCurrentPath)
  2203. {
  2204. // Did we already go over an alternate with the same number of strokes?
  2205. bAllBreakSkip = FALSE;
  2206. for (l = 0; l<i; l++)
  2207. {
  2208. if (vrc->pLattice->pAltList[iStroke].alts[i].nStrokes ==
  2209. vrc->pLattice->pAltList[iStroke].alts[l].nStrokes)
  2210. {
  2211. bAllBreakSkip = TRUE;
  2212. break;
  2213. }
  2214. }
  2215. if (bAllBreakSkip) continue;
  2216. // If we are considering a character which is not on the current path,
  2217. // then skip it if there is a current path character later in the alt
  2218. // list with the same number of strokes.
  2219. if (!vrc->pLattice->pAltList[iStroke].alts[i].fCurrentPath)
  2220. {
  2221. for (l = i + 1; l < vrc->pLattice->pAltList[iStroke].nUsed; l++)
  2222. {
  2223. if (vrc->pLattice->pAltList[iStroke].alts[l].fCurrentPath &&
  2224. vrc->pLattice->pAltList[iStroke].alts[i].nStrokes ==
  2225. vrc->pLattice->pAltList[iStroke].alts[l].nStrokes)
  2226. {
  2227. bAllBreakSkip = TRUE;
  2228. break;
  2229. }
  2230. }
  2231. }
  2232. if (bAllBreakSkip) continue;
  2233. }
  2234. if (vrc->pLattice->pAltList[iStroke].alts[i].nStrokes > iStroke-iLastStroke)
  2235. {
  2236. dbElement.iColumn = iStroke;
  2237. dbElement.iIndex = i;
  2238. dbElement.pNext = NULL;
  2239. dbList.bCurrentPath = vrc->pLattice->pAltList[iStroke].alts[i].fCurrentPath;
  2240. // We found one that contains uiLastStroke
  2241. if (!GetRecDifSegAltList(iStroke, i, &dbList, &altStruct))
  2242. {
  2243. hr = E_OUTOFMEMORY;
  2244. }
  2245. }
  2246. }
  2247. }
  2248. iStroke--;
  2249. }
  2250. // If something went wrong, then clean up before returning
  2251. if (!SUCCEEDED(hr))
  2252. {
  2253. AltRank *pCur = pAltRankList->pFirst;
  2254. while (pCur != NULL)
  2255. {
  2256. AltRank *pNext = pCur->next;
  2257. DestroyAlternateInternal(pCur->wispalt);
  2258. ExternFree(pCur);
  2259. pCur = pNext;
  2260. }
  2261. pAltRankList->pFirst = pAltRankList->pLast = NULL;
  2262. pAltRankList->ulSize = 0;
  2263. }
  2264. return hr;
  2265. }
  2266. HRESULT FitAlternateToRecoRange(VRC *vrc, struct WispAlternate *wispalt, RECO_RANGE recoRange)
  2267. {
  2268. HRESULT hr = S_OK;
  2269. int *newColumns = NULL;
  2270. int *newIndexes = NULL;
  2271. int iCurrent = 0, j = 0;
  2272. UINT i = 0;
  2273. int iNumberOfColumns = 0;
  2274. // Return straight away if the reco range is already good
  2275. if (recoRange.iwcBegin == wispalt->OriginalRecoRange.iwcBegin &&
  2276. recoRange.cCount == wispalt->OriginalRecoRange.cCount)
  2277. return S_OK;
  2278. // Allocate the new arrays
  2279. iNumberOfColumns = wispalt->iNumberOfColumns + recoRange.cCount - wispalt->OriginalRecoRange.cCount;
  2280. newColumns = (int*)ExternAlloc(sizeof(int)*iNumberOfColumns);
  2281. if (!newColumns)
  2282. return E_OUTOFMEMORY;
  2283. newIndexes = (int*)ExternAlloc(sizeof(int)*iNumberOfColumns);
  2284. if (!newIndexes)
  2285. {
  2286. ExternFree(newColumns);
  2287. return E_OUTOFMEMORY;
  2288. }
  2289. iCurrent = 0;
  2290. // Copy the first elements of the array
  2291. for (i = recoRange.iwcBegin; i < wispalt->OriginalRecoRange.iwcBegin; i++)
  2292. {
  2293. // Fill with the information from the best result
  2294. newColumns[iCurrent] = vrc->pLatticePath->pElem[i].iStroke;
  2295. newIndexes[iCurrent] = vrc->pLatticePath->pElem[i].iAlt;
  2296. iCurrent++;
  2297. }
  2298. // Copy the existing alternate information
  2299. for (j = 0; j < wispalt->iNumberOfColumns; j++)
  2300. {
  2301. newColumns[iCurrent] = wispalt->pColumnIndex[j];
  2302. newIndexes[iCurrent] = wispalt->pIndexInColumn[j];
  2303. iCurrent++;
  2304. }
  2305. // Copy what follows with the information from the best result
  2306. for (i = wispalt->OriginalRecoRange.iwcBegin + wispalt->OriginalRecoRange.cCount;
  2307. i < recoRange.iwcBegin + recoRange.cCount; i++)
  2308. {
  2309. newColumns[iCurrent] = vrc->pLatticePath->pElem[i].iStroke;
  2310. newIndexes[iCurrent] = vrc->pLatticePath->pElem[i].iAlt;
  2311. iCurrent++;
  2312. }
  2313. ASSERT (iCurrent == iNumberOfColumns);
  2314. // Swap the arrays
  2315. ExternFree(wispalt->pColumnIndex);
  2316. ExternFree(wispalt->pIndexInColumn);
  2317. wispalt->pColumnIndex = newColumns;
  2318. wispalt->pIndexInColumn = newIndexes;
  2319. wispalt->iLength = iCurrent;
  2320. wispalt->iNumberOfColumns = iCurrent;
  2321. return hr;
  2322. }
  2323. // GetAlternateList
  2324. //
  2325. // This function returns alternates of the best result
  2326. //
  2327. // Parameters:
  2328. // hrc [in] : The handle to the reco context
  2329. // pRecoRange [in, out] : Pointer to a RECO_RANGE that contains the range we want to get
  2330. // the alternates for. This range comes bck modified to
  2331. // reflect the range we actually used.
  2332. // pSize [in, out] : The number of alternates. If phrcalt is NULL then this function returns
  2333. // the number of alternates it can return - Note we may return an arbitrary
  2334. // number with an HRESULT S_FALSE if we think that the number of alternate
  2335. // is too long to compute.
  2336. // phrcalt [out] : Array of alternates used to return the alternate list
  2337. // iBreak [in] : Mode for querying alternates: ALT_BREAKS_SAME, ALT_BREAKS_FULL or ALT_BREAKS_UNIQUE
  2338. /////////////////
  2339. HRESULT WINAPI GetAlternateList(HRECOCONTEXT hrc, RECO_RANGE* pRecoRange, ULONG*pSize, HRECOALT *phrcalt, ALT_BREAKS iBreak)
  2340. {
  2341. HRESULT hr = S_OK;
  2342. struct WispContext *wisphrc;
  2343. VRC *vrc = NULL;
  2344. RECO_RANGE recoRange, widestRecoRange;
  2345. ULONG i = 0;
  2346. ULONG iAlt;
  2347. int j = 0;
  2348. int iColumnIndex = 0;
  2349. FLOAT fCurrentScore = 0.0;
  2350. BOOL bSomethingAdded = FALSE;
  2351. AltRank *pCur = NULL, *pTemp1 = NULL, *pTemp2 = NULL;
  2352. ULONG ulAltCountSameStrokeCount = 0;
  2353. AltRankList altRankList;
  2354. int iFirstStroke = 0;
  2355. int iLastStroke = 0;
  2356. // find the handle and validate the correpsonding pointer
  2357. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  2358. if (NULL == wisphrc)
  2359. {
  2360. return E_INVALIDARG;
  2361. }
  2362. if (pRecoRange != NULL && IsBadReadPtr(pRecoRange, sizeof(RECO_RANGE)))
  2363. {
  2364. return E_POINTER;
  2365. }
  2366. if (IsBadWritePtr(pSize, sizeof(ULONG)))
  2367. {
  2368. return E_POINTER;
  2369. }
  2370. if (phrcalt && IsBadWritePtr(phrcalt, (*pSize) * sizeof(*phrcalt)))
  2371. {
  2372. return E_POINTER;
  2373. }
  2374. if (iBreak != ALT_BREAKS_FULL &&
  2375. // iBreak != LARGE_BREAKS &&
  2376. iBreak != ALT_BREAKS_UNIQUE &&
  2377. iBreak != ALT_BREAKS_SAME)
  2378. {
  2379. return E_INVALIDARG;
  2380. }
  2381. vrc = (VRC*)wisphrc->hrc;
  2382. if (!vrc || !vrc->pLatticePath)
  2383. {
  2384. // There is no ink
  2385. *pSize = 0;
  2386. return TPC_E_NOT_RELEVANT;
  2387. }
  2388. // Check the reco range to see if it is valid
  2389. if (pRecoRange)
  2390. {
  2391. recoRange = *pRecoRange;
  2392. if (!recoRange.cCount) return E_INVALIDARG;
  2393. if (recoRange.iwcBegin + recoRange.cCount > (ULONG)vrc->pLatticePath->nChars)
  2394. return E_INVALIDARG;
  2395. }
  2396. else
  2397. {
  2398. recoRange.iwcBegin = 0;
  2399. recoRange.cCount = vrc->pLatticePath->nChars;
  2400. }
  2401. // First trim spaces (if any) from the beginning and end of the reco range
  2402. while (recoRange.cCount > 0 && vrc->pLatticePath->pElem[recoRange.iwcBegin].iAlt == SPACE_ALT_ID)
  2403. {
  2404. recoRange.iwcBegin++;
  2405. recoRange.cCount--;
  2406. }
  2407. while (recoRange.cCount > 0 && vrc->pLatticePath->pElem[recoRange.iwcBegin + recoRange.cCount - 1].iAlt == SPACE_ALT_ID)
  2408. {
  2409. recoRange.cCount--;
  2410. }
  2411. // If the range only contained spaces, then return an error
  2412. if (recoRange.cCount == 0)
  2413. {
  2414. return TPC_E_NOT_RELEVANT;
  2415. }
  2416. // If there aren't any results, error
  2417. if (!vrc->pLatticePath->nChars)
  2418. {
  2419. *pSize = 0;
  2420. return TPC_E_NOT_RELEVANT;
  2421. }
  2422. // If we are passed a buffer for alternates but it has size zero, just
  2423. // return immediately. (Some of the code for handling alternates doesn't
  2424. // work with a buffer size of zero.)
  2425. if (phrcalt != NULL && *pSize == 0)
  2426. {
  2427. return S_OK;
  2428. }
  2429. // If we're in single segmentation mode and have asked for full breaks, then
  2430. // switch to same breaks.
  2431. if ((wisphrc->dwFlags & RECOFLAG_SINGLESEG) != 0 && iBreak == ALT_BREAKS_FULL)
  2432. {
  2433. iBreak = ALT_BREAKS_SAME;
  2434. }
  2435. widestRecoRange = recoRange;
  2436. if (iBreak == ALT_BREAKS_FULL || iBreak == LARGE_BREAKS)
  2437. {
  2438. // Get the number of alternates
  2439. if (!phrcalt)
  2440. {
  2441. *pSize = 50; //We need to find a way how to calculate the real count easily...
  2442. return S_FALSE;
  2443. }
  2444. iLastStroke = vrc->pLatticePath->pElem[recoRange.iwcBegin+recoRange.cCount-1].iStroke;
  2445. iFirstStroke = vrc->pLatticePath->pElem[recoRange.iwcBegin].iStroke -
  2446. vrc->pLatticePath->pElem[recoRange.iwcBegin].nStrokes + 1;
  2447. hr = GetDifSegAltList(vrc, iLastStroke, iFirstStroke, *pSize, &altRankList, iBreak, recoRange.iwcBegin, recoRange.iwcBegin+recoRange.cCount-1);
  2448. if (FAILED(hr))
  2449. {
  2450. return hr;
  2451. }
  2452. // Copy the info from the list to the array
  2453. *pSize = altRankList.ulSize;
  2454. if (altRankList.ulSize == 0) return S_OK;
  2455. // TBD: Adjust the alternates to fit the "widest" returned
  2456. // alternate
  2457. // First find the widest alternate (widestRecoRange)
  2458. if (iBreak == LARGE_BREAKS)
  2459. {
  2460. pCur = altRankList.pFirst;
  2461. widestRecoRange = pCur->wispalt->OriginalRecoRange;
  2462. for (i = 0; i < *pSize; i++)
  2463. {
  2464. // Check the start point
  2465. if (widestRecoRange.iwcBegin > pCur->wispalt->OriginalRecoRange.iwcBegin)
  2466. {
  2467. widestRecoRange.cCount += widestRecoRange.iwcBegin -
  2468. pCur->wispalt->OriginalRecoRange.iwcBegin;
  2469. widestRecoRange.iwcBegin = pCur->wispalt->OriginalRecoRange.iwcBegin;
  2470. }
  2471. // Check the end point
  2472. if (widestRecoRange.iwcBegin + widestRecoRange.cCount <
  2473. pCur->wispalt->OriginalRecoRange.iwcBegin +
  2474. pCur->wispalt->OriginalRecoRange.cCount)
  2475. {
  2476. widestRecoRange.cCount = pCur->wispalt->OriginalRecoRange.iwcBegin
  2477. + pCur->wispalt->OriginalRecoRange.cCount
  2478. - widestRecoRange.iwcBegin;
  2479. }
  2480. // Go to the next
  2481. pCur = pCur->next;
  2482. }
  2483. pCur = altRankList.pFirst;
  2484. // Then call on each alternate the "fit" function
  2485. for (i = 0; i < *pSize; i++)
  2486. {
  2487. hr = FitAlternateToRecoRange(vrc, pCur->wispalt, widestRecoRange);
  2488. // Go to the next
  2489. pCur = pCur->next;
  2490. }
  2491. }
  2492. pCur = altRankList.pFirst;
  2493. pTemp1 = altRankList.pFirst;
  2494. }
  2495. if (iBreak == ALT_BREAKS_UNIQUE || iBreak == ALT_BREAKS_SAME)
  2496. {
  2497. if (iBreak == ALT_BREAKS_UNIQUE && !phrcalt)
  2498. {
  2499. // Get the number of alternates
  2500. *pSize = 50; //We need to find a way how to calculate the real count easily... Impossible?
  2501. // If we're pretending there is only one segmentation...
  2502. if (wisphrc->dwFlags & RECOFLAG_SINGLESEG)
  2503. {
  2504. *pSize = 1;
  2505. }
  2506. return S_FALSE;
  2507. }
  2508. if (!vrc->pLatticePath)
  2509. {
  2510. *pSize = 0;
  2511. return hr;
  2512. }
  2513. // Get the number of alternates
  2514. if (!phrcalt)
  2515. {
  2516. *pSize = 1;
  2517. for (i = recoRange.iwcBegin; i < recoRange.iwcBegin + recoRange.cCount; i++)
  2518. {
  2519. iColumnIndex = vrc->pLatticePath->pElem[i].iStroke;
  2520. // We need to get the number of alternates with the same number of strokes!!!
  2521. ulAltCountSameStrokeCount = 0;
  2522. for (j = 0; j < vrc->pLattice->pAltList[iColumnIndex].nUsed; j++)
  2523. {
  2524. if (vrc->pLattice->pAltList[iColumnIndex].alts[j].nStrokes ==
  2525. vrc->pLatticePath->pElem[i].nStrokes)
  2526. ulAltCountSameStrokeCount++;
  2527. }
  2528. *pSize *= ulAltCountSameStrokeCount;
  2529. }
  2530. return S_OK;
  2531. }
  2532. // If we're in single segmentation mode and have asked for unique breaks, then
  2533. // we just want to return the best path. Easiest way to do this is to
  2534. // ask for same breaks with one alternate.
  2535. if ((wisphrc->dwFlags & RECOFLAG_SINGLESEG) != 0 && iBreak == ALT_BREAKS_UNIQUE)
  2536. {
  2537. if (*pSize > 1)
  2538. {
  2539. *pSize = 1;
  2540. }
  2541. iBreak = ALT_BREAKS_SAME;
  2542. }
  2543. // Get the alternates
  2544. // Create a list of alternates
  2545. iLastStroke = vrc->pLatticePath->pElem[recoRange.iwcBegin+recoRange.cCount-1].iStroke;
  2546. iFirstStroke = vrc->pLatticePath->pElem[recoRange.iwcBegin].iStroke -
  2547. vrc->pLatticePath->pElem[recoRange.iwcBegin].nStrokes + 1;
  2548. hr = GetDifSegAltList(vrc, iLastStroke, iFirstStroke, *pSize, &altRankList, iBreak, recoRange.iwcBegin, recoRange.iwcBegin+recoRange.cCount-1);
  2549. if (FAILED(hr))
  2550. {
  2551. return hr;
  2552. }
  2553. // Copy the info from the list to the array
  2554. *pSize = altRankList.ulSize;
  2555. pCur = altRankList.pFirst;
  2556. pTemp1 = altRankList.pFirst;
  2557. if (altRankList.ulSize == 0) return S_OK;
  2558. }
  2559. for (i = 0; i < *pSize; i++)
  2560. {
  2561. // Allocate the memory for the pRecoRange array
  2562. pCur->wispalt->OriginalRecoRange = recoRange;
  2563. pCur->wispalt->hrc = hrc;
  2564. // create a tpg handle
  2565. phrcalt[i] = (HRECOALT)CreateTpgHandle(TPG_HRECOALT, pCur->wispalt);
  2566. // if we fail we'll have to destroy all the other handles
  2567. if (phrcalt[i] == NULL)
  2568. {
  2569. for (iAlt = 0; iAlt < i; iAlt++)
  2570. {
  2571. DestroyTpgHandle(phrcalt[iAlt], TPG_HRECOALT);
  2572. }
  2573. // And also destroy the alternate list
  2574. while (pTemp1)
  2575. {
  2576. pTemp2 = pTemp1->next;
  2577. DestroyAlternateInternal(pTemp1->wispalt);
  2578. ExternFree(pTemp1);
  2579. pTemp1 = pTemp2;
  2580. }
  2581. return E_OUTOFMEMORY;
  2582. }
  2583. // Go to the next
  2584. pCur = pCur->next;
  2585. }
  2586. // Get rid of the linked list which previous held all the alternates
  2587. while (pTemp1)
  2588. {
  2589. pTemp2 = pTemp1->next;
  2590. ExternFree(pTemp1);
  2591. pTemp1 = pTemp2;
  2592. }
  2593. if (pRecoRange)
  2594. *pRecoRange = widestRecoRange;
  2595. return hr;
  2596. }
  2597. HRESULT WINAPI Process(HRECOCONTEXT hrc, BOOL *pbPartialProcessing)
  2598. {
  2599. struct WispContext *wisphrc;
  2600. HRESULT hr = S_OK;
  2601. // find the handle and validate the correpsonding pointer
  2602. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  2603. if (NULL == wisphrc)
  2604. {
  2605. return E_INVALIDARG;
  2606. }
  2607. if (IsBadWritePtr(pbPartialProcessing, sizeof(BOOL)))
  2608. return E_POINTER;
  2609. *pbPartialProcessing = FALSE;
  2610. if (!wisphrc->hrc)
  2611. {
  2612. return S_OK; // There is no ink
  2613. }
  2614. // We should call the HwxProcess if there is a Guide
  2615. if (wisphrc->bIsBoxed)
  2616. {
  2617. if (wisphrc->bIsCAC)
  2618. {
  2619. hr = EndInkInput(hrc);
  2620. if (FAILED(hr)) return hr;
  2621. wisphrc->bCACEndInk = TRUE;
  2622. }
  2623. if (HwxProcess(wisphrc->hrc))
  2624. {
  2625. // Test wether the result is valid or invalid
  2626. // the result can be invalid if the call to Process was
  2627. // interrupted by a call to AdviseInkChanged
  2628. if (wisphrc->uAbort != (ULONG)((VRC*)wisphrc->hrc)->pLattice->nRealStrokes)
  2629. return TPC_S_INTERRUPTED;
  2630. return hr;
  2631. }
  2632. }
  2633. else
  2634. {
  2635. if (HRCR_OK == ProcessHRC(wisphrc->hrc, 0))
  2636. return S_OK;
  2637. }
  2638. return E_FAIL;
  2639. }
  2640. HRESULT WINAPI SetFactoid(HRECOCONTEXT hrc, ULONG cwcFactoid, const WCHAR* pwcFactoid)
  2641. {
  2642. struct WispContext *wisphrc;
  2643. HRESULT hr = S_OK;
  2644. WCHAR *wszOldFactoid;
  2645. // find the handle and validate the correpsonding pointer
  2646. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  2647. if (NULL == wisphrc)
  2648. {
  2649. return E_INVALIDARG;
  2650. }
  2651. // Save the old factoid so it can be restored if there is an error
  2652. wszOldFactoid = wisphrc->wszFactoid;
  2653. if (pwcFactoid)
  2654. {
  2655. // validate pointer if not null
  2656. if (IsBadReadPtr (pwcFactoid, cwcFactoid * sizeof (*pwcFactoid)))
  2657. {
  2658. return E_POINTER;
  2659. }
  2660. wisphrc->wszFactoid = (WCHAR*)ExternAlloc(sizeof(WCHAR)*(cwcFactoid+1));
  2661. if (!wisphrc->wszFactoid)
  2662. {
  2663. wisphrc->wszFactoid = wszOldFactoid;
  2664. return E_OUTOFMEMORY;
  2665. }
  2666. memcpy(wisphrc->wszFactoid, pwcFactoid, cwcFactoid * sizeof(WCHAR));
  2667. wisphrc->wszFactoid[cwcFactoid] = 0;
  2668. }
  2669. else
  2670. {
  2671. wisphrc->wszFactoid = NULL;
  2672. }
  2673. // If we have an HRC already, set the factoid in it.
  2674. if (wisphrc->hrc != NULL)
  2675. {
  2676. switch (SetHwxFactoid(wisphrc->hrc, wisphrc->wszFactoid))
  2677. {
  2678. case HRCR_OK:
  2679. hr = S_OK;
  2680. break;
  2681. case HRCR_UNSUPPORTED:
  2682. hr = TPC_E_INVALID_PROPERTY;
  2683. break;
  2684. case HRCR_CONFLICT:
  2685. hr = TPC_E_OUT_OF_ORDER_CALL;
  2686. break;
  2687. case HRCR_ERROR:
  2688. default:
  2689. hr = E_FAIL;
  2690. break;
  2691. }
  2692. }
  2693. else
  2694. {
  2695. // Try to create an HRC to test out the factoid setting,
  2696. // then destroy it immediately.
  2697. hr = CreateHRCinContext(wisphrc);
  2698. // Destroy the HRC if we succeeded in creating one.
  2699. if (wisphrc->hrc != NULL)
  2700. {
  2701. if (wisphrc->bIsBoxed)
  2702. HwxDestroy(wisphrc->hrc);
  2703. else
  2704. DestroyHRC(wisphrc->hrc);
  2705. wisphrc->hrc = NULL;
  2706. }
  2707. }
  2708. if (SUCCEEDED(hr))
  2709. {
  2710. ExternFree(wszOldFactoid);
  2711. }
  2712. else
  2713. {
  2714. ExternFree(wisphrc->wszFactoid);
  2715. wisphrc->wszFactoid = wszOldFactoid;
  2716. }
  2717. return hr;
  2718. }
  2719. HRESULT WINAPI SetFlags(HRECOCONTEXT hrc, DWORD dwFlags)
  2720. {
  2721. struct WispContext *wisphrc;
  2722. DWORD dwOldFlags;
  2723. HRESULT hr;
  2724. // find the handle and validate the correpsonding pointer
  2725. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  2726. if (NULL == wisphrc)
  2727. {
  2728. return E_INVALIDARG;
  2729. }
  2730. dwOldFlags = wisphrc->dwFlags;
  2731. wisphrc->dwFlags = dwFlags;
  2732. if (wisphrc->hrc)
  2733. {
  2734. if (SetHwxFlags(wisphrc->hrc, dwFlags))
  2735. hr = S_OK;
  2736. else
  2737. hr = E_INVALIDARG;
  2738. }
  2739. else
  2740. {
  2741. // Try to create an HRC to set out the flag setting,
  2742. // then destroy it immediately.
  2743. hr = CreateHRCinContext(wisphrc);
  2744. // Destroy the HRC if we succeeded in creating one.
  2745. if (wisphrc->hrc != NULL)
  2746. {
  2747. if (wisphrc->bIsBoxed)
  2748. HwxDestroy(wisphrc->hrc);
  2749. else
  2750. DestroyHRC(wisphrc->hrc);
  2751. wisphrc->hrc = NULL;
  2752. }
  2753. }
  2754. if (FAILED(hr))
  2755. {
  2756. wisphrc->dwFlags = dwOldFlags;
  2757. }
  2758. return hr;
  2759. }
  2760. HRESULT WINAPI IsStringSupported(HRECOCONTEXT hrc, ULONG cwcString, const WCHAR *pwcString)
  2761. {
  2762. struct WispContext *wisphrc;
  2763. WCHAR *tempBuffer;
  2764. ULONG ulSize = 0;
  2765. HRESULT hr = S_OK;
  2766. BOOL fCreatedHRC = FALSE;
  2767. // find the handle and validate the correpsonding pointer
  2768. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  2769. if (NULL == wisphrc)
  2770. {
  2771. return E_INVALIDARG;
  2772. }
  2773. if (IsBadReadPtr(pwcString, cwcString * sizeof(*pwcString)))
  2774. {
  2775. return E_POINTER;
  2776. }
  2777. // We need to have a valid HRC to check the factoid
  2778. if (!wisphrc->hrc)
  2779. {
  2780. hr = CreateHRCinContext(wisphrc);
  2781. if (FAILED(hr))
  2782. {
  2783. // Failure might mean we actually made the
  2784. // HRC, but a flag setting or factoid setting failed.
  2785. // So destroy the HRC if it was created.
  2786. if (wisphrc->hrc != NULL)
  2787. {
  2788. if (wisphrc->bIsBoxed)
  2789. HwxDestroy(wisphrc->hrc);
  2790. else
  2791. DestroyHRC(wisphrc->hrc);
  2792. wisphrc->hrc = NULL;
  2793. }
  2794. return E_FAIL;
  2795. }
  2796. fCreatedHRC = TRUE;
  2797. }
  2798. tempBuffer = (WCHAR *) ExternAlloc((cwcString + 1) * sizeof(*tempBuffer));
  2799. if (!tempBuffer)
  2800. return E_OUTOFMEMORY;
  2801. memcpy(tempBuffer, pwcString, cwcString * sizeof(WCHAR));
  2802. tempBuffer[cwcString] = 0;
  2803. if (IsWStringSupportedHRC(wisphrc->hrc, tempBuffer))
  2804. hr = S_OK;
  2805. else
  2806. hr = S_FALSE;
  2807. ExternFree(tempBuffer);
  2808. if (fCreatedHRC)
  2809. {
  2810. if (wisphrc->bIsBoxed)
  2811. HwxDestroy(wisphrc->hrc);
  2812. else
  2813. DestroyHRC(wisphrc->hrc);
  2814. wisphrc->hrc = NULL;
  2815. }
  2816. return hr;
  2817. }
  2818. int _cdecl CompareWCHAR(const void *arg1, const void *arg2)
  2819. {
  2820. int wch1 = *((WCHAR *) arg1);
  2821. int wch2 = *((WCHAR *) arg2);
  2822. return (wch1 - wch2);
  2823. }
  2824. // GetUnicodeRanges
  2825. //
  2826. // Parameters:
  2827. // hrec [in] : Handle to the recognizer
  2828. // pcRanges [in/out] : Count of ranges
  2829. // pcr [out] : Array of character ranges
  2830. ////////////////////////////////////////////////////////////////////////////////
  2831. HRESULT WINAPI GetUnicodeRanges(HRECOGNIZER hrec,
  2832. ULONG *pcRanges,
  2833. CHARACTER_RANGE *pcr)
  2834. {
  2835. struct WispRec *pRec;
  2836. WCHAR *aw;
  2837. int cChar, i;
  2838. ULONG cRange, iRange = 0;
  2839. HRESULT hr = S_OK;
  2840. // Check the recognizer handle
  2841. pRec = (struct WispRec*)FindTpgHandle((HANDLE)hrec, TPG_HRECOGNIZER);
  2842. if (NULL == pRec)
  2843. {
  2844. return E_INVALIDARG;
  2845. }
  2846. if (IsBadWritePtr(pcRanges, sizeof(ULONG)))
  2847. {
  2848. return E_POINTER;
  2849. }
  2850. if (pcr != NULL && IsBadWritePtr(pcr, sizeof(CHARACTER_RANGE) * (*pcRanges)))
  2851. {
  2852. return E_POINTER;
  2853. }
  2854. cChar = g_locRunInfo.cCodePoints - 1;
  2855. aw = (WCHAR *) ExternAlloc(cChar * sizeof(WCHAR));
  2856. if (!aw)
  2857. {
  2858. return E_OUTOFMEMORY;
  2859. }
  2860. for (i = 1; i < g_locRunInfo.cCodePoints; i++)
  2861. {
  2862. aw[i - 1] = LocRunDense2Unicode(&g_locRunInfo, (WCHAR) i);
  2863. }
  2864. if (cChar == 0)
  2865. {
  2866. cRange = 0;
  2867. }
  2868. else
  2869. {
  2870. qsort((void*)aw, (size_t)cChar, sizeof(WCHAR), CompareWCHAR);
  2871. // count the ranges
  2872. cRange = 1;
  2873. for (i = 1; i < cChar; i++)
  2874. {
  2875. if (aw[i] > aw[i - 1] + 1)
  2876. cRange++;
  2877. }
  2878. }
  2879. if (!pcr) // Need only a count of ranges
  2880. {
  2881. *pcRanges = cRange;
  2882. goto cleanup;
  2883. }
  2884. if (*pcRanges < cRange)
  2885. {
  2886. hr = TPC_E_INSUFFICIENT_BUFFER;
  2887. goto cleanup;
  2888. }
  2889. if (*pcRanges > cRange)
  2890. {
  2891. *pcRanges = cRange;
  2892. }
  2893. if (*pcRanges > 0)
  2894. {
  2895. // convert the array of Unicode values to an array of CHARACTER_RANGEs
  2896. pcr[iRange].wcLow = aw[0];
  2897. pcr[iRange].cChars = 1;
  2898. for (i = 1; i < cChar; i++)
  2899. {
  2900. if (aw[i] == aw[i - 1] + 1)
  2901. pcr[iRange].cChars++;
  2902. else
  2903. {
  2904. if (iRange >= (*pcRanges) - 1)
  2905. {
  2906. break;
  2907. }
  2908. iRange++;
  2909. pcr[iRange].wcLow = aw[i];
  2910. pcr[iRange].cChars = 1;
  2911. }
  2912. }
  2913. ASSERT(iRange == (*pcRanges) - 1);
  2914. }
  2915. cleanup:
  2916. ExternFree(aw);
  2917. return hr;
  2918. }
  2919. // GetEnabledUnicodeRanges
  2920. //
  2921. // Parameters:
  2922. // hrc [in] : Handle to the recognition context
  2923. // pcRanges [in/out] : Count of ranges
  2924. // pcr [out] : Array of character ranges
  2925. ////////////////////////////////////////////////////////////////////////////////
  2926. HRESULT WINAPI GetEnabledUnicodeRanges(HRECOCONTEXT hrc,
  2927. ULONG *pcRanges,
  2928. CHARACTER_RANGE *pcr)
  2929. {
  2930. VRC *pVRC;
  2931. CHARSET charset;
  2932. WCHAR *aw;
  2933. int cChar, i;
  2934. ULONG cRange, iRange = 0;
  2935. HRESULT hr = S_OK;
  2936. BOOL fCreatedHRC = FALSE;
  2937. struct WispContext *wisphrc;
  2938. // validate the correpsonding hrc pointer
  2939. wisphrc = (struct WispContext *) FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  2940. if (wisphrc == NULL)
  2941. {
  2942. return E_INVALIDARG;
  2943. }
  2944. if (IsBadWritePtr(pcRanges, sizeof(ULONG)))
  2945. {
  2946. return E_POINTER;
  2947. }
  2948. if (pcr != NULL && IsBadWritePtr(pcr, sizeof(CHARACTER_RANGE) * *(pcRanges)))
  2949. {
  2950. return E_POINTER;
  2951. }
  2952. cChar = 0;
  2953. aw = (WCHAR *) ExternAlloc((g_locRunInfo.cCodePoints - 1) * sizeof(WCHAR));
  2954. if (!aw)
  2955. return E_OUTOFMEMORY;
  2956. // If we have an HRC, the factoid should be set already. If not, we'll have
  2957. // to make one.
  2958. if (wisphrc->hrc == NULL)
  2959. {
  2960. fCreatedHRC = TRUE;
  2961. hr = CreateHRCinContext(wisphrc);
  2962. }
  2963. pVRC = (VRC *) wisphrc->hrc;
  2964. if (SUCCEEDED(hr) && pVRC != NULL)
  2965. {
  2966. charset.recmask = pVRC->pLattice->alcFactoid;
  2967. charset.pbAllowedChars = pVRC->pLattice->pbFactoidChars;
  2968. for (i = 1; i < g_locRunInfo.cCodePoints; i++)
  2969. {
  2970. if (IsAllowedChar(&g_locRunInfo, &charset, (WCHAR) i))
  2971. {
  2972. aw[cChar++] = LocRunDense2Unicode(&g_locRunInfo, (WCHAR) i);
  2973. }
  2974. }
  2975. }
  2976. if (fCreatedHRC)
  2977. {
  2978. // Destroy the HRC if we created one.
  2979. if (wisphrc->hrc != NULL)
  2980. {
  2981. if (wisphrc->bIsBoxed)
  2982. HwxDestroy(wisphrc->hrc);
  2983. else
  2984. DestroyHRC(wisphrc->hrc);
  2985. wisphrc->hrc = NULL;
  2986. }
  2987. }
  2988. if (cChar == 0)
  2989. {
  2990. cRange = 0;
  2991. }
  2992. else
  2993. {
  2994. qsort((void*)aw, (size_t)cChar, sizeof(WCHAR), CompareWCHAR);
  2995. // count the ranges
  2996. cRange = 1;
  2997. for (i = 1; i < cChar; i++)
  2998. {
  2999. if (aw[i] > aw[i - 1] + 1)
  3000. cRange++;
  3001. }
  3002. }
  3003. if (!pcr) // Need only a count of ranges
  3004. {
  3005. *pcRanges = cRange;
  3006. goto cleanup;
  3007. }
  3008. if (*pcRanges < cRange)
  3009. {
  3010. hr = TPC_E_INSUFFICIENT_BUFFER;
  3011. goto cleanup;
  3012. }
  3013. if (*pcRanges > cRange)
  3014. {
  3015. *pcRanges = cRange;
  3016. }
  3017. if (*pcRanges > 0)
  3018. {
  3019. // convert the array of Unicode values to an array of CHARACTER_RANGEs
  3020. pcr[iRange].wcLow = aw[0];
  3021. pcr[iRange].cChars = 1;
  3022. for (i = 1; i < cChar; i++)
  3023. {
  3024. if (aw[i] == aw[i - 1] + 1)
  3025. pcr[iRange].cChars++;
  3026. else
  3027. {
  3028. if (iRange >= (*pcRanges) - 1)
  3029. {
  3030. break;
  3031. }
  3032. iRange++;
  3033. pcr[iRange].wcLow = aw[i];
  3034. pcr[iRange].cChars = 1;
  3035. }
  3036. }
  3037. ASSERT(iRange == (*pcRanges) - 1);
  3038. }
  3039. cleanup:
  3040. ExternFree(aw);
  3041. return hr;
  3042. }
  3043. // SetEnabledUnicodeRanges
  3044. //
  3045. // Parameters:
  3046. // hrc [in] : Handle to the recognition context
  3047. // pcRanges [in/out] : Count of ranges
  3048. // pcr [out] : Array of character ranges
  3049. ////////////////////////////////////////////////////////////////////////////////
  3050. HRESULT WINAPI SetEnabledUnicodeRanges(HRECOCONTEXT hrc,
  3051. ULONG cRanges,
  3052. CHARACTER_RANGE *pcr)
  3053. {
  3054. struct WispContext *wisphrc;
  3055. // validate the correpsonding hrc pointer
  3056. wisphrc = (struct WispContext *) FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  3057. if (wisphrc == NULL)
  3058. {
  3059. return E_INVALIDARG;
  3060. }
  3061. if (IsBadReadPtr(pcr, sizeof(CHARACTER_RANGE) * cRanges))
  3062. {
  3063. return E_POINTER;
  3064. }
  3065. return E_NOTIMPL;
  3066. }
  3067. ////////////////////////
  3068. // IAlternate
  3069. ////////////////////////
  3070. HRESULT WINAPI GetString(HRECOALT hrcalt, RECO_RANGE *pRecoRange, ULONG* pcSize, WCHAR* szString)
  3071. {
  3072. struct WispContext *wisphrc;
  3073. struct WispAlternate *wispalt;
  3074. VRC *vrc = NULL;
  3075. HRESULT hr = S_OK;
  3076. ULONG i = 0, ulSize = 0;
  3077. // find the handle and validate the correpsonding pointer
  3078. wispalt = (struct WispAlternate*)FindTpgHandle((HANDLE)hrcalt, TPG_HRECOALT);
  3079. if (NULL == wispalt)
  3080. {
  3081. return E_INVALIDARG;
  3082. }
  3083. // validate the correpsonding hrc pointer
  3084. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)wispalt->hrc, TPG_HRECOCONTEXT);
  3085. if (wisphrc == NULL)
  3086. {
  3087. return E_INVALIDARG;
  3088. }
  3089. if (pRecoRange && IsBadWritePtr(pRecoRange, sizeof(RECO_RANGE)))
  3090. {
  3091. return E_POINTER;
  3092. }
  3093. if (IsBadWritePtr(pcSize, sizeof(ULONG)))
  3094. {
  3095. return E_POINTER;
  3096. }
  3097. // if the string pointer is not null, validate it
  3098. if (szString && IsBadWritePtr(szString, (*pcSize) * sizeof (*szString)))
  3099. {
  3100. return E_POINTER;
  3101. }
  3102. vrc = (VRC*)wisphrc->hrc;
  3103. if (!vrc)
  3104. {
  3105. return E_POINTER;
  3106. }
  3107. if (pRecoRange)
  3108. {
  3109. pRecoRange->iwcBegin = wispalt->OriginalRecoRange.iwcBegin;
  3110. pRecoRange->cCount = wispalt->OriginalRecoRange.cCount;
  3111. }
  3112. ulSize = (ULONG)wispalt->iLength;
  3113. if (!szString)
  3114. {
  3115. *pcSize = ulSize;
  3116. return S_OK;
  3117. }
  3118. if (*pcSize > ulSize)
  3119. {
  3120. *pcSize = ulSize;
  3121. }
  3122. if (*pcSize < ulSize)
  3123. {
  3124. hr = TPC_S_TRUNCATED;
  3125. }
  3126. // Fill the string
  3127. for (i = 0; i<*pcSize; i++)
  3128. {
  3129. // Fill the characters in the string
  3130. if (wispalt->pIndexInColumn[i] == SPACE_ALT_ID)
  3131. {
  3132. szString[i] = SYM_SPACE;
  3133. }
  3134. else
  3135. if (vrc->pLattice->pAltList[wispalt->pColumnIndex[i]].alts[wispalt->pIndexInColumn[i]].wChar != SYM_UNKNOWN)
  3136. {
  3137. szString[i] = LocRunDense2Unicode(&g_locRunInfo,
  3138. vrc->pLattice->pAltList[wispalt->pColumnIndex[i]].alts[wispalt->pIndexInColumn[i]].wChar);
  3139. }
  3140. else
  3141. {
  3142. szString[i] = SYM_UNKNOWN;
  3143. }
  3144. }
  3145. return hr;
  3146. }
  3147. HRESULT WINAPI GetStrokeRanges(HRECOALT hrcalt, RECO_RANGE* pRecoRange, ULONG* pcRanges, STROKE_RANGE* pStrokeRange)
  3148. {
  3149. HRESULT hr = S_OK;
  3150. RECO_RANGE recoRange;
  3151. struct WispContext *wisphrc;
  3152. struct WispAlternate *wispalt;
  3153. VRC *vrc;
  3154. ULONG ulStrokeCount = 0, ulCurrentIndex = 0;
  3155. ULONG i = 0;
  3156. int j = 0;
  3157. int k = 0;
  3158. ULONG *StrokeArray = NULL;
  3159. ULONG ulStrokeRangesCount = 0;
  3160. ULONG iCurrentStrokeRange = 0;
  3161. // find the handle and validate the corresponding pointer
  3162. wispalt = (struct WispAlternate*)FindTpgHandle((HANDLE)hrcalt, TPG_HRECOALT);
  3163. if (NULL == wispalt)
  3164. {
  3165. return E_INVALIDARG;
  3166. }
  3167. // validate the corresponding hrc pointer
  3168. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)wispalt->hrc, TPG_HRECOCONTEXT);
  3169. if (wisphrc == NULL)
  3170. {
  3171. return E_INVALIDARG;
  3172. }
  3173. vrc = (VRC *) wisphrc->hrc;
  3174. if (vrc == NULL)
  3175. {
  3176. return E_INVALIDARG;
  3177. }
  3178. if (IsBadReadPtr(pRecoRange, sizeof(RECO_RANGE)))
  3179. {
  3180. return E_POINTER;
  3181. }
  3182. if (IsBadWritePtr(pcRanges, sizeof(ULONG)))
  3183. {
  3184. return E_POINTER;
  3185. }
  3186. // validate pointer if not null
  3187. if (pStrokeRange && IsBadWritePtr(pStrokeRange, (*pcRanges) * sizeof (*pStrokeRange)))
  3188. {
  3189. return E_POINTER;
  3190. }
  3191. recoRange = *pRecoRange;
  3192. if (!recoRange.cCount)
  3193. {
  3194. *pcRanges = 0;
  3195. return S_OK;
  3196. }
  3197. if (recoRange.iwcBegin + recoRange.cCount > (ULONG)wispalt->iLength)
  3198. return E_INVALIDARG;
  3199. // Get the number of strokes
  3200. for (i = recoRange.iwcBegin; i < recoRange.iwcBegin + recoRange.cCount; i++)
  3201. {
  3202. // Make sure we're not looking at a space
  3203. if (wispalt->pIndexInColumn[i] != SPACE_ALT_ID)
  3204. {
  3205. // There might be some stroke that have been merged!
  3206. // We need to examine every single column to know if strokes have been merged
  3207. // (and also how many times)
  3208. for (j = 0; j < vrc->pLattice->pAltList[wispalt->pColumnIndex[i]].alts[wispalt->pIndexInColumn[i]].nStrokes; j++)
  3209. {
  3210. ulStrokeCount += vrc->pLattice->pStroke[wispalt->pColumnIndex[i]-j].iLast -
  3211. vrc->pLattice->pStroke[wispalt->pColumnIndex[i]-j].iOrder + 1;
  3212. }
  3213. }
  3214. }
  3215. // If there aren't any strokes, then exit early.
  3216. // (The rest of this function behaves badly if there are no strokes or no ranges.)
  3217. if (ulStrokeCount == 0)
  3218. {
  3219. *pcRanges = 0;
  3220. return S_OK;
  3221. }
  3222. // Allocate the array of strokes
  3223. StrokeArray = (ULONG*)ExternAlloc(sizeof(ULONG)*ulStrokeCount);
  3224. if (!StrokeArray)
  3225. {
  3226. ASSERT(StrokeArray);
  3227. return E_OUTOFMEMORY;
  3228. }
  3229. // Get the array of strokes
  3230. ulCurrentIndex = 0;
  3231. for (i = recoRange.iwcBegin; i < recoRange.iwcBegin + recoRange.cCount; i++)
  3232. {
  3233. // Make sure we're not looking at a space
  3234. if (wispalt->pIndexInColumn[i] != SPACE_ALT_ID)
  3235. {
  3236. // This loop goes backwards so we get the strokes in order
  3237. for (j = vrc->pLattice->pAltList[wispalt->pColumnIndex[i]].alts[wispalt->pIndexInColumn[i]].nStrokes - 1; j >= 0; j--)
  3238. {
  3239. // There might more than one stroke here
  3240. // because of the possibility of a stroke merge!!!
  3241. for (k = vrc->pLattice->pStroke[wispalt->pColumnIndex[i]-j].iOrder;
  3242. k <= vrc->pLattice->pStroke[wispalt->pColumnIndex[i]-j].iLast;
  3243. k++)
  3244. {
  3245. StrokeArray[ulCurrentIndex] = k;
  3246. ulCurrentIndex++;
  3247. }
  3248. }
  3249. }
  3250. }
  3251. // Sort the array
  3252. SlowSort(StrokeArray, ulStrokeCount);
  3253. // Get the number of STROKE_RANGES needed
  3254. ulStrokeRangesCount = 1;
  3255. for (i = 1; i<ulStrokeCount; i++)
  3256. {
  3257. if (StrokeArray[i-1]+1!=StrokeArray[i]) ulStrokeRangesCount++;
  3258. }
  3259. // Return the count is this is all that is asked
  3260. if (!pStrokeRange)
  3261. {
  3262. *pcRanges = ulStrokeRangesCount;
  3263. ExternFree(StrokeArray);
  3264. return S_OK;
  3265. }
  3266. if (*pcRanges < ulStrokeRangesCount)
  3267. {
  3268. ExternFree(StrokeArray);
  3269. return TPC_E_INSUFFICIENT_BUFFER;
  3270. }
  3271. // Fill in the Strokes
  3272. iCurrentStrokeRange = 0;
  3273. pStrokeRange[iCurrentStrokeRange].iStrokeBegin = StrokeArray[0];
  3274. pStrokeRange[ulStrokeRangesCount-1].iStrokeEnd = StrokeArray[ulStrokeCount-1];
  3275. for(i = 1; i < ulStrokeCount; i++)
  3276. {
  3277. if (StrokeArray[i-1]+1!=StrokeArray[i])
  3278. {
  3279. pStrokeRange[iCurrentStrokeRange].iStrokeEnd = StrokeArray[i-1];
  3280. iCurrentStrokeRange++;
  3281. if (iCurrentStrokeRange == ulStrokeRangesCount) break;
  3282. pStrokeRange[iCurrentStrokeRange].iStrokeBegin = StrokeArray[i];
  3283. }
  3284. }
  3285. *pcRanges = ulStrokeRangesCount;
  3286. ExternFree(StrokeArray);
  3287. return hr;
  3288. }
  3289. typedef struct AltScore
  3290. {
  3291. float flScore;
  3292. int iAlt;
  3293. BOOL fCurrentPath;
  3294. } AltScore;
  3295. int __cdecl CompareAltScore(const void *p1, const void *p2)
  3296. {
  3297. const AltScore *pAlt1 = (const AltScore *) p1;
  3298. const AltScore *pAlt2 = (const AltScore *) p2;
  3299. if (pAlt1->fCurrentPath) return -1;
  3300. if (pAlt2->fCurrentPath) return 1;
  3301. if (pAlt1->flScore > pAlt2->flScore) return -1;
  3302. if (pAlt1->flScore < pAlt2->flScore) return 1;
  3303. return 0;
  3304. }
  3305. HRESULT WINAPI GetSegmentAlternateList(HRECOALT hrcalt, RECO_RANGE* pRecoRange, ULONG *pcAlts, HRECOALT* pAlts)
  3306. {
  3307. HRESULT hr = S_OK;
  3308. struct WispAlternate *wispalt;
  3309. struct WispContext *wisphrc;
  3310. VRC *vrc;
  3311. ULONG ulMaxAlt = 0;
  3312. struct WispAlternate *pWispAlt = NULL;
  3313. int i = 0;
  3314. int j = 0;
  3315. ULONG ulCurrentIndex = 0;
  3316. // find the handle and validate the correpsonding pointer
  3317. wispalt = (struct WispAlternate*)FindTpgHandle((HANDLE)hrcalt, TPG_HRECOALT);
  3318. if (NULL == wispalt)
  3319. {
  3320. return E_INVALIDARG;
  3321. }
  3322. // validate the corresponding hrc pointer
  3323. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)wispalt->hrc, TPG_HRECOCONTEXT);
  3324. if (wisphrc == NULL)
  3325. {
  3326. return E_INVALIDARG;
  3327. }
  3328. vrc = (VRC *) wisphrc->hrc;
  3329. if (vrc == NULL)
  3330. {
  3331. return E_INVALIDARG;
  3332. }
  3333. if (IsBadWritePtr(pRecoRange, sizeof(RECO_RANGE)))
  3334. {
  3335. return E_POINTER;
  3336. }
  3337. if (IsBadWritePtr(pcAlts, sizeof(ULONG)))
  3338. {
  3339. return E_POINTER;
  3340. }
  3341. if (pAlts && IsBadWritePtr(pAlts, (*pcAlts) * sizeof (*pAlts)))
  3342. {
  3343. return E_POINTER;
  3344. }
  3345. // Check that the pRecoRange is valid:
  3346. if (pRecoRange->cCount == 0 ||
  3347. pRecoRange->iwcBegin + pRecoRange->cCount > (ULONG)wispalt->iLength)
  3348. {
  3349. return E_INVALIDARG;
  3350. }
  3351. // Get the number of alternates for this character (with the same number of strokes!!!)
  3352. ulMaxAlt = 0;
  3353. // if this char is a space
  3354. if (wispalt->pIndexInColumn[pRecoRange->iwcBegin] == SPACE_ALT_ID)
  3355. {
  3356. if (!pAlts)
  3357. {
  3358. *pcAlts = 1;
  3359. }
  3360. if (pAlts && *pcAlts > 0)
  3361. {
  3362. pWispAlt = (struct WispAlternate *)ExternAlloc(sizeof(struct WispAlternate));
  3363. if (!pWispAlt)
  3364. {
  3365. return E_OUTOFMEMORY;
  3366. }
  3367. pWispAlt->hrc = wispalt->hrc;
  3368. pWispAlt->iLength = 1;
  3369. pWispAlt->iNumberOfColumns = 1;
  3370. pWispAlt->OriginalRecoRange = *pRecoRange;
  3371. pWispAlt->pColumnIndex = NULL;
  3372. pWispAlt->pIndexInColumn = NULL;
  3373. pWispAlt->pColumnIndex = ExternAlloc(sizeof(int));
  3374. pWispAlt->pIndexInColumn = ExternAlloc(sizeof(int));
  3375. if (!pWispAlt->pColumnIndex || !pWispAlt->pIndexInColumn)
  3376. {
  3377. // Problem allocating memory, unallocate the array
  3378. HRESULT hrDA = DestroyAlternateInternal(pWispAlt);
  3379. ASSERT(SUCCEEDED(hrDA));
  3380. return E_OUTOFMEMORY;
  3381. }
  3382. pWispAlt->pColumnIndex[0] = wispalt->pColumnIndex[pRecoRange->iwcBegin];
  3383. pWispAlt->pIndexInColumn[0] = SPACE_ALT_ID;
  3384. pAlts[0] = (HRECOALT) CreateTpgHandle(TPG_HRECOALT, pWispAlt);
  3385. if (pAlts[0] == NULL)
  3386. {
  3387. // Problem allocating memory, unallocate the array
  3388. HRESULT hrDA = DestroyAlternateInternal(pWispAlt);
  3389. ASSERT(SUCCEEDED(hrDA));
  3390. return E_OUTOFMEMORY;
  3391. }
  3392. *pcAlts = 1;
  3393. }
  3394. }
  3395. else
  3396. {
  3397. AltScore *pAltScores;
  3398. int iCurrentPath = -1;
  3399. for (i = 0; i < vrc->pLattice->pAltList[wispalt->pColumnIndex[pRecoRange->iwcBegin]].nUsed; i++)
  3400. {
  3401. if (vrc->pLattice->pAltList[wispalt->pColumnIndex[pRecoRange->iwcBegin]].alts[i].nStrokes ==
  3402. vrc->pLatticePath->pElem[pRecoRange->iwcBegin].nStrokes)
  3403. ulMaxAlt++;
  3404. }
  3405. if (!pAlts)
  3406. {
  3407. *pcAlts = ulMaxAlt;
  3408. return S_OK;
  3409. }
  3410. pAltScores = (AltScore *) ExternAlloc(sizeof(AltScore) * ulMaxAlt);
  3411. if (pAltScores == NULL)
  3412. {
  3413. return E_OUTOFMEMORY;
  3414. }
  3415. ulCurrentIndex = 0;
  3416. for (i = 0; i < vrc->pLattice->pAltList[wispalt->pColumnIndex[pRecoRange->iwcBegin]].nUsed; i++)
  3417. {
  3418. // Do the stuff only when we have the same number of strokes
  3419. if (vrc->pLattice->pAltList[wispalt->pColumnIndex[pRecoRange->iwcBegin]].alts[i].nStrokes ==
  3420. vrc->pLatticePath->pElem[pRecoRange->iwcBegin].nStrokes)
  3421. {
  3422. pAltScores[ulCurrentIndex].fCurrentPath = vrc->pLattice->pAltList[wispalt->pColumnIndex[pRecoRange->iwcBegin]].alts[i].fCurrentPath;
  3423. pAltScores[ulCurrentIndex].iAlt = i;
  3424. pAltScores[ulCurrentIndex].flScore =
  3425. vrc->pLattice->pAltList[wispalt->pColumnIndex[pRecoRange->iwcBegin]].alts[i].logProbPath;
  3426. // GetScore(vrc->pLattice, wispalt->pColumnIndex[pRecoRange->iwcBegin], i);
  3427. ulCurrentIndex++;
  3428. }
  3429. }
  3430. qsort(pAltScores, ulMaxAlt, sizeof(AltScore), CompareAltScore);
  3431. if (*pcAlts>ulMaxAlt) *pcAlts = ulMaxAlt;
  3432. // Fill in the array of alternates
  3433. for (i = 0; i < (int)(*pcAlts); i++)
  3434. {
  3435. // Create an alternate
  3436. pWispAlt = (struct WispAlternate *)ExternAlloc(sizeof(struct WispAlternate));
  3437. if (!pWispAlt)
  3438. {
  3439. // Problem allocating memory, unallocate the array
  3440. ExternFree(pAltScores);
  3441. for (j = 0; j < i; j++)
  3442. {
  3443. HRESULT hrDA = DestroyAlternate(pAlts[j]);
  3444. ASSERT(SUCCEEDED(hrDA));
  3445. }
  3446. return E_OUTOFMEMORY;
  3447. }
  3448. pWispAlt->hrc = wispalt->hrc;
  3449. pWispAlt->iLength = 1;
  3450. pWispAlt->iNumberOfColumns = 1;
  3451. pWispAlt->OriginalRecoRange = *pRecoRange;
  3452. pWispAlt->pColumnIndex = NULL;
  3453. pWispAlt->pIndexInColumn = NULL;
  3454. pWispAlt->pColumnIndex = ExternAlloc(sizeof(int));
  3455. pWispAlt->pIndexInColumn = ExternAlloc(sizeof(int));
  3456. if (!pWispAlt->pColumnIndex || !pWispAlt->pIndexInColumn)
  3457. {
  3458. // Problem allocating memory, unallocate the array
  3459. ExternFree(pAltScores);
  3460. for (j = 0; j < i; j++)
  3461. {
  3462. HRESULT hrDA = DestroyAlternate(pAlts[j]);
  3463. ASSERT(SUCCEEDED(hrDA));
  3464. }
  3465. return E_OUTOFMEMORY;
  3466. }
  3467. pWispAlt->pColumnIndex[0] = wispalt->pColumnIndex[pRecoRange->iwcBegin];
  3468. pWispAlt->pIndexInColumn[0] = pAltScores[i].iAlt;
  3469. pAlts[i] = CreateTpgHandle(TPG_HRECOALT, pWispAlt);
  3470. if (pAlts[i] == NULL)
  3471. {
  3472. // Problem allocating memory, unallocate the array
  3473. ExternFree(pAltScores);
  3474. for (j = 0; j < i; j++)
  3475. {
  3476. HRESULT hrDA = DestroyAlternate(pAlts[j]);
  3477. ASSERT(SUCCEEDED(hrDA));
  3478. }
  3479. return E_OUTOFMEMORY;
  3480. }
  3481. }
  3482. ExternFree(pAltScores);
  3483. }
  3484. pRecoRange->cCount = 1;
  3485. return hr;
  3486. }
  3487. HRESULT WINAPI GetMetrics(HRECOALT hrcalt, RECO_RANGE* pRecoRange, LINE_METRICS lm, LINE_SEGMENT* pls)
  3488. {
  3489. struct WispAlternate *wispalt;
  3490. struct WispContext *wisphrc;
  3491. VRC *vrc;
  3492. HRESULT hr = S_OK;
  3493. ULONG index = 0;
  3494. LONG lPrevChar = 0;
  3495. float flTotalY = 0;
  3496. int cCharacters = 0;
  3497. // find the handle and validate the correpsonding pointer
  3498. wispalt = (struct WispAlternate*)FindTpgHandle((HANDLE)hrcalt, TPG_HRECOALT);
  3499. if (NULL == wispalt)
  3500. {
  3501. return E_INVALIDARG;
  3502. }
  3503. // validate the corresponding hrc pointer
  3504. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)wispalt->hrc, TPG_HRECOCONTEXT);
  3505. if (wisphrc == NULL)
  3506. {
  3507. return E_INVALIDARG;
  3508. }
  3509. vrc = (VRC *) wisphrc->hrc;
  3510. if (vrc == NULL)
  3511. {
  3512. return E_INVALIDARG;
  3513. }
  3514. if (IsBadWritePtr(pRecoRange, sizeof(RECO_RANGE)))
  3515. {
  3516. return E_POINTER;
  3517. }
  3518. if (IsBadWritePtr(pls, sizeof(LINE_SEGMENT)))
  3519. {
  3520. return E_POINTER;
  3521. }
  3522. if (pRecoRange->cCount == 0 ||
  3523. pRecoRange->iwcBegin+pRecoRange->cCount > (ULONG)wispalt->iLength)
  3524. {
  3525. return E_INVALIDARG;
  3526. }
  3527. // First trim spaces (if any) from the beginning and end of the reco range
  3528. while (pRecoRange->cCount > 0 && wispalt->pIndexInColumn[pRecoRange->iwcBegin] == SPACE_ALT_ID)
  3529. {
  3530. pRecoRange->iwcBegin++;
  3531. pRecoRange->cCount--;
  3532. }
  3533. while (pRecoRange->cCount > 0 && wispalt->pIndexInColumn[pRecoRange->iwcBegin + pRecoRange->cCount - 1] == SPACE_ALT_ID)
  3534. {
  3535. pRecoRange->cCount--;
  3536. }
  3537. // If the range only contained spaces, then return an error
  3538. if (pRecoRange->cCount == 0)
  3539. {
  3540. return TPC_E_NOT_RELEVANT;
  3541. }
  3542. index = 0;
  3543. lPrevChar = 0;
  3544. // Right now we only work from left to right, top to bottom so we will compare the x coordinates
  3545. // of two consecutive writing boxes to know if we have a new line
  3546. while (index < pRecoRange->cCount)
  3547. {
  3548. int iColumn = wispalt->pColumnIndex[pRecoRange->iwcBegin+index];
  3549. int iIndexInColumn = wispalt->pIndexInColumn[pRecoRange->iwcBegin+index];
  3550. // Skip over spaces in the range
  3551. if (iIndexInColumn == SPACE_ALT_ID)
  3552. {
  3553. index++;
  3554. continue;
  3555. }
  3556. // Check if we switched to a new line
  3557. if (index > 0 &&
  3558. lPrevChar > vrc->pLattice->pAltList[iColumn].alts[iIndexInColumn].writingBox.left)
  3559. {
  3560. // Looks like we moved to a new line, modify the reco range to end here and exit the loop.
  3561. pRecoRange->cCount = index;
  3562. // Back up over spaces until we reach the actual end of the previous line
  3563. while (pRecoRange->cCount > 0 && wispalt->pIndexInColumn[pRecoRange->iwcBegin + pRecoRange->cCount - 1] == SPACE_ALT_ID)
  3564. {
  3565. pRecoRange->cCount--;
  3566. }
  3567. break;
  3568. }
  3569. lPrevChar = vrc->pLattice->pAltList[iColumn].alts[iIndexInColumn].writingBox.left;
  3570. switch(lm)
  3571. {
  3572. case LM_BASELINE:
  3573. case LM_DESCENDER:
  3574. flTotalY += vrc->pLattice->pAltList[iColumn].alts[iIndexInColumn].writingBox.bottom;
  3575. break;
  3576. case LM_MIDLINE:
  3577. flTotalY += (vrc->pLattice->pAltList[iColumn].alts[iIndexInColumn].writingBox.bottom +
  3578. vrc->pLattice->pAltList[iColumn].alts[iIndexInColumn].writingBox.top) / 2;
  3579. break;
  3580. case LM_ASCENDER:
  3581. flTotalY += vrc->pLattice->pAltList[iColumn].alts[iIndexInColumn].writingBox.top;
  3582. break;
  3583. default:
  3584. ASSERT(!lm);
  3585. return E_INVALIDARG;
  3586. }
  3587. index++;
  3588. cCharacters++;
  3589. }
  3590. // Average the y positions
  3591. pls->PtA.y = (LONG) (flTotalY / cCharacters);
  3592. pls->PtB.y = pls->PtA.y;
  3593. // Set the x range based on the updated reco range
  3594. pls->PtA.x = vrc->pLattice->pAltList[wispalt->pColumnIndex[pRecoRange->iwcBegin]].alts[wispalt->pIndexInColumn[pRecoRange->iwcBegin]].writingBox.left;
  3595. pls->PtB.x = vrc->pLattice->pAltList[wispalt->pColumnIndex[pRecoRange->iwcBegin+pRecoRange->cCount-1]].alts[wispalt->pIndexInColumn[pRecoRange->iwcBegin+pRecoRange->cCount-1]].writingBox.right;
  3596. return hr;
  3597. }
  3598. HRESULT WINAPI GetGuideIndex(HRECOALT hrcalt, RECO_RANGE* pRecoRange, ULONG *piIndex)
  3599. {
  3600. struct WispAlternate *wispalt;
  3601. struct WispContext *wisphrc;
  3602. VRC *vrc;
  3603. // find the handle and validate the correpsonding pointer
  3604. wispalt = (struct WispAlternate*)FindTpgHandle((HANDLE)hrcalt, TPG_HRECOALT);
  3605. if (NULL == wispalt)
  3606. {
  3607. return E_INVALIDARG;
  3608. }
  3609. // validate the corresponding hrc pointer
  3610. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)wispalt->hrc, TPG_HRECOCONTEXT);
  3611. if (wisphrc == NULL)
  3612. {
  3613. return E_INVALIDARG;
  3614. }
  3615. vrc = (VRC *) wisphrc->hrc;
  3616. if (vrc == NULL)
  3617. {
  3618. return E_INVALIDARG;
  3619. }
  3620. if (IsBadWritePtr(pRecoRange, sizeof(RECO_RANGE)))
  3621. {
  3622. return E_POINTER;
  3623. }
  3624. if (IsBadWritePtr(piIndex, sizeof(ULONG)))
  3625. {
  3626. return E_POINTER;
  3627. }
  3628. if (!pRecoRange->cCount)
  3629. {
  3630. return E_INVALIDARG;
  3631. }
  3632. if (pRecoRange->iwcBegin + pRecoRange->cCount > (ULONG)wispalt->iLength)
  3633. {
  3634. return E_INVALIDARG;
  3635. }
  3636. if (!wisphrc->bIsBoxed)
  3637. {
  3638. return TPC_E_NOT_RELEVANT;
  3639. }
  3640. // Ok we are clean
  3641. pRecoRange->cCount = 1;
  3642. // Find the Guide index for this character
  3643. *piIndex = wisphrc->uiGuideIndex;
  3644. *piIndex += vrc->pLattice->pStroke[wispalt->pColumnIndex[pRecoRange->iwcBegin]].iBox;
  3645. // We should not have any spaces in boxed mode. But just in case, let's try to deal with
  3646. // them in a reasonable way, by assuming the space comes in the box immediately after
  3647. // the box containing the stroke.
  3648. if (wispalt->pIndexInColumn[pRecoRange->iwcBegin] == SPACE_ALT_ID)
  3649. {
  3650. (*piIndex)++;
  3651. }
  3652. return S_OK;
  3653. }
  3654. // If the specified character is a space or on the current path, return medium
  3655. // confidence. Otherwise return poor.
  3656. CONFIDENCE_LEVEL GetConfidenceLevelInternal(VRC *vrc, int iColumn, int iAlt)
  3657. {
  3658. if (iAlt == SPACE_ALT_ID || vrc->pLattice->pAltList[iColumn].alts[iAlt].fCurrentPath)
  3659. {
  3660. return CFL_INTERMEDIATE;
  3661. }
  3662. else
  3663. {
  3664. return CFL_POOR;
  3665. }
  3666. }
  3667. HRESULT WINAPI GetConfidenceLevel(HRECOALT hrcalt, RECO_RANGE* pRecoRange, CONFIDENCE_LEVEL* pcl)
  3668. {
  3669. struct WispAlternate *wispalt;
  3670. struct WispContext *wisphrc;
  3671. VRC *vrc;
  3672. // find the handle and validate the correpsonding pointer
  3673. wispalt = (struct WispAlternate*)FindTpgHandle((HANDLE)hrcalt, TPG_HRECOALT);
  3674. if (NULL == wispalt)
  3675. {
  3676. return E_INVALIDARG;
  3677. }
  3678. // validate the corresponding hrc pointer
  3679. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)wispalt->hrc, TPG_HRECOCONTEXT);
  3680. if (wisphrc == NULL)
  3681. {
  3682. return E_INVALIDARG;
  3683. }
  3684. vrc = (VRC *) wisphrc->hrc;
  3685. // Check the parameters
  3686. if (pRecoRange != NULL)
  3687. {
  3688. if (IsBadWritePtr(pRecoRange, sizeof(RECO_RANGE)))
  3689. {
  3690. return E_POINTER;
  3691. }
  3692. if (!pRecoRange->cCount)
  3693. {
  3694. return E_INVALIDARG;
  3695. }
  3696. if (pRecoRange->iwcBegin + pRecoRange->cCount > (ULONG)wispalt->iLength)
  3697. {
  3698. return E_INVALIDARG;
  3699. }
  3700. }
  3701. if (IsBadWritePtr(pcl, sizeof(CONFIDENCE_LEVEL)))
  3702. {
  3703. return E_POINTER;
  3704. }
  3705. #ifndef ENABLE_CONFIDENCE_LEVEL
  3706. // Ok we are clean
  3707. return E_NOTIMPL;
  3708. #else
  3709. if (pRecoRange != NULL)
  3710. {
  3711. // We only return confidence for single characters
  3712. pRecoRange->cCount = 1;
  3713. // If the specified character is a space or on the current path, return medium
  3714. // confidence. Otherwise return poor.
  3715. *pcl = GetConfidenceLevelInternal(vrc, wispalt->pColumnIndex[pRecoRange->iwcBegin], wispalt->pIndexInColumn[pRecoRange->iwcBegin]);
  3716. }
  3717. else
  3718. {
  3719. // If the first character is a space or on the current path, return medium
  3720. // confidence. Otherwise return poor.
  3721. *pcl = GetConfidenceLevelInternal(vrc, wispalt->pColumnIndex[0], wispalt->pIndexInColumn[0]);
  3722. }
  3723. return S_OK;
  3724. #endif
  3725. }
  3726. HRESULT WINAPI GetPropertyRanges(HRECOALT hrcalt, const GUID *pPropertyGuid, ULONG* pcRanges, RECO_RANGE* pRecoRange)
  3727. {
  3728. struct WispAlternate *wispalt;
  3729. struct WispContext *wisphrc;
  3730. VRC *vrc;
  3731. // find the handle and validate the correpsonding pointer
  3732. wispalt = (struct WispAlternate*)FindTpgHandle((HANDLE)hrcalt, TPG_HRECOALT);
  3733. if (NULL == wispalt)
  3734. {
  3735. return E_INVALIDARG;
  3736. }
  3737. // validate the corresponding hrc pointer
  3738. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)wispalt->hrc, TPG_HRECOCONTEXT);
  3739. if (wisphrc == NULL)
  3740. {
  3741. return E_INVALIDARG;
  3742. }
  3743. vrc = (VRC *) wisphrc->hrc;
  3744. if (IsBadReadPtr(pPropertyGuid, sizeof(GUID)))
  3745. {
  3746. return E_POINTER;
  3747. }
  3748. if (IsBadWritePtr(pcRanges, sizeof(ULONG)))
  3749. {
  3750. return E_POINTER;
  3751. }
  3752. if (pRecoRange != NULL && IsBadWritePtr(pRecoRange, sizeof(RECO_RANGE) * (*pcRanges)))
  3753. {
  3754. return E_POINTER;
  3755. }
  3756. if (IsEqualGUID(pPropertyGuid, &GUID_LINEMETRICS))
  3757. {
  3758. return TPC_E_NOT_RELEVANT;
  3759. }
  3760. #ifdef ENABLE_CONFIDENCE_LEVEL
  3761. if (IsEqualGUID(pPropertyGuid, &GUID_CONFIDENCELEVEL))
  3762. {
  3763. CONFIDENCE_LEVEL clPrev, clThis;
  3764. int iRange = 0;
  3765. int i;
  3766. for (i = 0; i < wispalt->iLength; i++)
  3767. {
  3768. clThis = GetConfidenceLevelInternal(vrc, wispalt->pColumnIndex[i], wispalt->pIndexInColumn[i]);
  3769. if (i == 0 || (clPrev != clThis))
  3770. {
  3771. iRange++;
  3772. }
  3773. clPrev = clThis;
  3774. }
  3775. if (pRecoRange != NULL && *pcRanges < (ULONG) iRange)
  3776. {
  3777. return TPC_E_INSUFFICIENT_BUFFER;
  3778. }
  3779. *pcRanges = iRange;
  3780. if (pRecoRange != NULL)
  3781. {
  3782. iRange = 0;
  3783. for (i = 0; i < wispalt->iLength; i++)
  3784. {
  3785. clThis = GetConfidenceLevelInternal(vrc, wispalt->pColumnIndex[i], wispalt->pIndexInColumn[i]);
  3786. if (i == 0 || (clPrev != clThis))
  3787. {
  3788. pRecoRange[iRange].iwcBegin = i;
  3789. pRecoRange[iRange].cCount = 1;
  3790. iRange++;
  3791. }
  3792. else
  3793. {
  3794. pRecoRange[iRange - 1].cCount++;
  3795. }
  3796. clPrev = clThis;
  3797. }
  3798. }
  3799. return S_OK;
  3800. }
  3801. #endif
  3802. return TPC_E_INVALID_PROPERTY;
  3803. }
  3804. HRESULT WINAPI GetRangePropertyValue(HRECOALT hrcalt, const GUID *pPropertyGuid, RECO_RANGE* pRecoRange, ULONG*pcbSize, BYTE* pProperty)
  3805. {
  3806. struct WispAlternate *wispalt;
  3807. struct WispContext *wisphrc;
  3808. VRC *vrc;
  3809. // find the handle and validate the correpsonding pointer
  3810. wispalt = (struct WispAlternate*)FindTpgHandle((HANDLE)hrcalt, TPG_HRECOALT);
  3811. if (NULL == wispalt)
  3812. {
  3813. return E_INVALIDARG;
  3814. }
  3815. // validate the corresponding hrc pointer
  3816. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)wispalt->hrc, TPG_HRECOCONTEXT);
  3817. if (wisphrc == NULL)
  3818. {
  3819. return E_INVALIDARG;
  3820. }
  3821. vrc = (VRC *) wisphrc->hrc;
  3822. if (IsBadReadPtr(pPropertyGuid, sizeof(GUID)))
  3823. {
  3824. return E_POINTER;
  3825. }
  3826. if (IsBadWritePtr(pRecoRange, sizeof(RECO_RANGE)))
  3827. {
  3828. return E_POINTER;
  3829. }
  3830. if (IsBadWritePtr(pcbSize, sizeof(ULONG)))
  3831. {
  3832. return E_POINTER;
  3833. }
  3834. if (pProperty != NULL && IsBadWritePtr(pProperty, *pcbSize))
  3835. {
  3836. return E_POINTER;
  3837. }
  3838. if (IsEqualGUID(pPropertyGuid, &GUID_LINEMETRICS))
  3839. {
  3840. HRESULT hr = S_OK;
  3841. LINE_SEGMENT baseline, midline;
  3842. if (pProperty == NULL)
  3843. {
  3844. *pcbSize = sizeof(LATTICE_METRICS);
  3845. }
  3846. else if (*pcbSize < sizeof(LATTICE_METRICS))
  3847. {
  3848. return TPC_E_INSUFFICIENT_BUFFER;
  3849. }
  3850. *pcbSize = sizeof(LATTICE_METRICS);
  3851. hr = GetMetrics(hrcalt, pRecoRange, LM_BASELINE, &baseline);
  3852. if (SUCCEEDED(hr))
  3853. {
  3854. hr = GetMetrics(hrcalt, pRecoRange, LM_MIDLINE, &midline);
  3855. }
  3856. if (SUCCEEDED(hr) && pProperty != NULL)
  3857. {
  3858. LATTICE_METRICS *plm = (LATTICE_METRICS *) pProperty;
  3859. plm->lsBaseline = baseline;
  3860. plm->iMidlineOffset = (short)(midline.PtA.y - baseline.PtA.y);
  3861. }
  3862. return hr;
  3863. }
  3864. #ifdef ENABLE_CONFIDENCE_LEVEL
  3865. if (IsEqualGUID(pPropertyGuid, &GUID_CONFIDENCELEVEL))
  3866. {
  3867. CONFIDENCE_LEVEL cl;
  3868. int iStart = pRecoRange->iwcBegin;
  3869. int iLimit = pRecoRange->iwcBegin + pRecoRange->cCount;
  3870. int i;
  3871. if (pProperty == NULL)
  3872. {
  3873. *pcbSize = sizeof(CONFIDENCE_LEVEL);
  3874. }
  3875. else if (*pcbSize < sizeof(CONFIDENCE_LEVEL))
  3876. {
  3877. return TPC_E_INSUFFICIENT_BUFFER;
  3878. }
  3879. *pcbSize = sizeof(CONFIDENCE_LEVEL);
  3880. cl = GetConfidenceLevelInternal(vrc, wispalt->pColumnIndex[iStart],
  3881. wispalt->pIndexInColumn[iStart]);
  3882. for (i = iStart + 1; i < iLimit; i++)
  3883. {
  3884. if (GetConfidenceLevelInternal(vrc, wispalt->pColumnIndex[i], wispalt->pIndexInColumn[i]) != cl)
  3885. {
  3886. pRecoRange->cCount = i - iStart;
  3887. break;
  3888. }
  3889. }
  3890. if (pProperty != NULL)
  3891. {
  3892. *((CONFIDENCE_LEVEL *) pProperty) = cl;
  3893. }
  3894. return S_OK;
  3895. }
  3896. #endif
  3897. return TPC_E_INVALID_PROPERTY;
  3898. }
  3899. void SortLatticeElements(RECO_LATTICE_ELEMENT *pStartElement, RECO_LATTICE_ELEMENT *pCurrent)
  3900. {
  3901. RECO_LATTICE_ELEMENT rleTemp;
  3902. BOOL bSwap = TRUE;
  3903. int iElementCount = pCurrent - pStartElement;
  3904. int i = 0, count = 0;
  3905. // For now just a quick bubble sort
  3906. count = 1;
  3907. while (bSwap)
  3908. {
  3909. bSwap = FALSE;
  3910. for (i = 0; i < iElementCount-count; i++)
  3911. {
  3912. if (pStartElement[i].score > pStartElement[i+1].score)
  3913. {
  3914. // swap
  3915. rleTemp = pStartElement[i];
  3916. pStartElement[i] = pStartElement[i+1];
  3917. pStartElement[i+1] = rleTemp;
  3918. bSwap = TRUE;
  3919. }
  3920. }
  3921. count++;
  3922. }
  3923. }
  3924. // GetLatticePtr
  3925. //
  3926. // Description: This method creates a Lattice structure for
  3927. // the recognition context and returns a pointer to it. The
  3928. // structure is going to be freed when the recognition
  3929. // context is destoyed or when a new call to GetLatticePtr
  3930. // is issued.
  3931. HRESULT WINAPI GetLatticePtr(HRECOCONTEXT hrc, RECO_LATTICE **ppLattice)
  3932. {
  3933. BOOL fNextColumnSpace = FALSE;
  3934. HRESULT hr = S_OK;
  3935. struct WispContext *wisphrc;
  3936. RECO_LATTICE_ELEMENT *pCurrent = NULL, *pStartElement = NULL, rleInPath;
  3937. int *pMapToLatticeColumn = NULL;
  3938. VRC *vrc = NULL;
  3939. int i = 0, j = 0, k = 0;
  3940. ULONG ulElementCount = 0, ulRealElementCount = 0;
  3941. ULONG ulMaxStroke = 0;
  3942. ULONG *pCurrentStroke = NULL;
  3943. ULONG ulBestResultIndex = 0;
  3944. RECO_LATTICE_ELEMENT *pCur = NULL;
  3945. int iStroke;
  3946. int iExternalColumn;
  3947. BYTE *pCurrentPropertyValue = NULL;
  3948. RECO_LATTICE_PROPERTY *pCurrentProperty = NULL;
  3949. RECO_LATTICE_PROPERTY *pConfidencePropStart = NULL;
  3950. RECO_LATTICE_PROPERTY **ppCurrentProperty = NULL;
  3951. LATTICE_METRICS *pLatticeMetrics;
  3952. FLOAT flScore;
  3953. // validate and destroy the handle & return the pointer
  3954. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  3955. if (NULL == wisphrc)
  3956. {
  3957. return E_INVALIDARG;
  3958. }
  3959. // Check the parameters
  3960. if (IsBadWritePtr(ppLattice, sizeof(RECO_LATTICE*)))
  3961. {
  3962. return E_POINTER;
  3963. }
  3964. *ppLattice = NULL;
  3965. // We should only do this if the lattice is dirty!
  3966. if (wisphrc->pLattice)
  3967. {
  3968. hr = FreeRecoLattice(wisphrc);
  3969. ASSERT(SUCCEEDED(hr));
  3970. hr = S_OK;
  3971. }
  3972. // Do we have results?
  3973. if (!wisphrc->hrc)
  3974. {
  3975. return TPC_E_NOT_RELEVANT;
  3976. }
  3977. vrc = (VRC*) wisphrc->hrc;
  3978. if (!vrc->pLatticePath)
  3979. {
  3980. return TPC_E_NOT_RELEVANT;
  3981. }
  3982. // Allocate the lattice
  3983. wisphrc->pLattice = (RECO_LATTICE*)ExternAlloc(sizeof(RECO_LATTICE));
  3984. if (!wisphrc->pLattice)
  3985. {
  3986. return E_OUTOFMEMORY;
  3987. }
  3988. // Initialize the RECO_LATTICE structure
  3989. ZeroMemory(wisphrc->pLattice, sizeof(RECO_LATTICE));
  3990. wisphrc->pLattice->pGuidProperties = NULL;
  3991. wisphrc->pLattice->ulPropertyCount = 0;
  3992. wisphrc->pLattice->ulColumnCount = vrc->pLattice->nStrokes;
  3993. wisphrc->pLattice->ulBestResultColumnCount = vrc->pLatticePath->nChars;
  3994. // Add columns to the lattice for each space
  3995. for (i = 0; i < vrc->pLattice->nStrokes; i++)
  3996. {
  3997. if (vrc->pLattice->pAltList[i].fSpaceAfterStroke)
  3998. {
  3999. wisphrc->pLattice->ulColumnCount++;
  4000. }
  4001. }
  4002. // For now we have only two properties: the GUID_CONFIDENCELEVEL and GUID_LINEMETRICS
  4003. wisphrc->pLattice->ulPropertyCount = 1;
  4004. #ifdef ENABLE_CONFIDENCE_LEVEL
  4005. wisphrc->pLattice->ulPropertyCount++;
  4006. #endif
  4007. wisphrc->pLattice->pGuidProperties = (GUID *) ExternAlloc(wisphrc->pLattice->ulPropertyCount * sizeof(GUID));
  4008. if (wisphrc->pLattice->pGuidProperties == NULL)
  4009. {
  4010. HRESULT hrFRL = FreeRecoLattice(wisphrc);
  4011. ASSERT(SUCCEEDED(hrFRL));
  4012. return E_OUTOFMEMORY;
  4013. }
  4014. wisphrc->pLattice->pGuidProperties[0] = GUID_LINEMETRICS;
  4015. #ifdef ENABLE_CONFIDENCE_LEVEL
  4016. wisphrc->pLattice->pGuidProperties[1] = GUID_CONFIDENCELEVEL;
  4017. #endif
  4018. if (wisphrc->pLattice->ulColumnCount)
  4019. {
  4020. // Allocating the array of LatticeColumns
  4021. wisphrc->pLattice->pLatticeColumns =
  4022. ExternAlloc(wisphrc->pLattice->ulColumnCount * sizeof(RECO_LATTICE_COLUMN));
  4023. if (wisphrc->pLattice->pLatticeColumns)
  4024. {
  4025. ZeroMemory(wisphrc->pLattice->pLatticeColumns,
  4026. wisphrc->pLattice->ulColumnCount * sizeof(RECO_LATTICE_COLUMN));
  4027. }
  4028. // Allocate the arrays for the best result
  4029. wisphrc->pLattice->pulBestResultColumns = (ULONG*)
  4030. ExternAlloc(vrc->pLatticePath->nChars * sizeof(ULONG));
  4031. wisphrc->pLattice->pulBestResultIndexes = (ULONG*)
  4032. ExternAlloc(vrc->pLatticePath->nChars * sizeof(ULONG));
  4033. if (!wisphrc->pLattice->pLatticeColumns ||
  4034. !wisphrc->pLattice->pulBestResultColumns ||
  4035. !wisphrc->pLattice->pulBestResultIndexes)
  4036. {
  4037. HRESULT hrFRL = FreeRecoLattice(wisphrc);
  4038. ASSERT(SUCCEEDED(hrFRL));
  4039. return E_OUTOFMEMORY;
  4040. }
  4041. // Allocating the array of Strokes
  4042. wisphrc->pLattice->pLatticeColumns[0].pStrokes =
  4043. ExternAlloc(vrc->pLattice->nRealStrokes * sizeof(ULONG));
  4044. if (!wisphrc->pLattice->pLatticeColumns[0].pStrokes)
  4045. {
  4046. HRESULT hrFRL = FreeRecoLattice(wisphrc);
  4047. ASSERT(SUCCEEDED(hrFRL));
  4048. return E_OUTOFMEMORY;
  4049. }
  4050. // Do corrections for the merged strokes - Annoying detail!
  4051. j = 0;
  4052. for (i = 0; i < vrc->pLattice->nStrokes; i++)
  4053. {
  4054. if (vrc->pLattice->pStroke[i].iLast != vrc->pLattice->pStroke[i].iOrder)
  4055. {
  4056. //There has been Stroke merging
  4057. // Add the Stroke list for this merge stroke
  4058. for (k = vrc->pLattice->pStroke[i].iOrder; k <= vrc->pLattice->pStroke[i].iLast; k++)
  4059. {
  4060. wisphrc->pLattice->pLatticeColumns[0].pStrokes[j] = k;
  4061. j++;
  4062. }
  4063. }
  4064. else
  4065. {
  4066. // No stroke merging for this stroke
  4067. wisphrc->pLattice->pLatticeColumns[0].pStrokes[j] =
  4068. vrc->pLattice->pStroke[i].iOrder;
  4069. j++;
  4070. }
  4071. }
  4072. // Count the number of Lattice elements
  4073. for (i = 0; i < vrc->pLattice->nStrokes; i++)
  4074. {
  4075. unsigned int ulElements = vrc->pLattice->pAltList[i].nUsed;
  4076. // For each column count the elements
  4077. if ((wisphrc->dwFlags & RECOFLAG_SINGLESEG) != 0)
  4078. {
  4079. int nStrokes = -1;
  4080. ulElements = 0;
  4081. for (j = 0; j < vrc->pLattice->pAltList[i].nUsed; j++)
  4082. {
  4083. if (vrc->pLattice->pAltList[i].alts[j].fCurrentPath)
  4084. {
  4085. nStrokes = vrc->pLattice->pAltList[i].alts[j].nStrokes;
  4086. break;
  4087. }
  4088. }
  4089. for (j = 0; j < vrc->pLattice->pAltList[i].nUsed; j++)
  4090. {
  4091. if (nStrokes == vrc->pLattice->pAltList[i].alts[j].nStrokes)
  4092. {
  4093. ulElements++;
  4094. }
  4095. }
  4096. }
  4097. ulElementCount += ulElements;
  4098. ulRealElementCount += ulElements;
  4099. // Add an element for each space
  4100. if (vrc->pLattice->pAltList[i].fSpaceAfterStroke)
  4101. {
  4102. ulElementCount++;
  4103. }
  4104. }
  4105. // Create the elements if needed
  4106. if (ulElementCount)
  4107. {
  4108. wisphrc->pLattice->pLatticeColumns[0].pLatticeElements
  4109. = ExternAlloc(ulElementCount*sizeof(RECO_LATTICE_ELEMENT));
  4110. if (!wisphrc->pLattice->pLatticeColumns[0].pLatticeElements)
  4111. {
  4112. HRESULT hrFRL = FreeRecoLattice(wisphrc);
  4113. ASSERT(SUCCEEDED(hrFRL));
  4114. return E_OUTOFMEMORY;
  4115. }
  4116. ZeroMemory(wisphrc->pLattice->pLatticeColumns[0].pLatticeElements,
  4117. ulElementCount*sizeof(RECO_LATTICE_ELEMENT));
  4118. pCurrent = wisphrc->pLattice->pLatticeColumns[0].pLatticeElements;
  4119. // Let's do the line metrics and the confidence levels
  4120. pCurrentProperty =
  4121. (RECO_LATTICE_PROPERTY *) ExternAlloc((3 + ulRealElementCount) * sizeof(RECO_LATTICE_PROPERTY));
  4122. wisphrc->pLatticeProperties = pCurrentProperty;
  4123. pCurrentPropertyValue =
  4124. (BYTE *) ExternAlloc(3 * sizeof(CONFIDENCE_LEVEL) + ulRealElementCount * sizeof(LATTICE_METRICS));
  4125. wisphrc->pLatticePropertyValues = pCurrentPropertyValue;
  4126. // Allocate property lists for each element. The non-spaces (real elements) each have two
  4127. // properties, and the spaces have one property.
  4128. #ifdef ENABLE_CONFIDENCE_LEVEL
  4129. ppCurrentProperty =
  4130. (RECO_LATTICE_PROPERTY **) ExternAlloc((ulElementCount + ulRealElementCount) * sizeof(RECO_LATTICE_PROPERTY *));
  4131. #else
  4132. ppCurrentProperty =
  4133. (RECO_LATTICE_PROPERTY **) ExternAlloc(ulRealElementCount * sizeof(RECO_LATTICE_PROPERTY *));
  4134. #endif
  4135. wisphrc->ppLatticeProperties = ppCurrentProperty;
  4136. if (!pCurrentProperty || !pCurrentPropertyValue || !ppCurrentProperty)
  4137. {
  4138. HRESULT hrFRL = FreeRecoLattice(wisphrc);
  4139. ASSERT(SUCCEEDED(hrFRL));
  4140. return E_OUTOFMEMORY;
  4141. }
  4142. // Fill the RECO_LATTICE_PROPERTY array to contain the confidence level
  4143. pConfidencePropStart = pCurrentProperty;
  4144. pCurrentProperty->guidProperty = GUID_CONFIDENCELEVEL;
  4145. pCurrentProperty->cbPropertyValue = sizeof(CONFIDENCE_LEVEL);
  4146. pCurrentProperty->pPropertyValue = pCurrentPropertyValue;
  4147. *( (CONFIDENCE_LEVEL*)pCurrentPropertyValue) = CFL_STRONG;
  4148. pCurrentPropertyValue += sizeof(CONFIDENCE_LEVEL);
  4149. pCurrentProperty++;
  4150. // next value
  4151. pCurrentProperty->guidProperty = GUID_CONFIDENCELEVEL;
  4152. pCurrentProperty->cbPropertyValue = sizeof(CONFIDENCE_LEVEL);
  4153. pCurrentProperty->pPropertyValue = pCurrentPropertyValue;
  4154. *( (CONFIDENCE_LEVEL*)pCurrentPropertyValue) = CFL_INTERMEDIATE;
  4155. pCurrentPropertyValue += sizeof(CONFIDENCE_LEVEL);
  4156. pCurrentProperty++;
  4157. // next value
  4158. pCurrentProperty->guidProperty = GUID_CONFIDENCELEVEL;
  4159. pCurrentProperty->cbPropertyValue = sizeof(CONFIDENCE_LEVEL);
  4160. pCurrentProperty->pPropertyValue = pCurrentPropertyValue;
  4161. *( (CONFIDENCE_LEVEL*)pCurrentPropertyValue) = CFL_POOR;
  4162. pCurrentPropertyValue += sizeof(CONFIDENCE_LEVEL);
  4163. pCurrentProperty++;
  4164. }
  4165. // Allocate space for the mapping
  4166. pMapToLatticeColumn = (int *) ExternAlloc(sizeof(int) * vrc->pLattice->nStrokes);
  4167. if (pMapToLatticeColumn == NULL)
  4168. {
  4169. HRESULT hrFRL = FreeRecoLattice(wisphrc);
  4170. ASSERT(SUCCEEDED(hrFRL));
  4171. return E_OUTOFMEMORY;
  4172. }
  4173. // Fill in mapping from internal lattice columns to the newly created lattice columns.
  4174. // Note that this mapping always points to columns related to strokes, not to spaces.
  4175. j = 0;
  4176. for (i = 0; i < vrc->pLattice->nStrokes; i++)
  4177. {
  4178. pMapToLatticeColumn[i] = j;
  4179. j++;
  4180. // Add a column for each space
  4181. if (vrc->pLattice->pAltList[i].fSpaceAfterStroke)
  4182. {
  4183. j++;
  4184. }
  4185. }
  4186. ASSERT( wisphrc->pLattice->ulColumnCount == j );
  4187. // Initialize the lattice columns
  4188. pCurrentStroke = wisphrc->pLattice->pLatticeColumns[0].pStrokes;
  4189. iExternalColumn = 0;
  4190. for (i = 0; i < vrc->pLattice->nStrokes; i++)
  4191. {
  4192. int iStartStroke = i, iEndStroke = vrc->pLattice->nStrokes;
  4193. ASSERT(pMapToLatticeColumn[i] == iExternalColumn);
  4194. rleInPath.type = RECO_TYPE_WSTRING;
  4195. ulMaxStroke = 0;
  4196. pStartElement = pCurrent;
  4197. // If we're pretending to have a single segmentation, then find the next
  4198. // element on the best path, and restrict the search for elements starting
  4199. // here to that column.
  4200. if ((wisphrc->dwFlags & RECOFLAG_SINGLESEG) != 0)
  4201. {
  4202. BOOL fFound = FALSE;
  4203. for (j = i; j < vrc->pLattice->nStrokes && !fFound; j++)
  4204. {
  4205. for (k = 0; k < vrc->pLattice->pAltList[j].nUsed && !fFound; k++)
  4206. {
  4207. if (vrc->pLattice->pAltList[j].alts[k].fCurrentPath)
  4208. {
  4209. // Make sure this element links up to the starting column we
  4210. // are at now. If it doesn't, then the current stroke is in
  4211. // the middle of a best path element, so this column will
  4212. // be empty.
  4213. if (j + 1 - vrc->pLattice->pAltList[j].alts[k].nStrokes != i)
  4214. {
  4215. goto NoElementsInColumn;
  4216. }
  4217. iStartStroke = j;
  4218. iEndStroke = j + 1;
  4219. fFound = TRUE;
  4220. }
  4221. }
  4222. }
  4223. if (!fFound)
  4224. {
  4225. goto NoElementsInColumn;
  4226. }
  4227. }
  4228. // Find all elements that start at this columns #
  4229. // This could probably be optimized
  4230. for (j = iStartStroke; j < iEndStroke; j++)
  4231. {
  4232. // Go through each alt
  4233. for (k = 0; k < vrc->pLattice->pAltList[j].nUsed; k++)
  4234. {
  4235. if (j + 1 - vrc->pLattice->pAltList[j].alts[k].nStrokes == i)
  4236. {
  4237. // This one starts at the right location
  4238. // Set up the properties. There are two properties for each
  4239. // character, the line metrics and the confidence level.
  4240. pCurrent->epProp.cProperties = 1;
  4241. ASSERT(pCurrent->epProp.apProps == NULL);
  4242. pCurrent->epProp.apProps = ppCurrentProperty;
  4243. *ppCurrentProperty = pCurrentProperty;
  4244. ppCurrentProperty++;
  4245. pCurrentProperty->guidProperty = GUID_LINEMETRICS;
  4246. pCurrentProperty->cbPropertyValue = sizeof(LATTICE_METRICS);
  4247. pCurrentProperty->pPropertyValue = pCurrentPropertyValue;
  4248. pCurrentProperty++;
  4249. pLatticeMetrics = (LATTICE_METRICS *) pCurrentPropertyValue;
  4250. pCurrentPropertyValue += sizeof(LATTICE_METRICS);
  4251. pLatticeMetrics->lsBaseline.PtA.x = vrc->pLattice->pAltList[j].alts[k].writingBox.left;
  4252. pLatticeMetrics->lsBaseline.PtA.y = vrc->pLattice->pAltList[j].alts[k].writingBox.bottom;
  4253. pLatticeMetrics->lsBaseline.PtB.x = vrc->pLattice->pAltList[j].alts[k].writingBox.right;
  4254. pLatticeMetrics->lsBaseline.PtB.y = vrc->pLattice->pAltList[j].alts[k].writingBox.bottom;
  4255. pLatticeMetrics->iMidlineOffset =
  4256. (SHORT) ((vrc->pLattice->pAltList[j].alts[k].writingBox.top -
  4257. vrc->pLattice->pAltList[j].alts[k].writingBox.bottom) / 2);
  4258. #ifdef ENABLE_CONFIDENCE_LEVEL
  4259. switch (GetConfidenceLevelInternal(vrc, i, k))
  4260. {
  4261. case CFL_STRONG:
  4262. pCurrent->epProp.cProperties++;
  4263. *ppCurrentProperty = pConfidencePropStart;
  4264. ppCurrentProperty++;
  4265. break;
  4266. case CFL_INTERMEDIATE:
  4267. pCurrent->epProp.cProperties++;
  4268. *ppCurrentProperty = pConfidencePropStart + 1;
  4269. ppCurrentProperty++;
  4270. break;
  4271. case CFL_POOR:
  4272. pCurrent->epProp.cProperties++;
  4273. *ppCurrentProperty = pConfidencePropStart + 2;
  4274. ppCurrentProperty++;
  4275. break;
  4276. }
  4277. #endif
  4278. // Get the character
  4279. if (vrc->pLattice->pAltList[j].alts[k].wChar == SYM_UNKNOWN)
  4280. {
  4281. pCurrent->pData = (void*) SYM_UNKNOWN;
  4282. }
  4283. else
  4284. {
  4285. pCurrent->pData = (void*)(LocRunDense2Unicode(&g_locRunInfo,
  4286. vrc->pLattice->pAltList[j].alts[k].wChar));
  4287. }
  4288. pCurrent->ulNextColumn = pMapToLatticeColumn[j] + 1;
  4289. // Count up the number of real strokes used by this alternate
  4290. pCurrent->ulStrokeNumber = 0;
  4291. // For each merged stroke in this alternate
  4292. for (iStroke = j; iStroke > j - vrc->pLattice->pAltList[j].alts[k].nStrokes; iStroke--)
  4293. {
  4294. // Add the number of real strokes contained in this merged stroke
  4295. pCurrent->ulStrokeNumber +=
  4296. vrc->pLattice->pStroke[iStroke].iLast -
  4297. vrc->pLattice->pStroke[iStroke].iOrder + 1;
  4298. }
  4299. if (ulMaxStroke < pCurrent->ulStrokeNumber)
  4300. ulMaxStroke = pCurrent->ulStrokeNumber;
  4301. pCurrent->type = RECO_TYPE_WCHAR;
  4302. flScore = -1024 *
  4303. vrc->pLattice->pAltList[j].alts[k].logProb;
  4304. // GetScore(vrc->pLattice, j, k);
  4305. if (flScore > INT_MAX)
  4306. {
  4307. flScore = (FLOAT) INT_MAX;
  4308. }
  4309. pCurrent->score = (int) flScore;
  4310. // Is it part of the best result?
  4311. if (vrc->pLattice->pAltList[j].alts[k].fCurrentPath)
  4312. {
  4313. // Yes, strore the column
  4314. wisphrc->pLattice->pulBestResultColumns[ulBestResultIndex] = iExternalColumn;
  4315. ASSERT(rleInPath.type == RECO_TYPE_WSTRING);
  4316. rleInPath = *pCurrent;
  4317. }
  4318. pCurrent++;
  4319. }
  4320. }
  4321. }
  4322. // We need to sort that list!
  4323. SortLatticeElements(pStartElement, pCurrent);
  4324. // Is there an element from the best result in this column?
  4325. if (rleInPath.type != RECO_TYPE_WSTRING)
  4326. {
  4327. // find its index in the column
  4328. for (pCur = pStartElement; pCur < pCurrent; pCur++)
  4329. {
  4330. if (!memcmp(pCur, &rleInPath, sizeof(RECO_LATTICE_ELEMENT)))
  4331. break;
  4332. }
  4333. ASSERT(pCur != pCurrent);
  4334. if (pCur != pCurrent)
  4335. {
  4336. wisphrc->pLattice->pulBestResultIndexes[ulBestResultIndex] = pCur - pStartElement;
  4337. ulBestResultIndex++;
  4338. }
  4339. }
  4340. NoElementsInColumn:
  4341. // Fill in the Reco Column information
  4342. wisphrc->pLattice->pLatticeColumns[iExternalColumn].key = iExternalColumn;
  4343. wisphrc->pLattice->pLatticeColumns[iExternalColumn].cpProp.cProperties = 0;
  4344. wisphrc->pLattice->pLatticeColumns[iExternalColumn].cpProp.apProps = NULL;
  4345. wisphrc->pLattice->pLatticeColumns[iExternalColumn].cStrokes = ulMaxStroke;
  4346. wisphrc->pLattice->pLatticeColumns[iExternalColumn].pStrokes = pCurrentStroke;
  4347. wisphrc->pLattice->pLatticeColumns[iExternalColumn].cLatticeElements = pCurrent-pStartElement;
  4348. wisphrc->pLattice->pLatticeColumns[iExternalColumn].pLatticeElements = pStartElement;
  4349. // Jump to the next "current" stroke : Always the annoying detail!
  4350. pCurrentStroke += vrc->pLattice->pStroke[i].iLast -
  4351. vrc->pLattice->pStroke[i].iOrder + 1;
  4352. // The new Start is:
  4353. pStartElement = pCurrent;
  4354. iExternalColumn++;
  4355. if (vrc->pLattice->pAltList[i].fSpaceAfterStroke)
  4356. {
  4357. // If there is a space, then it must be on the current path (because spaces are only
  4358. // created on the current path). This simplifies the code a lot.
  4359. pStartElement = pCurrent;
  4360. // Set up the properties. For spaces, the only property that applies is the
  4361. // confidence level.
  4362. pCurrent->epProp.cProperties = 0;
  4363. ASSERT(pCurrent->epProp.apProps == NULL);
  4364. pCurrent->epProp.apProps = ppCurrentProperty;
  4365. #ifdef ENABLE_CONFIDENCE_LEVEL
  4366. switch (GetConfidenceLevelInternal(vrc, i, SPACE_ALT_ID))
  4367. {
  4368. case CFL_STRONG:
  4369. pCurrent->epProp.cProperties++;
  4370. *ppCurrentProperty = pConfidencePropStart;
  4371. ppCurrentProperty++;
  4372. break;
  4373. case CFL_INTERMEDIATE:
  4374. pCurrent->epProp.cProperties++;
  4375. *ppCurrentProperty = pConfidencePropStart + 1;
  4376. ppCurrentProperty++;
  4377. break;
  4378. case CFL_POOR:
  4379. pCurrent->epProp.cProperties++;
  4380. *ppCurrentProperty = pConfidencePropStart + 2;
  4381. ppCurrentProperty++;
  4382. break;
  4383. }
  4384. #endif
  4385. pCurrent->pData = (void*) SYM_SPACE;
  4386. pCurrent->ulNextColumn = iExternalColumn + 1;
  4387. pCurrent->ulStrokeNumber = 0;
  4388. pCurrent->type = RECO_TYPE_WCHAR;
  4389. pCurrent->score = 0;
  4390. wisphrc->pLattice->pulBestResultColumns[ulBestResultIndex] = iExternalColumn;
  4391. wisphrc->pLattice->pulBestResultIndexes[ulBestResultIndex] = 0;
  4392. ulBestResultIndex++;
  4393. pCurrent++;
  4394. // Fill in the Reco Column information
  4395. wisphrc->pLattice->pLatticeColumns[iExternalColumn].key = iExternalColumn;
  4396. wisphrc->pLattice->pLatticeColumns[iExternalColumn].cpProp.cProperties = 0;
  4397. wisphrc->pLattice->pLatticeColumns[iExternalColumn].cpProp.apProps = NULL;
  4398. wisphrc->pLattice->pLatticeColumns[iExternalColumn].cStrokes = 0;
  4399. wisphrc->pLattice->pLatticeColumns[iExternalColumn].pStrokes = pCurrentStroke;
  4400. wisphrc->pLattice->pLatticeColumns[iExternalColumn].cLatticeElements = 1;
  4401. wisphrc->pLattice->pLatticeColumns[iExternalColumn].pLatticeElements = pStartElement;
  4402. // The new Start is:
  4403. pStartElement = pCurrent;
  4404. iExternalColumn++;
  4405. }
  4406. }
  4407. // Check the number of elements in the best path
  4408. ASSERT(ulBestResultIndex == (ULONG)vrc->pLatticePath->nChars);
  4409. ASSERT(iExternalColumn == wisphrc->pLattice->ulColumnCount);
  4410. ASSERT(pCurrentStroke - wisphrc->pLattice->pLatticeColumns[0].pStrokes == vrc->pLattice->nRealStrokes);
  4411. }
  4412. if (SUCCEEDED(hr))
  4413. *ppLattice = wisphrc->pLattice;
  4414. ExternFree(pMapToLatticeColumn);
  4415. return hr;
  4416. }
  4417. // Lists of properties
  4418. static const ULONG CONTEXT_PROPERTIES_COUNT = 1;
  4419. // {1ABC3828-BDF1-4ef3-8F2C-0751EC0DE742}
  4420. static const GUID GUID_ENABLE_IFELANG3 = { 0x1abc3828, 0xbdf1, 0x4ef3, { 0x8f, 0x2c, 0x7, 0x51, 0xec, 0xd, 0xe7, 0x42 } };
  4421. // GetContextPropertyList
  4422. // Return a list of properties supported on the context
  4423. //
  4424. // Parameters:
  4425. // hrc [in] : Handle to the recognition context
  4426. // pcProperties [in/out] : Number of properties supported
  4427. // pPropertyGUIDS [out] : List of properties supported
  4428. HRESULT WINAPI GetContextPropertyList(HRECOCONTEXT hrc,
  4429. ULONG* pcProperties,
  4430. GUID* pPropertyGUIDS)
  4431. {
  4432. if (NULL == (HRC *)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT) )
  4433. {
  4434. return E_POINTER;
  4435. }
  4436. if ( IsBadWritePtr(pcProperties, sizeof(ULONG)) )
  4437. return E_POINTER;
  4438. if (pPropertyGUIDS == NULL) // Need only the count
  4439. {
  4440. *pcProperties = CONTEXT_PROPERTIES_COUNT;
  4441. return S_OK;
  4442. }
  4443. if (*pcProperties < CONTEXT_PROPERTIES_COUNT)
  4444. return TPC_E_INSUFFICIENT_BUFFER;
  4445. *pcProperties = CONTEXT_PROPERTIES_COUNT;
  4446. if ( IsBadWritePtr(pPropertyGUIDS, CONTEXT_PROPERTIES_COUNT * sizeof(GUID)) )
  4447. return E_POINTER;
  4448. pPropertyGUIDS[0] = GUID_ENABLE_IFELANG3;
  4449. return S_OK;
  4450. }
  4451. // GetContextPropertyValue
  4452. // Return a property of the context, currently no properties are supported
  4453. //
  4454. // Parameters:
  4455. // hrc [in] : Handle to the recognition context
  4456. // pGuid [in] : Property GUID
  4457. // pcbSize [in/out] : Size of the property buffer (in BYTEs)
  4458. // pProperty [out] : Value of the desired property
  4459. HRESULT WINAPI GetContextPropertyValue(HRECOCONTEXT hrc,
  4460. GUID *pGuid,
  4461. ULONG *pcbSize,
  4462. BYTE *pProperty)
  4463. {
  4464. struct WispContext *wisphrc;
  4465. // find the handle and validate the correpsonding pointer
  4466. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  4467. if (wisphrc == NULL)
  4468. {
  4469. return E_POINTER;
  4470. }
  4471. if ( IsBadReadPtr(pGuid, sizeof(GUID)) )
  4472. return E_POINTER;
  4473. if ( IsBadWritePtr(pcbSize, sizeof(ULONG)) )
  4474. return E_POINTER;
  4475. if ( IsEqualGUID(pGuid, &GUID_ENABLE_IFELANG3) )
  4476. {
  4477. BOOL *pb = (BOOL *) pProperty;
  4478. if (pProperty == NULL)
  4479. {
  4480. *pcbSize = sizeof(BOOL);
  4481. return S_OK;
  4482. }
  4483. if (*pcbSize < sizeof(BOOL))
  4484. {
  4485. return TPC_E_INSUFFICIENT_BUFFER;
  4486. }
  4487. *pcbSize = sizeof(BOOL);
  4488. #ifdef USE_IFELANG3
  4489. *pb = LatticeIFELang3Available();
  4490. #else
  4491. *pb = FALSE;
  4492. #endif
  4493. return S_OK;
  4494. }
  4495. return TPC_E_INVALID_PROPERTY;
  4496. }
  4497. // SetContextPropertyValue
  4498. // Set a property of the context (currently only GUID_ENABLE_IFELANG3)
  4499. //
  4500. // Parameters:
  4501. // hrc [in] : Handle to the recognition context
  4502. // pGuid [in] : Property GUID
  4503. // pcbSize [in] : Size of the property buffer (in BYTEs)
  4504. // pProperty [in] : Value of the desired property
  4505. HRESULT WINAPI SetContextPropertyValue(HRECOCONTEXT hrc,
  4506. GUID *pGuid,
  4507. ULONG cbSize,
  4508. BYTE *pProperty)
  4509. {
  4510. struct WispContext *wisphrc;
  4511. // find the handle and validate the correpsonding pointer
  4512. wisphrc = (struct WispContext*)FindTpgHandle((HANDLE)hrc, TPG_HRECOCONTEXT);
  4513. if (wisphrc == NULL)
  4514. {
  4515. return E_POINTER;
  4516. }
  4517. if ( IsBadReadPtr(pGuid, sizeof(GUID)) )
  4518. return E_POINTER;
  4519. if ( IsBadReadPtr(pProperty, cbSize) )
  4520. return E_POINTER;
  4521. if ( IsEqualGUID(pGuid, &GUID_ENABLE_IFELANG3) )
  4522. {
  4523. BOOL *pb = (BOOL *) pProperty;
  4524. if (cbSize != sizeof(BOOL))
  4525. {
  4526. return E_INVALIDARG;
  4527. }
  4528. if (*pb)
  4529. {
  4530. #ifdef USE_IFELANG3
  4531. // If already enabled, return S_FALSE
  4532. if (LatticeIFELang3Available())
  4533. {
  4534. return S_FALSE;
  4535. }
  4536. return LatticeConfigIFELang3() ? S_OK : E_FAIL;
  4537. #else
  4538. return E_FAIL;
  4539. #endif
  4540. }
  4541. else
  4542. {
  4543. #ifdef USE_IFELANG3
  4544. // If already disabled, return S_FALSE
  4545. if (!LatticeIFELang3Available())
  4546. {
  4547. return S_FALSE;
  4548. }
  4549. return LatticeUnconfigIFELang3() ? S_OK : E_FAIL;
  4550. #else
  4551. return S_FALSE;
  4552. #endif
  4553. }
  4554. }
  4555. return TPC_E_INVALID_PROPERTY;
  4556. }
  4557. /////////////////////////////////////////////////////////////////
  4558. // Registration information
  4559. //
  4560. //
  4561. #define FULL_PATH_VALUE L"Recognizer dll"
  4562. #define RECO_LANGUAGES L"Recognized Languages"
  4563. #define RECO_CAPABILITIES L"Recognizer Capability Flags"
  4564. #define RECO_MANAGER_KEY L"CLSID\\{DE815B00-9460-4F6E-9471-892ED2275EA5}\\InprocServer32"
  4565. #define CLSID_KEY L"CLSID"
  4566. /////////////////////////////////////////////////////////////////////////////
  4567. // DllRegisterServer - Adds entries to the system registry
  4568. // This recognizer GUID is going to be
  4569. // {6D0087D7-61D2-495f-9293-5B7B1C3FCEAB}
  4570. // Each recognizer HAS to have a different GUID
  4571. STDAPI DllRegisterServer(void)
  4572. {
  4573. HKEY hKeyReco = NULL;
  4574. HKEY hKeyRecoManager = NULL;
  4575. LONG lRes = 0;
  4576. HKEY hkeyMyReco = NULL;
  4577. DWORD dwLength = 0, dwType = 0, dwSize = 0;
  4578. DWORD dwDisposition;
  4579. WCHAR szRecognizerPath[MAX_PATH];
  4580. WCHAR *RECO_SUBKEY = NULL, *RECOGNIZER_SUBKEY = NULL;
  4581. WCHAR *RECOPROC_SUBKEY = NULL, *RECOCLSID_SUBKEY = NULL;
  4582. RECO_ATTRS recoAttr;
  4583. HRESULT hr = S_OK;
  4584. HRECOGNIZER hrec;
  4585. if (FAILED(CreateRecognizer(NULL, &hrec)))
  4586. {
  4587. return E_FAIL;
  4588. }
  4589. hr = GetRecoAttributes(hrec, &recoAttr);
  4590. if (FAILED(hr))
  4591. {
  4592. return E_FAIL;
  4593. }
  4594. if (FAILED(DestroyRecognizer(hrec)))
  4595. {
  4596. return E_FAIL;
  4597. }
  4598. if (recoAttr.awLanguageId[0] == MAKELANGID(LANG_JAPANESE, SUBLANG_NEUTRAL))
  4599. {
  4600. RECO_SUBKEY = L"Software\\Microsoft\\TPG\\System Recognizers\\{6D4087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4601. RECOGNIZER_SUBKEY = L"CLSID\\{6D4087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4602. RECOPROC_SUBKEY = L"{6D4087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4603. RECOCLSID_SUBKEY = L"{6D4087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4604. }
  4605. if (recoAttr.awLanguageId[0] == MAKELANGID(LANG_KOREAN, SUBLANG_NEUTRAL))
  4606. {
  4607. RECO_SUBKEY = L"Software\\Microsoft\\TPG\\System Recognizers\\{6D5087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4608. RECOGNIZER_SUBKEY = L"CLSID\\{6D5087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4609. RECOPROC_SUBKEY = L"{6D5087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4610. RECOCLSID_SUBKEY = L"{6D5087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4611. }
  4612. if (recoAttr.awLanguageId[0] == MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED))
  4613. {
  4614. RECO_SUBKEY = L"Software\\Microsoft\\TPG\\System Recognizers\\{6D6087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4615. RECOGNIZER_SUBKEY = L"CLSID\\{6D6087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4616. RECOPROC_SUBKEY = L"{6D6087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4617. RECOCLSID_SUBKEY = L"{6D6087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4618. }
  4619. if (recoAttr.awLanguageId[0] == MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL))
  4620. {
  4621. RECO_SUBKEY = L"Software\\Microsoft\\TPG\\System Recognizers\\{6D7087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4622. RECOGNIZER_SUBKEY = L"CLSID\\{6D7087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4623. RECOPROC_SUBKEY = L"{6D7087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4624. RECOCLSID_SUBKEY = L"{6D7087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4625. }
  4626. // Write the path to this dll in the registry under
  4627. // the recognizer subkey
  4628. // Wipe out the previous values
  4629. lRes = RegDeleteKeyW(HKEY_LOCAL_MACHINE, RECO_SUBKEY);
  4630. // Create the new key
  4631. lRes = RegCreateKeyExW(HKEY_LOCAL_MACHINE, RECO_SUBKEY, 0, NULL,
  4632. REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyMyReco, &dwDisposition);
  4633. ASSERT(lRes == ERROR_SUCCESS);
  4634. if (lRes != ERROR_SUCCESS)
  4635. {
  4636. if (hkeyMyReco)
  4637. RegCloseKey(hkeyMyReco);
  4638. return E_FAIL;
  4639. }
  4640. // Get the current path
  4641. // Try to get the path of the RecoObj.dll
  4642. // It should be the same as the one for the RecoCom.dll
  4643. dwLength = GetModuleFileNameW((HMODULE)g_hInstanceDllCode, szRecognizerPath, MAX_PATH);
  4644. if (dwLength == 0 || (dwLength == MAX_PATH && szRecognizerPath[MAX_PATH - 1] != 0))
  4645. {
  4646. RegCloseKey(hkeyMyReco);
  4647. return E_FAIL;
  4648. }
  4649. // Write the path to the dll as a value
  4650. lRes = RegSetValueExW(hkeyMyReco, FULL_PATH_VALUE, 0, REG_SZ,
  4651. (BYTE*)szRecognizerPath, sizeof(WCHAR)*(wcslen(szRecognizerPath)+1));
  4652. ASSERT(lRes == ERROR_SUCCESS);
  4653. if (lRes != ERROR_SUCCESS)
  4654. {
  4655. RegCloseKey(hkeyMyReco);
  4656. return E_FAIL;
  4657. }
  4658. // Add the reco attribute information
  4659. lRes = RegSetValueExW(hkeyMyReco, RECO_LANGUAGES, 0, REG_BINARY,
  4660. (BYTE*)recoAttr.awLanguageId, 64 * sizeof(WORD));
  4661. ASSERT(lRes == ERROR_SUCCESS);
  4662. if (lRes != ERROR_SUCCESS)
  4663. {
  4664. RegCloseKey(hkeyMyReco);
  4665. return E_FAIL;
  4666. }
  4667. lRes = RegSetValueExW(hkeyMyReco, RECO_CAPABILITIES, 0, REG_DWORD,
  4668. (BYTE*)&(recoAttr.dwRecoCapabilityFlags), sizeof(DWORD));
  4669. ASSERT(lRes == ERROR_SUCCESS);
  4670. if (lRes != ERROR_SUCCESS)
  4671. {
  4672. RegCloseKey(hkeyMyReco);
  4673. return E_FAIL;
  4674. }
  4675. RegCloseKey(hkeyMyReco);
  4676. return S_OK;
  4677. }
  4678. /////////////////////////////////////////////////////////////////////////////
  4679. // DllUnregisterServer - Removes entries from the system registry
  4680. STDAPI DllUnregisterServer(void)
  4681. {
  4682. LONG lRes1 = 0;
  4683. // get language id
  4684. WCHAR *RECO_SUBKEY = NULL, *RECOGNIZER_SUBKEY = NULL;
  4685. WCHAR *RECOPROC_SUBKEY = NULL, *RECOCLSID_SUBKEY = NULL;
  4686. RECO_ATTRS recoAttr;
  4687. HRESULT hr = S_OK;
  4688. HRECOGNIZER hrec;
  4689. if (FAILED(CreateRecognizer(NULL, &hrec)))
  4690. {
  4691. return E_FAIL;
  4692. }
  4693. hr = GetRecoAttributes(hrec, &recoAttr);
  4694. if (FAILED(hr))
  4695. {
  4696. return E_FAIL;
  4697. }
  4698. if (FAILED(DestroyRecognizer(hrec)))
  4699. {
  4700. return E_FAIL;
  4701. }
  4702. if (recoAttr.awLanguageId[0] == MAKELANGID(LANG_JAPANESE, SUBLANG_NEUTRAL))
  4703. {
  4704. RECO_SUBKEY = L"Software\\Microsoft\\TPG\\System Recognizers\\{6D4087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4705. RECOGNIZER_SUBKEY = L"CLSID\\{6D4087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4706. RECOPROC_SUBKEY = L"{6D4087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4707. RECOCLSID_SUBKEY = L"{6D4087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4708. }
  4709. if (recoAttr.awLanguageId[0] == MAKELANGID(LANG_KOREAN, SUBLANG_NEUTRAL))
  4710. {
  4711. RECO_SUBKEY = L"Software\\Microsoft\\TPG\\System Recognizers\\{6D5087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4712. RECOGNIZER_SUBKEY = L"CLSID\\{6D5087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4713. RECOPROC_SUBKEY = L"{6D5087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4714. RECOCLSID_SUBKEY = L"{6D5087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4715. }
  4716. if (recoAttr.awLanguageId[0] == MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED))
  4717. {
  4718. RECO_SUBKEY = L"Software\\Microsoft\\TPG\\System Recognizers\\{6D6087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4719. RECOGNIZER_SUBKEY = L"CLSID\\{6D6087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4720. RECOPROC_SUBKEY = L"{6D6087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4721. RECOCLSID_SUBKEY = L"{6D6087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4722. }
  4723. if (recoAttr.awLanguageId[0] == MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL))
  4724. {
  4725. RECO_SUBKEY = L"Software\\Microsoft\\TPG\\System Recognizers\\{6D7087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4726. RECOGNIZER_SUBKEY = L"CLSID\\{6D7087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4727. RECOPROC_SUBKEY = L"{6D7087D7-61D2-495f-9293-5B7B1C3FCEAB}\\InprocServer32";
  4728. RECOCLSID_SUBKEY = L"{6D7087D7-61D2-495f-9293-5B7B1C3FCEAB}";
  4729. }
  4730. // Wipe out the registry information
  4731. lRes1 = RegDeleteKeyW(HKEY_LOCAL_MACHINE, RECO_SUBKEY);
  4732. // Try to erase the local machine\software\microsoft\tpg\recognizer
  4733. // if necessary (don't care if it fails)
  4734. RegDeleteKeyW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\TPG\\System Recognizers");
  4735. RegDeleteKeyW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\TPG");
  4736. if (lRes1 != ERROR_SUCCESS && lRes1 != ERROR_FILE_NOT_FOUND)
  4737. {
  4738. return E_FAIL;
  4739. }
  4740. return S_OK ;
  4741. }
  4742. /*************************************************
  4743. * NAME: validateTpgHandle
  4744. *
  4745. * Generic function to validate a pointer obtained from a WISP
  4746. * style handle. For now function checks the memory
  4747. * is writable
  4748. *
  4749. * RETURNS
  4750. * TRUE if the pointer passes a minimal validation
  4751. *
  4752. *************************************************/
  4753. BOOL validateTpgHandle(void *pPtr, int type)
  4754. {
  4755. BOOL bRet = FALSE;
  4756. switch (type)
  4757. {
  4758. case TPG_HRECOCONTEXT:
  4759. {
  4760. if (0 == IsBadWritePtr(pPtr, sizeof(struct WispContext)))
  4761. {
  4762. bRet = TRUE;
  4763. }
  4764. break;
  4765. }
  4766. case TPG_HRECOGNIZER:
  4767. {
  4768. if (0 == IsBadWritePtr(pPtr, sizeof(struct WispRec)))
  4769. {
  4770. bRet = TRUE;
  4771. }
  4772. break;
  4773. }
  4774. case TPG_HRECOALT:
  4775. {
  4776. if (0 == IsBadWritePtr(pPtr, sizeof(struct WispAlternate)))
  4777. {
  4778. bRet = TRUE;
  4779. }
  4780. break;
  4781. }
  4782. default:
  4783. break;
  4784. }
  4785. return bRet;
  4786. }