OLD | NEW |
| (Empty) |
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 "core/src/fpdfapi/fpdf_font/font_int.h" | |
8 | |
9 #include "core/include/fpdfapi/cpdf_array.h" | |
10 #include "core/include/fpdfapi/cpdf_dictionary.h" | |
11 #include "core/include/fpdfapi/cpdf_simple_parser.h" | |
12 #include "core/include/fpdfapi/fpdf_module.h" | |
13 #include "core/include/fpdfapi/fpdf_page.h" | |
14 #include "core/include/fpdfapi/fpdf_resource.h" | |
15 #include "core/include/fxcrt/fx_ext.h" | |
16 #include "core/include/fxge/fx_freetype.h" | |
17 #include "core/include/fxge/fx_ge.h" | |
18 #include "core/src/fpdfapi/fpdf_cmaps/cmap_int.h" | |
19 #include "core/src/fpdfapi/fpdf_font/ttgsubtable.h" | |
20 | |
21 namespace { | |
22 | |
23 const FX_CHAR* const g_CharsetNames[CIDSET_NUM_SETS] = | |
24 {nullptr, "GB1", "CNS1", "Japan1", "Korea1", "UCS"}; | |
25 | |
26 const int g_CharsetCPs[CIDSET_NUM_SETS] = {0, 936, 950, 932, 949, 1200}; | |
27 | |
28 class CPDF_PredefinedCMap { | |
29 public: | |
30 const FX_CHAR* m_pName; | |
31 CIDSet m_Charset; | |
32 int m_Coding; | |
33 CPDF_CMap::CodingScheme m_CodingScheme; | |
34 FX_DWORD m_LeadingSegCount; | |
35 uint8_t m_LeadingSegs[4]; | |
36 }; | |
37 | |
38 const CPDF_PredefinedCMap g_PredefinedCMaps[] = { | |
39 {"GB-EUC", | |
40 CIDSET_GB1, | |
41 CIDCODING_GB, | |
42 CPDF_CMap::MixedTwoBytes, | |
43 1, | |
44 {0xa1, 0xfe}}, | |
45 {"GBpc-EUC", | |
46 CIDSET_GB1, | |
47 CIDCODING_GB, | |
48 CPDF_CMap::MixedTwoBytes, | |
49 1, | |
50 {0xa1, 0xfc}}, | |
51 {"GBK-EUC", | |
52 CIDSET_GB1, | |
53 CIDCODING_GB, | |
54 CPDF_CMap::MixedTwoBytes, | |
55 1, | |
56 {0x81, 0xfe}}, | |
57 {"GBKp-EUC", | |
58 CIDSET_GB1, | |
59 CIDCODING_GB, | |
60 CPDF_CMap::MixedTwoBytes, | |
61 1, | |
62 {0x81, 0xfe}}, | |
63 {"GBK2K-EUC", | |
64 CIDSET_GB1, | |
65 CIDCODING_GB, | |
66 CPDF_CMap::MixedTwoBytes, | |
67 1, | |
68 {0x81, 0xfe}}, | |
69 {"GBK2K", | |
70 CIDSET_GB1, | |
71 CIDCODING_GB, | |
72 CPDF_CMap::MixedTwoBytes, | |
73 1, | |
74 {0x81, 0xfe}}, | |
75 {"UniGB-UCS2", CIDSET_GB1, CIDCODING_UCS2, CPDF_CMap::TwoBytes, 0, {}}, | |
76 {"UniGB-UTF16", CIDSET_GB1, CIDCODING_UTF16, CPDF_CMap::TwoBytes, 0, {}}, | |
77 {"B5pc", | |
78 CIDSET_CNS1, | |
79 CIDCODING_BIG5, | |
80 CPDF_CMap::MixedTwoBytes, | |
81 1, | |
82 {0xa1, 0xfc}}, | |
83 {"HKscs-B5", | |
84 CIDSET_CNS1, | |
85 CIDCODING_BIG5, | |
86 CPDF_CMap::MixedTwoBytes, | |
87 1, | |
88 {0x88, 0xfe}}, | |
89 {"ETen-B5", | |
90 CIDSET_CNS1, | |
91 CIDCODING_BIG5, | |
92 CPDF_CMap::MixedTwoBytes, | |
93 1, | |
94 {0xa1, 0xfe}}, | |
95 {"ETenms-B5", | |
96 CIDSET_CNS1, | |
97 CIDCODING_BIG5, | |
98 CPDF_CMap::MixedTwoBytes, | |
99 1, | |
100 {0xa1, 0xfe}}, | |
101 {"UniCNS-UCS2", CIDSET_CNS1, CIDCODING_UCS2, CPDF_CMap::TwoBytes, 0, {}}, | |
102 {"UniCNS-UTF16", CIDSET_CNS1, CIDCODING_UTF16, CPDF_CMap::TwoBytes, 0, {}}, | |
103 {"83pv-RKSJ", | |
104 CIDSET_JAPAN1, | |
105 CIDCODING_JIS, | |
106 CPDF_CMap::MixedTwoBytes, | |
107 2, | |
108 {0x81, 0x9f, 0xe0, 0xfc}}, | |
109 {"90ms-RKSJ", | |
110 CIDSET_JAPAN1, | |
111 CIDCODING_JIS, | |
112 CPDF_CMap::MixedTwoBytes, | |
113 2, | |
114 {0x81, 0x9f, 0xe0, 0xfc}}, | |
115 {"90msp-RKSJ", | |
116 CIDSET_JAPAN1, | |
117 CIDCODING_JIS, | |
118 CPDF_CMap::MixedTwoBytes, | |
119 2, | |
120 {0x81, 0x9f, 0xe0, 0xfc}}, | |
121 {"90pv-RKSJ", | |
122 CIDSET_JAPAN1, | |
123 CIDCODING_JIS, | |
124 CPDF_CMap::MixedTwoBytes, | |
125 2, | |
126 {0x81, 0x9f, 0xe0, 0xfc}}, | |
127 {"Add-RKSJ", | |
128 CIDSET_JAPAN1, | |
129 CIDCODING_JIS, | |
130 CPDF_CMap::MixedTwoBytes, | |
131 2, | |
132 {0x81, 0x9f, 0xe0, 0xfc}}, | |
133 {"EUC", | |
134 CIDSET_JAPAN1, | |
135 CIDCODING_JIS, | |
136 CPDF_CMap::MixedTwoBytes, | |
137 2, | |
138 {0x8e, 0x8e, 0xa1, 0xfe}}, | |
139 {"H", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::TwoBytes, 1, {0x21, 0x7e}}, | |
140 {"V", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::TwoBytes, 1, {0x21, 0x7e}}, | |
141 {"Ext-RKSJ", | |
142 CIDSET_JAPAN1, | |
143 CIDCODING_JIS, | |
144 CPDF_CMap::MixedTwoBytes, | |
145 2, | |
146 {0x81, 0x9f, 0xe0, 0xfc}}, | |
147 {"UniJIS-UCS2", CIDSET_JAPAN1, CIDCODING_UCS2, CPDF_CMap::TwoBytes, 0, {}}, | |
148 {"UniJIS-UCS2-HW", | |
149 CIDSET_JAPAN1, | |
150 CIDCODING_UCS2, | |
151 CPDF_CMap::TwoBytes, | |
152 0, | |
153 {}}, | |
154 {"UniJIS-UTF16", | |
155 CIDSET_JAPAN1, | |
156 CIDCODING_UTF16, | |
157 CPDF_CMap::TwoBytes, | |
158 0, | |
159 {}}, | |
160 {"KSC-EUC", | |
161 CIDSET_KOREA1, | |
162 CIDCODING_KOREA, | |
163 CPDF_CMap::MixedTwoBytes, | |
164 1, | |
165 {0xa1, 0xfe}}, | |
166 {"KSCms-UHC", | |
167 CIDSET_KOREA1, | |
168 CIDCODING_KOREA, | |
169 CPDF_CMap::MixedTwoBytes, | |
170 1, | |
171 {0x81, 0xfe}}, | |
172 {"KSCms-UHC-HW", | |
173 CIDSET_KOREA1, | |
174 CIDCODING_KOREA, | |
175 CPDF_CMap::MixedTwoBytes, | |
176 1, | |
177 {0x81, 0xfe}}, | |
178 {"KSCpc-EUC", | |
179 CIDSET_KOREA1, | |
180 CIDCODING_KOREA, | |
181 CPDF_CMap::MixedTwoBytes, | |
182 1, | |
183 {0xa1, 0xfd}}, | |
184 {"UniKS-UCS2", CIDSET_KOREA1, CIDCODING_UCS2, CPDF_CMap::TwoBytes, 0, {}}, | |
185 {"UniKS-UTF16", CIDSET_KOREA1, CIDCODING_UTF16, CPDF_CMap::TwoBytes, 0, {}}, | |
186 }; | |
187 | |
188 CIDSet CIDSetFromSizeT(size_t index) { | |
189 if (index >= CIDSET_NUM_SETS) { | |
190 NOTREACHED(); | |
191 return CIDSET_UNKNOWN; | |
192 } | |
193 return static_cast<CIDSet>(index); | |
194 } | |
195 | |
196 CIDSet CharsetFromOrdering(const CFX_ByteString& ordering) { | |
197 for (size_t charset = 1; charset < FX_ArraySize(g_CharsetNames); ++charset) { | |
198 if (ordering == CFX_ByteStringC(g_CharsetNames[charset])) | |
199 return CIDSetFromSizeT(charset); | |
200 } | |
201 return CIDSET_UNKNOWN; | |
202 } | |
203 | |
204 CFX_ByteString CMap_GetString(const CFX_ByteStringC& word) { | |
205 return word.Mid(1, word.GetLength() - 2); | |
206 } | |
207 | |
208 int CompareDWORD(const void* data1, const void* data2) { | |
209 return (*(FX_DWORD*)data1) - (*(FX_DWORD*)data2); | |
210 } | |
211 | |
212 int CompareCID(const void* key, const void* element) { | |
213 if ((*(FX_DWORD*)key) < (*(FX_DWORD*)element)) { | |
214 return -1; | |
215 } | |
216 if ((*(FX_DWORD*)key) > | |
217 (*(FX_DWORD*)element) + ((FX_DWORD*)element)[1] / 65536) { | |
218 return 1; | |
219 } | |
220 return 0; | |
221 } | |
222 | |
223 int CheckCodeRange(uint8_t* codes, | |
224 int size, | |
225 CMap_CodeRange* pRanges, | |
226 int nRanges) { | |
227 int iSeg = nRanges - 1; | |
228 while (iSeg >= 0) { | |
229 if (pRanges[iSeg].m_CharSize < size) { | |
230 --iSeg; | |
231 continue; | |
232 } | |
233 int iChar = 0; | |
234 while (iChar < size) { | |
235 if (codes[iChar] < pRanges[iSeg].m_Lower[iChar] || | |
236 codes[iChar] > pRanges[iSeg].m_Upper[iChar]) { | |
237 break; | |
238 } | |
239 ++iChar; | |
240 } | |
241 if (iChar == pRanges[iSeg].m_CharSize) | |
242 return 2; | |
243 | |
244 if (iChar) | |
245 return (size == pRanges[iSeg].m_CharSize) ? 2 : 1; | |
246 iSeg--; | |
247 } | |
248 return 0; | |
249 } | |
250 | |
251 int GetCharSizeImpl(FX_DWORD charcode, | |
252 CMap_CodeRange* pRanges, | |
253 int iRangesSize) { | |
254 if (!iRangesSize) | |
255 return 1; | |
256 | |
257 uint8_t codes[4]; | |
258 codes[0] = codes[1] = 0x00; | |
259 codes[2] = (uint8_t)(charcode >> 8 & 0xFF); | |
260 codes[3] = (uint8_t)charcode; | |
261 int offset = 0; | |
262 int size = 4; | |
263 for (int i = 0; i < 4; ++i) { | |
264 int iSeg = iRangesSize - 1; | |
265 while (iSeg >= 0) { | |
266 if (pRanges[iSeg].m_CharSize < size) { | |
267 --iSeg; | |
268 continue; | |
269 } | |
270 int iChar = 0; | |
271 while (iChar < size) { | |
272 if (codes[offset + iChar] < pRanges[iSeg].m_Lower[iChar] || | |
273 codes[offset + iChar] > pRanges[iSeg].m_Upper[iChar]) { | |
274 break; | |
275 } | |
276 ++iChar; | |
277 } | |
278 if (iChar == pRanges[iSeg].m_CharSize) | |
279 return size; | |
280 --iSeg; | |
281 } | |
282 --size; | |
283 ++offset; | |
284 } | |
285 return 1; | |
286 } | |
287 | |
288 bool IsValidEmbeddedCharcodeFromUnicodeCharset(CIDSet charset) { | |
289 switch (charset) { | |
290 case CIDSET_GB1: | |
291 case CIDSET_CNS1: | |
292 case CIDSET_JAPAN1: | |
293 case CIDSET_KOREA1: | |
294 return true; | |
295 | |
296 default: | |
297 return false; | |
298 } | |
299 } | |
300 | |
301 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | |
302 FX_DWORD EmbeddedCharcodeFromUnicode(const FXCMAP_CMap* pEmbedMap, | |
303 CIDSet charset, | |
304 FX_WCHAR unicode) { | |
305 if (!IsValidEmbeddedCharcodeFromUnicodeCharset(charset)) | |
306 return 0; | |
307 | |
308 CPDF_FontGlobals* pFontGlobals = | |
309 CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals(); | |
310 const FX_WORD* pCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_pMap; | |
311 if (!pCodes) | |
312 return 0; | |
313 | |
314 int nCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_Count; | |
315 for (int i = 0; i < nCodes; ++i) { | |
316 if (pCodes[i] == unicode) { | |
317 FX_DWORD CharCode = FPDFAPI_CharCodeFromCID(pEmbedMap, i); | |
318 if (CharCode != 0) { | |
319 return CharCode; | |
320 } | |
321 } | |
322 } | |
323 return 0; | |
324 } | |
325 #endif // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | |
326 | |
327 FX_WCHAR EmbeddedUnicodeFromCharcode(const FXCMAP_CMap* pEmbedMap, | |
328 CIDSet charset, | |
329 FX_DWORD charcode) { | |
330 if (!IsValidEmbeddedCharcodeFromUnicodeCharset(charset)) | |
331 return 0; | |
332 | |
333 FX_WORD cid = FPDFAPI_CIDFromCharCode(pEmbedMap, charcode); | |
334 if (cid == 0) | |
335 return 0; | |
336 | |
337 CPDF_FontGlobals* pFontGlobals = | |
338 CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals(); | |
339 const FX_WORD* pCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_pMap; | |
340 if (!pCodes) | |
341 return 0; | |
342 | |
343 if (cid < pFontGlobals->m_EmbeddedToUnicodes[charset].m_Count) | |
344 return pCodes[cid]; | |
345 return 0; | |
346 } | |
347 | |
348 void FT_UseCIDCharmap(FXFT_Face face, int coding) { | |
349 int encoding; | |
350 switch (coding) { | |
351 case CIDCODING_GB: | |
352 encoding = FXFT_ENCODING_GB2312; | |
353 break; | |
354 case CIDCODING_BIG5: | |
355 encoding = FXFT_ENCODING_BIG5; | |
356 break; | |
357 case CIDCODING_JIS: | |
358 encoding = FXFT_ENCODING_SJIS; | |
359 break; | |
360 case CIDCODING_KOREA: | |
361 encoding = FXFT_ENCODING_JOHAB; | |
362 break; | |
363 default: | |
364 encoding = FXFT_ENCODING_UNICODE; | |
365 } | |
366 int err = FXFT_Select_Charmap(face, encoding); | |
367 if (err) { | |
368 err = FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE); | |
369 } | |
370 if (err && FXFT_Get_Face_Charmaps(face)) { | |
371 FXFT_Set_Charmap(face, *FXFT_Get_Face_Charmaps(face)); | |
372 } | |
373 } | |
374 | |
375 const struct CIDTransform { | |
376 FX_WORD CID; | |
377 uint8_t a, b, c, d, e, f; | |
378 } g_Japan1_VertCIDs[] = { | |
379 {97, 129, 0, 0, 127, 55, 0}, | |
380 {7887, 127, 0, 0, 127, 76, 89}, | |
381 {7888, 127, 0, 0, 127, 79, 94}, | |
382 {7889, 0, 129, 127, 0, 17, 127}, | |
383 {7890, 0, 129, 127, 0, 17, 127}, | |
384 {7891, 0, 129, 127, 0, 17, 127}, | |
385 {7892, 0, 129, 127, 0, 17, 127}, | |
386 {7893, 0, 129, 127, 0, 17, 127}, | |
387 {7894, 0, 129, 127, 0, 17, 127}, | |
388 {7895, 0, 129, 127, 0, 17, 127}, | |
389 {7896, 0, 129, 127, 0, 17, 127}, | |
390 {7897, 0, 129, 127, 0, 17, 127}, | |
391 {7898, 0, 129, 127, 0, 17, 127}, | |
392 {7899, 0, 129, 127, 0, 17, 104}, | |
393 {7900, 0, 129, 127, 0, 17, 127}, | |
394 {7901, 0, 129, 127, 0, 17, 104}, | |
395 {7902, 0, 129, 127, 0, 17, 127}, | |
396 {7903, 0, 129, 127, 0, 17, 127}, | |
397 {7904, 0, 129, 127, 0, 17, 127}, | |
398 {7905, 0, 129, 127, 0, 17, 114}, | |
399 {7906, 0, 129, 127, 0, 17, 127}, | |
400 {7907, 0, 129, 127, 0, 17, 127}, | |
401 {7908, 0, 129, 127, 0, 17, 127}, | |
402 {7909, 0, 129, 127, 0, 17, 127}, | |
403 {7910, 0, 129, 127, 0, 17, 127}, | |
404 {7911, 0, 129, 127, 0, 17, 127}, | |
405 {7912, 0, 129, 127, 0, 17, 127}, | |
406 {7913, 0, 129, 127, 0, 17, 127}, | |
407 {7914, 0, 129, 127, 0, 17, 127}, | |
408 {7915, 0, 129, 127, 0, 17, 114}, | |
409 {7916, 0, 129, 127, 0, 17, 127}, | |
410 {7917, 0, 129, 127, 0, 17, 127}, | |
411 {7918, 127, 0, 0, 127, 18, 25}, | |
412 {7919, 127, 0, 0, 127, 18, 25}, | |
413 {7920, 127, 0, 0, 127, 18, 25}, | |
414 {7921, 127, 0, 0, 127, 18, 25}, | |
415 {7922, 127, 0, 0, 127, 18, 25}, | |
416 {7923, 127, 0, 0, 127, 18, 25}, | |
417 {7924, 127, 0, 0, 127, 18, 25}, | |
418 {7925, 127, 0, 0, 127, 18, 25}, | |
419 {7926, 127, 0, 0, 127, 18, 25}, | |
420 {7927, 127, 0, 0, 127, 18, 25}, | |
421 {7928, 127, 0, 0, 127, 18, 25}, | |
422 {7929, 127, 0, 0, 127, 18, 25}, | |
423 {7930, 127, 0, 0, 127, 18, 25}, | |
424 {7931, 127, 0, 0, 127, 18, 25}, | |
425 {7932, 127, 0, 0, 127, 18, 25}, | |
426 {7933, 127, 0, 0, 127, 18, 25}, | |
427 {7934, 127, 0, 0, 127, 18, 25}, | |
428 {7935, 127, 0, 0, 127, 18, 25}, | |
429 {7936, 127, 0, 0, 127, 18, 25}, | |
430 {7937, 127, 0, 0, 127, 18, 25}, | |
431 {7938, 127, 0, 0, 127, 18, 25}, | |
432 {7939, 127, 0, 0, 127, 18, 25}, | |
433 {8720, 0, 129, 127, 0, 19, 102}, | |
434 {8721, 0, 129, 127, 0, 13, 127}, | |
435 {8722, 0, 129, 127, 0, 19, 108}, | |
436 {8723, 0, 129, 127, 0, 19, 102}, | |
437 {8724, 0, 129, 127, 0, 19, 102}, | |
438 {8725, 0, 129, 127, 0, 19, 102}, | |
439 {8726, 0, 129, 127, 0, 19, 102}, | |
440 {8727, 0, 129, 127, 0, 19, 102}, | |
441 {8728, 0, 129, 127, 0, 19, 114}, | |
442 {8729, 0, 129, 127, 0, 19, 114}, | |
443 {8730, 0, 129, 127, 0, 38, 108}, | |
444 {8731, 0, 129, 127, 0, 13, 108}, | |
445 {8732, 0, 129, 127, 0, 19, 108}, | |
446 {8733, 0, 129, 127, 0, 19, 108}, | |
447 {8734, 0, 129, 127, 0, 19, 108}, | |
448 {8735, 0, 129, 127, 0, 19, 108}, | |
449 {8736, 0, 129, 127, 0, 19, 102}, | |
450 {8737, 0, 129, 127, 0, 19, 102}, | |
451 {8738, 0, 129, 127, 0, 19, 102}, | |
452 {8739, 0, 129, 127, 0, 19, 102}, | |
453 {8740, 0, 129, 127, 0, 19, 102}, | |
454 {8741, 0, 129, 127, 0, 19, 102}, | |
455 {8742, 0, 129, 127, 0, 19, 102}, | |
456 {8743, 0, 129, 127, 0, 19, 102}, | |
457 {8744, 0, 129, 127, 0, 19, 102}, | |
458 {8745, 0, 129, 127, 0, 19, 102}, | |
459 {8746, 0, 129, 127, 0, 19, 114}, | |
460 {8747, 0, 129, 127, 0, 19, 114}, | |
461 {8748, 0, 129, 127, 0, 19, 102}, | |
462 {8749, 0, 129, 127, 0, 19, 102}, | |
463 {8750, 0, 129, 127, 0, 19, 102}, | |
464 {8751, 0, 129, 127, 0, 19, 102}, | |
465 {8752, 0, 129, 127, 0, 19, 102}, | |
466 {8753, 0, 129, 127, 0, 19, 102}, | |
467 {8754, 0, 129, 127, 0, 19, 102}, | |
468 {8755, 0, 129, 127, 0, 19, 102}, | |
469 {8756, 0, 129, 127, 0, 19, 102}, | |
470 {8757, 0, 129, 127, 0, 19, 102}, | |
471 {8758, 0, 129, 127, 0, 19, 102}, | |
472 {8759, 0, 129, 127, 0, 19, 102}, | |
473 {8760, 0, 129, 127, 0, 19, 102}, | |
474 {8761, 0, 129, 127, 0, 19, 102}, | |
475 {8762, 0, 129, 127, 0, 19, 102}, | |
476 {8763, 0, 129, 127, 0, 19, 102}, | |
477 {8764, 0, 129, 127, 0, 19, 102}, | |
478 {8765, 0, 129, 127, 0, 19, 102}, | |
479 {8766, 0, 129, 127, 0, 19, 102}, | |
480 {8767, 0, 129, 127, 0, 19, 102}, | |
481 {8768, 0, 129, 127, 0, 19, 102}, | |
482 {8769, 0, 129, 127, 0, 19, 102}, | |
483 {8770, 0, 129, 127, 0, 19, 102}, | |
484 {8771, 0, 129, 127, 0, 19, 102}, | |
485 {8772, 0, 129, 127, 0, 19, 102}, | |
486 {8773, 0, 129, 127, 0, 19, 102}, | |
487 {8774, 0, 129, 127, 0, 19, 102}, | |
488 {8775, 0, 129, 127, 0, 19, 102}, | |
489 {8776, 0, 129, 127, 0, 19, 102}, | |
490 {8777, 0, 129, 127, 0, 19, 102}, | |
491 {8778, 0, 129, 127, 0, 19, 102}, | |
492 {8779, 0, 129, 127, 0, 19, 114}, | |
493 {8780, 0, 129, 127, 0, 19, 108}, | |
494 {8781, 0, 129, 127, 0, 19, 114}, | |
495 {8782, 0, 129, 127, 0, 13, 114}, | |
496 {8783, 0, 129, 127, 0, 19, 108}, | |
497 {8784, 0, 129, 127, 0, 13, 114}, | |
498 {8785, 0, 129, 127, 0, 19, 108}, | |
499 {8786, 0, 129, 127, 0, 19, 108}, | |
500 {8787, 0, 129, 127, 0, 19, 108}, | |
501 {8788, 0, 129, 127, 0, 19, 108}, | |
502 {8789, 0, 129, 127, 0, 19, 108}, | |
503 {8790, 0, 129, 127, 0, 19, 108}, | |
504 {8791, 0, 129, 127, 0, 19, 108}, | |
505 {8792, 0, 129, 127, 0, 19, 108}, | |
506 {8793, 0, 129, 127, 0, 19, 108}, | |
507 {8794, 0, 129, 127, 0, 19, 108}, | |
508 {8795, 0, 129, 127, 0, 19, 108}, | |
509 {8796, 0, 129, 127, 0, 19, 108}, | |
510 {8797, 0, 129, 127, 0, 19, 108}, | |
511 {8798, 0, 129, 127, 0, 19, 108}, | |
512 {8799, 0, 129, 127, 0, 19, 108}, | |
513 {8800, 0, 129, 127, 0, 19, 108}, | |
514 {8801, 0, 129, 127, 0, 19, 108}, | |
515 {8802, 0, 129, 127, 0, 19, 108}, | |
516 {8803, 0, 129, 127, 0, 19, 108}, | |
517 {8804, 0, 129, 127, 0, 19, 108}, | |
518 {8805, 0, 129, 127, 0, 19, 108}, | |
519 {8806, 0, 129, 127, 0, 19, 108}, | |
520 {8807, 0, 129, 127, 0, 19, 108}, | |
521 {8808, 0, 129, 127, 0, 19, 108}, | |
522 {8809, 0, 129, 127, 0, 19, 108}, | |
523 {8810, 0, 129, 127, 0, 19, 108}, | |
524 {8811, 0, 129, 127, 0, 19, 114}, | |
525 {8812, 0, 129, 127, 0, 19, 102}, | |
526 {8813, 0, 129, 127, 0, 19, 114}, | |
527 {8814, 0, 129, 127, 0, 76, 102}, | |
528 {8815, 0, 129, 127, 0, 13, 121}, | |
529 {8816, 0, 129, 127, 0, 19, 114}, | |
530 {8817, 0, 129, 127, 0, 19, 127}, | |
531 {8818, 0, 129, 127, 0, 19, 114}, | |
532 {8819, 0, 129, 127, 0, 218, 108}, | |
533 }; | |
534 | |
535 int CompareCIDTransform(const void* key, const void* element) { | |
536 FX_WORD CID = *static_cast<const FX_WORD*>(key); | |
537 return CID - static_cast<const struct CIDTransform*>(element)->CID; | |
538 } | |
539 | |
540 } // namespace | |
541 | |
542 CPDF_CMapManager::CPDF_CMapManager() { | |
543 m_bPrompted = FALSE; | |
544 FXSYS_memset(m_CID2UnicodeMaps, 0, sizeof m_CID2UnicodeMaps); | |
545 } | |
546 CPDF_CMapManager::~CPDF_CMapManager() { | |
547 for (const auto& pair : m_CMaps) { | |
548 delete pair.second; | |
549 } | |
550 m_CMaps.clear(); | |
551 for (size_t i = 0; i < FX_ArraySize(m_CID2UnicodeMaps); ++i) { | |
552 delete m_CID2UnicodeMaps[i]; | |
553 } | |
554 } | |
555 CPDF_CMap* CPDF_CMapManager::GetPredefinedCMap(const CFX_ByteString& name, | |
556 FX_BOOL bPromptCJK) { | |
557 auto it = m_CMaps.find(name); | |
558 if (it != m_CMaps.end()) { | |
559 return it->second; | |
560 } | |
561 CPDF_CMap* pCMap = LoadPredefinedCMap(name, bPromptCJK); | |
562 if (!name.IsEmpty()) { | |
563 m_CMaps[name] = pCMap; | |
564 } | |
565 return pCMap; | |
566 } | |
567 CPDF_CMap* CPDF_CMapManager::LoadPredefinedCMap(const CFX_ByteString& name, | |
568 FX_BOOL bPromptCJK) { | |
569 CPDF_CMap* pCMap = new CPDF_CMap; | |
570 const FX_CHAR* pname = name; | |
571 if (*pname == '/') { | |
572 pname++; | |
573 } | |
574 pCMap->LoadPredefined(this, pname, bPromptCJK); | |
575 return pCMap; | |
576 } | |
577 | |
578 void CPDF_CMapManager::ReloadAll() { | |
579 for (const auto& pair : m_CMaps) { | |
580 CPDF_CMap* pCMap = pair.second; | |
581 pCMap->LoadPredefined(this, pair.first, FALSE); | |
582 } | |
583 for (size_t i = 0; i < FX_ArraySize(m_CID2UnicodeMaps); ++i) { | |
584 if (CPDF_CID2UnicodeMap* pMap = m_CID2UnicodeMaps[i]) { | |
585 pMap->Load(this, CIDSetFromSizeT(i), FALSE); | |
586 } | |
587 } | |
588 } | |
589 CPDF_CID2UnicodeMap* CPDF_CMapManager::GetCID2UnicodeMap(CIDSet charset, | |
590 FX_BOOL bPromptCJK) { | |
591 if (!m_CID2UnicodeMaps[charset]) | |
592 m_CID2UnicodeMaps[charset] = LoadCID2UnicodeMap(charset, bPromptCJK); | |
593 return m_CID2UnicodeMaps[charset]; | |
594 } | |
595 CPDF_CID2UnicodeMap* CPDF_CMapManager::LoadCID2UnicodeMap(CIDSet charset, | |
596 FX_BOOL bPromptCJK) { | |
597 CPDF_CID2UnicodeMap* pMap = new CPDF_CID2UnicodeMap(); | |
598 if (!pMap->Initialize()) { | |
599 delete pMap; | |
600 return NULL; | |
601 } | |
602 pMap->Load(this, charset, bPromptCJK); | |
603 return pMap; | |
604 } | |
605 CPDF_CMapParser::CPDF_CMapParser() { | |
606 m_pCMap = NULL; | |
607 m_Status = 0; | |
608 m_CodeSeq = 0; | |
609 } | |
610 FX_BOOL CPDF_CMapParser::Initialize(CPDF_CMap* pCMap) { | |
611 m_pCMap = pCMap; | |
612 m_Status = 0; | |
613 m_CodeSeq = 0; | |
614 m_AddMaps.EstimateSize(0, 10240); | |
615 return TRUE; | |
616 } | |
617 | |
618 void CPDF_CMapParser::ParseWord(const CFX_ByteStringC& word) { | |
619 if (word.IsEmpty()) { | |
620 return; | |
621 } | |
622 if (word == "begincidchar") { | |
623 m_Status = 1; | |
624 m_CodeSeq = 0; | |
625 } else if (word == "begincidrange") { | |
626 m_Status = 2; | |
627 m_CodeSeq = 0; | |
628 } else if (word == "endcidrange" || word == "endcidchar") { | |
629 m_Status = 0; | |
630 } else if (word == "/WMode") { | |
631 m_Status = 6; | |
632 } else if (word == "/Registry") { | |
633 m_Status = 3; | |
634 } else if (word == "/Ordering") { | |
635 m_Status = 4; | |
636 } else if (word == "/Supplement") { | |
637 m_Status = 5; | |
638 } else if (word == "begincodespacerange") { | |
639 m_Status = 7; | |
640 m_CodeSeq = 0; | |
641 } else if (word == "usecmap") { | |
642 } else if (m_Status == 1 || m_Status == 2) { | |
643 m_CodePoints[m_CodeSeq] = CMap_GetCode(word); | |
644 m_CodeSeq++; | |
645 FX_DWORD StartCode, EndCode; | |
646 FX_WORD StartCID; | |
647 if (m_Status == 1) { | |
648 if (m_CodeSeq < 2) { | |
649 return; | |
650 } | |
651 EndCode = StartCode = m_CodePoints[0]; | |
652 StartCID = (FX_WORD)m_CodePoints[1]; | |
653 } else { | |
654 if (m_CodeSeq < 3) { | |
655 return; | |
656 } | |
657 StartCode = m_CodePoints[0]; | |
658 EndCode = m_CodePoints[1]; | |
659 StartCID = (FX_WORD)m_CodePoints[2]; | |
660 } | |
661 if (EndCode < 0x10000) { | |
662 for (FX_DWORD code = StartCode; code <= EndCode; code++) { | |
663 m_pCMap->m_pMapping[code] = (FX_WORD)(StartCID + code - StartCode); | |
664 } | |
665 } else { | |
666 FX_DWORD buf[2]; | |
667 buf[0] = StartCode; | |
668 buf[1] = ((EndCode - StartCode) << 16) + StartCID; | |
669 m_AddMaps.AppendBlock(buf, sizeof buf); | |
670 } | |
671 m_CodeSeq = 0; | |
672 } else if (m_Status == 3) { | |
673 CMap_GetString(word); | |
674 m_Status = 0; | |
675 } else if (m_Status == 4) { | |
676 m_pCMap->m_Charset = CharsetFromOrdering(CMap_GetString(word)); | |
677 m_Status = 0; | |
678 } else if (m_Status == 5) { | |
679 CMap_GetCode(word); | |
680 m_Status = 0; | |
681 } else if (m_Status == 6) { | |
682 m_pCMap->m_bVertical = CMap_GetCode(word); | |
683 m_Status = 0; | |
684 } else if (m_Status == 7) { | |
685 if (word == "endcodespacerange") { | |
686 int nSegs = m_CodeRanges.GetSize(); | |
687 if (nSegs > 1) { | |
688 m_pCMap->m_CodingScheme = CPDF_CMap::MixedFourBytes; | |
689 m_pCMap->m_nCodeRanges = nSegs; | |
690 m_pCMap->m_pLeadingBytes = | |
691 FX_Alloc2D(uint8_t, nSegs, sizeof(CMap_CodeRange)); | |
692 FXSYS_memcpy(m_pCMap->m_pLeadingBytes, m_CodeRanges.GetData(), | |
693 nSegs * sizeof(CMap_CodeRange)); | |
694 } else if (nSegs == 1) { | |
695 m_pCMap->m_CodingScheme = (m_CodeRanges[0].m_CharSize == 2) | |
696 ? CPDF_CMap::TwoBytes | |
697 : CPDF_CMap::OneByte; | |
698 } | |
699 m_Status = 0; | |
700 } else { | |
701 if (word.GetLength() == 0 || word.GetAt(0) != '<') { | |
702 return; | |
703 } | |
704 if (m_CodeSeq % 2) { | |
705 CMap_CodeRange range; | |
706 if (CMap_GetCodeRange(range, m_LastWord, word)) { | |
707 m_CodeRanges.Add(range); | |
708 } | |
709 } | |
710 m_CodeSeq++; | |
711 } | |
712 } | |
713 m_LastWord = word; | |
714 } | |
715 | |
716 // Static. | |
717 FX_DWORD CPDF_CMapParser::CMap_GetCode(const CFX_ByteStringC& word) { | |
718 int num = 0; | |
719 if (word.GetAt(0) == '<') { | |
720 for (int i = 1; i < word.GetLength() && std::isxdigit(word.GetAt(i)); ++i) | |
721 num = num * 16 + FXSYS_toHexDigit(word.GetAt(i)); | |
722 return num; | |
723 } | |
724 | |
725 for (int i = 0; i < word.GetLength() && std::isdigit(word.GetAt(i)); ++i) | |
726 num = num * 10 + FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(word.GetAt(i))); | |
727 return num; | |
728 } | |
729 | |
730 // Static. | |
731 bool CPDF_CMapParser::CMap_GetCodeRange(CMap_CodeRange& range, | |
732 const CFX_ByteStringC& first, | |
733 const CFX_ByteStringC& second) { | |
734 if (first.GetLength() == 0 || first.GetAt(0) != '<') | |
735 return false; | |
736 | |
737 int i; | |
738 for (i = 1; i < first.GetLength(); ++i) { | |
739 if (first.GetAt(i) == '>') { | |
740 break; | |
741 } | |
742 } | |
743 range.m_CharSize = (i - 1) / 2; | |
744 if (range.m_CharSize > 4) | |
745 return false; | |
746 | |
747 for (i = 0; i < range.m_CharSize; ++i) { | |
748 uint8_t digit1 = first.GetAt(i * 2 + 1); | |
749 uint8_t digit2 = first.GetAt(i * 2 + 2); | |
750 range.m_Lower[i] = FXSYS_toHexDigit(digit1) * 16 + FXSYS_toHexDigit(digit2); | |
751 } | |
752 | |
753 FX_DWORD size = second.GetLength(); | |
754 for (i = 0; i < range.m_CharSize; ++i) { | |
755 uint8_t digit1 = ((FX_DWORD)i * 2 + 1 < size) | |
756 ? second.GetAt((FX_STRSIZE)i * 2 + 1) | |
757 : '0'; | |
758 uint8_t digit2 = ((FX_DWORD)i * 2 + 2 < size) | |
759 ? second.GetAt((FX_STRSIZE)i * 2 + 2) | |
760 : '0'; | |
761 range.m_Upper[i] = FXSYS_toHexDigit(digit1) * 16 + FXSYS_toHexDigit(digit2); | |
762 } | |
763 return true; | |
764 } | |
765 | |
766 CPDF_CMap::CPDF_CMap() { | |
767 m_Charset = CIDSET_UNKNOWN; | |
768 m_Coding = CIDCODING_UNKNOWN; | |
769 m_CodingScheme = TwoBytes; | |
770 m_bVertical = 0; | |
771 m_bLoaded = FALSE; | |
772 m_pMapping = NULL; | |
773 m_pLeadingBytes = NULL; | |
774 m_pAddMapping = NULL; | |
775 m_pEmbedMap = NULL; | |
776 m_pUseMap = NULL; | |
777 m_nCodeRanges = 0; | |
778 } | |
779 CPDF_CMap::~CPDF_CMap() { | |
780 FX_Free(m_pMapping); | |
781 FX_Free(m_pAddMapping); | |
782 FX_Free(m_pLeadingBytes); | |
783 delete m_pUseMap; | |
784 } | |
785 void CPDF_CMap::Release() { | |
786 if (m_PredefinedCMap.IsEmpty()) { | |
787 delete this; | |
788 } | |
789 } | |
790 | |
791 FX_BOOL CPDF_CMap::LoadPredefined(CPDF_CMapManager* pMgr, | |
792 const FX_CHAR* pName, | |
793 FX_BOOL bPromptCJK) { | |
794 m_PredefinedCMap = pName; | |
795 if (m_PredefinedCMap == "Identity-H" || m_PredefinedCMap == "Identity-V") { | |
796 m_Coding = CIDCODING_CID; | |
797 m_bVertical = pName[9] == 'V'; | |
798 m_bLoaded = TRUE; | |
799 return TRUE; | |
800 } | |
801 CFX_ByteString cmapid = m_PredefinedCMap; | |
802 m_bVertical = cmapid.Right(1) == "V"; | |
803 if (cmapid.GetLength() > 2) { | |
804 cmapid = cmapid.Left(cmapid.GetLength() - 2); | |
805 } | |
806 const CPDF_PredefinedCMap* map = nullptr; | |
807 for (size_t i = 0; i < FX_ArraySize(g_PredefinedCMaps); ++i) { | |
808 if (cmapid == CFX_ByteStringC(g_PredefinedCMaps[i].m_pName)) { | |
809 map = &g_PredefinedCMaps[i]; | |
810 break; | |
811 } | |
812 } | |
813 if (!map) | |
814 return FALSE; | |
815 | |
816 m_Charset = map->m_Charset; | |
817 m_Coding = map->m_Coding; | |
818 m_CodingScheme = map->m_CodingScheme; | |
819 if (m_CodingScheme == MixedTwoBytes) { | |
820 m_pLeadingBytes = FX_Alloc(uint8_t, 256); | |
821 for (FX_DWORD i = 0; i < map->m_LeadingSegCount; ++i) { | |
822 const uint8_t* segs = map->m_LeadingSegs; | |
823 for (int b = segs[i * 2]; b <= segs[i * 2 + 1]; ++b) { | |
824 m_pLeadingBytes[b] = 1; | |
825 } | |
826 } | |
827 } | |
828 FPDFAPI_FindEmbeddedCMap(pName, m_Charset, m_Coding, m_pEmbedMap); | |
829 if (m_pEmbedMap) { | |
830 m_bLoaded = TRUE; | |
831 return TRUE; | |
832 } | |
833 return FALSE; | |
834 } | |
835 FX_BOOL CPDF_CMap::LoadEmbedded(const uint8_t* pData, FX_DWORD size) { | |
836 m_pMapping = FX_Alloc(FX_WORD, 65536); | |
837 CPDF_CMapParser parser; | |
838 parser.Initialize(this); | |
839 CPDF_SimpleParser syntax(pData, size); | |
840 while (1) { | |
841 CFX_ByteStringC word = syntax.GetWord(); | |
842 if (word.IsEmpty()) { | |
843 break; | |
844 } | |
845 parser.ParseWord(word); | |
846 } | |
847 if (m_CodingScheme == MixedFourBytes && parser.m_AddMaps.GetSize()) { | |
848 m_pAddMapping = FX_Alloc(uint8_t, parser.m_AddMaps.GetSize() + 4); | |
849 *(FX_DWORD*)m_pAddMapping = parser.m_AddMaps.GetSize() / 8; | |
850 FXSYS_memcpy(m_pAddMapping + 4, parser.m_AddMaps.GetBuffer(), | |
851 parser.m_AddMaps.GetSize()); | |
852 FXSYS_qsort(m_pAddMapping + 4, parser.m_AddMaps.GetSize() / 8, 8, | |
853 CompareDWORD); | |
854 } | |
855 return TRUE; | |
856 } | |
857 | |
858 FX_WORD CPDF_CMap::CIDFromCharCode(FX_DWORD charcode) const { | |
859 if (m_Coding == CIDCODING_CID) { | |
860 return (FX_WORD)charcode; | |
861 } | |
862 if (m_pEmbedMap) { | |
863 return FPDFAPI_CIDFromCharCode(m_pEmbedMap, charcode); | |
864 } | |
865 if (!m_pMapping) { | |
866 return (FX_WORD)charcode; | |
867 } | |
868 if (charcode >> 16) { | |
869 if (m_pAddMapping) { | |
870 void* found = FXSYS_bsearch(&charcode, m_pAddMapping + 4, | |
871 *(FX_DWORD*)m_pAddMapping, 8, CompareCID); | |
872 if (!found) { | |
873 if (m_pUseMap) { | |
874 return m_pUseMap->CIDFromCharCode(charcode); | |
875 } | |
876 return 0; | |
877 } | |
878 return (FX_WORD)(((FX_DWORD*)found)[1] % 65536 + charcode - | |
879 *(FX_DWORD*)found); | |
880 } | |
881 if (m_pUseMap) | |
882 return m_pUseMap->CIDFromCharCode(charcode); | |
883 return 0; | |
884 } | |
885 FX_DWORD CID = m_pMapping[charcode]; | |
886 if (!CID && m_pUseMap) | |
887 return m_pUseMap->CIDFromCharCode(charcode); | |
888 return (FX_WORD)CID; | |
889 } | |
890 | |
891 FX_DWORD CPDF_CMap::GetNextChar(const FX_CHAR* pString, | |
892 int nStrLen, | |
893 int& offset) const { | |
894 switch (m_CodingScheme) { | |
895 case OneByte: | |
896 return ((uint8_t*)pString)[offset++]; | |
897 case TwoBytes: | |
898 offset += 2; | |
899 return ((uint8_t*)pString)[offset - 2] * 256 + | |
900 ((uint8_t*)pString)[offset - 1]; | |
901 case MixedTwoBytes: { | |
902 uint8_t byte1 = ((uint8_t*)pString)[offset++]; | |
903 if (!m_pLeadingBytes[byte1]) { | |
904 return byte1; | |
905 } | |
906 uint8_t byte2 = ((uint8_t*)pString)[offset++]; | |
907 return byte1 * 256 + byte2; | |
908 } | |
909 case MixedFourBytes: { | |
910 uint8_t codes[4]; | |
911 int char_size = 1; | |
912 codes[0] = ((uint8_t*)pString)[offset++]; | |
913 CMap_CodeRange* pRanges = (CMap_CodeRange*)m_pLeadingBytes; | |
914 while (1) { | |
915 int ret = CheckCodeRange(codes, char_size, pRanges, m_nCodeRanges); | |
916 if (ret == 0) { | |
917 return 0; | |
918 } | |
919 if (ret == 2) { | |
920 FX_DWORD charcode = 0; | |
921 for (int i = 0; i < char_size; i++) { | |
922 charcode = (charcode << 8) + codes[i]; | |
923 } | |
924 return charcode; | |
925 } | |
926 if (char_size == 4 || offset == nStrLen) { | |
927 return 0; | |
928 } | |
929 codes[char_size++] = ((uint8_t*)pString)[offset++]; | |
930 } | |
931 break; | |
932 } | |
933 } | |
934 return 0; | |
935 } | |
936 int CPDF_CMap::GetCharSize(FX_DWORD charcode) const { | |
937 switch (m_CodingScheme) { | |
938 case OneByte: | |
939 return 1; | |
940 case TwoBytes: | |
941 return 2; | |
942 case MixedTwoBytes: | |
943 case MixedFourBytes: | |
944 if (charcode < 0x100) { | |
945 return 1; | |
946 } | |
947 if (charcode < 0x10000) { | |
948 return 2; | |
949 } | |
950 if (charcode < 0x1000000) { | |
951 return 3; | |
952 } | |
953 return 4; | |
954 } | |
955 return 1; | |
956 } | |
957 int CPDF_CMap::CountChar(const FX_CHAR* pString, int size) const { | |
958 switch (m_CodingScheme) { | |
959 case OneByte: | |
960 return size; | |
961 case TwoBytes: | |
962 return (size + 1) / 2; | |
963 case MixedTwoBytes: { | |
964 int count = 0; | |
965 for (int i = 0; i < size; i++) { | |
966 count++; | |
967 if (m_pLeadingBytes[((uint8_t*)pString)[i]]) { | |
968 i++; | |
969 } | |
970 } | |
971 return count; | |
972 } | |
973 case MixedFourBytes: { | |
974 int count = 0, offset = 0; | |
975 while (offset < size) { | |
976 GetNextChar(pString, size, offset); | |
977 count++; | |
978 } | |
979 return count; | |
980 } | |
981 } | |
982 return size; | |
983 } | |
984 | |
985 int CPDF_CMap::AppendChar(FX_CHAR* str, FX_DWORD charcode) const { | |
986 switch (m_CodingScheme) { | |
987 case OneByte: | |
988 str[0] = (uint8_t)charcode; | |
989 return 1; | |
990 case TwoBytes: | |
991 str[0] = (uint8_t)(charcode / 256); | |
992 str[1] = (uint8_t)(charcode % 256); | |
993 return 2; | |
994 case MixedTwoBytes: | |
995 case MixedFourBytes: | |
996 if (charcode < 0x100) { | |
997 CMap_CodeRange* pRanges = (CMap_CodeRange*)m_pLeadingBytes; | |
998 int iSize = GetCharSizeImpl(charcode, pRanges, m_nCodeRanges); | |
999 if (iSize == 0) { | |
1000 iSize = 1; | |
1001 } | |
1002 if (iSize > 1) { | |
1003 FXSYS_memset(str, 0, sizeof(uint8_t) * iSize); | |
1004 } | |
1005 str[iSize - 1] = (uint8_t)charcode; | |
1006 return iSize; | |
1007 } | |
1008 if (charcode < 0x10000) { | |
1009 str[0] = (uint8_t)(charcode >> 8); | |
1010 str[1] = (uint8_t)charcode; | |
1011 return 2; | |
1012 } | |
1013 if (charcode < 0x1000000) { | |
1014 str[0] = (uint8_t)(charcode >> 16); | |
1015 str[1] = (uint8_t)(charcode >> 8); | |
1016 str[2] = (uint8_t)charcode; | |
1017 return 3; | |
1018 } | |
1019 str[0] = (uint8_t)(charcode >> 24); | |
1020 str[1] = (uint8_t)(charcode >> 16); | |
1021 str[2] = (uint8_t)(charcode >> 8); | |
1022 str[3] = (uint8_t)charcode; | |
1023 return 4; | |
1024 } | |
1025 return 0; | |
1026 } | |
1027 CPDF_CID2UnicodeMap::CPDF_CID2UnicodeMap() { | |
1028 m_EmbeddedCount = 0; | |
1029 } | |
1030 CPDF_CID2UnicodeMap::~CPDF_CID2UnicodeMap() {} | |
1031 FX_BOOL CPDF_CID2UnicodeMap::Initialize() { | |
1032 return TRUE; | |
1033 } | |
1034 FX_BOOL CPDF_CID2UnicodeMap::IsLoaded() { | |
1035 return m_EmbeddedCount != 0; | |
1036 } | |
1037 FX_WCHAR CPDF_CID2UnicodeMap::UnicodeFromCID(FX_WORD CID) { | |
1038 if (m_Charset == CIDSET_UNICODE) { | |
1039 return CID; | |
1040 } | |
1041 if (CID < m_EmbeddedCount) { | |
1042 return m_pEmbeddedMap[CID]; | |
1043 } | |
1044 return 0; | |
1045 } | |
1046 | |
1047 void CPDF_CID2UnicodeMap::Load(CPDF_CMapManager* pMgr, | |
1048 CIDSet charset, | |
1049 FX_BOOL bPromptCJK) { | |
1050 m_Charset = charset; | |
1051 FPDFAPI_LoadCID2UnicodeMap(charset, m_pEmbeddedMap, m_EmbeddedCount); | |
1052 } | |
1053 | |
1054 CPDF_CIDFont::CPDF_CIDFont() | |
1055 : m_pCMap(nullptr), | |
1056 m_pAllocatedCMap(nullptr), | |
1057 m_pCID2UnicodeMap(nullptr), | |
1058 m_pCIDToGIDMap(nullptr), | |
1059 m_bCIDIsGID(FALSE), | |
1060 m_pAnsiWidths(nullptr), | |
1061 m_bAdobeCourierStd(FALSE), | |
1062 m_pTTGSUBTable(nullptr) { | |
1063 } | |
1064 | |
1065 CPDF_CIDFont::~CPDF_CIDFont() { | |
1066 if (m_pAnsiWidths) { | |
1067 FX_Free(m_pAnsiWidths); | |
1068 } | |
1069 delete m_pAllocatedCMap; | |
1070 delete m_pCIDToGIDMap; | |
1071 delete m_pTTGSUBTable; | |
1072 } | |
1073 | |
1074 bool CPDF_CIDFont::IsCIDFont() const { | |
1075 return true; | |
1076 } | |
1077 | |
1078 const CPDF_CIDFont* CPDF_CIDFont::AsCIDFont() const { | |
1079 return this; | |
1080 } | |
1081 | |
1082 CPDF_CIDFont* CPDF_CIDFont::AsCIDFont() { | |
1083 return this; | |
1084 } | |
1085 | |
1086 FX_WORD CPDF_CIDFont::CIDFromCharCode(FX_DWORD charcode) const { | |
1087 if (!m_pCMap) { | |
1088 return (FX_WORD)charcode; | |
1089 } | |
1090 return m_pCMap->CIDFromCharCode(charcode); | |
1091 } | |
1092 | |
1093 FX_BOOL CPDF_CIDFont::IsVertWriting() const { | |
1094 return m_pCMap ? m_pCMap->IsVertWriting() : FALSE; | |
1095 } | |
1096 | |
1097 CFX_WideString CPDF_CIDFont::UnicodeFromCharCode(FX_DWORD charcode) const { | |
1098 CFX_WideString str = CPDF_Font::UnicodeFromCharCode(charcode); | |
1099 if (!str.IsEmpty()) | |
1100 return str; | |
1101 FX_WCHAR ret = GetUnicodeFromCharCode(charcode); | |
1102 if (ret == 0) | |
1103 return CFX_WideString(); | |
1104 return ret; | |
1105 } | |
1106 | |
1107 FX_WCHAR CPDF_CIDFont::GetUnicodeFromCharCode(FX_DWORD charcode) const { | |
1108 switch (m_pCMap->m_Coding) { | |
1109 case CIDCODING_UCS2: | |
1110 case CIDCODING_UTF16: | |
1111 return (FX_WCHAR)charcode; | |
1112 case CIDCODING_CID: | |
1113 if (!m_pCID2UnicodeMap || !m_pCID2UnicodeMap->IsLoaded()) { | |
1114 return 0; | |
1115 } | |
1116 return m_pCID2UnicodeMap->UnicodeFromCID((FX_WORD)charcode); | |
1117 } | |
1118 if (!m_pCMap->IsLoaded() || !m_pCID2UnicodeMap || | |
1119 !m_pCID2UnicodeMap->IsLoaded()) { | |
1120 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
1121 FX_WCHAR unicode; | |
1122 int charsize = 1; | |
1123 if (charcode > 255) { | |
1124 charcode = (charcode % 256) * 256 + (charcode / 256); | |
1125 charsize = 2; | |
1126 } | |
1127 int ret = FXSYS_MultiByteToWideChar(g_CharsetCPs[m_pCMap->m_Coding], 0, | |
1128 (const FX_CHAR*)&charcode, charsize, | |
1129 &unicode, 1); | |
1130 if (ret != 1) { | |
1131 return 0; | |
1132 } | |
1133 return unicode; | |
1134 #endif | |
1135 if (m_pCMap->m_pEmbedMap) { | |
1136 return EmbeddedUnicodeFromCharcode(m_pCMap->m_pEmbedMap, | |
1137 m_pCMap->m_Charset, charcode); | |
1138 } | |
1139 return 0; | |
1140 } | |
1141 return m_pCID2UnicodeMap->UnicodeFromCID(CIDFromCharCode(charcode)); | |
1142 } | |
1143 | |
1144 FX_DWORD CPDF_CIDFont::CharCodeFromUnicode(FX_WCHAR unicode) const { | |
1145 FX_DWORD charcode = CPDF_Font::CharCodeFromUnicode(unicode); | |
1146 if (charcode) | |
1147 return charcode; | |
1148 switch (m_pCMap->m_Coding) { | |
1149 case CIDCODING_UNKNOWN: | |
1150 return 0; | |
1151 case CIDCODING_UCS2: | |
1152 case CIDCODING_UTF16: | |
1153 return unicode; | |
1154 case CIDCODING_CID: { | |
1155 if (!m_pCID2UnicodeMap || !m_pCID2UnicodeMap->IsLoaded()) { | |
1156 return 0; | |
1157 } | |
1158 FX_DWORD CID = 0; | |
1159 while (CID < 65536) { | |
1160 FX_WCHAR this_unicode = m_pCID2UnicodeMap->UnicodeFromCID((FX_WORD)CID); | |
1161 if (this_unicode == unicode) { | |
1162 return CID; | |
1163 } | |
1164 CID++; | |
1165 } | |
1166 break; | |
1167 } | |
1168 } | |
1169 | |
1170 if (unicode < 0x80) { | |
1171 return static_cast<FX_DWORD>(unicode); | |
1172 } | |
1173 if (m_pCMap->m_Coding == CIDCODING_CID) { | |
1174 return 0; | |
1175 } | |
1176 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
1177 uint8_t buffer[32]; | |
1178 int ret = | |
1179 FXSYS_WideCharToMultiByte(g_CharsetCPs[m_pCMap->m_Coding], 0, &unicode, 1, | |
1180 (char*)buffer, 4, NULL, NULL); | |
1181 if (ret == 1) { | |
1182 return buffer[0]; | |
1183 } | |
1184 if (ret == 2) { | |
1185 return buffer[0] * 256 + buffer[1]; | |
1186 } | |
1187 #else | |
1188 if (m_pCMap->m_pEmbedMap) { | |
1189 return EmbeddedCharcodeFromUnicode(m_pCMap->m_pEmbedMap, m_pCMap->m_Charset, | |
1190 unicode); | |
1191 } | |
1192 #endif | |
1193 return 0; | |
1194 } | |
1195 | |
1196 FX_BOOL CPDF_CIDFont::Load() { | |
1197 if (m_pFontDict->GetStringBy("Subtype") == "TrueType") { | |
1198 return LoadGB2312(); | |
1199 } | |
1200 CPDF_Array* pFonts = m_pFontDict->GetArrayBy("DescendantFonts"); | |
1201 if (!pFonts) { | |
1202 return FALSE; | |
1203 } | |
1204 if (pFonts->GetCount() != 1) { | |
1205 return FALSE; | |
1206 } | |
1207 CPDF_Dictionary* pCIDFontDict = pFonts->GetDictAt(0); | |
1208 if (!pCIDFontDict) { | |
1209 return FALSE; | |
1210 } | |
1211 m_BaseFont = pCIDFontDict->GetStringBy("BaseFont"); | |
1212 if ((m_BaseFont.Compare("CourierStd") == 0 || | |
1213 m_BaseFont.Compare("CourierStd-Bold") == 0 || | |
1214 m_BaseFont.Compare("CourierStd-BoldOblique") == 0 || | |
1215 m_BaseFont.Compare("CourierStd-Oblique") == 0) && | |
1216 !IsEmbedded()) { | |
1217 m_bAdobeCourierStd = TRUE; | |
1218 } | |
1219 CPDF_Dictionary* pFontDesc = pCIDFontDict->GetDictBy("FontDescriptor"); | |
1220 if (pFontDesc) { | |
1221 LoadFontDescriptor(pFontDesc); | |
1222 } | |
1223 CPDF_Object* pEncoding = m_pFontDict->GetElementValue("Encoding"); | |
1224 if (!pEncoding) { | |
1225 return FALSE; | |
1226 } | |
1227 CFX_ByteString subtype = pCIDFontDict->GetStringBy("Subtype"); | |
1228 m_bType1 = (subtype == "CIDFontType0"); | |
1229 | |
1230 if (pEncoding->IsName()) { | |
1231 CFX_ByteString cmap = pEncoding->GetString(); | |
1232 m_pCMap = | |
1233 CPDF_ModuleMgr::Get() | |
1234 ->GetPageModule() | |
1235 ->GetFontGlobals() | |
1236 ->m_CMapManager.GetPredefinedCMap(cmap, m_pFontFile && m_bType1); | |
1237 } else if (CPDF_Stream* pStream = pEncoding->AsStream()) { | |
1238 m_pAllocatedCMap = m_pCMap = new CPDF_CMap; | |
1239 CPDF_StreamAcc acc; | |
1240 acc.LoadAllData(pStream, FALSE); | |
1241 m_pCMap->LoadEmbedded(acc.GetData(), acc.GetSize()); | |
1242 } else { | |
1243 return FALSE; | |
1244 } | |
1245 if (!m_pCMap) { | |
1246 return FALSE; | |
1247 } | |
1248 m_Charset = m_pCMap->m_Charset; | |
1249 if (m_Charset == CIDSET_UNKNOWN) { | |
1250 CPDF_Dictionary* pCIDInfo = pCIDFontDict->GetDictBy("CIDSystemInfo"); | |
1251 if (pCIDInfo) { | |
1252 m_Charset = CharsetFromOrdering(pCIDInfo->GetStringBy("Ordering")); | |
1253 } | |
1254 } | |
1255 if (m_Charset != CIDSET_UNKNOWN) | |
1256 m_pCID2UnicodeMap = | |
1257 CPDF_ModuleMgr::Get() | |
1258 ->GetPageModule() | |
1259 ->GetFontGlobals() | |
1260 ->m_CMapManager.GetCID2UnicodeMap( | |
1261 m_Charset, | |
1262 !m_pFontFile && (m_pCMap->m_Coding == CIDCODING_CID || | |
1263 pCIDFontDict->KeyExist("W"))); | |
1264 if (m_Font.GetFace()) { | |
1265 if (m_bType1) { | |
1266 FXFT_Select_Charmap(m_Font.GetFace(), FXFT_ENCODING_UNICODE); | |
1267 } else { | |
1268 FT_UseCIDCharmap(m_Font.GetFace(), m_pCMap->m_Coding); | |
1269 } | |
1270 } | |
1271 m_DefaultWidth = pCIDFontDict->GetIntegerBy("DW", 1000); | |
1272 CPDF_Array* pWidthArray = pCIDFontDict->GetArrayBy("W"); | |
1273 if (pWidthArray) { | |
1274 LoadMetricsArray(pWidthArray, m_WidthList, 1); | |
1275 } | |
1276 if (!IsEmbedded()) { | |
1277 LoadSubstFont(); | |
1278 } | |
1279 if (1) { | |
1280 if (m_pFontFile || (GetSubstFont()->m_SubstFlags & FXFONT_SUBST_EXACT)) { | |
1281 CPDF_Object* pmap = pCIDFontDict->GetElementValue("CIDToGIDMap"); | |
1282 if (pmap) { | |
1283 if (CPDF_Stream* pStream = pmap->AsStream()) { | |
1284 m_pCIDToGIDMap = new CPDF_StreamAcc; | |
1285 m_pCIDToGIDMap->LoadAllData(pStream, FALSE); | |
1286 } else if (pmap->GetString() == "Identity") { | |
1287 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
1288 if (m_pFontFile) { | |
1289 m_bCIDIsGID = TRUE; | |
1290 } | |
1291 #else | |
1292 m_bCIDIsGID = TRUE; | |
1293 #endif | |
1294 } | |
1295 } | |
1296 } | |
1297 } | |
1298 CheckFontMetrics(); | |
1299 if (IsVertWriting()) { | |
1300 pWidthArray = pCIDFontDict->GetArrayBy("W2"); | |
1301 if (pWidthArray) { | |
1302 LoadMetricsArray(pWidthArray, m_VertMetrics, 3); | |
1303 } | |
1304 CPDF_Array* pDefaultArray = pCIDFontDict->GetArrayBy("DW2"); | |
1305 if (pDefaultArray) { | |
1306 m_DefaultVY = pDefaultArray->GetIntegerAt(0); | |
1307 m_DefaultW1 = pDefaultArray->GetIntegerAt(1); | |
1308 } else { | |
1309 m_DefaultVY = 880; | |
1310 m_DefaultW1 = -1000; | |
1311 } | |
1312 } | |
1313 return TRUE; | |
1314 } | |
1315 | |
1316 FX_RECT CPDF_CIDFont::GetCharBBox(FX_DWORD charcode, int level) { | |
1317 if (charcode < 256 && m_CharBBox[charcode].right != FX_SMALL_RECT::kInvalid) | |
1318 return FX_RECT(m_CharBBox[charcode]); | |
1319 | |
1320 FX_RECT rect; | |
1321 FX_BOOL bVert = FALSE; | |
1322 int glyph_index = GlyphFromCharCode(charcode, &bVert); | |
1323 FXFT_Face face = m_Font.GetFace(); | |
1324 if (face) { | |
1325 if (FXFT_Is_Face_Tricky(face)) { | |
1326 int err = FXFT_Load_Glyph(face, glyph_index, | |
1327 FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); | |
1328 if (!err) { | |
1329 FXFT_BBox cbox; | |
1330 FXFT_Glyph glyph; | |
1331 err = FXFT_Get_Glyph(((FXFT_Face)face)->glyph, &glyph); | |
1332 if (!err) { | |
1333 FXFT_Glyph_Get_CBox(glyph, FXFT_GLYPH_BBOX_PIXELS, &cbox); | |
1334 int pixel_size_x = ((FXFT_Face)face)->size->metrics.x_ppem; | |
1335 int pixel_size_y = ((FXFT_Face)face)->size->metrics.y_ppem; | |
1336 if (pixel_size_x == 0 || pixel_size_y == 0) { | |
1337 rect = FX_RECT(cbox.xMin, cbox.yMax, cbox.xMax, cbox.yMin); | |
1338 } else { | |
1339 rect = FX_RECT(cbox.xMin * 1000 / pixel_size_x, | |
1340 cbox.yMax * 1000 / pixel_size_y, | |
1341 cbox.xMax * 1000 / pixel_size_x, | |
1342 cbox.yMin * 1000 / pixel_size_y); | |
1343 } | |
1344 if (rect.top > FXFT_Get_Face_Ascender(face)) { | |
1345 rect.top = FXFT_Get_Face_Ascender(face); | |
1346 } | |
1347 if (rect.bottom < FXFT_Get_Face_Descender(face)) { | |
1348 rect.bottom = FXFT_Get_Face_Descender(face); | |
1349 } | |
1350 FXFT_Done_Glyph(glyph); | |
1351 } | |
1352 } | |
1353 } else { | |
1354 int err = FXFT_Load_Glyph(face, glyph_index, FXFT_LOAD_NO_SCALE); | |
1355 if (err == 0) { | |
1356 rect = FX_RECT(TT2PDF(FXFT_Get_Glyph_HoriBearingX(face), face), | |
1357 TT2PDF(FXFT_Get_Glyph_HoriBearingY(face), face), | |
1358 TT2PDF(FXFT_Get_Glyph_HoriBearingX(face) + | |
1359 FXFT_Get_Glyph_Width(face), | |
1360 face), | |
1361 TT2PDF(FXFT_Get_Glyph_HoriBearingY(face) - | |
1362 FXFT_Get_Glyph_Height(face), | |
1363 face)); | |
1364 rect.top += rect.top / 64; | |
1365 } | |
1366 } | |
1367 } | |
1368 if (!m_pFontFile && m_Charset == CIDSET_JAPAN1) { | |
1369 FX_WORD CID = CIDFromCharCode(charcode); | |
1370 const uint8_t* pTransform = GetCIDTransform(CID); | |
1371 if (pTransform && !bVert) { | |
1372 CFX_Matrix matrix(CIDTransformToFloat(pTransform[0]), | |
1373 CIDTransformToFloat(pTransform[1]), | |
1374 CIDTransformToFloat(pTransform[2]), | |
1375 CIDTransformToFloat(pTransform[3]), | |
1376 CIDTransformToFloat(pTransform[4]) * 1000, | |
1377 CIDTransformToFloat(pTransform[5]) * 1000); | |
1378 CFX_FloatRect rect_f(rect); | |
1379 rect_f.Transform(&matrix); | |
1380 rect = rect_f.GetOutterRect(); | |
1381 } | |
1382 } | |
1383 if (charcode < 256) | |
1384 m_CharBBox[charcode] = rect.ToSmallRect(); | |
1385 | |
1386 return rect; | |
1387 } | |
1388 int CPDF_CIDFont::GetCharWidthF(FX_DWORD charcode, int level) { | |
1389 if (m_pAnsiWidths && charcode < 0x80) { | |
1390 return m_pAnsiWidths[charcode]; | |
1391 } | |
1392 FX_WORD cid = CIDFromCharCode(charcode); | |
1393 int size = m_WidthList.GetSize(); | |
1394 FX_DWORD* list = m_WidthList.GetData(); | |
1395 for (int i = 0; i < size; i += 3) { | |
1396 if (cid >= list[i] && cid <= list[i + 1]) { | |
1397 return (int)list[i + 2]; | |
1398 } | |
1399 } | |
1400 return m_DefaultWidth; | |
1401 } | |
1402 short CPDF_CIDFont::GetVertWidth(FX_WORD CID) const { | |
1403 FX_DWORD vertsize = m_VertMetrics.GetSize() / 5; | |
1404 if (vertsize == 0) { | |
1405 return m_DefaultW1; | |
1406 } | |
1407 const FX_DWORD* pTable = m_VertMetrics.GetData(); | |
1408 for (FX_DWORD i = 0; i < vertsize; i++) | |
1409 if (pTable[i * 5] <= CID && pTable[i * 5 + 1] >= CID) { | |
1410 return (short)(int)pTable[i * 5 + 2]; | |
1411 } | |
1412 return m_DefaultW1; | |
1413 } | |
1414 void CPDF_CIDFont::GetVertOrigin(FX_WORD CID, short& vx, short& vy) const { | |
1415 FX_DWORD vertsize = m_VertMetrics.GetSize() / 5; | |
1416 if (vertsize) { | |
1417 const FX_DWORD* pTable = m_VertMetrics.GetData(); | |
1418 for (FX_DWORD i = 0; i < vertsize; i++) | |
1419 if (pTable[i * 5] <= CID && pTable[i * 5 + 1] >= CID) { | |
1420 vx = (short)(int)pTable[i * 5 + 3]; | |
1421 vy = (short)(int)pTable[i * 5 + 4]; | |
1422 return; | |
1423 } | |
1424 } | |
1425 FX_DWORD dwWidth = m_DefaultWidth; | |
1426 int size = m_WidthList.GetSize(); | |
1427 const FX_DWORD* list = m_WidthList.GetData(); | |
1428 for (int i = 0; i < size; i += 3) { | |
1429 if (CID >= list[i] && CID <= list[i + 1]) { | |
1430 dwWidth = (FX_WORD)list[i + 2]; | |
1431 break; | |
1432 } | |
1433 } | |
1434 vx = (short)dwWidth / 2; | |
1435 vy = (short)m_DefaultVY; | |
1436 } | |
1437 int CPDF_CIDFont::GetGlyphIndex(FX_DWORD unicode, FX_BOOL* pVertGlyph) { | |
1438 if (pVertGlyph) { | |
1439 *pVertGlyph = FALSE; | |
1440 } | |
1441 FXFT_Face face = m_Font.GetFace(); | |
1442 int index = FXFT_Get_Char_Index(face, unicode); | |
1443 if (unicode == 0x2502) { | |
1444 return index; | |
1445 } | |
1446 if (index && IsVertWriting()) { | |
1447 if (m_pTTGSUBTable) { | |
1448 uint32_t vindex = 0; | |
1449 m_pTTGSUBTable->GetVerticalGlyph(index, &vindex); | |
1450 if (vindex) { | |
1451 index = vindex; | |
1452 if (pVertGlyph) { | |
1453 *pVertGlyph = TRUE; | |
1454 } | |
1455 } | |
1456 return index; | |
1457 } | |
1458 if (!m_Font.GetSubData()) { | |
1459 unsigned long length = 0; | |
1460 int error = FXFT_Load_Sfnt_Table(face, FT_MAKE_TAG('G', 'S', 'U', 'B'), 0, | |
1461 NULL, &length); | |
1462 if (!error) { | |
1463 m_Font.SetSubData(FX_Alloc(uint8_t, length)); | |
1464 } | |
1465 } | |
1466 int error = FXFT_Load_Sfnt_Table(face, FT_MAKE_TAG('G', 'S', 'U', 'B'), 0, | |
1467 m_Font.GetSubData(), NULL); | |
1468 if (!error && m_Font.GetSubData()) { | |
1469 m_pTTGSUBTable = new CFX_CTTGSUBTable; | |
1470 m_pTTGSUBTable->LoadGSUBTable((FT_Bytes)m_Font.GetSubData()); | |
1471 uint32_t vindex = 0; | |
1472 m_pTTGSUBTable->GetVerticalGlyph(index, &vindex); | |
1473 if (vindex) { | |
1474 index = vindex; | |
1475 if (pVertGlyph) { | |
1476 *pVertGlyph = TRUE; | |
1477 } | |
1478 } | |
1479 } | |
1480 return index; | |
1481 } | |
1482 if (pVertGlyph) { | |
1483 *pVertGlyph = FALSE; | |
1484 } | |
1485 return index; | |
1486 } | |
1487 int CPDF_CIDFont::GlyphFromCharCode(FX_DWORD charcode, FX_BOOL* pVertGlyph) { | |
1488 if (pVertGlyph) { | |
1489 *pVertGlyph = FALSE; | |
1490 } | |
1491 if (!m_pFontFile && !m_pCIDToGIDMap) { | |
1492 FX_WORD cid = CIDFromCharCode(charcode); | |
1493 FX_WCHAR unicode = 0; | |
1494 if (m_bCIDIsGID) { | |
1495 #if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_ | |
1496 return cid; | |
1497 #else | |
1498 if (m_Flags & PDFFONT_SYMBOLIC) { | |
1499 return cid; | |
1500 } | |
1501 CFX_WideString uni_str = UnicodeFromCharCode(charcode); | |
1502 if (uni_str.IsEmpty()) { | |
1503 return cid; | |
1504 } | |
1505 unicode = uni_str.GetAt(0); | |
1506 #endif | |
1507 } else { | |
1508 if (cid && m_pCID2UnicodeMap && m_pCID2UnicodeMap->IsLoaded()) { | |
1509 unicode = m_pCID2UnicodeMap->UnicodeFromCID(cid); | |
1510 } | |
1511 if (unicode == 0) { | |
1512 unicode = GetUnicodeFromCharCode(charcode); | |
1513 } | |
1514 if (unicode == 0 && !(m_Flags & PDFFONT_SYMBOLIC)) { | |
1515 unicode = UnicodeFromCharCode(charcode).GetAt(0); | |
1516 } | |
1517 } | |
1518 FXFT_Face face = m_Font.GetFace(); | |
1519 if (unicode == 0) { | |
1520 if (!m_bAdobeCourierStd) { | |
1521 return charcode == 0 ? -1 : (int)charcode; | |
1522 } | |
1523 charcode += 31; | |
1524 int index = 0, iBaseEncoding; | |
1525 FX_BOOL bMSUnicode = FT_UseTTCharmap(face, 3, 1); | |
1526 FX_BOOL bMacRoman = FALSE; | |
1527 if (!bMSUnicode) { | |
1528 bMacRoman = FT_UseTTCharmap(face, 1, 0); | |
1529 } | |
1530 iBaseEncoding = PDFFONT_ENCODING_STANDARD; | |
1531 if (bMSUnicode) { | |
1532 iBaseEncoding = PDFFONT_ENCODING_WINANSI; | |
1533 } else if (bMacRoman) { | |
1534 iBaseEncoding = PDFFONT_ENCODING_MACROMAN; | |
1535 } | |
1536 const FX_CHAR* name = GetAdobeCharName(iBaseEncoding, NULL, charcode); | |
1537 if (!name) { | |
1538 return charcode == 0 ? -1 : (int)charcode; | |
1539 } | |
1540 FX_WORD unicode = PDF_UnicodeFromAdobeName(name); | |
1541 if (unicode) { | |
1542 if (bMSUnicode) { | |
1543 index = FXFT_Get_Char_Index(face, unicode); | |
1544 } else if (bMacRoman) { | |
1545 FX_DWORD maccode = | |
1546 FT_CharCodeFromUnicode(FXFT_ENCODING_APPLE_ROMAN, unicode); | |
1547 index = !maccode ? FXFT_Get_Name_Index(face, (char*)name) | |
1548 : FXFT_Get_Char_Index(face, maccode); | |
1549 } else { | |
1550 return FXFT_Get_Char_Index(face, unicode); | |
1551 } | |
1552 } else { | |
1553 return charcode == 0 ? -1 : (int)charcode; | |
1554 } | |
1555 if (index == 0 || index == 0xffff) { | |
1556 return charcode == 0 ? -1 : (int)charcode; | |
1557 } | |
1558 return index; | |
1559 } | |
1560 if (m_Charset == CIDSET_JAPAN1) { | |
1561 if (unicode == '\\') { | |
1562 unicode = '/'; | |
1563 #if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_ | |
1564 } else if (unicode == 0xa5) { | |
1565 unicode = 0x5c; | |
1566 #endif | |
1567 } | |
1568 } | |
1569 if (!face) | |
1570 return unicode; | |
1571 | |
1572 int err = FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE); | |
1573 if (err != 0) { | |
1574 int i; | |
1575 for (i = 0; i < FXFT_Get_Face_CharmapCount(face); i++) { | |
1576 FX_DWORD ret = FT_CharCodeFromUnicode( | |
1577 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]), | |
1578 (FX_WCHAR)charcode); | |
1579 if (ret == 0) { | |
1580 continue; | |
1581 } | |
1582 FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[i]); | |
1583 unicode = (FX_WCHAR)ret; | |
1584 break; | |
1585 } | |
1586 if (i == FXFT_Get_Face_CharmapCount(face) && i) { | |
1587 FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[0]); | |
1588 unicode = (FX_WCHAR)charcode; | |
1589 } | |
1590 } | |
1591 if (FXFT_Get_Face_Charmap(face)) { | |
1592 int index = GetGlyphIndex(unicode, pVertGlyph); | |
1593 if (index == 0) | |
1594 return -1; | |
1595 return index; | |
1596 } | |
1597 return unicode; | |
1598 } | |
1599 if (!m_Font.GetFace()) | |
1600 return -1; | |
1601 | |
1602 FX_WORD cid = CIDFromCharCode(charcode); | |
1603 if (m_bType1) { | |
1604 if (!m_pCIDToGIDMap) { | |
1605 return cid; | |
1606 } | |
1607 } else { | |
1608 if (!m_pCIDToGIDMap) { | |
1609 if (m_pFontFile && !m_pCMap->m_pMapping) | |
1610 return cid; | |
1611 if (m_pCMap->m_Coding == CIDCODING_UNKNOWN || | |
1612 !FXFT_Get_Face_Charmap(m_Font.GetFace())) { | |
1613 return cid; | |
1614 } | |
1615 if (FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmap(m_Font.GetFace())) == | |
1616 FXFT_ENCODING_UNICODE) { | |
1617 CFX_WideString unicode_str = UnicodeFromCharCode(charcode); | |
1618 if (unicode_str.IsEmpty()) { | |
1619 return -1; | |
1620 } | |
1621 charcode = unicode_str.GetAt(0); | |
1622 } | |
1623 return GetGlyphIndex(charcode, pVertGlyph); | |
1624 } | |
1625 } | |
1626 FX_DWORD byte_pos = cid * 2; | |
1627 if (byte_pos + 2 > m_pCIDToGIDMap->GetSize()) | |
1628 return -1; | |
1629 | |
1630 const uint8_t* pdata = m_pCIDToGIDMap->GetData() + byte_pos; | |
1631 return pdata[0] * 256 + pdata[1]; | |
1632 } | |
1633 FX_DWORD CPDF_CIDFont::GetNextChar(const FX_CHAR* pString, | |
1634 int nStrLen, | |
1635 int& offset) const { | |
1636 return m_pCMap->GetNextChar(pString, nStrLen, offset); | |
1637 } | |
1638 int CPDF_CIDFont::GetCharSize(FX_DWORD charcode) const { | |
1639 return m_pCMap->GetCharSize(charcode); | |
1640 } | |
1641 int CPDF_CIDFont::CountChar(const FX_CHAR* pString, int size) const { | |
1642 return m_pCMap->CountChar(pString, size); | |
1643 } | |
1644 int CPDF_CIDFont::AppendChar(FX_CHAR* str, FX_DWORD charcode) const { | |
1645 return m_pCMap->AppendChar(str, charcode); | |
1646 } | |
1647 FX_BOOL CPDF_CIDFont::IsUnicodeCompatible() const { | |
1648 if (!m_pCMap->IsLoaded() || !m_pCID2UnicodeMap || | |
1649 !m_pCID2UnicodeMap->IsLoaded()) { | |
1650 return m_pCMap->m_Coding != CIDCODING_UNKNOWN; | |
1651 } | |
1652 return TRUE; | |
1653 } | |
1654 FX_BOOL CPDF_CIDFont::IsFontStyleFromCharCode(FX_DWORD charcode) const { | |
1655 return TRUE; | |
1656 } | |
1657 void CPDF_CIDFont::LoadSubstFont() { | |
1658 m_Font.LoadSubst(m_BaseFont, !m_bType1, m_Flags, m_StemV * 5, m_ItalicAngle, | |
1659 g_CharsetCPs[m_Charset], IsVertWriting()); | |
1660 } | |
1661 void CPDF_CIDFont::LoadMetricsArray(CPDF_Array* pArray, | |
1662 CFX_DWordArray& result, | |
1663 int nElements) { | |
1664 int width_status = 0; | |
1665 int iCurElement = 0; | |
1666 int first_code = 0, last_code; | |
1667 FX_DWORD count = pArray->GetCount(); | |
1668 for (FX_DWORD i = 0; i < count; i++) { | |
1669 CPDF_Object* pObj = pArray->GetElementValue(i); | |
1670 if (!pObj) | |
1671 continue; | |
1672 | |
1673 if (CPDF_Array* pArray = pObj->AsArray()) { | |
1674 if (width_status != 1) | |
1675 return; | |
1676 | |
1677 FX_DWORD count = pArray->GetCount(); | |
1678 for (FX_DWORD j = 0; j < count; j += nElements) { | |
1679 result.Add(first_code); | |
1680 result.Add(first_code); | |
1681 for (int k = 0; k < nElements; k++) { | |
1682 result.Add(pArray->GetIntegerAt(j + k)); | |
1683 } | |
1684 first_code++; | |
1685 } | |
1686 width_status = 0; | |
1687 } else { | |
1688 if (width_status == 0) { | |
1689 first_code = pObj->GetInteger(); | |
1690 width_status = 1; | |
1691 } else if (width_status == 1) { | |
1692 last_code = pObj->GetInteger(); | |
1693 width_status = 2; | |
1694 iCurElement = 0; | |
1695 } else { | |
1696 if (!iCurElement) { | |
1697 result.Add(first_code); | |
1698 result.Add(last_code); | |
1699 } | |
1700 result.Add(pObj->GetInteger()); | |
1701 iCurElement++; | |
1702 if (iCurElement == nElements) { | |
1703 width_status = 0; | |
1704 } | |
1705 } | |
1706 } | |
1707 } | |
1708 } | |
1709 | |
1710 // static | |
1711 FX_FLOAT CPDF_CIDFont::CIDTransformToFloat(uint8_t ch) { | |
1712 if (ch < 128) { | |
1713 return ch * 1.0f / 127; | |
1714 } | |
1715 return (-255 + ch) * 1.0f / 127; | |
1716 } | |
1717 | |
1718 FX_BOOL CPDF_CIDFont::LoadGB2312() { | |
1719 m_BaseFont = m_pFontDict->GetStringBy("BaseFont"); | |
1720 CPDF_Dictionary* pFontDesc = m_pFontDict->GetDictBy("FontDescriptor"); | |
1721 if (pFontDesc) { | |
1722 LoadFontDescriptor(pFontDesc); | |
1723 } | |
1724 m_Charset = CIDSET_GB1; | |
1725 m_bType1 = FALSE; | |
1726 m_pCMap = CPDF_ModuleMgr::Get() | |
1727 ->GetPageModule() | |
1728 ->GetFontGlobals() | |
1729 ->m_CMapManager.GetPredefinedCMap("GBK-EUC-H", FALSE); | |
1730 m_pCID2UnicodeMap = CPDF_ModuleMgr::Get() | |
1731 ->GetPageModule() | |
1732 ->GetFontGlobals() | |
1733 ->m_CMapManager.GetCID2UnicodeMap(m_Charset, FALSE); | |
1734 if (!IsEmbedded()) { | |
1735 LoadSubstFont(); | |
1736 } | |
1737 CheckFontMetrics(); | |
1738 m_DefaultWidth = 1000; | |
1739 m_pAnsiWidths = FX_Alloc(FX_WORD, 128); | |
1740 for (int i = 32; i < 127; i++) { | |
1741 m_pAnsiWidths[i] = 500; | |
1742 } | |
1743 return TRUE; | |
1744 } | |
1745 | |
1746 const uint8_t* CPDF_CIDFont::GetCIDTransform(FX_WORD CID) const { | |
1747 if (m_Charset != CIDSET_JAPAN1 || m_pFontFile) | |
1748 return nullptr; | |
1749 | |
1750 const struct CIDTransform* found = (const struct CIDTransform*)FXSYS_bsearch( | |
1751 &CID, g_Japan1_VertCIDs, FX_ArraySize(g_Japan1_VertCIDs), | |
1752 sizeof(g_Japan1_VertCIDs[0]), CompareCIDTransform); | |
1753 return found ? &found->a : nullptr; | |
1754 } | |
OLD | NEW |