From d03ba8d1a5928e8f3f6bd7da063b53b0bc40abfd Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 8 Oct 2015 15:51:29 -0700 Subject: Fix a bunch of sign mismatch warnings. Also remove some gotos and move code into an anonymous namespace. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1356373003 . --- core/include/fpdfapi/fpdf_resource.h | 23 +- core/src/fpdfapi/fpdf_cmaps/fpdf_cmaps.cpp | 4 +- core/src/fpdfapi/fpdf_font/font_int.h | 42 +- core/src/fpdfapi/fpdf_font/fpdf_font.cpp | 16 +- core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp | 1269 +++++++++++--------- core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp | 2 +- .../fpdfapi/fpdf_render/fpdf_render_loadimage.cpp | 4 +- core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp | 16 +- core/src/fxcodec/codec/fx_codec_jpeg.cpp | 4 +- core/src/fxcodec/codec/fx_codec_jpx_opj.cpp | 10 +- core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp | 47 +- core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h | 4 +- core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp | 43 +- core/src/fxcodec/jbig2/JBig2_HuffmanTable.h | 6 +- core/src/fxge/ge/fx_ge_linux.cpp | 6 +- 15 files changed, 811 insertions(+), 685 deletions(-) diff --git a/core/include/fpdfapi/fpdf_resource.h b/core/include/fpdfapi/fpdf_resource.h index d687c97b9c..9fe6e2b95f 100644 --- a/core/include/fpdfapi/fpdf_resource.h +++ b/core/include/fpdfapi/fpdf_resource.h @@ -414,13 +414,15 @@ class CPDF_Type3Font : public CPDF_SimpleFont { CFX_MapPtrToPtr m_DeletedMap; }; -#define CIDSET_UNKNOWN 0 -#define CIDSET_GB1 1 -#define CIDSET_CNS1 2 -#define CIDSET_JAPAN1 3 -#define CIDSET_KOREA1 4 -#define CIDSET_UNICODE 5 -#define NUMBER_OF_CIDSETS 6 +enum CIDSet { + CIDSET_UNKNOWN, + CIDSET_GB1, + CIDSET_CNS1, + CIDSET_JAPAN1, + CIDSET_KOREA1, + CIDSET_UNICODE, + CIDSET_NUM_SETS +}; class CPDF_CIDFont : public CPDF_Font { public: @@ -428,6 +430,8 @@ class CPDF_CIDFont : public CPDF_Font { ~CPDF_CIDFont() override; + static FX_FLOAT CIDTransformToFloat(uint8_t ch); + FX_BOOL LoadGB2312(); int GlyphFromCharCode(FX_DWORD charcode, FX_BOOL* pVertGlyph = NULL) override; int GetCharWidthF(FX_DWORD charcode, int level = 0) override; @@ -442,9 +446,6 @@ class CPDF_CIDFont : public CPDF_Font { int CountChar(const FX_CHAR* pString, int size) const override; int AppendChar(FX_CHAR* str, FX_DWORD charcode) const override; int GetCharSize(FX_DWORD charcode) const override; - - int GetCharset() const { return m_Charset; } - const uint8_t* GetCIDTransform(FX_WORD CID) const; FX_BOOL IsVertWriting() const override; short GetVertWidth(FX_WORD CID) const; @@ -467,7 +468,7 @@ class CPDF_CIDFont : public CPDF_Font { CPDF_CMap* m_pCMap; CPDF_CMap* m_pAllocatedCMap; CPDF_CID2UnicodeMap* m_pCID2UnicodeMap; - int m_Charset; + CIDSet m_Charset; FX_BOOL m_bType1; CPDF_StreamAcc* m_pCIDToGIDMap; FX_BOOL m_bCIDIsGID; diff --git a/core/src/fpdfapi/fpdf_cmaps/fpdf_cmaps.cpp b/core/src/fpdfapi/fpdf_cmaps/fpdf_cmaps.cpp index 8b0bff0257..252a4d6e4f 100644 --- a/core/src/fpdfapi/fpdf_cmaps/fpdf_cmaps.cpp +++ b/core/src/fpdfapi/fpdf_cmaps/fpdf_cmaps.cpp @@ -8,6 +8,7 @@ #include "../../../include/fpdfapi/fpdf_module.h" #include "../fpdf_font/font_int.h" #include "cmap_int.h" + void FPDFAPI_FindEmbeddedCMap(const char* name, int charset, int coding, @@ -179,7 +180,8 @@ FX_DWORD FPDFAPI_CharCodeFromCID(const FXCMAP_CMap* pMap, FX_WORD cid) { } return 0; } -void FPDFAPI_LoadCID2UnicodeMap(int charset, + +void FPDFAPI_LoadCID2UnicodeMap(CIDSet charset, const FX_WORD*& pMap, FX_DWORD& count) { CPDF_FontGlobals* pFontGlobals = diff --git a/core/src/fpdfapi/fpdf_font/font_int.h b/core/src/fpdfapi/fpdf_font/font_int.h index 79c25219a0..48e58d6345 100644 --- a/core/src/fpdfapi/fpdf_font/font_int.h +++ b/core/src/fpdfapi/fpdf_font/font_int.h @@ -27,13 +27,13 @@ class CPDF_CMapManager { CPDF_CMapManager(); ~CPDF_CMapManager(); void* GetPackage(FX_BOOL bPrompt); - CPDF_CMap* GetPredefinedCMap(const CFX_ByteString& name, FX_BOOL bPrompt); - CPDF_CID2UnicodeMap* GetCID2UnicodeMap(int charset, FX_BOOL bPrompt); + CPDF_CMap* GetPredefinedCMap(const CFX_ByteString& name, FX_BOOL bPromptCJK); + CPDF_CID2UnicodeMap* GetCID2UnicodeMap(CIDSet charset, FX_BOOL bPromptCJK); void ReloadAll(); private: - CPDF_CMap* LoadPredefinedCMap(const CFX_ByteString& name, FX_BOOL bPrompt); - CPDF_CID2UnicodeMap* LoadCID2UnicodeMap(int charset, FX_BOOL bPrompt); + CPDF_CMap* LoadPredefinedCMap(const CFX_ByteString& name, FX_BOOL bPromptCJK); + CPDF_CID2UnicodeMap* LoadCID2UnicodeMap(CIDSet charset, FX_BOOL bPromptCJK); FX_BOOL m_bPrompted; std::map m_CMaps; @@ -51,21 +51,23 @@ class CPDF_FontGlobals { struct { const struct FXCMAP_CMap* m_pMapList; int m_Count; - } m_EmbeddedCharsets[NUMBER_OF_CIDSETS]; + } m_EmbeddedCharsets[CIDSET_NUM_SETS]; struct { const FX_WORD* m_pMap; int m_Count; - } m_EmbeddedToUnicodes[NUMBER_OF_CIDSETS]; + } m_EmbeddedToUnicodes[CIDSET_NUM_SETS]; private: CFX_MapPtrToPtr m_pStockMap; uint8_t* m_pContrastRamps; }; -struct _CMap_CodeRange { + +struct CMap_CodeRange { int m_CharSize; uint8_t m_Lower[4]; uint8_t m_Upper[4]; }; + class CPDF_CMapParser { public: CPDF_CMapParser(); @@ -79,7 +81,7 @@ class CPDF_CMapParser { int m_Status; int m_CodeSeq; FX_DWORD m_CodePoints[4]; - CFX_ArrayTemplate<_CMap_CodeRange> m_CodeRanges; + CFX_ArrayTemplate m_CodeRanges; CFX_ByteString m_Registry, m_Ordering, m_Supplement; CFX_ByteString m_LastWord; }; @@ -100,7 +102,6 @@ class CPDF_CMap { FX_BOOL LoadEmbedded(const uint8_t* pData, FX_DWORD dwSize); void Release(); FX_BOOL IsLoaded() const { return m_bLoaded; } - int GetCharset() { return m_Charset; } FX_BOOL IsVertWriting() const { return m_bVertical; } FX_WORD CIDFromCharCode(FX_DWORD charcode) const; FX_DWORD CharCodeFromCID(FX_WORD CID) const; @@ -124,7 +125,8 @@ class CPDF_CMap { protected: CFX_ByteString m_PredefinedCMap; FX_BOOL m_bVertical; - int m_Charset, m_Coding; + CIDSet m_Charset; + int m_Coding; CodingScheme m_CodingScheme; int m_nCodeRanges; uint8_t* m_pLeadingBytes; @@ -134,15 +136,7 @@ class CPDF_CMap { const FXCMAP_CMap* m_pEmbedMap; CPDF_CMap* m_pUseMap; }; -class CPDF_PredefinedCMap { - public: - const FX_CHAR* m_pName; - int m_Charset; - int m_Coding; - CPDF_CMap::CodingScheme m_CodingScheme; - FX_DWORD m_LeadingSegCount; - uint8_t m_LeadingSegs[4]; -}; + typedef struct _FileHeader { uint8_t btTag[4]; uint8_t btVersion; @@ -154,20 +148,22 @@ typedef struct _FileHeader { FX_DWORD dwDataOffset; FX_DWORD dwRecordSize; } FXMP_FILEHEADER; + class CPDF_CID2UnicodeMap { public: CPDF_CID2UnicodeMap(); ~CPDF_CID2UnicodeMap(); FX_BOOL Initialize(); FX_BOOL IsLoaded(); - void Load(CPDF_CMapManager* pMgr, int charset, FX_BOOL bPromptCJK); + void Load(CPDF_CMapManager* pMgr, CIDSet charset, FX_BOOL bPromptCJK); FX_WCHAR UnicodeFromCID(FX_WORD CID); protected: - int m_Charset; + CIDSet m_Charset; const FX_WORD* m_pEmbeddedMap; FX_DWORD m_EmbeddedCount; }; + class CPDF_ToUnicodeMap { public: void Load(CPDF_Stream* pStream); @@ -185,4 +181,8 @@ class CPDF_FontCharMap : public CFX_CharMap { CPDF_Font* m_pFont; }; +void FPDFAPI_LoadCID2UnicodeMap(CIDSet charset, + const FX_WORD*& pMap, + FX_DWORD& count); + #endif // CORE_SRC_FPDFAPI_FPDF_FONT_FONT_INT_H_ diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp index 20c4082d4a..ed5809c8a8 100644 --- a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp +++ b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp @@ -38,7 +38,7 @@ class CFX_StockFontArray { FXSYS_memset(m_pStockFonts, 0, sizeof(m_pStockFonts)); } ~CFX_StockFontArray() { - for (int i = 0; i < FX_ArraySize(m_pStockFonts); i++) { + for (size_t i = 0; i < FX_ArraySize(m_pStockFonts); i++) { if (!m_pStockFonts[i]) continue; CPDF_Dictionary* pFontDict = m_pStockFonts[i]->GetFontDict(); @@ -593,7 +593,7 @@ static CFX_WideString _StringToWideString(const CFX_ByteStringC& str) { return result; } void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) { - int CIDSet = 0; + CIDSet cid_set = CIDSET_UNKNOWN; CPDF_StreamAcc stream; stream.LoadAllData(pStream, FALSE); CPDF_SimpleParser parser(stream.GetData(), stream.GetSize()); @@ -681,20 +681,20 @@ void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) { } } } else if (word == FX_BSTRC("/Adobe-Korea1-UCS2")) { - CIDSet = CIDSET_KOREA1; + cid_set = CIDSET_KOREA1; } else if (word == FX_BSTRC("/Adobe-Japan1-UCS2")) { - CIDSet = CIDSET_JAPAN1; + cid_set = CIDSET_JAPAN1; } else if (word == FX_BSTRC("/Adobe-CNS1-UCS2")) { - CIDSet = CIDSET_CNS1; + cid_set = CIDSET_CNS1; } else if (word == FX_BSTRC("/Adobe-GB1-UCS2")) { - CIDSet = CIDSET_GB1; + cid_set = CIDSET_GB1; } } - if (CIDSet) { + if (cid_set) { m_pBaseMap = CPDF_ModuleMgr::Get() ->GetPageModule() ->GetFontGlobals() - ->m_CMapManager.GetCID2UnicodeMap(CIDSet, FALSE); + ->m_CMapManager.GetCID2UnicodeMap(cid_set, FALSE); } else { m_pBaseMap = NULL; } diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp index ad33a3f0a6..4d45d6f57e 100644 --- a/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp +++ b/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp @@ -12,285 +12,23 @@ #include "../fpdf_cmaps/cmap_int.h" #include "font_int.h" -CPDF_CMapManager::CPDF_CMapManager() { - m_bPrompted = FALSE; - FXSYS_memset(m_CID2UnicodeMaps, 0, sizeof m_CID2UnicodeMaps); -} -CPDF_CMapManager::~CPDF_CMapManager() { - for (const auto& pair : m_CMaps) { - delete pair.second; - } - m_CMaps.clear(); - for (int i = 0; i < FX_ArraySize(m_CID2UnicodeMaps); ++i) { - delete m_CID2UnicodeMaps[i]; - } -} -CPDF_CMap* CPDF_CMapManager::GetPredefinedCMap(const CFX_ByteString& name, - FX_BOOL bPromptCJK) { - auto it = m_CMaps.find(name); - if (it != m_CMaps.end()) { - return it->second; - } - CPDF_CMap* pCMap = LoadPredefinedCMap(name, bPromptCJK); - if (!name.IsEmpty()) { - m_CMaps[name] = pCMap; - } - return pCMap; -} -CPDF_CMap* CPDF_CMapManager::LoadPredefinedCMap(const CFX_ByteString& name, - FX_BOOL bPromptCJK) { - CPDF_CMap* pCMap = new CPDF_CMap; - const FX_CHAR* pname = name; - if (*pname == '/') { - pname++; - } - pCMap->LoadPredefined(this, pname, bPromptCJK); - return pCMap; -} -static const FX_CHAR* const g_CharsetNames[NUMBER_OF_CIDSETS] = { - NULL, "GB1", "CNS1", "Japan1", "Korea1", "UCS"}; -static const int g_CharsetCPs[NUMBER_OF_CIDSETS] = {0, 936, 950, - 932, 949, 1200}; -int _CharsetFromOrdering(const CFX_ByteString& Ordering) { - for (int charset = 1; charset < NUMBER_OF_CIDSETS; charset++) { - if (Ordering == CFX_ByteStringC(g_CharsetNames[charset])) - return charset; - } - return CIDSET_UNKNOWN; -} -void CPDF_CMapManager::ReloadAll() { - for (const auto& pair : m_CMaps) { - CPDF_CMap* pCMap = pair.second; - pCMap->LoadPredefined(this, pair.first, FALSE); - } - for (int i = 0; i < FX_ArraySize(m_CID2UnicodeMaps); ++i) { - if (CPDF_CID2UnicodeMap* pMap = m_CID2UnicodeMaps[i]) { - pMap->Load(this, i, FALSE); - } - } -} -CPDF_CID2UnicodeMap* CPDF_CMapManager::GetCID2UnicodeMap(int charset, - FX_BOOL bPromptCJK) { - if (m_CID2UnicodeMaps[charset] == NULL) { - m_CID2UnicodeMaps[charset] = LoadCID2UnicodeMap(charset, bPromptCJK); - } - return m_CID2UnicodeMaps[charset]; -} -CPDF_CID2UnicodeMap* CPDF_CMapManager::LoadCID2UnicodeMap(int charset, - FX_BOOL bPromptCJK) { - CPDF_CID2UnicodeMap* pMap = new CPDF_CID2UnicodeMap(); - if (!pMap->Initialize()) { - delete pMap; - return NULL; - } - pMap->Load(this, charset, bPromptCJK); - return pMap; -} -CPDF_CMapParser::CPDF_CMapParser() { - m_pCMap = NULL; - m_Status = 0; - m_CodeSeq = 0; -} -FX_BOOL CPDF_CMapParser::Initialize(CPDF_CMap* pCMap) { - m_pCMap = pCMap; - m_Status = 0; - m_CodeSeq = 0; - m_AddMaps.EstimateSize(0, 10240); - return TRUE; -} -static FX_DWORD CMap_GetCode(const CFX_ByteStringC& word) { - int num = 0; - if (word.GetAt(0) == '<') { - for (int i = 1; i < word.GetLength(); i++) { - uint8_t digit = word.GetAt(i); - if (digit >= '0' && digit <= '9') { - digit = digit - '0'; - } else if (digit >= 'a' && digit <= 'f') { - digit = digit - 'a' + 10; - } else if (digit >= 'A' && digit <= 'F') { - digit = digit - 'A' + 10; - } else { - return num; - } - num = num * 16 + digit; - } - } else { - for (int i = 0; i < word.GetLength(); i++) { - if (word.GetAt(i) < '0' || word.GetAt(i) > '9') { - return num; - } - num = num * 10 + word.GetAt(i) - '0'; - } - } - return num; -} -static FX_BOOL _CMap_GetCodeRange(_CMap_CodeRange& range, - const CFX_ByteStringC& first, - const CFX_ByteStringC& second) { - if (first.GetLength() == 0 || first.GetAt(0) != '<') { - return FALSE; - } - int i; - for (i = 1; i < first.GetLength(); i++) - if (first.GetAt(i) == '>') { - break; - } - range.m_CharSize = (i - 1) / 2; - if (range.m_CharSize > 4) { - return FALSE; - } - for (i = 0; i < range.m_CharSize; i++) { - uint8_t digit1 = first.GetAt(i * 2 + 1); - uint8_t digit2 = first.GetAt(i * 2 + 2); - uint8_t byte = (digit1 >= '0' && digit1 <= '9') - ? (digit1 - '0') - : ((digit1 & 0xdf) - 'A' + 10); - byte = byte * 16 + ((digit2 >= '0' && digit2 <= '9') - ? (digit2 - '0') - : ((digit2 & 0xdf) - 'A' + 10)); - range.m_Lower[i] = byte; - } - FX_DWORD size = second.GetLength(); - for (i = 0; i < range.m_CharSize; i++) { - uint8_t digit1 = - ((FX_DWORD)i * 2 + 1 < size) ? second.GetAt((FX_STRSIZE)i * 2 + 1) : 0; - uint8_t digit2 = - ((FX_DWORD)i * 2 + 2 < size) ? second.GetAt((FX_STRSIZE)i * 2 + 2) : 0; - uint8_t byte = (digit1 >= '0' && digit1 <= '9') - ? (digit1 - '0') - : ((digit1 & 0xdf) - 'A' + 10); - byte = byte * 16 + ((digit2 >= '0' && digit2 <= '9') - ? (digit2 - '0') - : ((digit2 & 0xdf) - 'A' + 10)); - range.m_Upper[i] = byte; - } - return TRUE; -} -static CFX_ByteString CMap_GetString(const CFX_ByteStringC& word) { - return word.Mid(1, word.GetLength() - 2); -} -void CPDF_CMapParser::ParseWord(const CFX_ByteStringC& word) { - if (word.IsEmpty()) { - return; - } - if (word == FX_BSTRC("begincidchar")) { - m_Status = 1; - m_CodeSeq = 0; - } else if (word == FX_BSTRC("begincidrange")) { - m_Status = 2; - m_CodeSeq = 0; - } else if (word == FX_BSTRC("endcidrange") || - word == FX_BSTRC("endcidchar")) { - m_Status = 0; - } else if (word == FX_BSTRC("/WMode")) { - m_Status = 6; - } else if (word == FX_BSTRC("/Registry")) { - m_Status = 3; - } else if (word == FX_BSTRC("/Ordering")) { - m_Status = 4; - } else if (word == FX_BSTRC("/Supplement")) { - m_Status = 5; - } else if (word == FX_BSTRC("begincodespacerange")) { - m_Status = 7; - m_CodeSeq = 0; - } else if (word == FX_BSTRC("usecmap")) { - } else if (m_Status == 1 || m_Status == 2) { - m_CodePoints[m_CodeSeq] = CMap_GetCode(word); - m_CodeSeq++; - FX_DWORD StartCode, EndCode; - FX_WORD StartCID; - if (m_Status == 1) { - if (m_CodeSeq < 2) { - return; - } - EndCode = StartCode = m_CodePoints[0]; - StartCID = (FX_WORD)m_CodePoints[1]; - } else { - if (m_CodeSeq < 3) { - return; - } - StartCode = m_CodePoints[0]; - EndCode = m_CodePoints[1]; - StartCID = (FX_WORD)m_CodePoints[2]; - } - if (EndCode < 0x10000) { - for (FX_DWORD code = StartCode; code <= EndCode; code++) { - m_pCMap->m_pMapping[code] = (FX_WORD)(StartCID + code - StartCode); - } - } else { - FX_DWORD buf[2]; - buf[0] = StartCode; - buf[1] = ((EndCode - StartCode) << 16) + StartCID; - m_AddMaps.AppendBlock(buf, sizeof buf); - } - m_CodeSeq = 0; - } else if (m_Status == 3) { - CMap_GetString(word); - m_Status = 0; - } else if (m_Status == 4) { - m_pCMap->m_Charset = _CharsetFromOrdering(CMap_GetString(word)); - m_Status = 0; - } else if (m_Status == 5) { - CMap_GetCode(word); - m_Status = 0; - } else if (m_Status == 6) { - m_pCMap->m_bVertical = CMap_GetCode(word); - m_Status = 0; - } else if (m_Status == 7) { - if (word == FX_BSTRC("endcodespacerange")) { - int nSegs = m_CodeRanges.GetSize(); - if (nSegs > 1) { - m_pCMap->m_CodingScheme = CPDF_CMap::MixedFourBytes; - m_pCMap->m_nCodeRanges = nSegs; - m_pCMap->m_pLeadingBytes = - FX_Alloc2D(uint8_t, nSegs, sizeof(_CMap_CodeRange)); - FXSYS_memcpy(m_pCMap->m_pLeadingBytes, m_CodeRanges.GetData(), - nSegs * sizeof(_CMap_CodeRange)); - } else if (nSegs == 1) { - m_pCMap->m_CodingScheme = (m_CodeRanges[0].m_CharSize == 2) - ? CPDF_CMap::TwoBytes - : CPDF_CMap::OneByte; - } - m_Status = 0; - } else { - if (word.GetLength() == 0 || word.GetAt(0) != '<') { - return; - } - if (m_CodeSeq % 2) { - _CMap_CodeRange range; - if (_CMap_GetCodeRange(range, m_LastWord, word)) { - m_CodeRanges.Add(range); - } - } - m_CodeSeq++; - } - } - m_LastWord = word; -} -CPDF_CMap::CPDF_CMap() { - m_Charset = CIDSET_UNKNOWN; - m_Coding = CIDCODING_UNKNOWN; - m_CodingScheme = TwoBytes; - m_bVertical = 0; - m_bLoaded = FALSE; - m_pMapping = NULL; - m_pLeadingBytes = NULL; - m_pAddMapping = NULL; - m_pEmbedMap = NULL; - m_pUseMap = NULL; - m_nCodeRanges = 0; -} -CPDF_CMap::~CPDF_CMap() { - FX_Free(m_pMapping); - FX_Free(m_pAddMapping); - FX_Free(m_pLeadingBytes); - delete m_pUseMap; -} -void CPDF_CMap::Release() { - if (m_PredefinedCMap.IsEmpty()) { - delete this; - } -} +namespace { + +const FX_CHAR* const g_CharsetNames[CIDSET_NUM_SETS] = + {nullptr, "GB1", "CNS1", "Japan1", "Korea1", "UCS"}; + +const int g_CharsetCPs[CIDSET_NUM_SETS] = {0, 936, 950, 932, 949, 1200}; + +class CPDF_PredefinedCMap { + public: + const FX_CHAR* m_pName; + CIDSet m_Charset; + int m_Coding; + CPDF_CMap::CodingScheme m_CodingScheme; + FX_DWORD m_LeadingSegCount; + uint8_t m_LeadingSegs[4]; +}; + const CPDF_PredefinedCMap g_PredefinedCMaps[] = { {"GB-EUC", CIDSET_GB1, @@ -429,7 +167,627 @@ const CPDF_PredefinedCMap g_PredefinedCMaps[] = { {0xa1, 0xfd}}, {"UniKS-UCS2", CIDSET_KOREA1, CIDCODING_UCS2, CPDF_CMap::TwoBytes}, {"UniKS-UTF16", CIDSET_KOREA1, CIDCODING_UTF16, CPDF_CMap::TwoBytes}, - {NULL, 0, 0}}; +}; + +CIDSet CIDSetFromSizeT(size_t index) { + if (index >= CIDSET_NUM_SETS) { + NOTREACHED(); + return CIDSET_UNKNOWN; + } + return static_cast(index); +} + +CIDSet CharsetFromOrdering(const CFX_ByteString& ordering) { + for (size_t charset = 1; charset < FX_ArraySize(g_CharsetNames); ++charset) { + if (ordering == CFX_ByteStringC(g_CharsetNames[charset])) + return CIDSetFromSizeT(charset); + } + return CIDSET_UNKNOWN; +} + +FX_DWORD CMap_GetCode(const CFX_ByteStringC& word) { + int num = 0; + if (word.GetAt(0) == '<') { + for (int i = 1; i < word.GetLength(); i++) { + uint8_t digit = word.GetAt(i); + if (digit >= '0' && digit <= '9') { + digit = digit - '0'; + } else if (digit >= 'a' && digit <= 'f') { + digit = digit - 'a' + 10; + } else if (digit >= 'A' && digit <= 'F') { + digit = digit - 'A' + 10; + } else { + return num; + } + num = num * 16 + digit; + } + } else { + for (int i = 0; i < word.GetLength(); i++) { + if (word.GetAt(i) < '0' || word.GetAt(i) > '9') { + return num; + } + num = num * 10 + word.GetAt(i) - '0'; + } + } + return num; +} + +bool CMap_GetCodeRange(CMap_CodeRange& range, + const CFX_ByteStringC& first, + const CFX_ByteStringC& second) { + if (first.GetLength() == 0 || first.GetAt(0) != '<') + return false; + + int i; + for (i = 1; i < first.GetLength(); ++i) { + if (first.GetAt(i) == '>') { + break; + } + } + range.m_CharSize = (i - 1) / 2; + if (range.m_CharSize > 4) + return false; + + for (i = 0; i < range.m_CharSize; ++i) { + uint8_t digit1 = first.GetAt(i * 2 + 1); + uint8_t digit2 = first.GetAt(i * 2 + 2); + uint8_t byte = (digit1 >= '0' && digit1 <= '9') + ? (digit1 - '0') + : ((digit1 & 0xdf) - 'A' + 10); + byte = byte * 16 + ((digit2 >= '0' && digit2 <= '9') + ? (digit2 - '0') + : ((digit2 & 0xdf) - 'A' + 10)); + range.m_Lower[i] = byte; + } + + FX_DWORD size = second.GetLength(); + for (i = 0; i < range.m_CharSize; ++i) { + uint8_t digit1 = + ((FX_DWORD)i * 2 + 1 < size) ? second.GetAt((FX_STRSIZE)i * 2 + 1) : 0; + uint8_t digit2 = + ((FX_DWORD)i * 2 + 2 < size) ? second.GetAt((FX_STRSIZE)i * 2 + 2) : 0; + uint8_t byte = (digit1 >= '0' && digit1 <= '9') + ? (digit1 - '0') + : ((digit1 & 0xdf) - 'A' + 10); + byte = byte * 16 + ((digit2 >= '0' && digit2 <= '9') + ? (digit2 - '0') + : ((digit2 & 0xdf) - 'A' + 10)); + range.m_Upper[i] = byte; + } + return true; +} + +CFX_ByteString CMap_GetString(const CFX_ByteStringC& word) { + return word.Mid(1, word.GetLength() - 2); +} + +int CompareDWORD(const void* data1, const void* data2) { + return (*(FX_DWORD*)data1) - (*(FX_DWORD*)data2); +} + +int CompareCID(const void* key, const void* element) { + if ((*(FX_DWORD*)key) < (*(FX_DWORD*)element)) { + return -1; + } + if ((*(FX_DWORD*)key) > + (*(FX_DWORD*)element) + ((FX_DWORD*)element)[1] / 65536) { + return 1; + } + return 0; +} + +int CheckCodeRange(uint8_t* codes, + int size, + CMap_CodeRange* pRanges, + int nRanges) { + int iSeg = nRanges - 1; + while (iSeg >= 0) { + if (pRanges[iSeg].m_CharSize < size) { + --iSeg; + continue; + } + int iChar = 0; + while (iChar < size) { + if (codes[iChar] < pRanges[iSeg].m_Lower[iChar] || + codes[iChar] > pRanges[iSeg].m_Upper[iChar]) { + break; + } + ++iChar; + } + if (iChar == pRanges[iSeg].m_CharSize) + return 2; + + if (iChar) + return (size == pRanges[iSeg].m_CharSize) ? 2 : 1; + iSeg--; + } + return 0; +} + +int GetCharSizeImpl(FX_DWORD charcode, + CMap_CodeRange* pRanges, + int iRangesSize) { + if (!iRangesSize) + return 1; + + uint8_t codes[4]; + codes[0] = codes[1] = 0x00; + codes[2] = (uint8_t)(charcode >> 8 & 0xFF); + codes[3] = (uint8_t)charcode; + int offset = 0; + int size = 4; + for (int i = 0; i < 4; ++i) { + int iSeg = iRangesSize - 1; + while (iSeg >= 0) { + if (pRanges[iSeg].m_CharSize < size) { + --iSeg; + continue; + } + int iChar = 0; + while (iChar < size) { + if (codes[offset + iChar] < pRanges[iSeg].m_Lower[iChar] || + codes[offset + iChar] > pRanges[iSeg].m_Upper[iChar]) { + break; + } + ++iChar; + } + if (iChar == pRanges[iSeg].m_CharSize) + return size; + --iSeg; + } + --size; + ++offset; + } + return 1; +} + +bool IsValidEmbeddedCharcodeFromUnicodeCharset(CIDSet charset) { + switch (charset) { + case CIDSET_GB1: + case CIDSET_CNS1: + case CIDSET_JAPAN1: + case CIDSET_KOREA1: + return true; + + default: + return false; + } +} + +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ +FX_DWORD EmbeddedCharcodeFromUnicode(const FXCMAP_CMap* pEmbedMap, + CIDSet charset, + FX_WCHAR unicode) { + if (!IsValidEmbeddedCharcodeFromUnicodeCharset(charset)) + return 0; + + CPDF_FontGlobals* pFontGlobals = + CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals(); + const FX_WORD* pCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_pMap; + if (!pCodes) + return 0; + + int nCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_Count; + for (int i = 0; i < nCodes; ++i) { + if (pCodes[i] == unicode) { + FX_DWORD CharCode = FPDFAPI_CharCodeFromCID(pEmbedMap, i); + if (CharCode != 0) { + return CharCode; + } + } + } + return 0; +} +#endif // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + +FX_WCHAR EmbeddedUnicodeFromCharcode(const FXCMAP_CMap* pEmbedMap, + CIDSet charset, + FX_DWORD charcode) { + if (!IsValidEmbeddedCharcodeFromUnicodeCharset(charset)) + return 0; + + FX_WORD cid = FPDFAPI_CIDFromCharCode(pEmbedMap, charcode); + if (cid == 0) + return 0; + + CPDF_FontGlobals* pFontGlobals = + CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals(); + const FX_WORD* pCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_pMap; + if (!pCodes) + return 0; + + if (cid < pFontGlobals->m_EmbeddedToUnicodes[charset].m_Count) + return pCodes[cid]; + return 0; +} + +void FT_UseCIDCharmap(FXFT_Face face, int coding) { + int encoding; + switch (coding) { + case CIDCODING_GB: + encoding = FXFT_ENCODING_GB2312; + break; + case CIDCODING_BIG5: + encoding = FXFT_ENCODING_BIG5; + break; + case CIDCODING_JIS: + encoding = FXFT_ENCODING_SJIS; + break; + case CIDCODING_KOREA: + encoding = FXFT_ENCODING_JOHAB; + break; + default: + encoding = FXFT_ENCODING_UNICODE; + } + int err = FXFT_Select_Charmap(face, encoding); + if (err) { + err = FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE); + } + if (err && FXFT_Get_Face_Charmaps(face)) { + FXFT_Set_Charmap(face, *FXFT_Get_Face_Charmaps(face)); + } +} + +const struct CIDTransform { + FX_WORD CID; + uint8_t a, b, c, d, e, f; +} g_Japan1_VertCIDs[] = { + {97, 129, 0, 0, 127, 55, 0}, + {7887, 127, 0, 0, 127, 76, 89}, + {7888, 127, 0, 0, 127, 79, 94}, + {7889, 0, 129, 127, 0, 17, 127}, + {7890, 0, 129, 127, 0, 17, 127}, + {7891, 0, 129, 127, 0, 17, 127}, + {7892, 0, 129, 127, 0, 17, 127}, + {7893, 0, 129, 127, 0, 17, 127}, + {7894, 0, 129, 127, 0, 17, 127}, + {7895, 0, 129, 127, 0, 17, 127}, + {7896, 0, 129, 127, 0, 17, 127}, + {7897, 0, 129, 127, 0, 17, 127}, + {7898, 0, 129, 127, 0, 17, 127}, + {7899, 0, 129, 127, 0, 17, 104}, + {7900, 0, 129, 127, 0, 17, 127}, + {7901, 0, 129, 127, 0, 17, 104}, + {7902, 0, 129, 127, 0, 17, 127}, + {7903, 0, 129, 127, 0, 17, 127}, + {7904, 0, 129, 127, 0, 17, 127}, + {7905, 0, 129, 127, 0, 17, 114}, + {7906, 0, 129, 127, 0, 17, 127}, + {7907, 0, 129, 127, 0, 17, 127}, + {7908, 0, 129, 127, 0, 17, 127}, + {7909, 0, 129, 127, 0, 17, 127}, + {7910, 0, 129, 127, 0, 17, 127}, + {7911, 0, 129, 127, 0, 17, 127}, + {7912, 0, 129, 127, 0, 17, 127}, + {7913, 0, 129, 127, 0, 17, 127}, + {7914, 0, 129, 127, 0, 17, 127}, + {7915, 0, 129, 127, 0, 17, 114}, + {7916, 0, 129, 127, 0, 17, 127}, + {7917, 0, 129, 127, 0, 17, 127}, + {7918, 127, 0, 0, 127, 18, 25}, + {7919, 127, 0, 0, 127, 18, 25}, + {7920, 127, 0, 0, 127, 18, 25}, + {7921, 127, 0, 0, 127, 18, 25}, + {7922, 127, 0, 0, 127, 18, 25}, + {7923, 127, 0, 0, 127, 18, 25}, + {7924, 127, 0, 0, 127, 18, 25}, + {7925, 127, 0, 0, 127, 18, 25}, + {7926, 127, 0, 0, 127, 18, 25}, + {7927, 127, 0, 0, 127, 18, 25}, + {7928, 127, 0, 0, 127, 18, 25}, + {7929, 127, 0, 0, 127, 18, 25}, + {7930, 127, 0, 0, 127, 18, 25}, + {7931, 127, 0, 0, 127, 18, 25}, + {7932, 127, 0, 0, 127, 18, 25}, + {7933, 127, 0, 0, 127, 18, 25}, + {7934, 127, 0, 0, 127, 18, 25}, + {7935, 127, 0, 0, 127, 18, 25}, + {7936, 127, 0, 0, 127, 18, 25}, + {7937, 127, 0, 0, 127, 18, 25}, + {7938, 127, 0, 0, 127, 18, 25}, + {7939, 127, 0, 0, 127, 18, 25}, + {8720, 0, 129, 127, 0, 19, 102}, + {8721, 0, 129, 127, 0, 13, 127}, + {8722, 0, 129, 127, 0, 19, 108}, + {8723, 0, 129, 127, 0, 19, 102}, + {8724, 0, 129, 127, 0, 19, 102}, + {8725, 0, 129, 127, 0, 19, 102}, + {8726, 0, 129, 127, 0, 19, 102}, + {8727, 0, 129, 127, 0, 19, 102}, + {8728, 0, 129, 127, 0, 19, 114}, + {8729, 0, 129, 127, 0, 19, 114}, + {8730, 0, 129, 127, 0, 38, 108}, + {8731, 0, 129, 127, 0, 13, 108}, + {8732, 0, 129, 127, 0, 19, 108}, + {8733, 0, 129, 127, 0, 19, 108}, + {8734, 0, 129, 127, 0, 19, 108}, + {8735, 0, 129, 127, 0, 19, 108}, + {8736, 0, 129, 127, 0, 19, 102}, + {8737, 0, 129, 127, 0, 19, 102}, + {8738, 0, 129, 127, 0, 19, 102}, + {8739, 0, 129, 127, 0, 19, 102}, + {8740, 0, 129, 127, 0, 19, 102}, + {8741, 0, 129, 127, 0, 19, 102}, + {8742, 0, 129, 127, 0, 19, 102}, + {8743, 0, 129, 127, 0, 19, 102}, + {8744, 0, 129, 127, 0, 19, 102}, + {8745, 0, 129, 127, 0, 19, 102}, + {8746, 0, 129, 127, 0, 19, 114}, + {8747, 0, 129, 127, 0, 19, 114}, + {8748, 0, 129, 127, 0, 19, 102}, + {8749, 0, 129, 127, 0, 19, 102}, + {8750, 0, 129, 127, 0, 19, 102}, + {8751, 0, 129, 127, 0, 19, 102}, + {8752, 0, 129, 127, 0, 19, 102}, + {8753, 0, 129, 127, 0, 19, 102}, + {8754, 0, 129, 127, 0, 19, 102}, + {8755, 0, 129, 127, 0, 19, 102}, + {8756, 0, 129, 127, 0, 19, 102}, + {8757, 0, 129, 127, 0, 19, 102}, + {8758, 0, 129, 127, 0, 19, 102}, + {8759, 0, 129, 127, 0, 19, 102}, + {8760, 0, 129, 127, 0, 19, 102}, + {8761, 0, 129, 127, 0, 19, 102}, + {8762, 0, 129, 127, 0, 19, 102}, + {8763, 0, 129, 127, 0, 19, 102}, + {8764, 0, 129, 127, 0, 19, 102}, + {8765, 0, 129, 127, 0, 19, 102}, + {8766, 0, 129, 127, 0, 19, 102}, + {8767, 0, 129, 127, 0, 19, 102}, + {8768, 0, 129, 127, 0, 19, 102}, + {8769, 0, 129, 127, 0, 19, 102}, + {8770, 0, 129, 127, 0, 19, 102}, + {8771, 0, 129, 127, 0, 19, 102}, + {8772, 0, 129, 127, 0, 19, 102}, + {8773, 0, 129, 127, 0, 19, 102}, + {8774, 0, 129, 127, 0, 19, 102}, + {8775, 0, 129, 127, 0, 19, 102}, + {8776, 0, 129, 127, 0, 19, 102}, + {8777, 0, 129, 127, 0, 19, 102}, + {8778, 0, 129, 127, 0, 19, 102}, + {8779, 0, 129, 127, 0, 19, 114}, + {8780, 0, 129, 127, 0, 19, 108}, + {8781, 0, 129, 127, 0, 19, 114}, + {8782, 0, 129, 127, 0, 13, 114}, + {8783, 0, 129, 127, 0, 19, 108}, + {8784, 0, 129, 127, 0, 13, 114}, + {8785, 0, 129, 127, 0, 19, 108}, + {8786, 0, 129, 127, 0, 19, 108}, + {8787, 0, 129, 127, 0, 19, 108}, + {8788, 0, 129, 127, 0, 19, 108}, + {8789, 0, 129, 127, 0, 19, 108}, + {8790, 0, 129, 127, 0, 19, 108}, + {8791, 0, 129, 127, 0, 19, 108}, + {8792, 0, 129, 127, 0, 19, 108}, + {8793, 0, 129, 127, 0, 19, 108}, + {8794, 0, 129, 127, 0, 19, 108}, + {8795, 0, 129, 127, 0, 19, 108}, + {8796, 0, 129, 127, 0, 19, 108}, + {8797, 0, 129, 127, 0, 19, 108}, + {8798, 0, 129, 127, 0, 19, 108}, + {8799, 0, 129, 127, 0, 19, 108}, + {8800, 0, 129, 127, 0, 19, 108}, + {8801, 0, 129, 127, 0, 19, 108}, + {8802, 0, 129, 127, 0, 19, 108}, + {8803, 0, 129, 127, 0, 19, 108}, + {8804, 0, 129, 127, 0, 19, 108}, + {8805, 0, 129, 127, 0, 19, 108}, + {8806, 0, 129, 127, 0, 19, 108}, + {8807, 0, 129, 127, 0, 19, 108}, + {8808, 0, 129, 127, 0, 19, 108}, + {8809, 0, 129, 127, 0, 19, 108}, + {8810, 0, 129, 127, 0, 19, 108}, + {8811, 0, 129, 127, 0, 19, 114}, + {8812, 0, 129, 127, 0, 19, 102}, + {8813, 0, 129, 127, 0, 19, 114}, + {8814, 0, 129, 127, 0, 76, 102}, + {8815, 0, 129, 127, 0, 13, 121}, + {8816, 0, 129, 127, 0, 19, 114}, + {8817, 0, 129, 127, 0, 19, 127}, + {8818, 0, 129, 127, 0, 19, 114}, + {8819, 0, 129, 127, 0, 218, 108}, +}; + +} // namespace + +CPDF_CMapManager::CPDF_CMapManager() { + m_bPrompted = FALSE; + FXSYS_memset(m_CID2UnicodeMaps, 0, sizeof m_CID2UnicodeMaps); +} +CPDF_CMapManager::~CPDF_CMapManager() { + for (const auto& pair : m_CMaps) { + delete pair.second; + } + m_CMaps.clear(); + for (size_t i = 0; i < FX_ArraySize(m_CID2UnicodeMaps); ++i) { + delete m_CID2UnicodeMaps[i]; + } +} +CPDF_CMap* CPDF_CMapManager::GetPredefinedCMap(const CFX_ByteString& name, + FX_BOOL bPromptCJK) { + auto it = m_CMaps.find(name); + if (it != m_CMaps.end()) { + return it->second; + } + CPDF_CMap* pCMap = LoadPredefinedCMap(name, bPromptCJK); + if (!name.IsEmpty()) { + m_CMaps[name] = pCMap; + } + return pCMap; +} +CPDF_CMap* CPDF_CMapManager::LoadPredefinedCMap(const CFX_ByteString& name, + FX_BOOL bPromptCJK) { + CPDF_CMap* pCMap = new CPDF_CMap; + const FX_CHAR* pname = name; + if (*pname == '/') { + pname++; + } + pCMap->LoadPredefined(this, pname, bPromptCJK); + return pCMap; +} + +void CPDF_CMapManager::ReloadAll() { + for (const auto& pair : m_CMaps) { + CPDF_CMap* pCMap = pair.second; + pCMap->LoadPredefined(this, pair.first, FALSE); + } + for (size_t i = 0; i < FX_ArraySize(m_CID2UnicodeMaps); ++i) { + if (CPDF_CID2UnicodeMap* pMap = m_CID2UnicodeMaps[i]) { + pMap->Load(this, CIDSetFromSizeT(i), FALSE); + } + } +} +CPDF_CID2UnicodeMap* CPDF_CMapManager::GetCID2UnicodeMap(CIDSet charset, + FX_BOOL bPromptCJK) { + if (!m_CID2UnicodeMaps[charset]) + m_CID2UnicodeMaps[charset] = LoadCID2UnicodeMap(charset, bPromptCJK); + return m_CID2UnicodeMaps[charset]; +} +CPDF_CID2UnicodeMap* CPDF_CMapManager::LoadCID2UnicodeMap(CIDSet charset, + FX_BOOL bPromptCJK) { + CPDF_CID2UnicodeMap* pMap = new CPDF_CID2UnicodeMap(); + if (!pMap->Initialize()) { + delete pMap; + return NULL; + } + pMap->Load(this, charset, bPromptCJK); + return pMap; +} +CPDF_CMapParser::CPDF_CMapParser() { + m_pCMap = NULL; + m_Status = 0; + m_CodeSeq = 0; +} +FX_BOOL CPDF_CMapParser::Initialize(CPDF_CMap* pCMap) { + m_pCMap = pCMap; + m_Status = 0; + m_CodeSeq = 0; + m_AddMaps.EstimateSize(0, 10240); + return TRUE; +} + +void CPDF_CMapParser::ParseWord(const CFX_ByteStringC& word) { + if (word.IsEmpty()) { + return; + } + if (word == FX_BSTRC("begincidchar")) { + m_Status = 1; + m_CodeSeq = 0; + } else if (word == FX_BSTRC("begincidrange")) { + m_Status = 2; + m_CodeSeq = 0; + } else if (word == FX_BSTRC("endcidrange") || + word == FX_BSTRC("endcidchar")) { + m_Status = 0; + } else if (word == FX_BSTRC("/WMode")) { + m_Status = 6; + } else if (word == FX_BSTRC("/Registry")) { + m_Status = 3; + } else if (word == FX_BSTRC("/Ordering")) { + m_Status = 4; + } else if (word == FX_BSTRC("/Supplement")) { + m_Status = 5; + } else if (word == FX_BSTRC("begincodespacerange")) { + m_Status = 7; + m_CodeSeq = 0; + } else if (word == FX_BSTRC("usecmap")) { + } else if (m_Status == 1 || m_Status == 2) { + m_CodePoints[m_CodeSeq] = CMap_GetCode(word); + m_CodeSeq++; + FX_DWORD StartCode, EndCode; + FX_WORD StartCID; + if (m_Status == 1) { + if (m_CodeSeq < 2) { + return; + } + EndCode = StartCode = m_CodePoints[0]; + StartCID = (FX_WORD)m_CodePoints[1]; + } else { + if (m_CodeSeq < 3) { + return; + } + StartCode = m_CodePoints[0]; + EndCode = m_CodePoints[1]; + StartCID = (FX_WORD)m_CodePoints[2]; + } + if (EndCode < 0x10000) { + for (FX_DWORD code = StartCode; code <= EndCode; code++) { + m_pCMap->m_pMapping[code] = (FX_WORD)(StartCID + code - StartCode); + } + } else { + FX_DWORD buf[2]; + buf[0] = StartCode; + buf[1] = ((EndCode - StartCode) << 16) + StartCID; + m_AddMaps.AppendBlock(buf, sizeof buf); + } + m_CodeSeq = 0; + } else if (m_Status == 3) { + CMap_GetString(word); + m_Status = 0; + } else if (m_Status == 4) { + m_pCMap->m_Charset = CharsetFromOrdering(CMap_GetString(word)); + m_Status = 0; + } else if (m_Status == 5) { + CMap_GetCode(word); + m_Status = 0; + } else if (m_Status == 6) { + m_pCMap->m_bVertical = CMap_GetCode(word); + m_Status = 0; + } else if (m_Status == 7) { + if (word == FX_BSTRC("endcodespacerange")) { + int nSegs = m_CodeRanges.GetSize(); + if (nSegs > 1) { + m_pCMap->m_CodingScheme = CPDF_CMap::MixedFourBytes; + m_pCMap->m_nCodeRanges = nSegs; + m_pCMap->m_pLeadingBytes = + FX_Alloc2D(uint8_t, nSegs, sizeof(CMap_CodeRange)); + FXSYS_memcpy(m_pCMap->m_pLeadingBytes, m_CodeRanges.GetData(), + nSegs * sizeof(CMap_CodeRange)); + } else if (nSegs == 1) { + m_pCMap->m_CodingScheme = (m_CodeRanges[0].m_CharSize == 2) + ? CPDF_CMap::TwoBytes + : CPDF_CMap::OneByte; + } + m_Status = 0; + } else { + if (word.GetLength() == 0 || word.GetAt(0) != '<') { + return; + } + if (m_CodeSeq % 2) { + CMap_CodeRange range; + if (CMap_GetCodeRange(range, m_LastWord, word)) { + m_CodeRanges.Add(range); + } + } + m_CodeSeq++; + } + } + m_LastWord = word; +} +CPDF_CMap::CPDF_CMap() { + m_Charset = CIDSET_UNKNOWN; + m_Coding = CIDCODING_UNKNOWN; + m_CodingScheme = TwoBytes; + m_bVertical = 0; + m_bLoaded = FALSE; + m_pMapping = NULL; + m_pLeadingBytes = NULL; + m_pAddMapping = NULL; + m_pEmbedMap = NULL; + m_pUseMap = NULL; + m_nCodeRanges = 0; +} +CPDF_CMap::~CPDF_CMap() { + FX_Free(m_pMapping); + FX_Free(m_pAddMapping); + FX_Free(m_pLeadingBytes); + delete m_pUseMap; +} +void CPDF_CMap::Release() { + if (m_PredefinedCMap.IsEmpty()) { + delete this; + } +} FX_BOOL CPDF_CMap::LoadPredefined(CPDF_CMapManager* pMgr, const FX_CHAR* pName, @@ -447,25 +805,24 @@ FX_BOOL CPDF_CMap::LoadPredefined(CPDF_CMapManager* pMgr, if (cmapid.GetLength() > 2) { cmapid = cmapid.Left(cmapid.GetLength() - 2); } - int index = 0; - while (1) { - if (g_PredefinedCMaps[index].m_pName == NULL) { - return FALSE; - } - if (cmapid == CFX_ByteStringC(g_PredefinedCMaps[index].m_pName)) { + const CPDF_PredefinedCMap* map = nullptr; + for (size_t i = 0; i < FX_ArraySize(g_PredefinedCMaps); ++i) { + if (cmapid == CFX_ByteStringC(g_PredefinedCMaps[i].m_pName)) { + map = &g_PredefinedCMaps[i]; break; } - index++; } - const CPDF_PredefinedCMap& map = g_PredefinedCMaps[index]; - m_Charset = map.m_Charset; - m_Coding = map.m_Coding; - m_CodingScheme = map.m_CodingScheme; + if (!map) + return FALSE; + + m_Charset = map->m_Charset; + m_Coding = map->m_Coding; + m_CodingScheme = map->m_CodingScheme; if (m_CodingScheme == MixedTwoBytes) { m_pLeadingBytes = FX_Alloc(uint8_t, 256); - for (FX_DWORD i = 0; i < map.m_LeadingSegCount; i++) { - for (int b = map.m_LeadingSegs[i * 2]; b <= map.m_LeadingSegs[i * 2 + 1]; - b++) { + for (FX_DWORD i = 0; i < map->m_LeadingSegCount; ++i) { + const uint8_t* segs = map->m_LeadingSegs; + for (int b = segs[i * 2]; b <= segs[i * 2 + 1]; ++b) { m_pLeadingBytes[b] = 1; } } @@ -477,11 +834,6 @@ FX_BOOL CPDF_CMap::LoadPredefined(CPDF_CMapManager* pMgr, } return FALSE; } -extern "C" { -static int compare_dword(const void* data1, const void* data2) { - return (*(FX_DWORD*)data1) - (*(FX_DWORD*)data2); -} -}; FX_BOOL CPDF_CMap::LoadEmbedded(const uint8_t* pData, FX_DWORD size) { m_pMapping = FX_Alloc(FX_WORD, 65536); CPDF_CMapParser parser; @@ -500,22 +852,11 @@ FX_BOOL CPDF_CMap::LoadEmbedded(const uint8_t* pData, FX_DWORD size) { FXSYS_memcpy(m_pAddMapping + 4, parser.m_AddMaps.GetBuffer(), parser.m_AddMaps.GetSize()); FXSYS_qsort(m_pAddMapping + 4, parser.m_AddMaps.GetSize() / 8, 8, - compare_dword); + CompareDWORD); } return TRUE; } -extern "C" { -static int compareCID(const void* key, const void* element) { - if ((*(FX_DWORD*)key) < (*(FX_DWORD*)element)) { - return -1; - } - if ((*(FX_DWORD*)key) > - (*(FX_DWORD*)element) + ((FX_DWORD*)element)[1] / 65536) { - return 1; - } - return 0; -} -}; + FX_WORD CPDF_CMap::CIDFromCharCode(FX_DWORD charcode) const { if (m_Coding == CIDCODING_CID) { return (FX_WORD)charcode; @@ -529,7 +870,7 @@ FX_WORD CPDF_CMap::CIDFromCharCode(FX_DWORD charcode) const { if (charcode >> 16) { if (m_pAddMapping) { void* found = FXSYS_bsearch(&charcode, m_pAddMapping + 4, - *(FX_DWORD*)m_pAddMapping, 8, compareCID); + *(FX_DWORD*)m_pAddMapping, 8, CompareCID); if (found == NULL) { if (m_pUseMap) { return m_pUseMap->CIDFromCharCode(charcode); @@ -539,48 +880,16 @@ FX_WORD CPDF_CMap::CIDFromCharCode(FX_DWORD charcode) const { return (FX_WORD)(((FX_DWORD*)found)[1] % 65536 + charcode - *(FX_DWORD*)found); } - if (m_pUseMap) { + if (m_pUseMap) return m_pUseMap->CIDFromCharCode(charcode); - } return 0; } FX_DWORD CID = m_pMapping[charcode]; - if (!CID && m_pUseMap) { + if (!CID && m_pUseMap) return m_pUseMap->CIDFromCharCode(charcode); - } return (FX_WORD)CID; } -static int _CheckCodeRange(uint8_t* codes, - int size, - _CMap_CodeRange* pRanges, - int nRanges) { - int iSeg = nRanges - 1; - while (iSeg >= 0) { - if (pRanges[iSeg].m_CharSize < size) { - iSeg--; - continue; - } - int iChar = 0; - while (iChar < size) { - if (codes[iChar] < pRanges[iSeg].m_Lower[iChar] || - codes[iChar] > pRanges[iSeg].m_Upper[iChar]) { - break; - } - iChar++; - } - if (iChar == pRanges[iSeg].m_CharSize) { - return 2; - } - if (iChar) { - if (size == pRanges[iSeg].m_CharSize) { - return 2; - } - return 1; - } - iSeg--; - } - return 0; -} + FX_DWORD CPDF_CMap::GetNextChar(const FX_CHAR* pString, int nStrLen, int& offset) const { @@ -603,9 +912,9 @@ FX_DWORD CPDF_CMap::GetNextChar(const FX_CHAR* pString, uint8_t codes[4]; int char_size = 1; codes[0] = ((uint8_t*)pString)[offset++]; - _CMap_CodeRange* pRanges = (_CMap_CodeRange*)m_pLeadingBytes; + CMap_CodeRange* pRanges = (CMap_CodeRange*)m_pLeadingBytes; while (1) { - int ret = _CheckCodeRange(codes, char_size, pRanges, m_nCodeRanges); + int ret = CheckCodeRange(codes, char_size, pRanges, m_nCodeRanges); if (ret == 0) { return 0; } @@ -674,40 +983,7 @@ int CPDF_CMap::CountChar(const FX_CHAR* pString, int size) const { } return size; } -int _GetCharSize(FX_DWORD charcode, _CMap_CodeRange* pRanges, int iRangesSize) { - if (!iRangesSize) { - return 1; - } - uint8_t codes[4]; - codes[0] = codes[1] = 0x00; - codes[2] = (uint8_t)(charcode >> 8 & 0xFF); - codes[3] = (uint8_t)charcode; - int offset = 0, size = 4; - for (int i = 0; i < 4; ++i) { - int iSeg = iRangesSize - 1; - while (iSeg >= 0) { - if (pRanges[iSeg].m_CharSize < size) { - iSeg--; - continue; - } - int iChar = 0; - while (iChar < size) { - if (codes[offset + iChar] < pRanges[iSeg].m_Lower[iChar] || - codes[offset + iChar] > pRanges[iSeg].m_Upper[iChar]) { - break; - } - iChar++; - } - if (iChar == pRanges[iSeg].m_CharSize) { - return size; - } - iSeg--; - } - size--; - offset++; - } - return 1; -} + int CPDF_CMap::AppendChar(FX_CHAR* str, FX_DWORD charcode) const { switch (m_CodingScheme) { case OneByte: @@ -720,8 +996,8 @@ int CPDF_CMap::AppendChar(FX_CHAR* str, FX_DWORD charcode) const { case MixedTwoBytes: case MixedFourBytes: if (charcode < 0x100) { - _CMap_CodeRange* pRanges = (_CMap_CodeRange*)m_pLeadingBytes; - int iSize = _GetCharSize(charcode, pRanges, m_nCodeRanges); + CMap_CodeRange* pRanges = (CMap_CodeRange*)m_pLeadingBytes; + int iSize = GetCharSizeImpl(charcode, pRanges, m_nCodeRanges); if (iSize == 0) { iSize = 1; } @@ -769,15 +1045,14 @@ FX_WCHAR CPDF_CID2UnicodeMap::UnicodeFromCID(FX_WORD CID) { } return 0; } -void FPDFAPI_LoadCID2UnicodeMap(int charset, - const FX_WORD*& pMap, - FX_DWORD& count); + void CPDF_CID2UnicodeMap::Load(CPDF_CMapManager* pMgr, - int charset, + CIDSet charset, FX_BOOL bPromptCJK) { m_Charset = charset; FPDFAPI_LoadCID2UnicodeMap(charset, m_pEmbeddedMap, m_EmbeddedCount); } + #include "ttgsubtable.h" CPDF_CIDFont::CPDF_CIDFont() : CPDF_Font(PDFFONT_CIDFONT) { m_pCMap = NULL; @@ -808,54 +1083,6 @@ FX_BOOL CPDF_CIDFont::IsVertWriting() const { return m_pCMap ? m_pCMap->IsVertWriting() : FALSE; } -#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ -static FX_DWORD _EmbeddedCharcodeFromUnicode(const FXCMAP_CMap* pEmbedMap, - int charset, - FX_WCHAR unicode) { - if (charset <= 0 || charset > 4) { - return 0; - } - CPDF_FontGlobals* pFontGlobals = - CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals(); - const FX_WORD* pCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_pMap; - if (pCodes == NULL) { - return 0; - } - int nCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_Count; - for (int i = 0; i < nCodes; i++) { - if (pCodes[i] == unicode) { - FX_DWORD CharCode = FPDFAPI_CharCodeFromCID(pEmbedMap, i); - if (CharCode == 0) { - continue; - } - return CharCode; - } - } - return 0; -} -#endif // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ - -static FX_WCHAR _EmbeddedUnicodeFromCharcode(const FXCMAP_CMap* pEmbedMap, - int charset, - FX_DWORD charcode) { - if (charset <= 0 || charset > 4) { - return 0; - } - FX_WORD cid = FPDFAPI_CIDFromCharCode(pEmbedMap, charcode); - if (cid == 0) { - return 0; - } - CPDF_FontGlobals* pFontGlobals = - CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals(); - const FX_WORD* pCodes = pFontGlobals->m_EmbeddedToUnicodes[charset].m_pMap; - if (pCodes == NULL) { - return 0; - } - if (cid < pFontGlobals->m_EmbeddedToUnicodes[charset].m_Count) { - return pCodes[cid]; - } - return 0; -} FX_WCHAR CPDF_CIDFont::_UnicodeFromCharCode(FX_DWORD charcode) const { switch (m_pCMap->m_Coding) { case CIDCODING_UCS2: @@ -885,8 +1112,8 @@ FX_WCHAR CPDF_CIDFont::_UnicodeFromCharCode(FX_DWORD charcode) const { return unicode; #endif if (m_pCMap->m_pEmbedMap) { - return _EmbeddedUnicodeFromCharcode(m_pCMap->m_pEmbedMap, - m_pCMap->m_Charset, charcode); + return EmbeddedUnicodeFromCharcode(m_pCMap->m_pEmbedMap, + m_pCMap->m_Charset, charcode); } return 0; } @@ -934,38 +1161,13 @@ FX_DWORD CPDF_CIDFont::_CharCodeFromUnicode(FX_WCHAR unicode) const { } #else if (m_pCMap->m_pEmbedMap) { - return _EmbeddedCharcodeFromUnicode(m_pCMap->m_pEmbedMap, - m_pCMap->m_Charset, unicode); + return EmbeddedCharcodeFromUnicode(m_pCMap->m_pEmbedMap, m_pCMap->m_Charset, + unicode); } #endif return 0; } -static void FT_UseCIDCharmap(FXFT_Face face, int coding) { - int encoding; - switch (coding) { - case CIDCODING_GB: - encoding = FXFT_ENCODING_GB2312; - break; - case CIDCODING_BIG5: - encoding = FXFT_ENCODING_BIG5; - break; - case CIDCODING_JIS: - encoding = FXFT_ENCODING_SJIS; - break; - case CIDCODING_KOREA: - encoding = FXFT_ENCODING_JOHAB; - break; - default: - encoding = FXFT_ENCODING_UNICODE; - } - int err = FXFT_Select_Charmap(face, encoding); - if (err) { - err = FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE); - } - if (err && FXFT_Get_Face_Charmaps(face)) { - FXFT_Set_Charmap(face, *FXFT_Get_Face_Charmaps(face)); - } -} + FX_BOOL CPDF_CIDFont::_Load() { if (m_pFontDict->GetString(FX_BSTRC("Subtype")) == FX_BSTRC("TrueType")) { return LoadGB2312(); @@ -1028,7 +1230,7 @@ FX_BOOL CPDF_CIDFont::_Load() { pCIDFontDict->GetDict(FX_BSTRC("CIDSystemInfo")); if (pCIDInfo) { m_Charset = - _CharsetFromOrdering(pCIDInfo->GetString(FX_BSTRC("Ordering"))); + CharsetFromOrdering(pCIDInfo->GetString(FX_BSTRC("Ordering"))); } } if (m_Charset != CIDSET_UNKNOWN) @@ -1092,12 +1294,7 @@ FX_BOOL CPDF_CIDFont::_Load() { } return TRUE; } -FX_FLOAT _CIDTransformToFloat(uint8_t ch) { - if (ch < 128) { - return ch * 1.0f / 127; - } - return (-255 + ch) * 1.0f / 127; -} + void CPDF_CIDFont::GetCharBBox(FX_DWORD charcode, FX_RECT& rect, int level) { if (charcode < 256 && m_CharBBox[charcode].Right != -1) { rect.bottom = m_CharBBox[charcode].Bottom; @@ -1163,12 +1360,12 @@ void CPDF_CIDFont::GetCharBBox(FX_DWORD charcode, FX_RECT& rect, int level) { FX_WORD CID = CIDFromCharCode(charcode); const uint8_t* pTransform = GetCIDTransform(CID); if (pTransform && !bVert) { - CFX_AffineMatrix matrix(_CIDTransformToFloat(pTransform[0]), - _CIDTransformToFloat(pTransform[1]), - _CIDTransformToFloat(pTransform[2]), - _CIDTransformToFloat(pTransform[3]), - _CIDTransformToFloat(pTransform[4]) * 1000, - _CIDTransformToFloat(pTransform[5]) * 1000); + CFX_AffineMatrix matrix(CIDTransformToFloat(pTransform[0]), + CIDTransformToFloat(pTransform[1]), + CIDTransformToFloat(pTransform[2]), + CIDTransformToFloat(pTransform[3]), + CIDTransformToFloat(pTransform[4]) * 1000, + CIDTransformToFloat(pTransform[5]) * 1000); CFX_FloatRect rect_f(rect); rect_f.Transform(&matrix); rect = rect_f.GetOutterRect(); @@ -1504,6 +1701,15 @@ void CPDF_CIDFont::LoadMetricsArray(CPDF_Array* pArray, } } } + +// static +FX_FLOAT CPDF_CIDFont::CIDTransformToFloat(uint8_t ch) { + if (ch < 128) { + return ch * 1.0f / 127; + } + return (-255 + ch) * 1.0f / 127; +} + FX_BOOL CPDF_CIDFont::LoadGB2312() { m_BaseFont = m_pFontDict->GetString(FX_BSTRC("BaseFont")); CPDF_Dictionary* pFontDesc = m_pFontDict->GetDict(FX_BSTRC("FontDescriptor")); @@ -1531,104 +1737,23 @@ FX_BOOL CPDF_CIDFont::LoadGB2312() { } return TRUE; } -const struct _CIDTransform { - FX_WORD CID; - uint8_t a, b, c, d, e, f; -} Japan1_VertCIDs[] = { - {97, 129, 0, 0, 127, 55, 0}, {7887, 127, 0, 0, 127, 76, 89}, - {7888, 127, 0, 0, 127, 79, 94}, {7889, 0, 129, 127, 0, 17, 127}, - {7890, 0, 129, 127, 0, 17, 127}, {7891, 0, 129, 127, 0, 17, 127}, - {7892, 0, 129, 127, 0, 17, 127}, {7893, 0, 129, 127, 0, 17, 127}, - {7894, 0, 129, 127, 0, 17, 127}, {7895, 0, 129, 127, 0, 17, 127}, - {7896, 0, 129, 127, 0, 17, 127}, {7897, 0, 129, 127, 0, 17, 127}, - {7898, 0, 129, 127, 0, 17, 127}, {7899, 0, 129, 127, 0, 17, 104}, - {7900, 0, 129, 127, 0, 17, 127}, {7901, 0, 129, 127, 0, 17, 104}, - {7902, 0, 129, 127, 0, 17, 127}, {7903, 0, 129, 127, 0, 17, 127}, - {7904, 0, 129, 127, 0, 17, 127}, {7905, 0, 129, 127, 0, 17, 114}, - {7906, 0, 129, 127, 0, 17, 127}, {7907, 0, 129, 127, 0, 17, 127}, - {7908, 0, 129, 127, 0, 17, 127}, {7909, 0, 129, 127, 0, 17, 127}, - {7910, 0, 129, 127, 0, 17, 127}, {7911, 0, 129, 127, 0, 17, 127}, - {7912, 0, 129, 127, 0, 17, 127}, {7913, 0, 129, 127, 0, 17, 127}, - {7914, 0, 129, 127, 0, 17, 127}, {7915, 0, 129, 127, 0, 17, 114}, - {7916, 0, 129, 127, 0, 17, 127}, {7917, 0, 129, 127, 0, 17, 127}, - {7918, 127, 0, 0, 127, 18, 25}, {7919, 127, 0, 0, 127, 18, 25}, - {7920, 127, 0, 0, 127, 18, 25}, {7921, 127, 0, 0, 127, 18, 25}, - {7922, 127, 0, 0, 127, 18, 25}, {7923, 127, 0, 0, 127, 18, 25}, - {7924, 127, 0, 0, 127, 18, 25}, {7925, 127, 0, 0, 127, 18, 25}, - {7926, 127, 0, 0, 127, 18, 25}, {7927, 127, 0, 0, 127, 18, 25}, - {7928, 127, 0, 0, 127, 18, 25}, {7929, 127, 0, 0, 127, 18, 25}, - {7930, 127, 0, 0, 127, 18, 25}, {7931, 127, 0, 0, 127, 18, 25}, - {7932, 127, 0, 0, 127, 18, 25}, {7933, 127, 0, 0, 127, 18, 25}, - {7934, 127, 0, 0, 127, 18, 25}, {7935, 127, 0, 0, 127, 18, 25}, - {7936, 127, 0, 0, 127, 18, 25}, {7937, 127, 0, 0, 127, 18, 25}, - {7938, 127, 0, 0, 127, 18, 25}, {7939, 127, 0, 0, 127, 18, 25}, - {8720, 0, 129, 127, 0, 19, 102}, {8721, 0, 129, 127, 0, 13, 127}, - {8722, 0, 129, 127, 0, 19, 108}, {8723, 0, 129, 127, 0, 19, 102}, - {8724, 0, 129, 127, 0, 19, 102}, {8725, 0, 129, 127, 0, 19, 102}, - {8726, 0, 129, 127, 0, 19, 102}, {8727, 0, 129, 127, 0, 19, 102}, - {8728, 0, 129, 127, 0, 19, 114}, {8729, 0, 129, 127, 0, 19, 114}, - {8730, 0, 129, 127, 0, 38, 108}, {8731, 0, 129, 127, 0, 13, 108}, - {8732, 0, 129, 127, 0, 19, 108}, {8733, 0, 129, 127, 0, 19, 108}, - {8734, 0, 129, 127, 0, 19, 108}, {8735, 0, 129, 127, 0, 19, 108}, - {8736, 0, 129, 127, 0, 19, 102}, {8737, 0, 129, 127, 0, 19, 102}, - {8738, 0, 129, 127, 0, 19, 102}, {8739, 0, 129, 127, 0, 19, 102}, - {8740, 0, 129, 127, 0, 19, 102}, {8741, 0, 129, 127, 0, 19, 102}, - {8742, 0, 129, 127, 0, 19, 102}, {8743, 0, 129, 127, 0, 19, 102}, - {8744, 0, 129, 127, 0, 19, 102}, {8745, 0, 129, 127, 0, 19, 102}, - {8746, 0, 129, 127, 0, 19, 114}, {8747, 0, 129, 127, 0, 19, 114}, - {8748, 0, 129, 127, 0, 19, 102}, {8749, 0, 129, 127, 0, 19, 102}, - {8750, 0, 129, 127, 0, 19, 102}, {8751, 0, 129, 127, 0, 19, 102}, - {8752, 0, 129, 127, 0, 19, 102}, {8753, 0, 129, 127, 0, 19, 102}, - {8754, 0, 129, 127, 0, 19, 102}, {8755, 0, 129, 127, 0, 19, 102}, - {8756, 0, 129, 127, 0, 19, 102}, {8757, 0, 129, 127, 0, 19, 102}, - {8758, 0, 129, 127, 0, 19, 102}, {8759, 0, 129, 127, 0, 19, 102}, - {8760, 0, 129, 127, 0, 19, 102}, {8761, 0, 129, 127, 0, 19, 102}, - {8762, 0, 129, 127, 0, 19, 102}, {8763, 0, 129, 127, 0, 19, 102}, - {8764, 0, 129, 127, 0, 19, 102}, {8765, 0, 129, 127, 0, 19, 102}, - {8766, 0, 129, 127, 0, 19, 102}, {8767, 0, 129, 127, 0, 19, 102}, - {8768, 0, 129, 127, 0, 19, 102}, {8769, 0, 129, 127, 0, 19, 102}, - {8770, 0, 129, 127, 0, 19, 102}, {8771, 0, 129, 127, 0, 19, 102}, - {8772, 0, 129, 127, 0, 19, 102}, {8773, 0, 129, 127, 0, 19, 102}, - {8774, 0, 129, 127, 0, 19, 102}, {8775, 0, 129, 127, 0, 19, 102}, - {8776, 0, 129, 127, 0, 19, 102}, {8777, 0, 129, 127, 0, 19, 102}, - {8778, 0, 129, 127, 0, 19, 102}, {8779, 0, 129, 127, 0, 19, 114}, - {8780, 0, 129, 127, 0, 19, 108}, {8781, 0, 129, 127, 0, 19, 114}, - {8782, 0, 129, 127, 0, 13, 114}, {8783, 0, 129, 127, 0, 19, 108}, - {8784, 0, 129, 127, 0, 13, 114}, {8785, 0, 129, 127, 0, 19, 108}, - {8786, 0, 129, 127, 0, 19, 108}, {8787, 0, 129, 127, 0, 19, 108}, - {8788, 0, 129, 127, 0, 19, 108}, {8789, 0, 129, 127, 0, 19, 108}, - {8790, 0, 129, 127, 0, 19, 108}, {8791, 0, 129, 127, 0, 19, 108}, - {8792, 0, 129, 127, 0, 19, 108}, {8793, 0, 129, 127, 0, 19, 108}, - {8794, 0, 129, 127, 0, 19, 108}, {8795, 0, 129, 127, 0, 19, 108}, - {8796, 0, 129, 127, 0, 19, 108}, {8797, 0, 129, 127, 0, 19, 108}, - {8798, 0, 129, 127, 0, 19, 108}, {8799, 0, 129, 127, 0, 19, 108}, - {8800, 0, 129, 127, 0, 19, 108}, {8801, 0, 129, 127, 0, 19, 108}, - {8802, 0, 129, 127, 0, 19, 108}, {8803, 0, 129, 127, 0, 19, 108}, - {8804, 0, 129, 127, 0, 19, 108}, {8805, 0, 129, 127, 0, 19, 108}, - {8806, 0, 129, 127, 0, 19, 108}, {8807, 0, 129, 127, 0, 19, 108}, - {8808, 0, 129, 127, 0, 19, 108}, {8809, 0, 129, 127, 0, 19, 108}, - {8810, 0, 129, 127, 0, 19, 108}, {8811, 0, 129, 127, 0, 19, 114}, - {8812, 0, 129, 127, 0, 19, 102}, {8813, 0, 129, 127, 0, 19, 114}, - {8814, 0, 129, 127, 0, 76, 102}, {8815, 0, 129, 127, 0, 13, 121}, - {8816, 0, 129, 127, 0, 19, 114}, {8817, 0, 129, 127, 0, 19, 127}, - {8818, 0, 129, 127, 0, 19, 114}, {8819, 0, 129, 127, 0, 218, 108}, -}; + const uint8_t* CPDF_CIDFont::GetCIDTransform(FX_WORD CID) const { - if (m_Charset != CIDSET_JAPAN1 || m_pFontFile != NULL) { - return NULL; - } - int begin = 0; - int end = sizeof Japan1_VertCIDs / sizeof(struct _CIDTransform) - 1; + if (m_Charset != CIDSET_JAPAN1 || m_pFontFile) + return nullptr; + + size_t begin = 0; + size_t end = FX_ArraySize(g_Japan1_VertCIDs) - 1; while (begin <= end) { - int middle = (begin + end) / 2; - FX_WORD middlecode = Japan1_VertCIDs[middle].CID; + size_t middle = (begin + end) / 2; + FX_WORD middlecode = g_Japan1_VertCIDs[middle].CID; if (middlecode > CID) { end = middle - 1; } else if (middlecode < CID) { begin = middle + 1; } else { - return &Japan1_VertCIDs[middle].a; + return &g_Japan1_VertCIDs[middle].a; } } - return NULL; + return nullptr; } diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp index 4e5ef1c898..127fb93250 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp @@ -71,7 +71,7 @@ CPDF_StreamContentParser::CPDF_StreamContentParser( m_pCurStates->m_TextState.New(); m_pCurStates->m_ColorState.New(); } - for (int i = 0; i < FX_ArraySize(m_Type3Data); ++i) { + for (size_t i = 0; i < FX_ArraySize(m_Type3Data); ++i) { m_Type3Data[i] = 0.0; } } diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp index 1e7f077d48..c3029e0091 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp @@ -738,7 +738,7 @@ void CPDF_DIBSource::LoadJpxBitmap() { } m_pCachedBitmap->Clear(0xFFFFFFFF); std::vector output_offsets(components); - for (int i = 0; i < components; ++i) + for (FX_DWORD i = 0; i < components; ++i) output_offsets[i] = i; if (bSwapRGB) { output_offsets[0] = 2; @@ -1484,7 +1484,7 @@ void CPDF_DIBSource::DownSampleScanline32Bit(int orig_Bpp, bTransMask); } } else { - for (int j = 0; j < m_nComponents; ++j) { + for (FX_DWORD j = 0; j < m_nComponents; ++j) { int color_value = (int)((m_pCompData[j].m_DecodeMin + m_pCompData[j].m_DecodeStep * (FX_FLOAT)pSrcPixel[j]) * diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp index 054dabc8ed..89ac8ac143 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp @@ -525,7 +525,7 @@ class CPDF_CharPosList { FXTEXT_CHARPOS* m_pCharPos; FX_DWORD m_nChars; }; -FX_FLOAT _CIDTransformToFloat(uint8_t ch); + CPDF_CharPosList::CPDF_CharPosList() { m_pCharPos = NULL; } @@ -578,12 +578,14 @@ void CPDF_CharPosList::Load(int nChars, } const uint8_t* pTransform = pCIDFont->GetCIDTransform(CID); if (pTransform && !bVert) { - charpos.m_AdjustMatrix[0] = _CIDTransformToFloat(pTransform[0]); - charpos.m_AdjustMatrix[2] = _CIDTransformToFloat(pTransform[2]); - charpos.m_AdjustMatrix[1] = _CIDTransformToFloat(pTransform[1]); - charpos.m_AdjustMatrix[3] = _CIDTransformToFloat(pTransform[3]); - charpos.m_OriginX += _CIDTransformToFloat(pTransform[4]) * FontSize; - charpos.m_OriginY += _CIDTransformToFloat(pTransform[5]) * FontSize; + charpos.m_AdjustMatrix[0] = pCIDFont->CIDTransformToFloat(pTransform[0]); + charpos.m_AdjustMatrix[2] = pCIDFont->CIDTransformToFloat(pTransform[2]); + charpos.m_AdjustMatrix[1] = pCIDFont->CIDTransformToFloat(pTransform[1]); + charpos.m_AdjustMatrix[3] = pCIDFont->CIDTransformToFloat(pTransform[3]); + charpos.m_OriginX += + pCIDFont->CIDTransformToFloat(pTransform[4]) * FontSize; + charpos.m_OriginY += + pCIDFont->CIDTransformToFloat(pTransform[5]) * FontSize; charpos.m_bGlyphAdjust = TRUE; } } diff --git a/core/src/fxcodec/codec/fx_codec_jpeg.cpp b/core/src/fxcodec/codec/fx_codec_jpeg.cpp index efd83f88ae..4fdbb8ed29 100644 --- a/core/src/fxcodec/codec/fx_codec_jpeg.cpp +++ b/core/src/fxcodec/codec/fx_codec_jpeg.cpp @@ -193,7 +193,7 @@ static void _JpegEncode(const CFX_DIBSource* pSource, if (nComponents > 1) { uint8_t* dest_scan = line_buf; if (nComponents == 3) { - for (int i = 0; i < width; i++) { + for (FX_DWORD i = 0; i < width; i++) { dest_scan[0] = src_scan[2]; dest_scan[1] = src_scan[1]; dest_scan[2] = src_scan[0]; @@ -201,7 +201,7 @@ static void _JpegEncode(const CFX_DIBSource* pSource, src_scan += Bpp; } } else { - for (int i = 0; i < pitch; i++) { + for (FX_DWORD i = 0; i < pitch; i++) { *dest_scan++ = ~*src_scan++; } } diff --git a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp index fdffcb66de..76d62f763b 100644 --- a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp +++ b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp @@ -616,7 +616,7 @@ class CJPX_Decoder { public: explicit CJPX_Decoder(bool use_colorspace); ~CJPX_Decoder(); - FX_BOOL Init(const unsigned char* src_data, int src_size); + FX_BOOL Init(const unsigned char* src_data, FX_DWORD src_size); void GetInfo(FX_DWORD* width, FX_DWORD* height, FX_DWORD* components); bool Decode(uint8_t* dest_buf, int pitch, @@ -624,7 +624,7 @@ class CJPX_Decoder { private: const uint8_t* m_SrcData; - int m_SrcSize; + FX_DWORD m_SrcSize; opj_image_t* image; opj_codec_t* l_codec; opj_stream_t* l_stream; @@ -650,12 +650,12 @@ CJPX_Decoder::~CJPX_Decoder() { } } -FX_BOOL CJPX_Decoder::Init(const unsigned char* src_data, int src_size) { +FX_BOOL CJPX_Decoder::Init(const unsigned char* src_data, FX_DWORD src_size) { static const unsigned char szJP2Header[] = { 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a}; - if (!src_data || src_size < sizeof(szJP2Header)) { + if (!src_data || src_size < sizeof(szJP2Header)) return FALSE; - } + image = NULL; m_SrcData = src_data; m_SrcSize = src_size; diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp index 61a45bebae..cbbe6af70e 100644 --- a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp +++ b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp @@ -8,47 +8,44 @@ #include "JBig2_Define.h" -CJBig2_HuffmanDecoder::CJBig2_HuffmanDecoder(CJBig2_BitStream* pStream) { - m_pStream = pStream; +CJBig2_HuffmanDecoder::CJBig2_HuffmanDecoder(CJBig2_BitStream* pStream) + : m_pStream(pStream) { } + CJBig2_HuffmanDecoder::~CJBig2_HuffmanDecoder() {} + int CJBig2_HuffmanDecoder::decodeAValue(CJBig2_HuffmanTable* pTable, int* nResult) { - int i; int nVal = 0; int nBits = 0; - FX_DWORD nTmp; while (1) { - if (m_pStream->read1Bit(&nTmp) == -1) { + FX_DWORD nTmp; + if (m_pStream->read1Bit(&nTmp) == -1) return -1; - } + nVal = (nVal << 1) | nTmp; - nBits++; - for (i = 0; i < pTable->NTEMP; i++) { + ++nBits; + for (FX_DWORD i = 0; i < pTable->NTEMP; ++i) { if ((pTable->PREFLEN[i] == nBits) && (pTable->CODES[i] == nVal)) { - if ((pTable->HTOOB == 1) && (i == pTable->NTEMP - 1)) { + if ((pTable->HTOOB == 1) && (i == pTable->NTEMP - 1)) return JBIG2_OOB; - } - if (m_pStream->readNBits(pTable->RANGELEN[i], &nTmp) == -1) { + + if (m_pStream->readNBits(pTable->RANGELEN[i], &nTmp) == -1) return -1; - } + if (pTable->HTOOB) { - if (i == pTable->NTEMP - 3) { + if (i == pTable->NTEMP - 3) *nResult = pTable->RANGELOW[i] - nTmp; - return 0; - } else { + else *nResult = pTable->RANGELOW[i] + nTmp; - return 0; - } - } else { - if (i == pTable->NTEMP - 2) { - *nResult = pTable->RANGELOW[i] - nTmp; - return 0; - } else { - *nResult = pTable->RANGELOW[i] + nTmp; - return 0; - } + return 0; } + + if (i == pTable->NTEMP - 2) + *nResult = pTable->RANGELOW[i] - nTmp; + else + *nResult = pTable->RANGELOW[i] + nTmp; + return 0; } } } diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h index c972aae074..e15e891060 100644 --- a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h +++ b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h @@ -10,13 +10,13 @@ #include "JBig2_HuffmanTable.h" class CJBig2_HuffmanDecoder { public: - CJBig2_HuffmanDecoder(CJBig2_BitStream* pStream); + explicit CJBig2_HuffmanDecoder(CJBig2_BitStream* pStream); ~CJBig2_HuffmanDecoder(); int decodeAValue(CJBig2_HuffmanTable* pTable, int* nResult); private: - CJBig2_BitStream* m_pStream; + CJBig2_BitStream* const m_pStream; }; #endif diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp index a09d152052..4effbfadef 100644 --- a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp +++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp @@ -35,25 +35,22 @@ CJBig2_HuffmanTable::~CJBig2_HuffmanTable() { void CJBig2_HuffmanTable::init() { HTOOB = FALSE; NTEMP = 0; - CODES = NULL; - PREFLEN = NULL; - RANGELEN = NULL; - RANGELOW = NULL; + CODES = nullptr; + PREFLEN = nullptr; + RANGELEN = nullptr; + RANGELOW = nullptr; } int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable, int nLines, FX_BOOL bHTOOB) { - int CURLEN, LENMAX, CURCODE, CURTEMP; - int* LENCOUNT; - int* FIRSTCODE; HTOOB = bHTOOB; NTEMP = nLines; CODES = FX_Alloc(int, NTEMP); PREFLEN = FX_Alloc(int, NTEMP); RANGELEN = FX_Alloc(int, NTEMP); RANGELOW = FX_Alloc(int, NTEMP); - LENMAX = 0; - for (int i = 0; i < NTEMP; i++) { + int LENMAX = 0; + for (FX_DWORD i = 0; i < NTEMP; ++i) { PREFLEN[i] = pTable[i].PREFLEN; RANGELEN[i] = pTable[i].RANDELEN; RANGELOW[i] = pTable[i].RANGELOW; @@ -61,19 +58,19 @@ int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable, LENMAX = PREFLEN[i]; } } - LENCOUNT = FX_Alloc(int, LENMAX + 1); + int* LENCOUNT = FX_Alloc(int, LENMAX + 1); JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); - FIRSTCODE = FX_Alloc(int, LENMAX + 1); - for (int i = 0; i < NTEMP; i++) { - LENCOUNT[PREFLEN[i]]++; - } - CURLEN = 1; + int* FIRSTCODE = FX_Alloc(int, LENMAX + 1); + for (FX_DWORD i = 0; i < NTEMP; ++i) + ++LENCOUNT[PREFLEN[i]]; + + int CURLEN = 1; FIRSTCODE[0] = 0; LENCOUNT[0] = 0; while (CURLEN <= LENMAX) { FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; - CURCODE = FIRSTCODE[CURLEN]; - CURTEMP = 0; + int CURCODE = FIRSTCODE[CURLEN]; + FX_DWORD CURTEMP = 0; while (CURTEMP < NTEMP) { if (PREFLEN[CURTEMP] == CURLEN) { CODES[CURTEMP] = CURCODE; @@ -109,6 +106,7 @@ int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) { pStream->readInteger(&HTHIGH) == -1 || HTLOW > HTHIGH) { return FALSE; } + FX_DWORD nSize = 16; PREFLEN = FX_Alloc(int, nSize); RANGELEN = FX_Alloc(int, nSize); @@ -131,7 +129,7 @@ int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) { RANGELEN[NTEMP] = 32; RANGELOW[NTEMP] = HTLOW - 1; - NTEMP = NTEMP + 1; + ++NTEMP; HT_CHECK_MEMORY_ADJUST if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) return FALSE; @@ -143,18 +141,19 @@ int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) { HT_CHECK_MEMORY_ADJUST if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) return FALSE; - NTEMP = NTEMP + 1; + + ++NTEMP; } CODES = FX_Alloc(int, NTEMP); int LENMAX = 0; - for (int i = 0; i < NTEMP; i++) { + for (FX_DWORD i = 0; i < NTEMP; ++i) { if (PREFLEN[i] > LENMAX) { LENMAX = PREFLEN[i]; } } std::vector LENCOUNT(LENMAX + 1); - for (int i = 0; i < NTEMP; ++i) + for (FX_DWORD i = 0; i < NTEMP; ++i) LENCOUNT[PREFLEN[i]]++; LENCOUNT[0] = 0; @@ -163,7 +162,7 @@ int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) { for (int i = 0; i <= LENMAX; ++i) { FIRSTCODE[i] = (FIRSTCODE[i - 1] + LENCOUNT[i - 1]) << 1; int CURCODE = FIRSTCODE[i]; - for (int j = 0; j < NTEMP; ++j) { + for (FX_DWORD j = 0; j < NTEMP; ++j) { if (PREFLEN[j] == i) CODES[j] = CURCODE++; } diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h index 7c843cf91b..fe8294dc7e 100644 --- a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h +++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h @@ -14,7 +14,7 @@ class CJBig2_HuffmanTable { public: CJBig2_HuffmanTable(const JBig2TableLine* pTable, int nLines, FX_BOOL bHTOOB); - CJBig2_HuffmanTable(CJBig2_BitStream* pStream); + explicit CJBig2_HuffmanTable(CJBig2_BitStream* pStream); ~CJBig2_HuffmanTable(); @@ -24,13 +24,13 @@ class CJBig2_HuffmanTable { int parseFromCodedBuffer(CJBig2_BitStream* pStream); - FX_BOOL isOK() { return m_bOK; } + FX_BOOL isOK() const { return m_bOK; } private: void init(); FX_BOOL HTOOB; - int NTEMP; + FX_DWORD NTEMP; int* CODES; int* PREFLEN; int* RANGELEN; diff --git a/core/src/fxge/ge/fx_ge_linux.cpp b/core/src/fxge/ge/fx_ge_linux.cpp index 609777fbb5..72971f7f5d 100644 --- a/core/src/fxge/ge/fx_ge_linux.cpp +++ b/core/src/fxge/ge/fx_ge_linux.cpp @@ -91,7 +91,7 @@ void* CFX_LinuxFontInfo::MapFont(int weight, } } break; case FXFONT_GB2312_CHARSET: { - for (int32_t i = 0; i < FX_ArraySize(g_LinuxGbFontList); ++i) { + for (size_t i = 0; i < FX_ArraySize(g_LinuxGbFontList); ++i) { auto it = m_FontList.find(g_LinuxGbFontList[i]); if (it != m_FontList.end()) { return it->second; @@ -99,7 +99,7 @@ void* CFX_LinuxFontInfo::MapFont(int weight, } } break; case FXFONT_CHINESEBIG5_CHARSET: { - for (int32_t i = 0; i < FX_ArraySize(g_LinuxB5FontList); ++i) { + for (size_t i = 0; i < FX_ArraySize(g_LinuxB5FontList); ++i) { auto it = m_FontList.find(g_LinuxB5FontList[i]); if (it != m_FontList.end()) { return it->second; @@ -107,7 +107,7 @@ void* CFX_LinuxFontInfo::MapFont(int weight, } } break; case FXFONT_HANGEUL_CHARSET: { - for (int32_t i = 0; i < FX_ArraySize(g_LinuxHGFontList); ++i) { + for (size_t i = 0; i < FX_ArraySize(g_LinuxHGFontList); ++i) { auto it = m_FontList.find(g_LinuxHGFontList[i]); if (it != m_FontList.end()) { return it->second; -- cgit v1.2.3