pdfium-native 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,164 @@
1
+ #pragma once
2
+
3
+ #include "napi_helpers.h"
4
+
5
+ #include <atomic>
6
+ #include <memory>
7
+ #include <string>
8
+ #include <vector>
9
+
10
+ #include "fpdf_structtree.h"
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // GetStructTreeWorker — async tagged-PDF structure tree extraction
14
+ // ---------------------------------------------------------------------------
15
+ // The structure tree is what makes a PDF "tagged": the logical document
16
+ // outline (headings, paragraphs, tables, figures) sitting behind the visual
17
+ // layout. It is what screen readers follow, and it carries the alternate text
18
+ // that accessibility audits check for. `metadata.isTagged` already reports
19
+ // whether a document has one; this reads it.
20
+ //
21
+ // The tree is per-page: PDFium hands back the elements whose content lives on
22
+ // the requested page, not the whole document's tree.
23
+
24
+ // maximum structure tree recursion depth, mirroring MAX_BOOKMARK_DEPTH
25
+ constexpr int MAX_STRUCT_DEPTH = 64;
26
+
27
+ // read one of the struct element's UTF-16LE string properties
28
+ template <typename Fn> inline std::u16string ReadStructString(Fn fn) {
29
+ return ReadU16([&](auto *, unsigned long) { return fn(nullptr, 0); },
30
+ [&](FPDF_WCHAR *buf, unsigned long len) {
31
+ return fn(buf, len);
32
+ });
33
+ }
34
+
35
+ struct StructElementInfo {
36
+ std::u16string type;
37
+ std::u16string objType;
38
+ std::u16string title;
39
+ std::u16string altText;
40
+ std::u16string actualText;
41
+ std::u16string id;
42
+ std::u16string lang;
43
+ int markedContentId = -1;
44
+ std::vector<StructElementInfo> children;
45
+ };
46
+
47
+ class GetStructTreeWorker : public SafeAsyncWorker {
48
+ public:
49
+ GetStructTreeWorker(Napi::Env env, FPDF_PAGE page,
50
+ std::shared_ptr<std::atomic<bool>> pageAlive,
51
+ std::shared_ptr<std::atomic<bool>> docAlive)
52
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
53
+ page_(page), pageAlive_(std::move(pageAlive)),
54
+ docAlive_(std::move(docAlive)) {}
55
+
56
+ Napi::Promise Promise() { return deferred_.Promise(); }
57
+
58
+ protected:
59
+ void Execute() override {
60
+ std::lock_guard<std::mutex> lock(g_pdfium_mutex);
61
+ CHECK_ALIVE();
62
+
63
+ FPDF_STRUCTTREE tree = FPDF_StructTree_GetForPage(page_);
64
+ if (!tree)
65
+ return; // untagged page — an empty tree, not an error
66
+
67
+ int count = FPDF_StructTree_CountChildren(tree);
68
+ for (int i = 0; i < count; i++) {
69
+ FPDF_STRUCTELEMENT element = FPDF_StructTree_GetChildAtIndex(tree, i);
70
+ if (!element)
71
+ continue;
72
+ roots_.push_back(ReadElement(element, 0));
73
+ }
74
+
75
+ FPDF_StructTree_Close(tree);
76
+ }
77
+
78
+ void OnOK() override {
79
+ Napi::Env env = Env();
80
+ deferred_.Resolve(BuildArray(env, roots_));
81
+ }
82
+
83
+ void OnError(const Napi::Error &err) override {
84
+ deferred_.Reject(err.Value());
85
+ }
86
+
87
+ private:
88
+ StructElementInfo ReadElement(FPDF_STRUCTELEMENT element, int depth) {
89
+ StructElementInfo info;
90
+
91
+ info.type = ReadStructString([&](FPDF_WCHAR *buf, unsigned long len) {
92
+ return FPDF_StructElement_GetType(element, buf, len);
93
+ });
94
+ info.objType = ReadStructString([&](FPDF_WCHAR *buf, unsigned long len) {
95
+ return FPDF_StructElement_GetObjType(element, buf, len);
96
+ });
97
+ info.title = ReadStructString([&](FPDF_WCHAR *buf, unsigned long len) {
98
+ return FPDF_StructElement_GetTitle(element, buf, len);
99
+ });
100
+ info.altText = ReadStructString([&](FPDF_WCHAR *buf, unsigned long len) {
101
+ return FPDF_StructElement_GetAltText(element, buf, len);
102
+ });
103
+ info.actualText = ReadStructString([&](FPDF_WCHAR *buf, unsigned long len) {
104
+ return FPDF_StructElement_GetActualText(element, buf, len);
105
+ });
106
+ info.id = ReadStructString([&](FPDF_WCHAR *buf, unsigned long len) {
107
+ return FPDF_StructElement_GetID(element, buf, len);
108
+ });
109
+ info.lang = ReadStructString([&](FPDF_WCHAR *buf, unsigned long len) {
110
+ return FPDF_StructElement_GetLang(element, buf, len);
111
+ });
112
+
113
+ info.markedContentId = FPDF_StructElement_GetMarkedContentID(element);
114
+
115
+ // A malformed or hostile PDF can nest structure elements arbitrarily deep,
116
+ // or cycle; stop descending rather than overflow the stack.
117
+ if (depth >= MAX_STRUCT_DEPTH)
118
+ return info;
119
+
120
+ int childCount = FPDF_StructElement_CountChildren(element);
121
+ for (int i = 0; i < childCount; i++) {
122
+ // returns null for children that are content references rather than
123
+ // elements, which is expected and simply skipped
124
+ FPDF_STRUCTELEMENT child =
125
+ FPDF_StructElement_GetChildAtIndex(element, i);
126
+ if (!child)
127
+ continue;
128
+ info.children.push_back(ReadElement(child, depth + 1));
129
+ }
130
+
131
+ return info;
132
+ }
133
+
134
+ Napi::Array BuildArray(Napi::Env env,
135
+ const std::vector<StructElementInfo> &items) {
136
+ Napi::Array arr = Napi::Array::New(env, items.size());
137
+ for (uint32_t i = 0; i < items.size(); i++) {
138
+ const auto &info = items[i];
139
+ Napi::Object obj = Napi::Object::New(env);
140
+ SetU16(obj, "type", env, info.type);
141
+ SetU16IfPresent(obj, "objType", env, info.objType);
142
+ SetU16IfPresent(obj, "title", env, info.title);
143
+ SetU16IfPresent(obj, "altText", env, info.altText);
144
+ SetU16IfPresent(obj, "actualText", env, info.actualText);
145
+ SetU16IfPresent(obj, "id", env, info.id);
146
+ SetU16IfPresent(obj, "lang", env, info.lang);
147
+ if (info.markedContentId >= 0) {
148
+ obj.Set("markedContentId",
149
+ Napi::Number::New(env, info.markedContentId));
150
+ }
151
+ if (!info.children.empty()) {
152
+ obj.Set("children", BuildArray(env, info.children));
153
+ }
154
+ arr.Set(i, obj);
155
+ }
156
+ return arr;
157
+ }
158
+
159
+ Napi::Promise::Deferred deferred_;
160
+ FPDF_PAGE page_;
161
+ std::shared_ptr<std::atomic<bool>> pageAlive_;
162
+ std::shared_ptr<std::atomic<bool>> docAlive_;
163
+ std::vector<StructElementInfo> roots_;
164
+ };