summaryrefslogtreecommitdiffstats
path: root/kpdf/core/generator_pdf/gp_outputdev.cpp
blob: df3fd85b1ea9cfa792640bc79db843ffd33b6c84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/***************************************************************************
 *   Copyright (C) 2003-2004 by Christophe Devriese                        *
 *                         <Christophe.Devriese@student.kuleuven.ac.be>    *
 *   Copyright (C) 2003 by Andy Goossens <andygoossens@telenet.be>         *
 *   Copyright (C) 2003 by Scott Wheeler <wheeler@kde.org>                 *
 *   Copyright (C) 2003 by Ingo Klöcker <kloecker@kde.org>                 *
 *   Copyright (C) 2003 by Will Andrews <will@csociety.org>                *
 *   Copyright (C) 2004 by Dominique Devriese <devriese@kde.org>           *
 *   Copyright (C) 2004 by Waldo Bastian <bastian@kde.org>                 *
 *   Copyright (C) 2004 by Albert Astals Cid <tsdgeos@terra.es>            *
 *                                                                         *
 *   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 <tqpixmap.h>
#include <tqimage.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;
}

TQPixmap * KPDFOutputDev::takePixmap()
{
    TQPixmap * pix = m_pixmap;
    m_pixmap = 0;
    return pix;
}

TQImage * KPDFOutputDev::takeImage()
{
    TQImage * img = m_image;
    m_image = 0;
    return img;
}

TQValueList< ObjectRect * > KPDFOutputDev::takeObjectRects()
{
    if ( m_rects.isEmpty() )
        return m_rects;
    TQValueList< 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
    TQImage * img = new TQImage( dataPtr, bw, bh, 32, 0, 0, TQImage::IgnoreEndian );*/
    TQImage * img = new TQImage( 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 TQImage or convert it immediately to TQPixmap 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 TQImage( img->smoothScale( m_pixmapWidth, m_pixmapHeight ) );
        else
            // dereference image from the xpdf memory
            m_image = new TQImage( 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 TQPixmap( img->smoothScale( m_pixmapWidth, m_pixmapHeight ) );
        else
            m_pixmap = new TQPixmap( *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() )
    {
        TQValueList< 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( TQString::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( (TQString)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