diff options
author | Tom Sepez <tsepez@chromium.org> | 2017-01-30 09:56:43 -0800 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-01-30 18:16:45 +0000 |
commit | 4ada364e183e05479bc45d2ce41700ae18b7f6a3 (patch) | |
tree | 435adbe6e6bdb202f150588793aad6afd3d02c4a /core/fpdfapi/font/ttgsubtable.cpp | |
parent | 5535ac7897358d9b5f2f7eb508f4aa44791e501f (diff) | |
download | pdfium-4ada364e183e05479bc45d2ce41700ae18b7f6a3.tar.xz |
Cleanup memory allocations in ttgsubtable.cpp
Rename array members to plurals.
Use std::vectors rather than native arrays and counts.
Use std::set instead of map of key to itself.
Use unique_ptr inside vector to clean subtables.
Mark non-existent constructors() = delete.
Change-Id: Ifde189157e8affb5de654a44fac8b9358de7079b
Reviewed-on: https://pdfium-review.googlesource.com/2452
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fpdfapi/font/ttgsubtable.cpp')
-rw-r--r-- | core/fpdfapi/font/ttgsubtable.cpp | 278 |
1 files changed, 101 insertions, 177 deletions
diff --git a/core/fpdfapi/font/ttgsubtable.cpp b/core/fpdfapi/font/ttgsubtable.cpp index c037746c1e..51e8e9cc20 100644 --- a/core/fpdfapi/font/ttgsubtable.cpp +++ b/core/fpdfapi/font/ttgsubtable.cpp @@ -7,6 +7,7 @@ #include "core/fpdfapi/font/ttgsubtable.h" #include "core/fxge/fx_freetype.h" +#include "third_party/base/ptr_util.h" #include "third_party/base/stl_util.h" CFX_GlyphMap::CFX_GlyphMap() {} @@ -93,33 +94,29 @@ bool CFX_CTTGSUBTable::GetVerticalGlyph(uint32_t glyphnum, (uint8_t)'t', }; if (!m_bFeautureMapLoad) { - for (int i = 0; i < ScriptList.ScriptCount; i++) { - for (int j = 0; j < ScriptList.ScriptRecord[i].Script.LangSysCount; ++j) { - const auto& record = ScriptList.ScriptRecord[i].Script.LangSysRecord[j]; - for (int k = 0; k < record.LangSys.FeatureCount; ++k) { - uint32_t index = record.LangSys.FeatureIndex[k]; - if (FeatureList.FeatureRecord[index].FeatureTag == tag[0] || - FeatureList.FeatureRecord[index].FeatureTag == tag[1]) { - if (!pdfium::ContainsKey(m_featureMap, index)) { - m_featureMap[index] = index; - } + for (const auto& script : ScriptList.ScriptRecords) { + for (const auto& record : script.Script.LangSysRecords) { + for (const auto& index : record.LangSys.FeatureIndices) { + if (FeatureList.FeatureRecords[index].FeatureTag == tag[0] || + FeatureList.FeatureRecords[index].FeatureTag == tag[1]) { + m_featureSet.insert(index); } } } } - if (m_featureMap.empty()) { - for (int i = 0; i < FeatureList.FeatureCount; i++) { - if (FeatureList.FeatureRecord[i].FeatureTag == tag[0] || - FeatureList.FeatureRecord[i].FeatureTag == tag[1]) { - m_featureMap[i] = i; - } + if (m_featureSet.empty()) { + int i = 0; + for (const auto& feature : FeatureList.FeatureRecords) { + if (feature.FeatureTag == tag[0] || feature.FeatureTag == tag[1]) + m_featureSet.insert(i); + ++i; } } m_bFeautureMapLoad = true; } - for (const auto& pair : m_featureMap) { + for (const auto& item : m_featureSet) { if (GetVerticalGlyphSub(glyphnum, vglyphnum, - &FeatureList.FeatureRecord[pair.second].Feature)) { + &FeatureList.FeatureRecords[item].Feature)) { return true; } } @@ -128,17 +125,14 @@ bool CFX_CTTGSUBTable::GetVerticalGlyph(uint32_t glyphnum, bool CFX_CTTGSUBTable::GetVerticalGlyphSub(uint32_t glyphnum, uint32_t* vglyphnum, - TFeature* Feature) const { - for (int i = 0; i < Feature->LookupCount; i++) { - int index = Feature->LookupListIndex[i]; - if (index < 0 || LookupList.LookupCount < index) { + TFeature* Feature) { + for (int index : Feature->LookupListIndices) { + if (index < 0 || index >= pdfium::CollectionSize<int>(LookupList.Lookups)) continue; - } - if (LookupList.Lookup[index].LookupType == 1) { - if (GetVerticalGlyphSub2(glyphnum, vglyphnum, - &LookupList.Lookup[index])) { - return true; - } + + if (LookupList.Lookups[index].LookupType == 1 && + GetVerticalGlyphSub2(glyphnum, vglyphnum, &LookupList.Lookups[index])) { + return true; } } return false; @@ -146,11 +140,11 @@ bool CFX_CTTGSUBTable::GetVerticalGlyphSub(uint32_t glyphnum, bool CFX_CTTGSUBTable::GetVerticalGlyphSub2(uint32_t glyphnum, uint32_t* vglyphnum, - TLookup* Lookup) const { - for (int i = 0; i < Lookup->SubTableCount; i++) { - switch (Lookup->SubTable[i]->SubstFormat) { + TLookup* Lookup) { + for (const auto& subTable : Lookup->SubTables) { + switch (subTable->SubstFormat) { case 1: { - TSingleSubstFormat1* tbl1 = (TSingleSubstFormat1*)Lookup->SubTable[i]; + auto tbl1 = static_cast<TSingleSubstFormat1*>(subTable.get()); if (GetCoverageIndex(tbl1->Coverage.get(), glyphnum) >= 0) { *vglyphnum = glyphnum + tbl1->DeltaGlyphID; return true; @@ -158,11 +152,11 @@ bool CFX_CTTGSUBTable::GetVerticalGlyphSub2(uint32_t glyphnum, break; } case 2: { - TSingleSubstFormat2* tbl2 = (TSingleSubstFormat2*)Lookup->SubTable[i]; - int index = -1; - index = GetCoverageIndex(tbl2->Coverage.get(), glyphnum); - if (0 <= index && index < tbl2->GlyphCount) { - *vglyphnum = tbl2->Substitute[index]; + auto tbl2 = static_cast<TSingleSubstFormat2*>(subTable.get()); + int index = GetCoverageIndex(tbl2->Coverage.get(), glyphnum); + if (index >= 0 && + index < pdfium::CollectionSize<int>(tbl2->Substitutes)) { + *vglyphnum = tbl2->Substitutes[index]; return true; } break; @@ -174,29 +168,28 @@ bool CFX_CTTGSUBTable::GetVerticalGlyphSub2(uint32_t glyphnum, int CFX_CTTGSUBTable::GetCoverageIndex(TCoverageFormatBase* Coverage, uint32_t g) const { - int i = 0; - if (!Coverage) { + if (!Coverage) return -1; - } + switch (Coverage->CoverageFormat) { case 1: { + int i = 0; TCoverageFormat1* c1 = (TCoverageFormat1*)Coverage; - for (i = 0; i < c1->GlyphCount; i++) { - if ((uint32_t)c1->GlyphArray[i] == g) { + for (const auto& glyph : c1->GlyphArray) { + if (static_cast<uint32_t>(glyph) == g) return i; - } + ++i; } return -1; } case 2: { TCoverageFormat2* c2 = (TCoverageFormat2*)Coverage; - for (i = 0; i < c2->RangeCount; i++) { - uint32_t s = c2->RangeRecord[i].Start; - uint32_t e = c2->RangeRecord[i].End; - uint32_t si = c2->RangeRecord[i].StartCoverageIndex; - if (s <= g && g <= e) { + for (const auto& rangeRec : c2->RangeRecords) { + uint32_t s = rangeRec.Start; + uint32_t e = rangeRec.End; + uint32_t si = rangeRec.StartCoverageIndex; + if (s <= g && g <= e) return si + g - s; - } } return -1; } @@ -244,33 +237,21 @@ bool CFX_CTTGSUBTable::Parse(FT_Bytes scriptlist, } void CFX_CTTGSUBTable::ParseScriptList(FT_Bytes raw, TScriptList* rec) { - int i; FT_Bytes sp = raw; - rec->ScriptCount = GetUInt16(sp); - if (rec->ScriptCount <= 0) { - return; - } - rec->ScriptRecord.reset(new TScriptRecord[rec->ScriptCount]); - for (i = 0; i < rec->ScriptCount; i++) { - rec->ScriptRecord[i].ScriptTag = GetUInt32(sp); - uint16_t offset = GetUInt16(sp); - ParseScript(&raw[offset], &rec->ScriptRecord[i].Script); + rec->ScriptRecords = std::vector<TScriptRecord>(GetUInt16(sp)); + for (auto& scriptRec : rec->ScriptRecords) { + scriptRec.ScriptTag = GetUInt32(sp); + ParseScript(&raw[GetUInt16(sp)], &scriptRec.Script); } } void CFX_CTTGSUBTable::ParseScript(FT_Bytes raw, TScript* rec) { - int i; FT_Bytes sp = raw; rec->DefaultLangSys = GetUInt16(sp); - rec->LangSysCount = GetUInt16(sp); - if (rec->LangSysCount <= 0) { - return; - } - rec->LangSysRecord.reset(new TLangSysRecord[rec->LangSysCount]); - for (i = 0; i < rec->LangSysCount; i++) { - rec->LangSysRecord[i].LangSysTag = GetUInt32(sp); - uint16_t offset = GetUInt16(sp); - ParseLangSys(&raw[offset], &rec->LangSysRecord[i].LangSys); + rec->LangSysRecords = std::vector<TLangSysRecord>(GetUInt16(sp)); + for (auto& sysRecord : rec->LangSysRecords) { + sysRecord.LangSysTag = GetUInt32(sp); + ParseLangSys(&raw[GetUInt16(sp)], &sysRecord.LangSys); } } @@ -278,81 +259,45 @@ void CFX_CTTGSUBTable::ParseLangSys(FT_Bytes raw, TLangSys* rec) { FT_Bytes sp = raw; rec->LookupOrder = GetUInt16(sp); rec->ReqFeatureIndex = GetUInt16(sp); - rec->FeatureCount = GetUInt16(sp); - if (rec->FeatureCount <= 0) { - return; - } - rec->FeatureIndex.reset(new uint16_t[rec->FeatureCount]); - FXSYS_memset(rec->FeatureIndex.get(), 0, - sizeof(uint16_t) * rec->FeatureCount); - for (int i = 0; i < rec->FeatureCount; ++i) { - rec->FeatureIndex[i] = GetUInt16(sp); - } + rec->FeatureIndices = std::vector<uint16_t>(GetUInt16(sp)); + for (auto& element : rec->FeatureIndices) + element = GetUInt16(sp); } void CFX_CTTGSUBTable::ParseFeatureList(FT_Bytes raw, TFeatureList* rec) { - int i; FT_Bytes sp = raw; - rec->FeatureCount = GetUInt16(sp); - if (rec->FeatureCount <= 0) { - return; - } - rec->FeatureRecord.reset(new TFeatureRecord[rec->FeatureCount]); - for (i = 0; i < rec->FeatureCount; i++) { - rec->FeatureRecord[i].FeatureTag = GetUInt32(sp); - uint16_t offset = GetUInt16(sp); - ParseFeature(&raw[offset], &rec->FeatureRecord[i].Feature); + rec->FeatureRecords = std::vector<TFeatureRecord>(GetUInt16(sp)); + for (auto& featureRec : rec->FeatureRecords) { + featureRec.FeatureTag = GetUInt32(sp); + ParseFeature(&raw[GetUInt16(sp)], &featureRec.Feature); } } void CFX_CTTGSUBTable::ParseFeature(FT_Bytes raw, TFeature* rec) { - int i; FT_Bytes sp = raw; rec->FeatureParams = GetUInt16(sp); - rec->LookupCount = GetUInt16(sp); - if (rec->LookupCount <= 0) { - return; - } - rec->LookupListIndex.reset(new uint16_t[rec->LookupCount]); - for (i = 0; i < rec->LookupCount; i++) { - rec->LookupListIndex[i] = GetUInt16(sp); - } + rec->LookupListIndices = std::vector<uint16_t>(GetUInt16(sp)); + for (auto& listIndex : rec->LookupListIndices) + listIndex = GetUInt16(sp); } void CFX_CTTGSUBTable::ParseLookupList(FT_Bytes raw, TLookupList* rec) { - int i; FT_Bytes sp = raw; - rec->LookupCount = GetUInt16(sp); - if (rec->LookupCount <= 0) { - return; - } - rec->Lookup.reset(new TLookup[rec->LookupCount]); - for (i = 0; i < rec->LookupCount; i++) { - uint16_t offset = GetUInt16(sp); - ParseLookup(&raw[offset], &rec->Lookup[i]); - } + rec->Lookups = std::vector<TLookup>(GetUInt16(sp)); + for (auto& lookup : rec->Lookups) + ParseLookup(&raw[GetUInt16(sp)], &lookup); } void CFX_CTTGSUBTable::ParseLookup(FT_Bytes raw, TLookup* rec) { - int i; FT_Bytes sp = raw; rec->LookupType = GetUInt16(sp); rec->LookupFlag = GetUInt16(sp); - rec->SubTableCount = GetUInt16(sp); - if (rec->SubTableCount <= 0) { - return; - } - rec->SubTable.reset(new TSubTableBase*[rec->SubTableCount]); - for (i = 0; i < rec->SubTableCount; i++) { - rec->SubTable[i] = nullptr; - } - if (rec->LookupType != 1) { + rec->SubTables = std::vector<std::unique_ptr<TSubTableBase>>(GetUInt16(sp)); + if (rec->LookupType != 1) return; - } - for (i = 0; i < rec->SubTableCount; i++) { - uint16_t offset = GetUInt16(sp); - ParseSingleSubst(&raw[offset], &rec->SubTable[i]); - } + + for (auto& subTable : rec->SubTables) + ParseSingleSubst(&raw[GetUInt16(sp)], &subTable); } CFX_CTTGSUBTable::TCoverageFormatBase* CFX_CTTGSUBTable::ParseCoverage( @@ -372,47 +317,39 @@ CFX_CTTGSUBTable::TCoverageFormatBase* CFX_CTTGSUBTable::ParseCoverage( void CFX_CTTGSUBTable::ParseCoverageFormat1(FT_Bytes raw, TCoverageFormat1* rec) { - int i; FT_Bytes sp = raw; - GetUInt16(sp); - rec->GlyphCount = GetUInt16(sp); - if (rec->GlyphCount <= 0) { - return; - } - rec->GlyphArray.reset(new uint16_t[rec->GlyphCount]); - for (i = 0; i < rec->GlyphCount; i++) { - rec->GlyphArray[i] = GetUInt16(sp); - } + (void)GetUInt16(sp); + rec->GlyphArray = std::vector<uint16_t>(GetUInt16(sp)); + for (auto& glyph : rec->GlyphArray) + glyph = GetUInt16(sp); } void CFX_CTTGSUBTable::ParseCoverageFormat2(FT_Bytes raw, TCoverageFormat2* rec) { - int i; FT_Bytes sp = raw; - GetUInt16(sp); - rec->RangeCount = GetUInt16(sp); - if (rec->RangeCount <= 0) { - return; - } - rec->RangeRecord.reset(new TRangeRecord[rec->RangeCount]); - for (i = 0; i < rec->RangeCount; i++) { - rec->RangeRecord[i].Start = GetUInt16(sp); - rec->RangeRecord[i].End = GetUInt16(sp); - rec->RangeRecord[i].StartCoverageIndex = GetUInt16(sp); + (void)GetUInt16(sp); + rec->RangeRecords = std::vector<TRangeRecord>(GetUInt16(sp)); + for (auto& rangeRec : rec->RangeRecords) { + rangeRec.Start = GetUInt16(sp); + rangeRec.End = GetUInt16(sp); + rangeRec.StartCoverageIndex = GetUInt16(sp); } } -void CFX_CTTGSUBTable::ParseSingleSubst(FT_Bytes raw, TSubTableBase** rec) { +void CFX_CTTGSUBTable::ParseSingleSubst(FT_Bytes raw, + std::unique_ptr<TSubTableBase>* rec) { FT_Bytes sp = raw; uint16_t Format = GetUInt16(sp); switch (Format) { case 1: - *rec = new TSingleSubstFormat1(); - ParseSingleSubstFormat1(raw, (TSingleSubstFormat1*)*rec); + *rec = pdfium::MakeUnique<TSingleSubstFormat1>(); + ParseSingleSubstFormat1(raw, + static_cast<TSingleSubstFormat1*>(rec->get())); break; case 2: - *rec = new TSingleSubstFormat2(); - ParseSingleSubstFormat2(raw, (TSingleSubstFormat2*)*rec); + *rec = pdfium::MakeUnique<TSingleSubstFormat2>(); + ParseSingleSubstFormat2(raw, + static_cast<TSingleSubstFormat2*>(rec->get())); break; } } @@ -428,23 +365,17 @@ void CFX_CTTGSUBTable::ParseSingleSubstFormat1(FT_Bytes raw, void CFX_CTTGSUBTable::ParseSingleSubstFormat2(FT_Bytes raw, TSingleSubstFormat2* rec) { - int i; FT_Bytes sp = raw; - GetUInt16(sp); + (void)GetUInt16(sp); uint16_t offset = GetUInt16(sp); rec->Coverage.reset(ParseCoverage(&raw[offset])); - rec->GlyphCount = GetUInt16(sp); - if (rec->GlyphCount <= 0) { - return; - } - rec->Substitute.reset(new uint16_t[rec->GlyphCount]); - for (i = 0; i < rec->GlyphCount; i++) { - rec->Substitute[i] = GetUInt16(sp); - } + rec->Substitutes = std::vector<uint16_t>(GetUInt16(sp)); + for (auto& substitute : rec->Substitutes) + substitute = GetUInt16(sp); } CFX_CTTGSUBTable::TCoverageFormat1::TCoverageFormat1() - : TCoverageFormatBase(1), GlyphCount(0) {} + : TCoverageFormatBase(1) {} CFX_CTTGSUBTable::TCoverageFormat1::~TCoverageFormat1() {} @@ -452,7 +383,7 @@ CFX_CTTGSUBTable::TRangeRecord::TRangeRecord() : Start(0), End(0), StartCoverageIndex(0) {} CFX_CTTGSUBTable::TCoverageFormat2::TCoverageFormat2() - : TCoverageFormatBase(2), RangeCount(0) {} + : TCoverageFormatBase(2) {} CFX_CTTGSUBTable::TCoverageFormat2::~TCoverageFormat2() {} @@ -462,41 +393,34 @@ CFX_CTTGSUBTable::TSingleSubstFormat1::TSingleSubstFormat1() CFX_CTTGSUBTable::TSingleSubstFormat1::~TSingleSubstFormat1() {} CFX_CTTGSUBTable::TSingleSubstFormat2::TSingleSubstFormat2() - : TSubTableBase(2), GlyphCount(0) {} + : TSubTableBase(2) {} CFX_CTTGSUBTable::TSingleSubstFormat2::~TSingleSubstFormat2() {} -CFX_CTTGSUBTable::TLookup::TLookup() - : LookupType(0), LookupFlag(0), SubTableCount(0) {} +CFX_CTTGSUBTable::TLookup::TLookup() : LookupType(0), LookupFlag(0) {} -CFX_CTTGSUBTable::TLookup::~TLookup() { - if (SubTable) { - for (int i = 0; i < SubTableCount; ++i) - delete SubTable[i]; - } -} +CFX_CTTGSUBTable::TLookup::~TLookup() {} -CFX_CTTGSUBTable::TScript::TScript() : DefaultLangSys(0), LangSysCount(0) {} +CFX_CTTGSUBTable::TScript::TScript() : DefaultLangSys(0) {} CFX_CTTGSUBTable::TScript::~TScript() {} -CFX_CTTGSUBTable::TScriptList::TScriptList() : ScriptCount(0) {} +CFX_CTTGSUBTable::TScriptList::TScriptList() {} CFX_CTTGSUBTable::TScriptList::~TScriptList() {} -CFX_CTTGSUBTable::TFeature::TFeature() : FeatureParams(0), LookupCount(0) {} +CFX_CTTGSUBTable::TFeature::TFeature() : FeatureParams(0) {} CFX_CTTGSUBTable::TFeature::~TFeature() {} -CFX_CTTGSUBTable::TFeatureList::TFeatureList() : FeatureCount(0) {} +CFX_CTTGSUBTable::TFeatureList::TFeatureList() {} CFX_CTTGSUBTable::TFeatureList::~TFeatureList() {} -CFX_CTTGSUBTable::TLookupList::TLookupList() : LookupCount(0) {} +CFX_CTTGSUBTable::TLookupList::TLookupList() {} CFX_CTTGSUBTable::TLookupList::~TLookupList() {} -CFX_CTTGSUBTable::TLangSys::TLangSys() - : LookupOrder(0), ReqFeatureIndex(0), FeatureCount(0) {} +CFX_CTTGSUBTable::TLangSys::TLangSys() : LookupOrder(0), ReqFeatureIndex(0) {} CFX_CTTGSUBTable::TLangSys::~TLangSys() {} |