generator_pdf.h 5.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/***************************************************************************
 *   Copyright (C) 2004 by Albert Astals Cid <tsdgeos@terra.es>            *
 *   Copyright (C) 2004 by Enrico Ros <eros.kde@email.it>                  *
 *                                                                         *
 *   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>
17
#include <qthread.h>
18 19 20
#include "core/generator.h"
#include "core/document.h"
#include "core/link.h"
Piotr Szymanski's avatar
Piotr Szymanski committed
21
#include "core/textpage.h"
22 23

class PDFDoc;
24
class GList;
25
class TextPage;
26
class Page;
27 28
class Dict;
class Ref;
29 30

class ObjectRect;
31
class KPDFOutputDev;
32
class PDFPixmapGeneratorThread;
33 34 35 36

/**
 * @short A generator that builds contents from a PDF document.
 *
37 38 39
 * 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
40
 * called KPDFOutputDev and KPDFTextDev (both defined in gp_outputdev.h).
41 42 43 44 45 46
 *
 * 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.
47
 */
48
class PDFGenerator : public Generator
49 50
{
    public:
Enrico Ros's avatar
Enrico Ros committed
51
        PDFGenerator( KPDFDocument * document );
52
        virtual ~PDFGenerator();
53 54 55

        // [INHERITED] load a document and fill up the pagesVector
        bool loadDocument( const QString & fileName, QValueVector<KPDFPage*> & pagesVector );
56
        void loadPages(QValueVector<KPDFPage*> & pagesVector, int rotation, bool clear=false);
57
        // [INHERITED] document informations
Enrico Ros's avatar
Enrico Ros committed
58 59
        const DocumentInfo * generateDocumentInfo();
        const DocumentSynopsis * generateDocumentSynopsis();
60
        const DocumentFonts * generateDocumentFonts();
61

62 63 64
        // [INHERITED] document informations
        bool isAllowed( int permissions );

65
        // [INHERITED] perform actions on document / pages
Enrico Ros's avatar
Enrico Ros committed
66 67
        bool canGeneratePixmap();
        void generatePixmap( PixmapRequest * request );
68
        bool canGenerateTextPage();
Enrico Ros's avatar
Enrico Ros committed
69
        void generateSyncTextPage( KPDFPage * page );
Piotr Szymanski's avatar
Piotr Szymanski committed
70
        bool supportsSearching() { return true; };
71
        bool supportsRotation() { return true; };
Piotr Szymanski's avatar
Piotr Szymanski committed
72
        bool prefersInternalSearching() { return false; };
73

Piotr Szymanski's avatar
Piotr Szymanski committed
74 75 76 77
        RegularAreaRect * findText (const QString & text, SearchDir dir, 
            const bool strictCase, const RegularAreaRect * lastRect, 
            KPDFPage * page );
        QString * getText( const RegularAreaRect * area, KPDFPage * page );
78 79 80

        void setOrientation(QValueVector<KPDFPage*> & pagesVector, int orientation);

Enrico Ros's avatar
Enrico Ros committed
81
        // [INHERITED] print page using an already configured kprinter
82
        bool print( KPrinter& printer );
Enrico Ros's avatar
Enrico Ros committed
83 84 85

        // [INHERITED] reply to some metadata requests
        QString getMetaData( const QString & key, const QString & option );
86 87 88 89 90

        // [INHERITED] reparse configuration
        bool reparseConfig();

    private:
91 92 93
        // friend class to access private document related variables
        friend class PDFPixmapGeneratorThread;

94
        // access document informations
95 96
        QString getDocumentInfo( const QString & data ) const;
        QString getDocumentDate( const QString & data ) const;
97
        // create the document synopsis hieracy
98
        void addSynopsisChildren( QDomNode * parent, GList * items );
99 100 101
        // add fonts (in resDict) to the private 'docFonts' class
        void addFonts( Dict * resDict, Ref ** fonts, int &fontsLen, int &fontsSize );
        // fetch annotations from the pdf file and add they to the page
102
        void addAnnotations( Page * xpdfPage, KPDFPage * page );
103
        // fetch the transition information and add it to the page
104
        void addTransition( Page * xpdfPage, KPDFPage * page );
Piotr Szymanski's avatar
Piotr Szymanski committed
105 106 107
        
        static KPDFTextPage * abstractTextPage(TextPage *tp, double height, double width);
        TextPage * fastTextPage (KPDFPage * page);
108

Enrico Ros's avatar
Enrico Ros committed
109
        // (async related) receive data from the generator thread
110
        void customEvent( QCustomEvent * );
111

112
        // xpdf dependant stuff
113 114 115 116
        QMutex docLock;
        PDFDoc * pdfdoc;
        KPDFOutputDev * kpdfOutputDev;
        QColor paperColor;
117

Enrico Ros's avatar
Enrico Ros committed
118
        // asyncronous generation related stuff
119
        PDFPixmapGeneratorThread * generatorThread;
120 121

        // misc variables for document info and synopsis caching
Enrico Ros's avatar
Enrico Ros committed
122 123
        bool ready;
        PixmapRequest * pixmapRequest;
124 125
        bool docInfoDirty;
        DocumentInfo docInfo;
126 127
        bool docSynopsisDirty;
        DocumentSynopsis docSyn;
128 129
        bool docFontsDirty;
        DocumentFonts docFonts;
Piotr Szymanski's avatar
Piotr Szymanski committed
130 131 132
	
	// static instances counter
	static unsigned int m_count;
133 134 135
};


136 137 138 139
/**
 * @short A thread that builds contents for PDFGenerator in the background.
 *
 */
140
class PDFPixmapGeneratorThread : public QThread
141 142
{
    public:
143 144 145 146 147 148 149
        PDFPixmapGeneratorThread( PDFGenerator * generator );
        ~PDFPixmapGeneratorThread();

        // set the request to the thread (it will be reparented)
        void startGeneration( PixmapRequest * request );
        // end generation
        void endGeneration();
150

151 152 153
        // methods for getting contents from the GUI thread
        QImage * takeImage() const;
        TextPage * takeTextPage() const;
154
        QValueList< ObjectRect * > takeObjectRects() const;
155

156 157
    private:
        // can't be called from the outside (but from startGeneration)
158
        void run();
159

160
        class PPGThreadPrivate * d;
161
};
162 163

#endif