1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../../include/fpdfapi/fpdf_module.h"
8 #include "../../../include/fpdfapi/fpdf_page.h"
10 #include "../fpdf_cmaps/cmap_int.h"
11 #include "../../../include/fxge/fx_ge.h"
12 #include "../../../include/fxge/fx_freetype.h"
13 extern FX_DWORD FT_CharCodeFromUnicode(int encoding, FX_WCHAR unicode);
14 extern short TT2PDF(int m, FXFT_Face face);
15 extern FX_BOOL FT_UseTTCharmap(FXFT_Face face, int platform_id, int encoding_id);
16 extern const FX_CHAR* GetAdobeCharName(int iBaseEncoding, const CFX_ByteString* pCharNames, int charcode);
17 CPDF_CMapManager::CPDF_CMapManager()
20 FXSYS_memset(m_CID2UnicodeMaps, 0, sizeof m_CID2UnicodeMaps);
22 CPDF_CMapManager::~CPDF_CMapManager()
26 CPDF_CMap* CPDF_CMapManager::GetPredefinedCMap(const CFX_ByteString& name, FX_BOOL bPromptCJK)
29 if (m_CMaps.Lookup(name, (void*&)pCMap)) {
32 pCMap = LoadPredefinedCMap(name, bPromptCJK);
36 m_CMaps.SetAt(name, pCMap);
39 CPDF_CMap* CPDF_CMapManager::LoadPredefinedCMap(const CFX_ByteString& name, FX_BOOL bPromptCJK)
41 CPDF_CMap* pCMap = new CPDF_CMap;
42 const FX_CHAR* pname = name;
46 pCMap->LoadPredefined(this, pname, bPromptCJK);
49 static const FX_CHAR* const g_CharsetNames[NUMBER_OF_CIDSETS] = {NULL, "GB1", "CNS1", "Japan1", "Korea1", "UCS" };
50 static const int g_CharsetCPs[NUMBER_OF_CIDSETS] = {0, 936, 950, 932, 949, 1200 };
51 int _CharsetFromOrdering(const CFX_ByteString& Ordering)
53 for (int charset = 1; charset < NUMBER_OF_CIDSETS; charset++) {
54 if (Ordering == CFX_ByteStringC(g_CharsetNames[charset]))
57 return CIDSET_UNKNOWN;
59 void CPDF_CMapManager::ReloadAll()
63 void CPDF_CMapManager::DropAll(FX_BOOL bReload)
65 FX_POSITION pos = m_CMaps.GetStartPosition();
69 m_CMaps.GetNextAssoc(pos, name, (void*&)pCMap);
74 pCMap->LoadPredefined(this, name, FALSE);
79 for (int i = 0; i < sizeof m_CID2UnicodeMaps / sizeof(CPDF_CID2UnicodeMap*); i ++) {
80 CPDF_CID2UnicodeMap* pMap = m_CID2UnicodeMaps[i];
85 pMap->Load(this, i, FALSE);
91 CPDF_CID2UnicodeMap* CPDF_CMapManager::GetCID2UnicodeMap(int charset, FX_BOOL bPromptCJK)
93 if (m_CID2UnicodeMaps[charset] == NULL) {
94 m_CID2UnicodeMaps[charset] = LoadCID2UnicodeMap(charset, bPromptCJK);
96 return m_CID2UnicodeMaps[charset];
98 CPDF_CID2UnicodeMap* CPDF_CMapManager::LoadCID2UnicodeMap(int charset, FX_BOOL bPromptCJK)
100 CPDF_CID2UnicodeMap* pMap = new CPDF_CID2UnicodeMap();
101 if (!pMap->Initialize()) {
105 pMap->Load(this, charset, bPromptCJK);
108 CPDF_CMapParser::CPDF_CMapParser()
114 FX_BOOL CPDF_CMapParser::Initialize(CPDF_CMap* pCMap)
119 m_AddMaps.EstimateSize(0, 10240);
122 static FX_DWORD CMap_GetCode(const CFX_ByteStringC& word)
125 if (word.GetAt(0) == '<') {
126 for (int i = 1; i < word.GetLength(); i ++) {
127 uint8_t digit = word.GetAt(i);
128 if (digit >= '0' && digit <= '9') {
130 } else if (digit >= 'a' && digit <= 'f') {
131 digit = digit - 'a' + 10;
132 } else if (digit >= 'A' && digit <= 'F') {
133 digit = digit - 'A' + 10;
137 num = num * 16 + digit;
140 for (int i = 0; i < word.GetLength(); i ++) {
141 if (word.GetAt(i) < '0' || word.GetAt(i) > '9') {
144 num = num * 10 + word.GetAt(i) - '0';
149 static FX_BOOL _CMap_GetCodeRange(_CMap_CodeRange& range, const CFX_ByteStringC& first, const CFX_ByteStringC& second)
151 if (first.GetLength() == 0 || first.GetAt(0) != '<') {
155 for (i = 1; i < first.GetLength(); i ++)
156 if (first.GetAt(i) == '>') {
159 range.m_CharSize = (i - 1) / 2;
160 if (range.m_CharSize > 4) {
163 for (i = 0; i < range.m_CharSize; i ++) {
164 uint8_t digit1 = first.GetAt(i * 2 + 1);
165 uint8_t digit2 = first.GetAt(i * 2 + 2);
166 uint8_t byte = (digit1 >= '0' && digit1 <= '9') ? (digit1 - '0') : ((digit1 & 0xdf) - 'A' + 10);
167 byte = byte * 16 + ((digit2 >= '0' && digit2 <= '9') ? (digit2 - '0') : ((digit2 & 0xdf) - 'A' + 10));
168 range.m_Lower[i] = byte;
170 FX_DWORD size = second.GetLength();
171 for (i = 0; i < range.m_CharSize; i ++) {
172 uint8_t digit1 = ((FX_DWORD)i * 2 + 1 < size) ? second.GetAt((FX_STRSIZE)i * 2 + 1) : 0;
173 uint8_t digit2 = ((FX_DWORD)i * 2 + 2 < size) ? second.GetAt((FX_STRSIZE)i * 2 + 2) : 0;
174 uint8_t byte = (digit1 >= '0' && digit1 <= '9') ? (digit1 - '0') : ((digit1 & 0xdf) - 'A' + 10);
175 byte = byte * 16 + ((digit2 >= '0' && digit2 <= '9') ? (digit2 - '0') : ((digit2 & 0xdf) - 'A' + 10));
176 range.m_Upper[i] = byte;
180 static CFX_ByteString CMap_GetString(const CFX_ByteStringC& word)
182 return word.Mid(1, word.GetLength() - 2);
184 void CPDF_CMapParser::ParseWord(const CFX_ByteStringC& word)
186 if (word.IsEmpty()) {
189 if (word == FX_BSTRC("begincidchar")) {
192 } else if (word == FX_BSTRC("begincidrange")) {
195 } else if (word == FX_BSTRC("endcidrange") || word == FX_BSTRC("endcidchar")) {
197 } else if (word == FX_BSTRC("/WMode")) {
199 } else if (word == FX_BSTRC("/Registry")) {
201 } else if (word == FX_BSTRC("/Ordering")) {
203 } else if (word == FX_BSTRC("/Supplement")) {
205 } else if (word == FX_BSTRC("begincodespacerange")) {
208 } else if (word == FX_BSTRC("usecmap")) {
209 } else if (m_Status == 1 || m_Status == 2) {
210 m_CodePoints[m_CodeSeq] = CMap_GetCode(word);
212 FX_DWORD StartCode, EndCode;
218 EndCode = StartCode = m_CodePoints[0];
219 StartCID = (FX_WORD)m_CodePoints[1];
224 StartCode = m_CodePoints[0];
225 EndCode = m_CodePoints[1];
226 StartCID = (FX_WORD)m_CodePoints[2];
228 if (EndCode < 0x10000) {
229 for (FX_DWORD code = StartCode; code <= EndCode; code ++) {
230 m_pCMap->m_pMapping[code] = (FX_WORD)(StartCID + code - StartCode);
235 buf[1] = ((EndCode - StartCode) << 16) + StartCID;
236 m_AddMaps.AppendBlock(buf, sizeof buf);
239 } else if (m_Status == 3) {
240 CMap_GetString(word);
242 } else if (m_Status == 4) {
243 m_pCMap->m_Charset = _CharsetFromOrdering(CMap_GetString(word));
245 } else if (m_Status == 5) {
248 } else if (m_Status == 6) {
249 m_pCMap->m_bVertical = CMap_GetCode(word);
251 } else if (m_Status == 7) {
252 if (word == FX_BSTRC("endcodespacerange")) {
253 int nSegs = m_CodeRanges.GetSize();
255 m_pCMap->m_CodingScheme = CPDF_CMap::MixedFourBytes;
256 m_pCMap->m_nCodeRanges = nSegs;
257 m_pCMap->m_pLeadingBytes = FX_Alloc2D(uint8_t, nSegs, sizeof(_CMap_CodeRange));
258 FXSYS_memcpy(m_pCMap->m_pLeadingBytes, m_CodeRanges.GetData(), nSegs * sizeof(_CMap_CodeRange));
259 } else if (nSegs == 1) {
260 m_pCMap->m_CodingScheme = (m_CodeRanges[0].m_CharSize == 2) ? CPDF_CMap::TwoBytes : CPDF_CMap::OneByte;
264 if (word.GetLength() == 0 || word.GetAt(0) != '<') {
268 _CMap_CodeRange range;
269 if (_CMap_GetCodeRange(range, m_LastWord, word)) {
270 m_CodeRanges.Add(range);
278 CPDF_CMap::CPDF_CMap()
280 m_Charset = CIDSET_UNKNOWN;
281 m_Coding = CIDCODING_UNKNOWN;
282 m_CodingScheme = TwoBytes;
286 m_pLeadingBytes = NULL;
287 m_pAddMapping = NULL;
292 CPDF_CMap::~CPDF_CMap()
298 FX_Free(m_pAddMapping);
300 if (m_pLeadingBytes) {
301 FX_Free(m_pLeadingBytes);
307 void CPDF_CMap::Release()
309 if (m_PredefinedCMap.IsEmpty()) {
313 const CPDF_PredefinedCMap g_PredefinedCMaps[] = {
314 { "GB-EUC", CIDSET_GB1, CIDCODING_GB, CPDF_CMap::MixedTwoBytes, 1, {0xa1, 0xfe} },
315 { "GBpc-EUC", CIDSET_GB1, CIDCODING_GB, CPDF_CMap::MixedTwoBytes, 1, {0xa1, 0xfc} },
316 { "GBK-EUC", CIDSET_GB1, CIDCODING_GB, CPDF_CMap::MixedTwoBytes, 1, {0x81, 0xfe} },
317 { "GBKp-EUC", CIDSET_GB1, CIDCODING_GB, CPDF_CMap::MixedTwoBytes, 1, {0x81, 0xfe} },
318 { "GBK2K-EUC", CIDSET_GB1, CIDCODING_GB, CPDF_CMap::MixedTwoBytes, 1, {0x81, 0xfe} },
319 { "GBK2K", CIDSET_GB1, CIDCODING_GB, CPDF_CMap::MixedTwoBytes, 1, {0x81, 0xfe} },
320 { "UniGB-UCS2", CIDSET_GB1, CIDCODING_UCS2, CPDF_CMap::TwoBytes },
321 { "UniGB-UTF16", CIDSET_GB1, CIDCODING_UTF16, CPDF_CMap::TwoBytes },
322 { "B5pc", CIDSET_CNS1, CIDCODING_BIG5, CPDF_CMap::MixedTwoBytes, 1, {0xa1, 0xfc} },
323 { "HKscs-B5", CIDSET_CNS1, CIDCODING_BIG5, CPDF_CMap::MixedTwoBytes, 1, {0x88, 0xfe} },
324 { "ETen-B5", CIDSET_CNS1, CIDCODING_BIG5, CPDF_CMap::MixedTwoBytes, 1, {0xa1, 0xfe} },
325 { "ETenms-B5", CIDSET_CNS1, CIDCODING_BIG5, CPDF_CMap::MixedTwoBytes, 1, {0xa1, 0xfe} },
326 { "UniCNS-UCS2", CIDSET_CNS1, CIDCODING_UCS2, CPDF_CMap::TwoBytes },
327 { "UniCNS-UTF16", CIDSET_CNS1, CIDCODING_UTF16, CPDF_CMap::TwoBytes },
328 { "83pv-RKSJ", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::MixedTwoBytes, 2, {0x81, 0x9f, 0xe0, 0xfc} },
329 { "90ms-RKSJ", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::MixedTwoBytes, 2, {0x81, 0x9f, 0xe0, 0xfc} },
330 { "90msp-RKSJ", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::MixedTwoBytes, 2, {0x81, 0x9f, 0xe0, 0xfc} },
331 { "90pv-RKSJ", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::MixedTwoBytes, 2, {0x81, 0x9f, 0xe0, 0xfc} },
332 { "Add-RKSJ", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::MixedTwoBytes, 2, {0x81, 0x9f, 0xe0, 0xfc} },
333 { "EUC", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::MixedTwoBytes, 2, {0x8e, 0x8e, 0xa1, 0xfe} },
334 { "H", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::TwoBytes, 1, {0x21, 0x7e} },
335 { "V", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::TwoBytes, 1, {0x21, 0x7e} },
336 { "Ext-RKSJ", CIDSET_JAPAN1, CIDCODING_JIS, CPDF_CMap::MixedTwoBytes, 2, {0x81, 0x9f, 0xe0, 0xfc} },
337 { "UniJIS-UCS2", CIDSET_JAPAN1, CIDCODING_UCS2, CPDF_CMap::TwoBytes },
338 { "UniJIS-UCS2-HW", CIDSET_JAPAN1, CIDCODING_UCS2, CPDF_CMap::TwoBytes },
339 { "UniJIS-UTF16", CIDSET_JAPAN1, CIDCODING_UTF16, CPDF_CMap::TwoBytes },
340 { "KSC-EUC", CIDSET_KOREA1, CIDCODING_KOREA, CPDF_CMap::MixedTwoBytes, 1, {0xa1, 0xfe} },
341 { "KSCms-UHC", CIDSET_KOREA1, CIDCODING_KOREA, CPDF_CMap::MixedTwoBytes, 1, {0x81, 0xfe} },
342 { "KSCms-UHC-HW", CIDSET_KOREA1, CIDCODING_KOREA, CPDF_CMap::MixedTwoBytes, 1, {0x81, 0xfe} },
343 { "KSCpc-EUC", CIDSET_KOREA1, CIDCODING_KOREA, CPDF_CMap::MixedTwoBytes, 1, {0xa1, 0xfd} },
344 { "UniKS-UCS2", CIDSET_KOREA1, CIDCODING_UCS2, CPDF_CMap::TwoBytes },
345 { "UniKS-UTF16", CIDSET_KOREA1, CIDCODING_UTF16, CPDF_CMap::TwoBytes },
348 extern void FPDFAPI_FindEmbeddedCMap(const char* name, int charset, int coding, const FXCMAP_CMap*& pMap);
349 extern FX_WORD FPDFAPI_CIDFromCharCode(const FXCMAP_CMap* pMap, FX_DWORD charcode);
350 FX_BOOL CPDF_CMap::LoadPredefined(CPDF_CMapManager* pMgr, const FX_CHAR* pName, FX_BOOL bPromptCJK)
352 m_PredefinedCMap = pName;
353 if (m_PredefinedCMap == FX_BSTRC("Identity-H") || m_PredefinedCMap == FX_BSTRC("Identity-V")) {
354 m_Coding = CIDCODING_CID;
355 m_bVertical = pName[9] == 'V';
359 CFX_ByteString cmapid = m_PredefinedCMap;
360 m_bVertical = cmapid.Right(1) == FX_BSTRC("V");
361 if (cmapid.GetLength() > 2) {
362 cmapid = cmapid.Left(cmapid.GetLength() - 2);
366 if (g_PredefinedCMaps[index].m_pName == NULL) {
369 if (cmapid == CFX_ByteStringC(g_PredefinedCMaps[index].m_pName)) {
374 const CPDF_PredefinedCMap& map = g_PredefinedCMaps[index];
375 m_Charset = map.m_Charset;
376 m_Coding = map.m_Coding;
377 m_CodingScheme = map.m_CodingScheme;
378 if (m_CodingScheme == MixedTwoBytes) {
379 m_pLeadingBytes = FX_Alloc(uint8_t, 256);
380 for (FX_DWORD i = 0; i < map.m_LeadingSegCount; i ++) {
381 for (int b = map.m_LeadingSegs[i * 2]; b <= map.m_LeadingSegs[i * 2 + 1]; b ++) {
382 m_pLeadingBytes[b] = 1;
386 FPDFAPI_FindEmbeddedCMap(pName, m_Charset, m_Coding, m_pEmbedMap);
394 static int compare_dword(const void* data1, const void* data2)
396 return (*(FX_DWORD*)data1) - (*(FX_DWORD*)data2);
399 FX_BOOL CPDF_CMap::LoadEmbedded(const uint8_t* pData, FX_DWORD size)
401 m_pMapping = FX_Alloc(FX_WORD, 65536);
402 CPDF_CMapParser parser;
403 parser.Initialize(this);
404 CPDF_SimpleParser syntax(pData, size);
406 CFX_ByteStringC word = syntax.GetWord();
407 if (word.IsEmpty()) {
410 parser.ParseWord(word);
412 if (m_CodingScheme == MixedFourBytes && parser.m_AddMaps.GetSize()) {
413 m_pAddMapping = FX_Alloc(uint8_t, parser.m_AddMaps.GetSize() + 4);
414 *(FX_DWORD*)m_pAddMapping = parser.m_AddMaps.GetSize() / 8;
415 FXSYS_memcpy(m_pAddMapping + 4, parser.m_AddMaps.GetBuffer(), parser.m_AddMaps.GetSize());
416 FXSYS_qsort(m_pAddMapping + 4, parser.m_AddMaps.GetSize() / 8, 8, compare_dword);
421 static int compareCID(const void* key, const void* element)
423 if ((*(FX_DWORD*)key) < (*(FX_DWORD*)element)) {
426 if ((*(FX_DWORD*)key) > (*(FX_DWORD*)element) + ((FX_DWORD*)element)[1] / 65536) {
432 FX_WORD CPDF_CMap::CIDFromCharCode(FX_DWORD charcode) const
434 if (m_Coding == CIDCODING_CID) {
435 return (FX_WORD)charcode;
438 return FPDFAPI_CIDFromCharCode(m_pEmbedMap, charcode);
440 if (m_pMapping == NULL) {
441 return (FX_WORD)charcode;
443 if (charcode >> 16) {
445 void* found = FXSYS_bsearch(&charcode, m_pAddMapping + 4, *(FX_DWORD*)m_pAddMapping, 8, compareCID);
448 return m_pUseMap->CIDFromCharCode(charcode);
452 return (FX_WORD)(((FX_DWORD*)found)[1] % 65536 + charcode - * (FX_DWORD*)found);
455 return m_pUseMap->CIDFromCharCode(charcode);
459 FX_DWORD CID = m_pMapping[charcode];
460 if (!CID && m_pUseMap) {
461 return m_pUseMap->CIDFromCharCode(charcode);
465 static int _CheckCodeRange(uint8_t* codes, int size, _CMap_CodeRange* pRanges, int nRanges)
467 int iSeg = nRanges - 1;
469 if (pRanges[iSeg].m_CharSize < size) {
474 while (iChar < size) {
475 if (codes[iChar] < pRanges[iSeg].m_Lower[iChar] ||
476 codes[iChar] > pRanges[iSeg].m_Upper[iChar]) {
481 if (iChar == pRanges[iSeg].m_CharSize) {
485 if (size == pRanges[iSeg].m_CharSize) {
494 FX_DWORD CPDF_CMap::GetNextChar(const FX_CHAR* pString, int nStrLen, int& offset) const
496 switch (m_CodingScheme) {
498 return ((uint8_t*)pString)[offset++];
501 return ((uint8_t*)pString)[offset - 2] * 256 + ((uint8_t*)pString)[offset - 1];
502 case MixedTwoBytes: {
503 uint8_t byte1 = ((uint8_t*)pString)[offset++];
504 if (!m_pLeadingBytes[byte1]) {
507 uint8_t byte2 = ((uint8_t*)pString)[offset++];
508 return byte1 * 256 + byte2;
510 case MixedFourBytes: {
513 codes[0] = ((uint8_t*)pString)[offset++];
514 _CMap_CodeRange* pRanges = (_CMap_CodeRange*)m_pLeadingBytes;
516 int ret = _CheckCodeRange(codes, char_size, pRanges, m_nCodeRanges);
521 FX_DWORD charcode = 0;
522 for (int i = 0; i < char_size; i ++) {
523 charcode = (charcode << 8) + codes[i];
527 if (char_size == 4 || offset == nStrLen) {
530 codes[char_size ++] = ((uint8_t*)pString)[offset++];
537 int CPDF_CMap::GetCharSize(FX_DWORD charcode) const
539 switch (m_CodingScheme) {
546 if (charcode < 0x100) {
549 if (charcode < 0x10000) {
552 if (charcode < 0x1000000) {
559 int CPDF_CMap::CountChar(const FX_CHAR* pString, int size) const
561 switch (m_CodingScheme) {
565 return (size + 1) / 2;
566 case MixedTwoBytes: {
568 for (int i = 0; i < size; i ++) {
570 if (m_pLeadingBytes[((uint8_t*)pString)[i]]) {
576 case MixedFourBytes: {
577 int count = 0, offset = 0;
578 while (offset < size) {
579 GetNextChar(pString, size, offset);
587 int _GetCharSize(FX_DWORD charcode, _CMap_CodeRange* pRanges, int iRangesSize)
593 codes[0] = codes[1] = 0x00;
594 codes[2] = (uint8_t)(charcode >> 8 & 0xFF);
595 codes[3] = (uint8_t)charcode;
596 int offset = 0, size = 4;
597 for (int i = 0; i < 4; ++i) {
598 int iSeg = iRangesSize - 1;
600 if (pRanges[iSeg].m_CharSize < size) {
605 while (iChar < size) {
606 if (codes[offset + iChar] < pRanges[iSeg].m_Lower[iChar] ||
607 codes[offset + iChar] > pRanges[iSeg].m_Upper[iChar]) {
612 if (iChar == pRanges[iSeg].m_CharSize) {
622 int CPDF_CMap::AppendChar(FX_CHAR* str, FX_DWORD charcode) const
624 switch (m_CodingScheme) {
626 str[0] = (uint8_t)charcode;
629 str[0] = (uint8_t)(charcode / 256);
630 str[1] = (uint8_t)(charcode % 256);
634 if (charcode < 0x100) {
635 _CMap_CodeRange* pRanges = (_CMap_CodeRange*)m_pLeadingBytes;
636 int iSize = _GetCharSize(charcode, pRanges, m_nCodeRanges);
641 FXSYS_memset(str, 0, sizeof(uint8_t) * iSize);
643 str[iSize - 1] = (uint8_t)charcode;
645 } else if (charcode < 0x10000) {
646 str[0] = (uint8_t)(charcode >> 8);
647 str[1] = (uint8_t)charcode;
649 } else if (charcode < 0x1000000) {
650 str[0] = (uint8_t)(charcode >> 16);
651 str[1] = (uint8_t)(charcode >> 8);
652 str[2] = (uint8_t)charcode;
655 str[0] = (uint8_t)(charcode >> 24);
656 str[1] = (uint8_t)(charcode >> 16);
657 str[2] = (uint8_t)(charcode >> 8);
658 str[3] = (uint8_t)charcode;
664 CPDF_CID2UnicodeMap::CPDF_CID2UnicodeMap()
668 CPDF_CID2UnicodeMap::~CPDF_CID2UnicodeMap()
671 FX_BOOL CPDF_CID2UnicodeMap::Initialize()
675 FX_BOOL CPDF_CID2UnicodeMap::IsLoaded()
677 return m_EmbeddedCount != 0;
679 FX_WCHAR CPDF_CID2UnicodeMap::UnicodeFromCID(FX_WORD CID)
681 if (m_Charset == CIDSET_UNICODE) {
684 if (CID < m_EmbeddedCount) {
685 return m_pEmbeddedMap[CID];
689 void FPDFAPI_LoadCID2UnicodeMap(int charset, const FX_WORD*& pMap, FX_DWORD& count);
690 void CPDF_CID2UnicodeMap::Load(CPDF_CMapManager* pMgr, int charset, FX_BOOL bPromptCJK)
693 FPDFAPI_LoadCID2UnicodeMap(charset, m_pEmbeddedMap, m_EmbeddedCount);
695 #include "ttgsubtable.h"
696 CPDF_CIDFont::CPDF_CIDFont() : CPDF_Font(PDFFONT_CIDFONT)
699 m_pAllocatedCMap = NULL;
700 m_pCID2UnicodeMap = NULL;
701 m_pAnsiWidths = NULL;
702 m_pCIDToGIDMap = NULL;
704 m_bAdobeCourierStd = FALSE;
705 m_pTTGSUBTable = NULL;
706 FXSYS_memset(m_CharBBox, 0xff, 256 * sizeof(FX_SMALL_RECT));
708 CPDF_CIDFont::~CPDF_CIDFont()
711 FX_Free(m_pAnsiWidths);
713 if (m_pAllocatedCMap) {
714 delete m_pAllocatedCMap;
716 if (m_pCIDToGIDMap) {
717 delete m_pCIDToGIDMap;
719 if (m_pTTGSUBTable) {
720 delete m_pTTGSUBTable;
723 FX_WORD CPDF_CIDFont::CIDFromCharCode(FX_DWORD charcode) const
725 if (m_pCMap == NULL) {
726 return (FX_WORD)charcode;
728 return m_pCMap->CIDFromCharCode(charcode);
730 FX_BOOL CPDF_CIDFont::IsVertWriting() const
732 return m_pCMap ? m_pCMap->IsVertWriting() : FALSE;
734 extern FX_DWORD FPDFAPI_CharCodeFromCID(const FXCMAP_CMap* pMap, FX_WORD cid);
735 static FX_DWORD _EmbeddedCharcodeFromUnicode(const FXCMAP_CMap* pEmbedMap, int charset, FX_WCHAR unicode)
737 if (charset <= 0 || charset > 4) {
740 CPDF_FontGlobals* pFontGlobals = CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals();
741 const FX_WORD* pCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_pMap;
742 if (pCodes == NULL) {
745 int nCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_Count;
746 for (int i = 0; i < nCodes; i++) {
747 if (pCodes[i] == unicode) {
748 FX_DWORD CharCode = FPDFAPI_CharCodeFromCID(pEmbedMap, i);
757 static FX_WCHAR _EmbeddedUnicodeFromCharcode(const FXCMAP_CMap* pEmbedMap, int charset, FX_DWORD charcode)
759 if (charset <= 0 || charset > 4) {
762 FX_WORD cid = FPDFAPI_CIDFromCharCode(pEmbedMap, charcode);
766 CPDF_FontGlobals* pFontGlobals = CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals();
767 const FX_WORD* pCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_pMap;
768 if (pCodes == NULL) {
771 if (cid < pFontGlobals->m_EmbeddedToUnicodes[charset].m_Count) {
776 FX_WCHAR CPDF_CIDFont::_UnicodeFromCharCode(FX_DWORD charcode) const
778 switch (m_pCMap->m_Coding) {
780 case CIDCODING_UTF16:
781 return (FX_WCHAR)charcode;
783 if (m_pCID2UnicodeMap == NULL || !m_pCID2UnicodeMap->IsLoaded()) {
786 return m_pCID2UnicodeMap->UnicodeFromCID((FX_WORD)charcode);
788 if (!m_pCMap->IsLoaded() || m_pCID2UnicodeMap == NULL || !m_pCID2UnicodeMap->IsLoaded()) {
789 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
792 if (charcode > 255) {
793 charcode = (charcode % 256) * 256 + (charcode / 256);
796 int ret = FXSYS_MultiByteToWideChar(g_CharsetCPs[m_pCMap->m_Coding], 0, (const FX_CHAR*)&charcode, charsize, &unicode, 1);
802 if (m_pCMap->m_pEmbedMap) {
803 return _EmbeddedUnicodeFromCharcode(m_pCMap->m_pEmbedMap, m_pCMap->m_Charset, charcode);
808 return m_pCID2UnicodeMap->UnicodeFromCID(CIDFromCharCode(charcode));
810 FX_DWORD CPDF_CIDFont::_CharCodeFromUnicode(FX_WCHAR unicode) const
812 switch (m_pCMap->m_Coding) {
813 case CIDCODING_UNKNOWN:
816 case CIDCODING_UTF16:
818 case CIDCODING_CID: {
819 if (m_pCID2UnicodeMap == NULL || !m_pCID2UnicodeMap->IsLoaded()) {
823 while (CID < 65536) {
824 FX_WCHAR this_unicode = m_pCID2UnicodeMap->UnicodeFromCID((FX_WORD)CID);
825 if (this_unicode == unicode) {
834 if (unicode < 0x80) {
835 return static_cast<FX_DWORD>(unicode);
836 } else if (m_pCMap->m_Coding == CIDCODING_CID) {
839 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
841 int ret = FXSYS_WideCharToMultiByte(g_CharsetCPs[m_pCMap->m_Coding], 0, &unicode, 1, (char*)buffer, 4, NULL, NULL);
844 } else if (ret == 2) {
845 return buffer[0] * 256 + buffer[1];
849 if (m_pCMap->m_pEmbedMap) {
850 return _EmbeddedCharcodeFromUnicode(m_pCMap->m_pEmbedMap, m_pCMap->m_Charset, unicode);
854 static void FT_UseCIDCharmap(FXFT_Face face, int coding)
859 encoding = FXFT_ENCODING_GB2312;
862 encoding = FXFT_ENCODING_BIG5;
865 encoding = FXFT_ENCODING_SJIS;
867 case CIDCODING_KOREA:
868 encoding = FXFT_ENCODING_JOHAB;
871 encoding = FXFT_ENCODING_UNICODE;
873 int err = FXFT_Select_Charmap(face, encoding);
875 err = FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE);
877 if (err && FXFT_Get_Face_Charmaps(face)) {
878 FXFT_Set_Charmap(face, *FXFT_Get_Face_Charmaps(face));
881 FX_BOOL CPDF_CIDFont::_Load()
883 if (m_pFontDict->GetString(FX_BSTRC("Subtype")) == FX_BSTRC("TrueType")) {
886 CPDF_Array* pFonts = m_pFontDict->GetArray(FX_BSTRC("DescendantFonts"));
887 if (pFonts == NULL) {
890 if (pFonts->GetCount() != 1) {
893 CPDF_Dictionary* pCIDFontDict = pFonts->GetDict(0);
894 if (pCIDFontDict == NULL) {
897 m_BaseFont = pCIDFontDict->GetString(FX_BSTRC("BaseFont"));
898 if ((m_BaseFont.Compare("CourierStd") == 0 || m_BaseFont.Compare("CourierStd-Bold") == 0
899 || m_BaseFont.Compare("CourierStd-BoldOblique") == 0 || m_BaseFont.Compare("CourierStd-Oblique") == 0)
901 m_bAdobeCourierStd = TRUE;
903 CPDF_Dictionary* pFontDesc = pCIDFontDict->GetDict(FX_BSTRC("FontDescriptor"));
905 LoadFontDescriptor(pFontDesc);
907 CPDF_Object* pEncoding = m_pFontDict->GetElementValue(FX_BSTRC("Encoding"));
908 if (pEncoding == NULL) {
911 CFX_ByteString subtype = pCIDFontDict->GetString(FX_BSTRC("Subtype"));
913 if (subtype == FX_BSTRC("CIDFontType0")) {
916 if (pEncoding->GetType() == PDFOBJ_NAME) {
917 CFX_ByteString cmap = pEncoding->GetString();
918 m_pCMap = CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals()->m_CMapManager.GetPredefinedCMap(cmap,
919 m_pFontFile && m_bType1);
920 } else if (pEncoding->GetType() == PDFOBJ_STREAM) {
921 m_pAllocatedCMap = m_pCMap = new CPDF_CMap;
922 CPDF_Stream* pStream = (CPDF_Stream*)pEncoding;
924 acc.LoadAllData(pStream, FALSE);
925 m_pCMap->LoadEmbedded(acc.GetData(), acc.GetSize());
929 if (m_pCMap == NULL) {
932 m_Charset = m_pCMap->m_Charset;
933 if (m_Charset == CIDSET_UNKNOWN) {
934 CPDF_Dictionary* pCIDInfo = pCIDFontDict->GetDict(FX_BSTRC("CIDSystemInfo"));
936 m_Charset = _CharsetFromOrdering(pCIDInfo->GetString(FX_BSTRC("Ordering")));
939 if (m_Charset != CIDSET_UNKNOWN)
940 m_pCID2UnicodeMap = CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals()->m_CMapManager.GetCID2UnicodeMap(m_Charset,
941 m_pFontFile == NULL && (m_pCMap->m_Coding == CIDCODING_CID || pCIDFontDict->KeyExist(FX_BSTRC("W"))));
942 if (m_Font.GetFace()) {
944 FXFT_Select_Charmap(m_Font.GetFace(), FXFT_ENCODING_UNICODE);
946 FT_UseCIDCharmap(m_Font.GetFace(), m_pCMap->m_Coding);
949 m_DefaultWidth = pCIDFontDict->GetInteger(FX_BSTRC("DW"), 1000);
950 CPDF_Array* pWidthArray = pCIDFontDict->GetArray(FX_BSTRC("W"));
952 LoadMetricsArray(pWidthArray, m_WidthList, 1);
958 if (m_pFontFile || (GetSubstFont()->m_SubstFlags & FXFONT_SUBST_EXACT)) {
959 CPDF_Object* pmap = pCIDFontDict->GetElementValue(FX_BSTRC("CIDToGIDMap"));
961 if (pmap->GetType() == PDFOBJ_STREAM) {
962 m_pCIDToGIDMap = new CPDF_StreamAcc;
963 m_pCIDToGIDMap->LoadAllData((CPDF_Stream*)pmap, FALSE);
964 } else if (pmap->GetString() == FX_BSTRC("Identity")) {
965 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
977 if (IsVertWriting()) {
978 pWidthArray = pCIDFontDict->GetArray(FX_BSTRC("W2"));
980 LoadMetricsArray(pWidthArray, m_VertMetrics, 3);
982 CPDF_Array* pDefaultArray = pCIDFontDict->GetArray(FX_BSTRC("DW2"));
984 m_DefaultVY = pDefaultArray->GetInteger(0);
985 m_DefaultW1 = pDefaultArray->GetInteger(1);
993 FX_FLOAT _CIDTransformToFloat(uint8_t ch)
996 return ch * 1.0f / 127;
998 return (-255 + ch) * 1.0f / 127;
1000 void CPDF_CIDFont::GetCharBBox(FX_DWORD charcode, FX_RECT& rect, int level)
1002 if (charcode < 256 && m_CharBBox[charcode].Right != -1) {
1003 rect.bottom = m_CharBBox[charcode].Bottom;
1004 rect.left = m_CharBBox[charcode].Left;
1005 rect.right = m_CharBBox[charcode].Right;
1006 rect.top = m_CharBBox[charcode].Top;
1009 FX_BOOL bVert = FALSE;
1010 int glyph_index = GlyphFromCharCode(charcode, &bVert);
1011 if (m_Font.m_Face == NULL) {
1012 rect = FX_RECT(0, 0, 0, 0);
1014 rect.left = rect.bottom = rect.right = rect.top = 0;
1015 FXFT_Face face = m_Font.m_Face;
1016 if (FXFT_Is_Face_Tricky(face)) {
1017 int err = FXFT_Load_Glyph(face, glyph_index, FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
1021 err = FXFT_Get_Glyph(((FXFT_Face)face)->glyph, &glyph);
1023 FXFT_Glyph_Get_CBox(glyph, FXFT_GLYPH_BBOX_PIXELS, &cbox);
1024 int pixel_size_x = ((FXFT_Face)face)->size->metrics.x_ppem;
1025 int pixel_size_y = ((FXFT_Face)face)->size->metrics.y_ppem;
1026 if (pixel_size_x == 0 || pixel_size_y == 0) {
1027 rect.left = cbox.xMin;
1028 rect.right = cbox.xMax;
1029 rect.top = cbox.yMax;
1030 rect.bottom = cbox.yMin;
1032 rect.left = cbox.xMin * 1000 / pixel_size_x;
1033 rect.right = cbox.xMax * 1000 / pixel_size_x;
1034 rect.top = cbox.yMax * 1000 / pixel_size_y;
1035 rect.bottom = cbox.yMin * 1000 / pixel_size_y;
1037 if (rect.top > FXFT_Get_Face_Ascender(face)) {
1038 rect.top = FXFT_Get_Face_Ascender(face);
1040 if (rect.bottom < FXFT_Get_Face_Descender(face)) {
1041 rect.bottom = FXFT_Get_Face_Descender(face);
1043 FXFT_Done_Glyph(glyph);
1047 int err = FXFT_Load_Glyph(face, glyph_index, FXFT_LOAD_NO_SCALE);
1049 rect.left = TT2PDF(FXFT_Get_Glyph_HoriBearingX(face), face);
1050 rect.right = TT2PDF(FXFT_Get_Glyph_HoriBearingX(face) + FXFT_Get_Glyph_Width(face), face);
1051 rect.top = TT2PDF(FXFT_Get_Glyph_HoriBearingY(face), face);
1052 rect.top += rect.top / 64;
1053 rect.bottom = TT2PDF(FXFT_Get_Glyph_HoriBearingY(face) - FXFT_Get_Glyph_Height(face), face);
1057 if (m_pFontFile == NULL && m_Charset == CIDSET_JAPAN1) {
1058 FX_WORD CID = CIDFromCharCode(charcode);
1059 const uint8_t* pTransform = GetCIDTransform(CID);
1060 if (pTransform && !bVert) {
1061 CFX_AffineMatrix matrix(_CIDTransformToFloat(pTransform[0]), _CIDTransformToFloat(pTransform[1]),
1062 _CIDTransformToFloat(pTransform[2]), _CIDTransformToFloat(pTransform[3]),
1063 _CIDTransformToFloat(pTransform[4]) * 1000 , _CIDTransformToFloat(pTransform[5]) * 1000);
1064 CFX_FloatRect rect_f(rect);
1065 rect_f.Transform(&matrix);
1066 rect = rect_f.GetOutterRect();
1069 if (charcode < 256) {
1070 m_CharBBox[charcode].Bottom = (short)rect.bottom;
1071 m_CharBBox[charcode].Left = (short)rect.left;
1072 m_CharBBox[charcode].Right = (short)rect.right;
1073 m_CharBBox[charcode].Top = (short)rect.top;
1076 int CPDF_CIDFont::GetCharWidthF(FX_DWORD charcode, int level)
1078 if (m_pAnsiWidths && charcode < 0x80) {
1079 return m_pAnsiWidths[charcode];
1081 FX_WORD cid = CIDFromCharCode(charcode);
1082 int size = m_WidthList.GetSize();
1083 FX_DWORD* list = m_WidthList.GetData();
1084 for (int i = 0; i < size; i += 3) {
1085 if (cid >= list[i] && cid <= list[i + 1]) {
1086 return (int)list[i + 2];
1089 return m_DefaultWidth;
1091 short CPDF_CIDFont::GetVertWidth(FX_WORD CID) const
1093 FX_DWORD vertsize = m_VertMetrics.GetSize() / 5;
1094 if (vertsize == 0) {
1097 const FX_DWORD* pTable = m_VertMetrics.GetData();
1098 for (FX_DWORD i = 0; i < vertsize; i ++)
1099 if (pTable[i * 5] <= CID && pTable[i * 5 + 1] >= CID) {
1100 return (short)(int)pTable[i * 5 + 2];
1104 void CPDF_CIDFont::GetVertOrigin(FX_WORD CID, short& vx, short &vy) const
1106 FX_DWORD vertsize = m_VertMetrics.GetSize() / 5;
1108 const FX_DWORD* pTable = m_VertMetrics.GetData();
1109 for (FX_DWORD i = 0; i < vertsize; i ++)
1110 if (pTable[i * 5] <= CID && pTable[i * 5 + 1] >= CID) {
1111 vx = (short)(int)pTable[i * 5 + 3];
1112 vy = (short)(int)pTable[i * 5 + 4];
1116 FX_DWORD dwWidth = m_DefaultWidth;
1117 int size = m_WidthList.GetSize();
1118 const FX_DWORD* list = m_WidthList.GetData();
1119 for (int i = 0; i < size; i += 3) {
1120 if (CID >= list[i] && CID <= list[i + 1]) {
1121 dwWidth = (FX_WORD)list[i + 2];
1125 vx = (short)dwWidth / 2;
1126 vy = (short)m_DefaultVY;
1128 int CPDF_CIDFont::GetGlyphIndex(FX_DWORD unicode, FX_BOOL *pVertGlyph)
1131 *pVertGlyph = FALSE;
1133 int index = FXFT_Get_Char_Index(m_Font.m_Face, unicode );
1134 if (unicode == 0x2502) {
1137 if (index && IsVertWriting()) {
1138 if (m_pTTGSUBTable) {
1139 TT_uint32_t vindex = 0;
1140 m_pTTGSUBTable->GetVerticalGlyph(index, &vindex);
1149 if (NULL == m_Font.m_pGsubData) {
1150 unsigned long length = 0;
1151 int error = FXFT_Load_Sfnt_Table( m_Font.m_Face, FT_MAKE_TAG('G', 'S', 'U', 'B'), 0, NULL, &length);
1153 m_Font.m_pGsubData = (unsigned char*)FX_Alloc(uint8_t, length);
1156 int error = FXFT_Load_Sfnt_Table( m_Font.m_Face, FT_MAKE_TAG('G', 'S', 'U', 'B'), 0, m_Font.m_pGsubData, NULL);
1157 if (!error && m_Font.m_pGsubData) {
1158 m_pTTGSUBTable = new CFX_CTTGSUBTable;
1159 m_pTTGSUBTable->LoadGSUBTable((FT_Bytes)m_Font.m_pGsubData);
1160 TT_uint32_t vindex = 0;
1161 m_pTTGSUBTable->GetVerticalGlyph(index, &vindex);
1172 *pVertGlyph = FALSE;
1176 int CPDF_CIDFont::GlyphFromCharCode(FX_DWORD charcode, FX_BOOL *pVertGlyph)
1179 *pVertGlyph = FALSE;
1181 if (m_pFontFile == NULL && m_pCIDToGIDMap == NULL) {
1182 FX_WORD cid = CIDFromCharCode(charcode);
1183 FX_WCHAR unicode = 0;
1185 #if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_
1188 if (m_Flags & PDFFONT_SYMBOLIC) {
1191 CFX_WideString uni_str = UnicodeFromCharCode(charcode);
1192 if (uni_str.IsEmpty()) {
1195 unicode = uni_str.GetAt(0);
1198 if (cid && m_pCID2UnicodeMap && m_pCID2UnicodeMap->IsLoaded()) {
1199 unicode = m_pCID2UnicodeMap->UnicodeFromCID(cid);
1202 unicode = _UnicodeFromCharCode(charcode);
1204 if (unicode == 0 && !(m_Flags & PDFFONT_SYMBOLIC)) {
1205 unicode = UnicodeFromCharCode(charcode).GetAt(0);
1209 if (!m_bAdobeCourierStd) {
1210 return charcode == 0 ? -1 : (int)charcode;
1213 int index = 0, iBaseEncoding;
1214 FX_BOOL bMSUnicode = FT_UseTTCharmap(m_Font.m_Face, 3, 1);
1215 FX_BOOL bMacRoman = FALSE;
1217 bMacRoman = FT_UseTTCharmap(m_Font.m_Face, 1, 0);
1219 iBaseEncoding = PDFFONT_ENCODING_STANDARD;
1221 iBaseEncoding = PDFFONT_ENCODING_WINANSI;
1222 } else if (bMacRoman) {
1223 iBaseEncoding = PDFFONT_ENCODING_MACROMAN;
1225 const FX_CHAR* name = GetAdobeCharName(iBaseEncoding, NULL, charcode);
1227 return charcode == 0 ? -1 : (int)charcode;
1229 FX_WORD unicode = PDF_UnicodeFromAdobeName(name);
1232 index = FXFT_Get_Char_Index(m_Font.m_Face, unicode);
1233 } else if (bMacRoman) {
1234 FX_DWORD maccode = FT_CharCodeFromUnicode(FXFT_ENCODING_APPLE_ROMAN, unicode);
1235 index = !maccode ? FXFT_Get_Name_Index(m_Font.m_Face, (char *)name) : FXFT_Get_Char_Index(m_Font.m_Face, maccode);
1237 return FXFT_Get_Char_Index(m_Font.m_Face, unicode);
1240 return charcode == 0 ? -1 : (int)charcode;
1242 if (index == 0 || index == 0xffff) {
1243 return charcode == 0 ? -1 : (int)charcode;
1248 if (m_Charset == CIDSET_JAPAN1) {
1249 if (unicode == '\\') {
1252 #if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_
1253 else if (unicode == 0xa5) {
1258 if (m_Font.m_Face == NULL) {
1261 int err = FXFT_Select_Charmap(m_Font.m_Face, FXFT_ENCODING_UNICODE);
1264 for (i = 0; i < FXFT_Get_Face_CharmapCount(m_Font.m_Face); i ++) {
1265 FX_DWORD ret = FT_CharCodeFromUnicode(FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(m_Font.m_Face)[i]), (FX_WCHAR)charcode);
1269 FXFT_Set_Charmap(m_Font.m_Face, FXFT_Get_Face_Charmaps(m_Font.m_Face)[i]);
1270 unicode = (FX_WCHAR)ret;
1273 if (i == FXFT_Get_Face_CharmapCount(m_Font.m_Face) && i) {
1274 FXFT_Set_Charmap(m_Font.m_Face, FXFT_Get_Face_Charmaps(m_Font.m_Face)[0]);
1275 unicode = (FX_WCHAR)charcode;
1278 if (FXFT_Get_Face_Charmap(m_Font.m_Face)) {
1279 int index = GetGlyphIndex(unicode, pVertGlyph);
1287 if (m_Font.m_Face == NULL) {
1290 FX_WORD cid = CIDFromCharCode(charcode);
1292 if (NULL == m_pCIDToGIDMap) {
1296 if (m_pCIDToGIDMap == NULL) {
1297 if (m_pFontFile && m_pCMap->m_pMapping == NULL) {
1300 if (m_pCMap->m_Coding == CIDCODING_UNKNOWN || FXFT_Get_Face_Charmap(m_Font.m_Face) == NULL) {
1303 if (FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmap(m_Font.m_Face)) == FXFT_ENCODING_UNICODE) {
1304 CFX_WideString unicode_str = UnicodeFromCharCode(charcode);
1305 if (unicode_str.IsEmpty()) {
1308 charcode = unicode_str.GetAt(0);
1310 return GetGlyphIndex(charcode, pVertGlyph);
1313 FX_DWORD byte_pos = cid * 2;
1314 if (byte_pos + 2 > m_pCIDToGIDMap->GetSize()) {
1317 const uint8_t* pdata = m_pCIDToGIDMap->GetData() + byte_pos;
1318 return pdata[0] * 256 + pdata[1];
1320 FX_DWORD CPDF_CIDFont::GetNextChar(const FX_CHAR* pString, int nStrLen, int& offset) const
1322 return m_pCMap->GetNextChar(pString, nStrLen, offset);
1324 int CPDF_CIDFont::GetCharSize(FX_DWORD charcode) const
1326 return m_pCMap->GetCharSize(charcode);
1328 int CPDF_CIDFont::CountChar(const FX_CHAR* pString, int size) const
1330 return m_pCMap->CountChar(pString, size);
1332 int CPDF_CIDFont::AppendChar(FX_CHAR* str, FX_DWORD charcode) const
1334 return m_pCMap->AppendChar(str, charcode);
1336 FX_BOOL CPDF_CIDFont::IsUnicodeCompatible() const
1338 if (!m_pCMap->IsLoaded() || m_pCID2UnicodeMap == NULL || !m_pCID2UnicodeMap->IsLoaded()) {
1339 return m_pCMap->m_Coding != CIDCODING_UNKNOWN;
1343 FX_BOOL CPDF_CIDFont::IsFontStyleFromCharCode(FX_DWORD charcode) const
1347 void CPDF_CIDFont::LoadSubstFont()
1349 m_Font.LoadSubst(m_BaseFont, !m_bType1, m_Flags, m_StemV * 5, m_ItalicAngle, g_CharsetCPs[m_Charset], IsVertWriting());
1351 void CPDF_CIDFont::LoadMetricsArray(CPDF_Array* pArray, CFX_DWordArray& result, int nElements)
1353 int width_status = 0;
1354 int iCurElement = 0;
1355 int first_code = 0, last_code;
1356 FX_DWORD count = pArray->GetCount();
1357 for (FX_DWORD i = 0; i < count; i ++) {
1358 CPDF_Object* pObj = pArray->GetElementValue(i);
1362 if (pObj->GetType() == PDFOBJ_ARRAY) {
1363 if (width_status != 1) {
1366 CPDF_Array* pArray = (CPDF_Array*)pObj;
1367 FX_DWORD count = pArray->GetCount();
1368 for (FX_DWORD j = 0; j < count; j += nElements) {
1369 result.Add(first_code);
1370 result.Add(first_code);
1371 for (int k = 0; k < nElements; k ++) {
1372 result.Add(pArray->GetInteger(j + k));
1378 if (width_status == 0) {
1379 first_code = pObj->GetInteger();
1381 } else if (width_status == 1) {
1382 last_code = pObj->GetInteger();
1387 result.Add(first_code);
1388 result.Add(last_code);
1390 result.Add(pObj->GetInteger());
1392 if (iCurElement == nElements) {
1399 FX_BOOL CPDF_CIDFont::LoadGB2312()
1401 m_BaseFont = m_pFontDict->GetString(FX_BSTRC("BaseFont"));
1402 CPDF_Dictionary* pFontDesc = m_pFontDict->GetDict(FX_BSTRC("FontDescriptor"));
1404 LoadFontDescriptor(pFontDesc);
1406 m_Charset = CIDSET_GB1;
1408 m_pCMap = CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals()->m_CMapManager.GetPredefinedCMap(
1409 FX_BSTRC("GBK-EUC-H"), FALSE);
1410 m_pCID2UnicodeMap = CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals()->m_CMapManager.GetCID2UnicodeMap(m_Charset, FALSE);
1411 if (!IsEmbedded()) {
1415 m_DefaultWidth = 1000;
1416 m_pAnsiWidths = FX_Alloc(FX_WORD, 128);
1417 for (int i = 32; i < 127; i ++) {
1418 m_pAnsiWidths[i] = 500;
1422 const struct _CIDTransform {
1424 uint8_t a, b, c, d, e, f;
1426 Japan1_VertCIDs[] = {
1427 {97, 129, 0, 0, 127, 55, 0},
1428 {7887, 127, 0, 0, 127, 76, 89},
1429 {7888, 127, 0, 0, 127, 79, 94},
1430 {7889, 0, 129, 127, 0, 17, 127},
1431 {7890, 0, 129, 127, 0, 17, 127},
1432 {7891, 0, 129, 127, 0, 17, 127},
1433 {7892, 0, 129, 127, 0, 17, 127},
1434 {7893, 0, 129, 127, 0, 17, 127},
1435 {7894, 0, 129, 127, 0, 17, 127},
1436 {7895, 0, 129, 127, 0, 17, 127},
1437 {7896, 0, 129, 127, 0, 17, 127},
1438 {7897, 0, 129, 127, 0, 17, 127},
1439 {7898, 0, 129, 127, 0, 17, 127},
1440 {7899, 0, 129, 127, 0, 17, 104},
1441 {7900, 0, 129, 127, 0, 17, 127},
1442 {7901, 0, 129, 127, 0, 17, 104},
1443 {7902, 0, 129, 127, 0, 17, 127},
1444 {7903, 0, 129, 127, 0, 17, 127},
1445 {7904, 0, 129, 127, 0, 17, 127},
1446 {7905, 0, 129, 127, 0, 17, 114},
1447 {7906, 0, 129, 127, 0, 17, 127},
1448 {7907, 0, 129, 127, 0, 17, 127},
1449 {7908, 0, 129, 127, 0, 17, 127},
1450 {7909, 0, 129, 127, 0, 17, 127},
1451 {7910, 0, 129, 127, 0, 17, 127},
1452 {7911, 0, 129, 127, 0, 17, 127},
1453 {7912, 0, 129, 127, 0, 17, 127},
1454 {7913, 0, 129, 127, 0, 17, 127},
1455 {7914, 0, 129, 127, 0, 17, 127},
1456 {7915, 0, 129, 127, 0, 17, 114},
1457 {7916, 0, 129, 127, 0, 17, 127},
1458 {7917, 0, 129, 127, 0, 17, 127},
1459 {7918, 127, 0, 0, 127, 18, 25},
1460 {7919, 127, 0, 0, 127, 18, 25},
1461 {7920, 127, 0, 0, 127, 18, 25},
1462 {7921, 127, 0, 0, 127, 18, 25},
1463 {7922, 127, 0, 0, 127, 18, 25},
1464 {7923, 127, 0, 0, 127, 18, 25},
1465 {7924, 127, 0, 0, 127, 18, 25},
1466 {7925, 127, 0, 0, 127, 18, 25},
1467 {7926, 127, 0, 0, 127, 18, 25},
1468 {7927, 127, 0, 0, 127, 18, 25},
1469 {7928, 127, 0, 0, 127, 18, 25},
1470 {7929, 127, 0, 0, 127, 18, 25},
1471 {7930, 127, 0, 0, 127, 18, 25},
1472 {7931, 127, 0, 0, 127, 18, 25},
1473 {7932, 127, 0, 0, 127, 18, 25},
1474 {7933, 127, 0, 0, 127, 18, 25},
1475 {7934, 127, 0, 0, 127, 18, 25},
1476 {7935, 127, 0, 0, 127, 18, 25},
1477 {7936, 127, 0, 0, 127, 18, 25},
1478 {7937, 127, 0, 0, 127, 18, 25},
1479 {7938, 127, 0, 0, 127, 18, 25},
1480 {7939, 127, 0, 0, 127, 18, 25},
1481 {8720, 0, 129, 127, 0, 19, 102},
1482 {8721, 0, 129, 127, 0, 13, 127},
1483 {8722, 0, 129, 127, 0, 19, 108},
1484 {8723, 0, 129, 127, 0, 19, 102},
1485 {8724, 0, 129, 127, 0, 19, 102},
1486 {8725, 0, 129, 127, 0, 19, 102},
1487 {8726, 0, 129, 127, 0, 19, 102},
1488 {8727, 0, 129, 127, 0, 19, 102},
1489 {8728, 0, 129, 127, 0, 19, 114},
1490 {8729, 0, 129, 127, 0, 19, 114},
1491 {8730, 0, 129, 127, 0, 38, 108},
1492 {8731, 0, 129, 127, 0, 13, 108},
1493 {8732, 0, 129, 127, 0, 19, 108},
1494 {8733, 0, 129, 127, 0, 19, 108},
1495 {8734, 0, 129, 127, 0, 19, 108},
1496 {8735, 0, 129, 127, 0, 19, 108},
1497 {8736, 0, 129, 127, 0, 19, 102},
1498 {8737, 0, 129, 127, 0, 19, 102},
1499 {8738, 0, 129, 127, 0, 19, 102},
1500 {8739, 0, 129, 127, 0, 19, 102},
1501 {8740, 0, 129, 127, 0, 19, 102},
1502 {8741, 0, 129, 127, 0, 19, 102},
1503 {8742, 0, 129, 127, 0, 19, 102},
1504 {8743, 0, 129, 127, 0, 19, 102},
1505 {8744, 0, 129, 127, 0, 19, 102},
1506 {8745, 0, 129, 127, 0, 19, 102},
1507 {8746, 0, 129, 127, 0, 19, 114},
1508 {8747, 0, 129, 127, 0, 19, 114},
1509 {8748, 0, 129, 127, 0, 19, 102},
1510 {8749, 0, 129, 127, 0, 19, 102},
1511 {8750, 0, 129, 127, 0, 19, 102},
1512 {8751, 0, 129, 127, 0, 19, 102},
1513 {8752, 0, 129, 127, 0, 19, 102},
1514 {8753, 0, 129, 127, 0, 19, 102},
1515 {8754, 0, 129, 127, 0, 19, 102},
1516 {8755, 0, 129, 127, 0, 19, 102},
1517 {8756, 0, 129, 127, 0, 19, 102},
1518 {8757, 0, 129, 127, 0, 19, 102},
1519 {8758, 0, 129, 127, 0, 19, 102},
1520 {8759, 0, 129, 127, 0, 19, 102},
1521 {8760, 0, 129, 127, 0, 19, 102},
1522 {8761, 0, 129, 127, 0, 19, 102},
1523 {8762, 0, 129, 127, 0, 19, 102},
1524 {8763, 0, 129, 127, 0, 19, 102},
1525 {8764, 0, 129, 127, 0, 19, 102},
1526 {8765, 0, 129, 127, 0, 19, 102},
1527 {8766, 0, 129, 127, 0, 19, 102},
1528 {8767, 0, 129, 127, 0, 19, 102},
1529 {8768, 0, 129, 127, 0, 19, 102},
1530 {8769, 0, 129, 127, 0, 19, 102},
1531 {8770, 0, 129, 127, 0, 19, 102},
1532 {8771, 0, 129, 127, 0, 19, 102},
1533 {8772, 0, 129, 127, 0, 19, 102},
1534 {8773, 0, 129, 127, 0, 19, 102},
1535 {8774, 0, 129, 127, 0, 19, 102},
1536 {8775, 0, 129, 127, 0, 19, 102},
1537 {8776, 0, 129, 127, 0, 19, 102},
1538 {8777, 0, 129, 127, 0, 19, 102},
1539 {8778, 0, 129, 127, 0, 19, 102},
1540 {8779, 0, 129, 127, 0, 19, 114},
1541 {8780, 0, 129, 127, 0, 19, 108},
1542 {8781, 0, 129, 127, 0, 19, 114},
1543 {8782, 0, 129, 127, 0, 13, 114},
1544 {8783, 0, 129, 127, 0, 19, 108},
1545 {8784, 0, 129, 127, 0, 13, 114},
1546 {8785, 0, 129, 127, 0, 19, 108},
1547 {8786, 0, 129, 127, 0, 19, 108},
1548 {8787, 0, 129, 127, 0, 19, 108},
1549 {8788, 0, 129, 127, 0, 19, 108},
1550 {8789, 0, 129, 127, 0, 19, 108},
1551 {8790, 0, 129, 127, 0, 19, 108},
1552 {8791, 0, 129, 127, 0, 19, 108},
1553 {8792, 0, 129, 127, 0, 19, 108},
1554 {8793, 0, 129, 127, 0, 19, 108},
1555 {8794, 0, 129, 127, 0, 19, 108},
1556 {8795, 0, 129, 127, 0, 19, 108},
1557 {8796, 0, 129, 127, 0, 19, 108},
1558 {8797, 0, 129, 127, 0, 19, 108},
1559 {8798, 0, 129, 127, 0, 19, 108},
1560 {8799, 0, 129, 127, 0, 19, 108},
1561 {8800, 0, 129, 127, 0, 19, 108},
1562 {8801, 0, 129, 127, 0, 19, 108},
1563 {8802, 0, 129, 127, 0, 19, 108},
1564 {8803, 0, 129, 127, 0, 19, 108},
1565 {8804, 0, 129, 127, 0, 19, 108},
1566 {8805, 0, 129, 127, 0, 19, 108},
1567 {8806, 0, 129, 127, 0, 19, 108},
1568 {8807, 0, 129, 127, 0, 19, 108},
1569 {8808, 0, 129, 127, 0, 19, 108},
1570 {8809, 0, 129, 127, 0, 19, 108},
1571 {8810, 0, 129, 127, 0, 19, 108},
1572 {8811, 0, 129, 127, 0, 19, 114},
1573 {8812, 0, 129, 127, 0, 19, 102},
1574 {8813, 0, 129, 127, 0, 19, 114},
1575 {8814, 0, 129, 127, 0, 76, 102},
1576 {8815, 0, 129, 127, 0, 13, 121},
1577 {8816, 0, 129, 127, 0, 19, 114},
1578 {8817, 0, 129, 127, 0, 19, 127},
1579 {8818, 0, 129, 127, 0, 19, 114},
1580 {8819, 0, 129, 127, 0, 218, 108},
1582 const uint8_t* CPDF_CIDFont::GetCIDTransform(FX_WORD CID) const
1584 if (m_Charset != CIDSET_JAPAN1 || m_pFontFile != NULL) {
1588 int end = sizeof Japan1_VertCIDs / sizeof(struct _CIDTransform) - 1;
1589 while (begin <= end) {
1590 int middle = (begin + end) / 2;
1591 FX_WORD middlecode = Japan1_VertCIDs[middle].CID;
1592 if (middlecode > CID) {
1594 } else if (middlecode < CID) {
1597 return &Japan1_VertCIDs[middle].a;