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 "cmap_int.h"
8
9 #include "core/include/fpdfapi/fpdf_module.h"
10 #include "core/include/fpdfapi/fpdf_resource.h"
11 #include "core/src/fpdfapi/fpdf_font/font_int.h"
12
FPDFAPI_FindEmbeddedCMap(const char * name,int charset,int coding,const FXCMAP_CMap * & pMap)13 void FPDFAPI_FindEmbeddedCMap(const char* name,
14 int charset,
15 int coding,
16 const FXCMAP_CMap*& pMap) {
17 pMap = NULL;
18 CPDF_FontGlobals* pFontGlobals =
19 CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals();
20 const FXCMAP_CMap* pCMaps =
21 pFontGlobals->m_EmbeddedCharsets[charset].m_pMapList;
22 int nCMaps = pFontGlobals->m_EmbeddedCharsets[charset].m_Count;
23 for (int i = 0; i < nCMaps; i++) {
24 if (FXSYS_strcmp(name, pCMaps[i].m_Name)) {
25 continue;
26 }
27 pMap = &pCMaps[i];
28 break;
29 }
30 }
31 extern "C" {
compareWord(const void * p1,const void * p2)32 static int compareWord(const void* p1, const void* p2) {
33 return (*(FX_WORD*)p1) - (*(FX_WORD*)p2);
34 }
35 };
36 extern "C" {
compareWordRange(const void * key,const void * element)37 static int compareWordRange(const void* key, const void* element) {
38 if (*(FX_WORD*)key < *(FX_WORD*)element) {
39 return -1;
40 }
41 if (*(FX_WORD*)key > ((FX_WORD*)element)[1]) {
42 return 1;
43 }
44 return 0;
45 }
46 };
47 extern "C" {
compareDWordRange(const void * p1,const void * p2)48 static int compareDWordRange(const void* p1, const void* p2) {
49 FX_DWORD key = *(FX_DWORD*)p1;
50 FX_WORD hiword = (FX_WORD)(key >> 16);
51 FX_WORD* element = (FX_WORD*)p2;
52 if (hiword < element[0]) {
53 return -1;
54 }
55 if (hiword > element[0]) {
56 return 1;
57 }
58 FX_WORD loword = (FX_WORD)key;
59 if (loword < element[1]) {
60 return -1;
61 }
62 if (loword > element[2]) {
63 return 1;
64 }
65 return 0;
66 }
67 };
68 extern "C" {
compareDWordSingle(const void * p1,const void * p2)69 static int compareDWordSingle(const void* p1, const void* p2) {
70 FX_DWORD key = *(FX_DWORD*)p1;
71 FX_DWORD value = ((*(FX_WORD*)p2) << 16) | ((FX_WORD*)p2)[1];
72 if (key < value) {
73 return -1;
74 }
75 if (key > value) {
76 return 1;
77 }
78 return 0;
79 }
80 };
FPDFAPI_CIDFromCharCode(const FXCMAP_CMap * pMap,FX_DWORD charcode)81 FX_WORD FPDFAPI_CIDFromCharCode(const FXCMAP_CMap* pMap, FX_DWORD charcode) {
82 if (charcode >> 16) {
83 while (1) {
84 if (pMap->m_DWordMapType == FXCMAP_CMap::Range) {
85 FX_WORD* found =
86 (FX_WORD*)FXSYS_bsearch(&charcode, pMap->m_pDWordMap,
87 pMap->m_DWordCount, 8, compareDWordRange);
88 if (found) {
89 return found[3] + (FX_WORD)charcode - found[1];
90 }
91 } else if (pMap->m_DWordMapType == FXCMAP_CMap::Single) {
92 FX_WORD* found =
93 (FX_WORD*)FXSYS_bsearch(&charcode, pMap->m_pDWordMap,
94 pMap->m_DWordCount, 6, compareDWordSingle);
95 if (found) {
96 return found[2];
97 }
98 }
99 if (pMap->m_UseOffset == 0) {
100 return 0;
101 }
102 pMap = pMap + pMap->m_UseOffset;
103 }
104 return 0;
105 }
106 FX_WORD code = (FX_WORD)charcode;
107 while (1) {
108 if (!pMap->m_pWordMap) {
109 return 0;
110 }
111 if (pMap->m_WordMapType == FXCMAP_CMap::Single) {
112 FX_WORD* found = (FX_WORD*)FXSYS_bsearch(
113 &code, pMap->m_pWordMap, pMap->m_WordCount, 4, compareWord);
114 if (found) {
115 return found[1];
116 }
117 } else if (pMap->m_WordMapType == FXCMAP_CMap::Range) {
118 FX_WORD* found = (FX_WORD*)FXSYS_bsearch(
119 &code, pMap->m_pWordMap, pMap->m_WordCount, 6, compareWordRange);
120 if (found) {
121 return found[2] + code - found[0];
122 }
123 }
124 if (pMap->m_UseOffset == 0) {
125 return 0;
126 }
127 pMap = pMap + pMap->m_UseOffset;
128 }
129 return 0;
130 }
FPDFAPI_CharCodeFromCID(const FXCMAP_CMap * pMap,FX_WORD cid)131 FX_DWORD FPDFAPI_CharCodeFromCID(const FXCMAP_CMap* pMap, FX_WORD cid) {
132 while (1) {
133 if (pMap->m_WordMapType == FXCMAP_CMap::Single) {
134 const FX_WORD* pCur = pMap->m_pWordMap;
135 const FX_WORD* pEnd = pMap->m_pWordMap + pMap->m_WordCount * 2;
136 while (pCur < pEnd) {
137 if (pCur[1] == cid) {
138 return pCur[0];
139 }
140 pCur += 2;
141 }
142 } else if (pMap->m_WordMapType == FXCMAP_CMap::Range) {
143 const FX_WORD* pCur = pMap->m_pWordMap;
144 const FX_WORD* pEnd = pMap->m_pWordMap + pMap->m_WordCount * 3;
145 while (pCur < pEnd) {
146 if (cid >= pCur[2] && cid <= pCur[2] + pCur[1] - pCur[0]) {
147 return pCur[0] + cid - pCur[2];
148 }
149 pCur += 3;
150 }
151 }
152 if (pMap->m_UseOffset == 0) {
153 return 0;
154 }
155 pMap = pMap + pMap->m_UseOffset;
156 }
157 while (1) {
158 if (pMap->m_DWordMapType == FXCMAP_CMap::Range) {
159 const FX_WORD* pCur = pMap->m_pDWordMap;
160 const FX_WORD* pEnd = pMap->m_pDWordMap + pMap->m_DWordCount * 4;
161 while (pCur < pEnd) {
162 if (cid >= pCur[3] && cid <= pCur[3] + pCur[2] - pCur[1]) {
163 return (((FX_DWORD)pCur[0] << 16) | pCur[1]) + cid - pCur[3];
164 }
165 pCur += 4;
166 }
167 } else if (pMap->m_DWordMapType == FXCMAP_CMap::Single) {
168 const FX_WORD* pCur = pMap->m_pDWordMap;
169 const FX_WORD* pEnd = pMap->m_pDWordMap + pMap->m_DWordCount * 3;
170 while (pCur < pEnd) {
171 if (pCur[2] == cid) {
172 return ((FX_DWORD)pCur[0] << 16) | pCur[1];
173 }
174 pCur += 3;
175 }
176 }
177 if (pMap->m_UseOffset == 0) {
178 return 0;
179 }
180 pMap = pMap + pMap->m_UseOffset;
181 }
182 return 0;
183 }
184
FPDFAPI_LoadCID2UnicodeMap(CIDSet charset,const FX_WORD * & pMap,FX_DWORD & count)185 void FPDFAPI_LoadCID2UnicodeMap(CIDSet charset,
186 const FX_WORD*& pMap,
187 FX_DWORD& count) {
188 CPDF_FontGlobals* pFontGlobals =
189 CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals();
190 pMap = pFontGlobals->m_EmbeddedToUnicodes[charset].m_pMap;
191 count = pFontGlobals->m_EmbeddedToUnicodes[charset].m_Count;
192 }
193