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