forked from NetsoftHoldings/poppler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoppler-structure-element.cc
1979 lines (1759 loc) · 72.5 KB
/
poppler-structure-element.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-structure.cc: glib interface to poppler
*
* Copyright (C) 2013 Igalia S.L.
* Copyright (C) 2018 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"
#ifndef __GI_SCANNER__
# include <StructTreeRoot.h>
# include <StructElement.h>
# include <GlobalParams.h>
# include <UnicodeMap.h>
# include <cmath>
#endif /* !__GI_SCANNER__ */
#include "poppler.h"
#include "poppler-private.h"
#include "poppler-structure-element.h"
/**
* SECTION:poppler-structure-element
* @short_description: Document structure element.
* @title: PopplerStructureElement
*
* Instances of #PopplerStructureElement are used to describe the structure
* of a #PopplerDocument. To access the elements in the structure of the
* document, use poppler_structure_element_iter_new() to obtain an iterator
* for the top-level #PopplerStructureElement, and then use the
* #PopplerStructureElementIter methods to traverse the structure tree.
*/
typedef struct _PopplerStructureElementClass
{
GObjectClass parent_class;
} PopplerStructureElementClass;
G_DEFINE_TYPE(PopplerStructureElement, poppler_structure_element, G_TYPE_OBJECT)
static PopplerStructureElement *_poppler_structure_element_new(PopplerDocument *document, const StructElement *element)
{
PopplerStructureElement *poppler_structure_element;
g_assert(POPPLER_IS_DOCUMENT(document));
g_assert(element);
poppler_structure_element = (PopplerStructureElement *)g_object_new(POPPLER_TYPE_STRUCTURE_ELEMENT, nullptr, NULL);
poppler_structure_element->document = (PopplerDocument *)g_object_ref(document);
poppler_structure_element->elem = element;
return poppler_structure_element;
}
static void poppler_structure_element_init(PopplerStructureElement *poppler_structure_element) { }
static void poppler_structure_element_finalize(GObject *object)
{
PopplerStructureElement *poppler_structure_element = POPPLER_STRUCTURE_ELEMENT(object);
/* poppler_structure_element->elem is owned by the StructTreeRoot */
g_object_unref(poppler_structure_element->document);
G_OBJECT_CLASS(poppler_structure_element_parent_class)->finalize(object);
}
static void poppler_structure_element_class_init(PopplerStructureElementClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = poppler_structure_element_finalize;
}
/**
* poppler_structure_element_get_kind:
* @poppler_structure_element: A #PopplerStructureElement
*
* Return value: A #PopplerStructureElementKind value.
*
* Since: 0.26
*/
PopplerStructureElementKind poppler_structure_element_get_kind(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), POPPLER_STRUCTURE_ELEMENT_CONTENT);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, POPPLER_STRUCTURE_ELEMENT_CONTENT);
switch (poppler_structure_element->elem->getType()) {
case StructElement::MCID:
return POPPLER_STRUCTURE_ELEMENT_CONTENT;
case StructElement::OBJR:
return POPPLER_STRUCTURE_ELEMENT_OBJECT_REFERENCE;
case StructElement::Document:
return POPPLER_STRUCTURE_ELEMENT_DOCUMENT;
case StructElement::Part:
return POPPLER_STRUCTURE_ELEMENT_PART;
case StructElement::Art:
return POPPLER_STRUCTURE_ELEMENT_ARTICLE;
case StructElement::Sect:
return POPPLER_STRUCTURE_ELEMENT_SECTION;
case StructElement::Div:
return POPPLER_STRUCTURE_ELEMENT_DIV;
case StructElement::Span:
return POPPLER_STRUCTURE_ELEMENT_SPAN;
case StructElement::Quote:
return POPPLER_STRUCTURE_ELEMENT_QUOTE;
case StructElement::Note:
return POPPLER_STRUCTURE_ELEMENT_NOTE;
case StructElement::Reference:
return POPPLER_STRUCTURE_ELEMENT_REFERENCE;
case StructElement::BibEntry:
return POPPLER_STRUCTURE_ELEMENT_BIBENTRY;
case StructElement::Code:
return POPPLER_STRUCTURE_ELEMENT_CODE;
case StructElement::Link:
return POPPLER_STRUCTURE_ELEMENT_LINK;
case StructElement::Annot:
return POPPLER_STRUCTURE_ELEMENT_ANNOT;
case StructElement::BlockQuote:
return POPPLER_STRUCTURE_ELEMENT_BLOCKQUOTE;
case StructElement::Caption:
return POPPLER_STRUCTURE_ELEMENT_CAPTION;
case StructElement::NonStruct:
return POPPLER_STRUCTURE_ELEMENT_NONSTRUCT;
case StructElement::TOC:
return POPPLER_STRUCTURE_ELEMENT_TOC;
case StructElement::TOCI:
return POPPLER_STRUCTURE_ELEMENT_TOC_ITEM;
case StructElement::Index:
return POPPLER_STRUCTURE_ELEMENT_INDEX;
case StructElement::Private:
return POPPLER_STRUCTURE_ELEMENT_PRIVATE;
case StructElement::P:
return POPPLER_STRUCTURE_ELEMENT_PARAGRAPH;
case StructElement::H:
return POPPLER_STRUCTURE_ELEMENT_HEADING;
case StructElement::H1:
return POPPLER_STRUCTURE_ELEMENT_HEADING_1;
case StructElement::H2:
return POPPLER_STRUCTURE_ELEMENT_HEADING_2;
case StructElement::H3:
return POPPLER_STRUCTURE_ELEMENT_HEADING_3;
case StructElement::H4:
return POPPLER_STRUCTURE_ELEMENT_HEADING_4;
case StructElement::H5:
return POPPLER_STRUCTURE_ELEMENT_HEADING_5;
case StructElement::H6:
return POPPLER_STRUCTURE_ELEMENT_HEADING_6;
case StructElement::L:
return POPPLER_STRUCTURE_ELEMENT_LIST;
case StructElement::LI:
return POPPLER_STRUCTURE_ELEMENT_LIST_ITEM;
case StructElement::Lbl:
return POPPLER_STRUCTURE_ELEMENT_LIST_LABEL;
case StructElement::LBody:
return POPPLER_STRUCTURE_ELEMENT_LIST_BODY;
case StructElement::Table:
return POPPLER_STRUCTURE_ELEMENT_TABLE;
case StructElement::TR:
return POPPLER_STRUCTURE_ELEMENT_TABLE_ROW;
case StructElement::TH:
return POPPLER_STRUCTURE_ELEMENT_TABLE_HEADING;
case StructElement::TD:
return POPPLER_STRUCTURE_ELEMENT_TABLE_DATA;
case StructElement::THead:
return POPPLER_STRUCTURE_ELEMENT_TABLE_HEADER;
case StructElement::TFoot:
return POPPLER_STRUCTURE_ELEMENT_TABLE_FOOTER;
case StructElement::TBody:
return POPPLER_STRUCTURE_ELEMENT_TABLE_BODY;
case StructElement::Ruby:
return POPPLER_STRUCTURE_ELEMENT_RUBY;
case StructElement::RB:
return POPPLER_STRUCTURE_ELEMENT_RUBY_BASE_TEXT;
case StructElement::RT:
return POPPLER_STRUCTURE_ELEMENT_RUBY_ANNOT_TEXT;
case StructElement::RP:
return POPPLER_STRUCTURE_ELEMENT_RUBY_PUNCTUATION;
case StructElement::Warichu:
return POPPLER_STRUCTURE_ELEMENT_WARICHU;
case StructElement::WT:
return POPPLER_STRUCTURE_ELEMENT_WARICHU_TEXT;
case StructElement::WP:
return POPPLER_STRUCTURE_ELEMENT_WARICHU_PUNCTUATION;
case StructElement::Figure:
return POPPLER_STRUCTURE_ELEMENT_FIGURE;
case StructElement::Formula:
return POPPLER_STRUCTURE_ELEMENT_FORMULA;
case StructElement::Form:
return POPPLER_STRUCTURE_ELEMENT_FORM;
/* There should never be elements of type StructElement::Unknown */
case StructElement::Unknown:
g_assert_not_reached();
}
g_assert_not_reached();
return POPPLER_STRUCTURE_ELEMENT_CONTENT;
}
template<typename EnumType>
struct EnumNameValue
{
const gchar *name;
EnumType value;
static const EnumNameValue<EnumType> values[];
static const Attribute::Type attribute_type;
};
#define ENUM_VALUES(E, A) \
template<> \
const Attribute::Type EnumNameValue<E>::attribute_type = Attribute::A; \
template<> \
const EnumNameValue<E> EnumNameValue<E>::values[] =
ENUM_VALUES(PopplerStructurePlacement, Placement) { { "Block", POPPLER_STRUCTURE_PLACEMENT_BLOCK }, { "Inline", POPPLER_STRUCTURE_PLACEMENT_INLINE }, { "Before", POPPLER_STRUCTURE_PLACEMENT_BEFORE },
{ "Start", POPPLER_STRUCTURE_PLACEMENT_START }, { "End", POPPLER_STRUCTURE_PLACEMENT_END }, {} };
ENUM_VALUES(PopplerStructureWritingMode, WritingMode) { { "LrTb", POPPLER_STRUCTURE_WRITING_MODE_LR_TB }, { "RlTb", POPPLER_STRUCTURE_WRITING_MODE_RL_TB }, { "TbRl", POPPLER_STRUCTURE_WRITING_MODE_TB_RL }, {} };
ENUM_VALUES(PopplerStructureBorderStyle, BorderStyle) { { "None", POPPLER_STRUCTURE_BORDER_STYLE_NONE }, { "Hidden", POPPLER_STRUCTURE_BORDER_STYLE_HIDDEN },
{ "Dotted", POPPLER_STRUCTURE_BORDER_STYLE_DOTTED }, { "Dashed", POPPLER_STRUCTURE_BORDER_STYLE_DASHED },
{ "Solid", POPPLER_STRUCTURE_BORDER_STYLE_SOLID }, { "Double", POPPLER_STRUCTURE_BORDER_STYLE_DOUBLE },
{ "Groove", POPPLER_STRUCTURE_BORDER_STYLE_GROOVE }, { "Inset", POPPLER_STRUCTURE_BORDER_STYLE_INSET },
{ "Outset", POPPLER_STRUCTURE_BORDER_STYLE_OUTSET }, {} };
ENUM_VALUES(PopplerStructureTextAlign,
TextAlign) { { "Start", POPPLER_STRUCTURE_TEXT_ALIGN_START }, { "Center", POPPLER_STRUCTURE_TEXT_ALIGN_CENTER }, { "End", POPPLER_STRUCTURE_TEXT_ALIGN_END }, { "Justify", POPPLER_STRUCTURE_TEXT_ALIGN_JUSTIFY }, {} };
ENUM_VALUES(PopplerStructureBlockAlign,
BlockAlign) { { "Before", POPPLER_STRUCTURE_BLOCK_ALIGN_BEFORE }, { "Middle", POPPLER_STRUCTURE_BLOCK_ALIGN_MIDDLE }, { "After", POPPLER_STRUCTURE_BLOCK_ALIGN_AFTER }, { "Justify", POPPLER_STRUCTURE_BLOCK_ALIGN_JUSTIFY }, {} };
ENUM_VALUES(PopplerStructureInlineAlign, InlineAlign) { { "Start", POPPLER_STRUCTURE_INLINE_ALIGN_START }, { "Center", POPPLER_STRUCTURE_INLINE_ALIGN_CENTER }, { "End", POPPLER_STRUCTURE_INLINE_ALIGN_END }, {} };
ENUM_VALUES(PopplerStructureTextDecoration, TextDecorationType) { { "None", POPPLER_STRUCTURE_TEXT_DECORATION_NONE },
{ "Underline", POPPLER_STRUCTURE_TEXT_DECORATION_UNDERLINE },
{ "Overline", POPPLER_STRUCTURE_TEXT_DECORATION_OVERLINE },
{ "LineThrough", POPPLER_STRUCTURE_TEXT_DECORATION_LINETHROUGH },
{} };
ENUM_VALUES(PopplerStructureRubyAlign, RubyAlign) { { "Start", POPPLER_STRUCTURE_RUBY_ALIGN_START }, { "Center", POPPLER_STRUCTURE_RUBY_ALIGN_CENTER }, { "End", POPPLER_STRUCTURE_RUBY_ALIGN_END },
{ "Justify", POPPLER_STRUCTURE_RUBY_ALIGN_JUSTIFY }, { "Distribute", POPPLER_STRUCTURE_RUBY_ALIGN_DISTRIBUTE }, {} };
ENUM_VALUES(PopplerStructureRubyPosition, RubyPosition) {
{ "Before", POPPLER_STRUCTURE_RUBY_POSITION_BEFORE }, { "After", POPPLER_STRUCTURE_RUBY_POSITION_AFTER }, { "Warichu", POPPLER_STRUCTURE_RUBY_POSITION_WARICHU }, { "Inline", POPPLER_STRUCTURE_RUBY_POSITION_INLINE }, {}
};
ENUM_VALUES(PopplerStructureGlyphOrientation, GlyphOrientationVertical) { { "Auto", POPPLER_STRUCTURE_GLYPH_ORIENTATION_AUTO }, { "90", POPPLER_STRUCTURE_GLYPH_ORIENTATION_90 },
{ "180", POPPLER_STRUCTURE_GLYPH_ORIENTATION_180 }, { "270", POPPLER_STRUCTURE_GLYPH_ORIENTATION_270 },
{ "360", POPPLER_STRUCTURE_GLYPH_ORIENTATION_0 }, { "-90", POPPLER_STRUCTURE_GLYPH_ORIENTATION_270 },
{ "-180", POPPLER_STRUCTURE_GLYPH_ORIENTATION_180 }, {} };
ENUM_VALUES(PopplerStructureListNumbering, ListNumbering) { { "None", POPPLER_STRUCTURE_LIST_NUMBERING_NONE },
{ "Disc", POPPLER_STRUCTURE_LIST_NUMBERING_DISC },
{ "Circle", POPPLER_STRUCTURE_LIST_NUMBERING_CIRCLE },
{ "Square", POPPLER_STRUCTURE_LIST_NUMBERING_SQUARE },
{ "Decimal", POPPLER_STRUCTURE_LIST_NUMBERING_DECIMAL },
{ "UpperRoman", POPPLER_STRUCTURE_LIST_NUMBERING_UPPER_ROMAN },
{ "LowerRoman", POPPLER_STRUCTURE_LIST_NUMBERING_LOWER_ROMAN },
{ "UpperAlpha", POPPLER_STRUCTURE_LIST_NUMBERING_UPPER_ALPHA },
{ "LowerAlpha", POPPLER_STRUCTURE_LIST_NUMBERING_LOWER_ALPHA },
{} };
ENUM_VALUES(PopplerStructureFormRole,
Role) { { "rb", POPPLER_STRUCTURE_FORM_ROLE_RADIO_BUTTON }, { "cb", POPPLER_STRUCTURE_FORM_ROLE_CHECKBOX }, { "pb", POPPLER_STRUCTURE_FORM_ROLE_PUSH_BUTTON }, { "tv", POPPLER_STRUCTURE_FORM_ROLE_TEXT_VALUE }, {} };
ENUM_VALUES(PopplerStructureFormState, checked) { { "on", POPPLER_STRUCTURE_FORM_STATE_ON }, { "off", POPPLER_STRUCTURE_FORM_STATE_OFF }, { "neutral", POPPLER_STRUCTURE_FORM_STATE_NEUTRAL }, {} };
ENUM_VALUES(PopplerStructureTableScope, Scope) { { "Row", POPPLER_STRUCTURE_TABLE_SCOPE_ROW }, { "Column", POPPLER_STRUCTURE_TABLE_SCOPE_COLUMN }, { "Both", POPPLER_STRUCTURE_TABLE_SCOPE_BOTH }, {} };
#undef ENUM_VALUES
template<typename EnumType>
static EnumType name_to_enum(Object *name_value)
{
/*
* Non-NULL names must always be valid because Poppler
* discards the invalid attributes when parsing them.
*/
g_assert(name_value != nullptr);
for (const EnumNameValue<EnumType> *item = EnumNameValue<EnumType>::values; item->name; item++)
if (name_value->isName(item->name))
return item->value;
g_assert_not_reached();
return static_cast<EnumType>(-1);
}
template<typename EnumType>
static EnumType attr_to_enum(PopplerStructureElement *poppler_structure_element)
{
const Attribute *attr = poppler_structure_element->elem->findAttribute(EnumNameValue<EnumType>::attribute_type, true);
return name_to_enum<EnumType>((attr != nullptr) ? attr->getValue() : Attribute::getDefaultValue(EnumNameValue<EnumType>::attribute_type));
}
static inline Object *attr_value_or_default(PopplerStructureElement *poppler_structure_element, Attribute::Type attribute_type)
{
const Attribute *attr = poppler_structure_element->elem->findAttribute(attribute_type, true);
return attr ? attr->getValue() : Attribute::getDefaultValue(attribute_type);
}
/**
* poppler_structure_element_get_page:
* @poppler_structure_element: A #PopplerStructureElement
*
* Obtains the page number in which the element is contained.
*
* Return value: Number of the page that contains the element, of
* <code>-1</code> if not defined.
*
* Since: 0.26
*/
gint poppler_structure_element_get_page(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), -1);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, -1);
Ref ref;
if (poppler_structure_element->elem->getPageRef(ref)) {
return poppler_structure_element->document->doc->findPage(ref) - 1;
}
return -1;
}
/**
* poppler_structure_element_is_content:
* @poppler_structure_element: A #PopplerStructureElement
*
* Checks whether an element is actual document content.
*
* Return value: %TRUE if the element is content, or %FALSE otherwise.
*
* Since: 0.26
*/
gboolean poppler_structure_element_is_content(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), FALSE);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, FALSE);
return poppler_structure_element->elem->isContent();
}
/**
* poppler_structure_element_is_inline:
* @poppler_structure_element: A #PopplerStructureElement
*
* Checks whether an element is an inline element.
*
* Return value: %TRUE if the element is an inline element, or %FALSE otherwise.
*
* Since: 0.26
*/
gboolean poppler_structure_element_is_inline(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), FALSE);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, FALSE);
return poppler_structure_element->elem->isInline();
}
/**
* poppler_structure_element_is_block:
* @poppler_structure_element: A #PopplerStructureElement
*
* Checks whether an element is a block element.
*
* Return value: %TRUE if the element is a block element, or %FALSE otherwise.
*
* Since: 0.26
*/
gboolean poppler_structure_element_is_block(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), FALSE);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, FALSE);
return poppler_structure_element->elem->isBlock();
}
/**
* poppler_structure_element_is_grouping:
* @poppler_structure_element: A #PopplerStructureElement
*
* Checks whether an element is a grouping element.
*
* Return value: %TRUE if the element is a grouping element, %FALSE
* otherwise.
*
* Since: 0.26
*/
gboolean poppler_structure_element_is_grouping(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), FALSE);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, FALSE);
return poppler_structure_element->elem->isGrouping();
}
/**
* poppler_structure_element_get_id:
* @poppler_structure_element: A #PopplerStructureElement
*
* Obtains the identifier of an element.
*
* Return value: (transfer full): The identifier of the element (if
* defined), or %NULL.
*
* Since: 0.26
*/
gchar *poppler_structure_element_get_id(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), NULL);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, NULL);
const GooString *string = poppler_structure_element->elem->getID();
return string ? _poppler_goo_string_to_utf8(string) : nullptr;
}
/**
* poppler_structure_element_get_title:
* @poppler_structure_element: A #PopplerStructureElement
*
* Obtains the title of an element.
*
* Return value: (transfer full): The title of the element, or %NULL.
*
* Since: 0.26
*/
gchar *poppler_structure_element_get_title(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), NULL);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, NULL);
const GooString *string = poppler_structure_element->elem->getTitle();
return string ? _poppler_goo_string_to_utf8(string) : nullptr;
}
/**
* poppler_structure_element_get_abbreviation:
* @poppler_structure_element: A #PopplerStructureElement
*
* Acronyms and abbreviations contained in elements of type
* #POPPLER_STRUCTURE_ELEMENT_SPAN may have an associated expanded
* text form, which can be retrieved using this function.
*
* Return value: (transfer full): Text of the expanded abbreviation if the
* element text is an abbreviation or acrony, %NULL if not.
*
* Since: 0.26
*/
gchar *poppler_structure_element_get_abbreviation(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), NULL);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, NULL);
if (poppler_structure_element->elem->getType() != StructElement::Span)
return nullptr;
const GooString *string = poppler_structure_element->elem->getExpandedAbbr();
return string ? _poppler_goo_string_to_utf8(string) : nullptr;
}
/**
* poppler_structure_element_get_language:
* @poppler_structure_element: A #PopplerStructureElement
*
* Obtains the language and country code for the content in an element,
* in two-letter ISO format, e.g. <code>en_ES</code>, or %NULL if not
* defined.
*
* Return value: (transfer full): language and country code, or %NULL.
*
* Since: 0.26
*/
gchar *poppler_structure_element_get_language(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), NULL);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, NULL);
const GooString *string = poppler_structure_element->elem->getLanguage();
return string ? _poppler_goo_string_to_utf8(string) : nullptr;
}
/**
* poppler_structure_element_get_alt_text:
* @poppler_structure_element: A #PopplerStructureElement
*
* Obtains the “alternate” text representation of the element (and its child
* elements). This is mostly used for non-text elements like images and
* figures, to specify a textual description of the element.
*
* Note that for elements containing proper text, the function
* poppler_structure_element_get_text() must be used instead.
*
* Return value: (transfer full): The alternate text representation for the
* element, or %NULL if not defined.
*
* Since: 0.26
*/
gchar *poppler_structure_element_get_alt_text(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), NULL);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, NULL);
const GooString *string = poppler_structure_element->elem->getAltText();
return string ? _poppler_goo_string_to_utf8(string) : nullptr;
}
/**
* poppler_structure_element_get_actual_text:
* @poppler_structure_element: A #PopplerStructureElement
*
* Obtains the actual text enclosed by the element (and its child elements).
* The actual text is mostly used for non-text elements like images and
* figures which <emphasis>do</emphasis> have the graphical appearance of text, like
* a logo. For those the actual text is the equivalent text to those
* graphical elements which look like text when rendered.
*
* Note that for elements containing proper text, the function
* poppler_structure_element_get_text() must be used instead.
*
* Return value: (transfer full): The actual text for the element, or %NULL
* if not defined.
*
* Since: 0.26
*/
gchar *poppler_structure_element_get_actual_text(PopplerStructureElement *poppler_structure_element)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), NULL);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, NULL);
const GooString *string = poppler_structure_element->elem->getActualText();
return string ? _poppler_goo_string_to_utf8(string) : nullptr;
}
/**
* poppler_structure_element_get_text:
* @poppler_structure_element: A #PopplerStructureElement
* @flags: A #PopplerStructureGetTextFlags value, or
* %POPPLER_STRUCTURE_GET_TEXT_NONE to disable all the flags.
*
* Obtains the text enclosed by an element, or the text enclosed by the
* elements in the subtree (including the element itself).
*
* Return value: (transfer full): A string.
*
* Since: 0.26
*/
gchar *poppler_structure_element_get_text(PopplerStructureElement *poppler_structure_element, PopplerStructureGetTextFlags flags)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), NULL);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, NULL);
GooString *string = poppler_structure_element->elem->getText(flags & POPPLER_STRUCTURE_GET_TEXT_RECURSIVE);
gchar *result = string ? _poppler_goo_string_to_utf8(string) : nullptr;
delete string;
return result;
}
struct _PopplerStructureElementIter
{
PopplerDocument *document;
union {
const StructElement *elem;
const StructTreeRoot *root;
};
gboolean is_root;
unsigned index;
};
G_DEFINE_BOXED_TYPE(PopplerStructureElementIter, poppler_structure_element_iter, poppler_structure_element_iter_copy, poppler_structure_element_iter_free)
/**
* poppler_structure_element_iter_copy:
* @iter: a #PopplerStructureElementIter
*
* Creates a new #PopplerStructureElementIter as a copy of @iter. The
* returned value must be freed with poppler_structure_element_iter_free().
*
* Return value: (transfer full): a new #PopplerStructureElementIter
*
* Since: 0.26
*/
PopplerStructureElementIter *poppler_structure_element_iter_copy(PopplerStructureElementIter *iter)
{
PopplerStructureElementIter *new_iter;
g_return_val_if_fail(iter != nullptr, NULL);
new_iter = g_slice_dup(PopplerStructureElementIter, iter);
new_iter->document = (PopplerDocument *)g_object_ref(new_iter->document);
return new_iter;
}
/**
* poppler_structure_element_iter_free:
* @iter: a #PopplerStructureElementIter
*
* Frees @iter.
*
* Since: 0.26
*/
void poppler_structure_element_iter_free(PopplerStructureElementIter *iter)
{
if (G_UNLIKELY(iter == nullptr))
return;
g_object_unref(iter->document);
g_slice_free(PopplerStructureElementIter, iter);
}
/**
* poppler_structure_element_iter_new:
* @poppler_document: a #PopplerDocument.
*
* Returns the root #PopplerStructureElementIter for @document, or %NULL. The
* returned value must be freed with poppler_structure_element_iter_free().
*
* Documents may have an associated structure tree &mdashmostly, Tagged-PDF
* compliant documents— which can be used to obtain information about
* the document structure and its contents. Each node in the tree contains
* a #PopplerStructureElement.
*
* Here is a simple example that walks the whole tree:
*
* <informalexample><programlisting>
* static void
* walk_structure (PopplerStructureElementIter *iter)
* {
* do {
* /<!-- -->* Get the element and do something with it *<!-- -->/
* PopplerStructureElementIter *child = poppler_structure_element_iter_get_child (iter);
* if (child)
* walk_structure (child);
* poppler_structure_element_iter_free (child);
* } while (poppler_structure_element_iter_next (iter));
* }
* ...
* {
* iter = poppler_structure_element_iter_new (document);
* walk_structure (iter);
* poppler_structure_element_iter_free (iter);
* }
* </programlisting></informalexample>
*
* Return value: (transfer full): a new #PopplerStructureElementIter, or %NULL if document
* doesn't have structure tree.
*
* Since: 0.26
*/
PopplerStructureElementIter *poppler_structure_element_iter_new(PopplerDocument *poppler_document)
{
PopplerStructureElementIter *iter;
g_return_val_if_fail(POPPLER_IS_DOCUMENT(poppler_document), NULL);
const StructTreeRoot *root = poppler_document->doc->getStructTreeRoot();
if (root == nullptr)
return nullptr;
if (root->getNumChildren() == 0)
return nullptr;
iter = g_slice_new0(PopplerStructureElementIter);
iter->document = (PopplerDocument *)g_object_ref(poppler_document);
iter->is_root = TRUE;
iter->root = root;
return iter;
}
/**
* poppler_structure_element_iter_next:
* @iter: a #PopplerStructureElementIter
*
* Sets @iter to point to the next structure element at the current level
* of the tree, if valid. See poppler_structure_element_iter_new() for more
* information.
*
* Return value: %TRUE, if @iter was set to the next structure element
*
* Since: 0.26
*/
gboolean poppler_structure_element_iter_next(PopplerStructureElementIter *iter)
{
unsigned elements;
g_return_val_if_fail(iter != nullptr, FALSE);
elements = iter->is_root ? iter->root->getNumChildren() : iter->elem->getNumChildren();
return ++iter->index < elements;
}
/**
* poppler_structure_element_iter_get_element:
* @iter: a #PopplerStructureElementIter
*
* Returns the #PopplerStructureElementIter associated with @iter.
*
* Return value: (transfer full): a new #PopplerStructureElementIter
*
* Since: 0.26
*/
PopplerStructureElement *poppler_structure_element_iter_get_element(PopplerStructureElementIter *iter)
{
g_return_val_if_fail(iter != nullptr, NULL);
const StructElement *elem = iter->is_root ? iter->root->getChild(iter->index) : iter->elem->getChild(iter->index);
return _poppler_structure_element_new(iter->document, elem);
}
/**
* poppler_structure_element_iter_get_child:
* @parent: a #PopplerStructureElementIter
*
* Returns a new iterator to the children elements of the
* #PopplerStructureElement associated with @iter. The returned value must
* be freed with poppler_structure_element_iter_free().
*
* Return value: a new #PopplerStructureElementIter
*
* Since: 0.26
*/
PopplerStructureElementIter *poppler_structure_element_iter_get_child(PopplerStructureElementIter *parent)
{
const StructElement *elem;
g_return_val_if_fail(parent != nullptr, NULL);
elem = parent->is_root ? parent->root->getChild(parent->index) : parent->elem->getChild(parent->index);
if (elem->getNumChildren() > 0) {
PopplerStructureElementIter *child = g_slice_new0(PopplerStructureElementIter);
child->document = (PopplerDocument *)g_object_ref(parent->document);
child->elem = elem;
return child;
}
return nullptr;
}
struct _PopplerTextSpan
{
gchar *text;
gchar *font_name;
guint flags;
PopplerColor color;
};
G_DEFINE_BOXED_TYPE(PopplerTextSpan, poppler_text_span, poppler_text_span_copy, poppler_text_span_free)
enum
{
POPPLER_TEXT_SPAN_FIXED_WIDTH = (1 << 0),
POPPLER_TEXT_SPAN_SERIF = (1 << 1),
POPPLER_TEXT_SPAN_ITALIC = (1 << 2),
POPPLER_TEXT_SPAN_BOLD = (1 << 3),
};
static PopplerTextSpan *text_span_poppler_text_span(const TextSpan &span)
{
PopplerTextSpan *new_span = g_slice_new0(PopplerTextSpan);
if (GooString *text = span.getText())
new_span->text = _poppler_goo_string_to_utf8(text);
new_span->color.red = colToDbl(span.getColor().r) * 65535;
new_span->color.green = colToDbl(span.getColor().g) * 65535;
new_span->color.blue = colToDbl(span.getColor().b) * 65535;
if (span.getFont()) {
// GfxFont sometimes does not have a family name but there
// is always a font name that can be used as fallback.
const GooString *font_name = span.getFont()->getFamily();
if (font_name == nullptr)
font_name = span.getFont()->getName();
new_span->font_name = _poppler_goo_string_to_utf8(font_name);
if (span.getFont()->isFixedWidth())
new_span->flags |= POPPLER_TEXT_SPAN_FIXED_WIDTH;
if (span.getFont()->isSerif())
new_span->flags |= POPPLER_TEXT_SPAN_SERIF;
if (span.getFont()->isItalic())
new_span->flags |= POPPLER_TEXT_SPAN_ITALIC;
if (span.getFont()->isBold())
new_span->flags |= POPPLER_TEXT_SPAN_BOLD;
/* isBold() can return false for some fonts whose weight is heavy */
switch (span.getFont()->getWeight()) {
case GfxFont::W500:
case GfxFont::W600:
case GfxFont::W700:
case GfxFont::W800:
case GfxFont::W900:
new_span->flags |= POPPLER_TEXT_SPAN_BOLD;
default:
break;
}
}
return new_span;
}
/**
* poppler_text_span_copy:
* @poppler_text_span: a #PopplerTextSpan
*
* Makes a copy of a text span.
*
* Return value: (transfer full): A new #PopplerTextSpan
*
* Since: 0.26
*/
PopplerTextSpan *poppler_text_span_copy(PopplerTextSpan *poppler_text_span)
{
PopplerTextSpan *new_span;
g_return_val_if_fail(poppler_text_span != nullptr, NULL);
new_span = g_slice_dup(PopplerTextSpan, poppler_text_span);
new_span->text = g_strdup(poppler_text_span->text);
if (poppler_text_span->font_name)
new_span->font_name = g_strdup(poppler_text_span->font_name);
return new_span;
}
/**
* poppler_text_span_free:
* @poppler_text_span: A #PopplerTextSpan
*
* Frees a text span.
*
* Since: 0.26
*/
void poppler_text_span_free(PopplerTextSpan *poppler_text_span)
{
if (G_UNLIKELY(poppler_text_span == nullptr))
return;
g_free(poppler_text_span->text);
g_free(poppler_text_span->font_name);
g_slice_free(PopplerTextSpan, poppler_text_span);
}
/**
* poppler_text_span_is_fixed_width_font:
* @poppler_text_span: a #PopplerTextSpan
*
* Check wether a text span is meant to be rendered using a fixed-width font.
*
* Return value: Whether the span uses a fixed-width font.
*
* Since: 0.26
*/
gboolean poppler_text_span_is_fixed_width_font(PopplerTextSpan *poppler_text_span)
{
g_return_val_if_fail(poppler_text_span != nullptr, FALSE);
return (poppler_text_span->flags & POPPLER_TEXT_SPAN_FIXED_WIDTH);
}
/**
* poppler_text_span_is_serif_font:
* @poppler_text_span: a #PopplerTextSpan
*
* Check whether a text span is meant to be rendered using a serif font.
*
* Return value: Whether the span uses a serif font.
*
* Since: 0.26
*/
gboolean poppler_text_span_is_serif_font(PopplerTextSpan *poppler_text_span)
{
g_return_val_if_fail(poppler_text_span != nullptr, FALSE);
return (poppler_text_span->flags & POPPLER_TEXT_SPAN_SERIF);
}
/**
* poppler_text_span_is_bold_font:
* @poppler_text_span: a #PopplerTextSpan
*
* Check whether a text span is meant to be rendered using a bold font.
*
* Return value: Whether the span uses bold font.
*
* Since: 0.26
*/
gboolean poppler_text_span_is_bold_font(PopplerTextSpan *poppler_text_span)
{
g_return_val_if_fail(poppler_text_span != nullptr, FALSE);
return (poppler_text_span->flags & POPPLER_TEXT_SPAN_BOLD);
}
/**
* poppler_text_span_get_color:
* @poppler_text_span: a #PopplerTextSpan
* @color: (out): a return location for a #PopplerColor
*
* Obtains the color in which the text is to be rendered.
*
* Since: 0.26
*/
void poppler_text_span_get_color(PopplerTextSpan *poppler_text_span, PopplerColor *color)
{
g_return_if_fail(poppler_text_span != nullptr);
g_return_if_fail(color != nullptr);
*color = poppler_text_span->color;
}
/**
* poppler_text_span_get_text:
* @poppler_text_span: a #PopplerTextSpan
*
* Obtains the text contained in the span.
*
* Return value: (transfer none): A string.
*
* Since: 0.26
*/
const gchar *poppler_text_span_get_text(PopplerTextSpan *poppler_text_span)
{
g_return_val_if_fail(poppler_text_span != nullptr, NULL);
return poppler_text_span->text;
}
/**
* poppler_text_span_get_font_name:
* @poppler_text_span: a #PopplerTextSpan
*
* Obtains the name of the font in which the span is to be rendered.
*
* Return value: (transfer none): A string containing the font name, or
* %NULL if a font is not defined.
*
* Since: 0.26
*/
const gchar *poppler_text_span_get_font_name(PopplerTextSpan *poppler_text_span)
{
g_return_val_if_fail(poppler_text_span != nullptr, NULL);
return poppler_text_span->font_name;
}
/**
* poppler_structure_element_get_text_spans:
* @poppler_structure_element: A #PopplerStructureElement
* @n_text_spans: (out): A pointer to the location where the number of elements in
* the returned array will be stored.
*
* Obtains the text enclosed by an element, as an array of #PopplerTextSpan
* structures. Each item in the list is a piece of text which share the same
* attributes, plus its attributes. The following example shows how to
* obtain and free the text spans of an element:
*
* <informalexample><programlisting>
* guint i, n_spans;
* PopplerTextSpan **text_spans =
* poppler_structure_element_get_text_spans (element, &n_spans);
* /<!-- -->* Use the text spans *<!-- -->/
* for (i = 0; i < n_spans; i++)
* poppler_text_span_free (text_spans[i]);
* g_free (text_spans);
* </programlisting></informalexample>
*
* Return value: (transfer full) (array length=n_text_spans) (element-type PopplerTextSpan):
* An array of #PopplerTextSpan elements.
*
* Since: 0.26
*/
PopplerTextSpan **poppler_structure_element_get_text_spans(PopplerStructureElement *poppler_structure_element, guint *n_text_spans)
{
g_return_val_if_fail(POPPLER_IS_STRUCTURE_ELEMENT(poppler_structure_element), NULL);
g_return_val_if_fail(n_text_spans != nullptr, NULL);
g_return_val_if_fail(poppler_structure_element->elem != nullptr, NULL);
if (!poppler_structure_element->elem->isContent())
return nullptr;
const TextSpanArray spans(poppler_structure_element->elem->getTextSpans());
PopplerTextSpan **text_spans = g_new0(PopplerTextSpan *, spans.size());