poppler-page.cc 23.5 KB
Newer Older
1
/* poppler.h: glib wrapper for poppler
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * Copyright (C) 2005, Red Hat, Inc.
 *
 * 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.
 */

19
#include "config.h"
20
21
#include <math.h>

22
23
24
25
26
27
28
29
30
31
32
33
#include <goo/GooList.h>
#include <splash/SplashBitmap.h>
#include <GlobalParams.h>
#include <PDFDoc.h>
#include <Outline.h>
#include <ErrorCodes.h>
#include <UnicodeMap.h>
#include <GfxState.h>

#include "poppler.h"
#include "poppler-private.h"

34
35
36
37
enum
{
  PROP_0,
  PROP_LABEL
38
39
};

40
41
42
43
44
45
46
47
typedef struct _PopplerPageClass PopplerPageClass;
struct _PopplerPageClass
{
  GObjectClass parent_class;
};

G_DEFINE_TYPE (PopplerPage, poppler_page, G_TYPE_OBJECT);

48
49
50
51
52
53
54
PopplerPage *
_poppler_page_new (PopplerDocument *document, Page *page, int index)
{
  PopplerPage *poppler_page;

  g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL);

55
  poppler_page = (PopplerPage *) g_object_new (POPPLER_TYPE_PAGE, NULL, NULL);
56
57
58
59
60
61
62
63
64
65
  poppler_page->document = document;
  poppler_page->page = page;
  poppler_page->index = index;

  return poppler_page;
}

static void
poppler_page_finalize (GObject *object)
{
66
67
68
69
70
71
72
  PopplerPage *page = POPPLER_PAGE (object);

  if (page->gfx != NULL)
    delete page->gfx;
  if (page->text_dev != NULL)
    delete page->text_dev;

73
74
75
  /* page->page is owned by the document */
}

76
void
77
78
79
poppler_page_get_size (PopplerPage *page,
		       double      *width,
		       double      *height)
80
{
81
  double page_width, page_height;
82
  int rotate;
83

84
85
  g_return_if_fail (POPPLER_IS_PAGE (page));

86
87
  rotate = page->page->getRotate ();
  if (rotate == 90 || rotate == 270) {
88
89
    page_height = page->page->getCropWidth ();
    page_width = page->page->getCropHeight ();
90
  } else {
91
92
    page_width = page->page->getCropWidth ();
    page_height = page->page->getCropHeight ();
93
94
  }

95
  if (width != NULL)
96
    *width = page_width;
97
  if (height != NULL)
98
    *height = page_height;
99
100
}

101
102
103
int
poppler_page_get_index (PopplerPage *page)
{
104
105
  g_return_val_if_fail (POPPLER_IS_PAGE (page), 0);

106
107
108
  return page->index;
}

109
110
#if defined (HAVE_CAIRO)

111
112
113
114
115
typedef struct {
  unsigned char *cairo_data;
  cairo_surface_t *surface;
} OutputDevData;

116
static void
117
118
poppler_page_prepare_output_dev (PopplerPage *page,
				 double scale,
119
				 int rotation,
120
121
				 gboolean transparent,
				 OutputDevData *output_dev_data)
122
{
123
124
  CairoOutputDev *output_dev;
  cairo_surface_t *surface;
125
  int cairo_width, cairo_height, cairo_rowstride;
126
  int rotate;
127
  unsigned char *cairo_data;
128

129
130
131
  rotate = (rotation + page->page->getRotate()) % 360;

  if (rotate == 90 || rotate == 270) {
132
133
    cairo_width = MAX ((int)(page->page->getCropHeight() * scale + 0.5), 1);
    cairo_height = MAX ((int)(page->page->getCropWidth() * scale + 0.5), 1);
134
  } else {
135
136
    cairo_width = MAX ((int)(page->page->getCropWidth() * scale + 0.5), 1);
    cairo_height = MAX ((int)(page->page->getCropHeight() * scale + 0.5), 1);
137
  }
138

139
  output_dev = page->document->output_dev;
140
141
  cairo_rowstride = cairo_width * 4;
  cairo_data = (guchar *) gmalloc (cairo_height * cairo_rowstride);
142
143
144
145
146
  if (transparent)
      memset (cairo_data, 0x00, cairo_height * cairo_rowstride);
  else
      memset (cairo_data, 0xff, cairo_height * cairo_rowstride);

147
148
149
150
  surface = cairo_image_surface_create_for_data(cairo_data,
						CAIRO_FORMAT_ARGB32,
	  					cairo_width, cairo_height, 
						cairo_rowstride);
151
152
153

  output_dev_data->cairo_data = cairo_data;
  output_dev_data->surface = surface;
154
  output_dev->setSurface (surface);
155
}
156

157
static void
158
159
160
161
162
163
164
165
166
167
168
169
170
171
poppler_page_copy_to_pixbuf (PopplerPage *page,
			     GdkPixbuf *pixbuf,
			     OutputDevData *output_dev_data)
{
  int cairo_width, cairo_height, cairo_rowstride;
  unsigned char *pixbuf_data, *dst, *cairo_data;
  int pixbuf_rowstride, pixbuf_n_channels;
  unsigned int *src;
  int x, y;

  cairo_width = cairo_image_surface_get_width (output_dev_data->surface);
  cairo_height = cairo_image_surface_get_height (output_dev_data->surface);
  cairo_rowstride = cairo_width * 4;
  cairo_data = output_dev_data->cairo_data;
172
173
174
175
176

  pixbuf_data = gdk_pixbuf_get_pixels (pixbuf);
  pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);

177
178
179
180
181
  if (cairo_width > gdk_pixbuf_get_width (pixbuf))
    cairo_width = gdk_pixbuf_get_width (pixbuf);
  if (cairo_height > gdk_pixbuf_get_height (pixbuf))
    cairo_height = gdk_pixbuf_get_height (pixbuf);

182
183
184
  for (y = 0; y < cairo_height; y++)
    {
      src = (unsigned int *) (cairo_data + y * cairo_rowstride);
185
      dst = pixbuf_data + y * pixbuf_rowstride;
186
187
188
189
190
      for (x = 0; x < cairo_width; x++) 
	{
	  dst[0] = (*src >> 16) & 0xff;
	  dst[1] = (*src >> 8) & 0xff; 
	  dst[2] = (*src >> 0) & 0xff;
191
192
	  if (pixbuf_n_channels == 4)
	      dst[3] = (*src >> 24) & 0xff;
193
194
195
196
	  dst += pixbuf_n_channels;
	  src++;
	}
    }
197

198
199
200
  page->document->output_dev->setSurface (NULL);
  cairo_surface_destroy (output_dev_data->surface);
  gfree (output_dev_data->cairo_data);
201
202
203
}

#elif defined (HAVE_SPLASH)
204
 
205
206
207
typedef struct {
} OutputDevData;

208
static void
209
210
poppler_page_prepare_output_dev (PopplerPage *page,
				 double scale,
211
				 int rotation,
212
213
214
215
216
217
				 gboolean transparent,
				 OutputDevData *output_dev_data)
{
  /* pft */
}

218
static void
219
220
221
poppler_page_copy_to_pixbuf(PopplerPage *page,
			    GdkPixbuf *pixbuf,
			    OutputDevData *data)
222
223
224
225
226
{
  SplashOutputDev *output_dev;
  SplashBitmap *bitmap;
  SplashColorPtr color_ptr;
  int splash_width, splash_height, splash_rowstride;
227
  int pixbuf_rowstride, pixbuf_n_channels;
228
  guchar *pixbuf_data, *dst;
229
230
  int x, y;

231
  output_dev = page->document->output_dev;
232
233
234
235
236
237
238
239
240
241
242
243

  bitmap = output_dev->getBitmap ();
  color_ptr = bitmap->getDataPtr ();

  splash_width = bitmap->getWidth ();
  splash_height = bitmap->getHeight ();
  splash_rowstride = bitmap->getRowSize ();

  pixbuf_data = gdk_pixbuf_get_pixels (pixbuf);
  pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);

244
245
246
247
  if (splash_width > gdk_pixbuf_get_width (pixbuf))
    splash_width = gdk_pixbuf_get_width (pixbuf);
  if (splash_height > gdk_pixbuf_get_height (pixbuf))
    splash_height = gdk_pixbuf_get_height (pixbuf);
248
249
250
251
252
253

  for (y = 0; y < splash_height; y++)
    {
      SplashRGB8 *src;

      src = (SplashRGB8 *) (color_ptr.rgb8p + y * splash_rowstride);
254
      dst = pixbuf_data + y * pixbuf_rowstride;
255
256
257
258
259
      for (x = 0; x < splash_width; x++) 
	{
	  dst[0] = splashRGB8R(*src);
	  dst[1] = splashRGB8G(*src); 
	  dst[2] = splashRGB8B(*src);
260
	  if (pixbuf_n_channels == 4)
261
	    dst[3] = 0xff;
262
263
264
	  dst += pixbuf_n_channels;
	  src++;
	}
265
266
267
    }
}

268
269
270
271
272
273
274
275
276
#endif

/**
 * poppler_page_render_to_pixbuf:
 * @page: the page to render from
 * @src_x: x coordinate of upper left corner
 * @src_y: y coordinate of upper left corner
 * @src_width: width of rectangle to render
 * @src_height: height of rectangle to render
277
 * @scale: scale specified as pixels per point
278
 * @rotation: rotate the document by the specified degree
279
280
281
282
 * @pixbuf: pixbuf to render into
 *
 * First scale the document to match the specified pixels per point,
 * then render the rectangle given by the upper left corner at
283
 * (src_x, src_y) and src_width and src_height.
284
285
286
287
288
289
 **/
void
poppler_page_render_to_pixbuf (PopplerPage *page,
			       int src_x, int src_y,
			       int src_width, int src_height,
			       double scale,
290
			       int rotation,
291
			       GdkPixbuf *pixbuf)
292
{
293
  OutputDevData data;
294
295
296
297
298

  g_return_if_fail (POPPLER_IS_PAGE (page));
  g_return_if_fail (scale > 0.0);
  g_return_if_fail (pixbuf != NULL);

299
  poppler_page_prepare_output_dev (page, scale, rotation, FALSE, &data);
300
301
302

  page->page->displaySlice(page->document->output_dev,
			   72.0 * scale, 72.0 * scale,
303
			   rotation,
304
			   gFalse, /* useMediaBox */
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
			   gTrue, /* Crop */
			   src_x, src_y,
			   src_width, src_height,
			   NULL, /* links */
			   page->document->doc->getCatalog ());

  poppler_page_copy_to_pixbuf (page, pixbuf, &data);
}

static TextOutputDev *
poppler_page_get_text_output_dev (PopplerPage *page)
{
  if (page->text_dev == NULL) {
    page->text_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);

    page->gfx = page->page->createGfx(page->text_dev,
321
				      72.0, 72.0, 0,
322
				      gFalse, /* useMediaBox */
323
324
325
326
327
328
329
330
331
332
333
334
				      gTrue, /* Crop */
				      -1, -1, -1, -1,
				      NULL, /* links */
				      page->document->doc->getCatalog (),
				      NULL, NULL, NULL, NULL);

    page->page->display(page->gfx);

    page->text_dev->endPage();
  }

  return page->text_dev;
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
GdkRegion *
poppler_page_get_selection_region (PopplerPage      *page,
				   gdouble           scale,
				   PopplerRectangle *selection)
{
  TextOutputDev *text_dev;
  PDFRectangle poppler_selection;
  GooList *list;
  GdkRectangle rect;
  GdkRegion *region;
  int i;

  poppler_selection.x1 = selection->x1;
  poppler_selection.y1 = selection->y1;
  poppler_selection.x2 = selection->x2;
  poppler_selection.y2 = selection->y2;

  text_dev = poppler_page_get_text_output_dev (page);
  list = text_dev->getSelectionRegion(&poppler_selection, scale);

  region = gdk_region_new();

  for (i = 0; i < list->getLength(); i++) {
    PDFRectangle *selection_rect = (PDFRectangle *) list->get(i);
    rect.x      = (gint) selection_rect->x1;
    rect.y      = (gint) selection_rect->y1;
    rect.width  = (gint) (selection_rect->x2 - selection_rect->x1);
    rect.height = (gint) (selection_rect->y2 - selection_rect->y1);
    gdk_region_union_with_rect (region, &rect);
    delete selection_rect;
  }

  delete list;

  return region;
}

374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#if defined (HAVE_CAIRO)

static void
poppler_page_set_selection_alpha (PopplerPage      *page,
				  double            scale,
				  GdkPixbuf        *pixbuf,
				  PopplerRectangle *selection)
{
  /* Cairo doesn't need this, since cairo generates an alpha channel. */ 
}

#elif defined (HAVE_SPLASH)

static void
poppler_page_set_selection_alpha (PopplerPage      *page,
				  double            scale,
				  GdkPixbuf        *pixbuf,
				  PopplerRectangle *selection)
{
  GdkRegion *region;
  gint n_rectangles, i, x, y;
  GdkRectangle *rectangles;
  int pixbuf_rowstride, pixbuf_n_channels;
  guchar *pixbuf_data, *dst;

  pixbuf_data = gdk_pixbuf_get_pixels (pixbuf);
  pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);

  if (pixbuf_n_channels != 4)
    return;

  region = poppler_page_get_selection_region (page, scale, selection);

  gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
  for (i = 0; i < n_rectangles; i++) {
    for (y = 0; y < rectangles[i].height; y++) {
      dst = pixbuf_data + (rectangles[i].y + y) * pixbuf_rowstride +
	rectangles[i].x * pixbuf_n_channels;
      for (x = 0; x < rectangles[i].width; x++) {
	  dst[3] = 0xff;
	  dst += pixbuf_n_channels;
      }
    }
  }

  g_free (rectangles);

  gdk_region_destroy (region);
}

#endif

427
428
429
/**
 * poppler_page_render_selection:
 * @page: the #PopplerPage for which to render selection
430
431
 * @scale: scale specified as pixels per point
 * @rotation: rotate the document by the specified degree
432
433
434
435
436
437
438
439
440
 * @pixbuf: pixbuf to render to
 * @selection: start and end point of selection as a rectangle
 * @old_selection: previous selection
 * @glyph_color: color to use for drawing glyphs
 * @background_color: color to use for the selection background
 * 
 * Render the selection specified by @selection for @page into
 * @pixbuf.  The selection will be rendered at @scale, using
 * @glyph_color for the glyphs and @background_color for the selection
441
 * background.
442
443
444
445
446
 *
 * If non-NULL, @old_selection specifies the selection that is already
 * rendered in @pixbuf, in which case this function will (some day)
 * only render the changed part of the selection.
 **/
447
void
448
449
poppler_page_render_selection (PopplerPage      *page,
			       gdouble           scale,
450
			       int		 rotation,
451
			       GdkPixbuf        *pixbuf,
452
			       PopplerRectangle *selection,
453
			       PopplerRectangle *old_selection,
454
455
			       GdkColor         *glyph_color,
			       GdkColor         *background_color)
456
457
{
  TextOutputDev *text_dev;
458
  OutputDev *output_dev;
459
460
461
462
  OutputDevData data;
  PDFRectangle pdf_selection(selection->x1, selection->y1,
			     selection->x2, selection->y2);

463
  GfxColor gfx_background_color = { 
464
465
466
    background_color->red / 65535.0,
    background_color->green / 65535.0,
    background_color->blue / 65535.0
467
468
  };
  GfxColor gfx_glyph_color = {
469
470
471
    glyph_color->red / 65535.0,
    glyph_color->green / 65535.0,
    glyph_color->blue / 65535.0
472
473
  };

474
475
476
  text_dev = poppler_page_get_text_output_dev (page);
  output_dev = page->document->output_dev;

477
  poppler_page_prepare_output_dev (page, scale, rotation, TRUE, &data);
478

479
  text_dev->drawSelection (output_dev, scale, rotation, &pdf_selection,
480
			   &gfx_glyph_color, &gfx_background_color);
481
482
483

  poppler_page_copy_to_pixbuf (page, pixbuf, &data);

484
485
  poppler_page_set_selection_alpha (page, scale, pixbuf, selection);

486
487
488
489
490
491
492
493
494
495
496
  /* We'll need a function to destroy page->text_dev and page->gfx
   * when the application wants to get rid of them.
   *
   * Two improvements: 1) make GfxFont refcounted and let TextPage and
   * friends hold a reference to the GfxFonts they need so we can free
   * up Gfx early.  2) use a TextPage directly when rendering the page
   * so we don't have to use TextOutputDev and render a second
   * time. */
}


497
static void
498
499
500
501
502
destroy_thumb_data (guchar *pixels, gpointer data)
{
  gfree (pixels);
}

503
504
505
506
507
508
509
510
511
512
513
/**
 * poppler_page_get_thumbnail:
 * @page: the #PopperPage to get the thumbnail for
 * 
 * Get the embedded thumbnail for the specified page.  If the document
 * doesn't have an embedded thumbnail for the page, this function
 * returns %NULL.
 * 
 * Return value: the tumbnail as a #GdkPixbuf or %NULL if the document
 * doesn't have a thumbnail for this page.
 **/
514
515
516
517
518
519
GdkPixbuf *
poppler_page_get_thumbnail (PopplerPage *page)
{
  unsigned char *data;
  int width, height, rowstride;

520
521
  g_return_val_if_fail (POPPLER_IS_PAGE (page), FALSE);

522
523
524
525
526
527
528
529
  if (!page->page->loadThumb (&data, &width, &height, &rowstride))
    return NULL;

  return gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB,
				   FALSE, 8, width, height, rowstride,
				   destroy_thumb_data, NULL);
}

530
531
532
533
534
/**
 * poppler_page_get_thumbnail_size:
 * @page: A #PopplerPage
 * @width: return location for width
 * @height: return location for height
535
 *
536
537
538
539
 * Returns %TRUE if @page has a thumbnail associated with it.  It also
 * fills in @width and @height with the width and height of the
 * thumbnail.  The values of width and height are not changed if no
 * appropriate thumbnail exists.
540
 *
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
 * Return value: %TRUE, if @page has a thumbnail associated with it.
 **/
gboolean
poppler_page_get_thumbnail_size (PopplerPage *page,
				 int         *width,
				 int         *height)
{
  Object thumb;
  Dict *dict;
  gboolean retval = FALSE;

  g_return_val_if_fail (POPPLER_IS_PAGE (page), FALSE);
  g_return_val_if_fail (width != NULL, FALSE);
  g_return_val_if_fail (height != NULL, FALSE);

  page->page->getThumb (&thumb);
  if (thumb.isNull ())
    {
      thumb.free ();
      return FALSE;
    }

  dict = thumb.streamGetDict();

565
566
  /* Theoretically, this could succeed and you would still fail when
   * loading the thumb */
567
568
569
570
571
572
573
574
575
  if (dict->lookupInt ("Width", "W", width)  &&
      dict->lookupInt ("Height", "H", height))
    retval = TRUE;

  thumb.free ();

  return retval;
}

576
577
578
579
580
581
582
583
584
585
586
587
/**
 * poppler_page_get_text:
 * @page: a #PopplerPage
 * @rect: the rectangle including the text
 * 
 * Retrieves the contents of the specified rectangle as text 
 * 
 * Return value: a pointer to the contents of the rectangle
 *               as a string
 **/
char *
poppler_page_get_text (PopplerPage      *page,
588
		       PopplerRectangle *selection)
589
{
590
  TextOutputDev *text_dev;
591
592
593
594
  PDFDoc *doc;
  GooString *sel_text = new GooString;
  double height, y1, y2;
  char *result;
595
  PDFRectangle pdf_selection;
596
597

  g_return_val_if_fail (POPPLER_IS_PAGE (page), FALSE);
598
  g_return_val_if_fail (selection != NULL, NULL);
599

600
  text_dev = poppler_page_get_text_output_dev (page);
601
  poppler_page_get_size (page, NULL, &height);
602

603
604
605
606
607
608
609
610
  pdf_selection.x1 = selection->x1;
  pdf_selection.y1 = height - selection->y2;
  pdf_selection.x2 = selection->x2;
  pdf_selection.y2 = height - selection->y1;
  
  sel_text = text_dev->getSelectionText (&pdf_selection);
  result = g_strdup (sel_text->getCString ());
  delete sel_text;
611

612
  return result;
613
614
}

615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/**
 * poppler_page_find_text:
 * @page: a #PopplerPage
 * @text: the text to search for (UTF-8 encoded)
 * 
 * A #GList of rectangles for each occurance of the text on the page.
 * The coordinates are in PDF points.
 * 
 * Return value: a #GList of PopplerRectangle, 
 **/
GList *
poppler_page_find_text (PopplerPage *page,
			const char  *text)
{
  PopplerRectangle *match;
  TextOutputDev *output_dev;
  PDFDoc *doc;
  GList *matches;
  double xMin, yMin, xMax, yMax;
  gunichar *ucs4;
  glong ucs4_len;
636
  double height;
637
638
639
640
641
642
643
644
645

  g_return_val_if_fail (POPPLER_IS_PAGE (page), FALSE);
  g_return_val_if_fail (text != NULL, FALSE);

  ucs4 = g_utf8_to_ucs4_fast (text, -1, &ucs4_len);

  output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
  doc = page->document->doc;

646
  poppler_page_get_size (page, NULL, &height);
647
  page->page->display (output_dev, 72, 72, 0, gFalse,
648
		       gTrue, NULL, doc->getCatalog());
649
650
  
  matches = NULL;
651
652
  xMin = 0;
  yMin = 0;
653
#warning you probably want to add caseSensitive and backwards as parameters
654
655
656
  while (output_dev->findText (ucs4, ucs4_len,
			       gFalse, gTrue, // startAtTop, stopAtBottom
			       gTrue, gFalse, // startAtLast, stopAtLast
657
			       gFalse, gFalse, // caseSensitive, backwards
658
659
660
661
			       &xMin, &yMin, &xMax, &yMax))
    {
      match = g_new (PopplerRectangle, 1);
      match->x1 = xMin;
662
      match->y1 = height - yMax;
663
      match->x2 = xMax;
664
      match->y2 = height - yMin;
665
666
667
668
669
670
      matches = g_list_prepend (matches, match);
    }

  delete output_dev;
  g_free (ucs4);

671
  return g_list_reverse (matches);
672
673
}

674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/**
 * poppler_page_render_to_ps:
 * @page: a #PopplerPage
 * @ps_file: the PopplerPSFile to render to
 * 
 * Render the page on a postscript file
 * 
 **/
void
poppler_page_render_to_ps (PopplerPage   *page,
			   PopplerPSFile *ps_file)
{
  g_return_if_fail (POPPLER_IS_PAGE (page));
  g_return_if_fail (ps_file != NULL);

689
690
691
692
  if (!ps_file->out)
    ps_file->out = new PSOutputDev (ps_file->filename,
                                    ps_file->document->doc->getXRef(),
                                    ps_file->document->doc->getCatalog(),
693
                                    ps_file->first_page, ps_file->last_page,
694
695
696
697
698
                                    psModePS, (int)ps_file->paper_width,
                                    (int)ps_file->paper_height, ps_file->duplex,
                                    0, 0, 0, 0, gFalse);


699
  ps_file->document->doc->displayPage (ps_file->out, page->index + 1, 72.0, 72.0,
700
				       0, gFalse, gTrue, gFalse);
701
702
}

703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
static void
poppler_page_get_property (GObject *object,
			   guint prop_id,
			   GValue *value,
			   GParamSpec *pspec)
{
  PopplerPage *page = POPPLER_PAGE (object);
  GooString label;

  switch (prop_id)
    {
    case PROP_LABEL:
      page->document->doc->getCatalog ()->indexToLabel (page->index, &label);
      g_value_set_string (value, label.getCString());
      break;
    }
}

721
722
723
724
static void
poppler_page_class_init (PopplerPageClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
725
  GParamSpec *pspec;
726
727

  gobject_class->finalize = poppler_page_finalize;
728
729
730
731
732
733
734
735
736
737
  gobject_class->get_property = poppler_page_get_property;

  pspec = g_param_spec_string ("label",
			       "Page Label",
			       "The label of the page",
			       NULL,
			       G_PARAM_READABLE);
  g_object_class_install_property (G_OBJECT_CLASS (klass),
				   PROP_LABEL,
				   pspec);
738
739
740
741
742
743
}

static void
poppler_page_init (PopplerPage *page)
{
}
744
745
746
747
748
749


/**
 * poppler_page_get_link_mapping:
 * @page: A #PopplerPage
 * 
750
751
752
 * Returns a list of #PopplerLinkMapping items that map from a
 * location on @page to a #PopplerAction.  This list must be freed
 * with poppler_page_free_link_mapping() when done.
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
 * 
 * Return value: A #GList of #PopplerLinkMapping
 **/
GList *
poppler_page_get_link_mapping (PopplerPage *page)
{
	GList *map_list = NULL;
	gint i;
	Links *links;
	Object obj;

	g_return_val_if_fail (POPPLER_IS_PAGE (page), NULL);

	links = new Links (page->page->getAnnots (&obj),
			   page->document->doc->getCatalog ()->getBaseURI ());
	obj.free ();

	if (links == NULL)
		return NULL;

	for (i = 0; i < links->getNumLinks (); i++) {
		PopplerLinkMapping *mapping;
		LinkAction *link_action;
		Link *link;

		link = links->getLink (i);
		link_action = link->getAction ();

		/* Create the mapping */
		mapping = g_new (PopplerLinkMapping, 1);
		mapping->action = _poppler_action_new (page->document, link_action, NULL);
784
785
		link->getRect (&(mapping->area.x1), &(mapping->area.y1),
			       &(mapping->area.x2), &(mapping->area.y2));
786

787
788
789
790
		mapping->area.x1 -= page->page->getCropBox()->x1;
		mapping->area.x2 -= page->page->getCropBox()->x1;
		mapping->area.y1 -= page->page->getCropBox()->y1;
		mapping->area.y2 -= page->page->getCropBox()->y1;
791

792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
		map_list = g_list_prepend (map_list, mapping);
	}

	return map_list;
}

static void
poppler_mapping_free (PopplerLinkMapping *mapping)
{
	poppler_action_free (mapping->action);
	g_free (mapping);
}

/**
 * poppler_page_free_link_mapping:
 * @list: A list of #PopplerLinkMapping<!-- -->s
 * 
 * Frees a list of #PopplerLinkMapping<!-- -->s allocated by
 * poppler_page_get_link_mapping().  It also frees the #PopplerAction<!-- -->s
 * that each mapping contains, so if you want to keep them around, you need to
 * copy them with poppler_action_copy().
 **/
void
poppler_page_free_link_mapping (GList *list)
{
	if (list == NULL)
		return;

	g_list_foreach (list, (GFunc) (poppler_mapping_free), NULL);
	g_list_free (list);
}
823

824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/* PopplerRectangle type */

GType
poppler_rectangle_get_type (void)
{
  static GType our_type = 0;

  if (our_type == 0)
    our_type = g_boxed_type_register_static ("PopplerRectangle",
					     (GBoxedCopyFunc) poppler_rectangle_copy,
					     (GBoxedFreeFunc) poppler_rectangle_free);

  return our_type;
}

PopplerRectangle *
poppler_rectangle_new (void)
{
	return g_new0 (PopplerRectangle, 1);
}

PopplerRectangle *
poppler_rectangle_copy (PopplerRectangle *rectangle)
{
	PopplerRectangle *new_rectangle;

	g_return_val_if_fail (rectangle != NULL, NULL);

	new_rectangle = g_new0 (PopplerRectangle, 1);
	*new_rectangle = *rectangle;

	return new_rectangle;
}

void
poppler_rectangle_free (PopplerRectangle *rectangle)
{
	g_free (rectangle);
}

/* PopplerLinkMapping type */
GType
poppler_link_mapping_get_type (void)
{
  static GType our_type = 0;

  if (our_type == 0)
    our_type = g_boxed_type_register_static ("PopplerLinkMapping",
					     (GBoxedCopyFunc) poppler_link_mapping_copy,
					     (GBoxedFreeFunc) poppler_link_mapping_free);

  return our_type;
}

PopplerLinkMapping *
poppler_link_mapping_new (void)
{
	return (PopplerLinkMapping *) g_new0 (PopplerLinkMapping, 1);
}

PopplerLinkMapping *
poppler_link_mapping_copy (PopplerLinkMapping *mapping)
{
	PopplerLinkMapping *new_mapping;

	new_mapping = poppler_link_mapping_new ();
	
	*new_mapping = *mapping;
	if (new_mapping->action)
		new_mapping->action = poppler_action_copy (new_mapping->action);

	return new_mapping;
}

void
poppler_link_mapping_free (PopplerLinkMapping *mapping)
{
	if (mapping)
		poppler_action_free (mapping->action);

	g_free (mapping);
}