From da65a8ea74d2748c93015fb5521da678e29e6548 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 30 Apr 2018 21:02:03 +0000 Subject: Saner memory managment in cttfontdesc, part 1. A subsequent patch will tackle the ad-hoc ref counting, but we can tidy this before going down that hole. Decouple CTTFontDesc creation from face setting. Remove union and treat single-entry case as vector's first element. Pass unique_ptr to prove memory ownership. Change-Id: Ic427798da04f3afbb65a56ee10045b9f22457a73 Reviewed-on: https://pdfium-review.googlesource.com/31730 Reviewed-by: dsinclair Commit-Queue: Tom Sepez --- core/fxge/cfx_fontmapper.cpp | 52 ++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 24 deletions(-) (limited to 'core/fxge/cfx_fontmapper.cpp') diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp index b3e88c00fe..c0b111a51e 100644 --- a/core/fxge/cfx_fontmapper.cpp +++ b/core/fxge/cfx_fontmapper.cpp @@ -751,22 +751,25 @@ FXFT_Face CFX_FontMapper::GetCachedTTCFace(void* hFont, const uint32_t tableTTCF, uint32_t ttc_size, uint32_t font_size) { - uint8_t buffer[1024]; - m_pFontInfo->GetFontData(hFont, tableTTCF, buffer, FX_ArraySize(buffer)); - uint32_t* pBuffer = reinterpret_cast(buffer); uint32_t checksum = 0; - for (int i = 0; i < 256; i++) - checksum += pBuffer[i]; - uint8_t* pFontData; - FXFT_Face face = m_pFontMgr->GetCachedTTCFace( - ttc_size, checksum, ttc_size - font_size, &pFontData); - if (!face) { - pFontData = FX_Alloc(uint8_t, ttc_size); - m_pFontInfo->GetFontData(hFont, tableTTCF, pFontData, ttc_size); - face = m_pFontMgr->AddCachedTTCFace(ttc_size, checksum, pFontData, ttc_size, - ttc_size - font_size); + { + uint8_t buffer[1024]; + m_pFontInfo->GetFontData(hFont, tableTTCF, buffer, sizeof(buffer)); + uint32_t* pBuffer = reinterpret_cast(buffer); + for (int i = 0; i < 256; i++) + checksum += pBuffer[i]; } - return face; + uint8_t* pIgnore = nullptr; + FXFT_Face face = m_pFontMgr->GetCachedTTCFace(ttc_size, checksum, + ttc_size - font_size, &pIgnore); + if (face) + return face; + + std::unique_ptr pFontData( + FX_Alloc(uint8_t, ttc_size)); + m_pFontInfo->GetFontData(hFont, tableTTCF, pFontData.get(), ttc_size); + return m_pFontMgr->AddCachedTTCFace(ttc_size, checksum, std::move(pFontData), + ttc_size, ttc_size - font_size); } FXFT_Face CFX_FontMapper::GetCachedFace(void* hFont, @@ -774,17 +777,18 @@ FXFT_Face CFX_FontMapper::GetCachedFace(void* hFont, int weight, bool bItalic, uint32_t font_size) { - uint8_t* pFontData; + uint8_t* pIgnore = nullptr; FXFT_Face face = - m_pFontMgr->GetCachedFace(SubstName, weight, bItalic, &pFontData); - if (!face) { - pFontData = FX_Alloc(uint8_t, font_size); - m_pFontInfo->GetFontData(hFont, 0, pFontData, font_size); - face = - m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, pFontData, - font_size, m_pFontInfo->GetFaceIndex(hFont)); - } - return face; + m_pFontMgr->GetCachedFace(SubstName, weight, bItalic, &pIgnore); + if (face) + return face; + + std::unique_ptr pFontData( + FX_Alloc(uint8_t, font_size)); + m_pFontInfo->GetFontData(hFont, 0, pFontData.get(), font_size); + return m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, + std::move(pFontData), font_size, + m_pFontInfo->GetFaceIndex(hFont)); } int PDF_GetStandardFontName(ByteString* name) { -- cgit v1.2.3