summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2016-09-23 07:26:29 -0700
committerCommit bot <commit-bot@chromium.org>2016-09-23 07:26:29 -0700
commit3f4111fbff1233de9f5c67eda433bee0f5f88c4e (patch)
tree5cb30b258413915f8b800c871d235c123df1ad95
parent917d24852841f4919f418076c0277d2742f961ca (diff)
downloadpdfium-3f4111fbff1233de9f5c67eda433bee0f5f88c4e.tar.xz
Make CPDF_Font::Create() return a std::unique_ptr.
Review-Url: https://codereview.chromium.org/2364643003
-rw-r--r--core/fpdfapi/fpdf_font/cpdf_font.cpp18
-rw-r--r--core/fpdfapi/fpdf_font/font_int.h10
-rw-r--r--core/fpdfapi/fpdf_font/fpdf_font.cpp18
-rw-r--r--core/fpdfapi/fpdf_font/include/cpdf_font.h4
-rw-r--r--core/fpdfapi/fpdf_page/fpdf_page_doc.cpp14
5 files changed, 33 insertions, 31 deletions
diff --git a/core/fpdfapi/fpdf_font/cpdf_font.cpp b/core/fpdfapi/fpdf_font/cpdf_font.cpp
index d3f8664cd3..64511c8025 100644
--- a/core/fpdfapi/fpdf_font/cpdf_font.cpp
+++ b/core/fpdfapi/fpdf_font/cpdf_font.cpp
@@ -299,27 +299,25 @@ CPDF_Font* CPDF_Font::GetStockFont(CPDF_Document* pDoc,
const CFX_ByteStringC& name) {
CFX_ByteString fontname(name);
int font_id = PDF_GetStandardFontName(&fontname);
- if (font_id < 0) {
+ if (font_id < 0)
return nullptr;
- }
+
CPDF_FontGlobals* pFontGlobals =
CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals();
CPDF_Font* pFont = pFontGlobals->Find(pDoc, font_id);
- if (pFont) {
+ if (pFont)
return pFont;
- }
+
CPDF_Dictionary* pDict = new CPDF_Dictionary;
pDict->SetNameFor("Type", "Font");
pDict->SetNameFor("Subtype", "Type1");
pDict->SetNameFor("BaseFont", fontname);
pDict->SetNameFor("Encoding", "WinAnsiEncoding");
- pFont = CPDF_Font::CreateFontF(nullptr, pDict);
- pFontGlobals->Set(pDoc, font_id, pFont);
- return pFont;
+ return pFontGlobals->Set(pDoc, font_id, CPDF_Font::Create(nullptr, pDict));
}
-CPDF_Font* CPDF_Font::CreateFontF(CPDF_Document* pDoc,
- CPDF_Dictionary* pFontDict) {
+std::unique_ptr<CPDF_Font> CPDF_Font::Create(CPDF_Document* pDoc,
+ CPDF_Dictionary* pFontDict) {
CFX_ByteString type = pFontDict->GetStringFor("Subtype");
std::unique_ptr<CPDF_Font> pFont;
if (type == "TrueType") {
@@ -344,7 +342,7 @@ CPDF_Font* CPDF_Font::CreateFontF(CPDF_Document* pDoc,
pFont->m_pFontDict = pFontDict;
pFont->m_pDocument = pDoc;
pFont->m_BaseFont = pFontDict->GetStringFor("BaseFont");
- return pFont->Load() ? pFont.release() : nullptr;
+ return pFont->Load() ? std::move(pFont) : std::unique_ptr<CPDF_Font>();
}
uint32_t CPDF_Font::GetNextChar(const FX_CHAR* pString,
diff --git a/core/fpdfapi/fpdf_font/font_int.h b/core/fpdfapi/fpdf_font/font_int.h
index 512fb6c474..bcadc8d826 100644
--- a/core/fpdfapi/fpdf_font/font_int.h
+++ b/core/fpdfapi/fpdf_font/font_int.h
@@ -46,8 +46,8 @@ class CFX_StockFontArray {
CFX_StockFontArray();
~CFX_StockFontArray();
- // Takes ownership of |pFont|.
- void SetFont(uint32_t index, CPDF_Font* pFont);
+ // Takes ownership of |pFont|, returns unowned pointer to it.
+ CPDF_Font* SetFont(uint32_t index, std::unique_ptr<CPDF_Font> pFont);
CPDF_Font* GetFont(uint32_t index) const;
private:
@@ -62,8 +62,10 @@ class CPDF_FontGlobals {
void Clear(CPDF_Document* pDoc);
CPDF_Font* Find(CPDF_Document* pDoc, uint32_t index);
- // Takes ownership of |pFont|.
- void Set(CPDF_Document* key, uint32_t index, CPDF_Font* pFont);
+ // Takes ownership of |pFont|, returns unowned pointer to it.
+ CPDF_Font* Set(CPDF_Document* key,
+ uint32_t index,
+ std::unique_ptr<CPDF_Font> pFont);
CPDF_CMapManager m_CMapManager;
struct {
diff --git a/core/fpdfapi/fpdf_font/fpdf_font.cpp b/core/fpdfapi/fpdf_font/fpdf_font.cpp
index 1cb67be51f..a463c15da5 100644
--- a/core/fpdfapi/fpdf_font/fpdf_font.cpp
+++ b/core/fpdfapi/fpdf_font/fpdf_font.cpp
@@ -60,10 +60,12 @@ CPDF_Font* CFX_StockFontArray::GetFont(uint32_t index) const {
return m_StockFonts[index].get();
}
-void CFX_StockFontArray::SetFont(uint32_t index, CPDF_Font* font) {
- if (index >= FX_ArraySize(m_StockFonts))
- return;
- m_StockFonts[index].reset(font);
+CPDF_Font* CFX_StockFontArray::SetFont(uint32_t index,
+ std::unique_ptr<CPDF_Font> pFont) {
+ CPDF_Font* result = pFont.get();
+ if (index < FX_ArraySize(m_StockFonts))
+ m_StockFonts[index] = std::move(pFont);
+ return result;
}
CPDF_FontGlobals::CPDF_FontGlobals() {
@@ -80,12 +82,12 @@ CPDF_Font* CPDF_FontGlobals::Find(CPDF_Document* pDoc, uint32_t index) {
return it->second ? it->second->GetFont(index) : nullptr;
}
-void CPDF_FontGlobals::Set(CPDF_Document* pDoc,
- uint32_t index,
- CPDF_Font* pFont) {
+CPDF_Font* CPDF_FontGlobals::Set(CPDF_Document* pDoc,
+ uint32_t index,
+ std::unique_ptr<CPDF_Font> pFont) {
if (!pdfium::ContainsKey(m_StockMap, pDoc))
m_StockMap[pDoc].reset(new CFX_StockFontArray);
- m_StockMap[pDoc]->SetFont(index, pFont);
+ return m_StockMap[pDoc]->SetFont(index, std::move(pFont));
}
void CPDF_FontGlobals::Clear(CPDF_Document* pDoc) {
diff --git a/core/fpdfapi/fpdf_font/include/cpdf_font.h b/core/fpdfapi/fpdf_font/include/cpdf_font.h
index a47d8655c2..eb91524320 100644
--- a/core/fpdfapi/fpdf_font/include/cpdf_font.h
+++ b/core/fpdfapi/fpdf_font/include/cpdf_font.h
@@ -38,8 +38,8 @@ class CPDF_ToUnicodeMap;
class CPDF_Font {
public:
- static CPDF_Font* CreateFontF(CPDF_Document* pDoc,
- CPDF_Dictionary* pFontDict);
+ static std::unique_ptr<CPDF_Font> Create(CPDF_Document* pDoc,
+ CPDF_Dictionary* pFontDict);
static CPDF_Font* GetStockFont(CPDF_Document* pDoc,
const CFX_ByteStringC& fontname);
static const uint32_t kInvalidCharCode = static_cast<uint32_t>(-1);
diff --git a/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp b/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp
index 77c18df5fa..c4e2a24424 100644
--- a/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp
+++ b/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp
@@ -145,14 +145,14 @@ CPDF_Font* CPDF_DocPageData::GetFont(CPDF_Dictionary* pFontDict,
if (findOnly)
return nullptr;
- CPDF_Font* pFont = CPDF_Font::CreateFontF(m_pPDFDoc, pFontDict);
+ std::unique_ptr<CPDF_Font> pFont = CPDF_Font::Create(m_pPDFDoc, pFontDict);
if (!pFont)
return nullptr;
if (pFontData) {
- pFontData->reset(pFont);
+ pFontData->reset(pFont.release());
} else {
- pFontData = new CPDF_CountedFont(pFont);
+ pFontData = new CPDF_CountedFont(pFont.release());
m_FontMap[pFontDict] = pFontData;
}
return pFontData->AddRef();
@@ -192,11 +192,11 @@ CPDF_Font* CPDF_DocPageData::GetStandardFont(const CFX_ByteString& fontName,
pDict->SetFor("Encoding", pEncoding->Realize());
}
m_pPDFDoc->AddIndirectObject(pDict);
- CPDF_Font* pFont = CPDF_Font::CreateFontF(m_pPDFDoc, pDict);
- if (!pFont) {
+ std::unique_ptr<CPDF_Font> pFont = CPDF_Font::Create(m_pPDFDoc, pDict);
+ if (!pFont)
return nullptr;
- }
- CPDF_CountedFont* fontData = new CPDF_CountedFont(pFont);
+
+ CPDF_CountedFont* fontData = new CPDF_CountedFont(pFont.release());
m_FontMap[pDict] = fontData;
return fontData->AddRef();
}