pdfium-native 0.2.0 → 0.2.3
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 +172 -41
- package/binding.gyp +27 -1
- 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 +16 -3
- package/scripts/bundle-lib.mjs +24 -3
- package/scripts/download-pdfium.mjs +2 -1
- package/scripts/generate-fixtures.mjs +308 -1
- package/scripts/install.mjs +19 -1
- package/src/annotations_worker.h +274 -0
- package/src/bookmarks_worker.h +162 -0
- package/src/document.h +48 -5
- package/src/form_fields_worker.h +250 -0
- package/src/links_worker.h +158 -0
- package/src/napi_helpers.h +76 -1
- package/src/objects_worker.h +322 -0
- package/src/page.h +62 -9
- 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
package/src/napi_helpers.h
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
|
+
#include <atomic>
|
|
3
4
|
#include <mutex>
|
|
4
5
|
#include <napi.h>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
5
8
|
|
|
9
|
+
#include "fpdf_doc.h"
|
|
6
10
|
#include "fpdfview.h"
|
|
7
11
|
|
|
8
|
-
inline bool g_initialized
|
|
12
|
+
inline std::atomic<bool> g_initialized{false};
|
|
9
13
|
|
|
10
14
|
// PDFium is not thread-safe — serialize all calls through a global mutex.
|
|
11
15
|
// This unblocks the Node.js event loop while waiting for the lock.
|
|
@@ -20,6 +24,77 @@ constexpr int MAX_RENDER_DIMENSION = 16384;
|
|
|
20
24
|
// maximum bookmark tree recursion depth
|
|
21
25
|
constexpr int MAX_BOOKMARK_DEPTH = 64;
|
|
22
26
|
|
|
27
|
+
// common alive-check macro for all page workers
|
|
28
|
+
#define CHECK_ALIVE() \
|
|
29
|
+
if (!pageAlive_->load() || (docAlive_ && !docAlive_->load())) { \
|
|
30
|
+
SetError("Page or document was closed"); \
|
|
31
|
+
return; \
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// PDFium UTF-16 string helpers
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
// read a PDFium UTF-16LE buffer into a std::u16string.
|
|
39
|
+
// `getLen` returns the byte length (including null terminator).
|
|
40
|
+
// `getData` writes into the provided buffer and returns the byte length.
|
|
41
|
+
template <typename GetLen, typename GetData>
|
|
42
|
+
inline std::u16string ReadU16(GetLen getLen, GetData getData) {
|
|
43
|
+
unsigned long len = getLen(static_cast<FPDF_WCHAR *>(nullptr), 0);
|
|
44
|
+
if (len < 4 || len % 2 != 0)
|
|
45
|
+
return {};
|
|
46
|
+
std::vector<unsigned short> buf(len / sizeof(unsigned short));
|
|
47
|
+
getData(reinterpret_cast<FPDF_WCHAR *>(buf.data()), len);
|
|
48
|
+
return std::u16string(reinterpret_cast<const char16_t *>(buf.data()),
|
|
49
|
+
len / sizeof(unsigned short) - 1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// convert a std::u16string to a Napi::String (empty string if input is empty)
|
|
53
|
+
inline Napi::String ToNapiString(Napi::Env env, const std::u16string &s) {
|
|
54
|
+
if (s.empty())
|
|
55
|
+
return Napi::String::New(env, "");
|
|
56
|
+
return Napi::String::New(env, reinterpret_cast<const char16_t *>(s.data()),
|
|
57
|
+
s.size());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// set a Napi object property from a u16string (always sets, uses "" if empty)
|
|
61
|
+
inline void SetU16(Napi::Object &obj, const char *key, Napi::Env env,
|
|
62
|
+
const std::u16string &s) {
|
|
63
|
+
obj.Set(key, ToNapiString(env, s));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// set a Napi object property from a u16string only if non-empty
|
|
67
|
+
inline void SetU16IfPresent(Napi::Object &obj, const char *key, Napi::Env env,
|
|
68
|
+
const std::u16string &s) {
|
|
69
|
+
if (!s.empty())
|
|
70
|
+
obj.Set(key, ToNapiString(env, s));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
// action type string from FPDF action type constant
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
inline const char *ActionTypeString(unsigned long actionType) {
|
|
78
|
+
switch (actionType) {
|
|
79
|
+
case PDFACTION_GOTO:
|
|
80
|
+
return "goto";
|
|
81
|
+
case PDFACTION_REMOTEGOTO:
|
|
82
|
+
return "remoteGoto";
|
|
83
|
+
case PDFACTION_URI:
|
|
84
|
+
return "uri";
|
|
85
|
+
case PDFACTION_LAUNCH:
|
|
86
|
+
return "launch";
|
|
87
|
+
case PDFACTION_EMBEDDEDGOTO:
|
|
88
|
+
return "embeddedGoto";
|
|
89
|
+
default:
|
|
90
|
+
return "unknown";
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
// Napi object helpers
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
|
|
23
98
|
// helper: create a { left, bottom, right, top } bounds object
|
|
24
99
|
inline Napi::Object CreateBoundsObject(Napi::Env env, double left,
|
|
25
100
|
double bottom, double right,
|
|
@@ -0,0 +1,322 @@
|
|
|
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_edit.h"
|
|
11
|
+
#include "fpdf_text.h"
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// GetObjectWorker — async page object extraction
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
struct PageObjectData {
|
|
18
|
+
std::string type;
|
|
19
|
+
float left = 0, bottom = 0, right = 0, top = 0;
|
|
20
|
+
unsigned int fillR = 0, fillG = 0, fillB = 0, fillA = 0;
|
|
21
|
+
bool hasFillColor = false;
|
|
22
|
+
unsigned int strokeR = 0, strokeG = 0, strokeB = 0, strokeA = 0;
|
|
23
|
+
bool hasStrokeColor = false;
|
|
24
|
+
// text-specific
|
|
25
|
+
std::u16string text;
|
|
26
|
+
float fontSize = 0;
|
|
27
|
+
std::string fontName;
|
|
28
|
+
int fontWeight = -1;
|
|
29
|
+
int italicAngle = 0;
|
|
30
|
+
bool hasItalicAngle = false;
|
|
31
|
+
std::string renderMode;
|
|
32
|
+
std::string fontFamily;
|
|
33
|
+
int isEmbedded = -1; // -1 = unknown, 0 = no, 1 = yes
|
|
34
|
+
int fontFlags = -1;
|
|
35
|
+
// image-specific
|
|
36
|
+
unsigned int imageWidth = 0, imageHeight = 0;
|
|
37
|
+
float horizontalDpi = 0, verticalDpi = 0;
|
|
38
|
+
unsigned int bitsPerPixel = 0;
|
|
39
|
+
std::string colorspace;
|
|
40
|
+
std::vector<std::string> filters;
|
|
41
|
+
bool hasImageMetadata = false;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
class GetObjectWorker : public Napi::AsyncWorker {
|
|
45
|
+
public:
|
|
46
|
+
GetObjectWorker(Napi::Env env, FPDF_PAGE page, int index,
|
|
47
|
+
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
48
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
49
|
+
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
50
|
+
page_(page), index_(index), pageAlive_(std::move(pageAlive)),
|
|
51
|
+
docAlive_(std::move(docAlive)) {}
|
|
52
|
+
|
|
53
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
54
|
+
|
|
55
|
+
protected:
|
|
56
|
+
void Execute() override {
|
|
57
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
58
|
+
CHECK_ALIVE();
|
|
59
|
+
|
|
60
|
+
FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page_, index_);
|
|
61
|
+
if (!obj) {
|
|
62
|
+
SetError("Object not found");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
int type = FPDFPageObj_GetType(obj);
|
|
67
|
+
switch (type) {
|
|
68
|
+
case FPDF_PAGEOBJ_TEXT:
|
|
69
|
+
data_.type = "text";
|
|
70
|
+
break;
|
|
71
|
+
case FPDF_PAGEOBJ_PATH:
|
|
72
|
+
data_.type = "path";
|
|
73
|
+
break;
|
|
74
|
+
case FPDF_PAGEOBJ_IMAGE:
|
|
75
|
+
data_.type = "image";
|
|
76
|
+
break;
|
|
77
|
+
case FPDF_PAGEOBJ_SHADING:
|
|
78
|
+
data_.type = "shading";
|
|
79
|
+
break;
|
|
80
|
+
case FPDF_PAGEOBJ_FORM:
|
|
81
|
+
data_.type = "form";
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
data_.type = "unknown";
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
FPDFPageObj_GetBounds(obj, &data_.left, &data_.bottom, &data_.right,
|
|
89
|
+
&data_.top);
|
|
90
|
+
|
|
91
|
+
unsigned int r, g, b, a;
|
|
92
|
+
if (FPDFPageObj_GetFillColor(obj, &r, &g, &b, &a)) {
|
|
93
|
+
data_.hasFillColor = true;
|
|
94
|
+
data_.fillR = r;
|
|
95
|
+
data_.fillG = g;
|
|
96
|
+
data_.fillB = b;
|
|
97
|
+
data_.fillA = a;
|
|
98
|
+
}
|
|
99
|
+
if (FPDFPageObj_GetStrokeColor(obj, &r, &g, &b, &a)) {
|
|
100
|
+
data_.hasStrokeColor = true;
|
|
101
|
+
data_.strokeR = r;
|
|
102
|
+
data_.strokeG = g;
|
|
103
|
+
data_.strokeB = b;
|
|
104
|
+
data_.strokeA = a;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (type == FPDF_PAGEOBJ_TEXT) {
|
|
108
|
+
FPDF_TEXTPAGE textPage = FPDFText_LoadPage(page_);
|
|
109
|
+
if (textPage) {
|
|
110
|
+
data_.text = ReadU16(
|
|
111
|
+
[&](auto *, unsigned long) {
|
|
112
|
+
return FPDFTextObj_GetText(obj, textPage, nullptr, 0);
|
|
113
|
+
},
|
|
114
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
115
|
+
return FPDFTextObj_GetText(obj, textPage, buf, len);
|
|
116
|
+
});
|
|
117
|
+
FPDFText_ClosePage(textPage);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
float fs = 0;
|
|
121
|
+
if (FPDFTextObj_GetFontSize(obj, &fs))
|
|
122
|
+
data_.fontSize = fs;
|
|
123
|
+
|
|
124
|
+
FPDF_FONT font = FPDFTextObj_GetFont(obj);
|
|
125
|
+
if (font) {
|
|
126
|
+
size_t nameLen = FPDFFont_GetBaseFontName(font, nullptr, 0);
|
|
127
|
+
if (nameLen > 1) {
|
|
128
|
+
std::vector<char> nameBuf(nameLen);
|
|
129
|
+
FPDFFont_GetBaseFontName(font, nameBuf.data(), nameLen);
|
|
130
|
+
data_.fontName = std::string(nameBuf.data(), nameLen - 1);
|
|
131
|
+
}
|
|
132
|
+
data_.fontWeight = FPDFFont_GetWeight(font);
|
|
133
|
+
int angle = 0;
|
|
134
|
+
if (FPDFFont_GetItalicAngle(font, &angle)) {
|
|
135
|
+
data_.hasItalicAngle = true;
|
|
136
|
+
data_.italicAngle = angle;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// font family name
|
|
140
|
+
size_t famLen = FPDFFont_GetFamilyName(font, nullptr, 0);
|
|
141
|
+
if (famLen > 1) {
|
|
142
|
+
std::vector<char> famBuf(famLen);
|
|
143
|
+
FPDFFont_GetFamilyName(font, famBuf.data(), famLen);
|
|
144
|
+
data_.fontFamily = std::string(famBuf.data(), famLen - 1);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
data_.isEmbedded = FPDFFont_GetIsEmbedded(font);
|
|
148
|
+
data_.fontFlags = FPDFFont_GetFlags(font);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// text render mode
|
|
152
|
+
FPDF_TEXT_RENDERMODE rm = FPDFTextObj_GetTextRenderMode(obj);
|
|
153
|
+
switch (rm) {
|
|
154
|
+
case FPDF_TEXTRENDERMODE_FILL:
|
|
155
|
+
data_.renderMode = "fill";
|
|
156
|
+
break;
|
|
157
|
+
case FPDF_TEXTRENDERMODE_STROKE:
|
|
158
|
+
data_.renderMode = "stroke";
|
|
159
|
+
break;
|
|
160
|
+
case FPDF_TEXTRENDERMODE_FILL_STROKE:
|
|
161
|
+
data_.renderMode = "fillStroke";
|
|
162
|
+
break;
|
|
163
|
+
case FPDF_TEXTRENDERMODE_INVISIBLE:
|
|
164
|
+
data_.renderMode = "invisible";
|
|
165
|
+
break;
|
|
166
|
+
case FPDF_TEXTRENDERMODE_FILL_CLIP:
|
|
167
|
+
data_.renderMode = "fillClip";
|
|
168
|
+
break;
|
|
169
|
+
case FPDF_TEXTRENDERMODE_STROKE_CLIP:
|
|
170
|
+
data_.renderMode = "strokeClip";
|
|
171
|
+
break;
|
|
172
|
+
case FPDF_TEXTRENDERMODE_FILL_STROKE_CLIP:
|
|
173
|
+
data_.renderMode = "fillStrokeClip";
|
|
174
|
+
break;
|
|
175
|
+
case FPDF_TEXTRENDERMODE_CLIP:
|
|
176
|
+
data_.renderMode = "clip";
|
|
177
|
+
break;
|
|
178
|
+
default:
|
|
179
|
+
data_.renderMode = "unknown";
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (type == FPDF_PAGEOBJ_IMAGE) {
|
|
185
|
+
FPDFImageObj_GetImagePixelSize(obj, &data_.imageWidth,
|
|
186
|
+
&data_.imageHeight);
|
|
187
|
+
|
|
188
|
+
// rich image metadata
|
|
189
|
+
FPDF_IMAGEOBJ_METADATA meta;
|
|
190
|
+
if (FPDFImageObj_GetImageMetadata(obj, page_, &meta)) {
|
|
191
|
+
data_.hasImageMetadata = true;
|
|
192
|
+
data_.horizontalDpi = meta.horizontal_dpi;
|
|
193
|
+
data_.verticalDpi = meta.vertical_dpi;
|
|
194
|
+
data_.bitsPerPixel = meta.bits_per_pixel;
|
|
195
|
+
switch (meta.colorspace) {
|
|
196
|
+
case FPDF_COLORSPACE_DEVICEGRAY:
|
|
197
|
+
data_.colorspace = "deviceGray";
|
|
198
|
+
break;
|
|
199
|
+
case FPDF_COLORSPACE_DEVICERGB:
|
|
200
|
+
data_.colorspace = "deviceRGB";
|
|
201
|
+
break;
|
|
202
|
+
case FPDF_COLORSPACE_DEVICECMYK:
|
|
203
|
+
data_.colorspace = "deviceCMYK";
|
|
204
|
+
break;
|
|
205
|
+
case FPDF_COLORSPACE_CALGRAY:
|
|
206
|
+
data_.colorspace = "calGray";
|
|
207
|
+
break;
|
|
208
|
+
case FPDF_COLORSPACE_CALRGB:
|
|
209
|
+
data_.colorspace = "calRGB";
|
|
210
|
+
break;
|
|
211
|
+
case FPDF_COLORSPACE_LAB:
|
|
212
|
+
data_.colorspace = "lab";
|
|
213
|
+
break;
|
|
214
|
+
case FPDF_COLORSPACE_ICCBASED:
|
|
215
|
+
data_.colorspace = "iccBased";
|
|
216
|
+
break;
|
|
217
|
+
case FPDF_COLORSPACE_SEPARATION:
|
|
218
|
+
data_.colorspace = "separation";
|
|
219
|
+
break;
|
|
220
|
+
case FPDF_COLORSPACE_DEVICEN:
|
|
221
|
+
data_.colorspace = "deviceN";
|
|
222
|
+
break;
|
|
223
|
+
case FPDF_COLORSPACE_INDEXED:
|
|
224
|
+
data_.colorspace = "indexed";
|
|
225
|
+
break;
|
|
226
|
+
case FPDF_COLORSPACE_PATTERN:
|
|
227
|
+
data_.colorspace = "pattern";
|
|
228
|
+
break;
|
|
229
|
+
default:
|
|
230
|
+
data_.colorspace = "unknown";
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// compression filters
|
|
236
|
+
int filterCount = FPDFImageObj_GetImageFilterCount(obj);
|
|
237
|
+
for (int fi = 0; fi < filterCount; fi++) {
|
|
238
|
+
unsigned long fLen = FPDFImageObj_GetImageFilter(obj, fi, nullptr, 0);
|
|
239
|
+
if (fLen > 1) {
|
|
240
|
+
std::vector<char> fBuf(fLen);
|
|
241
|
+
FPDFImageObj_GetImageFilter(obj, fi, fBuf.data(), fLen);
|
|
242
|
+
data_.filters.emplace_back(fBuf.data(), fLen - 1);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
void OnOK() override {
|
|
249
|
+
Napi::Env env = Env();
|
|
250
|
+
Napi::Object result = Napi::Object::New(env);
|
|
251
|
+
result.Set("type", Napi::String::New(env, data_.type));
|
|
252
|
+
result.Set("bounds", CreateBoundsObject(env, data_.left, data_.bottom,
|
|
253
|
+
data_.right, data_.top));
|
|
254
|
+
|
|
255
|
+
if (data_.hasFillColor) {
|
|
256
|
+
result.Set("fillColor", CreateColorObject(env, data_.fillR, data_.fillG,
|
|
257
|
+
data_.fillB, data_.fillA));
|
|
258
|
+
} else {
|
|
259
|
+
result.Set("fillColor", env.Null());
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (data_.hasStrokeColor) {
|
|
263
|
+
result.Set("strokeColor",
|
|
264
|
+
CreateColorObject(env, data_.strokeR, data_.strokeG,
|
|
265
|
+
data_.strokeB, data_.strokeA));
|
|
266
|
+
} else {
|
|
267
|
+
result.Set("strokeColor", env.Null());
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (data_.type == "text") {
|
|
271
|
+
SetU16(result, "text", env, data_.text);
|
|
272
|
+
result.Set("fontSize", Napi::Number::New(env, data_.fontSize));
|
|
273
|
+
result.Set("fontName", Napi::String::New(env, data_.fontName));
|
|
274
|
+
if (data_.fontWeight >= 0)
|
|
275
|
+
result.Set("fontWeight", Napi::Number::New(env, data_.fontWeight));
|
|
276
|
+
if (data_.hasItalicAngle)
|
|
277
|
+
result.Set("italicAngle", Napi::Number::New(env, data_.italicAngle));
|
|
278
|
+
if (!data_.renderMode.empty())
|
|
279
|
+
result.Set("renderMode", Napi::String::New(env, data_.renderMode));
|
|
280
|
+
if (!data_.fontFamily.empty())
|
|
281
|
+
result.Set("fontFamily", Napi::String::New(env, data_.fontFamily));
|
|
282
|
+
if (data_.isEmbedded >= 0)
|
|
283
|
+
result.Set("isEmbedded",
|
|
284
|
+
Napi::Boolean::New(env, data_.isEmbedded != 0));
|
|
285
|
+
if (data_.fontFlags >= 0)
|
|
286
|
+
result.Set("fontFlags", Napi::Number::New(env, data_.fontFlags));
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (data_.type == "image") {
|
|
290
|
+
result.Set("imageWidth", Napi::Number::New(env, data_.imageWidth));
|
|
291
|
+
result.Set("imageHeight", Napi::Number::New(env, data_.imageHeight));
|
|
292
|
+
if (data_.hasImageMetadata) {
|
|
293
|
+
result.Set("horizontalDpi",
|
|
294
|
+
Napi::Number::New(env, data_.horizontalDpi));
|
|
295
|
+
result.Set("verticalDpi", Napi::Number::New(env, data_.verticalDpi));
|
|
296
|
+
result.Set("bitsPerPixel", Napi::Number::New(env, data_.bitsPerPixel));
|
|
297
|
+
if (!data_.colorspace.empty())
|
|
298
|
+
result.Set("colorspace", Napi::String::New(env, data_.colorspace));
|
|
299
|
+
}
|
|
300
|
+
if (!data_.filters.empty()) {
|
|
301
|
+
Napi::Array fArr = Napi::Array::New(env, data_.filters.size());
|
|
302
|
+
for (uint32_t fi = 0; fi < data_.filters.size(); fi++)
|
|
303
|
+
fArr.Set(fi, Napi::String::New(env, data_.filters[fi]));
|
|
304
|
+
result.Set("filters", fArr);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
deferred_.Resolve(result);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
void OnError(const Napi::Error &err) override {
|
|
312
|
+
deferred_.Reject(err.Value());
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private:
|
|
316
|
+
Napi::Promise::Deferred deferred_;
|
|
317
|
+
FPDF_PAGE page_;
|
|
318
|
+
int index_;
|
|
319
|
+
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
320
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
321
|
+
PageObjectData data_;
|
|
322
|
+
};
|
package/src/page.h
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
|
-
#include "
|
|
3
|
+
#include "annotations_worker.h"
|
|
4
|
+
#include "form_fields_worker.h"
|
|
5
|
+
#include "links_worker.h"
|
|
6
|
+
#include "objects_worker.h"
|
|
4
7
|
#include "render_worker.h"
|
|
8
|
+
#include "search_worker.h"
|
|
9
|
+
#include "text_worker.h"
|
|
5
10
|
|
|
6
|
-
#include <algorithm>
|
|
7
11
|
#include <atomic>
|
|
8
12
|
#include <memory>
|
|
9
13
|
#include <string>
|
|
@@ -29,6 +33,7 @@ public:
|
|
|
29
33
|
InstanceMethod<&PDFiumPage::GetLinks>("getLinks"),
|
|
30
34
|
InstanceMethod<&PDFiumPage::Search>("search"),
|
|
31
35
|
InstanceMethod<&PDFiumPage::GetAnnotations>("getAnnotations"),
|
|
36
|
+
InstanceMethod<&PDFiumPage::GetFormFields>("getFormFields"),
|
|
32
37
|
InstanceMethod<&PDFiumPage::Close>("close"),
|
|
33
38
|
});
|
|
34
39
|
}
|
|
@@ -37,10 +42,13 @@ public:
|
|
|
37
42
|
: Napi::ObjectWrap<PDFiumPage>(info),
|
|
38
43
|
alive_(std::make_shared<std::atomic<bool>>(true)) {}
|
|
39
44
|
|
|
40
|
-
void SetPage(FPDF_PAGE page, FPDF_DOCUMENT doc, int index
|
|
45
|
+
void SetPage(FPDF_PAGE page, FPDF_DOCUMENT doc, int index, float width,
|
|
46
|
+
float height) {
|
|
41
47
|
page_ = page;
|
|
42
48
|
doc_ = doc;
|
|
43
49
|
index_ = index;
|
|
50
|
+
width_ = width;
|
|
51
|
+
height_ = height;
|
|
44
52
|
}
|
|
45
53
|
|
|
46
54
|
// called by document to give this page a reference to the document's alive
|
|
@@ -56,6 +64,8 @@ private:
|
|
|
56
64
|
FPDF_PAGE page_ = nullptr;
|
|
57
65
|
FPDF_DOCUMENT doc_ = nullptr;
|
|
58
66
|
int index_ = -1;
|
|
67
|
+
float width_ = 0;
|
|
68
|
+
float height_ = 0;
|
|
59
69
|
std::shared_ptr<std::atomic<bool>> alive_;
|
|
60
70
|
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
61
71
|
|
|
@@ -102,13 +112,18 @@ private:
|
|
|
102
112
|
if (env.IsExceptionPending())
|
|
103
113
|
return env.Null();
|
|
104
114
|
|
|
105
|
-
|
|
106
|
-
|
|
115
|
+
// use cached dimensions (set under mutex in GetPageWorker) to avoid
|
|
116
|
+
// PDFium API calls on the main thread that could race with workers
|
|
117
|
+
float origWidth = width_;
|
|
118
|
+
float origHeight = height_;
|
|
107
119
|
double scale = 1.0;
|
|
108
120
|
int renderWidth = 0;
|
|
109
121
|
int renderHeight = 0;
|
|
110
122
|
int format = IMAGE_FORMAT_JPEG;
|
|
111
123
|
int quality = 100;
|
|
124
|
+
int rotation = 0;
|
|
125
|
+
bool transparent = false;
|
|
126
|
+
int renderFlags = FPDF_ANNOT | FPDF_PRINTING;
|
|
112
127
|
|
|
113
128
|
std::string outputPath;
|
|
114
129
|
|
|
@@ -135,6 +150,25 @@ private:
|
|
|
135
150
|
quality = opts.Get("quality").As<Napi::Number>().Int32Value();
|
|
136
151
|
if (opts.Has("output"))
|
|
137
152
|
outputPath = opts.Get("output").As<Napi::String>().Utf8Value();
|
|
153
|
+
if (opts.Has("rotation")) {
|
|
154
|
+
rotation = opts.Get("rotation").As<Napi::Number>().Int32Value();
|
|
155
|
+
if (rotation != 0 && rotation != 1 && rotation != 2 && rotation != 3) {
|
|
156
|
+
Napi::RangeError::New(env, "rotation must be 0, 1, 2, or 3")
|
|
157
|
+
.ThrowAsJavaScriptException();
|
|
158
|
+
return env.Null();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (opts.Has("transparent"))
|
|
162
|
+
transparent = opts.Get("transparent").As<Napi::Boolean>().Value();
|
|
163
|
+
if (opts.Has("renderAnnotations") &&
|
|
164
|
+
!opts.Get("renderAnnotations").As<Napi::Boolean>().Value())
|
|
165
|
+
renderFlags &= ~FPDF_ANNOT;
|
|
166
|
+
if (opts.Has("grayscale") &&
|
|
167
|
+
opts.Get("grayscale").As<Napi::Boolean>().Value())
|
|
168
|
+
renderFlags |= FPDF_GRAYSCALE;
|
|
169
|
+
if (opts.Has("lcdText") &&
|
|
170
|
+
opts.Get("lcdText").As<Napi::Boolean>().Value())
|
|
171
|
+
renderFlags |= FPDF_LCD_TEXT;
|
|
138
172
|
}
|
|
139
173
|
|
|
140
174
|
if (renderWidth == 0)
|
|
@@ -159,9 +193,9 @@ private:
|
|
|
159
193
|
// clamp quality to valid range
|
|
160
194
|
quality = std::clamp(quality, 1, 100);
|
|
161
195
|
|
|
162
|
-
auto *worker =
|
|
163
|
-
|
|
164
|
-
|
|
196
|
+
auto *worker = new RenderWorker(env, page_, renderWidth, renderHeight,
|
|
197
|
+
format, quality, std::move(outputPath),
|
|
198
|
+
rotation, transparent, renderFlags, alive_);
|
|
165
199
|
auto promise = worker->Promise();
|
|
166
200
|
worker->Queue();
|
|
167
201
|
return promise;
|
|
@@ -229,6 +263,9 @@ private:
|
|
|
229
263
|
if (opts.Has("wholeWord") &&
|
|
230
264
|
opts.Get("wholeWord").As<Napi::Boolean>().Value())
|
|
231
265
|
flags |= FPDF_MATCHWHOLEWORD;
|
|
266
|
+
if (opts.Has("consecutive") &&
|
|
267
|
+
opts.Get("consecutive").As<Napi::Boolean>().Value())
|
|
268
|
+
flags |= FPDF_CONSECUTIVE;
|
|
232
269
|
}
|
|
233
270
|
|
|
234
271
|
auto *worker = new SearchWorker(env, page_, std::move(needle), flags,
|
|
@@ -254,6 +291,22 @@ private:
|
|
|
254
291
|
return promise;
|
|
255
292
|
}
|
|
256
293
|
|
|
294
|
+
/**
|
|
295
|
+
* Returns all form fields on the page (async). Returns a
|
|
296
|
+
* Promise<FormField[]>.
|
|
297
|
+
*/
|
|
298
|
+
Napi::Value GetFormFields(const Napi::CallbackInfo &info) {
|
|
299
|
+
Napi::Env env = info.Env();
|
|
300
|
+
EnsureOpen(env);
|
|
301
|
+
if (env.IsExceptionPending())
|
|
302
|
+
return env.Null();
|
|
303
|
+
|
|
304
|
+
auto *worker = new GetFormFieldsWorker(env, page_, doc_, alive_, docAlive_);
|
|
305
|
+
auto promise = worker->Promise();
|
|
306
|
+
worker->Queue();
|
|
307
|
+
return promise;
|
|
308
|
+
}
|
|
309
|
+
|
|
257
310
|
/**
|
|
258
311
|
* Closes the page and releases its resources.
|
|
259
312
|
*/
|
|
@@ -269,8 +322,8 @@ private:
|
|
|
269
322
|
|
|
270
323
|
void CleanUp() {
|
|
271
324
|
if (page_) {
|
|
272
|
-
alive_->store(false);
|
|
273
325
|
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
326
|
+
alive_->store(false);
|
|
274
327
|
FPDF_ClosePage(page_);
|
|
275
328
|
page_ = nullptr;
|
|
276
329
|
doc_ = nullptr;
|