mirror of
https://github.com/YACReader/yacreader
synced 2025-06-03 09:08:20 -04:00
119 lines
2.1 KiB
C++
119 lines
2.1 KiB
C++
#include "comic.h"
|
|
#include "pdf_comic.h"
|
|
|
|
#if defined USE_PDFIUM && !defined NO_PDF
|
|
|
|
int pdfRead(void* param,
|
|
unsigned long position,
|
|
unsigned char* pBuf,
|
|
unsigned long size) {
|
|
|
|
QFile *file = static_cast<QFile *>(param);
|
|
|
|
file->seek(position);
|
|
|
|
qint64 numBytesRead = file->read(reinterpret_cast<char *>(pBuf), size);
|
|
|
|
if(numBytesRead > 0)
|
|
{
|
|
return numBytesRead;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
PdfiumComic::PdfiumComic()
|
|
{
|
|
FPDF_InitLibrary();
|
|
}
|
|
|
|
PdfiumComic::~PdfiumComic()
|
|
{
|
|
if (doc)
|
|
{
|
|
FPDF_CloseDocument(doc);
|
|
}
|
|
FPDF_DestroyLibrary();
|
|
}
|
|
|
|
bool PdfiumComic::openComic(const QString & path)
|
|
{
|
|
pdfFile.setFileName(path);
|
|
|
|
if(pdfFile.open(QIODevice::ReadOnly) == false)
|
|
{
|
|
qDebug() << "unable to open file : " << path;
|
|
return false;
|
|
}
|
|
|
|
fileAccess.m_FileLen = pdfFile.size();
|
|
fileAccess.m_GetBlock = pdfRead;
|
|
fileAccess.m_Param = &pdfFile;
|
|
|
|
doc = FPDF_LoadCustomDocument(&fileAccess, NULL);
|
|
if (doc)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
qDebug() << FPDF_GetLastError();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void PdfiumComic::closeComic()
|
|
{
|
|
FPDF_CloseDocument(doc);
|
|
}
|
|
|
|
unsigned int PdfiumComic::numPages()
|
|
{
|
|
if (doc)
|
|
{
|
|
return FPDF_GetPageCount(doc);
|
|
}
|
|
else
|
|
{
|
|
return 0; //-1?
|
|
}
|
|
}
|
|
|
|
QImage PdfiumComic::getPage(const int page)
|
|
{
|
|
if (!doc)
|
|
{
|
|
return QImage();
|
|
}
|
|
|
|
QImage image;
|
|
FPDF_PAGE pdfpage;
|
|
FPDF_BITMAP bitmap;
|
|
|
|
pdfpage = FPDF_LoadPage(doc, page);
|
|
|
|
if (!pdfpage)
|
|
{
|
|
return QImage();
|
|
}
|
|
|
|
//TODO: make target DPI configurable
|
|
double width = (FPDF_GetPageWidth(pdfpage)/72)*150;
|
|
double height = (FPDF_GetPageHeight(pdfpage)/72)*150;
|
|
|
|
image = QImage(width, height, QImage::Format_ARGB32);// QImage::Format_RGBX8888);
|
|
if (image.isNull())
|
|
{
|
|
return image;
|
|
}
|
|
image.fill(0xFFFFFFFF);
|
|
|
|
bitmap = FPDFBitmap_CreateEx(image.width(), image.height(), FPDFBitmap_BGRA, image.scanLine(0), image.bytesPerLine());
|
|
//TODO: make render flags costumizable
|
|
FPDF_RenderPageBitmap(bitmap, pdfpage, 0,0, image.width(), image.height(), 0, (FPDF_LCD_TEXT));
|
|
FPDFBitmap_Destroy(bitmap);
|
|
FPDF_ClosePage(pdfpage);
|
|
return image;
|
|
}
|
|
#endif //USE_PDFIUM
|