From 8ea3f5176aab4d6688710d9937cf38a43c14d030 Mon Sep 17 00:00:00 2001 From: thestig Date: Mon, 27 Jun 2016 11:55:24 -0700 Subject: Clean up some CPDF_Font subclasses. Review-Url: https://codereview.chromium.org/2093313002 --- core/fpdfapi/fpdf_font/cpdf_cidfont.cpp | 199 ++++++++++++-------------- core/fpdfapi/fpdf_font/cpdf_cidfont.h | 6 +- core/fpdfapi/fpdf_font/cpdf_font.cpp | 35 ++--- core/fpdfapi/fpdf_font/cpdf_simplefont.cpp | 23 ++- core/fpdfapi/fpdf_font/cpdf_simplefont.h | 3 +- core/fpdfapi/fpdf_font/include/cpdf_font.h | 3 +- core/fpdfapi/fpdf_render/fpdf_render_text.cpp | 2 +- fpdfsdk/pdfwindow/PWL_FontMap.cpp | 23 ++- 8 files changed, 130 insertions(+), 164 deletions(-) diff --git a/core/fpdfapi/fpdf_font/cpdf_cidfont.cpp b/core/fpdfapi/fpdf_font/cpdf_cidfont.cpp index 317e6ddb01..73197a8427 100644 --- a/core/fpdfapi/fpdf_font/cpdf_cidfont.cpp +++ b/core/fpdfapi/fpdf_font/cpdf_cidfont.cpp @@ -463,7 +463,7 @@ FX_RECT CPDF_CIDFont::GetCharBBox(uint32_t charcode, int level) { return FX_RECT(m_CharBBox[charcode]); FX_RECT rect; - FX_BOOL bVert = FALSE; + bool bVert = false; int glyph_index = GlyphFromCharCode(charcode, &bVert); FXFT_Face face = m_Font.GetFace(); if (face) { @@ -579,60 +579,55 @@ void CPDF_CIDFont::GetVertOrigin(uint16_t CID, short& vx, short& vy) const { vx = (short)dwWidth / 2; vy = (short)m_DefaultVY; } -int CPDF_CIDFont::GetGlyphIndex(uint32_t unicode, FX_BOOL* pVertGlyph) { - if (pVertGlyph) { - *pVertGlyph = FALSE; - } + +int CPDF_CIDFont::GetGlyphIndex(uint32_t unicode, bool* pVertGlyph) { + if (pVertGlyph) + *pVertGlyph = false; + FXFT_Face face = m_Font.GetFace(); int index = FXFT_Get_Char_Index(face, unicode); - if (unicode == 0x2502) { + if (unicode == 0x2502) return index; - } - if (index && IsVertWriting()) { - if (m_pTTGSUBTable) { - uint32_t vindex = 0; - m_pTTGSUBTable->GetVerticalGlyph(index, &vindex); - if (vindex) { - index = vindex; - if (pVertGlyph) { - *pVertGlyph = TRUE; - } - } - return index; - } - if (!m_Font.GetSubData()) { - unsigned long length = 0; - int error = FXFT_Load_Sfnt_Table(face, FT_MAKE_TAG('G', 'S', 'U', 'B'), 0, - nullptr, &length); - if (!error) { - m_Font.SetSubData(FX_Alloc(uint8_t, length)); - } - } - int error = FXFT_Load_Sfnt_Table(face, FT_MAKE_TAG('G', 'S', 'U', 'B'), 0, - m_Font.GetSubData(), nullptr); - if (!error && m_Font.GetSubData()) { - m_pTTGSUBTable.reset(new CFX_CTTGSUBTable); - m_pTTGSUBTable->LoadGSUBTable((FT_Bytes)m_Font.GetSubData()); - uint32_t vindex = 0; - m_pTTGSUBTable->GetVerticalGlyph(index, &vindex); - if (vindex) { - index = vindex; - if (pVertGlyph) { - *pVertGlyph = TRUE; - } - } - } + + if (!index || !IsVertWriting()) return index; + + if (m_pTTGSUBTable) + return GetVerticalGlyph(index, pVertGlyph); + + if (!m_Font.GetSubData()) { + unsigned long length = 0; + int error = FXFT_Load_Sfnt_Table(face, FT_MAKE_TAG('G', 'S', 'U', 'B'), 0, + nullptr, &length); + if (!error) + m_Font.SetSubData(FX_Alloc(uint8_t, length)); } - if (pVertGlyph) { - *pVertGlyph = FALSE; - } + int error = FXFT_Load_Sfnt_Table(face, FT_MAKE_TAG('G', 'S', 'U', 'B'), 0, + m_Font.GetSubData(), nullptr); + if (error || !m_Font.GetSubData()) + return index; + + m_pTTGSUBTable.reset(new CFX_CTTGSUBTable); + m_pTTGSUBTable->LoadGSUBTable((FT_Bytes)m_Font.GetSubData()); + return GetVerticalGlyph(index, pVertGlyph); +} + +int CPDF_CIDFont::GetVerticalGlyph(int index, bool* pVertGlyph) { + uint32_t vindex = 0; + m_pTTGSUBTable->GetVerticalGlyph(index, &vindex); + if (!vindex) + return index; + + index = vindex; + if (pVertGlyph) + *pVertGlyph = true; return index; } -int CPDF_CIDFont::GlyphFromCharCode(uint32_t charcode, FX_BOOL* pVertGlyph) { - if (pVertGlyph) { + +int CPDF_CIDFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) { + if (pVertGlyph) *pVertGlyph = FALSE; - } + if (!m_pFontFile && !m_pStreamAcc) { uint16_t cid = CIDFromCharCode(charcode); FX_WCHAR unicode = 0; @@ -640,66 +635,60 @@ int CPDF_CIDFont::GlyphFromCharCode(uint32_t charcode, FX_BOOL* pVertGlyph) { #if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_ return cid; #else - if (m_Flags & PDFFONT_SYMBOLIC) { + if (m_Flags & PDFFONT_SYMBOLIC) return cid; - } + CFX_WideString uni_str = UnicodeFromCharCode(charcode); - if (uni_str.IsEmpty()) { + if (uni_str.IsEmpty()) return cid; - } + unicode = uni_str.GetAt(0); #endif } else { - if (cid && m_pCID2UnicodeMap && m_pCID2UnicodeMap->IsLoaded()) { + if (cid && m_pCID2UnicodeMap && m_pCID2UnicodeMap->IsLoaded()) unicode = m_pCID2UnicodeMap->UnicodeFromCID(cid); - } - if (unicode == 0) { + if (unicode == 0) unicode = GetUnicodeFromCharCode(charcode); - } - if (unicode == 0 && !(m_Flags & PDFFONT_SYMBOLIC)) { + if (unicode == 0 && !(m_Flags & PDFFONT_SYMBOLIC)) unicode = UnicodeFromCharCode(charcode).GetAt(0); - } } FXFT_Face face = m_Font.GetFace(); if (unicode == 0) { - if (!m_bAdobeCourierStd) { - return charcode == 0 ? -1 : (int)charcode; - } + if (!m_bAdobeCourierStd) + return charcode ? static_cast(charcode) : -1; + charcode += 31; - int index = 0, iBaseEncoding; FX_BOOL bMSUnicode = FT_UseTTCharmap(face, 3, 1); - FX_BOOL bMacRoman = FALSE; - if (!bMSUnicode) { - bMacRoman = FT_UseTTCharmap(face, 1, 0); - } - iBaseEncoding = PDFFONT_ENCODING_STANDARD; + FX_BOOL bMacRoman = bMSUnicode ? FALSE : FT_UseTTCharmap(face, 1, 0); + int iBaseEncoding = PDFFONT_ENCODING_STANDARD; if (bMSUnicode) { iBaseEncoding = PDFFONT_ENCODING_WINANSI; } else if (bMacRoman) { iBaseEncoding = PDFFONT_ENCODING_MACROMAN; } const FX_CHAR* name = GetAdobeCharName(iBaseEncoding, nullptr, charcode); - if (!name) { - return charcode == 0 ? -1 : (int)charcode; - } + if (!name) + return charcode ? static_cast(charcode) : -1; + + int index = 0; uint16_t name_unicode = PDF_UnicodeFromAdobeName(name); - if (name_unicode) { - if (bMSUnicode) { - index = FXFT_Get_Char_Index(face, name_unicode); - } else if (bMacRoman) { - uint32_t maccode = - FT_CharCodeFromUnicode(FXFT_ENCODING_APPLE_ROMAN, name_unicode); - index = !maccode ? FXFT_Get_Name_Index(face, (char*)name) - : FXFT_Get_Char_Index(face, maccode); - } else { - return FXFT_Get_Char_Index(face, name_unicode); - } + if (!name_unicode) + return charcode ? static_cast(charcode) : -1; + + if (iBaseEncoding == PDFFONT_ENCODING_STANDARD) + return FXFT_Get_Char_Index(face, name_unicode); + + if (iBaseEncoding == PDFFONT_ENCODING_WINANSI) { + index = FXFT_Get_Char_Index(face, name_unicode); } else { - return charcode == 0 ? -1 : (int)charcode; - } - if (index == 0 || index == 0xffff) { - return charcode == 0 ? -1 : (int)charcode; + ASSERT(iBaseEncoding == PDFFONT_ENCODING_MACROMAN); + uint32_t maccode = + FT_CharCodeFromUnicode(FXFT_ENCODING_APPLE_ROMAN, name_unicode); + index = maccode ? FXFT_Get_Char_Index(face, maccode) + : FXFT_Get_Name_Index(face, const_cast(name)); } + if (index == 0 || index == 0xffff) + return charcode ? static_cast(charcode) : -1; return index; } if (m_Charset == CIDSET_JAPAN1) { @@ -715,7 +704,7 @@ int CPDF_CIDFont::GlyphFromCharCode(uint32_t charcode, FX_BOOL* pVertGlyph) { return unicode; int err = FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE); - if (err != 0) { + if (err) { int i; for (i = 0; i < FXFT_Get_Face_CharmapCount(face); i++) { uint32_t ret = FT_CharCodeFromUnicode( @@ -735,38 +724,34 @@ int CPDF_CIDFont::GlyphFromCharCode(uint32_t charcode, FX_BOOL* pVertGlyph) { } if (FXFT_Get_Face_Charmap(face)) { int index = GetGlyphIndex(unicode, pVertGlyph); - if (index == 0) - return -1; - return index; + return index != 0 ? index : -1; } return unicode; } + if (!m_Font.GetFace()) return -1; uint16_t cid = CIDFromCharCode(charcode); - if (m_bType1) { - if (!m_pStreamAcc) { + if (!m_pStreamAcc) { + if (m_bType1) + return cid; + + if (m_pFontFile && !m_pCMap->m_pMapping) + return cid; + if (m_pCMap->m_Coding == CIDCODING_UNKNOWN || + !FXFT_Get_Face_Charmap(m_Font.GetFace())) { return cid; } - } else { - if (!m_pStreamAcc) { - if (m_pFontFile && !m_pCMap->m_pMapping) - return cid; - if (m_pCMap->m_Coding == CIDCODING_UNKNOWN || - !FXFT_Get_Face_Charmap(m_Font.GetFace())) { - return cid; - } - if (FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmap(m_Font.GetFace())) == - FXFT_ENCODING_UNICODE) { - CFX_WideString unicode_str = UnicodeFromCharCode(charcode); - if (unicode_str.IsEmpty()) { - return -1; - } - charcode = unicode_str.GetAt(0); - } - return GetGlyphIndex(charcode, pVertGlyph); + if (FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmap(m_Font.GetFace())) == + FXFT_ENCODING_UNICODE) { + CFX_WideString unicode_str = UnicodeFromCharCode(charcode); + if (unicode_str.IsEmpty()) + return -1; + + charcode = unicode_str.GetAt(0); } + return GetGlyphIndex(charcode, pVertGlyph); } uint32_t byte_pos = cid * 2; if (byte_pos + 2 > m_pStreamAcc->GetSize()) diff --git a/core/fpdfapi/fpdf_font/cpdf_cidfont.h b/core/fpdfapi/fpdf_font/cpdf_cidfont.h index 5abb20f7aa..2ae64cbd10 100644 --- a/core/fpdfapi/fpdf_font/cpdf_cidfont.h +++ b/core/fpdfapi/fpdf_font/cpdf_cidfont.h @@ -40,8 +40,7 @@ class CPDF_CIDFont : public CPDF_Font { bool IsCIDFont() const override; const CPDF_CIDFont* AsCIDFont() const override; CPDF_CIDFont* AsCIDFont() override; - int GlyphFromCharCode(uint32_t charcode, - FX_BOOL* pVertGlyph = nullptr) override; + int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) override; int GetCharWidthF(uint32_t charcode, int level = 0) override; FX_RECT GetCharBBox(uint32_t charcode, int level = 0) override; uint32_t GetNextChar(const FX_CHAR* pString, @@ -63,7 +62,8 @@ class CPDF_CIDFont : public CPDF_Font { void GetVertOrigin(uint16_t CID, short& vx, short& vy) const; protected: - int GetGlyphIndex(uint32_t unicodeb, FX_BOOL* pVertGlyph); + int GetGlyphIndex(uint32_t unicodeb, bool* pVertGlyph); + int GetVerticalGlyph(int index, bool* pVertGlyph); void LoadMetricsArray(CPDF_Array* pArray, CFX_ArrayTemplate& result, int nElements); diff --git a/core/fpdfapi/fpdf_font/cpdf_font.cpp b/core/fpdfapi/fpdf_font/cpdf_font.cpp index c15ac6b445..061b477f17 100644 --- a/core/fpdfapi/fpdf_font/cpdf_font.cpp +++ b/core/fpdfapi/fpdf_font/cpdf_font.cpp @@ -128,24 +128,13 @@ int CPDF_Font::GetCharSize(uint32_t charcode) const { return 1; } -int CPDF_Font::GlyphFromCharCode(uint32_t charcode, FX_BOOL* pVertGlyph) { - ASSERT(false); - return 0; -} - int CPDF_Font::GlyphFromCharCodeExt(uint32_t charcode) { - return GlyphFromCharCode(charcode); + return GlyphFromCharCode(charcode, nullptr); } FX_BOOL CPDF_Font::IsVertWriting() const { - FX_BOOL bVertWriting = FALSE; const CPDF_CIDFont* pCIDFont = AsCIDFont(); - if (pCIDFont) { - bVertWriting = pCIDFont->IsVertWriting(); - } else { - bVertWriting = m_Font.IsVertical(); - } - return bVertWriting; + return pCIDFont ? pCIDFont->IsVertWriting() : m_Font.IsVertical(); } int CPDF_Font::AppendChar(FX_CHAR* buf, uint32_t charcode) const { @@ -167,18 +156,14 @@ CFX_WideString CPDF_Font::UnicodeFromCharCode(uint32_t charcode) const { if (!m_bToUnicodeLoaded) LoadUnicodeMap(); - if (m_pToUnicodeMap) - return m_pToUnicodeMap->Lookup(charcode); - return CFX_WideString(); + return m_pToUnicodeMap ? m_pToUnicodeMap->Lookup(charcode) : CFX_WideString(); } uint32_t CPDF_Font::CharCodeFromUnicode(FX_WCHAR unicode) const { if (!m_bToUnicodeLoaded) LoadUnicodeMap(); - if (m_pToUnicodeMap) - return m_pToUnicodeMap->ReverseLookup(unicode); - return 0; + return m_pToUnicodeMap ? m_pToUnicodeMap->ReverseLookup(unicode) : 0; } void CPDF_Font::LoadFontDescriptor(CPDF_Dictionary* pFontDesc) { @@ -458,14 +443,16 @@ FX_BOOL CPDF_Font::IsStandardFont() const { const FX_CHAR* CPDF_Font::GetAdobeCharName(int iBaseEncoding, const CFX_ByteString* pCharNames, int charcode) { - ASSERT(charcode >= 0 && charcode < 256); - if (charcode < 0 || charcode >= 256) + if (charcode < 0 || charcode >= 256) { + ASSERT(false); return nullptr; + } + + if (pCharNames && !pCharNames[charcode].IsEmpty()) + return pCharNames[charcode].c_str(); const FX_CHAR* name = nullptr; - if (pCharNames) - name = pCharNames[charcode].c_str(); - if ((!name || name[0] == 0) && iBaseEncoding) + if (iBaseEncoding) name = PDF_CharNameFromPredefinedCharSet(iBaseEncoding, charcode); return name && name[0] ? name : nullptr; } diff --git a/core/fpdfapi/fpdf_font/cpdf_simplefont.cpp b/core/fpdfapi/fpdf_font/cpdf_simplefont.cpp index 129441625a..0eb7e2c057 100644 --- a/core/fpdfapi/fpdf_font/cpdf_simplefont.cpp +++ b/core/fpdfapi/fpdf_font/cpdf_simplefont.cpp @@ -13,27 +13,24 @@ CPDF_SimpleFont::CPDF_SimpleFont() : m_pCharNames(nullptr), m_BaseEncoding(PDFFONT_ENCODING_BUILTIN) { - FXSYS_memset(m_CharWidth, 0xff, sizeof m_CharWidth); - FXSYS_memset(m_GlyphIndex, 0xff, sizeof m_GlyphIndex); - FXSYS_memset(m_ExtGID, 0xff, sizeof m_ExtGID); + FXSYS_memset(m_CharWidth, 0xff, sizeof(m_CharWidth)); + FXSYS_memset(m_GlyphIndex, 0xff, sizeof(m_GlyphIndex)); + FXSYS_memset(m_ExtGID, 0xff, sizeof(m_ExtGID)); } CPDF_SimpleFont::~CPDF_SimpleFont() { delete[] m_pCharNames; } -int CPDF_SimpleFont::GlyphFromCharCode(uint32_t charcode, FX_BOOL* pVertGlyph) { - if (pVertGlyph) { - *pVertGlyph = FALSE; - } - if (charcode > 0xff) { +int CPDF_SimpleFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) { + if (pVertGlyph) + *pVertGlyph = false; + + if (charcode > 0xff) return -1; - } + int index = m_GlyphIndex[(uint8_t)charcode]; - if (index == 0xffff) { - return -1; - } - return index; + return index != 0xffff ? index : -1; } void CPDF_SimpleFont::LoadCharMetrics(int charcode) { diff --git a/core/fpdfapi/fpdf_font/cpdf_simplefont.h b/core/fpdfapi/fpdf_font/cpdf_simplefont.h index 51266bbe6c..3cc81cfe11 100644 --- a/core/fpdfapi/fpdf_font/cpdf_simplefont.h +++ b/core/fpdfapi/fpdf_font/cpdf_simplefont.h @@ -20,8 +20,7 @@ class CPDF_SimpleFont : public CPDF_Font { // CPDF_Font int GetCharWidthF(uint32_t charcode, int level = 0) override; FX_RECT GetCharBBox(uint32_t charcode, int level = 0) override; - int GlyphFromCharCode(uint32_t charcode, - FX_BOOL* pVertGlyph = nullptr) override; + int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) override; FX_BOOL IsUnicodeCompatible() const override; CFX_WideString UnicodeFromCharCode(uint32_t charcode) const override; uint32_t CharCodeFromUnicode(FX_WCHAR Unicode) const override; diff --git a/core/fpdfapi/fpdf_font/include/cpdf_font.h b/core/fpdfapi/fpdf_font/include/cpdf_font.h index 548283ee79..e8a7901043 100644 --- a/core/fpdfapi/fpdf_font/include/cpdf_font.h +++ b/core/fpdfapi/fpdf_font/include/cpdf_font.h @@ -64,8 +64,7 @@ class CPDF_Font { virtual int CountChar(const FX_CHAR* pString, int size) const; virtual int AppendChar(FX_CHAR* buf, uint32_t charcode) const; virtual int GetCharSize(uint32_t charcode) const; - virtual int GlyphFromCharCode(uint32_t charcode, - FX_BOOL* pVertGlyph = nullptr); + virtual int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) = 0; virtual int GlyphFromCharCodeExt(uint32_t charcode); virtual CFX_WideString UnicodeFromCharCode(uint32_t charcode) const; virtual uint32_t CharCodeFromUnicode(FX_WCHAR Unicode) const; diff --git a/core/fpdfapi/fpdf_render/fpdf_render_text.cpp b/core/fpdfapi/fpdf_render/fpdf_render_text.cpp index d3e70c169e..d8d416dd39 100644 --- a/core/fpdfapi/fpdf_render/fpdf_render_text.cpp +++ b/core/fpdfapi/fpdf_render/fpdf_render_text.cpp @@ -559,7 +559,7 @@ void CPDF_CharPosList::Load(int nChars, if (CharCode == (uint32_t)-1) { continue; } - FX_BOOL bVert = FALSE; + bool bVert = false; FXTEXT_CHARPOS& charpos = m_pCharPos[m_nChars++]; if (pCIDFont) { charpos.m_bFontStyle = true; diff --git a/fpdfsdk/pdfwindow/PWL_FontMap.cpp b/fpdfsdk/pdfwindow/PWL_FontMap.cpp index e810ec6a8c..9192afd5e8 100644 --- a/fpdfsdk/pdfwindow/PWL_FontMap.cpp +++ b/fpdfsdk/pdfwindow/PWL_FontMap.cpp @@ -122,18 +122,17 @@ int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word, } int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) { - if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) { - if (pData->pFont) { - if (pData->pFont->IsUnicodeCompatible()) { - int nCharCode = pData->pFont->CharCodeFromUnicode(word); - pData->pFont->GlyphFromCharCode(nCharCode); - return nCharCode; - } - if (word < 0xFF) - return word; - } - } - return -1; + CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex); + if (!pData) + return -1; + + if (!pData->pFont) + return -1; + + if (pData->pFont->IsUnicodeCompatible()) + return pData->pFont->CharCodeFromUnicode(word); + + return word < 0xFF ? word : -1; } CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) { -- cgit v1.2.3