pdfium-native 0.1.1

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.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +227 -0
  3. package/binding.gyp +85 -0
  4. package/dist/document.d.ts +23 -0
  5. package/dist/document.d.ts.map +1 -0
  6. package/dist/document.js +36 -0
  7. package/dist/document.js.map +1 -0
  8. package/dist/errors.d.ts +23 -0
  9. package/dist/errors.d.ts.map +1 -0
  10. package/dist/errors.js +58 -0
  11. package/dist/errors.js.map +1 -0
  12. package/dist/index.d.ts +11 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +33 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/page.d.ts +40 -0
  17. package/dist/page.d.ts.map +1 -0
  18. package/dist/page.js +59 -0
  19. package/dist/page.js.map +1 -0
  20. package/dist/types.d.ts +120 -0
  21. package/dist/types.d.ts.map +1 -0
  22. package/dist/types.js +2 -0
  23. package/dist/types.js.map +1 -0
  24. package/lib/__tests__/document.test.ts +99 -0
  25. package/lib/__tests__/features.test.ts +72 -0
  26. package/lib/__tests__/fixtures.ts +71 -0
  27. package/lib/__tests__/load.test.ts +68 -0
  28. package/lib/__tests__/page.test.ts +123 -0
  29. package/lib/__tests__/render.test.ts +103 -0
  30. package/lib/__tests__/search.test.ts +44 -0
  31. package/lib/document.ts +40 -0
  32. package/lib/errors.ts +63 -0
  33. package/lib/index.ts +63 -0
  34. package/lib/page.ts +80 -0
  35. package/lib/types.ts +154 -0
  36. package/package.json +80 -0
  37. package/scripts/bundle-lib.mjs +66 -0
  38. package/scripts/download-pdfium.mjs +84 -0
  39. package/scripts/generate-fixtures.mjs +194 -0
  40. package/scripts/install.mjs +80 -0
  41. package/src/document.h +254 -0
  42. package/src/napi_helpers.h +50 -0
  43. package/src/page.h +576 -0
  44. package/src/pdfium_addon.cc +218 -0
  45. package/src/render_worker.h +141 -0
  46. package/src/stb_image_write.c +2 -0
  47. package/src/stb_image_write.h +1724 -0
package/src/page.h ADDED
@@ -0,0 +1,576 @@
1
+ #pragma once
2
+
3
+ #include "render_worker.h"
4
+
5
+ #include <algorithm>
6
+ #include <atomic>
7
+ #include <memory>
8
+ #include <string>
9
+
10
+ #include "fpdf_annot.h"
11
+ #include "fpdf_doc.h"
12
+ #include "fpdf_edit.h"
13
+ #include "fpdf_text.h"
14
+
15
+ // forward declarations
16
+ class GetPageWorker;
17
+ class LoadDocumentWorker;
18
+
19
+ // ---------------------------------------------------------------------------
20
+ // PDFiumPage
21
+ // ---------------------------------------------------------------------------
22
+
23
+ class PDFiumPage : public Napi::ObjectWrap<PDFiumPage> {
24
+ public:
25
+ static Napi::Function Init(Napi::Env env) {
26
+ return DefineClass(
27
+ env, "PDFiumPage",
28
+ {
29
+ InstanceAccessor<&PDFiumPage::GetNumber>("number"),
30
+ InstanceMethod<&PDFiumPage::GetText>("getText"),
31
+ InstanceMethod<&PDFiumPage::RenderAsync>("render"),
32
+ InstanceMethod<&PDFiumPage::GetObject>("getObject"),
33
+ InstanceMethod<&PDFiumPage::GetLinks>("getLinks"),
34
+ InstanceMethod<&PDFiumPage::Search>("search"),
35
+ InstanceMethod<&PDFiumPage::GetAnnotations>("getAnnotations"),
36
+ InstanceMethod<&PDFiumPage::Close>("close"),
37
+ });
38
+ }
39
+
40
+ PDFiumPage(const Napi::CallbackInfo &info)
41
+ : Napi::ObjectWrap<PDFiumPage>(info),
42
+ alive_(std::make_shared<std::atomic<bool>>(true)) {}
43
+
44
+ void SetPage(FPDF_PAGE page, FPDF_DOCUMENT doc, int index) {
45
+ page_ = page;
46
+ doc_ = doc;
47
+ index_ = index;
48
+ }
49
+
50
+ // called by document to give this page a reference to the document's alive
51
+ // flag so we can detect doc.destroy() from page methods
52
+ void SetDocAlive(std::shared_ptr<std::atomic<bool>> docAlive) {
53
+ docAlive_ = std::move(docAlive);
54
+ }
55
+
56
+ // returns the page alive flag (shared with RenderWorker and document)
57
+ std::shared_ptr<std::atomic<bool>> GetAliveFlag() const { return alive_; }
58
+
59
+ private:
60
+ FPDF_PAGE page_ = nullptr;
61
+ FPDF_DOCUMENT doc_ = nullptr;
62
+ int index_ = -1;
63
+ std::shared_ptr<std::atomic<bool>> alive_;
64
+ std::shared_ptr<std::atomic<bool>> docAlive_;
65
+
66
+ void EnsureOpen(Napi::Env env) {
67
+ if (!alive_->load()) {
68
+ Napi::Error::New(env, "Page is closed").ThrowAsJavaScriptException();
69
+ return;
70
+ }
71
+ if (docAlive_ && !docAlive_->load()) {
72
+ Napi::Error::New(env, "Document is destroyed")
73
+ .ThrowAsJavaScriptException();
74
+ return;
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Returns the 0-based page index.
80
+ */
81
+ Napi::Value GetNumber(const Napi::CallbackInfo &info) {
82
+ return Napi::Number::New(info.Env(), index_);
83
+ }
84
+
85
+ /**
86
+ * Extracts all text from the page.
87
+ */
88
+ Napi::Value GetText(const Napi::CallbackInfo &info) {
89
+ Napi::Env env = info.Env();
90
+ EnsureOpen(env);
91
+ if (env.IsExceptionPending())
92
+ return env.Null();
93
+
94
+ std::lock_guard<std::mutex> lock(g_pdfium_mutex);
95
+
96
+ FPDF_TEXTPAGE textPage = FPDFText_LoadPage(page_);
97
+ if (!textPage) {
98
+ Napi::Error::New(env, "Failed to load text page")
99
+ .ThrowAsJavaScriptException();
100
+ return env.Null();
101
+ }
102
+
103
+ int charCount = FPDFText_CountChars(textPage);
104
+ if (charCount <= 0) {
105
+ FPDFText_ClosePage(textPage);
106
+ return Napi::String::New(env, "");
107
+ }
108
+
109
+ int bufLen = charCount + 1;
110
+ std::vector<unsigned short> textBuf(bufLen);
111
+ FPDFText_GetText(textPage, 0, charCount, textBuf.data());
112
+ FPDFText_ClosePage(textPage);
113
+
114
+ return Napi::String::New(
115
+ env, reinterpret_cast<const char16_t *>(textBuf.data()), charCount);
116
+ }
117
+
118
+ /**
119
+ * Renders the page to a JPEG or PNG buffer (async).
120
+ */
121
+ Napi::Value RenderAsync(const Napi::CallbackInfo &info) {
122
+ Napi::Env env = info.Env();
123
+ EnsureOpen(env);
124
+ if (env.IsExceptionPending())
125
+ return env.Null();
126
+
127
+ float origWidth = FPDF_GetPageWidthF(page_);
128
+ float origHeight = FPDF_GetPageHeightF(page_);
129
+ double scale = 1.0;
130
+ int renderWidth = 0;
131
+ int renderHeight = 0;
132
+ int format = IMAGE_FORMAT_JPEG;
133
+ int quality = 100;
134
+
135
+ std::string outputPath;
136
+
137
+ if (info.Length() > 0 && info[0].IsObject()) {
138
+ Napi::Object opts = info[0].As<Napi::Object>();
139
+ if (opts.Has("scale")) {
140
+ scale = opts.Get("scale").As<Napi::Number>().DoubleValue();
141
+ if (scale <= 0) {
142
+ Napi::RangeError::New(env, "scale must be positive")
143
+ .ThrowAsJavaScriptException();
144
+ return env.Null();
145
+ }
146
+ }
147
+ if (opts.Has("width"))
148
+ renderWidth = opts.Get("width").As<Napi::Number>().Int32Value();
149
+ if (opts.Has("height"))
150
+ renderHeight = opts.Get("height").As<Napi::Number>().Int32Value();
151
+ if (opts.Has("format")) {
152
+ std::string fmt = opts.Get("format").As<Napi::String>().Utf8Value();
153
+ if (fmt == "png")
154
+ format = IMAGE_FORMAT_PNG;
155
+ }
156
+ if (opts.Has("quality"))
157
+ quality = opts.Get("quality").As<Napi::Number>().Int32Value();
158
+ if (opts.Has("output"))
159
+ outputPath = opts.Get("output").As<Napi::String>().Utf8Value();
160
+ }
161
+
162
+ if (renderWidth == 0)
163
+ renderWidth = static_cast<int>(origWidth * scale);
164
+ if (renderHeight == 0)
165
+ renderHeight = static_cast<int>(origHeight * scale);
166
+
167
+ // validate dimensions
168
+ if (renderWidth <= 0 || renderHeight <= 0) {
169
+ Napi::RangeError::New(env, "Render dimensions must be positive")
170
+ .ThrowAsJavaScriptException();
171
+ return env.Null();
172
+ }
173
+ if (renderWidth > MAX_RENDER_DIMENSION ||
174
+ renderHeight > MAX_RENDER_DIMENSION) {
175
+ Napi::RangeError::New(env, "Render dimensions exceed maximum (" +
176
+ std::to_string(MAX_RENDER_DIMENSION) + ")")
177
+ .ThrowAsJavaScriptException();
178
+ return env.Null();
179
+ }
180
+
181
+ // clamp quality to valid range
182
+ quality = std::clamp(quality, 1, 100);
183
+
184
+ auto *worker =
185
+ new RenderWorker(env, page_, renderWidth, renderHeight, format, quality,
186
+ std::move(outputPath), alive_);
187
+ auto promise = worker->Promise();
188
+ worker->Queue();
189
+ return promise;
190
+ }
191
+
192
+ /**
193
+ * Returns a page object at the given index with type and bounds.
194
+ */
195
+ Napi::Value GetObject(const Napi::CallbackInfo &info) {
196
+ Napi::Env env = info.Env();
197
+ EnsureOpen(env);
198
+ if (env.IsExceptionPending())
199
+ return env.Null();
200
+
201
+ if (info.Length() < 1 || !info[0].IsNumber()) {
202
+ Napi::TypeError::New(env, "Expected object index")
203
+ .ThrowAsJavaScriptException();
204
+ return env.Null();
205
+ }
206
+
207
+ std::lock_guard<std::mutex> lock(g_pdfium_mutex);
208
+
209
+ int idx = info[0].As<Napi::Number>().Int32Value();
210
+ FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page_, idx);
211
+ if (!obj) {
212
+ Napi::Error::New(env, "Object not found").ThrowAsJavaScriptException();
213
+ return env.Null();
214
+ }
215
+
216
+ int type = FPDFPageObj_GetType(obj);
217
+ const char *typeName;
218
+ switch (type) {
219
+ case FPDF_PAGEOBJ_TEXT:
220
+ typeName = "text";
221
+ break;
222
+ case FPDF_PAGEOBJ_PATH:
223
+ typeName = "path";
224
+ break;
225
+ case FPDF_PAGEOBJ_IMAGE:
226
+ typeName = "image";
227
+ break;
228
+ case FPDF_PAGEOBJ_SHADING:
229
+ typeName = "shading";
230
+ break;
231
+ case FPDF_PAGEOBJ_FORM:
232
+ typeName = "form";
233
+ break;
234
+ default:
235
+ typeName = "unknown";
236
+ break;
237
+ }
238
+
239
+ float left = 0, bottom = 0, right = 0, top = 0;
240
+ FPDFPageObj_GetBounds(obj, &left, &bottom, &right, &top);
241
+
242
+ Napi::Object result = Napi::Object::New(env);
243
+ result.Set("type", Napi::String::New(env, typeName));
244
+ result.Set("bounds", CreateBoundsObject(env, left, bottom, right, top));
245
+
246
+ // fill and stroke colors (all object types)
247
+ unsigned int r, g, b, a;
248
+ if (FPDFPageObj_GetFillColor(obj, &r, &g, &b, &a)) {
249
+ result.Set("fillColor", CreateColorObject(env, r, g, b, a));
250
+ } else {
251
+ result.Set("fillColor", env.Null());
252
+ }
253
+
254
+ if (FPDFPageObj_GetStrokeColor(obj, &r, &g, &b, &a)) {
255
+ result.Set("strokeColor", CreateColorObject(env, r, g, b, a));
256
+ } else {
257
+ result.Set("strokeColor", env.Null());
258
+ }
259
+
260
+ // text-specific properties
261
+ if (type == FPDF_PAGEOBJ_TEXT) {
262
+ // text content
263
+ FPDF_TEXTPAGE textPage = FPDFText_LoadPage(page_);
264
+ if (textPage) {
265
+ unsigned long byteLen = FPDFTextObj_GetText(obj, textPage, nullptr, 0);
266
+ if (byteLen > 0) {
267
+ size_t charCount = byteLen / sizeof(unsigned short);
268
+ std::vector<unsigned short> textBuf(charCount);
269
+ FPDFTextObj_GetText(obj, textPage, textBuf.data(), byteLen);
270
+ result.Set("text",
271
+ Napi::String::New(
272
+ env,
273
+ reinterpret_cast<const char16_t *>(textBuf.data()),
274
+ charCount - 1));
275
+ } else {
276
+ result.Set("text", Napi::String::New(env, ""));
277
+ }
278
+ FPDFText_ClosePage(textPage);
279
+ } else {
280
+ result.Set("text", Napi::String::New(env, ""));
281
+ }
282
+
283
+ // font size
284
+ float fontSize = 0;
285
+ if (FPDFTextObj_GetFontSize(obj, &fontSize)) {
286
+ result.Set("fontSize", Napi::Number::New(env, fontSize));
287
+ } else {
288
+ result.Set("fontSize", Napi::Number::New(env, 0));
289
+ }
290
+
291
+ // font name
292
+ FPDF_FONT font = FPDFTextObj_GetFont(obj);
293
+ if (font) {
294
+ size_t nameLen = FPDFFont_GetBaseFontName(font, nullptr, 0);
295
+ if (nameLen > 1) {
296
+ std::vector<char> nameBuf(nameLen);
297
+ FPDFFont_GetBaseFontName(font, nameBuf.data(), nameLen);
298
+ result.Set("fontName",
299
+ Napi::String::New(env, nameBuf.data(), nameLen - 1));
300
+ } else {
301
+ result.Set("fontName", Napi::String::New(env, ""));
302
+ }
303
+ } else {
304
+ result.Set("fontName", Napi::String::New(env, ""));
305
+ }
306
+ }
307
+
308
+ // image-specific properties
309
+ if (type == FPDF_PAGEOBJ_IMAGE) {
310
+ unsigned int imgWidth = 0, imgHeight = 0;
311
+ if (FPDFImageObj_GetImagePixelSize(obj, &imgWidth, &imgHeight)) {
312
+ result.Set("imageWidth", Napi::Number::New(env, imgWidth));
313
+ result.Set("imageHeight", Napi::Number::New(env, imgHeight));
314
+ } else {
315
+ result.Set("imageWidth", Napi::Number::New(env, 0));
316
+ result.Set("imageHeight", Napi::Number::New(env, 0));
317
+ }
318
+ }
319
+
320
+ return result;
321
+ }
322
+
323
+ /**
324
+ * Returns all links on the page with URL/destination and bounds.
325
+ */
326
+ Napi::Value GetLinks(const Napi::CallbackInfo &info) {
327
+ Napi::Env env = info.Env();
328
+ EnsureOpen(env);
329
+ if (env.IsExceptionPending())
330
+ return env.Null();
331
+
332
+ std::lock_guard<std::mutex> lock(g_pdfium_mutex);
333
+
334
+ Napi::Array links = Napi::Array::New(env);
335
+ int pos = 0;
336
+ FPDF_LINK link;
337
+ uint32_t idx = 0;
338
+
339
+ while (FPDFLink_Enumerate(page_, &pos, &link)) {
340
+ Napi::Object obj = Napi::Object::New(env);
341
+
342
+ // bounds
343
+ FS_RECTF rect;
344
+ if (FPDFLink_GetAnnotRect(link, &rect)) {
345
+ obj.Set("bounds", CreateBoundsFromRect(env, rect));
346
+ }
347
+
348
+ // try URI action first
349
+ FPDF_ACTION action = FPDFLink_GetAction(link);
350
+ if (action && FPDFAction_GetType(action) == PDFACTION_URI) {
351
+ unsigned long len = FPDFAction_GetURIPath(doc_, action, nullptr, 0);
352
+ if (len > 0) {
353
+ std::vector<char> buf(len);
354
+ FPDFAction_GetURIPath(doc_, action, buf.data(), len);
355
+ obj.Set("url", Napi::String::New(env, buf.data(), len - 1));
356
+ }
357
+ }
358
+
359
+ // try destination (internal link)
360
+ FPDF_DEST dest = FPDFLink_GetDest(doc_, link);
361
+ if (!dest && action) {
362
+ dest = FPDFAction_GetDest(doc_, action);
363
+ }
364
+ if (dest) {
365
+ int pageIndex = FPDFDest_GetDestPageIndex(doc_, dest);
366
+ if (pageIndex >= 0) {
367
+ obj.Set("pageIndex", Napi::Number::New(env, pageIndex));
368
+ }
369
+ }
370
+
371
+ links.Set(idx++, obj);
372
+ }
373
+ return links;
374
+ }
375
+
376
+ /**
377
+ * Searches for text on the page. Returns array of matches with charIndex and
378
+ * bounds.
379
+ */
380
+ Napi::Value Search(const Napi::CallbackInfo &info) {
381
+ Napi::Env env = info.Env();
382
+ EnsureOpen(env);
383
+ if (env.IsExceptionPending())
384
+ return env.Null();
385
+
386
+ if (info.Length() < 1 || !info[0].IsString()) {
387
+ Napi::TypeError::New(env, "Expected search string")
388
+ .ThrowAsJavaScriptException();
389
+ return env.Null();
390
+ }
391
+
392
+ std::u16string needle = info[0].As<Napi::String>().Utf16Value();
393
+ unsigned long flags = 0;
394
+ if (info.Length() > 1 && info[1].IsObject()) {
395
+ Napi::Object opts = info[1].As<Napi::Object>();
396
+ if (opts.Has("caseSensitive") &&
397
+ opts.Get("caseSensitive").As<Napi::Boolean>().Value())
398
+ flags |= FPDF_MATCHCASE;
399
+ if (opts.Has("wholeWord") &&
400
+ opts.Get("wholeWord").As<Napi::Boolean>().Value())
401
+ flags |= FPDF_MATCHWHOLEWORD;
402
+ }
403
+
404
+ std::lock_guard<std::mutex> lock(g_pdfium_mutex);
405
+
406
+ FPDF_TEXTPAGE textPage = FPDFText_LoadPage(page_);
407
+ if (!textPage) {
408
+ return Napi::Array::New(env, 0);
409
+ }
410
+
411
+ FPDF_SCHHANDLE handle = FPDFText_FindStart(
412
+ textPage, reinterpret_cast<const unsigned short *>(needle.c_str()),
413
+ flags, 0);
414
+
415
+ Napi::Array results = Napi::Array::New(env);
416
+ uint32_t idx = 0;
417
+
418
+ if (handle) {
419
+ while (FPDFText_FindNext(handle)) {
420
+ int charIdx = FPDFText_GetSchResultIndex(handle);
421
+ int count = FPDFText_GetSchCount(handle);
422
+
423
+ Napi::Object match = Napi::Object::New(env);
424
+ match.Set("charIndex", Napi::Number::New(env, charIdx));
425
+ match.Set("length", Napi::Number::New(env, count));
426
+
427
+ // get bounding rectangles for the match
428
+ int numRects = FPDFText_CountRects(textPage, charIdx, count);
429
+ Napi::Array rects = Napi::Array::New(env, numRects > 0 ? numRects : 0);
430
+ for (int r = 0; r < numRects; r++) {
431
+ double left, top, right, bottom;
432
+ if (FPDFText_GetRect(textPage, r, &left, &top, &right, &bottom)) {
433
+ rects.Set(static_cast<uint32_t>(r),
434
+ CreateBoundsObject(env, left, bottom, right, top));
435
+ }
436
+ }
437
+ match.Set("rects", rects);
438
+
439
+ results.Set(idx++, match);
440
+ }
441
+ FPDFText_FindClose(handle);
442
+ }
443
+
444
+ FPDFText_ClosePage(textPage);
445
+ return results;
446
+ }
447
+
448
+ /**
449
+ * Returns all annotations on the page.
450
+ */
451
+ Napi::Value GetAnnotations(const Napi::CallbackInfo &info) {
452
+ Napi::Env env = info.Env();
453
+ EnsureOpen(env);
454
+ if (env.IsExceptionPending())
455
+ return env.Null();
456
+
457
+ std::lock_guard<std::mutex> lock(g_pdfium_mutex);
458
+
459
+ int count = FPDFPage_GetAnnotCount(page_);
460
+ Napi::Array annotations = Napi::Array::New(env, count > 0 ? count : 0);
461
+
462
+ for (int i = 0; i < count; i++) {
463
+ FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page_, i);
464
+ if (!annot)
465
+ continue;
466
+
467
+ Napi::Object obj = Napi::Object::New(env);
468
+
469
+ // subtype
470
+ FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot);
471
+ const char *typeName;
472
+ switch (subtype) {
473
+ case FPDF_ANNOT_TEXT:
474
+ typeName = "text";
475
+ break;
476
+ case FPDF_ANNOT_LINK:
477
+ typeName = "link";
478
+ break;
479
+ case FPDF_ANNOT_FREETEXT:
480
+ typeName = "freetext";
481
+ break;
482
+ case FPDF_ANNOT_LINE:
483
+ typeName = "line";
484
+ break;
485
+ case FPDF_ANNOT_SQUARE:
486
+ typeName = "square";
487
+ break;
488
+ case FPDF_ANNOT_CIRCLE:
489
+ typeName = "circle";
490
+ break;
491
+ case FPDF_ANNOT_HIGHLIGHT:
492
+ typeName = "highlight";
493
+ break;
494
+ case FPDF_ANNOT_UNDERLINE:
495
+ typeName = "underline";
496
+ break;
497
+ case FPDF_ANNOT_SQUIGGLY:
498
+ typeName = "squiggly";
499
+ break;
500
+ case FPDF_ANNOT_STRIKEOUT:
501
+ typeName = "strikeout";
502
+ break;
503
+ case FPDF_ANNOT_STAMP:
504
+ typeName = "stamp";
505
+ break;
506
+ case FPDF_ANNOT_INK:
507
+ typeName = "ink";
508
+ break;
509
+ case FPDF_ANNOT_POPUP:
510
+ typeName = "popup";
511
+ break;
512
+ case FPDF_ANNOT_WIDGET:
513
+ typeName = "widget";
514
+ break;
515
+ case FPDF_ANNOT_REDACT:
516
+ typeName = "redact";
517
+ break;
518
+ default:
519
+ typeName = "unknown";
520
+ break;
521
+ }
522
+ obj.Set("type", Napi::String::New(env, typeName));
523
+
524
+ // bounds
525
+ FS_RECTF rect;
526
+ if (FPDFAnnot_GetRect(annot, &rect)) {
527
+ obj.Set("bounds", CreateBoundsFromRect(env, rect));
528
+ }
529
+
530
+ // contents (the /Contents key)
531
+ unsigned long contentsLen =
532
+ FPDFAnnot_GetStringValue(annot, "Contents", nullptr, 0);
533
+ if (contentsLen > 2) {
534
+ std::vector<unsigned short> contentsBuf(contentsLen /
535
+ sizeof(unsigned short));
536
+ FPDFAnnot_GetStringValue(
537
+ annot, "Contents",
538
+ reinterpret_cast<FPDF_WCHAR *>(contentsBuf.data()), contentsLen);
539
+ size_t charCount = contentsLen / sizeof(unsigned short) - 1;
540
+ obj.Set("contents",
541
+ Napi::String::New(
542
+ env, reinterpret_cast<const char16_t *>(contentsBuf.data()),
543
+ charCount));
544
+ } else {
545
+ obj.Set("contents", Napi::String::New(env, ""));
546
+ }
547
+
548
+ // color
549
+ unsigned int r, g, b, a;
550
+ if (FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &r, &g, &b,
551
+ &a)) {
552
+ obj.Set("color", CreateColorObject(env, r, g, b, a));
553
+ } else {
554
+ obj.Set("color", env.Null());
555
+ }
556
+
557
+ annotations.Set(static_cast<uint32_t>(i), obj);
558
+ FPDFPage_CloseAnnot(annot);
559
+ }
560
+ return annotations;
561
+ }
562
+
563
+ /**
564
+ * Closes the page and releases its resources.
565
+ */
566
+ Napi::Value Close(const Napi::CallbackInfo &info) {
567
+ if (page_) {
568
+ alive_->store(false);
569
+ std::lock_guard<std::mutex> lock(g_pdfium_mutex);
570
+ FPDF_ClosePage(page_);
571
+ page_ = nullptr;
572
+ doc_ = nullptr;
573
+ }
574
+ return info.Env().Undefined();
575
+ }
576
+ };