forked from NetsoftHoldings/poppler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoppler-document.cc
3510 lines (3008 loc) · 99.9 KB
/
poppler-document.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-document.cc: glib wrapper for poppler
* Copyright (C) 2005, Red Hat, Inc.
*
* Copyright (C) 2016 Jakub Alba <[email protected]>
* Copyright (C) 2018-2019 Marek Kasik <[email protected]>
* Copyright (C) 2019 Masamichi Hosoda <[email protected]>
* Copyright (C) 2019, Oliver Sander <[email protected]>
* Copyright (C) 2020 Albert Astals Cid <[email protected]>
*
* 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 <cstring>
#ifndef __GI_SCANNER__
# include <memory>
# include <splash/SplashBitmap.h>
# include <DateInfo.h>
# include <GlobalParams.h>
# include <PDFDoc.h>
# include <Outline.h>
# include <ErrorCodes.h>
# include <UnicodeMap.h>
# include <GfxState.h>
# include <SplashOutputDev.h>
# include <Stream.h>
# include <FontInfo.h>
# include <PDFDocEncoding.h>
# include <OptionalContent.h>
# include <ViewerPreferences.h>
#endif
#include "poppler.h"
#include "poppler-private.h"
#include "poppler-enums.h"
#include "poppler-input-stream.h"
#include "poppler-cached-file-loader.h"
#ifdef G_OS_WIN32
# include <stringapiset.h>
#endif
/**
* SECTION:poppler-document
* @short_description: Information about a document
* @title: PopplerDocument
*
* The #PopplerDocument is an object used to refer to a main document.
*/
enum
{
PROP_0,
PROP_TITLE,
PROP_FORMAT,
PROP_FORMAT_MAJOR,
PROP_FORMAT_MINOR,
PROP_SUBTYPE,
PROP_SUBTYPE_STRING,
PROP_SUBTYPE_PART,
PROP_SUBTYPE_CONF,
PROP_AUTHOR,
PROP_SUBJECT,
PROP_KEYWORDS,
PROP_CREATOR,
PROP_PRODUCER,
PROP_CREATION_DATE,
PROP_MOD_DATE,
PROP_LINEARIZED,
PROP_PAGE_LAYOUT,
PROP_PAGE_MODE,
PROP_VIEWER_PREFERENCES,
PROP_PERMISSIONS,
PROP_METADATA,
PROP_PRINT_SCALING,
PROP_PRINT_DUPLEX,
PROP_PRINT_N_COPIES,
PROP_CREATION_DATETIME,
PROP_MOD_DATETIME
};
static void poppler_document_layers_free(PopplerDocument *document);
typedef struct _PopplerDocumentClass PopplerDocumentClass;
struct _PopplerDocumentClass
{
GObjectClass parent_class;
};
G_DEFINE_TYPE(PopplerDocument, poppler_document, G_TYPE_OBJECT)
static PopplerDocument *_poppler_document_new_from_pdfdoc(std::unique_ptr<GlobalParamsIniter> &&initer, PDFDoc *newDoc, GError **error)
{
PopplerDocument *document;
if (!newDoc->isOk()) {
int fopen_errno;
switch (newDoc->getErrorCode()) {
case errOpenFile:
// If there was an error opening the file, count it as a G_FILE_ERROR
// and set the GError parameters accordingly. (this assumes that the
// only way to get an errOpenFile error is if newDoc was created using
// a filename and thus fopen was called, which right now is true.
fopen_errno = newDoc->getFopenErrno();
g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(fopen_errno), "%s", g_strerror(fopen_errno));
break;
case errBadCatalog:
g_set_error(error, POPPLER_ERROR, POPPLER_ERROR_BAD_CATALOG, "Failed to read the document catalog");
break;
case errDamaged:
g_set_error(error, POPPLER_ERROR, POPPLER_ERROR_DAMAGED, "PDF document is damaged");
break;
case errEncrypted:
g_set_error(error, POPPLER_ERROR, POPPLER_ERROR_ENCRYPTED, "Document is encrypted");
break;
default:
g_set_error(error, POPPLER_ERROR, POPPLER_ERROR_INVALID, "Failed to load document");
}
delete newDoc;
return nullptr;
}
document = (PopplerDocument *)g_object_new(POPPLER_TYPE_DOCUMENT, nullptr);
document->initer = std::move(initer);
document->doc = newDoc;
document->output_dev = new CairoOutputDev();
document->output_dev->startDoc(document->doc);
return document;
}
static GooString *poppler_password_to_latin1(const gchar *password)
{
gchar *password_latin;
GooString *password_g;
if (!password)
return nullptr;
password_latin = g_convert(password, -1, "ISO-8859-1", "UTF-8", nullptr, nullptr, nullptr);
password_g = new GooString(password_latin);
g_free(password_latin);
return password_g;
}
/**
* poppler_document_new_from_file:
* @uri: uri of the file to load
* @password: (allow-none): password to unlock the file with, or %NULL
* @error: (allow-none): Return location for an error, or %NULL
*
* Creates a new #PopplerDocument. If %NULL is returned, then @error will be
* set. Possible errors include those in the #POPPLER_ERROR and #G_FILE_ERROR
* domains.
*
* Return value: A newly created #PopplerDocument, or %NULL
**/
PopplerDocument *poppler_document_new_from_file(const char *uri, const char *password, GError **error)
{
PDFDoc *newDoc;
GooString *password_g;
char *filename;
auto initer = std::make_unique<GlobalParamsIniter>(_poppler_error_cb);
filename = g_filename_from_uri(uri, nullptr, error);
if (!filename)
return nullptr;
password_g = poppler_password_to_latin1(password);
#ifdef G_OS_WIN32
wchar_t *filenameW;
int length;
length = MultiByteToWideChar(CP_UTF8, 0, filename, -1, nullptr, 0);
filenameW = new WCHAR[length];
if (!filenameW)
return nullptr;
length = MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, length);
newDoc = new PDFDoc(filenameW, length, password_g, password_g);
delete[] filenameW;
#else
GooString *filename_g;
filename_g = new GooString(filename);
newDoc = new PDFDoc(filename_g, password_g, password_g);
#endif
g_free(filename);
delete password_g;
return _poppler_document_new_from_pdfdoc(std::move(initer), newDoc, error);
}
/**
* poppler_document_new_from_data:
* @data: (array length=length) (element-type guint8): the pdf data
* @length: the length of #data
* @password: (nullable): password to unlock the file with, or %NULL
* @error: (nullable): Return location for an error, or %NULL
*
* Creates a new #PopplerDocument. If %NULL is returned, then @error will be
* set. Possible errors include those in the #POPPLER_ERROR and #G_FILE_ERROR
* domains.
*
* Note that @data is not copied nor is a new reference to it created.
* It must remain valid and cannot be destroyed as long as the returned
* document exists.
*
* Return value: A newly created #PopplerDocument, or %NULL
*
* Deprecated: 0.82: This requires directly managing @length and @data.
* Use poppler_document_new_from_bytes() instead.
**/
PopplerDocument *poppler_document_new_from_data(char *data, int length, const char *password, GError **error)
{
PDFDoc *newDoc;
MemStream *str;
GooString *password_g;
auto initer = std::make_unique<GlobalParamsIniter>(_poppler_error_cb);
// create stream
str = new MemStream(data, 0, length, Object(objNull));
password_g = poppler_password_to_latin1(password);
newDoc = new PDFDoc(str, password_g, password_g);
delete password_g;
return _poppler_document_new_from_pdfdoc(std::move(initer), newDoc, error);
}
class BytesStream : public MemStream
{
std::unique_ptr<GBytes, decltype(&g_bytes_unref)> m_bytes;
public:
BytesStream(GBytes *bytes, Object &&dictA) : MemStream(static_cast<const char *>(g_bytes_get_data(bytes, nullptr)), 0, g_bytes_get_size(bytes), std::move(dictA)), m_bytes { g_bytes_ref(bytes), &g_bytes_unref } { }
~BytesStream() override;
};
BytesStream::~BytesStream() = default;
/**
* poppler_document_new_from_bytes:
* @bytes: a #GBytes
* @password: (allow-none): password to unlock the file with, or %NULL
* @error: (allow-none): Return location for an error, or %NULL
*
* Creates a new #PopplerDocument from @bytes. The returned document
* will hold a reference to @bytes.
*
* On error, %NULL is returned, with @error set. Possible errors include
* those in the #POPPLER_ERROR and #G_FILE_ERROR domains.
*
* Return value: (transfer full): a newly created #PopplerDocument, or %NULL
*
* Since: 0.82
**/
PopplerDocument *poppler_document_new_from_bytes(GBytes *bytes, const char *password, GError **error)
{
PDFDoc *newDoc;
BytesStream *str;
GooString *password_g;
g_return_val_if_fail(bytes != nullptr, nullptr);
g_return_val_if_fail(error == nullptr || *error == nullptr, nullptr);
auto initer = std::make_unique<GlobalParamsIniter>(_poppler_error_cb);
// create stream
str = new BytesStream(bytes, Object(objNull));
password_g = poppler_password_to_latin1(password);
newDoc = new PDFDoc(str, password_g, password_g);
delete password_g;
return _poppler_document_new_from_pdfdoc(std::move(initer), newDoc, error);
}
static inline gboolean stream_is_memory_buffer_or_local_file(GInputStream *stream)
{
return G_IS_MEMORY_INPUT_STREAM(stream) || (G_IS_FILE_INPUT_STREAM(stream) && strcmp(g_type_name_from_instance((GTypeInstance *)stream), "GLocalFileInputStream") == 0);
}
/**
* poppler_document_new_from_stream:
* @stream: a #GInputStream to read from
* @length: the stream length, or -1 if not known
* @password: (allow-none): password to unlock the file with, or %NULL
* @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: (allow-none): Return location for an error, or %NULL
*
* Creates a new #PopplerDocument reading the PDF contents from @stream.
* Note that the given #GInputStream must be seekable or %G_IO_ERROR_NOT_SUPPORTED
* will be returned.
* Possible errors include those in the #POPPLER_ERROR, #G_FILE_ERROR
* and #G_IO_ERROR domains.
*
* Returns: (transfer full): a new #PopplerDocument, or %NULL
*
* Since: 0.22
*/
PopplerDocument *poppler_document_new_from_stream(GInputStream *stream, goffset length, const char *password, GCancellable *cancellable, GError **error)
{
PDFDoc *newDoc;
BaseStream *str;
GooString *password_g;
g_return_val_if_fail(G_IS_INPUT_STREAM(stream), NULL);
g_return_val_if_fail(length == (goffset)-1 || length > 0, NULL);
auto initer = std::make_unique<GlobalParamsIniter>(_poppler_error_cb);
if (!G_IS_SEEKABLE(stream) || !g_seekable_can_seek(G_SEEKABLE(stream))) {
g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Stream is not seekable");
return nullptr;
}
if (stream_is_memory_buffer_or_local_file(stream)) {
if (length == (goffset)-1) {
if (!g_seekable_seek(G_SEEKABLE(stream), 0, G_SEEK_END, cancellable, error)) {
g_prefix_error(error, "Unable to determine length of stream: ");
return nullptr;
}
length = g_seekable_tell(G_SEEKABLE(stream));
}
str = new PopplerInputStream(stream, cancellable, 0, false, length, Object(objNull));
} else {
CachedFile *cachedFile = new CachedFile(new PopplerCachedFileLoader(stream, cancellable, length), new GooString());
str = new CachedFileStream(cachedFile, 0, false, cachedFile->getLength(), Object(objNull));
}
password_g = poppler_password_to_latin1(password);
newDoc = new PDFDoc(str, password_g, password_g);
delete password_g;
return _poppler_document_new_from_pdfdoc(std::move(initer), newDoc, error);
}
/**
* poppler_document_new_from_gfile:
* @file: a #GFile to load
* @password: (allow-none): password to unlock the file with, or %NULL
* @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: (allow-none): Return location for an error, or %NULL
*
* Creates a new #PopplerDocument reading the PDF contents from @file.
* Possible errors include those in the #POPPLER_ERROR and #G_FILE_ERROR
* domains.
*
* Returns: (transfer full): a new #PopplerDocument, or %NULL
*
* Since: 0.22
*/
PopplerDocument *poppler_document_new_from_gfile(GFile *file, const char *password, GCancellable *cancellable, GError **error)
{
PopplerDocument *document;
GFileInputStream *stream;
g_return_val_if_fail(G_IS_FILE(file), NULL);
if (g_file_is_native(file)) {
gchar *uri;
uri = g_file_get_uri(file);
document = poppler_document_new_from_file(uri, password, error);
g_free(uri);
return document;
}
stream = g_file_read(file, cancellable, error);
if (!stream)
return nullptr;
document = poppler_document_new_from_stream(G_INPUT_STREAM(stream), -1, password, cancellable, error);
g_object_unref(stream);
return document;
}
static gboolean handle_save_error(int err_code, GError **error)
{
switch (err_code) {
case errNone:
break;
case errOpenFile:
g_set_error(error, POPPLER_ERROR, POPPLER_ERROR_OPEN_FILE, "Failed to open file for writing");
break;
case errEncrypted:
g_set_error(error, POPPLER_ERROR, POPPLER_ERROR_ENCRYPTED, "Document is encrypted");
break;
default:
g_set_error(error, POPPLER_ERROR, POPPLER_ERROR_INVALID, "Failed to save document");
}
return err_code == errNone;
}
/**
* poppler_document_save:
* @document: a #PopplerDocument
* @uri: uri of file to save
* @error: (allow-none): return location for an error, or %NULL
*
* Saves @document. Any change made in the document such as
* form fields filled, annotations added or modified
* will be saved.
* If @error is set, %FALSE will be returned. Possible errors
* include those in the #G_FILE_ERROR domain.
*
* Return value: %TRUE, if the document was successfully saved
**/
gboolean poppler_document_save(PopplerDocument *document, const char *uri, GError **error)
{
char *filename;
gboolean retval = FALSE;
g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), FALSE);
filename = g_filename_from_uri(uri, nullptr, error);
if (filename != nullptr) {
GooString *fname = new GooString(filename);
int err_code;
g_free(filename);
err_code = document->doc->saveAs(fname);
retval = handle_save_error(err_code, error);
delete fname;
}
return retval;
}
/**
* poppler_document_save_a_copy:
* @document: a #PopplerDocument
* @uri: uri of file to save
* @error: (allow-none): return location for an error, or %NULL
*
* Saves a copy of the original @document.
* Any change made in the document such as
* form fields filled by the user will not be saved.
* If @error is set, %FALSE will be returned. Possible errors
* include those in the #G_FILE_ERROR domain.
*
* Return value: %TRUE, if the document was successfully saved
**/
gboolean poppler_document_save_a_copy(PopplerDocument *document, const char *uri, GError **error)
{
char *filename;
gboolean retval = FALSE;
g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), FALSE);
filename = g_filename_from_uri(uri, nullptr, error);
if (filename != nullptr) {
GooString *fname = new GooString(filename);
int err_code;
g_free(filename);
err_code = document->doc->saveWithoutChangesAs(fname);
retval = handle_save_error(err_code, error);
delete fname;
}
return retval;
}
static void poppler_document_finalize(GObject *object)
{
PopplerDocument *document = POPPLER_DOCUMENT(object);
poppler_document_layers_free(document);
delete document->output_dev;
delete document->doc;
delete document->initer.release();
G_OBJECT_CLASS(poppler_document_parent_class)->finalize(object);
}
/**
* poppler_document_get_id:
* @document: A #PopplerDocument
* @permanent_id: (out) (allow-none): location to store an allocated string, use g_free() to free the returned string
* @update_id: (out) (allow-none): location to store an allocated string, use g_free() to free the returned string
*
* Returns the PDF file identifier represented as two byte string arrays of size 32.
* @permanent_id is the permanent identifier that is built based on the file
* contents at the time it was originally created, so that this identifer
* never changes. @update_id is the update identifier that is built based on
* the file contents at the time it was last updated.
*
* Note that returned strings are not null-terminated, they have a fixed
* size of 32 bytes.
*
* Returns: %TRUE if the @document contains an id, %FALSE otherwise
*
* Since: 0.16
*/
gboolean poppler_document_get_id(PopplerDocument *document, gchar **permanent_id, gchar **update_id)
{
GooString permanent;
GooString update;
gboolean retval = FALSE;
g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), FALSE);
if (permanent_id)
*permanent_id = nullptr;
if (update_id)
*update_id = nullptr;
if (document->doc->getID(permanent_id ? &permanent : nullptr, update_id ? &update : nullptr)) {
if (permanent_id)
*permanent_id = (gchar *)g_memdup(permanent.c_str(), 32);
if (update_id)
*update_id = (gchar *)g_memdup(update.c_str(), 32);
retval = TRUE;
}
return retval;
}
/**
* poppler_document_get_n_pages:
* @document: A #PopplerDocument
*
* Returns the number of pages in a loaded document.
*
* Return value: Number of pages
**/
int poppler_document_get_n_pages(PopplerDocument *document)
{
g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), 0);
return document->doc->getNumPages();
}
/**
* poppler_document_get_page:
* @document: A #PopplerDocument
* @index: a page index
*
* Returns the #PopplerPage indexed at @index. This object is owned by the
* caller.
*
* Return value: (transfer full) : The #PopplerPage at @index
**/
PopplerPage *poppler_document_get_page(PopplerDocument *document, int index)
{
Page *page;
g_return_val_if_fail(0 <= index && index < poppler_document_get_n_pages(document), NULL);
page = document->doc->getPage(index + 1);
if (!page)
return nullptr;
return _poppler_page_new(document, page, index);
}
/**
* poppler_document_get_page_by_label:
* @document: A #PopplerDocument
* @label: a page label
*
* Returns the #PopplerPage reference by @label. This object is owned by the
* caller. @label is a human-readable string representation of the page number,
* and can be document specific. Typically, it is a value such as "iii" or "3".
*
* By default, "1" refers to the first page.
*
* Return value: (transfer full) :The #PopplerPage referenced by @label
**/
PopplerPage *poppler_document_get_page_by_label(PopplerDocument *document, const char *label)
{
Catalog *catalog;
GooString label_g(label);
int index;
catalog = document->doc->getCatalog();
if (!catalog->labelToIndex(&label_g, &index))
return nullptr;
return poppler_document_get_page(document, index);
}
/**
* poppler_document_get_n_attachments:
* @document: A #PopplerDocument
*
* Returns the number of attachments in a loaded document.
* See also poppler_document_get_attachments()
*
* Return value: Number of attachments
*
* Since: 0.18
*/
guint poppler_document_get_n_attachments(PopplerDocument *document)
{
Catalog *catalog;
g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), 0);
catalog = document->doc->getCatalog();
return catalog && catalog->isOk() ? catalog->numEmbeddedFiles() : 0;
}
/**
* poppler_document_has_attachments:
* @document: A #PopplerDocument
*
* Returns %TRUE of @document has any attachments.
*
* Return value: %TRUE, if @document has attachments.
**/
gboolean poppler_document_has_attachments(PopplerDocument *document)
{
g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), FALSE);
return (poppler_document_get_n_attachments(document) != 0);
}
/**
* poppler_document_get_attachments:
* @document: A #PopplerDocument
*
* Returns a #GList containing #PopplerAttachment<!-- -->s. These attachments
* are unowned, and must be unreffed, and the list must be freed with
* g_list_free().
*
* Return value: (element-type PopplerAttachment) (transfer full): a list of available attachments.
**/
GList *poppler_document_get_attachments(PopplerDocument *document)
{
Catalog *catalog;
int n_files, i;
GList *retval = nullptr;
g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), NULL);
catalog = document->doc->getCatalog();
if (catalog == nullptr || !catalog->isOk())
return nullptr;
n_files = catalog->numEmbeddedFiles();
for (i = 0; i < n_files; i++) {
PopplerAttachment *attachment;
FileSpec *emb_file;
emb_file = catalog->embeddedFile(i);
if (!emb_file->isOk() || !emb_file->getEmbeddedFile()->isOk()) {
delete emb_file;
continue;
}
attachment = _poppler_attachment_new(emb_file);
delete emb_file;
if (attachment != nullptr)
retval = g_list_prepend(retval, attachment);
}
return g_list_reverse(retval);
}
/**
* poppler_named_dest_from_bytestring:
* @data: (array length=length): the bytestring data
* @length: the bytestring length
*
* Converts a bytestring into a zero-terminated string suitable to
* pass to poppler_document_find_dest().
*
* Note that the returned string has no defined encoding and is not
* suitable for display to the user.
*
* The returned data must be freed using g_free().
*
* Returns: (transfer full): the named dest
*
* Since: 0.73
*/
char *poppler_named_dest_from_bytestring(const guint8 *data, gsize length)
{
const guint8 *p, *pend;
char *dest, *q;
g_return_val_if_fail(length != 0 || data != nullptr, nullptr);
/* Each source byte needs maximally 2 destination chars (\\ or \0) */
q = dest = (gchar *)g_malloc(length * 2 + 1);
pend = data + length;
for (p = data; p < pend; ++p) {
switch (*p) {
case '\0':
*q++ = '\\';
*q++ = '0';
break;
case '\\':
*q++ = '\\';
*q++ = '\\';
break;
default:
*q++ = *p;
break;
}
}
*q = 0; /* zero terminate */
return dest;
}
/**
* poppler_named_dest_to_bytestring:
* @name: the named dest string
* @length: (out): a location to store the length of the returned bytestring
*
* Converts a named dest string (e.g. from #PopplerDest.named_dest) into a
* bytestring, inverting the transformation of
* poppler_named_dest_from_bytestring().
*
* Note that the returned data is not zero terminated and may also
* contains embedded NUL bytes.
*
* If @name is not a valid named dest string, returns %NULL.
*
* The returned data must be freed using g_free().
*
* Returns: (array length=length) (transfer full) (nullable): a new bytestring,
* or %NULL
*
* Since: 0.73
*/
guint8 *poppler_named_dest_to_bytestring(const char *name, gsize *length)
{
const char *p;
guint8 *data, *q;
gsize len;
g_return_val_if_fail(name != nullptr, nullptr);
g_return_val_if_fail(length != nullptr, nullptr);
len = strlen(name);
q = data = (guint8 *)g_malloc(len);
for (p = name; *p; ++p) {
if (*p == '\\') {
p++;
len--;
if (*p == '0')
*q++ = '\0';
else if (*p == '\\')
*q++ = '\\';
else
goto invalid;
} else {
*q++ = *p;
}
}
*length = len;
return data;
invalid:
g_free(data);
*length = 0;
return nullptr;
}
/**
* poppler_document_find_dest:
* @document: A #PopplerDocument
* @link_name: a named destination
*
* Creates a #PopplerDest for the named destination @link_name in @document.
*
* Note that named destinations are bytestrings, not string. That means that
* unless @link_name was returned by a poppler function (e.g. is
* #PopplerDest.named_dest), it needs to be converted to string
* using poppler_named_dest_from_bytestring() before being passed to this
* function.
*
* The returned value must be freed with poppler_dest_free().
*
* Return value: (transfer full): a new #PopplerDest destination, or %NULL if
* @link_name is not a destination.
**/
PopplerDest *poppler_document_find_dest(PopplerDocument *document, const gchar *link_name)
{
g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), nullptr);
g_return_val_if_fail(link_name != nullptr, nullptr);
gsize len;
guint8 *data = poppler_named_dest_to_bytestring(link_name, &len);
if (data == nullptr)
return nullptr;
GooString g_link_name((const char *)data, (int)len);
g_free(data);
std::unique_ptr<LinkDest> link_dest = document->doc->findDest(&g_link_name);
if (link_dest == nullptr)
return nullptr;
PopplerDest *dest = _poppler_dest_new_goto(document, link_dest.get());
return dest;
}
static gint _poppler_dest_compare_keys(gconstpointer a, gconstpointer b, gpointer user_data)
{
return g_strcmp0(static_cast<const gchar *>(a), static_cast<const gchar *>(b));
}
static void _poppler_dest_destroy_value(gpointer value)
{
poppler_dest_free(static_cast<PopplerDest *>(value));
}
/**
* poppler_document_create_dests_tree:
* @document: A #PopplerDocument
*
* Creates a balanced binary tree of all named destinations in @document
*
* The tree key is strings in the form returned by
* poppler_named_dest_to_bytestring() which constains a destination name.
* The tree value is the #PopplerDest which contains a named destination.
* The return value must be freed with g_tree_destroy().
*
* Returns: (transfer full) (nullable): the #GTree, or %NULL
* Since: 0.78
**/
GTree *poppler_document_create_dests_tree(PopplerDocument *document)
{
GTree *tree;
Catalog *catalog;
PopplerDest *dest;
int i;
gchar *key;
g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), nullptr);
catalog = document->doc->getCatalog();
if (catalog == nullptr)
return nullptr;
tree = g_tree_new_full(_poppler_dest_compare_keys, nullptr, g_free, _poppler_dest_destroy_value);
// Iterate from name-dict
const int nDests = catalog->numDests();
for (i = 0; i < nDests; ++i) {
// The names of name-dict cannot contain \0,
// so we can use strlen().
auto name = catalog->getDestsName(i);
key = poppler_named_dest_from_bytestring(reinterpret_cast<const guint8 *>(name), strlen(name));
std::unique_ptr<LinkDest> link_dest = catalog->getDestsDest(i);
if (link_dest) {
dest = _poppler_dest_new_goto(document, link_dest.get());
g_tree_insert(tree, key, dest);
}
}
// Iterate form name-tree
const int nDestsNameTree = catalog->numDestNameTree();
for (i = 0; i < nDestsNameTree; ++i) {
auto name = catalog->getDestNameTreeName(i);
key = poppler_named_dest_from_bytestring(reinterpret_cast<const guint8 *>(name->c_str()), name->getLength());
std::unique_ptr<LinkDest> link_dest = catalog->getDestNameTreeDest(i);
if (link_dest) {
dest = _poppler_dest_new_goto(document, link_dest.get());
g_tree_insert(tree, key, dest);
}
}
return tree;
}
char *_poppler_goo_string_to_utf8(const GooString *s)
{
if (s == nullptr) {
return nullptr;
}
char *result;
if (s->hasUnicodeMarker()) {
result = g_convert(s->c_str() + 2, s->getLength() - 2, "UTF-8", "UTF-16BE", nullptr, nullptr, nullptr);
} else if (s->hasUnicodeMarkerLE()) {
result = g_convert(s->c_str() + 2, s->getLength() - 2, "UTF-8", "UTF-16LE", nullptr, nullptr, nullptr);
} else {
int len;
gunichar *ucs4_temp;
int i;
len = s->getLength();
ucs4_temp = g_new(gunichar, len + 1);
for (i = 0; i < len; ++i) {
ucs4_temp[i] = pdfDocEncoding[(unsigned char)s->getChar(i)];
}
ucs4_temp[i] = 0;
result = g_ucs4_to_utf8(ucs4_temp, -1, nullptr, nullptr, nullptr);
g_free(ucs4_temp);
}
return result;
}
static GooString *_poppler_goo_string_from_utf8(const gchar *src)
{
if (src == nullptr) {
return nullptr;
}
gsize outlen;
gchar *utf16 = g_convert(src, -1, "UTF-16BE", "UTF-8", nullptr, &outlen, nullptr);
if (utf16 == nullptr) {
return nullptr;
}
GooString *result = new GooString(utf16, outlen);
g_free(utf16);
if (!result->hasUnicodeMarker()) {
result->prependUnicodeMarker();
}
return result;
}
static PopplerPageLayout convert_page_layout(Catalog::PageLayout pageLayout)
{
switch (pageLayout) {
case Catalog::pageLayoutSinglePage:
return POPPLER_PAGE_LAYOUT_SINGLE_PAGE;
case Catalog::pageLayoutOneColumn:
return POPPLER_PAGE_LAYOUT_ONE_COLUMN;
case Catalog::pageLayoutTwoColumnLeft:
return POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT;
case Catalog::pageLayoutTwoColumnRight:
return POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT;
case Catalog::pageLayoutTwoPageLeft:
return POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT;
case Catalog::pageLayoutTwoPageRight:
return POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT;
case Catalog::pageLayoutNone:
default:
return POPPLER_PAGE_LAYOUT_UNSET;
}
}
static PopplerPageMode convert_page_mode(Catalog::PageMode pageMode)
{
switch (pageMode) {
case Catalog::pageModeOutlines:
return POPPLER_PAGE_MODE_USE_OUTLINES;
case Catalog::pageModeThumbs:
return POPPLER_PAGE_MODE_USE_THUMBS;
case Catalog::pageModeFullScreen:
return POPPLER_PAGE_MODE_FULL_SCREEN;
case Catalog::pageModeOC:
return POPPLER_PAGE_MODE_USE_OC;
case Catalog::pageModeAttach:
return POPPLER_PAGE_MODE_USE_ATTACHMENTS;
case Catalog::pageModeNone:
default:
return POPPLER_PAGE_MODE_UNSET;
}
}
static PopplerPDFSubtype convert_pdf_subtype(PDFSubtype pdfSubtype)
{
switch (pdfSubtype) {
case subtypePDFA: