diff options
author | Sune Vuorela <sune@vuorela.dk> | 2025-01-15 14:05:03 +0100 |
---|---|---|
committer | Albert Astals Cid <aacid@kde.org> | 2025-01-15 22:50:09 +0000 |
commit | ac53e421182487f1b016398556a1d7ddc9b0a77f (patch) | |
tree | 1be5f149b2da7e196ed3667199b9d418f065084e | |
parent | 2ed82df7fbd398410a5a42005958aa70699d6b4c (diff) |
More unique pointers and vectors
PageAttr and PageCache and related helpers
-rw-r--r-- | poppler/Catalog.cc | 22 | ||||
-rw-r--r-- | poppler/Catalog.h | 2 | ||||
-rw-r--r-- | poppler/PDFDoc.cc | 21 | ||||
-rw-r--r-- | poppler/PDFDoc.h | 6 | ||||
-rw-r--r-- | poppler/Page.cc | 6 | ||||
-rw-r--r-- | poppler/Page.h | 6 |
6 files changed, 20 insertions, 43 deletions
diff --git a/poppler/Catalog.cc b/poppler/Catalog.cc index 58f99d08..a6b090c4 100644 --- a/poppler/Catalog.cc +++ b/poppler/Catalog.cc @@ -94,7 +94,6 @@ Catalog::Catalog(PDFDoc *docA) pagesList = nullptr; pagesRefList = nullptr; - attrsList = nullptr; kidsIdxList = nullptr; markInfo = markInfoNull; @@ -145,13 +144,6 @@ Catalog::Catalog(PDFDoc *docA) Catalog::~Catalog() { delete kidsIdxList; - if (attrsList) { - std::vector<PageAttrs *>::iterator it; - for (it = attrsList->begin(); it != attrsList->end(); ++it) { - delete *it; - } - delete attrsList; - } delete pagesRefList; delete pagesList; delete destNameTree; @@ -257,8 +249,7 @@ bool Catalog::initPageList() pages.clear(); refPageMap.clear(); - attrsList = new std::vector<PageAttrs *>(); - attrsList->push_back(new PageAttrs(nullptr, obj.getDict())); + attrsList.push_back(std::make_unique<PageAttrs>(nullptr, obj.getDict())); pagesList = new std::vector<Object>(); pagesList->push_back(std::move(obj)); pagesRefList = new std::vector<Ref>(); @@ -328,8 +319,7 @@ bool Catalog::cacheSubTree() if (kidsIdx >= kids.arrayGetLength()) { pagesList->pop_back(); pagesRefList->pop_back(); - delete attrsList->back(); - attrsList->pop_back(); + attrsList.pop_back(); kidsIdxList->pop_back(); if (!kidsIdxList->empty()) { kidsIdxList->back()++; @@ -359,8 +349,8 @@ bool Catalog::cacheSubTree() Object kid = kids.arrayGet(kidsIdx); if (kid.isDict("Page") || (kid.isDict() && !kid.getDict()->hasKey("Kids"))) { - PageAttrs *attrs = new PageAttrs(attrsList->back(), kid.getDict()); - auto p = std::make_unique<Page>(doc, pages.size() + 1, std::move(kid), kidRef.getRef(), attrs, form); + auto attrs = std::make_unique<PageAttrs>(attrsList.back().get(), kid.getDict()); + auto p = std::make_unique<Page>(doc, pages.size() + 1, std::move(kid), kidRef.getRef(), std::move(attrs), form); if (!p->isOk()) { error(errSyntaxError, -1, "Failed to create page (page {0:uld})", pages.size() + 1); return false; @@ -380,7 +370,7 @@ bool Catalog::cacheSubTree() // This should really be isDict("Pages"), but I've seen at least one // PDF file where the /Type entry is missing. } else if (kid.isDict()) { - attrsList->push_back(new PageAttrs(attrsList->back(), kid.getDict())); + attrsList.push_back(std::make_unique<PageAttrs>(attrsList.back().get(), kid.getDict())); pagesRefList->push_back(kidRef.getRef()); pagesList->push_back(std::move(kid)); kidsIdxList->push_back(0); @@ -856,7 +846,7 @@ int Catalog::getNumPages() Dict *pageDict = pagesDict.getDict(); if (pageRootRef.isRef()) { const Ref pageRef = pageRootRef.getRef(); - auto p = std::make_unique<Page>(doc, 1, std::move(pagesDict), pageRef, new PageAttrs(nullptr, pageDict), form); + auto p = std::make_unique<Page>(doc, 1, std::move(pagesDict), pageRef, std::make_unique<PageAttrs>(nullptr, pageDict), form); if (p->isOk()) { pages.emplace_back(std::move(p), pageRef); refPageMap.emplace(pageRef, pages.size()); diff --git a/poppler/Catalog.h b/poppler/Catalog.h index 92b6a1ce..f41fa8bb 100644 --- a/poppler/Catalog.h +++ b/poppler/Catalog.h @@ -279,7 +279,7 @@ private: std::unordered_map<Ref, std::size_t> refPageMap; std::vector<Object> *pagesList; std::vector<Ref> *pagesRefList; - std::vector<PageAttrs *> *attrsList; + std::vector<std::unique_ptr<PageAttrs>> attrsList; std::vector<int> *kidsIdxList; Form *form; ViewerPreferences *viewerPrefs; diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index d2d20508..6f94ac2b 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -300,14 +300,6 @@ bool PDFDoc::setup(const std::optional<GooString> &ownerPassword, const std::opt PDFDoc::~PDFDoc() { - if (pageCache) { - for (int i = 0; i < getNumPages(); i++) { - if (pageCache[i]) { - delete pageCache[i]; - } - } - gfree(static_cast<void *>(pageCache)); - } delete secHdlr; delete outline; delete catalog; @@ -2120,7 +2112,7 @@ int PDFDoc::getNumPages() return catalog->getNumPages(); } -Page *PDFDoc::parsePage(int page) +std::unique_ptr<Page> PDFDoc::parsePage(int page) { Ref pageRef; @@ -2144,7 +2136,7 @@ Page *PDFDoc::parsePage(int page) } Dict *pageDict = obj.getDict(); - return new Page(this, page, std::move(obj), pageRef, new PageAttrs(nullptr, pageDict), catalog->getForm()); + return std::make_unique<Page>(this, page, std::move(obj), pageRef, std::make_unique<PageAttrs>(nullptr, pageDict), catalog->getForm()); } Page *PDFDoc::getPage(int page) @@ -2155,17 +2147,14 @@ Page *PDFDoc::getPage(int page) if (isLinearized() && checkLinearization()) { pdfdocLocker(); - if (!pageCache) { - pageCache = (Page **)gmallocn(getNumPages(), sizeof(Page *)); - for (int i = 0; i < getNumPages(); i++) { - pageCache[i] = nullptr; - } + if (pageCache.empty()) { + pageCache.resize(getNumPages()); } if (!pageCache[page - 1]) { pageCache[page - 1] = parsePage(page); } if (pageCache[page - 1]) { - return pageCache[page - 1]; + return pageCache[page - 1].get(); } else { error(errSyntaxWarning, -1, "Failed parsing page {0:d} using hint tables", page); } diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h index 0129e047..420f89bd 100644 --- a/poppler/PDFDoc.h +++ b/poppler/PDFDoc.h @@ -196,7 +196,7 @@ public: // Return the structure tree root object. const StructTreeRoot *getStructTreeRoot() const { return catalog->getStructTreeRoot(); } - // Get page. + // Get page. First page is page 1. Page *getPage(int page); // Display a page. @@ -380,7 +380,7 @@ private: void saveIncrementalUpdate(OutStream *outStr); void saveCompleteRewrite(OutStream *outStr); - Page *parsePage(int page); + std::unique_ptr<Page> parsePage(int page); // Get hints. Hints *getHints(); @@ -420,7 +420,7 @@ private: Catalog *catalog = nullptr; Hints *hints = nullptr; Outline *outline = nullptr; - Page **pageCache = nullptr; + std::vector<std::unique_ptr<Page>> pageCache; bool ok = false; int errCode = errNone; diff --git a/poppler/Page.cc b/poppler/Page.cc index 8e14bc27..d2a4e5a2 100644 --- a/poppler/Page.cc +++ b/poppler/Page.cc @@ -94,7 +94,7 @@ void PDFRectangle::clipTo(PDFRectangle *rect) // PageAttrs //------------------------------------------------------------------------ -PageAttrs::PageAttrs(PageAttrs *attrs, Dict *dict) +PageAttrs::PageAttrs(const PageAttrs *attrs, Dict *dict) { Object obj1; PDFRectangle mBox; @@ -252,7 +252,7 @@ bool PageAttrs::readBox(Dict *dict, const char *key, PDFRectangle *box) #define pageLocker() const std::scoped_lock locker(mutex) -Page::Page(PDFDoc *docA, int numA, Object &&pageDict, Ref pageRefA, PageAttrs *attrsA, Form *form) : pageRef(pageRefA) +Page::Page(PDFDoc *docA, int numA, Object &&pageDict, Ref pageRefA, std::unique_ptr<PageAttrs> attrsA, Form *form) : pageRef(pageRefA), attrs(std::move(attrsA)) { ok = true; doc = docA; @@ -265,7 +265,6 @@ Page::Page(PDFDoc *docA, int numA, Object &&pageDict, Ref pageRefA, PageAttrs *a pageObj = std::move(pageDict); // get attributes - attrs = attrsA; attrs->clipBoxes(); // transtion @@ -330,7 +329,6 @@ err1: Page::~Page() { - delete attrs; delete annots; } diff --git a/poppler/Page.h b/poppler/Page.h index 944f802f..7faf1bd1 100644 --- a/poppler/Page.h +++ b/poppler/Page.h @@ -89,7 +89,7 @@ public: // Construct a new PageAttrs object by merging a dictionary // (of type Pages or Page) into another PageAttrs object. If // <attrs> is nullptr, uses defaults. - PageAttrs(PageAttrs *attrs, Dict *dict); + PageAttrs(const PageAttrs *attrs, Dict *dict); // Destructor. ~PageAttrs(); @@ -142,7 +142,7 @@ class POPPLER_PRIVATE_EXPORT Page { public: // Constructor. - Page(PDFDoc *docA, int numA, Object &&pageDict, Ref pageRefA, PageAttrs *attrsA, Form *form); + Page(PDFDoc *docA, int numA, Object &&pageDict, Ref pageRefA, std::unique_ptr<PageAttrs> attrsA, Form *form); // Destructor. ~Page(); @@ -257,7 +257,7 @@ private: Object pageObj; // page dictionary const Ref pageRef; // page reference int num; // page number - PageAttrs *attrs; // page attributes + std::unique_ptr<PageAttrs> attrs; // page attributes Annots *annots; // annotations Object annotsObj; // annotations array Object contents; // page contents |