pdfium-native 0.2.2 → 0.2.4
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.
- package/README.md +114 -27
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/page.d.ts +13 -3
- package/dist/page.d.ts.map +1 -1
- package/dist/page.js +22 -3
- package/dist/page.js.map +1 -1
- package/dist/types.d.ts +213 -7
- package/dist/types.d.ts.map +1 -1
- package/lib/index.ts +9 -2
- package/lib/page.ts +22 -4
- package/lib/types.ts +256 -7
- package/package.json +2 -2
- package/scripts/generate-fixtures.mjs +308 -1
- package/scripts/install.mjs +4 -3
- package/src/annotations_worker.h +274 -0
- package/src/bookmarks_worker.h +162 -0
- package/src/document.h +47 -4
- package/src/form_fields_worker.h +250 -0
- package/src/links_worker.h +158 -0
- package/src/napi_helpers.h +74 -0
- package/src/objects_worker.h +322 -0
- package/src/page.h +51 -5
- package/src/pdfium_addon.cc +93 -17
- package/src/render_worker.h +37 -17
- package/src/search_worker.h +121 -0
- package/src/text_worker.h +69 -0
- package/src/page_workers.h +0 -724
|
@@ -0,0 +1,162 @@
|
|
|
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_doc.h"
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// GetBookmarksWorker — async bookmark extraction
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
struct BookmarkData {
|
|
17
|
+
std::u16string title;
|
|
18
|
+
int pageIndex = -1;
|
|
19
|
+
bool hasPageIndex = false;
|
|
20
|
+
bool open = false;
|
|
21
|
+
std::string actionType;
|
|
22
|
+
std::string url;
|
|
23
|
+
float destX = 0, destY = 0, destZoom = 0;
|
|
24
|
+
bool hasDestX = false, hasDestY = false, hasDestZoom = false;
|
|
25
|
+
std::vector<BookmarkData> children;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
class GetBookmarksWorker : public Napi::AsyncWorker {
|
|
29
|
+
public:
|
|
30
|
+
GetBookmarksWorker(Napi::Env env, FPDF_DOCUMENT doc,
|
|
31
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
32
|
+
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
33
|
+
doc_(doc), docAlive_(std::move(docAlive)) {}
|
|
34
|
+
|
|
35
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
36
|
+
|
|
37
|
+
protected:
|
|
38
|
+
void Execute() override {
|
|
39
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
40
|
+
if (!docAlive_->load()) {
|
|
41
|
+
SetError("Document was destroyed");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
CollectBookmarks(nullptr, bookmarks_, 0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
void OnOK() override { deferred_.Resolve(BuildArray(Env(), bookmarks_)); }
|
|
48
|
+
|
|
49
|
+
void OnError(const Napi::Error &err) override {
|
|
50
|
+
deferred_.Reject(err.Value());
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private:
|
|
54
|
+
Napi::Promise::Deferred deferred_;
|
|
55
|
+
FPDF_DOCUMENT doc_;
|
|
56
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
57
|
+
std::vector<BookmarkData> bookmarks_;
|
|
58
|
+
|
|
59
|
+
// maximum siblings per level to prevent DoS from malicious PDFs
|
|
60
|
+
static constexpr int MAX_SIBLINGS_PER_LEVEL = 10000;
|
|
61
|
+
|
|
62
|
+
void CollectBookmarks(FPDF_BOOKMARK parent, std::vector<BookmarkData> &out,
|
|
63
|
+
int depth) {
|
|
64
|
+
if (depth >= MAX_BOOKMARK_DEPTH)
|
|
65
|
+
return;
|
|
66
|
+
|
|
67
|
+
int siblingCount = 0;
|
|
68
|
+
FPDF_BOOKMARK child = FPDFBookmark_GetFirstChild(doc_, parent);
|
|
69
|
+
while (child && siblingCount < MAX_SIBLINGS_PER_LEVEL) {
|
|
70
|
+
siblingCount++;
|
|
71
|
+
BookmarkData data;
|
|
72
|
+
|
|
73
|
+
data.title = ReadU16(
|
|
74
|
+
[&](auto *, unsigned long) {
|
|
75
|
+
return FPDFBookmark_GetTitle(child, nullptr, 0);
|
|
76
|
+
},
|
|
77
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
78
|
+
return FPDFBookmark_GetTitle(child, buf, len);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// open/closed state: positive count = open, negative = closed
|
|
82
|
+
int count = FPDFBookmark_GetCount(child);
|
|
83
|
+
data.open = count > 0;
|
|
84
|
+
|
|
85
|
+
FPDF_DEST dest = FPDFBookmark_GetDest(doc_, child);
|
|
86
|
+
FPDF_ACTION action = FPDFBookmark_GetAction(child);
|
|
87
|
+
|
|
88
|
+
if (!dest && action) {
|
|
89
|
+
dest = FPDFAction_GetDest(doc_, action);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (dest) {
|
|
93
|
+
data.hasPageIndex = true;
|
|
94
|
+
data.pageIndex = FPDFDest_GetDestPageIndex(doc_, dest);
|
|
95
|
+
|
|
96
|
+
FPDF_BOOL hasX, hasY, hasZoom;
|
|
97
|
+
FS_FLOAT x, y, zoom;
|
|
98
|
+
if (FPDFDest_GetLocationInPage(dest, &hasX, &hasY, &hasZoom, &x, &y,
|
|
99
|
+
&zoom)) {
|
|
100
|
+
if (hasX) {
|
|
101
|
+
data.hasDestX = true;
|
|
102
|
+
data.destX = x;
|
|
103
|
+
}
|
|
104
|
+
if (hasY) {
|
|
105
|
+
data.hasDestY = true;
|
|
106
|
+
data.destY = y;
|
|
107
|
+
}
|
|
108
|
+
if (hasZoom) {
|
|
109
|
+
data.hasDestZoom = true;
|
|
110
|
+
data.destZoom = zoom;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (action) {
|
|
116
|
+
unsigned long actionType = FPDFAction_GetType(action);
|
|
117
|
+
data.actionType = ActionTypeString(actionType);
|
|
118
|
+
|
|
119
|
+
if (actionType == PDFACTION_URI) {
|
|
120
|
+
unsigned long uriLen =
|
|
121
|
+
FPDFAction_GetURIPath(doc_, action, nullptr, 0);
|
|
122
|
+
if (uriLen > 0) {
|
|
123
|
+
std::vector<char> uriBuf(uriLen);
|
|
124
|
+
FPDFAction_GetURIPath(doc_, action, uriBuf.data(), uriLen);
|
|
125
|
+
data.url = std::string(uriBuf.data(), uriLen - 1);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
CollectBookmarks(child, data.children, depth + 1);
|
|
131
|
+
out.push_back(std::move(data));
|
|
132
|
+
child = FPDFBookmark_GetNextSibling(doc_, child);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
Napi::Array BuildArray(Napi::Env env,
|
|
137
|
+
const std::vector<BookmarkData> &items) {
|
|
138
|
+
Napi::Array arr = Napi::Array::New(env, items.size());
|
|
139
|
+
for (uint32_t i = 0; i < items.size(); i++) {
|
|
140
|
+
auto &d = items[i];
|
|
141
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
142
|
+
SetU16(obj, "title", env, d.title);
|
|
143
|
+
if (d.hasPageIndex)
|
|
144
|
+
obj.Set("pageIndex", Napi::Number::New(env, d.pageIndex));
|
|
145
|
+
obj.Set("open", Napi::Boolean::New(env, d.open));
|
|
146
|
+
if (!d.actionType.empty())
|
|
147
|
+
obj.Set("actionType", Napi::String::New(env, d.actionType));
|
|
148
|
+
if (!d.url.empty())
|
|
149
|
+
obj.Set("url", Napi::String::New(env, d.url));
|
|
150
|
+
if (d.hasDestX)
|
|
151
|
+
obj.Set("destX", Napi::Number::New(env, d.destX));
|
|
152
|
+
if (d.hasDestY)
|
|
153
|
+
obj.Set("destY", Napi::Number::New(env, d.destY));
|
|
154
|
+
if (d.hasDestZoom)
|
|
155
|
+
obj.Set("destZoom", Napi::Number::New(env, d.destZoom));
|
|
156
|
+
if (!d.children.empty())
|
|
157
|
+
obj.Set("children", BuildArray(env, d.children));
|
|
158
|
+
arr.Set(i, obj);
|
|
159
|
+
}
|
|
160
|
+
return arr;
|
|
161
|
+
}
|
|
162
|
+
};
|
package/src/document.h
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
|
+
#include "bookmarks_worker.h"
|
|
3
4
|
#include "page.h"
|
|
4
5
|
|
|
5
6
|
#include <atomic>
|
|
6
7
|
#include <memory>
|
|
7
8
|
|
|
9
|
+
#include "fpdf_transformpage.h"
|
|
10
|
+
|
|
8
11
|
// ---------------------------------------------------------------------------
|
|
9
12
|
// PDFiumDocument
|
|
10
13
|
// ---------------------------------------------------------------------------
|
|
@@ -143,6 +146,34 @@ protected:
|
|
|
143
146
|
width_ = FPDF_GetPageWidthF(page_);
|
|
144
147
|
height_ = FPDF_GetPageHeightF(page_);
|
|
145
148
|
objectCount_ = FPDFPage_CountObjects(page_);
|
|
149
|
+
rotation_ = FPDFPage_GetRotation(page_);
|
|
150
|
+
hasTransparency_ = FPDFPage_HasTransparency(page_) != 0;
|
|
151
|
+
|
|
152
|
+
// page label (UTF-16LE)
|
|
153
|
+
label_ = ReadU16(
|
|
154
|
+
[&](auto *, unsigned long) {
|
|
155
|
+
return FPDF_GetPageLabel(doc_, pageIndex_, nullptr, 0);
|
|
156
|
+
},
|
|
157
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
158
|
+
return FPDF_GetPageLabel(doc_, pageIndex_, buf, len);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// optional page boxes
|
|
162
|
+
float l, b, r, t;
|
|
163
|
+
if (FPDFPage_GetCropBox(page_, &l, &b, &r, &t)) {
|
|
164
|
+
hasCropBox_ = true;
|
|
165
|
+
cropLeft_ = l;
|
|
166
|
+
cropBottom_ = b;
|
|
167
|
+
cropRight_ = r;
|
|
168
|
+
cropTop_ = t;
|
|
169
|
+
}
|
|
170
|
+
if (FPDFPage_GetTrimBox(page_, &l, &b, &r, &t)) {
|
|
171
|
+
hasTrimBox_ = true;
|
|
172
|
+
trimLeft_ = l;
|
|
173
|
+
trimBottom_ = b;
|
|
174
|
+
trimRight_ = r;
|
|
175
|
+
trimTop_ = t;
|
|
176
|
+
}
|
|
146
177
|
}
|
|
147
178
|
|
|
148
179
|
void OnOK() override {
|
|
@@ -158,11 +189,16 @@ protected:
|
|
|
158
189
|
// set dimensions as plain JS properties (no native roundtrip on access)
|
|
159
190
|
pageObj.Set("width", Napi::Number::New(env, width_));
|
|
160
191
|
pageObj.Set("height", Napi::Number::New(env, height_));
|
|
161
|
-
Napi::Object sizeObj = Napi::Object::New(env);
|
|
162
|
-
sizeObj.Set("width", Napi::Number::New(env, width_));
|
|
163
|
-
sizeObj.Set("height", Napi::Number::New(env, height_));
|
|
164
|
-
pageObj.Set("size", sizeObj);
|
|
165
192
|
pageObj.Set("objectCount", Napi::Number::New(env, objectCount_));
|
|
193
|
+
pageObj.Set("rotation", Napi::Number::New(env, rotation_));
|
|
194
|
+
pageObj.Set("hasTransparency", Napi::Boolean::New(env, hasTransparency_));
|
|
195
|
+
SetU16IfPresent(pageObj, "label", env, label_);
|
|
196
|
+
if (hasCropBox_)
|
|
197
|
+
pageObj.Set("cropBox", CreateBoundsObject(env, cropLeft_, cropBottom_,
|
|
198
|
+
cropRight_, cropTop_));
|
|
199
|
+
if (hasTrimBox_)
|
|
200
|
+
pageObj.Set("trimBox", CreateBoundsObject(env, trimLeft_, trimBottom_,
|
|
201
|
+
trimRight_, trimTop_));
|
|
166
202
|
|
|
167
203
|
deferred_.Resolve(pageObj);
|
|
168
204
|
}
|
|
@@ -181,6 +217,13 @@ private:
|
|
|
181
217
|
float width_ = 0;
|
|
182
218
|
float height_ = 0;
|
|
183
219
|
int objectCount_ = 0;
|
|
220
|
+
int rotation_ = 0;
|
|
221
|
+
bool hasTransparency_ = false;
|
|
222
|
+
std::u16string label_;
|
|
223
|
+
bool hasCropBox_ = false;
|
|
224
|
+
float cropLeft_ = 0, cropBottom_ = 0, cropRight_ = 0, cropTop_ = 0;
|
|
225
|
+
bool hasTrimBox_ = false;
|
|
226
|
+
float trimLeft_ = 0, trimBottom_ = 0, trimRight_ = 0, trimTop_ = 0;
|
|
184
227
|
};
|
|
185
228
|
|
|
186
229
|
// deferred definition of GetPage (needs GetPageWorker)
|
|
@@ -0,0 +1,250 @@
|
|
|
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_annot.h"
|
|
11
|
+
#include "fpdf_formfill.h"
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// GetFormFieldsWorker — async form field extraction
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
struct FormFieldOptionData {
|
|
18
|
+
std::u16string label;
|
|
19
|
+
bool isSelected = false;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
struct FormFieldData {
|
|
23
|
+
std::string type;
|
|
24
|
+
std::u16string name;
|
|
25
|
+
std::u16string value;
|
|
26
|
+
std::u16string alternateName;
|
|
27
|
+
std::u16string exportValue;
|
|
28
|
+
int flags = 0;
|
|
29
|
+
double left = 0, bottom = 0, right = 0, top = 0;
|
|
30
|
+
bool hasBounds = false;
|
|
31
|
+
bool isChecked = false;
|
|
32
|
+
std::vector<FormFieldOptionData> options;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
class GetFormFieldsWorker : public Napi::AsyncWorker {
|
|
36
|
+
public:
|
|
37
|
+
GetFormFieldsWorker(Napi::Env env, FPDF_PAGE page, FPDF_DOCUMENT doc,
|
|
38
|
+
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
39
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
40
|
+
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
41
|
+
page_(page), doc_(doc), pageAlive_(std::move(pageAlive)),
|
|
42
|
+
docAlive_(std::move(docAlive)) {}
|
|
43
|
+
|
|
44
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
45
|
+
|
|
46
|
+
protected:
|
|
47
|
+
void Execute() override {
|
|
48
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
49
|
+
CHECK_ALIVE();
|
|
50
|
+
|
|
51
|
+
// create a temporary form fill environment for read-only field access
|
|
52
|
+
FPDF_FORMFILLINFO formFillInfo{};
|
|
53
|
+
formFillInfo.version = 1;
|
|
54
|
+
FPDF_FORMHANDLE formHandle =
|
|
55
|
+
FPDFDOC_InitFormFillEnvironment(doc_, &formFillInfo);
|
|
56
|
+
if (!formHandle) {
|
|
57
|
+
SetError("Failed to initialize form fill environment");
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
int count = FPDFPage_GetAnnotCount(page_);
|
|
62
|
+
for (int i = 0; i < count; i++) {
|
|
63
|
+
FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page_, i);
|
|
64
|
+
if (!annot)
|
|
65
|
+
continue;
|
|
66
|
+
|
|
67
|
+
// only process widget annotations (form fields)
|
|
68
|
+
if (FPDFAnnot_GetSubtype(annot) != FPDF_ANNOT_WIDGET) {
|
|
69
|
+
FPDFPage_CloseAnnot(annot);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
FormFieldData data;
|
|
74
|
+
|
|
75
|
+
// field type
|
|
76
|
+
int fieldType = FPDFAnnot_GetFormFieldType(formHandle, annot);
|
|
77
|
+
switch (fieldType) {
|
|
78
|
+
case FPDF_FORMFIELD_PUSHBUTTON:
|
|
79
|
+
data.type = "pushButton";
|
|
80
|
+
break;
|
|
81
|
+
case FPDF_FORMFIELD_CHECKBOX:
|
|
82
|
+
data.type = "checkbox";
|
|
83
|
+
break;
|
|
84
|
+
case FPDF_FORMFIELD_RADIOBUTTON:
|
|
85
|
+
data.type = "radioButton";
|
|
86
|
+
break;
|
|
87
|
+
case FPDF_FORMFIELD_COMBOBOX:
|
|
88
|
+
data.type = "comboBox";
|
|
89
|
+
break;
|
|
90
|
+
case FPDF_FORMFIELD_LISTBOX:
|
|
91
|
+
data.type = "listBox";
|
|
92
|
+
break;
|
|
93
|
+
case FPDF_FORMFIELD_TEXTFIELD:
|
|
94
|
+
data.type = "textField";
|
|
95
|
+
break;
|
|
96
|
+
case FPDF_FORMFIELD_SIGNATURE:
|
|
97
|
+
data.type = "signature";
|
|
98
|
+
break;
|
|
99
|
+
default:
|
|
100
|
+
data.type = "unknown";
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// field name (UTF-16LE)
|
|
105
|
+
data.name = ReadU16(
|
|
106
|
+
[&](auto *, unsigned long) {
|
|
107
|
+
return FPDFAnnot_GetFormFieldName(formHandle, annot, nullptr, 0);
|
|
108
|
+
},
|
|
109
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
110
|
+
return FPDFAnnot_GetFormFieldName(formHandle, annot, buf, len);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// field value (UTF-16LE)
|
|
114
|
+
data.value = ReadU16(
|
|
115
|
+
[&](auto *, unsigned long) {
|
|
116
|
+
return FPDFAnnot_GetFormFieldValue(formHandle, annot, nullptr, 0);
|
|
117
|
+
},
|
|
118
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
119
|
+
return FPDFAnnot_GetFormFieldValue(formHandle, annot, buf, len);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// alternate name (tooltip)
|
|
123
|
+
data.alternateName = ReadU16(
|
|
124
|
+
[&](auto *, unsigned long) {
|
|
125
|
+
return FPDFAnnot_GetFormFieldAlternateName(formHandle, annot,
|
|
126
|
+
nullptr, 0);
|
|
127
|
+
},
|
|
128
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
129
|
+
return FPDFAnnot_GetFormFieldAlternateName(formHandle, annot, buf,
|
|
130
|
+
len);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// export value (for checkboxes and radio buttons)
|
|
134
|
+
data.exportValue = ReadU16(
|
|
135
|
+
[&](auto *, unsigned long) {
|
|
136
|
+
return FPDFAnnot_GetFormFieldExportValue(formHandle, annot, nullptr,
|
|
137
|
+
0);
|
|
138
|
+
},
|
|
139
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
140
|
+
return FPDFAnnot_GetFormFieldExportValue(formHandle, annot, buf,
|
|
141
|
+
len);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// field flags
|
|
145
|
+
data.flags = FPDFAnnot_GetFormFieldFlags(formHandle, annot);
|
|
146
|
+
|
|
147
|
+
// bounds
|
|
148
|
+
FS_RECTF rect;
|
|
149
|
+
if (FPDFAnnot_GetRect(annot, &rect)) {
|
|
150
|
+
data.hasBounds = true;
|
|
151
|
+
data.left = rect.left;
|
|
152
|
+
data.bottom = rect.bottom;
|
|
153
|
+
data.right = rect.right;
|
|
154
|
+
data.top = rect.top;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// checked state — derive from value/exportValue to avoid needing
|
|
158
|
+
// FORM_OnAfterLoadPage (which conflicts with page lifecycle)
|
|
159
|
+
if (fieldType == FPDF_FORMFIELD_CHECKBOX) {
|
|
160
|
+
// checkbox is checked when its value is not "Off"
|
|
161
|
+
static const std::u16string off = u"Off";
|
|
162
|
+
data.isChecked = !data.value.empty() && data.value != off;
|
|
163
|
+
} else if (fieldType == FPDF_FORMFIELD_RADIOBUTTON) {
|
|
164
|
+
// radio is checked when its appearance state (/AS) is not "Off"
|
|
165
|
+
static const std::u16string off = u"Off";
|
|
166
|
+
std::u16string as = ReadU16(
|
|
167
|
+
[&](auto *, unsigned long) {
|
|
168
|
+
return FPDFAnnot_GetStringValue(annot, "AS", nullptr, 0);
|
|
169
|
+
},
|
|
170
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
171
|
+
return FPDFAnnot_GetStringValue(annot, "AS", buf, len);
|
|
172
|
+
});
|
|
173
|
+
data.isChecked = !as.empty() && as != off;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// options (combo box / list box)
|
|
177
|
+
int optCount = FPDFAnnot_GetOptionCount(formHandle, annot);
|
|
178
|
+
if (optCount > 0) {
|
|
179
|
+
for (int j = 0; j < optCount; j++) {
|
|
180
|
+
FormFieldOptionData opt;
|
|
181
|
+
opt.label = ReadU16(
|
|
182
|
+
[&](auto *, unsigned long) {
|
|
183
|
+
return FPDFAnnot_GetOptionLabel(formHandle, annot, j, nullptr,
|
|
184
|
+
0);
|
|
185
|
+
},
|
|
186
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
187
|
+
return FPDFAnnot_GetOptionLabel(formHandle, annot, j, buf, len);
|
|
188
|
+
});
|
|
189
|
+
opt.isSelected =
|
|
190
|
+
FPDFAnnot_IsOptionSelected(formHandle, annot, j) != 0;
|
|
191
|
+
data.options.push_back(std::move(opt));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
FPDFPage_CloseAnnot(annot);
|
|
196
|
+
fields_.push_back(std::move(data));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (formHandle)
|
|
200
|
+
FPDFDOC_ExitFormFillEnvironment(formHandle);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
void OnOK() override {
|
|
204
|
+
Napi::Env env = Env();
|
|
205
|
+
Napi::Array arr = Napi::Array::New(env, fields_.size());
|
|
206
|
+
for (uint32_t i = 0; i < fields_.size(); i++) {
|
|
207
|
+
auto &d = fields_[i];
|
|
208
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
209
|
+
obj.Set("type", Napi::String::New(env, d.type));
|
|
210
|
+
SetU16(obj, "name", env, d.name);
|
|
211
|
+
SetU16(obj, "value", env, d.value);
|
|
212
|
+
SetU16IfPresent(obj, "alternateName", env, d.alternateName);
|
|
213
|
+
SetU16IfPresent(obj, "exportValue", env, d.exportValue);
|
|
214
|
+
obj.Set("flags", Napi::Number::New(env, d.flags));
|
|
215
|
+
|
|
216
|
+
if (d.hasBounds)
|
|
217
|
+
obj.Set("bounds",
|
|
218
|
+
CreateBoundsObject(env, d.left, d.bottom, d.right, d.top));
|
|
219
|
+
|
|
220
|
+
obj.Set("isChecked", Napi::Boolean::New(env, d.isChecked));
|
|
221
|
+
|
|
222
|
+
if (!d.options.empty()) {
|
|
223
|
+
Napi::Array optArr = Napi::Array::New(env, d.options.size());
|
|
224
|
+
for (uint32_t j = 0; j < d.options.size(); j++) {
|
|
225
|
+
auto &opt = d.options[j];
|
|
226
|
+
Napi::Object optObj = Napi::Object::New(env);
|
|
227
|
+
SetU16(optObj, "label", env, opt.label);
|
|
228
|
+
optObj.Set("isSelected", Napi::Boolean::New(env, opt.isSelected));
|
|
229
|
+
optArr.Set(j, optObj);
|
|
230
|
+
}
|
|
231
|
+
obj.Set("options", optArr);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
arr.Set(i, obj);
|
|
235
|
+
}
|
|
236
|
+
deferred_.Resolve(arr);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
void OnError(const Napi::Error &err) override {
|
|
240
|
+
deferred_.Reject(err.Value());
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private:
|
|
244
|
+
Napi::Promise::Deferred deferred_;
|
|
245
|
+
FPDF_PAGE page_;
|
|
246
|
+
FPDF_DOCUMENT doc_;
|
|
247
|
+
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
248
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
249
|
+
std::vector<FormFieldData> fields_;
|
|
250
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
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_doc.h"
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// GetLinksWorker — async link extraction
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
struct LinkData {
|
|
17
|
+
double left = 0, bottom = 0, right = 0, top = 0;
|
|
18
|
+
bool hasBounds = false;
|
|
19
|
+
std::string url;
|
|
20
|
+
int pageIndex = -1;
|
|
21
|
+
bool hasPageIndex = false;
|
|
22
|
+
std::string actionType;
|
|
23
|
+
// destination location
|
|
24
|
+
float destX = 0, destY = 0, destZoom = 0;
|
|
25
|
+
bool hasDestX = false, hasDestY = false, hasDestZoom = false;
|
|
26
|
+
std::string filePath;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
class GetLinksWorker : public Napi::AsyncWorker {
|
|
30
|
+
public:
|
|
31
|
+
GetLinksWorker(Napi::Env env, FPDF_PAGE page, FPDF_DOCUMENT doc,
|
|
32
|
+
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
33
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
34
|
+
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
35
|
+
page_(page), doc_(doc), pageAlive_(std::move(pageAlive)),
|
|
36
|
+
docAlive_(std::move(docAlive)) {}
|
|
37
|
+
|
|
38
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
39
|
+
|
|
40
|
+
protected:
|
|
41
|
+
void Execute() override {
|
|
42
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
43
|
+
CHECK_ALIVE();
|
|
44
|
+
|
|
45
|
+
int pos = 0;
|
|
46
|
+
FPDF_LINK link;
|
|
47
|
+
while (FPDFLink_Enumerate(page_, &pos, &link)) {
|
|
48
|
+
LinkData data;
|
|
49
|
+
|
|
50
|
+
FS_RECTF rect;
|
|
51
|
+
if (FPDFLink_GetAnnotRect(link, &rect)) {
|
|
52
|
+
data.hasBounds = true;
|
|
53
|
+
data.left = rect.left;
|
|
54
|
+
data.bottom = rect.bottom;
|
|
55
|
+
data.right = rect.right;
|
|
56
|
+
data.top = rect.top;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
FPDF_ACTION action = FPDFLink_GetAction(link);
|
|
60
|
+
if (action) {
|
|
61
|
+
unsigned long actionType = FPDFAction_GetType(action);
|
|
62
|
+
data.actionType = ActionTypeString(actionType);
|
|
63
|
+
|
|
64
|
+
if (actionType == PDFACTION_URI) {
|
|
65
|
+
unsigned long len = FPDFAction_GetURIPath(doc_, action, nullptr, 0);
|
|
66
|
+
if (len > 0) {
|
|
67
|
+
std::vector<char> buf(len);
|
|
68
|
+
FPDFAction_GetURIPath(doc_, action, buf.data(), len);
|
|
69
|
+
data.url = std::string(buf.data(), len - 1);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// file path for remote goto / launch actions
|
|
74
|
+
if (actionType == PDFACTION_REMOTEGOTO ||
|
|
75
|
+
actionType == PDFACTION_LAUNCH) {
|
|
76
|
+
unsigned long fpLen = FPDFAction_GetFilePath(action, nullptr, 0);
|
|
77
|
+
if (fpLen > 0) {
|
|
78
|
+
std::vector<char> fpBuf(fpLen);
|
|
79
|
+
FPDFAction_GetFilePath(action, fpBuf.data(), fpLen);
|
|
80
|
+
data.filePath = std::string(fpBuf.data(), fpLen - 1);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
FPDF_DEST dest = FPDFLink_GetDest(doc_, link);
|
|
86
|
+
if (!dest && action) {
|
|
87
|
+
dest = FPDFAction_GetDest(doc_, action);
|
|
88
|
+
}
|
|
89
|
+
if (dest) {
|
|
90
|
+
int idx = FPDFDest_GetDestPageIndex(doc_, dest);
|
|
91
|
+
if (idx >= 0) {
|
|
92
|
+
data.hasPageIndex = true;
|
|
93
|
+
data.pageIndex = idx;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
FPDF_BOOL hasX, hasY, hasZoom;
|
|
97
|
+
FS_FLOAT x, y, zoom;
|
|
98
|
+
if (FPDFDest_GetLocationInPage(dest, &hasX, &hasY, &hasZoom, &x, &y,
|
|
99
|
+
&zoom)) {
|
|
100
|
+
if (hasX) {
|
|
101
|
+
data.hasDestX = true;
|
|
102
|
+
data.destX = x;
|
|
103
|
+
}
|
|
104
|
+
if (hasY) {
|
|
105
|
+
data.hasDestY = true;
|
|
106
|
+
data.destY = y;
|
|
107
|
+
}
|
|
108
|
+
if (hasZoom) {
|
|
109
|
+
data.hasDestZoom = true;
|
|
110
|
+
data.destZoom = zoom;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
links_.push_back(std::move(data));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
void OnOK() override {
|
|
120
|
+
Napi::Env env = Env();
|
|
121
|
+
Napi::Array arr = Napi::Array::New(env, links_.size());
|
|
122
|
+
for (uint32_t i = 0; i < links_.size(); i++) {
|
|
123
|
+
auto &d = links_[i];
|
|
124
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
125
|
+
if (d.hasBounds)
|
|
126
|
+
obj.Set("bounds",
|
|
127
|
+
CreateBoundsObject(env, d.left, d.bottom, d.right, d.top));
|
|
128
|
+
if (!d.url.empty())
|
|
129
|
+
obj.Set("url", Napi::String::New(env, d.url));
|
|
130
|
+
if (d.hasPageIndex)
|
|
131
|
+
obj.Set("pageIndex", Napi::Number::New(env, d.pageIndex));
|
|
132
|
+
if (!d.actionType.empty())
|
|
133
|
+
obj.Set("actionType", Napi::String::New(env, d.actionType));
|
|
134
|
+
if (d.hasDestX)
|
|
135
|
+
obj.Set("destX", Napi::Number::New(env, d.destX));
|
|
136
|
+
if (d.hasDestY)
|
|
137
|
+
obj.Set("destY", Napi::Number::New(env, d.destY));
|
|
138
|
+
if (d.hasDestZoom)
|
|
139
|
+
obj.Set("destZoom", Napi::Number::New(env, d.destZoom));
|
|
140
|
+
if (!d.filePath.empty())
|
|
141
|
+
obj.Set("filePath", Napi::String::New(env, d.filePath));
|
|
142
|
+
arr.Set(i, obj);
|
|
143
|
+
}
|
|
144
|
+
deferred_.Resolve(arr);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
void OnError(const Napi::Error &err) override {
|
|
148
|
+
deferred_.Reject(err.Value());
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private:
|
|
152
|
+
Napi::Promise::Deferred deferred_;
|
|
153
|
+
FPDF_PAGE page_;
|
|
154
|
+
FPDF_DOCUMENT doc_;
|
|
155
|
+
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
156
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
157
|
+
std::vector<LinkData> links_;
|
|
158
|
+
};
|