Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Unified Diff: core/src/fpdfapi/fpdf_font/fpdf_font.cpp

Issue 1729823004: refactor CPDF_Font and subclasses (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Remove types and delete unused Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: core/src/fpdfapi/fpdf_font/fpdf_font.cpp
diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp
index 1e9335f30f4b5cad8ec37a401d3bb4b0adcf1755..ddba738b33d7e7659939770778c9a50ace18bf2c 100644
--- a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp
+++ b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp
@@ -19,6 +19,77 @@
#include "core/src/fxge/apple/apple_int.h"
#endif
+namespace {
+
+#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
+struct _GlyphNameMap {
Lei Zhang 2016/02/25 01:54:02 No leading underscore. Ditto below.
Wei Li 2016/02/25 18:48:42 Done.
+ const FX_CHAR* m_pStrAdobe;
+ const FX_CHAR* m_pStrUnicode;
+};
+const _GlyphNameMap g_GlyphNameSubsts[] = {{"ff", "uniFB00"},
+ {"fi", "uniFB01"},
+ {"fl", "uniFB02"},
+ {"ffi", "uniFB03"},
+ {"ffl", "uniFB04"}};
+extern "C" {
Lei Zhang 2016/02/25 01:54:02 not needed
Wei Li 2016/02/25 18:48:42 Done.
+int compareString(const void* key, const void* element) {
+ return FXSYS_stricmp((const FX_CHAR*)key,
+ ((_GlyphNameMap*)element)->m_pStrAdobe);
+}
+}
+const FX_CHAR* _GlyphNameRemap(const FX_CHAR* pStrAdobe) {
+ _GlyphNameMap* found = (_GlyphNameMap*)FXSYS_bsearch(
Lei Zhang 2016/02/25 01:54:02 Just do std::find() given the size of the array?
Wei Li 2016/02/25 18:48:42 For such a small array, plain linear search should
+ pStrAdobe, g_GlyphNameSubsts,
+ sizeof g_GlyphNameSubsts / sizeof(_GlyphNameMap), sizeof(_GlyphNameMap),
+ compareString);
+ if (found) {
+ return found->m_pStrUnicode;
+ }
+ return NULL;
+}
+#endif
+
+const uint8_t ChineseFontNames[][5] = {{0xCB, 0xCE, 0xCC, 0xE5, 0x00},
+ {0xBF, 0xAC, 0xCC, 0xE5, 0x00},
+ {0xBA, 0xDA, 0xCC, 0xE5, 0x00},
+ {0xB7, 0xC2, 0xCB, 0xCE, 0x00},
+ {0xD0, 0xC2, 0xCB, 0xCE, 0x00}};
+
+FX_BOOL GetPredefinedEncoding(int& basemap, const CFX_ByteString& value) {
+ if (value == "WinAnsiEncoding") {
+ basemap = PDFFONT_ENCODING_WINANSI;
+ } else if (value == "MacRomanEncoding") {
+ basemap = PDFFONT_ENCODING_MACROMAN;
+ } else if (value == "MacExpertEncoding") {
+ basemap = PDFFONT_ENCODING_MACEXPERT;
+ } else if (value == "PDFDocEncoding") {
+ basemap = PDFFONT_ENCODING_PDFDOC;
+ } else {
+ return FALSE;
+ }
+ return TRUE;
+}
+
+static FX_BOOL FT_UseType1Charmap(FXFT_Face face) {
Lei Zhang 2016/02/25 01:54:02 no need for static inside anonymous namespace
Wei Li 2016/02/25 18:48:42 Missed this, thanks
+ if (FXFT_Get_Face_CharmapCount(face) == 0) {
+ return FALSE;
+ }
+ if (FXFT_Get_Face_CharmapCount(face) == 1 &&
+ FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[0]) ==
+ FXFT_ENCODING_UNICODE) {
+ return FALSE;
+ }
+ if (FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[0]) ==
+ FXFT_ENCODING_UNICODE) {
+ FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[1]);
+ } else {
+ FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[0]);
+ }
+ return TRUE;
+}
+
+} // namespace
+
FX_BOOL FT_UseTTCharmap(FXFT_Face face, int platform_id, int encoding_id) {
for (int i = 0; i < FXFT_Get_Face_CharmapCount(face); i++) {
if (FXFT_Get_Charmap_PlatformID(FXFT_Get_Face_Charmaps(face)[i]) ==
@@ -81,14 +152,17 @@ void CPDF_FontGlobals::Clear(CPDF_Document* pDoc) {
m_StockMap.erase(pDoc);
}
-CPDF_Font::CPDF_Font(int fonttype) : m_FontType(fonttype) {
- m_FontBBox.left = m_FontBBox.right = m_FontBBox.top = m_FontBBox.bottom = 0;
- m_StemV = m_Ascent = m_Descent = m_ItalicAngle = 0;
- m_pFontFile = NULL;
- m_Flags = 0;
- m_pToUnicodeMap = NULL;
- m_bToUnicodeLoaded = FALSE;
-}
+CPDF_Font::CPDF_Font()
+ : m_pFontFile(nullptr),
+ m_pFontDict(nullptr),
+ m_pToUnicodeMap(nullptr),
+ m_bToUnicodeLoaded(FALSE),
+ m_Flags(0),
+ m_StemV(0),
+ m_Ascent(0),
+ m_Descent(0),
+ m_ItalicAngle(0) {}
+
CPDF_Font::~CPDF_Font() {
delete m_pToUnicodeMap;
m_pToUnicodeMap = NULL;
@@ -98,9 +172,10 @@ CPDF_Font::~CPDF_Font() {
const_cast<CPDF_Stream*>(m_pFontFile->GetStream()->AsStream()));
}
}
+
FX_BOOL CPDF_Font::IsVertWriting() const {
FX_BOOL bVertWriting = FALSE;
- CPDF_CIDFont* pCIDFont = GetCIDFont();
+ const CPDF_CIDFont* pCIDFont = AsCIDFont();
if (pCIDFont) {
bVertWriting = pCIDFont->IsVertWriting();
} else {
@@ -108,19 +183,12 @@ FX_BOOL CPDF_Font::IsVertWriting() const {
}
return bVertWriting;
}
-CFX_ByteString CPDF_Font::GetFontTypeName() const {
- switch (m_FontType) {
- case PDFFONT_TYPE1:
- return "Type1";
- case PDFFONT_TRUETYPE:
- return "TrueType";
- case PDFFONT_TYPE3:
- return "Type3";
- case PDFFONT_CIDFONT:
- return "Type0";
- }
- return CFX_ByteString();
+
+int CPDF_Font::AppendChar(FX_CHAR* buf, FX_DWORD charcode) const {
+ *buf = (FX_CHAR)charcode;
+ return 1;
}
+
void CPDF_Font::AppendChar(CFX_ByteString& str, FX_DWORD charcode) const {
char buf[4];
int len = AppendChar(buf, charcode);
@@ -130,6 +198,7 @@ void CPDF_Font::AppendChar(CFX_ByteString& str, FX_DWORD charcode) const {
str += CFX_ByteString(buf, len);
}
}
+
CFX_WideString CPDF_Font::UnicodeFromCharCode(FX_DWORD charcode) const {
if (!m_bToUnicodeLoaded) {
((CPDF_Font*)this)->LoadUnicodeMap();
@@ -146,6 +215,7 @@ CFX_WideString CPDF_Font::UnicodeFromCharCode(FX_DWORD charcode) const {
}
return unicode;
}
+
FX_DWORD CPDF_Font::CharCodeFromUnicode(FX_WCHAR unicode) const {
if (!m_bToUnicodeLoaded) {
((CPDF_Font*)this)->LoadUnicodeMap();
@@ -233,6 +303,7 @@ short TT2PDF(int m, FXFT_Face face) {
}
return (m * 1000 + upm / 2) / upm;
}
+
void CPDF_Font::CheckFontMetrics() {
if (m_FontBBox.top == 0 && m_FontBBox.bottom == 0 && m_FontBBox.left == 0 &&
m_FontBBox.right == 0) {
@@ -288,6 +359,7 @@ void CPDF_Font::CheckFontMetrics() {
}
}
}
+
void CPDF_Font::LoadUnicodeMap() {
m_bToUnicodeLoaded = TRUE;
CPDF_Stream* pStream = m_pFontDict->GetStreamBy("ToUnicode");
@@ -297,6 +369,7 @@ void CPDF_Font::LoadUnicodeMap() {
m_pToUnicodeMap = new CPDF_ToUnicodeMap;
m_pToUnicodeMap->Load(pStream);
}
+
int CPDF_Font::GetStringWidth(const FX_CHAR* pString, int size) {
int offset = 0;
int width = 0;
@@ -306,16 +379,6 @@ int CPDF_Font::GetStringWidth(const FX_CHAR* pString, int size) {
}
return width;
}
-int CPDF_Font::GetCharTypeWidth(FX_DWORD charcode) {
- if (!m_Font.GetFace())
- return 0;
-
- int glyph_index = GlyphFromCharCode(charcode);
- if (glyph_index == 0xffff) {
- return 0;
- }
- return m_Font.GetGlyphWidth(glyph_index);
-}
CPDF_Font* CPDF_Font::GetStockFont(CPDF_Document* pDoc,
const CFX_ByteStringC& name) {
@@ -339,11 +402,7 @@ CPDF_Font* CPDF_Font::GetStockFont(CPDF_Document* pDoc,
pFontGlobals->Set(pDoc, font_id, pFont);
return pFont;
}
-const uint8_t ChineseFontNames[][5] = {{0xCB, 0xCE, 0xCC, 0xE5, 0x00},
- {0xBF, 0xAC, 0xCC, 0xE5, 0x00},
- {0xBA, 0xDA, 0xCC, 0xE5, 0x00},
- {0xB7, 0xC2, 0xCB, 0xCE, 0x00},
- {0xD0, 0xC2, 0xCB, 0xCE, 0x00}};
+
CPDF_Font* CPDF_Font::CreateFontF(CPDF_Document* pDoc,
CPDF_Dictionary* pFontDict) {
CFX_ByteString type = pFontDict->GetStringBy("Subtype");
@@ -394,6 +453,7 @@ CPDF_Font* CPDF_Font::CreateFontF(CPDF_Document* pDoc,
}
return pFont;
}
+
FX_BOOL CPDF_Font::Load() {
if (!m_pFontDict) {
return FALSE;
@@ -434,6 +494,7 @@ CFX_WideString CPDF_ToUnicodeMap::Lookup(FX_DWORD charcode) {
}
return CFX_WideString();
}
+
FX_DWORD CPDF_ToUnicodeMap::ReverseLookup(FX_WCHAR unicode) {
for (const auto& pair : m_Map) {
if (pair.second == unicode)
@@ -461,6 +522,7 @@ FX_DWORD CPDF_ToUnicodeMap::StringToCode(const CFX_ByteStringC& str) {
return result;
}
+
static CFX_WideString StringDataAdd(CFX_WideString str) {
CFX_WideString ret;
int len = str.GetLength();
@@ -505,6 +567,7 @@ CFX_WideString CPDF_ToUnicodeMap::StringToWideString(
}
return result;
}
+
void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) {
CIDSet cid_set = CIDSET_UNKNOWN;
CPDF_StreamAcc stream;
@@ -612,21 +675,17 @@ void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) {
m_pBaseMap = NULL;
}
}
-static FX_BOOL GetPredefinedEncoding(int& basemap,
- const CFX_ByteString& value) {
- if (value == "WinAnsiEncoding") {
- basemap = PDFFONT_ENCODING_WINANSI;
- } else if (value == "MacRomanEncoding") {
- basemap = PDFFONT_ENCODING_MACROMAN;
- } else if (value == "MacExpertEncoding") {
- basemap = PDFFONT_ENCODING_MACEXPERT;
- } else if (value == "PDFDocEncoding") {
- basemap = PDFFONT_ENCODING_PDFDOC;
- } else {
- return FALSE;
+
+FX_DWORD CPDF_Font::GetNextChar(const FX_CHAR* pString,
+ int nStrLen,
+ int& offset) const {
+ if (offset < 0 || nStrLen < 1) {
+ return 0;
}
- return TRUE;
+ uint8_t ch = offset < nStrLen ? pString[offset++] : pString[nStrLen - 1];
+ return static_cast<FX_DWORD>(ch);
}
+
void CPDF_Font::LoadPDFEncoding(CPDF_Object* pEncoding,
int& iBaseEncoding,
CFX_ByteString*& pCharNames,
@@ -697,15 +756,16 @@ void CPDF_Font::LoadPDFEncoding(CPDF_Object* pEncoding,
}
FX_BOOL CPDF_Font::IsStandardFont() const {
- if (m_FontType != PDFFONT_TYPE1)
+ if (!IsType1Font())
return FALSE;
if (m_pFontFile)
return FALSE;
- if (((CPDF_Type1Font*)this)->GetBase14Font() < 0)
+ if (this->AsType1Font()->GetBase14Font() < 0)
return FALSE;
return TRUE;
}
-CPDF_SimpleFont::CPDF_SimpleFont(int fonttype) : CPDF_Font(fonttype) {
+
+CPDF_SimpleFont::CPDF_SimpleFont() {
FXSYS_memset(m_CharBBox, 0xff, sizeof m_CharBBox);
FXSYS_memset(m_CharWidth, 0xff, sizeof m_CharWidth);
FXSYS_memset(m_GlyphIndex, 0xff, sizeof m_GlyphIndex);
@@ -713,9 +773,11 @@ CPDF_SimpleFont::CPDF_SimpleFont(int fonttype) : CPDF_Font(fonttype) {
m_pCharNames = NULL;
m_BaseEncoding = PDFFONT_ENCODING_BUILTIN;
}
+
CPDF_SimpleFont::~CPDF_SimpleFont() {
delete[] m_pCharNames;
}
+
int CPDF_SimpleFont::GlyphFromCharCode(FX_DWORD charcode, FX_BOOL* pVertGlyph) {
if (pVertGlyph) {
*pVertGlyph = FALSE;
@@ -729,6 +791,7 @@ int CPDF_SimpleFont::GlyphFromCharCode(FX_DWORD charcode, FX_BOOL* pVertGlyph) {
}
return index;
}
+
void CPDF_SimpleFont::LoadCharMetrics(int charcode) {
if (!m_Font.GetFace())
return;
@@ -772,6 +835,7 @@ void CPDF_SimpleFont::LoadCharMetrics(int charcode) {
}
}
}
+
int CPDF_SimpleFont::GetCharWidthF(FX_DWORD charcode, int level) {
if (charcode > 0xff) {
charcode = 0;
@@ -784,6 +848,7 @@ int CPDF_SimpleFont::GetCharWidthF(FX_DWORD charcode, int level) {
}
return (int16_t)m_CharWidth[charcode];
}
+
void CPDF_SimpleFont::GetCharBBox(FX_DWORD charcode, FX_RECT& rect, int level) {
if (charcode > 0xff) {
charcode = 0;
@@ -796,6 +861,7 @@ void CPDF_SimpleFont::GetCharBBox(FX_DWORD charcode, FX_RECT& rect, int level) {
rect.bottom = m_CharBBox[charcode].Bottom;
rect.top = m_CharBBox[charcode].Top;
}
+
const FX_CHAR* GetAdobeCharName(int iBaseEncoding,
const CFX_ByteString* pCharNames,
int charcode) {
@@ -812,6 +878,7 @@ const FX_CHAR* GetAdobeCharName(int iBaseEncoding,
}
return name && name[0] ? name : nullptr;
}
+
FX_BOOL CPDF_SimpleFont::LoadCommon() {
CPDF_Dictionary* pFontDesc = m_pFontDict->GetDictBy("FontDescriptor");
if (pFontDesc) {
@@ -880,6 +947,7 @@ FX_BOOL CPDF_SimpleFont::LoadCommon() {
CheckFontMetrics();
return TRUE;
}
+
void CPDF_SimpleFont::LoadSubstFont() {
if (!m_bUseFontWidth && !(m_Flags & PDFFONT_FIXEDPITCH)) {
int width = 0, i;
@@ -898,19 +966,28 @@ void CPDF_SimpleFont::LoadSubstFont() {
}
}
int weight = m_StemV < 140 ? m_StemV * 5 : (m_StemV * 4 + 140);
- m_Font.LoadSubst(m_BaseFont, IsFontType(PDFFONT_TRUETYPE), m_Flags, weight,
- m_ItalicAngle, 0);
+ m_Font.LoadSubst(m_BaseFont, IsTrueTypeFont(), m_Flags, weight, m_ItalicAngle,
+ 0);
if (m_Font.GetSubstFont()->m_SubstFlags & FXFONT_SUBST_NONSYMBOL) {
}
}
+
FX_BOOL CPDF_SimpleFont::IsUnicodeCompatible() const {
return m_BaseEncoding != PDFFONT_ENCODING_BUILTIN &&
m_BaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL &&
m_BaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS;
}
-CPDF_Type1Font::CPDF_Type1Font() : CPDF_SimpleFont(PDFFONT_TYPE1) {
- m_Base14Font = -1;
+
+FX_WCHAR CPDF_SimpleFont::_UnicodeFromCharCode(FX_DWORD charcode) const {
+ return m_Encoding.UnicodeFromCharCode((uint8_t)charcode);
}
+
+FX_DWORD CPDF_SimpleFont::_CharCodeFromUnicode(FX_WCHAR Unicode) const {
+ return m_Encoding.CharCodeFromUnicode(Unicode);
+}
+
+CPDF_Type1Font::CPDF_Type1Font() : m_Base14Font(-1) {}
+
FX_BOOL CPDF_Type1Font::_Load() {
m_Base14Font = PDF_GetStandardFontName(&m_BaseFont);
if (m_Base14Font >= 0) {
@@ -934,60 +1011,7 @@ FX_BOOL CPDF_Type1Font::_Load() {
}
return LoadCommon();
}
-static FX_BOOL FT_UseType1Charmap(FXFT_Face face) {
- if (FXFT_Get_Face_CharmapCount(face) == 0) {
- return FALSE;
- }
- if (FXFT_Get_Face_CharmapCount(face) == 1 &&
- FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[0]) ==
- FXFT_ENCODING_UNICODE) {
- return FALSE;
- }
- if (FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[0]) ==
- FXFT_ENCODING_UNICODE) {
- FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[1]);
- } else {
- FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[0]);
- }
- return TRUE;
-}
-int CPDF_Type1Font::GlyphFromCharCodeExt(FX_DWORD charcode) {
- if (charcode > 0xff) {
- return -1;
- }
- int index = m_ExtGID[(uint8_t)charcode];
- if (index == 0xffff) {
- return -1;
- }
- return index;
-}
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
-struct _GlyphNameMap {
- const FX_CHAR* m_pStrAdobe;
- const FX_CHAR* m_pStrUnicode;
-};
-static const _GlyphNameMap g_GlyphNameSubsts[] = {{"ff", "uniFB00"},
- {"fi", "uniFB01"},
- {"fl", "uniFB02"},
- {"ffi", "uniFB03"},
- {"ffl", "uniFB04"}};
-extern "C" {
-static int compareString(const void* key, const void* element) {
- return FXSYS_stricmp((const FX_CHAR*)key,
- ((_GlyphNameMap*)element)->m_pStrAdobe);
-}
-}
-static const FX_CHAR* _GlyphNameRemap(const FX_CHAR* pStrAdobe) {
- _GlyphNameMap* found = (_GlyphNameMap*)FXSYS_bsearch(
- pStrAdobe, g_GlyphNameSubsts,
- sizeof g_GlyphNameSubsts / sizeof(_GlyphNameMap), sizeof(_GlyphNameMap),
- compareString);
- if (found) {
- return found->m_pStrUnicode;
- }
- return NULL;
-}
-#endif
+
void CPDF_Type1Font::LoadGlyphMap() {
if (!m_Font.GetFace())
return;
@@ -1280,6 +1304,7 @@ void CPDF_Type1Font::LoadGlyphMap() {
CPDF_FontEncoding::CPDF_FontEncoding() {
FXSYS_memset(m_Unicodes, 0, sizeof(m_Unicodes));
}
+
int CPDF_FontEncoding::CharCodeFromUnicode(FX_WCHAR unicode) const {
for (int i = 0; i < 256; i++)
if (m_Unicodes[i] == unicode) {
@@ -1287,6 +1312,7 @@ int CPDF_FontEncoding::CharCodeFromUnicode(FX_WCHAR unicode) const {
}
return -1;
}
+
CPDF_FontEncoding::CPDF_FontEncoding(int PredefinedEncoding) {
const FX_WORD* pSrc = PDF_UnicodesForPredefinedCharSet(PredefinedEncoding);
if (!pSrc) {
@@ -1296,10 +1322,12 @@ CPDF_FontEncoding::CPDF_FontEncoding(int PredefinedEncoding) {
m_Unicodes[i] = pSrc[i];
}
}
+
FX_BOOL CPDF_FontEncoding::IsIdentical(CPDF_FontEncoding* pAnother) const {
return FXSYS_memcmp(m_Unicodes, pAnother->m_Unicodes, sizeof(m_Unicodes)) ==
0;
}
+
CPDF_Object* CPDF_FontEncoding::Realize() {
int predefined = 0;
for (int cs = PDFFONT_ENCODING_WINANSI; cs < PDFFONT_ENCODING_ZAPFDINGBATS;
@@ -1345,10 +1373,13 @@ CPDF_Object* CPDF_FontEncoding::Realize() {
pDict->SetAt("Differences", pDiff);
return pDict;
}
-CPDF_TrueTypeFont::CPDF_TrueTypeFont() : CPDF_SimpleFont(PDFFONT_TRUETYPE) {}
+
+CPDF_TrueTypeFont::CPDF_TrueTypeFont() {}
+
FX_BOOL CPDF_TrueTypeFont::_Load() {
return LoadCommon();
}
+
void CPDF_TrueTypeFont::LoadGlyphMap() {
if (!m_Font.GetFace())
return;
@@ -1546,8 +1577,7 @@ void CPDF_TrueTypeFont::LoadGlyphMap() {
}
CPDF_Type3Font::CPDF_Type3Font()
- : CPDF_SimpleFont(PDFFONT_TYPE3),
- m_pCharProcs(nullptr),
+ : m_pCharProcs(nullptr),
m_pPageResources(nullptr),
m_pFontResources(nullptr) {
FXSYS_memset(m_CharWidthL, 0, sizeof(m_CharWidthL));
@@ -1604,6 +1634,7 @@ FX_BOOL CPDF_Type3Font::_Load() {
}
return TRUE;
}
+
void CPDF_Type3Font::CheckType3FontMetrics() {
CheckFontMetrics();
}

Powered by Google App Engine
This is Rietveld 408576698