summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2017-01-19 06:56:13 -0800
committerCommit bot <commit-bot@chromium.org>2017-01-19 06:56:13 -0800
commitdd533baad22f5143c093b98e98463a0dc62899ea (patch)
treee5704fa1f4a8e08109d7d42d10b77a2eb8826e35
parente6cf01356e3336dc4f0717a391d9067693a418c1 (diff)
downloadpdfium-dd533baad22f5143c093b98e98463a0dc62899ea.tar.xz
use unique_ptr in fpdf_font_cid.cpp
Also return CFX_MaybeOwned<> from functions so to make cleanup automatic, avoiding a theoretical leak. Review-Url: https://codereview.chromium.org/2641853004
-rw-r--r--core/fpdfapi/font/cpdf_cidfont.cpp6
-rw-r--r--core/fpdfapi/font/cpdf_cidfont.h4
-rw-r--r--core/fpdfapi/font/font_int.h14
-rw-r--r--core/fpdfapi/font/fpdf_font_cid.cpp65
4 files changed, 47 insertions, 42 deletions
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index c6c6992562..e96f824bd5 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -200,8 +200,7 @@ bool IsMetricForCID(const uint32_t* pEntry, uint16_t CID) {
} // namespace
CPDF_CIDFont::CPDF_CIDFont()
- : m_pCMap(nullptr),
- m_pCID2UnicodeMap(nullptr),
+ : m_pCID2UnicodeMap(nullptr),
m_bCIDIsGID(false),
m_bAnsiWidthsFixed(false),
m_bAdobeCourierStd(false) {
@@ -360,8 +359,7 @@ bool CPDF_CIDFont::Load() {
if (!m_pCMap)
return false;
} else if (CPDF_Stream* pStream = pEncoding->AsStream()) {
- m_pCMap = new CPDF_CMap;
- m_pAllocatedCMap.reset(m_pCMap);
+ m_pCMap = pdfium::MakeUnique<CPDF_CMap>();
CPDF_StreamAcc acc;
acc.LoadAllData(pStream, false);
m_pCMap->LoadEmbedded(acc.GetData(), acc.GetSize());
diff --git a/core/fpdfapi/font/cpdf_cidfont.h b/core/fpdfapi/font/cpdf_cidfont.h
index ae3be3908f..79ec7cbb35 100644
--- a/core/fpdfapi/font/cpdf_cidfont.h
+++ b/core/fpdfapi/font/cpdf_cidfont.h
@@ -11,6 +11,7 @@
#include <vector>
#include "core/fpdfapi/font/cpdf_font.h"
+#include "core/fxcrt/cfx_maybe_owned.h"
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/fx_system.h"
@@ -71,8 +72,7 @@ class CPDF_CIDFont : public CPDF_Font {
void LoadSubstFont();
FX_WCHAR GetUnicodeFromCharCode(uint32_t charcode) const;
- CPDF_CMap* m_pCMap;
- std::unique_ptr<CPDF_CMap> m_pAllocatedCMap;
+ CFX_MaybeOwned<CPDF_CMap> m_pCMap;
CPDF_CID2UnicodeMap* m_pCID2UnicodeMap;
CIDSet m_Charset;
bool m_bType1;
diff --git a/core/fpdfapi/font/font_int.h b/core/fpdfapi/font/font_int.h
index 2410741e53..41d821ec81 100644
--- a/core/fpdfapi/font/font_int.h
+++ b/core/fpdfapi/font/font_int.h
@@ -12,6 +12,7 @@
#include <vector>
#include "core/fpdfapi/font/cpdf_cidfont.h"
+#include "core/fxcrt/cfx_maybe_owned.h"
#include "core/fxcrt/fx_basic.h"
class CPDF_CID2UnicodeMap;
@@ -30,15 +31,18 @@ class CPDF_CMapManager {
CPDF_CMapManager();
~CPDF_CMapManager();
- CPDF_CMap* GetPredefinedCMap(const CFX_ByteString& name, bool bPromptCJK);
+ CFX_MaybeOwned<CPDF_CMap> GetPredefinedCMap(const CFX_ByteString& name,
+ bool bPromptCJK);
CPDF_CID2UnicodeMap* GetCID2UnicodeMap(CIDSet charset, bool bPromptCJK);
private:
- CPDF_CMap* LoadPredefinedCMap(const CFX_ByteString& name, bool bPromptCJK);
- CPDF_CID2UnicodeMap* LoadCID2UnicodeMap(CIDSet charset, bool bPromptCJK);
+ std::unique_ptr<CPDF_CMap> LoadPredefinedCMap(const CFX_ByteString& name,
+ bool bPromptCJK);
+ std::unique_ptr<CPDF_CID2UnicodeMap> LoadCID2UnicodeMap(CIDSet charset,
+ bool bPromptCJK);
- std::map<CFX_ByteString, CPDF_CMap*> m_CMaps;
- CPDF_CID2UnicodeMap* m_CID2UnicodeMaps[6];
+ std::map<CFX_ByteString, std::unique_ptr<CPDF_CMap>> m_CMaps;
+ std::unique_ptr<CPDF_CID2UnicodeMap> m_CID2UnicodeMaps[6];
};
class CFX_StockFontArray {
diff --git a/core/fpdfapi/font/fpdf_font_cid.cpp b/core/fpdfapi/font/fpdf_font_cid.cpp
index 010c94ced2..386eec334a 100644
--- a/core/fpdfapi/font/fpdf_font_cid.cpp
+++ b/core/fpdfapi/font/fpdf_font_cid.cpp
@@ -6,6 +6,9 @@
#include "core/fpdfapi/font/font_int.h"
+#include <memory>
+#include <utility>
+
#include "core/fpdfapi/cmaps/cmap_int.h"
#include "core/fpdfapi/cpdf_modulemgr.h"
#include "core/fpdfapi/font/ttgsubtable.h"
@@ -278,37 +281,34 @@ int GetCharSizeImpl(uint32_t charcode,
} // namespace
-CPDF_CMapManager::CPDF_CMapManager() {
- 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,
- bool bPromptCJK) {
+CPDF_CMapManager::CPDF_CMapManager() {}
+
+CPDF_CMapManager::~CPDF_CMapManager() {}
+
+CFX_MaybeOwned<CPDF_CMap> CPDF_CMapManager::GetPredefinedCMap(
+ const CFX_ByteString& name,
+ 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;
+ if (it != m_CMaps.end())
+ return CFX_MaybeOwned<CPDF_CMap>(it->second.get()); // Unowned.
+
+ std::unique_ptr<CPDF_CMap> pCMap = LoadPredefinedCMap(name, bPromptCJK);
+ if (name.IsEmpty())
+ return CFX_MaybeOwned<CPDF_CMap>(std::move(pCMap)); // Owned.
+
+ CPDF_CMap* pUnowned = pCMap.get();
+ m_CMaps[name] = std::move(pCMap);
+ return CFX_MaybeOwned<CPDF_CMap>(pUnowned); // Unowned.
}
-CPDF_CMap* CPDF_CMapManager::LoadPredefinedCMap(const CFX_ByteString& name,
- bool bPromptCJK) {
- CPDF_CMap* pCMap = new CPDF_CMap;
+
+std::unique_ptr<CPDF_CMap> CPDF_CMapManager::LoadPredefinedCMap(
+ const CFX_ByteString& name,
+ bool bPromptCJK) {
+ auto pCMap = pdfium::MakeUnique<CPDF_CMap>();
const FX_CHAR* pname = name.c_str();
- if (*pname == '/') {
+ if (*pname == '/')
pname++;
- }
+
pCMap->LoadPredefined(this, pname, bPromptCJK);
return pCMap;
}
@@ -317,11 +317,14 @@ CPDF_CID2UnicodeMap* CPDF_CMapManager::GetCID2UnicodeMap(CIDSet charset,
bool bPromptCJK) {
if (!m_CID2UnicodeMaps[charset])
m_CID2UnicodeMaps[charset] = LoadCID2UnicodeMap(charset, bPromptCJK);
- return m_CID2UnicodeMaps[charset];
+
+ return m_CID2UnicodeMaps[charset].get();
}
-CPDF_CID2UnicodeMap* CPDF_CMapManager::LoadCID2UnicodeMap(CIDSet charset,
- bool bPromptCJK) {
- CPDF_CID2UnicodeMap* pMap = new CPDF_CID2UnicodeMap();
+
+std::unique_ptr<CPDF_CID2UnicodeMap> CPDF_CMapManager::LoadCID2UnicodeMap(
+ CIDSet charset,
+ bool bPromptCJK) {
+ auto pMap = pdfium::MakeUnique<CPDF_CID2UnicodeMap>();
pMap->Load(this, charset, bPromptCJK);
return pMap;
}