From 9b671ace8aec906683b05399cf8a2882ed8ef7b6 Mon Sep 17 00:00:00 2001 From: weili Date: Mon, 25 Jul 2016 07:40:27 -0700 Subject: Use smart pointers for CFX_Font and CFX_Type3Font classes For the class owned member variables, use std::unique_ptr or std::vector for memory management. BUG=pdfium:518 Review-Url: https://codereview.chromium.org/2169793002 --- core/fxge/ge/fx_ge_font.cpp | 104 ++++++++++++++++++++------------------------ core/fxge/include/fx_font.h | 12 +++-- 2 files changed, 56 insertions(+), 60 deletions(-) (limited to 'core/fxge') diff --git a/core/fxge/ge/fx_ge_font.cpp b/core/fxge/ge/fx_ge_font.cpp index bd0bb0e3e5..1aa5f9fd1a 100644 --- a/core/fxge/ge/fx_ge_font.cpp +++ b/core/fxge/ge/fx_ge_font.cpp @@ -32,6 +32,41 @@ CFX_UnicodeEncodingEx* _FXFM_CreateFontEncoding(CFX_Font* pFont, return nullptr; return new CFX_UnicodeEncodingEx(pFont, nEncodingID); } + +unsigned long FTStreamRead(FXFT_Stream stream, + unsigned long offset, + unsigned char* buffer, + unsigned long count) { + if (count == 0) + return 0; + + IFX_FileRead* pFile = static_cast(stream->descriptor.pointer); + return pFile->ReadBlock(buffer, offset, count) ? count : 0; +} + +void FTStreamClose(FXFT_Stream stream) {} + +FX_BOOL LoadFileImp(FXFT_Library library, + FXFT_Face* Face, + IFX_FileRead* pFile, + int32_t faceIndex, + std::unique_ptr* stream) { + std::unique_ptr stream1(new FXFT_StreamRec()); + stream1->base = nullptr; + stream1->size = static_cast(pFile->GetSize()); + stream1->pos = 0; + stream1->descriptor.pointer = pFile; + stream1->close = FTStreamClose; + stream1->read = FTStreamRead; + FXFT_Open_Args args; + args.flags = FT_OPEN_STREAM; + args.stream = stream1.get(); + if (FXFT_Open_Face(library, &args, faceIndex, Face)) + return FALSE; + if (stream) + *stream = std::move(stream1); + return TRUE; +} #endif // PDF_ENABLE_XFA FXFT_Face FT_LoadFont(const uint8_t* pData, int size) { @@ -48,8 +83,6 @@ CFX_Font::CFX_Font() #else : m_Face(nullptr), #endif // PDF_ENABLE_XFA - m_pSubstFont(nullptr), - m_pFontDataAllocation(nullptr), m_pFontData(nullptr), m_pGsubData(nullptr), m_dwSize(0), @@ -67,7 +100,7 @@ FX_BOOL CFX_Font::LoadClone(const CFX_Font* pFont) { m_bLogic = TRUE; if (pFont->m_pSubstFont) { - m_pSubstFont = new CFX_SubstFont; + m_pSubstFont.reset(new CFX_SubstFont); m_pSubstFont->m_Charset = pFont->m_pSubstFont->m_Charset; m_pSubstFont->m_ExtHandle = pFont->m_pSubstFont->m_ExtHandle; m_pSubstFont->m_SubstFlags = pFont->m_pSubstFont->m_SubstFlags; @@ -94,8 +127,6 @@ FX_BOOL CFX_Font::LoadClone(const CFX_Font* pFont) { #endif // PDF_ENABLE_XFA CFX_Font::~CFX_Font() { - delete m_pSubstFont; - FX_Free(m_pFontDataAllocation); #ifdef PDF_ENABLE_XFA if (m_bLogic) { m_OtfFontData.DetachBuffer(); @@ -122,10 +153,12 @@ CFX_Font::~CFX_Font() { ReleasePlatformResource(); #endif } + void CFX_Font::DeleteFace() { FXFT_Done_Face(m_Face); m_Face = nullptr; } + void CFX_Font::LoadSubst(const CFX_ByteString& face_name, FX_BOOL bTrueType, uint32_t flags, @@ -135,10 +168,10 @@ void CFX_Font::LoadSubst(const CFX_ByteString& face_name, FX_BOOL bVertical) { m_bEmbedded = FALSE; m_bVertical = bVertical; - m_pSubstFont = new CFX_SubstFont; + m_pSubstFont.reset(new CFX_SubstFont); m_Face = CFX_GEModule::Get()->GetFontMgr()->FindSubstFont( face_name, bTrueType, flags, weight, italic_angle, CharsetCP, - m_pSubstFont); + m_pSubstFont.get()); #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ if (m_pSubstFont->m_ExtHandle) { m_pPlatformFont = m_pSubstFont->m_ExtHandle; @@ -150,49 +183,8 @@ void CFX_Font::LoadSubst(const CFX_ByteString& face_name, m_dwSize = FXFT_Get_Face_Stream_Size(m_Face); } } -#ifdef PDF_ENABLE_XFA -extern "C" { -unsigned long _FTStreamRead(FXFT_Stream stream, - unsigned long offset, - unsigned char* buffer, - unsigned long count) { - if (count == 0) { - return 0; - } - IFX_FileRead* pFile = (IFX_FileRead*)stream->descriptor.pointer; - int res = pFile->ReadBlock(buffer, offset, count); - if (res) { - return count; - } - return 0; -} -void _FTStreamClose(FXFT_Stream stream) {} -}; -FX_BOOL _LoadFile(FXFT_Library library, - FXFT_Face* Face, - IFX_FileRead* pFile, - FXFT_Stream* stream, - int32_t faceIndex = 0) { - FXFT_Stream stream1 = (FXFT_Stream)FX_Alloc(uint8_t, sizeof(FXFT_StreamRec)); - stream1->base = nullptr; - stream1->size = (unsigned long)pFile->GetSize(); - stream1->pos = 0; - stream1->descriptor.pointer = pFile; - stream1->close = _FTStreamClose; - stream1->read = _FTStreamRead; - FXFT_Open_Args args; - args.flags = FT_OPEN_STREAM; - args.stream = stream1; - if (FXFT_Open_Face(library, &args, faceIndex, Face)) { - FX_Free(stream1); - return FALSE; - } - if (stream) { - *stream = stream1; - } - return TRUE; -} +#ifdef PDF_ENABLE_XFA FX_BOOL CFX_Font::LoadFile(IFX_FileRead* pFile, int nFaceIndex, int* pFaceCount) { @@ -202,13 +194,13 @@ FX_BOOL CFX_Font::LoadFile(IFX_FileRead* pFile, pFontMgr->InitFTLibrary(); FXFT_Library library = pFontMgr->GetFTLibrary(); - FXFT_Stream stream = nullptr; - if (!_LoadFile(library, &m_Face, pFile, &stream, nFaceIndex)) + std::unique_ptr stream; + if (!LoadFileImp(library, &m_Face, pFile, nFaceIndex, &stream)) return FALSE; if (pFaceCount) *pFaceCount = (int)m_Face->num_faces; - m_pOwnedStream = stream; + m_pOwnedStream = stream.release(); FXFT_Set_Pixel_Sizes(m_Face, 0, 64); return TRUE; } @@ -233,10 +225,10 @@ int CFX_Font::GetGlyphWidth(uint32_t glyph_index) { } FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, uint32_t size) { - m_pFontDataAllocation = FX_Alloc(uint8_t, size); - FXSYS_memcpy(m_pFontDataAllocation, data, size); - m_Face = FT_LoadFont(m_pFontDataAllocation, size); - m_pFontData = m_pFontDataAllocation; + std::vector temp(data, data + size); + m_pFontDataAllocation.swap(temp); + m_Face = FT_LoadFont(m_pFontDataAllocation.data(), size); + m_pFontData = m_pFontDataAllocation.data(); m_bEmbedded = TRUE; m_dwSize = size; return !!m_Face; diff --git a/core/fxge/include/fx_font.h b/core/fxge/include/fx_font.h index 8c9bdb92c4..9b43f16710 100644 --- a/core/fxge/include/fx_font.h +++ b/core/fxge/include/fx_font.h @@ -86,7 +86,7 @@ class CFX_Font { FX_BOOL LoadEmbedded(const uint8_t* data, uint32_t size); FXFT_Face GetFace() const { return m_Face; } - CFX_SubstFont* GetSubstFont() const { return m_pSubstFont; } + CFX_SubstFont* GetSubstFont() const { return m_pSubstFont.get(); } #ifdef PDF_ENABLE_XFA FX_BOOL LoadFile(IFX_FileRead* pFile, @@ -95,7 +95,9 @@ class CFX_Font { FX_BOOL LoadClone(const CFX_Font* pFont); void SetFace(FXFT_Face face) { m_Face = face; } - void SetSubstFont(CFX_SubstFont* subst) { m_pSubstFont = subst; } + void SetSubstFont(std::unique_ptr subst) { + m_pSubstFont = std::move(subst); + } #endif // PDF_ENABLE_XFA CFX_PathData* LoadGlyphPath(uint32_t glyph_index, int dest_width = 0); @@ -139,8 +141,8 @@ class CFX_Font { void DeleteFace(); FXFT_Face m_Face; - CFX_SubstFont* m_pSubstFont; - uint8_t* m_pFontDataAllocation; + std::unique_ptr m_pSubstFont; + std::vector m_pFontDataAllocation; uint8_t* m_pFontData; uint8_t* m_pGsubData; uint32_t m_dwSize; @@ -501,6 +503,7 @@ class CFX_AutoFontCache { CFX_FontCache* m_pFontCache; CFX_Font* m_pFont; }; + #define FX_FONTCACHE_DEFINE(pFontCache, pFont) \ CFX_AutoFontCache autoFontCache((pFontCache), (pFont)) class CFX_GlyphBitmap { @@ -509,6 +512,7 @@ class CFX_GlyphBitmap { int m_Left; CFX_DIBitmap m_Bitmap; }; + class CFX_FaceCache { public: explicit CFX_FaceCache(FXFT_Face face); -- cgit v1.2.3