cpp/poppler-font.cpp | 15 ++++-------- glib/poppler-document.cc | 51 ++++++++++++++++++++------------------------ poppler/FontInfo.cc | 17 ++++++-------- poppler/FontInfo.h | 2 - qt5/src/poppler-fontinfo.cc | 13 +++-------- utils/pdffonts.cc | 14 ++---------- 6 files changed, 45 insertions(+), 67 deletions(-)
New commits: commit e076a478e4d2f7d6445215814c86e6ac4654f575 Author: Oliver Sander <[email protected]> Date: Mon Oct 14 20:08:33 2019 +0200 Make FontInfo::scan return a std::vector object ... rather than a pointer to a std::vector. Given that a std::vector is little more than a pointer and some size information, there is no need to create std::vector objects on the heap. Returning them by value is just as fast (the vector content is not copied), and makes the code more readable, too. diff --git a/cpp/poppler-font.cpp b/cpp/poppler-font.cpp index df795eb9..6d833c19 100644 --- a/cpp/poppler-font.cpp +++ b/cpp/poppler-font.cpp @@ -218,18 +218,13 @@ std::vector<font_info> font_iterator::next() ++d->current_page; - std::vector<FontInfo*> *items = d->font_info_scanner.scan(1); - if (!items) { - return std::vector<font_info>(); - } - std::vector<font_info> fonts(items->size()); - for (std::size_t i = 0; i < items->size(); ++i) { - fonts[i] = font_info(*new font_info_private((*items)[i])); - } - for (auto entry : *items) { + const std::vector<FontInfo*> items = d->font_info_scanner.scan(1); + std::vector<font_info> fonts; + fonts.reserve(items.size()); + for (FontInfo* entry : items) { + fonts.push_back(font_info(*new font_info_private(entry))); delete entry; } - delete items; return fonts; } diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index fbeaf34d..22b3e4e2 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -4,6 +4,7 @@ * Copyright (C) 2016 Jakub Alba <[email protected]> * Copyright (C) 2018-2019 Marek Kasik <[email protected]> * Copyright (C) 2019 Masamichi Hosoda <[email protected]> + * Copyright (C) 2019, Oliver Sander <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -2718,7 +2719,7 @@ poppler_index_iter_free (PopplerIndexIter *iter) struct _PopplerFontsIter { - std::vector<FontInfo*> *items; + std::vector<FontInfo*> items; int index; }; @@ -2740,7 +2741,7 @@ poppler_fonts_iter_get_full_name (PopplerFontsIter *iter) GooString *name; FontInfo *info; - info = (*iter->items)[iter->index]; + info = iter->items[iter->index]; name = info->getName(); if (name != nullptr) { @@ -2765,7 +2766,7 @@ poppler_fonts_iter_get_name (PopplerFontsIter *iter) const char *name; name = poppler_fonts_iter_get_full_name (iter); - info = (*iter->items)[iter->index]; + info = iter->items[iter->index]; if (info->getSubset() && name) { while (*name && *name != '+') @@ -2795,7 +2796,7 @@ poppler_fonts_iter_get_substitute_name (PopplerFontsIter *iter) GooString *name; FontInfo *info; - info = (*iter->items)[iter->index]; + info = iter->items[iter->index]; name = info->getSubstituteName(); if (name != nullptr) { @@ -2820,7 +2821,7 @@ poppler_fonts_iter_get_file_name (PopplerFontsIter *iter) GooString *file; FontInfo *info; - info = (*iter->items)[iter->index]; + info = iter->items[iter->index]; file = info->getFile(); if (file != nullptr) { @@ -2845,7 +2846,7 @@ poppler_fonts_iter_get_font_type (PopplerFontsIter *iter) g_return_val_if_fail (iter != nullptr, POPPLER_FONT_TYPE_UNKNOWN); - info = (*iter->items)[iter->index]; + info = iter->items[iter->index]; return (PopplerFontType)info->getType (); } @@ -2866,7 +2867,7 @@ poppler_fonts_iter_get_encoding (PopplerFontsIter *iter) GooString *encoding; FontInfo *info; - info = (*iter->items)[iter->index]; + info = iter->items[iter->index]; encoding = info->getEncoding(); if (encoding != nullptr) { @@ -2889,7 +2890,7 @@ poppler_fonts_iter_is_embedded (PopplerFontsIter *iter) { FontInfo *info; - info = (*iter->items)[iter->index]; + info = iter->items[iter->index]; return info->getEmbedded(); } @@ -2907,7 +2908,7 @@ poppler_fonts_iter_is_subset (PopplerFontsIter *iter) { FontInfo *info; - info = (*iter->items)[iter->index]; + info = iter->items[iter->index]; return info->getSubset(); } @@ -2926,7 +2927,7 @@ poppler_fonts_iter_next (PopplerFontsIter *iter) g_return_val_if_fail (iter != nullptr, FALSE); iter->index++; - if (iter->index >= (int)iter->items->size()) + if (iter->index >= (int)iter->items.size()) return FALSE; return TRUE; @@ -2949,10 +2950,10 @@ poppler_fonts_iter_copy (PopplerFontsIter *iter) new_iter = g_slice_dup (PopplerFontsIter, iter); - new_iter->items = new std::vector<FontInfo*> (); - for (std::size_t i = 0; i < iter->items->size(); i++) { - FontInfo *info = (*iter->items)[i]; - new_iter->items->push_back (new FontInfo (*info)); + new_iter->items.resize(iter->items.size()); + for (std::size_t i = 0; i < iter->items.size(); i++) { + FontInfo *info = iter->items[i]; + new_iter->items[i] = new FontInfo (*info); } return new_iter; @@ -2970,21 +2971,21 @@ poppler_fonts_iter_free (PopplerFontsIter *iter) if (G_UNLIKELY (iter == nullptr)) return; - for (auto entry : *iter->items) { + for (auto entry : iter->items) { delete entry; } - delete iter->items; + iter->items.~vector<FontInfo*>(); g_slice_free (PopplerFontsIter, iter); } static PopplerFontsIter * -poppler_fonts_iter_new (std::vector<FontInfo*> *items) +poppler_fonts_iter_new (std::vector<FontInfo*> &&items) { PopplerFontsIter *iter; iter = g_slice_new (PopplerFontsIter); - iter->items = items; + new ((void*)&iter->items) std::vector<FontInfo*>(std::move(items)); iter->index = 0; return iter; @@ -3083,22 +3084,18 @@ poppler_font_info_scan (PopplerFontInfo *font_info, int n_pages, PopplerFontsIter **iter) { - std::vector<FontInfo*> *items; - g_return_val_if_fail (iter != nullptr, FALSE); - items = font_info->scanner->scan(n_pages); + std::vector<FontInfo*> items = font_info->scanner->scan(n_pages); - if (items == nullptr) { + if (items.empty()) { *iter = nullptr; - } else if (items->empty()) { - *iter = nullptr; - delete items; + return FALSE; } else { - *iter = poppler_fonts_iter_new(items); + *iter = poppler_fonts_iter_new(std::move(items)); } - return (items != nullptr); + return TRUE; } /* For backward compatibility */ diff --git a/poppler/FontInfo.cc b/poppler/FontInfo.cc index 7dc62769..87c212ee 100644 --- a/poppler/FontInfo.cc +++ b/poppler/FontInfo.cc @@ -52,17 +52,17 @@ FontInfoScanner::FontInfoScanner(PDFDoc *docA, int firstPage) { FontInfoScanner::~FontInfoScanner() { } -std::vector<FontInfo*> *FontInfoScanner::scan(int nPages) { +std::vector<FontInfo*> FontInfoScanner::scan(int nPages) { Page *page; Dict *resDict; Annots *annots; int lastPage; + std::vector<FontInfo*> result; + if (currentPage > doc->getNumPages()) { - return nullptr; + return result; } - - auto result = new std::vector<FontInfo*>(); lastPage = currentPage + nPages; if (lastPage > doc->getNumPages() + 1) { diff --git a/poppler/FontInfo.h b/poppler/FontInfo.h index 6ed6fb2f..0ecef820 100644 --- a/poppler/FontInfo.h +++ b/poppler/FontInfo.h @@ -92,7 +92,7 @@ public: // Destructor. ~FontInfoScanner(); - std::vector<FontInfo*> *scan(int nPages); + std::vector<FontInfo*> scan(int nPages); private: diff --git a/qt5/src/poppler-fontinfo.cc b/qt5/src/poppler-fontinfo.cc index 3948a298..6b3ae7cb 100644 --- a/qt5/src/poppler-fontinfo.cc +++ b/qt5/src/poppler-fontinfo.cc @@ -134,17 +134,12 @@ QList<FontInfo> FontIterator::next() ++d->currentPage; QList<FontInfo> fonts; - std::vector<::FontInfo*> *items = d->fontInfoScanner.scan( 1 ); - if ( !items ) - return fonts; - fonts.reserve( items->size() ); - for ( std::size_t i = 0; i < items->size(); ++i ) { - fonts.append( FontInfo( FontInfoData( (*items)[ i ] ) ) ); - } - for ( auto entry : *items ) { + const std::vector<::FontInfo*> items = d->fontInfoScanner.scan( 1 ); + fonts.reserve( items.size() ); + for ( ::FontInfo* entry : items ) { + fonts.append( FontInfo( FontInfoData( entry ) ) ); delete entry; } - delete items; return fonts; } diff --git a/utils/pdffonts.cc b/utils/pdffonts.cc index 0746bc21..24eb5038 100644 --- a/utils/pdffonts.cc +++ b/utils/pdffonts.cc @@ -165,15 +165,13 @@ int main(int argc, char *argv[]) { // get the fonts { FontInfoScanner scanner(doc, firstPage - 1); - std::vector<FontInfo*> *fonts = scanner.scan(lastPage - firstPage + 1); + const std::vector<FontInfo*> fonts = scanner.scan(lastPage - firstPage + 1); if (showSubst) { // print the font substitutions printf("name object ID substitute font substitute font file\n"); printf("------------------------------------ --------- ------------------------------------ ------------------------------------\n"); - if (fonts) { - for (std::size_t i = 0; i < fonts->size(); ++i) { - FontInfo *font = (*fonts)[i]; + for (FontInfo* font : fonts) { if (font->getFile()) { printf("%-36s", font->getName() ? font->getName()->c_str() : "[none]"); @@ -189,15 +187,11 @@ int main(int argc, char *argv[]) { } delete font; } - delete fonts; - } } else { // print the font info printf("name type encoding emb sub uni object ID\n"); printf("------------------------------------ ----------------- ---------------- --- --- --- ---------\n"); - if (fonts) { - for (std::size_t i = 0; i < fonts->size(); ++i) { - FontInfo *font = (*fonts)[i]; + for (FontInfo* font : fonts) { printf("%-36s %-17s %-16s %-3s %-3s %-3s", font->getName() ? font->getName()->c_str() : "[none]", fontTypeNames[font->getType()], @@ -213,8 +207,6 @@ int main(int argc, char *argv[]) { } delete font; } - delete fonts; - } } } commit 8911527dc38908ee56626fc55d09041475cc5145 Author: Oliver Sander <[email protected]> Date: Mon Oct 14 15:15:45 2019 +0200 Use a std::unique_ptr for XRef This makes memory handling a tiny bit more robust. diff --git a/poppler/FontInfo.cc b/poppler/FontInfo.cc index 39557894..7dc62769 100644 --- a/poppler/FontInfo.cc +++ b/poppler/FontInfo.cc @@ -69,27 +69,26 @@ std::vector<FontInfo*> *FontInfoScanner::scan(int nPages) { lastPage = doc->getNumPages() + 1; } - XRef *xrefA = doc->getXRef()->copy(); + std::unique_ptr<XRef> xrefA(doc->getXRef()->copy()); for (int pg = currentPage; pg < lastPage; ++pg) { page = doc->getPage(pg); if (!page) continue; - if ((resDict = page->getResourceDictCopy(xrefA))) { - scanFonts(xrefA, resDict, result); + if ((resDict = page->getResourceDictCopy(xrefA.get()))) { + scanFonts(xrefA.get(), resDict, &result); delete resDict; } annots = page->getAnnots(); for (int i = 0; i < annots->getNumAnnots(); ++i) { Object obj1 = annots->getAnnot(i)->getAppearanceResDict(); if (obj1.isDict()) { - scanFonts(xrefA, obj1.getDict(), result); + scanFonts(xrefA.get(), obj1.getDict(), &result); } } } currentPage = lastPage; - delete xrefA; return result; } _______________________________________________ poppler mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/poppler
