~ubuntu-branches/ubuntu/natty/luatex/natty

« back to all changes in this revision

Viewing changes to source/libs/poppler/poppler-0.12.4/qt4/src/poppler-private.cc

  • Committer: Package Import Robot
  • Author(s): Norbert Preining
  • Date: 2010-12-13 23:22:59 UTC
  • mfrom: (0.2.1) (1.5.4) (4.3.12 experimental)
  • Revision ID: package-import@ubuntu.com-20101213232259-nqq2mq5z5x6qldw3
Tags: 0.65.0-1
* new upstream release
* ship two source packages as they are distributed by upstream, only
  renamed to match source package requirements. Fix debian/rules
  to install the manual pdf from the right place

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* poppler-private.cc: qt interface to poppler
 
2
 * Copyright (C) 2005, Net Integration Technologies, Inc.
 
3
 * Copyright (C) 2006 by Albert Astals Cid <aacid@kde.org>
 
4
 * Copyright (C) 2008 by Pino Toscano <pino@kde.org>
 
5
 * Inspired on code by
 
6
 * Copyright (C) 2004 by Albert Astals Cid <tsdgeos@terra.es>
 
7
 * Copyright (C) 2004 by Enrico Ros <eros.kde@email.it>
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2, or (at your option)
 
12
 * any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 
22
 */
 
23
 
 
24
#include "poppler-private.h"
 
25
 
 
26
#include <QtCore/QByteArray>
 
27
#include <QtCore/QDebug>
 
28
#include <QtCore/QVariant>
 
29
 
 
30
#include <Link.h>
 
31
#include <Outline.h>
 
32
 
 
33
namespace Poppler {
 
34
 
 
35
    void qt4ErrorFunction(int pos, char *msg, va_list args)
 
36
    {
 
37
        QString emsg;
 
38
        char buffer[1024]; // should be big enough
 
39
 
 
40
        if (pos >= 0)
 
41
        {
 
42
            emsg = QString::fromLatin1("Error (%1): ").arg(pos);
 
43
        }
 
44
        else
 
45
        {
 
46
            emsg = QString::fromLatin1("Error: ");
 
47
        }
 
48
        qvsnprintf(buffer, sizeof(buffer) - 1, msg, args);
 
49
        emsg += QString::fromAscii(buffer);
 
50
        qDebug() << qPrintable(emsg);
 
51
    }
 
52
 
 
53
    QString unicodeToQString(Unicode* u, int len) {
 
54
        QString ret;
 
55
        ret.resize(len);
 
56
        QChar* qch = (QChar*) ret.unicode();
 
57
        for (;len;--len)
 
58
          *qch++ = (QChar) *u++;
 
59
        return ret;
 
60
    }
 
61
 
 
62
    QString UnicodeParsedString(GooString *s1) {
 
63
        if ( !s1 || s1->getLength() == 0 )
 
64
            return QString();
 
65
 
 
66
        GBool isUnicode;
 
67
        int i;
 
68
        Unicode u;
 
69
        QString result;
 
70
        if ( ( s1->getChar(0) & 0xff ) == 0xfe && ( s1->getLength() > 1 && ( s1->getChar(1) & 0xff ) == 0xff ) )
 
71
        {
 
72
            isUnicode = gTrue;
 
73
            i = 2;
 
74
        }
 
75
        else
 
76
        {
 
77
            isUnicode = gFalse;
 
78
            i = 0;
 
79
        }
 
80
        while ( i < s1->getLength() )
 
81
        {
 
82
            if ( isUnicode )
 
83
            {
 
84
                u = ( ( s1->getChar(i) & 0xff ) << 8 ) | ( s1->getChar(i+1) & 0xff );
 
85
                i += 2;
 
86
            }
 
87
            else
 
88
            {
 
89
                u = s1->getChar(i) & 0xff;
 
90
                ++i;
 
91
            }
 
92
            result += unicodeToQString( &u, 1 );
 
93
        }
 
94
        return result;
 
95
    }
 
96
 
 
97
    GooString *QStringToUnicodeGooString(const QString &s) {
 
98
        int len = s.length() * 2 + 2;
 
99
        char *cstring = (char *)gmallocn(len, sizeof(char));
 
100
        cstring[0] = 0xfe;
 
101
        cstring[1] = 0xff;
 
102
        for (int i = 0; i < s.length(); ++i)
 
103
        {
 
104
            cstring[2+i*2] = s.at(i).row();
 
105
            cstring[3+i*2] = s.at(i).cell();
 
106
        }
 
107
        GooString *ret = new GooString(cstring, len);
 
108
        gfree(cstring);
 
109
        return ret;
 
110
    }
 
111
 
 
112
    GooString *QStringToGooString(const QString &s) {
 
113
        int len = s.length();
 
114
        char *cstring = (char *)gmallocn(s.length(), sizeof(char));
 
115
        for (int i = 0; i < len; ++i)
 
116
            cstring[i] = s.at(i).unicode();
 
117
        GooString *ret = new GooString(cstring, len);
 
118
        gfree(cstring);
 
119
        return ret;
 
120
    }
 
121
 
 
122
    void linkActionToTocItem( ::LinkAction * a, DocumentData * doc, QDomElement * e )
 
123
    {
 
124
        if ( !a || !e )
 
125
            return;
 
126
 
 
127
        switch ( a->getKind() )
 
128
        {
 
129
            case actionGoTo:
 
130
            {
 
131
                // page number is contained/referenced in a LinkGoTo
 
132
                LinkGoTo * g = static_cast< LinkGoTo * >( a );
 
133
                LinkDest * destination = g->getDest();
 
134
                if ( !destination && g->getNamedDest() )
 
135
                {
 
136
                    // no 'destination' but an internal 'named reference'. we could
 
137
                    // get the destination for the page now, but it's VERY time consuming,
 
138
                    // so better storing the reference and provide the viewport on demand
 
139
                    GooString *s = g->getNamedDest();
 
140
                    QChar *charArray = new QChar[s->getLength()];
 
141
                    for (int i = 0; i < s->getLength(); ++i) charArray[i] = QChar(s->getCString()[i]);
 
142
                    QString aux(charArray, s->getLength());
 
143
                    e->setAttribute( "DestinationName", aux );
 
144
                    delete[] charArray;
 
145
                }
 
146
                else if ( destination && destination->isOk() )
 
147
                {
 
148
                    LinkDestinationData ldd(destination, NULL, doc, false);
 
149
                    e->setAttribute( "Destination", LinkDestination(ldd).toString() );
 
150
                }
 
151
                break;
 
152
            }
 
153
            case actionGoToR:
 
154
            {
 
155
                // page number is contained/referenced in a LinkGoToR
 
156
                LinkGoToR * g = static_cast< LinkGoToR * >( a );
 
157
                LinkDest * destination = g->getDest();
 
158
                if ( !destination && g->getNamedDest() )
 
159
                {
 
160
                    // no 'destination' but an internal 'named reference'. we could
 
161
                    // get the destination for the page now, but it's VERY time consuming,
 
162
                    // so better storing the reference and provide the viewport on demand
 
163
                    GooString *s = g->getNamedDest();
 
164
                    QChar *charArray = new QChar[s->getLength()];
 
165
                    for (int i = 0; i < s->getLength(); ++i) charArray[i] = QChar(s->getCString()[i]);
 
166
                    QString aux(charArray, s->getLength());
 
167
                    e->setAttribute( "DestinationName", aux );
 
168
                    delete[] charArray;
 
169
                }
 
170
                else if ( destination && destination->isOk() )
 
171
                {
 
172
                    LinkDestinationData ldd(destination, NULL, doc, g->getFileName() != 0);
 
173
                    e->setAttribute( "Destination", LinkDestination(ldd).toString() );
 
174
                }
 
175
                e->setAttribute( "ExternalFileName", g->getFileName()->getCString() );
 
176
                break;
 
177
            }
 
178
            case actionURI:
 
179
            {
 
180
                LinkURI * u = static_cast< LinkURI * >( a );
 
181
                e->setAttribute( "DestinationURI", u->getURI()->getCString() );
 
182
            }
 
183
            default: ;
 
184
        }
 
185
    }
 
186
 
 
187
    void DocumentData::addTocChildren( QDomDocument * docSyn, QDomNode * parent, GooList * items )
 
188
    {
 
189
        int numItems = items->getLength();
 
190
        for ( int i = 0; i < numItems; ++i )
 
191
        {
 
192
            // iterate over every object in 'items'
 
193
            OutlineItem * outlineItem = (OutlineItem *)items->get( i );
 
194
 
 
195
            // 1. create element using outlineItem's title as tagName
 
196
            QString name;
 
197
            Unicode * uniChar = outlineItem->getTitle();
 
198
            int titleLength = outlineItem->getTitleLength();
 
199
            name = unicodeToQString(uniChar, titleLength);
 
200
            if ( name.isEmpty() )
 
201
                continue;
 
202
 
 
203
            QDomElement item = docSyn->createElement( name );
 
204
            parent->appendChild( item );
 
205
 
 
206
            // 2. find the page the link refers to
 
207
            ::LinkAction * a = outlineItem->getAction();
 
208
            linkActionToTocItem( a, this, &item );
 
209
 
 
210
            item.setAttribute( "Open", QVariant( (bool)outlineItem->isOpen() ).toString() );
 
211
 
 
212
            // 3. recursively descend over children
 
213
            outlineItem->open();
 
214
            GooList * children = outlineItem->getKids();
 
215
            if ( children )
 
216
                addTocChildren( docSyn, &item, children );
 
217
        }
 
218
    }
 
219
 
 
220
}