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>
6
* Copyright (C) 2004 by Albert Astals Cid <tsdgeos@terra.es>
7
* Copyright (C) 2004 by Enrico Ros <eros.kde@email.it>
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)
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.
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.
24
#include "poppler-private.h"
26
#include <QtCore/QByteArray>
27
#include <QtCore/QDebug>
28
#include <QtCore/QVariant>
35
void qt4ErrorFunction(int pos, char *msg, va_list args)
38
char buffer[1024]; // should be big enough
42
emsg = QString::fromLatin1("Error (%1): ").arg(pos);
46
emsg = QString::fromLatin1("Error: ");
48
qvsnprintf(buffer, sizeof(buffer) - 1, msg, args);
49
emsg += QString::fromAscii(buffer);
50
qDebug() << qPrintable(emsg);
53
QString unicodeToQString(Unicode* u, int len) {
56
QChar* qch = (QChar*) ret.unicode();
58
*qch++ = (QChar) *u++;
62
QString UnicodeParsedString(GooString *s1) {
63
if ( !s1 || s1->getLength() == 0 )
70
if ( ( s1->getChar(0) & 0xff ) == 0xfe && ( s1->getLength() > 1 && ( s1->getChar(1) & 0xff ) == 0xff ) )
80
while ( i < s1->getLength() )
84
u = ( ( s1->getChar(i) & 0xff ) << 8 ) | ( s1->getChar(i+1) & 0xff );
89
u = s1->getChar(i) & 0xff;
92
result += unicodeToQString( &u, 1 );
97
GooString *QStringToUnicodeGooString(const QString &s) {
98
int len = s.length() * 2 + 2;
99
char *cstring = (char *)gmallocn(len, sizeof(char));
102
for (int i = 0; i < s.length(); ++i)
104
cstring[2+i*2] = s.at(i).row();
105
cstring[3+i*2] = s.at(i).cell();
107
GooString *ret = new GooString(cstring, len);
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);
122
void linkActionToTocItem( ::LinkAction * a, DocumentData * doc, QDomElement * e )
127
switch ( a->getKind() )
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() )
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 );
146
else if ( destination && destination->isOk() )
148
LinkDestinationData ldd(destination, NULL, doc, false);
149
e->setAttribute( "Destination", LinkDestination(ldd).toString() );
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() )
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 );
170
else if ( destination && destination->isOk() )
172
LinkDestinationData ldd(destination, NULL, doc, g->getFileName() != 0);
173
e->setAttribute( "Destination", LinkDestination(ldd).toString() );
175
e->setAttribute( "ExternalFileName", g->getFileName()->getCString() );
180
LinkURI * u = static_cast< LinkURI * >( a );
181
e->setAttribute( "DestinationURI", u->getURI()->getCString() );
187
void DocumentData::addTocChildren( QDomDocument * docSyn, QDomNode * parent, GooList * items )
189
int numItems = items->getLength();
190
for ( int i = 0; i < numItems; ++i )
192
// iterate over every object in 'items'
193
OutlineItem * outlineItem = (OutlineItem *)items->get( i );
195
// 1. create element using outlineItem's title as tagName
197
Unicode * uniChar = outlineItem->getTitle();
198
int titleLength = outlineItem->getTitleLength();
199
name = unicodeToQString(uniChar, titleLength);
200
if ( name.isEmpty() )
203
QDomElement item = docSyn->createElement( name );
204
parent->appendChild( item );
206
// 2. find the page the link refers to
207
::LinkAction * a = outlineItem->getAction();
208
linkActionToTocItem( a, this, &item );
210
item.setAttribute( "Open", QVariant( (bool)outlineItem->isOpen() ).toString() );
212
// 3. recursively descend over children
214
GooList * children = outlineItem->getKids();
216
addTocChildren( docSyn, &item, children );