forked from NetsoftHoldings/poppler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoppler-page.cc
2421 lines (2061 loc) · 71.1 KB
/
poppler-page.cc
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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
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
784
785
786
787
788
789
790
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
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* poppler-page.cc: glib wrapper for poppler
* 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <cmath>
#ifndef __GI_SCANNER__
# include <GlobalParams.h>
# include <PDFDoc.h>
# include <Outline.h>
# include <ErrorCodes.h>
# include <UnicodeMap.h>
# include <GfxState.h>
# include <PageTransition.h>
# include <BBoxOutputDev.h>
#endif
#include "poppler.h"
#include "poppler-private.h"
static void _page_unrotate_xy(Page *page, double *x, double *y);
/**
* SECTION:poppler-page
* @short_description: Information about a page in a document
* @title: PopplerPage
*/
enum
{
PROP_0,
PROP_LABEL
};
typedef struct _PopplerPageClass PopplerPageClass;
struct _PopplerPageClass
{
GObjectClass parent_class;
};
G_DEFINE_TYPE(PopplerPage, poppler_page, G_TYPE_OBJECT)
PopplerPage *_poppler_page_new(PopplerDocument *document, Page *page, int index)
{
PopplerPage *poppler_page;
g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), NULL);
poppler_page = (PopplerPage *)g_object_new(POPPLER_TYPE_PAGE, nullptr, NULL);
poppler_page->document = (PopplerDocument *)g_object_ref(document);
poppler_page->page = page;
poppler_page->index = index;
return poppler_page;
}
static void poppler_page_finalize(GObject *object)
{
PopplerPage *page = POPPLER_PAGE(object);
g_object_unref(page->document);
page->document = nullptr;
if (page->text != nullptr)
page->text->decRefCnt();
/* page->page is owned by the document */
G_OBJECT_CLASS(poppler_page_parent_class)->finalize(object);
}
/**
* poppler_page_get_size:
* @page: A #PopplerPage
* @width: (out) (allow-none): return location for the width of @page
* @height: (out) (allow-none): return location for the height of @page
*
* Gets the size of @page at the current scale and rotation.
**/
void poppler_page_get_size(PopplerPage *page, double *width, double *height)
{
double page_width, page_height;
int rotate;
g_return_if_fail(POPPLER_IS_PAGE(page));
rotate = page->page->getRotate();
if (rotate == 90 || rotate == 270) {
page_height = page->page->getCropWidth();
page_width = page->page->getCropHeight();
} else {
page_width = page->page->getCropWidth();
page_height = page->page->getCropHeight();
}
if (width != nullptr)
*width = page_width;
if (height != nullptr)
*height = page_height;
}
/**
* poppler_page_get_index:
* @page: a #PopplerPage
*
* Returns the index of @page
*
* Return value: index value of @page
**/
int poppler_page_get_index(PopplerPage *page)
{
g_return_val_if_fail(POPPLER_IS_PAGE(page), 0);
return page->index;
}
/**
* poppler_page_get_label:
* @page: a #PopplerPage
*
* Returns the label of @page. Note that page labels
* and page indices might not coincide.
*
* Return value: a new allocated string containing the label of @page,
* or %NULL if @page doesn't have a label
*
* Since: 0.16
**/
gchar *poppler_page_get_label(PopplerPage *page)
{
GooString label;
g_return_val_if_fail(POPPLER_IS_PAGE(page), NULL);
page->document->doc->getCatalog()->indexToLabel(page->index, &label);
return _poppler_goo_string_to_utf8(&label);
}
/**
* poppler_page_get_duration:
* @page: a #PopplerPage
*
* Returns the duration of @page
*
* Return value: duration in seconds of @page or -1.
**/
double poppler_page_get_duration(PopplerPage *page)
{
g_return_val_if_fail(POPPLER_IS_PAGE(page), -1);
return page->page->getDuration();
}
/**
* poppler_page_get_transition:
* @page: a #PopplerPage
*
* Returns the transition effect of @page
*
* Return value: a #PopplerPageTransition or %NULL.
**/
PopplerPageTransition *poppler_page_get_transition(PopplerPage *page)
{
PageTransition *trans;
PopplerPageTransition *transition;
g_return_val_if_fail(POPPLER_IS_PAGE(page), NULL);
Object obj = page->page->getTrans();
trans = new PageTransition(&obj);
if (!trans->isOk()) {
delete trans;
return nullptr;
}
transition = poppler_page_transition_new();
switch (trans->getType()) {
case transitionReplace:
transition->type = POPPLER_PAGE_TRANSITION_REPLACE;
break;
case transitionSplit:
transition->type = POPPLER_PAGE_TRANSITION_SPLIT;
break;
case transitionBlinds:
transition->type = POPPLER_PAGE_TRANSITION_BLINDS;
break;
case transitionBox:
transition->type = POPPLER_PAGE_TRANSITION_BOX;
break;
case transitionWipe:
transition->type = POPPLER_PAGE_TRANSITION_WIPE;
break;
case transitionDissolve:
transition->type = POPPLER_PAGE_TRANSITION_DISSOLVE;
break;
case transitionGlitter:
transition->type = POPPLER_PAGE_TRANSITION_GLITTER;
break;
case transitionFly:
transition->type = POPPLER_PAGE_TRANSITION_FLY;
break;
case transitionPush:
transition->type = POPPLER_PAGE_TRANSITION_PUSH;
break;
case transitionCover:
transition->type = POPPLER_PAGE_TRANSITION_COVER;
break;
case transitionUncover:
transition->type = POPPLER_PAGE_TRANSITION_UNCOVER;
break;
case transitionFade:
transition->type = POPPLER_PAGE_TRANSITION_FADE;
break;
default:
g_assert_not_reached();
}
transition->alignment = (trans->getAlignment() == transitionHorizontal) ? POPPLER_PAGE_TRANSITION_HORIZONTAL : POPPLER_PAGE_TRANSITION_VERTICAL;
transition->direction = (trans->getDirection() == transitionInward) ? POPPLER_PAGE_TRANSITION_INWARD : POPPLER_PAGE_TRANSITION_OUTWARD;
transition->duration = trans->getDuration();
transition->duration_real = trans->getDuration();
transition->angle = trans->getAngle();
transition->scale = trans->getScale();
transition->rectangular = trans->isRectangular();
delete trans;
return transition;
}
static TextPage *poppler_page_get_text_page(PopplerPage *page)
{
if (page->text == nullptr) {
TextOutputDev *text_dev;
Gfx *gfx;
text_dev = new TextOutputDev(nullptr, true, 0, false, false);
gfx = page->page->createGfx(text_dev, 72.0, 72.0, 0, false, /* useMediaBox */
true, /* Crop */
-1, -1, -1, -1, false, /* printing */
nullptr, nullptr);
page->page->display(gfx);
text_dev->endPage();
page->text = text_dev->takeText();
delete gfx;
delete text_dev;
}
return page->text;
}
static gboolean annot_is_markup(Annot *annot)
{
switch (annot->getType()) {
case Annot::typeLink:
case Annot::typePopup:
case Annot::typeMovie:
case Annot::typeScreen:
case Annot::typePrinterMark:
case Annot::typeTrapNet:
case Annot::typeWatermark:
case Annot::type3D:
case Annot::typeWidget:
return FALSE;
default:
return TRUE;
}
}
static bool poppler_print_annot_cb(Annot *annot, void *user_data)
{
PopplerPrintFlags user_print_flags = (PopplerPrintFlags)GPOINTER_TO_INT(user_data);
if (annot->getFlags() & Annot::flagHidden)
return false;
if (user_print_flags & POPPLER_PRINT_STAMP_ANNOTS_ONLY) {
return (annot->getType() == Annot::typeStamp) ? (annot->getFlags() & Annot::flagPrint) : (annot->getType() == Annot::typeWidget);
}
if (user_print_flags & POPPLER_PRINT_MARKUP_ANNOTS) {
return annot_is_markup(annot) ? (annot->getFlags() & Annot::flagPrint) : (annot->getType() == Annot::typeWidget);
}
/* Print document only, form fields are always printed */
return (annot->getType() == Annot::typeWidget);
}
static void _poppler_page_render(PopplerPage *page, cairo_t *cairo, bool printing, PopplerPrintFlags print_flags)
{
CairoOutputDev *output_dev;
g_return_if_fail(POPPLER_IS_PAGE(page));
output_dev = page->document->output_dev;
output_dev->setCairo(cairo);
output_dev->setPrinting(printing);
if (!printing && page->text == nullptr) {
page->text = new TextPage(false);
output_dev->setTextPage(page->text);
}
/* NOTE: instead of passing -1 we should/could use cairo_clip_extents()
* to get a bounding box */
cairo_save(cairo);
page->page->displaySlice(output_dev, 72.0, 72.0, 0, false, /* useMediaBox */
true, /* Crop */
-1, -1, -1, -1, printing, nullptr, nullptr, printing ? poppler_print_annot_cb : nullptr, printing ? GINT_TO_POINTER((gint)print_flags) : nullptr);
cairo_restore(cairo);
output_dev->setCairo(nullptr);
output_dev->setTextPage(nullptr);
}
/**
* poppler_page_render:
* @page: the page to render from
* @cairo: cairo context to render to
*
* Render the page to the given cairo context. This function
* is for rendering a page that will be displayed. If you want
* to render a page that will be printed use
* poppler_page_render_for_printing() instead. Please see the documentation
* for that function for the differences between rendering to the screen and
* rendering to a printer.
**/
void poppler_page_render(PopplerPage *page, cairo_t *cairo)
{
g_return_if_fail(POPPLER_IS_PAGE(page));
_poppler_page_render(page, cairo, false, (PopplerPrintFlags)0);
}
/**
* poppler_page_render_for_printing_with_options:
* @page: the page to render from
* @cairo: cairo context to render to
* @options: print options
*
* Render the page to the given cairo context for printing
* with the specified options
*
* See the documentation for poppler_page_render_for_printing() for the
* differences between rendering to the screen and rendering to a printer.
*
* Since: 0.16
**/
void poppler_page_render_for_printing_with_options(PopplerPage *page, cairo_t *cairo, PopplerPrintFlags options)
{
g_return_if_fail(POPPLER_IS_PAGE(page));
_poppler_page_render(page, cairo, true, options);
}
/**
* poppler_page_render_for_printing:
* @page: the page to render from
* @cairo: cairo context to render to
*
* Render the page to the given cairo context for printing with
* #POPPLER_PRINT_ALL flags selected. If you want a different set of flags,
* use poppler_page_render_for_printing_with_options().
*
* The difference between poppler_page_render() and this function is that some
* things get rendered differently between screens and printers:
*
* <itemizedlist>
* <listitem>
* PDF annotations get rendered according to their #PopplerAnnotFlag value.
* For example, #POPPLER_ANNOT_FLAG_PRINT refers to whether an annotation
* is printed or not, whereas #POPPLER_ANNOT_FLAG_NO_VIEW refers to whether
* an annotation is invisible when displaying to the screen.
* </listitem>
* <listitem>
* PDF supports "hairlines" of width 0.0, which often get rendered as
* having a width of 1 device pixel. When displaying on a screen, Cairo
* may render such lines wide so that they are hard to see, and Poppler
* makes use of PDF's Stroke Adjust graphics parameter to make the lines
* easier to see. However, when printing, Poppler is able to directly use a
* printer's pixel size instead.
* </listitem>
* <listitem>
* Some advanced features in PDF may require an image to be rasterized
* before sending off to a printer. This may produce raster images which
* exceed Cairo's limits. The "printing" functions will detect this condition
* and try to down-scale the intermediate surfaces as appropriate.
* </listitem>
* </itemizedlist>
*
**/
void poppler_page_render_for_printing(PopplerPage *page, cairo_t *cairo)
{
g_return_if_fail(POPPLER_IS_PAGE(page));
_poppler_page_render(page, cairo, true, POPPLER_PRINT_ALL);
}
static cairo_surface_t *create_surface_from_thumbnail_data(guchar *data, gint width, gint height, gint rowstride)
{
guchar *cairo_pixels;
gint cairo_stride;
cairo_surface_t *surface;
int j;
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
if (cairo_surface_status(surface))
return nullptr;
cairo_pixels = cairo_image_surface_get_data(surface);
cairo_stride = cairo_image_surface_get_stride(surface);
for (j = height; j; j--) {
guchar *p = data;
guchar *q = cairo_pixels;
guchar *end = p + 3 * width;
while (p < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
q[0] = p[2];
q[1] = p[1];
q[2] = p[0];
#else
q[1] = p[0];
q[2] = p[1];
q[3] = p[2];
#endif
p += 3;
q += 4;
}
data += rowstride;
cairo_pixels += cairo_stride;
}
return surface;
}
/**
* poppler_page_get_thumbnail:
* @page: the #PopplerPage 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 cairo_surface_t or %NULL if the document
* doesn't have a thumbnail for this page.
**/
cairo_surface_t *poppler_page_get_thumbnail(PopplerPage *page)
{
unsigned char *data;
int width, height, rowstride;
cairo_surface_t *surface;
g_return_val_if_fail(POPPLER_IS_PAGE(page), NULL);
if (!page->page->loadThumb(&data, &width, &height, &rowstride))
return nullptr;
surface = create_surface_from_thumbnail_data(data, width, height, rowstride);
gfree(data);
return surface;
}
/**
* poppler_page_render_selection:
* @page: the #PopplerPage for which to render selection
* @cairo: cairo context to render to
* @selection: start and end point of selection as a rectangle
* @old_selection: previous selection
* @style: a #PopplerSelectionStyle
* @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 to
* the given cairo context. The selection will be rendered, using
* @glyph_color for the glyphs and @background_color for the selection
* background.
*
* If non-NULL, @old_selection specifies the selection that is already
* rendered to @cairo, in which case this function will (some day)
* only render the changed part of the selection.
**/
void poppler_page_render_selection(PopplerPage *page, cairo_t *cairo, PopplerRectangle *selection, PopplerRectangle *old_selection, PopplerSelectionStyle style, PopplerColor *glyph_color, PopplerColor *background_color)
{
CairoOutputDev *output_dev;
TextPage *text;
SelectionStyle selection_style = selectionStyleGlyph;
PDFRectangle pdf_selection(selection->x1, selection->y1, selection->x2, selection->y2);
GfxColor gfx_background_color = { { background_color->red, background_color->green, background_color->blue } };
GfxColor gfx_glyph_color = { { glyph_color->red, glyph_color->green, glyph_color->blue } };
switch (style) {
case POPPLER_SELECTION_GLYPH:
selection_style = selectionStyleGlyph;
break;
case POPPLER_SELECTION_WORD:
selection_style = selectionStyleWord;
break;
case POPPLER_SELECTION_LINE:
selection_style = selectionStyleLine;
break;
}
output_dev = page->document->output_dev;
output_dev->setCairo(cairo);
text = poppler_page_get_text_page(page);
text->drawSelection(output_dev, 1.0, 0, &pdf_selection, selection_style, &gfx_glyph_color, &gfx_background_color);
output_dev->setCairo(nullptr);
}
/**
* poppler_page_get_thumbnail_size:
* @page: A #PopplerPage
* @width: (out): return location for width
* @height: (out): return location for height
*
* 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.
*
* Return value: %TRUE, if @page has a thumbnail associated with it.
**/
gboolean poppler_page_get_thumbnail_size(PopplerPage *page, int *width, int *height)
{
Dict *dict;
gboolean retval = FALSE;
g_return_val_if_fail(POPPLER_IS_PAGE(page), FALSE);
g_return_val_if_fail(width != nullptr, FALSE);
g_return_val_if_fail(height != nullptr, FALSE);
Object thumb = page->page->getThumb();
if (!thumb.isStream()) {
return FALSE;
}
dict = thumb.streamGetDict();
/* Theoretically, this could succeed and you would still fail when
* loading the thumb */
if (dict->lookupInt("Width", "W", width) && dict->lookupInt("Height", "H", height))
retval = TRUE;
return retval;
}
/**
* poppler_page_get_selection_region:
* @page: a #PopplerPage
* @scale: scale specified as pixels per point
* @style: a #PopplerSelectionStyle
* @selection: start and end point of selection as a rectangle
*
* Returns a region containing the area that would be rendered by
* poppler_page_render_selection() as a #GList of
* #PopplerRectangle. The returned list must be freed with
* poppler_page_selection_region_free().
*
* Return value: (element-type PopplerRectangle) (transfer full): a #GList of #PopplerRectangle
*
* Deprecated: 0.16: Use poppler_page_get_selected_region() instead.
**/
GList *poppler_page_get_selection_region(PopplerPage *page, gdouble scale, PopplerSelectionStyle style, PopplerRectangle *selection)
{
PDFRectangle poppler_selection;
TextPage *text;
SelectionStyle selection_style = selectionStyleGlyph;
GList *region = nullptr;
poppler_selection.x1 = selection->x1;
poppler_selection.y1 = selection->y1;
poppler_selection.x2 = selection->x2;
poppler_selection.y2 = selection->y2;
switch (style) {
case POPPLER_SELECTION_GLYPH:
selection_style = selectionStyleGlyph;
break;
case POPPLER_SELECTION_WORD:
selection_style = selectionStyleWord;
break;
case POPPLER_SELECTION_LINE:
selection_style = selectionStyleLine;
break;
}
text = poppler_page_get_text_page(page);
std::vector<PDFRectangle *> *list = text->getSelectionRegion(&poppler_selection, selection_style, scale);
for (const PDFRectangle *selection_rect : *list) {
PopplerRectangle *rect;
rect = poppler_rectangle_new();
rect->x1 = selection_rect->x1;
rect->y1 = selection_rect->y1;
rect->x2 = selection_rect->x2;
rect->y2 = selection_rect->y2;
region = g_list_prepend(region, rect);
delete selection_rect;
}
delete list;
return g_list_reverse(region);
}
/**
* poppler_page_selection_region_free:
* @region: (element-type PopplerRectangle): a #GList of
* #PopplerRectangle
*
* Frees @region
*
* Deprecated: 0.16: Use only to free deprecated regions created by
* poppler_page_get_selection_region(). Regions created by
* poppler_page_get_selected_region() should be freed with
* cairo_region_destroy() instead.
*/
void poppler_page_selection_region_free(GList *region)
{
if (G_UNLIKELY(!region))
return;
g_list_free_full(region, (GDestroyNotify)poppler_rectangle_free);
}
/**
* poppler_page_get_selected_region:
* @page: a #PopplerPage
* @scale: scale specified as pixels per point
* @style: a #PopplerSelectionStyle
* @selection: start and end point of selection as a rectangle
*
* Returns a region containing the area that would be rendered by
* poppler_page_render_selection().
* The returned region must be freed with cairo_region_destroy()
*
* Return value: (transfer full): a cairo_region_t
*
* Since: 0.16
**/
cairo_region_t *poppler_page_get_selected_region(PopplerPage *page, gdouble scale, PopplerSelectionStyle style, PopplerRectangle *selection)
{
PDFRectangle poppler_selection;
TextPage *text;
SelectionStyle selection_style = selectionStyleGlyph;
cairo_region_t *region;
poppler_selection.x1 = selection->x1;
poppler_selection.y1 = selection->y1;
poppler_selection.x2 = selection->x2;
poppler_selection.y2 = selection->y2;
switch (style) {
case POPPLER_SELECTION_GLYPH:
selection_style = selectionStyleGlyph;
break;
case POPPLER_SELECTION_WORD:
selection_style = selectionStyleWord;
break;
case POPPLER_SELECTION_LINE:
selection_style = selectionStyleLine;
break;
}
text = poppler_page_get_text_page(page);
std::vector<PDFRectangle *> *list = text->getSelectionRegion(&poppler_selection, selection_style, 1.0);
region = cairo_region_create();
for (const PDFRectangle *selection_rect : *list) {
cairo_rectangle_int_t rect;
rect.x = (gint)((selection_rect->x1 * scale) + 0.5);
rect.y = (gint)((selection_rect->y1 * scale) + 0.5);
rect.width = (gint)(((selection_rect->x2 - selection_rect->x1) * scale) + 0.5);
rect.height = (gint)(((selection_rect->y2 - selection_rect->y1) * scale) + 0.5);
cairo_region_union_rectangle(region, &rect);
delete selection_rect;
}
delete list;
return region;
}
/**
* poppler_page_get_selected_text:
* @page: a #PopplerPage
* @style: a #PopplerSelectionStyle
* @selection: the #PopplerRectangle including the text
*
* Retrieves the contents of the specified @selection as text.
*
* Return value: a pointer to the contents of the @selection
* as a string
* Since: 0.16
**/
char *poppler_page_get_selected_text(PopplerPage *page, PopplerSelectionStyle style, PopplerRectangle *selection)
{
GooString *sel_text;
char *result;
TextPage *text;
SelectionStyle selection_style = selectionStyleGlyph;
PDFRectangle pdf_selection;
g_return_val_if_fail(POPPLER_IS_PAGE(page), NULL);
g_return_val_if_fail(selection != nullptr, NULL);
pdf_selection.x1 = selection->x1;
pdf_selection.y1 = selection->y1;
pdf_selection.x2 = selection->x2;
pdf_selection.y2 = selection->y2;
switch (style) {
case POPPLER_SELECTION_GLYPH:
selection_style = selectionStyleGlyph;
break;
case POPPLER_SELECTION_WORD:
selection_style = selectionStyleWord;
break;
case POPPLER_SELECTION_LINE:
selection_style = selectionStyleLine;
break;
}
text = poppler_page_get_text_page(page);
sel_text = text->getSelectionText(&pdf_selection, selection_style);
result = g_strdup(sel_text->c_str());
delete sel_text;
return result;
}
/**
* poppler_page_get_text:
* @page: a #PopplerPage
*
* Retrieves the text of @page.
*
* Return value: a pointer to the text of the @page
* as a string
* Since: 0.16
**/
char *poppler_page_get_text(PopplerPage *page)
{
PopplerRectangle rectangle = { 0, 0, 0, 0 };
g_return_val_if_fail(POPPLER_IS_PAGE(page), NULL);
poppler_page_get_size(page, &rectangle.x2, &rectangle.y2);
return poppler_page_get_selected_text(page, POPPLER_SELECTION_GLYPH, &rectangle);
}
/**
* poppler_page_get_text_for_area:
* @page: a #PopplerPage
* @area: a #PopplerRectangle
*
* Retrieves the text of @page contained in @area.
*
* Return value: a pointer to the text as a string
*
* Since: 0.26
**/
char *poppler_page_get_text_for_area(PopplerPage *page, PopplerRectangle *area)
{
g_return_val_if_fail(POPPLER_IS_PAGE(page), NULL);
g_return_val_if_fail(area != nullptr, NULL);
return poppler_page_get_selected_text(page, POPPLER_SELECTION_GLYPH, area);
}
/**
* poppler_page_find_text_with_options:
* @page: a #PopplerPage
* @text: the text to search for (UTF-8 encoded)
* @options: find options
*
* Finds @text in @page with the given #PopplerFindFlags options and
* returns a #GList of rectangles for each occurrence of the text on the page.
* The coordinates are in PDF points.
*
* Return value: (element-type PopplerRectangle) (transfer full): a #GList of #PopplerRectangle,
*
* Since: 0.22
**/
GList *poppler_page_find_text_with_options(PopplerPage *page, const char *text, PopplerFindFlags options)
{
PopplerRectangle *match;
GList *matches;
double xMin, yMin, xMax, yMax;
gunichar *ucs4;
glong ucs4_len;
double height;
TextPage *text_dev;
gboolean backwards;
gboolean start_at_last = FALSE;
g_return_val_if_fail(POPPLER_IS_PAGE(page), NULL);
g_return_val_if_fail(text != nullptr, NULL);
text_dev = poppler_page_get_text_page(page);
ucs4 = g_utf8_to_ucs4_fast(text, -1, &ucs4_len);
poppler_page_get_size(page, nullptr, &height);
backwards = options & POPPLER_FIND_BACKWARDS;
matches = nullptr;
xMin = 0;
yMin = backwards ? height : 0;
while (text_dev->findText(ucs4, ucs4_len, false, true, // startAtTop, stopAtBottom
start_at_last,
false, // stopAtLast
options & POPPLER_FIND_CASE_SENSITIVE, options & POPPLER_FIND_IGNORE_DIACRITICS, backwards, options & POPPLER_FIND_WHOLE_WORDS_ONLY, &xMin, &yMin, &xMax, &yMax)) {
match = poppler_rectangle_new();
match->x1 = xMin;
match->y1 = height - yMax;
match->x2 = xMax;
match->y2 = height - yMin;
matches = g_list_prepend(matches, match);
start_at_last = TRUE;
}
g_free(ucs4);
return g_list_reverse(matches);
}
/**
* poppler_page_find_text:
* @page: a #PopplerPage
* @text: the text to search for (UTF-8 encoded)
*
* Finds @text in @page with the default options (%POPPLER_FIND_DEFAULT) and
* returns a #GList of rectangles for each occurrence of the text on the page.
* The coordinates are in PDF points.
*
* Return value: (element-type PopplerRectangle) (transfer full): a #GList of #PopplerRectangle,
**/
GList *poppler_page_find_text(PopplerPage *page, const char *text)
{
return poppler_page_find_text_with_options(page, text, POPPLER_FIND_DEFAULT);
}
static CairoImageOutputDev *poppler_page_get_image_output_dev(PopplerPage *page, bool (*imgDrawDeviceCbk)(int img_id, void *data), void *imgDrawCbkData)
{
CairoImageOutputDev *image_dev;
Gfx *gfx;
image_dev = new CairoImageOutputDev();
if (imgDrawDeviceCbk) {
image_dev->setImageDrawDecideCbk(imgDrawDeviceCbk, imgDrawCbkData);
}
gfx = page->page->createGfx(image_dev, 72.0, 72.0, 0, false, /* useMediaBox */
true, /* Crop */
-1, -1, -1, -1, false, /* printing */
nullptr, nullptr);
page->page->display(gfx);
delete gfx;
return image_dev;
}
/**
* poppler_page_get_image_mapping:
* @page: A #PopplerPage
*
* Returns a list of #PopplerImageMapping items that map from a
* location on @page to an image of the page. This list must be freed
* with poppler_page_free_image_mapping() when done.
*
* Return value: (element-type PopplerImageMapping) (transfer full): A #GList of #PopplerImageMapping
**/
GList *poppler_page_get_image_mapping(PopplerPage *page)
{
GList *map_list = nullptr;
CairoImageOutputDev *out;
gint i;
g_return_val_if_fail(POPPLER_IS_PAGE(page), NULL);
out = poppler_page_get_image_output_dev(page, nullptr, nullptr);
for (i = 0; i < out->getNumImages(); i++) {
PopplerImageMapping *mapping;
CairoImage *image;
image = out->getImage(i);
/* Create the mapping */
mapping = poppler_image_mapping_new();
image->getRect(&(mapping->area.x1), &(mapping->area.y1), &(mapping->area.x2), &(mapping->area.y2));
mapping->image_id = i;
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;
map_list = g_list_prepend(map_list, mapping);
}
delete out;
return map_list;
}
static bool image_draw_decide_cb(int image_id, void *data)
{
return (image_id == GPOINTER_TO_INT(data));
}
/**
* poppler_page_get_image:
* @page: A #PopplerPage
* @image_id: The image identifier
*
* Returns a cairo surface for the image of the @page
*
* Return value: A cairo surface for the image
**/
cairo_surface_t *poppler_page_get_image(PopplerPage *page, gint image_id)
{
CairoImageOutputDev *out;
cairo_surface_t *image;
g_return_val_if_fail(POPPLER_IS_PAGE(page), NULL);
out = poppler_page_get_image_output_dev(page, image_draw_decide_cb, GINT_TO_POINTER(image_id));
if (image_id >= out->getNumImages()) {
delete out;
return nullptr;
}
image = out->getImage(image_id)->getImage();
if (!image) {
delete out;
return nullptr;
}
cairo_surface_reference(image);
delete out;
return image;
}
/**
* poppler_page_free_image_mapping:
* @list: (element-type PopplerImageMapping): A list of
* #PopplerImageMapping<!-- -->s
*
* Frees a list of #PopplerImageMapping<!-- -->s allocated by
* poppler_page_get_image_mapping().
**/
void poppler_page_free_image_mapping(GList *list)
{
if (G_UNLIKELY(list == nullptr))
return;
g_list_free_full(list, (GDestroyNotify)poppler_image_mapping_free);
}