Skip to content

Commit

Permalink
Use std::unique_ptr for pdf objects
Browse files Browse the repository at this point in the history
  • Loading branch information
selmf committed Nov 13, 2022
1 parent 34fde78 commit 133e378
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 35 deletions.
24 changes: 5 additions & 19 deletions YACReaderLibrary/initial_comic_info_extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,27 @@ void InitialComicInfoExtractor::extract()
#ifndef NO_PDF
if (fi.suffix().compare("pdf", Qt::CaseInsensitive) == 0) {
#if defined Q_OS_MAC && defined USE_PDFKIT
MacOSXPDFComic *pdfComic = new MacOSXPDFComic();
auto pdfComic = std::make_unique<MacOSXPDFComic>();
if (!pdfComic->openComic(_fileSource)) {
delete pdfComic;
// QImage p;
// p.load(":/images/notCover.png");
// p.save(_target);
return;
}
#elif defined USE_PDFIUM
auto pdfComic = new PdfiumComic();
auto pdfComic = std::make_unique<PdfiumComic>();
if (!pdfComic->openComic(_fileSource)) {
delete pdfComic;
return;
}
#else
Poppler::Document *pdfComic = Poppler::Document::load(_fileSource);
auto _pdfComic = Poppler::Document::load(_fileSource);
auto pdfComic = std::unique_ptr<Poppler::Document>(_pdfComic);
#endif

if (!pdfComic) {
QLOG_WARN() << "Extracting cover: unable to open PDF file " << _fileSource;
// delete pdfComic; //TODO check if the delete is needed
pdfComic = 0;
// QImage p;
// p.load(":/images/notCover.png");
// p.save(_target);
return;
}
#if !defined USE_PDFKIT && !defined USE_PDFIUM
// poppler only, not mac
if (pdfComic->isLocked()) {
QLOG_WARN() << "Extracting cover: unable to open PDF file " << _fileSource;
delete pdfComic;
return;
}
#endif
Expand All @@ -75,11 +64,8 @@ void InitialComicInfoExtractor::extract()
saveCover(_target, p);
} else if (_target != "") {
QLOG_WARN() << "Extracting cover: requested cover index greater than numPages " << _fileSource;
// QImage p;
// p.load(":/images/notCover.png");
// p.save(_target);

}
delete pdfComic;
}
return;
}
Expand Down
19 changes: 6 additions & 13 deletions common/comic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,24 +796,22 @@ bool PDFComic::load(const QString &path, const ComicDB &comic)
void PDFComic::process()
{
#if defined Q_OS_MAC && defined USE_PDFKIT
pdfComic = new MacOSXPDFComic();
pdfComic = std::make_unique<MacOSXPDFComic>();
if (!pdfComic->openComic(_path)) {
delete pdfComic;
emit errorOpening();
return;
}
#elif defined USE_PDFIUM
pdfComic = new PdfiumComic();
pdfComic = std::make_unique<PdfiumComic>();
if (!pdfComic->openComic(_path)) {
delete pdfComic;
emit errorOpening();
return;
}
#else
pdfComic = Poppler::Document::load(_path);
auto _pdfComic = Poppler::Document::load(_path);
pdfComic = std::unique_ptr<Poppler::Document>(_pdfComic);

if (!pdfComic) {
// delete pdfComic;
// pdfComic = 0;
moveToThread(QCoreApplication::instance()->thread());
emit errorOpening();
return;
Expand All @@ -824,7 +822,6 @@ void PDFComic::process()
return;
}

// pdfComic->setRenderHint(Poppler::Document::Antialiasing, true);
pdfComic->setRenderHint(Poppler::Document::TextAntialiasing, true);
#endif

Expand Down Expand Up @@ -853,7 +850,6 @@ void PDFComic::process()
int buffered_index = _index;
for (int i = buffered_index; i < nPages; i++) {
if (_invalidated) {
delete pdfComic;
moveToThread(QCoreApplication::instance()->thread());
return;
}
Expand All @@ -862,14 +858,12 @@ void PDFComic::process()
}
for (int i = 0; i < buffered_index; i++) {
if (_invalidated) {
delete pdfComic;
moveToThread(QCoreApplication::instance()->thread());
return;
}
renderPage(i);
}

delete pdfComic;
moveToThread(QCoreApplication::instance()->thread());
emit imagesLoaded();
}
Expand All @@ -883,10 +877,9 @@ void PDFComic::renderPage(int page)
QImage img = pdfComic->getPage(page);
if (!img.isNull()) {
#else
Poppler::Page *pdfpage = pdfComic->page(page);
std::unique_ptr<Poppler::Page> pdfpage (pdfComic->page(page));
if (pdfpage) {
QImage img = pdfpage->renderToImage(150, 150);
delete pdfpage;
#endif
QByteArray ba;
QBuffer buf(&ba);
Expand Down
6 changes: 3 additions & 3 deletions common/comic.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ class PDFComic : public Comic
private:
// pdf
#if defined Q_OS_MAC && defined USE_PDFKIT
MacOSXPDFComic *pdfComic;
std::unique_ptr<MacOSXPDFComic> pdfComic;
#elif defined USE_PDFIUM
PdfiumComic *pdfComic;
std::unique_ptr<PdfiumComic> pdfComic;
#else
Poppler::Document *pdfComic;
std::unique_ptr<Poppler::Document> pdfComic;
#endif
void renderPage(int page);
// void run();
Expand Down

0 comments on commit 133e378

Please sign in to comment.