summaryrefslogtreecommitdiffstats
path: root/kpdf/core/generator_pdf
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commit47d455dd55be855e4cc691c32f687f723d9247ee (patch)
tree52e236aaa2576bdb3840ebede26619692fed6d7d /kpdf/core/generator_pdf
downloadtdegraphics-47d455dd55be855e4cc691c32f687f723d9247ee.tar.gz
tdegraphics-47d455dd55be855e4cc691c32f687f723d9247ee.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdegraphics@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kpdf/core/generator_pdf')
-rw-r--r--kpdf/core/generator_pdf/Makefile.am10
-rw-r--r--kpdf/core/generator_pdf/generator_pdf.cpp1258
-rw-r--r--kpdf/core/generator_pdf/generator_pdf.h150
-rw-r--r--kpdf/core/generator_pdf/gp_outputdev.cpp397
-rw-r--r--kpdf/core/generator_pdf/gp_outputdev.h90
5 files changed, 1905 insertions, 0 deletions
diff --git a/kpdf/core/generator_pdf/Makefile.am b/kpdf/core/generator_pdf/Makefile.am
new file mode 100644
index 00000000..700740b7
--- /dev/null
+++ b/kpdf/core/generator_pdf/Makefile.am
@@ -0,0 +1,10 @@
+INCLUDES = -I$(srcdir)/../.. -I$(srcdir)/../../xpdf -I$(srcdir)/../../xpdf/goo -I$(srcdir)/../../xpdf/splash -I$(top_builddir)/kpdf $(all_includes)
+
+libgeneratorpdf_la_LDFLAGS = $(all_libraries)
+libgeneratorpdf_la_SOURCES = generator_pdf.cpp gp_outputdev.cpp
+
+noinst_LTLIBRARIES = libgeneratorpdf.la
+
+KDE_OPTIONS = nofinal
+
+generator_pdf.lo: ../../conf/settings.h
diff --git a/kpdf/core/generator_pdf/generator_pdf.cpp b/kpdf/core/generator_pdf/generator_pdf.cpp
new file mode 100644
index 00000000..7ad34152
--- /dev/null
+++ b/kpdf/core/generator_pdf/generator_pdf.cpp
@@ -0,0 +1,1258 @@
+/***************************************************************************
+ * Copyright (C) 2004 by Albert Astals Cid <[email protected]> *
+ * Copyright (C) 2004 by Enrico Ros <[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 *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+// qt/kde includes
+#include <qfile.h>
+#include <qevent.h>
+#include <qimage.h>
+#include <qapplication.h>
+#include <qpaintdevicemetrics.h>
+#include <qregexp.h>
+#include <qvariant.h>
+#include <kapplication.h>
+#include <klistview.h>
+#include <klocale.h>
+#include <kpassdlg.h>
+#include <kwallet.h>
+#include <kprinter.h>
+#include <ktempfile.h>
+#include <kmessagebox.h>
+#include <kdebug.h>
+
+// xpdf includes
+#include "xpdf/Object.h"
+#include "xpdf/Dict.h"
+#include "xpdf/Annot.h"
+#include "xpdf/PSOutputDev.h"
+#include "xpdf/TextOutputDev.h"
+#include "xpdf/Link.h"
+#include "xpdf/ErrorCodes.h"
+#include "xpdf/UnicodeMap.h"
+#include "xpdf/Outline.h"
+#include "goo/GList.h"
+
+// local includes
+#include "generator_pdf.h"
+#include "gp_outputdev.h"
+#include "core/observer.h" //for PAGEVIEW_ID
+#include "core/page.h"
+#include "core/pagetransition.h"
+#include "conf/settings.h"
+
+#include <stdlib.h>
+
+#include <config.h>
+
+// id for DATA_READY PDFPixmapGeneratorThread Event
+#define TGE_DATAREADY_ID 6969
+
+/** NOTES on threading:
+ * internal: thread race prevention is done via the 'docLock' mutex. the
+ * mutex is needed only because we have the asyncronous thread; else
+ * the operations are all within the 'gui' thread, scheduled by the
+ * Qt scheduler and no mutex is needed.
+ * external: dangerous operations are all locked via mutex internally, and the
+ * only needed external thing is the 'canGeneratePixmap' method
+ * that tells if the generator is free (since we don't want an
+ * internal queue to store PixmapRequests). A generatedPixmap call
+ * without the 'ready' flag set, results in undefined behavior.
+ * So, as example, printing while generating a pixmap asyncronously is safe,
+ * it might only block the gui thread by 1) waiting for the mutex to unlock
+ * in async thread and 2) doing the 'heavy' print operation.
+ */
+
+PDFGenerator::PDFGenerator( KPDFDocument * doc )
+ : Generator( doc ), pdfdoc( 0 ), kpdfOutputDev( 0 ), ready( true ),
+ pixmapRequest( 0 ), docInfoDirty( true ), docSynopsisDirty( true )
+{
+ // generate kpdfOutputDev and cache page color
+ reparseConfig();
+ // generate the pixmapGeneratorThread
+ generatorThread = new PDFPixmapGeneratorThread( this );
+}
+
+PDFGenerator::~PDFGenerator()
+{
+ // first stop and delete the generator thread
+ if ( generatorThread )
+ {
+ generatorThread->wait();
+ delete generatorThread;
+ }
+ // remove other internal objects
+ docLock.lock();
+ delete kpdfOutputDev;
+ delete pdfdoc;
+ docLock.unlock();
+}
+
+
+//BEGIN Generator inherited functions
+bool PDFGenerator::loadDocument( const QString & filePath, QValueVector<KPDFPage*> & pagesVector )
+{
+#ifndef NDEBUG
+ if ( pdfdoc )
+ {
+ kdDebug() << "PDFGenerator: multiple calls to loadDocument. Check it." << endl;
+ return false;
+ }
+#endif
+ // create PDFDoc for the given file
+ pdfdoc = new PDFDoc( new GString( QFile::encodeName( filePath ) ), 0, 0 );
+
+ // if the file didn't open correctly it might be encrypted, so ask for a pass
+ bool firstInput = true;
+ bool triedWallet = false;
+ KWallet::Wallet * wallet = 0;
+ int keep = 1;
+ while ( !pdfdoc->isOk() && pdfdoc->getErrorCode() == errEncrypted )
+ {
+ QCString password;
+
+ // 1.A. try to retrieve the first password from the kde wallet system
+ if ( !triedWallet )
+ {
+ QString walletName = KWallet::Wallet::NetworkWallet();
+ wallet = KWallet::Wallet::openWallet( walletName );
+ if ( wallet )
+ {
+ // use the KPdf folder (and create if missing)
+ if ( !wallet->hasFolder( "KPdf" ) )
+ wallet->createFolder( "KPdf" );
+ wallet->setFolder( "KPdf" );
+
+ // look for the pass in that folder
+ QString retrievedPass;
+ if ( !wallet->readPassword( filePath.section('/', -1, -1), retrievedPass ) )
+ password = retrievedPass.local8Bit();
+ }
+ triedWallet = true;
+ }
+
+ // 1.B. if not retrieved, ask the password using the kde password dialog
+ if ( password.isNull() )
+ {
+ QString prompt;
+ if ( firstInput )
+ prompt = i18n( "Please insert the password to read the document:" );
+ else
+ prompt = i18n( "Incorrect password. Try again:" );
+ firstInput = false;
+
+ // if the user presses cancel, abort opening
+ if ( KPasswordDialog::getPassword( password, prompt, wallet ? &keep : 0 ) != KPasswordDialog::Accepted )
+ break;
+ }
+
+ // 2. reopen the document using the password
+ GString * pwd2 = new GString( QString::fromLocal8Bit(password.data()).latin1() );
+ delete pdfdoc;
+ pdfdoc = new PDFDoc( new GString( QFile::encodeName( filePath ) ), pwd2, pwd2 );
+ delete pwd2;
+
+ // 3. if the password is correct and the user chose to remember it, store it to the wallet
+ if ( pdfdoc->isOk() && wallet && /*safety check*/ wallet->isOpen() && keep > 0 )
+ {
+ QString goodPass = QString::fromLocal8Bit( password.data() );
+ wallet->writePassword( filePath.section('/', -1, -1), goodPass );
+ }
+ }
+ if ( !pdfdoc->isOk() )
+ {
+ delete pdfdoc;
+ pdfdoc = 0;
+ return false;
+ }
+
+ // initialize output device for rendering current pdf
+ kpdfOutputDev->initDevice( pdfdoc );
+
+ // build Pages (currentPage was set -1 by deletePages)
+ uint pageCount = pdfdoc->getNumPages();
+ pagesVector.resize( pageCount );
+ for ( uint i = 0; i < pageCount ; i++ )
+ {
+ KPDFPage * page = new KPDFPage( i, pdfdoc->getPageCropWidth(i+1),
+ pdfdoc->getPageCropHeight(i+1),
+ pdfdoc->getPageRotate(i+1) );
+ addTransition( i, page );
+ pagesVector[i] = page;
+ }
+
+ // the file has been loaded correctly
+ return true;
+}
+
+
+const DocumentInfo * PDFGenerator::generateDocumentInfo()
+{
+ if ( docInfoDirty )
+ {
+ docLock.lock();
+ // compile internal structure reading properties from PDFDoc
+ docInfo.set( "title", getDocumentInfo("Title"), i18n("Title") );
+ docInfo.set( "subject", getDocumentInfo("Subject"), i18n("Subject") );
+ docInfo.set( "author", getDocumentInfo("Author"), i18n("Author") );
+ docInfo.set( "keywords", getDocumentInfo("Keywords"), i18n("Keywords") );
+ docInfo.set( "creator", getDocumentInfo("Creator"), i18n("Creator") );
+ docInfo.set( "producer", getDocumentInfo("Producer"), i18n("Producer") );
+ docInfo.set( "creationDate", getDocumentDate("CreationDate"), i18n("Created") );
+ docInfo.set( "modificationDate", getDocumentDate("ModDate"), i18n("Modified") );
+ docInfo.set( "mimeType", "application/pdf" );
+ if ( pdfdoc )
+ {
+ docInfo.set( "format", i18n( "PDF v. <version>", "PDF v. %1" )
+ .arg( QString::number( pdfdoc->getPDFVersion() ) ), i18n( "Format" ) );
+ docInfo.set( "encryption", pdfdoc->isEncrypted() ? i18n( "Encrypted" ) : i18n( "Unencrypted" ),
+ i18n("Security") );
+ docInfo.set( "optimization", pdfdoc->isLinearized() ? i18n( "Yes" ) : i18n( "No" ),
+ i18n("Optimized") );
+ docInfo.set( "pages", QString::number( pdfdoc->getCatalog()->getNumPages() ), i18n("Pages") );
+ }
+ else
+ {
+ docInfo.set( "format", "PDF", i18n( "Format" ) );
+ docInfo.set( "encryption", i18n( "Unknown Encryption" ), i18n( "Security" ) );
+ docInfo.set( "optimization", i18n( "Unknown Optimization" ), i18n( "Optimized" ) );
+ }
+ docLock.unlock();
+
+ // if pdfdoc is valid then we cached good info -> don't cache them again
+ if ( pdfdoc )
+ docInfoDirty = false;
+ }
+ return &docInfo;
+}
+
+const DocumentSynopsis * PDFGenerator::generateDocumentSynopsis()
+{
+ if ( !docSynopsisDirty )
+ return &docSyn;
+
+ if ( !pdfdoc )
+ return NULL;
+
+ Outline * outline = pdfdoc->getOutline();
+ if ( !outline )
+ return NULL;
+
+ GList * items = outline->getItems();
+ if ( !items || items->getLength() < 1 )
+ return NULL;
+
+ docLock.lock();
+ docSyn = DocumentSynopsis();
+ if ( items->getLength() > 0 )
+ addSynopsisChildren( &docSyn, items );
+ docLock.unlock();
+
+ docSynopsisDirty = false;
+ return &docSyn;
+}
+
+bool PDFGenerator::isAllowed( int permissions )
+{
+#if !KPDF_FORCE_DRM
+ if (kapp->authorize("skip_drm") && !KpdfSettings::obeyDRM()) return true;
+#endif
+
+ bool b = true;
+ if (permissions & KPDFDocument::AllowModify) b = b && pdfdoc->okToChange();
+ if (permissions & KPDFDocument::AllowCopy) b = b && pdfdoc->okToCopy();
+ if (permissions & KPDFDocument::AllowPrint) b = b && pdfdoc->okToPrint();
+ if (permissions & KPDFDocument::AllowNotes) b = b && pdfdoc->okToAddNotes();
+ return b;
+}
+
+bool PDFGenerator::canGeneratePixmap()
+{
+ return ready;
+}
+
+void PDFGenerator::generatePixmap( PixmapRequest * request )
+{
+#ifndef NDEBUG
+ if ( !ready )
+ kdDebug() << "calling generatePixmap() when not in READY state!" << endl;
+#endif
+ // update busy state (not really needed here, because the flag needs to
+ // be set only to prevent asking a pixmap while the thread is running)
+ ready = false;
+
+ // debug requests to this (xpdf) generator
+ //kdDebug() << "id: " << request->id << " is requesting " << (request->async ? "ASYNC" : "sync") << " pixmap for page " << request->page->number() << " [" << request->width << " x " << request->height << "]." << endl;
+
+ /** asyncronous requests (generation in PDFPixmapGeneratorThread::run() **/
+ if ( request->async )
+ {
+ // start the generation into the thread
+ generatorThread->startGeneration( request );
+ return;
+ }
+
+ /** syncronous request: in-place generation **/
+ // compute dpi used to get an image with desired width and height
+ KPDFPage * page = request->page;
+ double fakeDpiX = request->width * 72.0 / page->width(),
+ fakeDpiY = request->height * 72.0 / page->height();
+
+ // setup kpdf output device: text page is generated only if we are at 72dpi.
+ // since we can pre-generate the TextPage at the right res.. why not?
+ bool genTextPage = !page->hasSearchPage() && (request->width == page->width()) &&
+ (request->height == page->height());
+ // generate links and image rects if rendering pages on pageview
+ bool genObjectRects = request->id & (PAGEVIEW_ID | PRESENTATION_ID);
+
+ // 0. LOCK [waits for the thread end]
+ docLock.lock();
+
+ // 1. Set OutputDev parameters and Generate contents
+ // note: thread safety is set on 'false' for the GUI (this) thread
+ kpdfOutputDev->setParams( request->width, request->height, genObjectRects, genObjectRects, false );
+ pdfdoc->displayPage( kpdfOutputDev, page->number() + 1, fakeDpiX, fakeDpiY, 0, false, true, false );
+ if ( genObjectRects )
+ pdfdoc->processLinks( kpdfOutputDev, page->number() + 1 );
+
+ // 2. Take data from outputdev and attach it to the Page
+ page->setPixmap( request->id, kpdfOutputDev->takePixmap() );
+ if ( genObjectRects )
+ page->setObjectRects( kpdfOutputDev->takeObjectRects() );
+
+ // 3. UNLOCK [re-enables shared access]
+ docLock.unlock();
+
+ if ( genTextPage )
+ generateSyncTextPage( page );
+
+ // update ready state
+ ready = true;
+
+ // notify the new generation
+ signalRequestDone( request );
+}
+
+void PDFGenerator::generateSyncTextPage( KPDFPage * page )
+{
+ // build a TextPage...
+ TextOutputDev td(NULL, gTrue, gFalse, gFalse);
+ docLock.lock();
+ pdfdoc->displayPage( &td, page->number()+1, 72, 72, 0, false, true, false );
+ // ..and attach it to the page
+ page->setSearchPage( td.takeText() );
+ docLock.unlock();
+}
+
+bool PDFGenerator::supportsSearching() const
+{
+ return true;
+}
+
+bool PDFGenerator::hasFonts() const
+{
+ return true;
+}
+
+void PDFGenerator::putFontInfo(KListView *list)
+{
+ Page *page;
+ Dict *resDict;
+ Annots *annots;
+ Object obj1, obj2;
+ int pg, i;
+
+ Ref *fonts;
+ int fontsLen;
+ int fontsSize;
+
+ list->addColumn(i18n("Name"));
+ list->addColumn(i18n("Type"));
+ list->addColumn(i18n("Embedded"));
+ list->addColumn(i18n("File"));
+
+ docLock.lock();
+
+ fonts = NULL;
+ fontsLen = fontsSize = 0;
+ QValueVector<Ref> visitedXObjects;
+ for (pg = 1; pg <= pdfdoc->getNumPages(); ++pg)
+ {
+ page = pdfdoc->getCatalog()->getPage(pg);
+ if ((resDict = page->getResourceDict()))
+ {
+ scanFonts(resDict, list, &fonts, fontsLen, fontsSize, &visitedXObjects);
+ }
+ annots = new Annots(pdfdoc->getXRef(), pdfdoc->getCatalog(), page->getAnnots(&obj1));
+ obj1.free();
+ for (i = 0; i < annots->getNumAnnots(); ++i)
+ {
+ if (annots->getAnnot(i)->getAppearance(&obj1)->isStream())
+ {
+ obj1.streamGetDict()->lookup("Resources", &obj2);
+ if (obj2.isDict())
+ {
+ scanFonts(obj2.getDict(), list, &fonts, fontsLen, fontsSize, &visitedXObjects);
+ }
+ obj2.free();
+ }
+ obj1.free();
+ }
+ delete annots;
+ }
+ gfree(fonts);
+
+ docLock.unlock();
+}
+
+bool PDFGenerator::print( KPrinter& printer )
+{
+ // PageSize is a CUPS artificially created setting
+ QString ps = printer.option("PageSize");
+ int paperWidth, paperHeight;
+ int marginTop, marginLeft, marginRight, marginBottom;
+ marginTop = (int)printer.option("kde-margin-top").toDouble();
+ marginLeft = (int)printer.option("kde-margin-left").toDouble();
+ marginRight = (int)printer.option("kde-margin-right").toDouble();
+ marginBottom = (int)printer.option("kde-margin-bottom").toDouble();
+ bool forceRasterize = printer.option("kde-kpdf-forceRaster").toInt();
+
+ if (ps.find(QRegExp("w\\d+h\\d+")) == 0)
+ {
+ // size not supported by Qt, CUPS gives us the size as wWIDTHhHEIGHT, at least on the printers i tester
+ // remove the w
+ ps = ps.mid(1);
+ int hPos = ps.find("h");
+ paperWidth = ps.left(hPos).toInt();
+ paperHeight = ps.mid(hPos+1).toInt();
+ }
+ else
+ {
+ // size is supported by Qt, we get either the pageSize name or nothing because the CUPS driver
+ // does not do any translation, then use KPrinter::pageSize to get the page size
+ KPrinter::PageSize qtPageSize;
+ if (!ps.isEmpty()) qtPageSize = pageNameToPageSize(ps);
+ else qtPageSize = printer.pageSize();
+
+ QPrinter dummy(QPrinter::PrinterResolution);
+ dummy.setFullPage(true);
+ dummy.setPageSize((QPrinter::PageSize)qtPageSize);
+
+ QPaintDeviceMetrics metrics(&dummy);
+ paperWidth = metrics.width();
+ paperHeight = metrics.height();
+ }
+
+ KTempFile tf( QString::null, ".ps" );
+ globalParams->setPSPaperWidth(paperWidth);
+ globalParams->setPSPaperHeight(paperHeight);
+ QString pstitle = getDocumentInfo("Title", true);
+ if ( pstitle.isEmpty() )
+ {
+ pstitle = m_document->currentDocument().fileName( false );
+ }
+ // this looks non-unicode-safe and it is. anything other than ASCII is not specified
+ // and some printers actually stop printing when they encounter non-ASCII characters in the
+ // Postscript %%Title tag
+ QCString pstitle8Bit = pstitle.latin1();
+ const char* pstitlechar;
+ if (!pstitle.isEmpty())
+ {
+ pstitlechar = pstitle8Bit.data();
+ for (unsigned char* p = (unsigned char*) pstitle8Bit.data(); *p; ++p)
+ if (*p >= 0x80)
+ *p = '?';
+
+ printer.setDocName(pstitle);
+ }
+ else
+ {
+ pstitlechar = 0;
+ }
+ PSOutputDev *psOut = new PSOutputDev(const_cast<char*>(tf.name().latin1()), const_cast<char*>(pstitlechar), pdfdoc->getXRef(), pdfdoc->getCatalog(), 1, pdfdoc->getNumPages(), psModePS, marginLeft, marginBottom, paperWidth - marginRight, paperHeight - marginTop, forceRasterize);
+
+ if (psOut->isOk())
+ {
+ double xScale = ((double)paperWidth - (double)marginLeft - (double)marginRight) / (double)paperWidth;
+ double yScale = ((double)paperHeight - (double)marginBottom - (double)marginTop) / (double)paperHeight;
+
+ if ( abs((int)(xScale * 100) - (int)(yScale * 100)) > 5 ) {
+ int result = KMessageBox::questionYesNo(m_document->widget(),
+ i18n("The margins you specified change the page aspect ratio. Do you want to print with the aspect ratio changed or do you want the margins to be adapted so that the aspect ratio is preserved?"),
+ i18n("Aspect ratio change"),
+ i18n("Print with specified margins"),
+ i18n("Print adapting margins to keep aspect ratio"),
+ "kpdfStrictlyObeyMargins");
+ if (result == KMessageBox::Yes) psOut->setScale(xScale, yScale);
+ }
+
+ QValueList<int> pageList;
+
+ if (!printer.previewOnly())
+ {
+ pageList = printer.pageList();
+ }
+ else
+ {
+ for(int i = 1; i <= pdfdoc->getNumPages(); i++) pageList.append(i);
+ }
+
+ QValueList<int>::const_iterator pIt = pageList.begin(), pEnd = pageList.end();
+ docLock.lock();
+ for ( ; pIt != pEnd; ++pIt )
+ {
+ pdfdoc->displayPage(psOut, *pIt, 72, 72, 0, false, globalParams->getPSCrop(), gTrue);
+ }
+ docLock.unlock();
+
+ // needs to be here so that the file is flushed, do not merge with the one
+ // in the else
+ delete psOut;
+ printer.printFiles(tf.name(), true);
+ return true;
+ }
+ else
+ {
+ delete psOut;
+ return false;
+ }
+}
+
+static GString *QStringToGString(const QString &s) {
+ int len = s.length();
+ char *cstring = (char *)gmallocn(s.length(), sizeof(char));
+ for (int i = 0; i < len; ++i)
+ cstring[i] = s.at(i).unicode();
+ return new GString(cstring, len);
+}
+
+static QString unicodeToQString(Unicode* u, int len) {
+ QString ret;
+ ret.setLength(len);
+ QChar* qch = (QChar*) ret.unicode();
+ for (;len;--len)
+ *qch++ = (QChar) *u++;
+ return ret;
+}
+
+static QString UnicodeParsedString(GString *s1) {
+ GBool isUnicode;
+ int i;
+ Unicode u;
+ QString result;
+ if ( ( s1->getChar(0) & 0xff ) == 0xfe && ( s1->getChar(1) & 0xff ) == 0xff )
+ {
+ isUnicode = gTrue;
+ i = 2;
+ }
+ else
+ {
+ isUnicode = gFalse;
+ i = 0;
+ }
+ while ( i < s1->getLength() )
+ {
+ if ( isUnicode )
+ {
+ u = ( ( s1->getChar(i) & 0xff ) << 8 ) | ( s1->getChar(i+1) & 0xff );
+ i += 2;
+ }
+ else
+ {
+ u = s1->getChar(i) & 0xff;
+ ++i;
+ }
+ result += unicodeToQString( &u, 1 );
+ }
+ return result;
+}
+
+QString PDFGenerator::getMetaData( const QString & key, const QString & option )
+{
+ if ( key == "StartFullScreen" )
+ {
+ // asking for the 'start in fullscreen mode' (pdf property)
+ if ( pdfdoc->getCatalog()->getPageMode() == Catalog::FullScreen )
+ return "yes";
+ }
+ else if ( key == "NamedViewport" && !option.isEmpty() )
+ {
+ // asking for the page related to a 'named link destination'. the
+ // option is the link name. @see addSynopsisChildren.
+ DocumentViewport viewport;
+ GString * namedDest = QStringToGString(option);
+ docLock.lock();
+ LinkDest * destination = pdfdoc->findDest( namedDest );
+ if ( destination )
+ {
+ fillViewportFromLink( viewport, destination );
+ }
+ docLock.unlock();
+ delete namedDest;
+ if ( viewport.pageNumber >= 0 )
+ return viewport.toString();
+ }
+ else if ( key == "OpenTOC" )
+ {
+ if ( pdfdoc->getCatalog()->getPageMode() == Catalog::UseOutlines )
+ return "yes";
+ }
+ return QString();
+}
+
+bool PDFGenerator::reparseConfig()
+{
+ // load paper color from Settings or use the white default color
+ QColor color = ( (KpdfSettings::renderMode() == KpdfSettings::EnumRenderMode::Paper ) &&
+ KpdfSettings::changeColors() ) ? KpdfSettings::paperColor() : Qt::white;
+ // if paper color is changed we have to rebuild every visible pixmap in addition
+ // to the outputDevice. it's the 'heaviest' case, other effect are just recoloring
+ // over the page rendered on 'standard' white background.
+ if ( color != paperColor || !kpdfOutputDev )
+ {
+ paperColor = color;
+ SplashColor splashCol;
+ splashCol[0] = paperColor.red();
+ splashCol[1] = paperColor.green();
+ splashCol[2] = paperColor.blue();
+ // rebuild the output device using the new paper color and initialize it
+ docLock.lock();
+ delete kpdfOutputDev;
+ kpdfOutputDev = new KPDFOutputDev( splashCol );
+ if ( pdfdoc )
+ kpdfOutputDev->initDevice( pdfdoc );
+ docLock.unlock();
+ return true;
+ }
+ return false;
+}
+//END Generator inherited functions
+
+void PDFGenerator::scanFonts(Dict *resDict, KListView *list, Ref **fonts, int &fontsLen, int &fontsSize, QValueVector<Ref> *visitedXObjects)
+{
+ Object obj1, obj2, xObjDict, xObj, xObj2, resObj;
+ Ref r;
+ GfxFontDict *gfxFontDict;
+ GfxFont *font;
+ int i;
+
+ // scan the fonts in this resource dictionary
+ gfxFontDict = NULL;
+ resDict->lookupNF("Font", &obj1);
+ if (obj1.isRef())
+ {
+ obj1.fetch(pdfdoc->getXRef(), &obj2);
+ if (obj2.isDict())
+ {
+ r = obj1.getRef();
+ gfxFontDict = new GfxFontDict(pdfdoc->getXRef(), &r, obj2.getDict());
+ }
+ obj2.free();
+ }
+ else if (obj1.isDict())
+ {
+ gfxFontDict = new GfxFontDict(pdfdoc->getXRef(), NULL, obj1.getDict());
+ }
+ if (gfxFontDict)
+ {
+ for (i = 0; i < gfxFontDict->getNumFonts(); ++i)
+ {
+ if ((font = gfxFontDict->getFont(i))) scanFont(font, list, fonts, fontsLen, fontsSize);
+ }
+ delete gfxFontDict;
+ }
+ obj1.free();
+
+ // recursively scan any resource dictionaries in objects in this
+ // resource dictionary
+ resDict->lookup("XObject", &xObjDict);
+ if (xObjDict.isDict()) {
+ for (i = 0; i < xObjDict.dictGetLength(); ++i) {
+ xObjDict.dictGetValNF(i, &xObj);
+ if (xObj.isRef()) {
+ bool alreadySeen = false;
+ // check for an already-seen XObject
+ for (int k = 0; k < visitedXObjects->count(); ++k) {
+ if (xObj.getRef().num == visitedXObjects->at(k).num &&
+ xObj.getRef().gen == visitedXObjects->at(k).gen) {
+ alreadySeen = true;
+ }
+ }
+
+ if (alreadySeen) {
+ xObj.free();
+ continue;
+ }
+
+ visitedXObjects->append(xObj.getRef());
+ }
+
+ xObj.fetch(pdfdoc->getXRef(), &xObj2);
+
+ if (xObj2.isStream()) {
+ xObj2.streamGetDict()->lookup("Resources", &resObj);
+ if (resObj.isDict() && resObj.getDict() != resDict) {
+ scanFonts(resObj.getDict(), list, fonts, fontsLen, fontsSize, visitedXObjects);
+ }
+ resObj.free();
+ }
+ xObj.free();
+ xObj2.free();
+ }
+ }
+ xObjDict.free();
+}
+
+void PDFGenerator::scanFont(GfxFont *font, KListView *list, Ref **fonts, int &fontsLen, int &fontsSize)
+{
+ Ref fontRef, embRef;
+ Object fontObj, toUnicodeObj;
+ GString *name;
+ GBool emb;
+ int i;
+
+ QString fontTypeNames[12] = {
+ i18n("unknown"),
+ i18n("Type 1"),
+ i18n("Type 1C"),
+ i18n("OT means OpenType", "Type 1C (OT)"),
+ i18n("Type 3"),
+ i18n("TrueType"),
+ i18n("OT means OpenType", "TrueType (OT)"),
+ i18n("CID Type 0"),
+ i18n("CID Type 0C"),
+ i18n("OT means OpenType", "CID Type 0C (OT)"),
+ i18n("CID TrueType"),
+ i18n("OT means OpenType", "CID TrueType (OT)")
+ };
+
+ fontRef = *font->getID();
+
+ // check for an already-seen font
+ for (i = 0; i < fontsLen; ++i)
+ {
+ if (fontRef.num == (*fonts)[i].num && fontRef.gen == (*fonts)[i].gen)
+ {
+ return;
+ }
+ }
+
+ // font name
+ name = font->getOrigName();
+
+ // check for an embedded font
+ if (font->getType() == fontType3) emb = gTrue;
+ else emb = font->getEmbeddedFontID(&embRef);
+
+ QString sName, sEmb, sPath;
+ if (name)
+ {
+ sName = name->getCString();
+ if (!emb)
+ {
+ DisplayFontParam *dfp = globalParams->getDisplayFont(name);
+ if (dfp)
+ {
+ if (dfp -> kind == displayFontT1) sPath = dfp->t1.fileName->getCString();
+ else sPath = dfp->tt.fileName->getCString();
+ }
+ else sPath = i18n("-");
+ }
+ else sPath = i18n("-");
+ }
+ else
+ {
+ sName = i18n("[none]");
+ sPath = i18n("-");
+ }
+ sEmb = emb ? i18n("Yes") : i18n("No");
+ new KListViewItem(list, sName, fontTypeNames[font->getType()], sEmb, sPath);
+
+ // add this font to the list
+ if (fontsLen == fontsSize)
+ {
+ fontsSize += 32;
+ *fonts = (Ref *)grealloc(*fonts, fontsSize * sizeof(Ref));
+ }
+ (*fonts)[fontsLen++] = *font->getID();
+}
+
+QString PDFGenerator::getDocumentInfo( const QString & data, bool canReturnNull ) const
+// note: MUTEX is LOCKED while calling this
+{
+ // [Albert] Code adapted from pdfinfo.cc on xpdf
+ Object info;
+ if ( !pdfdoc )
+ return canReturnNull ? QString::null : i18n( "Unknown" );
+
+ pdfdoc->getDocInfo( &info );
+ if ( !info.isDict() )
+ return canReturnNull ? QString::null : i18n( "Unknown" );
+
+ Object obj;
+ Dict *infoDict = info.getDict();
+
+ if ( infoDict->lookup( (char*)data.latin1(), &obj )->isString() )
+ {
+ QString result = UnicodeParsedString(obj.getString());
+ obj.free();
+ info.free();
+ return result;
+ }
+ obj.free();
+ info.free();
+ return canReturnNull ? QString::null : i18n( "Unknown" );
+}
+
+QString PDFGenerator::getDocumentDate( const QString & data ) const
+// note: MUTEX is LOCKED while calling this
+{
+ // [Albert] Code adapted from pdfinfo.cc on xpdf
+ if ( !pdfdoc )
+ return i18n( "Unknown Date" );
+
+ Object info;
+ pdfdoc->getDocInfo( &info );
+ if ( !info.isDict() )
+ return i18n( "Unknown Date" );
+
+ Object obj;
+ int year, mon, day, hour, min, sec;
+ Dict *infoDict = info.getDict();
+ UnicodeMap *uMap = globalParams->getTextEncoding();
+ QString result;
+
+ if ( !uMap )
+ return i18n( "Unknown Date" );
+
+ if ( infoDict->lookup( (char*)data.latin1(), &obj )->isString() )
+ {
+ QString s = UnicodeParsedString(obj.getString());
+ if ( s[0] == 'D' && s[1] == ':' )
+ s = s.mid(2);
+
+ if ( !s.isEmpty() && sscanf( s.latin1(), "%4d%2d%2d%2d%2d%2d", &year, &mon, &day, &hour, &min, &sec ) == 6 )
+ {
+ QDate d( year, mon, day ); //CHECK: it was mon-1, Jan->0 (??)
+ QTime t( hour, min, sec );
+ if ( d.isValid() && t.isValid() )
+ result = KGlobal::locale()->formatDateTime( QDateTime(d, t), false, true );
+ else
+ result = s;
+ }
+ else
+ result = s;
+ }
+ else
+ result = i18n( "Unknown Date" );
+ obj.free();
+ info.free();
+ return result;
+}
+
+void PDFGenerator::addSynopsisChildren( QDomNode * parent, GList * items )
+{
+ int numItems = items->getLength();
+ for ( int i = 0; i < numItems; ++i )
+ {
+ // iterate over every object in 'items'
+ OutlineItem * outlineItem = (OutlineItem *)items->get( i );
+
+ // 1. create element using outlineItem's title as tagName
+ QString name;
+ Unicode * uniChar = outlineItem->getTitle();
+ int titleLength = outlineItem->getTitleLength();
+ name = unicodeToQString(uniChar, titleLength);
+ if ( name.isEmpty() )
+ continue;
+ QDomElement item = docSyn.createElement( name );
+ parent->appendChild( item );
+
+ // 2. find the page the link refers to
+ LinkAction * a = outlineItem->getAction();
+ if ( a && ( a->getKind() == actionGoTo || a->getKind() == actionGoToR ) )
+ {
+ // page number is contained/referenced in a LinkGoTo
+ LinkGoTo * g = static_cast< LinkGoTo * >( a );
+ LinkDest * destination = g->getDest();
+ if ( !destination && g->getNamedDest() )
+ {
+ // no 'destination' but an internal 'named reference'. we could
+ // get the destination for the page now, but it's VERY time consuming,
+ // so better storing the reference and provide the viewport as metadata
+ // on demand
+ GString *s = g->getNamedDest();
+ QChar *charArray = new QChar[s->getLength()];
+ for (int i = 0; i < s->getLength(); ++i) charArray[i] = QChar(s->getCString()[i]);
+ QString option(charArray, s->getLength());
+ item.setAttribute( "ViewportName", option );
+ delete[] charArray;
+ }
+ else if ( destination && destination->isOk() )
+ {
+ DocumentViewport vp;
+ fillViewportFromLink( vp, destination );
+ item.setAttribute( "Viewport", vp.toString() );
+ }
+ if ( a->getKind() == actionGoToR )
+ {
+ LinkGoToR * g2 = static_cast< LinkGoToR * >( a );
+ item.setAttribute( "ExternalFileName", g2->getFileName()->getCString() );
+ }
+ }
+
+ item.setAttribute( "Open", QVariant( (bool)outlineItem->isOpen() ).toString() );
+
+ // 3. recursively descend over children
+ outlineItem->open();
+ GList * children = outlineItem->getKids();
+ if ( children )
+ addSynopsisChildren( &item, children );
+ }
+}
+
+void PDFGenerator::fillViewportFromLink( DocumentViewport &viewport, LinkDest *destination )
+{
+ if ( !destination->isPageRef() )
+ viewport.pageNumber = destination->getPageNum() - 1;
+ else
+ {
+ Ref ref = destination->getPageRef();
+ viewport.pageNumber = pdfdoc->findPage( ref.num, ref.gen ) - 1;
+ }
+
+ if (viewport.pageNumber < 0) return;
+ if (viewport.pageNumber >= pdfdoc->getNumPages()) return;
+
+ // get destination position
+ // TODO add other attributes to the viewport (taken from link)
+// switch ( destination->getKind() )
+// {
+// case destXYZ:
+ if (destination->getChangeLeft() || destination->getChangeTop())
+ {
+ double CTM[6];
+ Page *page = pdfdoc->getCatalog()->getPage( viewport.pageNumber + 1 );
+ // TODO remember to change this if we implement DPI and/or rotation
+ page->getDefaultCTM(CTM, 72.0, 72.0, 0, gFalse, gTrue);
+
+ int left, top;
+ // this is OutputDev::cvtUserToDev
+ left = (int)(CTM[0] * destination->getLeft() + CTM[2] * destination->getTop() + CTM[4] + 0.5);
+ top = (int)(CTM[1] * destination->getLeft() + CTM[3] * destination->getTop() + CTM[5] + 0.5);
+
+ viewport.rePos.normalizedX = (double)left / (double)page->getCropWidth();
+ viewport.rePos.normalizedY = (double)top / (double)page->getCropHeight();
+ viewport.rePos.enabled = true;
+ viewport.rePos.pos = DocumentViewport::TopLeft;
+ }
+ /* TODO
+ if ( dest->getChangeZoom() )
+ make zoom change*/
+/* break;
+
+ default:
+ // implement the others cases
+ break;*/
+// }
+}
+
+void PDFGenerator::addTransition( int pageNumber, KPDFPage * page )
+{
+ Page *pdfPage = pdfdoc->getCatalog()->getPage( pageNumber + 1 );
+ if ( !pdfPage )
+ return;
+
+ PageTransition *pdfTransition = pdfPage->getTransition();
+ if ( !pdfTransition || pdfTransition->getType() == PageTransition::Replace )
+ return;
+
+ KPDFPageTransition *transition = new KPDFPageTransition();
+ switch ( pdfTransition->getType() ) {
+ case PageTransition::Replace:
+ // won't get here, added to avoid warning
+ break;
+ case PageTransition::Split:
+ transition->setType( KPDFPageTransition::Split );
+ break;
+ case PageTransition::Blinds:
+ transition->setType( KPDFPageTransition::Blinds );
+ break;
+ case PageTransition::Box:
+ transition->setType( KPDFPageTransition::Box );
+ break;
+ case PageTransition::Wipe:
+ transition->setType( KPDFPageTransition::Wipe );
+ break;
+ case PageTransition::Dissolve:
+ transition->setType( KPDFPageTransition::Dissolve );
+ break;
+ case PageTransition::Glitter:
+ transition->setType( KPDFPageTransition::Glitter );
+ break;
+ case PageTransition::Fly:
+ transition->setType( KPDFPageTransition::Fly );
+ break;
+ case PageTransition::Push:
+ transition->setType( KPDFPageTransition::Push );
+ break;
+ case PageTransition::Cover:
+ transition->setType( KPDFPageTransition::Cover );
+ break;
+ case PageTransition::Uncover:
+ transition->setType( KPDFPageTransition::Uncover );
+ break;
+ case PageTransition::Fade:
+ transition->setType( KPDFPageTransition::Fade );
+ break;
+ }
+
+ transition->setDuration( pdfTransition->getDuration() );
+
+ switch ( pdfTransition->getAlignment() ) {
+ case PageTransition::Horizontal:
+ transition->setAlignment( KPDFPageTransition::Horizontal );
+ break;
+ case PageTransition::Vertical:
+ transition->setAlignment( KPDFPageTransition::Vertical );
+ break;
+ }
+
+ switch ( pdfTransition->getDirection() ) {
+ case PageTransition::Inward:
+ transition->setDirection( KPDFPageTransition::Inward );
+ break;
+ case PageTransition::Outward:
+ transition->setDirection( KPDFPageTransition::Outward );
+ break;
+ }
+
+ transition->setAngle( pdfTransition->getAngle() );
+ transition->setScale( pdfTransition->getScale() );
+ transition->setIsRectangular( pdfTransition->isRectangular() == gTrue );
+
+ page->setTransition( transition );
+}
+
+
+
+void PDFGenerator::customEvent( QCustomEvent * event )
+{
+ // catch generator 'ready events' only
+ if ( event->type() != TGE_DATAREADY_ID )
+ return;
+
+#if 0
+ // check if thread is running (has to be stopped now)
+ if ( generatorThread->running() )
+ {
+ // if so, wait for effective thread termination
+ if ( !generatorThread->wait( 9999 /*10s timeout*/ ) )
+ {
+ kdWarning() << "PDFGenerator: thread sent 'data available' "
+ << "signal but had problems ending." << endl;
+ return;
+ }
+}
+#endif
+
+ // 1. the mutex must be unlocked now
+ if ( docLock.locked() )
+ {
+ kdWarning() << "PDFGenerator: 'data available' but mutex still "
+ << "held. Recovering." << endl;
+ // syncronize GUI thread (must not happen)
+ docLock.lock();
+ docLock.unlock();
+ }
+
+ // 2. put thread's generated data into the KPDFPage
+ PixmapRequest * request = static_cast< PixmapRequest * >( event->data() );
+ QImage * outImage = generatorThread->takeImage();
+ TextPage * outTextPage = generatorThread->takeTextPage();
+ QValueList< ObjectRect * > outRects = generatorThread->takeObjectRects();
+
+ request->page->setPixmap( request->id, new QPixmap( *outImage ) );
+ delete outImage;
+ if ( outTextPage )
+ request->page->setSearchPage( outTextPage );
+ if ( !outRects.isEmpty() )
+ request->page->setObjectRects( outRects );
+
+ // 3. tell generator that data has been taken
+ generatorThread->endGeneration();
+
+ // update ready state
+ ready = true;
+ // notify the new generation
+ signalRequestDone( request );
+}
+
+
+
+/** The PDF Pixmap Generator Thread **/
+
+struct PPGThreadPrivate
+{
+ // reference to main objects
+ PDFGenerator * generator;
+ PixmapRequest * currentRequest;
+
+ // internal temp stored items. don't delete this.
+ QImage * m_image;
+ TextPage * m_textPage;
+ QValueList< ObjectRect * > m_rects;
+ bool m_rectsTaken;
+};
+
+PDFPixmapGeneratorThread::PDFPixmapGeneratorThread( PDFGenerator * gen )
+ : d( new PPGThreadPrivate() )
+{
+ d->generator = gen;
+ d->currentRequest = 0;
+ d->m_image = 0;
+ d->m_textPage = 0;
+ d->m_rectsTaken = true;
+}
+
+PDFPixmapGeneratorThread::~PDFPixmapGeneratorThread()
+{
+ // delete internal objects if the class is deleted before the gui thread
+ // takes the data
+ delete d->m_image;
+ delete d->m_textPage;
+ if ( !d->m_rectsTaken && d->m_rects.count() )
+ {
+ QValueList< ObjectRect * >::iterator it = d->m_rects.begin(), end = d->m_rects.end();
+ for ( ; it != end; ++it )
+ delete *it;
+ }
+ delete d->currentRequest;
+ // delete internal storage structure
+ delete d;
+}
+
+void PDFPixmapGeneratorThread::startGeneration( PixmapRequest * request )
+{
+#ifndef NDEBUG
+ // check if a generation is already running
+ if ( d->currentRequest )
+ {
+ kdDebug() << "PDFPixmapGeneratorThread: requesting a pixmap "
+ << "when another is being generated." << endl;
+ delete request;
+ return;
+ }
+
+ // check if the mutex is already held
+ if ( d->generator->docLock.locked() )
+ {
+ kdDebug() << "PDFPixmapGeneratorThread: requesting a pixmap "
+ << "with the mutex already held." << endl;
+ delete request;
+ return;
+ }
+#endif
+ // set generation parameters and run thread
+ d->currentRequest = request;
+ start( QThread::InheritPriority );
+}
+
+void PDFPixmapGeneratorThread::endGeneration()
+{
+#ifndef NDEBUG
+ // check if a generation is already running
+ if ( !d->currentRequest )
+ {
+ kdDebug() << "PDFPixmapGeneratorThread: 'end generation' called "
+ << "but generation was not started." << endl;
+ return;
+ }
+#endif
+ // reset internal members preparing for a new generation
+ d->currentRequest = 0;
+}
+
+QImage * PDFPixmapGeneratorThread::takeImage() const
+{
+ QImage * img = d->m_image;
+ d->m_image = 0;
+ return img;
+}
+
+TextPage * PDFPixmapGeneratorThread::takeTextPage() const
+{
+ TextPage * tp = d->m_textPage;
+ d->m_textPage = 0;
+ return tp;
+}
+
+QValueList< ObjectRect * > PDFPixmapGeneratorThread::takeObjectRects() const
+{
+ d->m_rectsTaken = true;
+ return d->m_rects;
+}
+
+void PDFPixmapGeneratorThread::run()
+// perform contents generation, when the MUTEX is already LOCKED
+// @see PDFGenerator::generatePixmap( .. ) (and be aware to sync the code)
+{
+ // compute dpi used to get an image with desired width and height
+ KPDFPage * page = d->currentRequest->page;
+ int width = d->currentRequest->width,
+ height = d->currentRequest->height;
+ double fakeDpiX = width * 72.0 / page->width(),
+ fakeDpiY = height * 72.0 / page->height();
+
+ // setup kpdf output device: text page is generated only if we are at 72dpi.
+ // since we can pre-generate the TextPage at the right res.. why not?
+ bool genTextPage = !page->hasSearchPage() &&
+ ( width == page->width() ) &&
+ ( height == page->height() );
+
+ // generate links and image rects if rendering pages on pageview
+ bool genObjectRects = d->currentRequest->id & (PAGEVIEW_ID | PRESENTATION_ID);
+
+ // 0. LOCK s[tart locking XPDF thread unsafe classes]
+ d->generator->docLock.lock();
+
+ // 1. set OutputDev parameters and Generate contents
+ d->generator->kpdfOutputDev->setParams( width, height,
+ genObjectRects, genObjectRects, TRUE /*thread safety*/ );
+ d->generator->pdfdoc->displayPage( d->generator->kpdfOutputDev, page->number() + 1,
+ fakeDpiX, fakeDpiY, 0, false, true, false );
+ if ( genObjectRects )
+ d->generator->pdfdoc->processLinks( d->generator->kpdfOutputDev, page->number() + 1 );
+
+ // 2. grab data from the OutputDev and store it locally (note takeIMAGE)
+#ifndef NDEBUG
+ if ( d->m_image )
+ kdDebug() << "PDFPixmapGeneratorThread: previous image not taken" << endl;
+ if ( d->m_textPage )
+ kdDebug() << "PDFPixmapGeneratorThread: previous textpage not taken" << endl;
+#endif
+ d->m_image = d->generator->kpdfOutputDev->takeImage();
+ d->m_rects = d->generator->kpdfOutputDev->takeObjectRects();
+ d->m_rectsTaken = false;
+
+ if ( genTextPage )
+ {
+ TextOutputDev td(NULL, gTrue, gFalse, gFalse);
+ d->generator->pdfdoc->displayPage( &td, page->number()+1, 72, 72, 0, false, true, false );
+ // ..and attach it to the page
+ d->m_textPage = td.takeText();
+ }
+
+ // 3. [UNLOCK] mutex
+ d->generator->docLock.unlock();
+
+ // notify the GUI thread that data is pending and can be read
+ QCustomEvent * readyEvent = new QCustomEvent( TGE_DATAREADY_ID );
+ readyEvent->setData( d->currentRequest );
+ QApplication::postEvent( d->generator, readyEvent );
+}
diff --git a/kpdf/core/generator_pdf/generator_pdf.h b/kpdf/core/generator_pdf/generator_pdf.h
new file mode 100644
index 00000000..ca267b18
--- /dev/null
+++ b/kpdf/core/generator_pdf/generator_pdf.h
@@ -0,0 +1,150 @@
+/***************************************************************************
+ * Copyright (C) 2004 by Albert Astals Cid <[email protected]> *
+ * Copyright (C) 2004 by Enrico Ros <[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 *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#ifndef _KPDF_GENERATOR_PDF_H_
+#define _KPDF_GENERATOR_PDF_H_
+
+#include <qmutex.h>
+#include <qcolor.h>
+#include <qstring.h>
+#include <qthread.h>
+#include "core/generator.h"
+#include "core/document.h"
+#include "core/link.h"
+
+class Dict;
+class GfxFont;
+class LinkDest;
+class Ref;
+class PDFDoc;
+class GList;
+class TextPage;
+
+class ObjectRect;
+class KPDFOutputDev;
+class PDFPixmapGeneratorThread;
+
+/**
+ * @short A generator that builds contents from a PDF document.
+ *
+ * All Generator features are supported and implented by this one.
+ * Internally this holds a reference to xpdf's core objects and provides
+ * contents generation using the PDFDoc object and a couple of OutputDevices
+ * called KPDFOutputDev and KPDFTextDev (both defined in gp_outputdev.h).
+ *
+ * For generating page contents we tell PDFDoc to render a page and grab
+ * contents from out OutputDevs when rendering finishes.
+ *
+ * Background asyncronous contents providing is done via a QThread inherited
+ * class defined at the bottom of the file.
+ */
+class PDFGenerator : public Generator
+{
+ public:
+ PDFGenerator( KPDFDocument * document );
+ virtual ~PDFGenerator();
+
+ // [INHERITED] load a document and fill up the pagesVector
+ bool loadDocument( const QString & fileName, QValueVector<KPDFPage*> & pagesVector );
+
+ // [INHERITED] document informations
+ const DocumentInfo * generateDocumentInfo();
+ const DocumentSynopsis * generateDocumentSynopsis();
+
+ // [INHERITED] document informations
+ bool isAllowed( int permissions );
+
+ // [INHERITED] perform actions on document / pages
+ bool canGeneratePixmap();
+ void generatePixmap( PixmapRequest * request );
+ void generateSyncTextPage( KPDFPage * page );
+
+ // [INHERITED] capability querying
+ bool supportsSearching() const;
+ bool hasFonts() const;
+
+ // [INHERITED] font related
+ void putFontInfo(KListView *list);
+
+ // [INHERITED] print page using an already configured kprinter
+ bool print( KPrinter& printer );
+
+ // [INHERITED] reply to some metadata requests
+ QString getMetaData( const QString & key, const QString & option );
+
+ // [INHERITED] reparse configuration
+ bool reparseConfig();
+
+ private:
+ // friend class to access private document related variables
+ friend class PDFPixmapGeneratorThread;
+
+ void scanFonts(Dict *resDict, KListView *list, Ref **fonts, int &fontsLen, int &fontsSize, QValueVector<Ref> *visitedXObjects);
+ void scanFont(GfxFont *font, KListView *list, Ref **fonts, int &fontsLen, int &fontsSize);
+
+ void fillViewportFromLink( DocumentViewport &viewport, LinkDest *destination );
+
+ // private functions for accessing document informations via PDFDoc
+ QString getDocumentInfo( const QString & data, bool canReturnNull = false ) const;
+ QString getDocumentDate( const QString & data ) const;
+ // private function for creating the document synopsis hieracy
+ void addSynopsisChildren( QDomNode * parent, GList * items );
+ // private function for creating the transition information
+ void addTransition( int pageNumber, KPDFPage * page );
+ // (async related) receive data from the generator thread
+ void customEvent( QCustomEvent * );
+
+ // xpdf dependant stuff
+ QMutex docLock;
+ PDFDoc * pdfdoc;
+ KPDFOutputDev * kpdfOutputDev;
+ QColor paperColor;
+
+ // asyncronous generation related stuff
+ PDFPixmapGeneratorThread * generatorThread;
+
+ // misc variables for document info and synopsis caching
+ bool ready;
+ PixmapRequest * pixmapRequest;
+ bool docInfoDirty;
+ DocumentInfo docInfo;
+ bool docSynopsisDirty;
+ DocumentSynopsis docSyn;
+};
+
+
+/**
+ * @short A thread that builds contents for PDFGenerator in the background.
+ *
+ */
+class PDFPixmapGeneratorThread : public QThread
+{
+ public:
+ PDFPixmapGeneratorThread( PDFGenerator * generator );
+ ~PDFPixmapGeneratorThread();
+
+ // set the request to the thread (it will be reparented)
+ void startGeneration( PixmapRequest * request );
+ // end generation
+ void endGeneration();
+
+ // methods for getting contents from the GUI thread
+ QImage * takeImage() const;
+ TextPage * takeTextPage() const;
+ QValueList< ObjectRect * > takeObjectRects() const;
+
+ private:
+ // can't be called from the outside (but from startGeneration)
+ void run();
+
+ class PPGThreadPrivate * d;
+};
+
+#endif
diff --git a/kpdf/core/generator_pdf/gp_outputdev.cpp b/kpdf/core/generator_pdf/gp_outputdev.cpp
new file mode 100644
index 00000000..c55ccd7c
--- /dev/null
+++ b/kpdf/core/generator_pdf/gp_outputdev.cpp
@@ -0,0 +1,397 @@
+/***************************************************************************
+ * Copyright (C) 2003-2004 by Christophe Devriese *
+ * Copyright (C) 2003 by Andy Goossens <[email protected]> *
+ * Copyright (C) 2003 by Scott Wheeler <[email protected]> *
+ * Copyright (C) 2003 by Ingo Klöcker <[email protected]> *
+ * Copyright (C) 2003 by Will Andrews <[email protected]> *
+ * Copyright (C) 2004 by Dominique Devriese <[email protected]> *
+ * Copyright (C) 2004 by Waldo Bastian <[email protected]> *
+ * Copyright (C) 2004 by Albert Astals Cid <[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 *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#ifdef __GNUC__
+#pragma implementation
+#endif
+
+#include <kdebug.h>
+#include <qpixmap.h>
+#include <qimage.h>
+
+#include "gp_outputdev.h"
+#include "generator_pdf.h"
+#include "core/document.h" // for DocumentViewport
+#include "core/page.h"
+#include "core/link.h"
+#include "xpdf/Link.h"
+#include "xpdf/GfxState.h"
+#include "xpdf/TextOutputDev.h"
+#include "splash/SplashBitmap.h"
+
+//NOTE: XPDF/Splash *implementation dependant* code is marked with '###'
+
+/** KPDFOutputDev implementation **/
+
+KPDFOutputDev::KPDFOutputDev( SplashColor paperColor )
+ : SplashOutputDev( splashModeRGB8, 4, false, paperColor ),
+ m_doc( 0 ), m_pixmap( 0 ), m_image( 0 )
+{
+}
+
+KPDFOutputDev::~KPDFOutputDev()
+{
+ clear();
+}
+
+void KPDFOutputDev::initDevice( PDFDoc * pdfDoc )
+{
+ m_doc = pdfDoc;
+ startDoc( pdfDoc->getXRef() );
+}
+
+void KPDFOutputDev::setParams( int width, int height, bool genL, bool genI, bool safe )
+{
+ clear();
+
+ m_pixmapWidth = width;
+ m_pixmapHeight = height;
+
+ m_qtThreadSafety = safe;
+ m_generateLinks = genL;
+ m_generateImages = genI;
+}
+
+QPixmap * KPDFOutputDev::takePixmap()
+{
+ QPixmap * pix = m_pixmap;
+ m_pixmap = 0;
+ return pix;
+}
+
+QImage * KPDFOutputDev::takeImage()
+{
+ QImage * img = m_image;
+ m_image = 0;
+ return img;
+}
+
+QValueList< ObjectRect * > KPDFOutputDev::takeObjectRects()
+{
+ if ( m_rects.isEmpty() )
+ return m_rects;
+ QValueList< ObjectRect * > rectsCopy( m_rects );
+ m_rects.clear();
+ return rectsCopy;
+}
+
+//BEGIN - OutputDev hooked calls
+void KPDFOutputDev::endPage()
+{
+ SplashOutputDev::endPage();
+
+ int bh = getBitmap()->getHeight(),
+ bw = getBitmap()->getWidth();
+ // TODO The below loop can be avoided if using the code that is commented here and
+ // we change splashModeRGB8 to splashModeARGB8 the problem is that then bug101800.pdf
+ // does not work
+/* SplashColorPtr dataPtr = getBitmap()->getDataPtr();
+ // construct a qimage SHARING the raw bitmap data in memory
+ QImage * img = new QImage( dataPtr, bw, bh, 32, 0, 0, QImage::IgnoreEndian );*/
+ QImage * img = new QImage( bw, bh, 32 );
+ SplashColorPtr pixel = new Guchar[4];
+ for (int i = 0; i < bw; i++)
+ {
+ for (int j = 0; j < bh; j++)
+ {
+ getBitmap()->getPixel(i, j, pixel);
+ img->setPixel( i, j, qRgb( pixel[0], pixel[1], pixel[2] ) );
+ }
+ }
+ delete [] pixel;
+
+ // use the QImage or convert it immediately to QPixmap for better
+ // handling and memory unloading
+ if ( m_qtThreadSafety )
+ {
+ delete m_image;
+ // it may happen (in fact it doesn't) that we need a rescaling
+ if ( bw != m_pixmapWidth && bh != m_pixmapHeight )
+ m_image = new QImage( img->smoothScale( m_pixmapWidth, m_pixmapHeight ) );
+ else
+ // dereference image from the xpdf memory
+ m_image = new QImage( img->copy() );
+ }
+ else
+ {
+ delete m_pixmap;
+ // it may happen (in fact it doesn't) that we need a rescaling
+ if ( bw != m_pixmapWidth || bh != m_pixmapHeight )
+ m_pixmap = new QPixmap( img->smoothScale( m_pixmapWidth, m_pixmapHeight ) );
+ else
+ m_pixmap = new QPixmap( *img );
+ }
+
+ // destroy the shared descriptor and (###) unload underlying xpdf bitmap
+ delete img;
+ SplashOutputDev::startPage( 0, NULL );
+}
+
+void KPDFOutputDev::processLink( Link * link, Catalog * catalog )
+{
+ if ( !link->isOk() )
+ return;
+
+ if ( m_generateLinks )
+ {
+ // create the link descriptor
+ KPDFLink * l = generateLink( link->getAction() );
+ if ( l )
+ {
+ // create the page rect representing the link
+ double x1, y1, x2, y2;
+ link->getRect( &x1, &y1, &x2, &y2 );
+ int left, top, right, bottom;
+ cvtUserToDev( x1, y1, &left, &top );
+ cvtUserToDev( x2, y2, &right, &bottom );
+ double nl = (double)left / (double)m_pixmapWidth,
+ nt = (double)top / (double)m_pixmapHeight,
+ nr = (double)right / (double)m_pixmapWidth,
+ nb = (double)bottom / (double)m_pixmapHeight;
+ // create the rect using normalized coords and attach the KPDFLink to it
+ ObjectRect * rect = new ObjectRect( nl, nt, nr, nb, ObjectRect::Link, l );
+ // add the ObjectRect to the vector container
+ m_rects.push_front( rect );
+ }
+ }
+ SplashOutputDev::processLink( link, catalog );
+}
+
+void KPDFOutputDev::drawImage( GfxState *state, Object *ref, Stream *str,
+ int _width, int _height, GfxImageColorMap *colorMap, int *maskColors, GBool inlineImg )
+{
+ if ( m_generateImages )
+ {
+ // find out image rect from the Coord Transform Matrix
+ double * ctm = state->getCTM();
+ int left = (int)ctm[4],
+ top = (int)ctm[5],
+ width = (int)ctm[0],
+ height = (int)ctm[3];
+ // normalize width
+ if ( width < 0 )
+ {
+ width = -width;
+ left -= width;
+ }
+ // normalize height
+ if ( height < 0 )
+ {
+ height = -height;
+ top -= height;
+ }
+ if ( width > 10 && height > 10 )
+ {
+ // build a descriptor for the image rect
+ double nl = (double)left / (double)m_pixmapWidth,
+ nt = (double)top / (double)m_pixmapHeight,
+ nr = (double)(left + width) / (double)m_pixmapWidth,
+ nb = (double)(top + height) / (double)m_pixmapHeight;
+ // create the rect using normalized coords and set it of KPDFImage type
+ ObjectRect * rect = new ObjectRect( nl, nt, nr, nb, ObjectRect::Image, 0 );
+ // add the ObjectRect to the vector container
+ m_rects.push_back( rect );
+ }
+ }
+ SplashOutputDev::drawImage( state, ref, str, _width, _height, colorMap, maskColors, inlineImg );
+}
+//END - OutputDev hooked calls
+
+//BEGIN - private helpers
+void KPDFOutputDev::clear()
+{
+ // delete rects
+ if ( m_rects.count() )
+ {
+ QValueList< ObjectRect * >::iterator it = m_rects.begin(), end = m_rects.end();
+ for ( ; it != end; ++it )
+ delete *it;
+ m_rects.clear();
+ }
+ // delete pixmap
+ if ( m_pixmap )
+ {
+ delete m_pixmap;
+ m_pixmap = 0;
+ }
+ // delete image
+ if ( m_image )
+ {
+ delete m_image;
+ m_image = 0;
+ }
+}
+
+KPDFLink * KPDFOutputDev::generateLink( LinkAction * a )
+// note: this function is called when processing a page, when the MUTEX is already LOCKED
+{
+ KPDFLink * link = NULL;
+ if ( a ) switch ( a->getKind() )
+ {
+ case actionGoTo:
+ {
+ LinkGoTo * g = (LinkGoTo *) a;
+ // ceate link: no ext file, namedDest, object pointer
+ link = new KPDFLinkGoto( QString::null, decodeViewport( g->getNamedDest(), g->getDest() ) );
+ }
+ break;
+
+ case actionGoToR:
+ {
+ LinkGoToR * g = (LinkGoToR *) a;
+ // copy link file
+ const char * fileName = g->getFileName()->getCString();
+ // ceate link: fileName, namedDest, object pointer
+ link = new KPDFLinkGoto( (QString)fileName, decodeViewport( g->getNamedDest(), g->getDest() ) );
+ }
+ break;
+
+ case actionLaunch:
+ {
+ LinkLaunch * e = (LinkLaunch *)a;
+ GString * p = e->getParams();
+ link = new KPDFLinkExecute( e->getFileName()->getCString(), p ? p->getCString() : 0 );
+ }
+ break;
+
+ case actionNamed:
+ {
+ const char * name = ((LinkNamed *)a)->getName()->getCString();
+ if ( !strcmp( name, "NextPage" ) )
+ link = new KPDFLinkAction( KPDFLinkAction::PageNext );
+ else if ( !strcmp( name, "PrevPage" ) )
+ link = new KPDFLinkAction( KPDFLinkAction::PagePrev );
+ else if ( !strcmp( name, "FirstPage" ) )
+ link = new KPDFLinkAction( KPDFLinkAction::PageFirst );
+ else if ( !strcmp( name, "LastPage" ) )
+ link = new KPDFLinkAction( KPDFLinkAction::PageLast );
+ else if ( !strcmp( name, "GoBack" ) )
+ link = new KPDFLinkAction( KPDFLinkAction::HistoryBack );
+ else if ( !strcmp( name, "GoForward" ) )
+ link = new KPDFLinkAction( KPDFLinkAction::HistoryForward );
+ else if ( !strcmp( name, "Quit" ) )
+ link = new KPDFLinkAction( KPDFLinkAction::Quit );
+ else if ( !strcmp( name, "GoToPage" ) )
+ link = new KPDFLinkAction( KPDFLinkAction::GoToPage );
+ else if ( !strcmp( name, "Find" ) )
+ link = new KPDFLinkAction( KPDFLinkAction::Find );
+ else if ( !strcmp( name, "Close" ) )
+ link = new KPDFLinkAction( KPDFLinkAction::Close );
+ else
+ kdDebug() << "Unknown named action: '" << name << "'" << endl;
+ }
+ break;
+
+ case actionURI:
+ link = new KPDFLinkBrowse( ((LinkURI *)a)->getURI()->getCString() );
+ break;
+
+ case actionMovie:
+/* { TODO this (Movie link)
+ m_type = Movie;
+ LinkMovie * m = (LinkMovie *) a;
+ // copy Movie parameters (2 IDs and a const char *)
+ Ref * r = m->getAnnotRef();
+ m_refNum = r->num;
+ m_refGen = r->gen;
+ copyString( m_uri, m->getTitle()->getCString() );
+ }
+*/ break;
+
+ case actionUnknown:
+ kdDebug() << "Unknown link." << endl;
+ break;
+ }
+
+ // link may be zero at that point
+ return link;
+}
+
+DocumentViewport KPDFOutputDev::decodeViewport( GString * namedDest, LinkDest * dest )
+// note: this function is called when processing a page, when the MUTEX is already LOCKED
+{
+ DocumentViewport vp( -1 );
+ bool deleteDest = false;
+
+ if ( namedDest && !dest )
+ {
+ deleteDest = true;
+ dest = m_doc->findDest( namedDest );
+ }
+
+ if ( !dest || !dest->isOk() )
+ {
+ if (deleteDest) delete dest;
+ return vp;
+ }
+
+ // get destination page number
+ if ( !dest->isPageRef() )
+ vp.pageNumber = dest->getPageNum() - 1;
+ else
+ {
+ Ref ref = dest->getPageRef();
+ vp.pageNumber = m_doc->findPage( ref.num, ref.gen ) - 1;
+ }
+
+ // get destination position
+ // TODO add other attributes to the viewport (taken from link)
+ switch ( dest->getKind() )
+ {
+ case destXYZ:
+ if (dest->getChangeLeft() || dest->getChangeTop())
+ {
+ int left, top;
+ cvtUserToDev( dest->getLeft(), dest->getTop(), &left, &top );
+ vp.rePos.normalizedX = (double)left / (double)m_pixmapWidth;
+ vp.rePos.normalizedY = (double)top / (double)m_pixmapHeight;
+ vp.rePos.enabled = true;
+ vp.rePos.pos = DocumentViewport::TopLeft;
+ }
+ /* TODO
+ if ( dest->getChangeZoom() )
+ make zoom change*/
+ break;
+
+ case destFit:
+ case destFitB:
+ //vp.fitWidth = true;
+ //vp.fitHeight = true;
+ break;
+
+ case destFitH:
+ case destFitBH:
+// read top, fit Width
+ //vp.fitWidth = true;
+ break;
+
+ case destFitV:
+ case destFitBV:
+// read left, fit Height
+ //vp.fitHeight = true;
+ break;
+
+ case destFitR:
+// read and fit left,bottom,right,top
+ break;
+ }
+
+ if (deleteDest) delete dest;
+ return vp;
+}
+//END - private helpers
+
diff --git a/kpdf/core/generator_pdf/gp_outputdev.h b/kpdf/core/generator_pdf/gp_outputdev.h
new file mode 100644
index 00000000..e08724e0
--- /dev/null
+++ b/kpdf/core/generator_pdf/gp_outputdev.h
@@ -0,0 +1,90 @@
+/***************************************************************************
+ * Copyright (C) 2003-2004 by Christophe Devriese *
+ * Copyright (C) 2003 by Helio Chissini de Castro *
+ * Copyright (C) 2004 by Dominique Devriese <[email protected]> *
+ * Copyright (C) 2004 by Albert Astals Cid <[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 *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#ifndef KPDFOUTPUTDEV_H
+#define KPDFOUTPUTDEV_H
+
+#ifdef __GNUC__
+#pragma interface
+#endif
+
+#include <qvaluelist.h>
+#include "xpdf/PDFDoc.h" // for 'Object'
+#include "xpdf/SplashOutputDev.h"
+
+class QPixmap;
+class KPDFLink;
+class ObjectRect;
+class DocumentViewport;
+
+/**
+ * @short A SplashOutputDev renderer that grabs text and links.
+ *
+ * This output device:
+ * - renders the page using SplashOutputDev (its parent)
+ * - harvests text into a textPage (for searching text)
+ * - harvests links and collects them
+ * - collects images and collects them
+ */
+class KPDFOutputDev : public SplashOutputDev
+{
+ public:
+ KPDFOutputDev( SplashColor paperColor );
+ virtual ~KPDFOutputDev();
+
+ // initialize device -> attach device to PDFDoc
+ void initDevice( class PDFDoc * pdfDoc );
+
+ // set parameters before rendering *each* page
+ // @param qtThreadSafety: duplicate memory buffer (slow but safe)
+ void setParams( int pixmapWidth, int pixmapHeight,
+ bool decodeLinks, bool decodeImages, bool qtThreadSafety = false );
+
+ // takes pointers out of the class (so deletion it's up to others)
+ QPixmap * takePixmap();
+ QImage * takeImage();
+ QValueList< ObjectRect * > takeObjectRects();
+
+ /** inherited from OutputDev */
+ // End a page.
+ virtual void endPage();
+ //----- link borders
+ virtual void processLink(Link *link, Catalog *catalog);
+ //----- image drawing
+ virtual void drawImage(GfxState *state, Object *ref, Stream *str, int width, int height,
+ GfxImageColorMap *colorMap, int *maskColors, GBool inlineImg);
+
+ private:
+ // delete all interal objects and data
+ void clear();
+ // generate a valid KPDFLink subclass (or null) from a xpdf's LinkAction
+ KPDFLink * generateLink( LinkAction * a );
+ // fills up a Viewport structure out of a given LinkGoto link
+ DocumentViewport decodeViewport( GString *, class LinkDest * );
+
+ // generator switches and parameters
+ bool m_qtThreadSafety;
+ bool m_generateLinks;
+ bool m_generateImages;
+ int m_pixmapWidth;
+ int m_pixmapHeight;
+
+ // Internal objects
+ PDFDoc * m_doc;
+ QPixmap * m_pixmap;
+ QImage * m_image;
+ QValueList< ObjectRect * > m_rects; // objectRects (links/images)
+};
+
+#endif