summaryrefslogtreecommitdiff
path: root/qt/poppler-page.cc
blob: 6e292a3fac47acbeaf90e673c7be28a2bb9f3579 (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
/* poppler-page.cc: qt interface to poppler
 * Copyright (C) 2005, Net Integration Technologies, Inc.
 * Copyright (C) 2005, Tobias Koening
 *
 * 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, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <poppler-qt.h>
#include <qfile.h>
#include <qimage.h>
#include <GlobalParams.h>
#include <PDFDoc.h>
#include <Catalog.h>
#include <ErrorCodes.h>
#include <SplashOutputDev.h>
#include <TextOutputDev.h>
#include <splash/SplashBitmap.h>
#include "poppler-private.h"

namespace Poppler {

class PageTransitionData {
  public:
  Object *trans;
};

//------------------------------------------------------------------------
// PageTransition
//------------------------------------------------------------------------

PageTransition::PageTransition(const PageTransitionData &data)
  : type(Replace),
    duration(1),
    alignment(Horizontal),
    direction(Inward),
    angle(0),
    scale(1.0),
    rectangular(false)
{
  Object obj;
  Object *dictObj = data.trans;

  if (dictObj->isDict()) {
    Dict *transDict = dictObj->getDict();

    if (transDict->lookup("S", &obj)->isName()) {
      const char *s = obj.getName();
      if (strcmp("R", s) == 0)
        type = Replace;
      else if (strcmp("Split", s) == 0)
        type = Split;
      else if (strcmp("Blinds", s) == 0)
        type = Blinds;
      else if (strcmp("Box", s) == 0)
        type = Box;
      else if (strcmp("Wipe", s) == 0)
        type = Wipe;
      else if (strcmp("Dissolve", s) == 0)
        type = Dissolve;
      else if (strcmp("Glitter", s) == 0)
        type = Glitter;
      else if (strcmp("Fly", s) == 0)
        type = Fly;
      else if (strcmp("Push", s) == 0)
        type = Push;
      else if (strcmp("Cover", s) == 0)
        type = Cover;
      else if (strcmp("Uncover", s) == 0)
        type = Push;
      else if (strcmp("Fade", s) == 0)
        type = Cover;
    }
    obj.free();

    if (transDict->lookup("D", &obj)->isInt()) {
      duration = obj.getInt();
    }
    obj.free();

    if (transDict->lookup("Dm", &obj)->isName()) {
      const char *dm = obj.getName();
      if ( strcmp( "H", dm ) == 0 )
        alignment = Horizontal;
      else if ( strcmp( "V", dm ) == 0 )
        alignment = Vertical;
    }
    obj.free();

    if (transDict->lookup("M", &obj)->isName()) {
      const char *m = obj.getName();
      if ( strcmp( "I", m ) == 0 )
        direction = Inward;
      else if ( strcmp( "O", m ) == 0 )
        direction = Outward;
    }
    obj.free();

    if (transDict->lookup("Di", &obj)->isInt()) {
      angle = obj.getInt();
    }
    obj.free();

    if (transDict->lookup("Di", &obj)->isName()) {
      if ( strcmp( "None", obj.getName() ) == 0 )
        angle = 0;
    }
    obj.free();

    if (transDict->lookup("SS", &obj)->isReal()) {
      scale = obj.getReal();
    }
    obj.free();

    if (transDict->lookup("B", &obj)->isBool()) {
      rectangular = obj.getBool();
    }
    obj.free();
  }
}

PageTransition::~PageTransition()
{
}


class PageData {
  public:
  const Document *doc;
  int index;
  PageTransition *transition;
};

Page::Page(const Document *doc, int index) {
  data = new PageData();
  data->index = index;
  data->doc = doc;
  data->transition = 0;
}

Page::~Page()
{
  delete data->transition;
  delete data;
}

void Page::renderToPixmap(QPixmap **q, int x, int y, int w, int h) const
{
  renderToPixmap(q, x, y, w, h, 72.0, 72.0);
}

void Page::renderToPixmap(QPixmap **q, int x, int y, int w, int h, double xres, double yres) const
{
  SplashOutputDev *output_dev;
  SplashBitmap *bitmap;
  SplashColor white;
  white[0] = 255;
  white[1] = 255;
  white[2] = 255;
  SplashColorPtr color_ptr;
  output_dev = new SplashOutputDev(splashModeRGB8, 4, gFalse, white);
  output_dev->startDoc(data->doc->data->doc.getXRef ());
  
  data->doc->data->doc.displayPageSlice(output_dev, data->index + 1, xres, yres,
      0, false, false, false, -1, -1, -1, -1);
  bitmap = output_dev->getBitmap ();
  color_ptr = bitmap->getDataPtr ();
  int bw = output_dev->getBitmap()->getWidth();
  int bh = output_dev->getBitmap()->getHeight();
  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++)
    {
      output_dev->getBitmap()->getPixel(i, j, pixel);
      img->setPixel( i, j, qRgb( pixel[0], pixel[1], pixel[2] ) );
    }
  }
  delete[] pixel;
  *q = new QPixmap( *img );
  
  delete img;
  delete output_dev;
}

QString Page::getText(const Rectangle &r) const
{
  TextOutputDev *output_dev;
  GooString *s;
  PDFRectangle *rect;
  QString result;
  ::Page *p;
  
  output_dev = new TextOutputDev(0, gFalse, gFalse, gFalse);
  data->doc->data->doc.displayPageSlice(output_dev, data->index + 1, 72, 72,
      0, false, false, false, -1, -1, -1, -1);
  p = data->doc->data->doc.getCatalog()->getPage(data->index + 1);
  if (r.isNull())
  {
    rect = p->getCropBox();
    s = output_dev->getText(rect->x1, rect->y1, rect->x2, rect->y2);
  }
  else
  {
    double height, y1, y2;
    height = p->getCropHeight();
    y1 = height - r.m_y2;
    y2 = height - r.m_y1;
    s = output_dev->getText(r.m_x1, y1, r.m_x2, y2);
  }

  result = QString::fromUtf8(s->getCString());

  delete output_dev;
  delete s;
  return result;
}

QValueList<TextBox*> Page::textList() const
{
  TextOutputDev *output_dev;
  
  QValueList<TextBox*> output_list;
  
  output_dev = new TextOutputDev(0, gFalse, gFalse, gFalse);

  data->doc->data->doc.displayPageSlice(output_dev, data->index + 1, 72, 72,
      0, false, false, false, -1, -1, -1, -1);

  TextWordList *word_list = output_dev->makeWordList();
  
  if (!word_list) {
    delete output_dev;
    return output_list;
  }
  
  for (int i = 0; i < word_list->getLength(); i++) {
    TextWord *word = word_list->get(i);
    QString string = QString::fromUtf8(word->getText()->getCString());
    double xMin, yMin, xMax, yMax;
    word->getBBox(&xMin, &yMin, &xMax, &yMax);
    
    TextBox* text_box = new TextBox(string, Rectangle(xMin, yMin, xMax, yMax));
    
    output_list.append(text_box);
  }
  
  delete word_list;
  delete output_dev;
  
  return output_list;
}

PageTransition *Page::getTransition() const
{
  if (!data->transition) 
  {
    Object o;
    PageTransitionData ptd;
    ptd.trans = data->doc->data->doc.getCatalog()->getPage(data->index + 1)->getTrans(&o);
    data->transition = new PageTransition(ptd);
    o.free();
  }
  return data->transition;
}

QSize Page::pageSize() const
{
  ::Page *p = data->doc->data->doc.getCatalog()->getPage(data->index + 1);
  return QSize( (int)p->getMediaWidth(), (int)p->getMediaHeight() );
}

Page::Orientation Page::orientation() const
{
  ::Page *p = data->doc->data->doc.getCatalog()->getPage(data->index + 1);

  int rotation = p->getRotate();
  switch (rotation) {
  case 90:
    return Page::Landscape;
    break;
  case 180:
    return Page::UpsideDown;
    break;
  case 270:
    return Page::Seascape;
    break;
  default:
    return Page::Portrait;
  }
}


}