1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "../../include/fpdftext/fpdf_text.h"
8 extern const FX_WCHAR g_UnicodeData_Normalization[];
9 extern const FX_WCHAR g_UnicodeData_Normalization_Map1[];
10 extern const FX_WCHAR g_UnicodeData_Normalization_Map2[];
11 extern const FX_WCHAR g_UnicodeData_Normalization_Map3[];
12 extern const FX_WCHAR g_UnicodeData_Normalization_Map4[];
13 FX_LPCWSTR g_UnicodeData_Normalization_Maps[5] = {
14     NULL,
15     g_UnicodeData_Normalization_Map1,
16     g_UnicodeData_Normalization_Map2,
17     g_UnicodeData_Normalization_Map3,
18     g_UnicodeData_Normalization_Map4
19 };
FX_Unicode_GetNormalization(FX_WCHAR wch,FX_LPWSTR pDst)20 FX_STRSIZE FX_Unicode_GetNormalization(FX_WCHAR wch, FX_LPWSTR pDst)
21 {
22     wch = wch & 0xFFFF;
23     FX_WCHAR wFind = g_UnicodeData_Normalization[wch];
24     if (!wFind) {
25         if (pDst) {
26             *pDst = wch;
27         }
28         return 1;
29     }
30     if(wFind >= 0x8000) {
31         wch = wFind - 0x8000;
32         wFind = 1;
33     } else {
34         wch = wFind & 0x0FFF;
35         wFind >>= 12;
36     }
37     FX_LPCWSTR pMap = g_UnicodeData_Normalization_Maps[wFind];
38     if (pMap == g_UnicodeData_Normalization_Map4) {
39         pMap = g_UnicodeData_Normalization_Map4 + wch;
40         wFind = (FX_WCHAR)(*pMap ++);
41     } else {
42         pMap += wch;
43     }
44     if (pDst) {
45         FX_WCHAR n = wFind;
46         while (n --) {
47             *pDst ++ = *pMap ++;
48         }
49     }
50     return (FX_STRSIZE)wFind;
51 }
FX_WideString_GetNormalization(FX_WSTR wsSrc,FX_LPWSTR pDst)52 FX_STRSIZE FX_WideString_GetNormalization(FX_WSTR wsSrc, FX_LPWSTR pDst)
53 {
54     FX_STRSIZE nCount = 0;
55     for (FX_STRSIZE len = 0; len < wsSrc.GetLength(); len ++) {
56         FX_WCHAR wch = wsSrc.GetAt(len);
57         if(pDst) {
58             nCount += FX_Unicode_GetNormalization(wch, pDst + nCount);
59         } else {
60             nCount += FX_Unicode_GetNormalization(wch, pDst);
61         }
62     }
63     return nCount;
64 }
FX_WideString_GetNormalization(FX_WSTR wsSrc,CFX_WideString & wsDst)65 FX_STRSIZE FX_WideString_GetNormalization(FX_WSTR wsSrc, CFX_WideString &wsDst)
66 {
67     FX_STRSIZE nLen = FX_WideString_GetNormalization(wsSrc, (FX_LPWSTR)NULL);
68     if (!nLen) {
69         return 0;
70     }
71     FX_LPWSTR pBuf = wsDst.GetBuffer(nLen);
72     FX_WideString_GetNormalization(wsSrc, pBuf);
73     wsDst.ReleaseBuffer(nLen);
74     return nLen;
75 }
76