pdfium-native 0.1.7 → 0.2.2

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/src/page.h CHANGED
@@ -1,5 +1,6 @@
1
1
  #pragma once
2
2
 
3
+ #include "page_workers.h"
3
4
  #include "render_worker.h"
4
5
 
5
6
  #include <algorithm>
@@ -7,11 +8,6 @@
7
8
  #include <memory>
8
9
  #include <string>
9
10
 
10
- #include "fpdf_annot.h"
11
- #include "fpdf_doc.h"
12
- #include "fpdf_edit.h"
13
- #include "fpdf_text.h"
14
-
15
11
  // forward declarations
16
12
  class GetPageWorker;
17
13
  class LoadDocumentWorker;
@@ -28,7 +24,7 @@ public:
28
24
  {
29
25
  InstanceAccessor<&PDFiumPage::GetNumber>("number"),
30
26
  InstanceMethod<&PDFiumPage::GetText>("getText"),
31
- InstanceMethod<&PDFiumPage::RenderAsync>("render"),
27
+ InstanceMethod<&PDFiumPage::Render>("render"),
32
28
  InstanceMethod<&PDFiumPage::GetObject>("getObject"),
33
29
  InstanceMethod<&PDFiumPage::GetLinks>("getLinks"),
34
30
  InstanceMethod<&PDFiumPage::Search>("search"),
@@ -41,10 +37,13 @@ public:
41
37
  : Napi::ObjectWrap<PDFiumPage>(info),
42
38
  alive_(std::make_shared<std::atomic<bool>>(true)) {}
43
39
 
44
- void SetPage(FPDF_PAGE page, FPDF_DOCUMENT doc, int index) {
40
+ void SetPage(FPDF_PAGE page, FPDF_DOCUMENT doc, int index, float width,
41
+ float height) {
45
42
  page_ = page;
46
43
  doc_ = doc;
47
44
  index_ = index;
45
+ width_ = width;
46
+ height_ = height;
48
47
  }
49
48
 
50
49
  // called by document to give this page a reference to the document's alive
@@ -60,6 +59,8 @@ private:
60
59
  FPDF_PAGE page_ = nullptr;
61
60
  FPDF_DOCUMENT doc_ = nullptr;
62
61
  int index_ = -1;
62
+ float width_ = 0;
63
+ float height_ = 0;
63
64
  std::shared_ptr<std::atomic<bool>> alive_;
64
65
  std::shared_ptr<std::atomic<bool>> docAlive_;
65
66
 
@@ -83,7 +84,7 @@ private:
83
84
  }
84
85
 
85
86
  /**
86
- * Extracts all text from the page.
87
+ * Extracts all text from the page (async). Returns a Promise<string>.
87
88
  */
88
89
  Napi::Value GetText(const Napi::CallbackInfo &info) {
89
90
  Napi::Env env = info.Env();
@@ -91,41 +92,25 @@ private:
91
92
  if (env.IsExceptionPending())
92
93
  return env.Null();
93
94
 
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);
95
+ auto *worker = new GetTextWorker(env, page_, alive_, docAlive_);
96
+ auto promise = worker->Promise();
97
+ worker->Queue();
98
+ return promise;
116
99
  }
117
100
 
118
101
  /**
119
102
  * Renders the page to a JPEG or PNG buffer (async).
120
103
  */
121
- Napi::Value RenderAsync(const Napi::CallbackInfo &info) {
104
+ Napi::Value Render(const Napi::CallbackInfo &info) {
122
105
  Napi::Env env = info.Env();
123
106
  EnsureOpen(env);
124
107
  if (env.IsExceptionPending())
125
108
  return env.Null();
126
109
 
127
- float origWidth = FPDF_GetPageWidthF(page_);
128
- float origHeight = FPDF_GetPageHeightF(page_);
110
+ // use cached dimensions (set under mutex in GetPageWorker) to avoid
111
+ // PDFium API calls on the main thread that could race with workers
112
+ float origWidth = width_;
113
+ float origHeight = height_;
129
114
  double scale = 1.0;
130
115
  int renderWidth = 0;
131
116
  int renderHeight = 0;
@@ -190,7 +175,7 @@ private:
190
175
  }
191
176
 
192
177
  /**
193
- * Returns a page object at the given index with type and bounds.
178
+ * Returns a page object at the given index with type and bounds (async).
194
179
  */
195
180
  Napi::Value GetObject(const Napi::CallbackInfo &info) {
196
181
  Napi::Env env = info.Env();
@@ -204,124 +189,15 @@ private:
204
189
  return env.Null();
205
190
  }
206
191
 
207
- std::lock_guard<std::mutex> lock(g_pdfium_mutex);
208
-
209
192
  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;
193
+ auto *worker = new GetObjectWorker(env, page_, idx, alive_, docAlive_);
194
+ auto promise = worker->Promise();
195
+ worker->Queue();
196
+ return promise;
321
197
  }
322
198
 
323
199
  /**
324
- * Returns all links on the page with URL/destination and bounds.
200
+ * Returns all links on the page (async). Returns a Promise<Link[]>.
325
201
  */
326
202
  Napi::Value GetLinks(const Napi::CallbackInfo &info) {
327
203
  Napi::Env env = info.Env();
@@ -329,53 +205,14 @@ private:
329
205
  if (env.IsExceptionPending())
330
206
  return env.Null();
331
207
 
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;
208
+ auto *worker = new GetLinksWorker(env, page_, doc_, alive_, docAlive_);
209
+ auto promise = worker->Promise();
210
+ worker->Queue();
211
+ return promise;
374
212
  }
375
213
 
376
214
  /**
377
- * Searches for text on the page. Returns array of matches with charIndex and
378
- * bounds.
215
+ * Searches for text on the page (async). Returns a Promise<SearchMatch[]>.
379
216
  */
380
217
  Napi::Value Search(const Napi::CallbackInfo &info) {
381
218
  Napi::Env env = info.Env();
@@ -401,52 +238,16 @@ private:
401
238
  flags |= FPDF_MATCHWHOLEWORD;
402
239
  }
403
240
 
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;
241
+ auto *worker = new SearchWorker(env, page_, std::move(needle), flags,
242
+ alive_, docAlive_);
243
+ auto promise = worker->Promise();
244
+ worker->Queue();
245
+ return promise;
446
246
  }
447
247
 
448
248
  /**
449
- * Returns all annotations on the page.
249
+ * Returns all annotations on the page (async). Returns a
250
+ * Promise<Annotation[]>.
450
251
  */
451
252
  Napi::Value GetAnnotations(const Napi::CallbackInfo &info) {
452
253
  Napi::Env env = info.Env();
@@ -454,123 +255,32 @@ private:
454
255
  if (env.IsExceptionPending())
455
256
  return env.Null();
456
257
 
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;
258
+ auto *worker = new GetAnnotationsWorker(env, page_, alive_, docAlive_);
259
+ auto promise = worker->Promise();
260
+ worker->Queue();
261
+ return promise;
561
262
  }
562
263
 
563
264
  /**
564
265
  * Closes the page and releases its resources.
565
266
  */
566
267
  Napi::Value Close(const Napi::CallbackInfo &info) {
268
+ CleanUp();
269
+ return info.Env().Undefined();
270
+ }
271
+
272
+ /**
273
+ * GC destructor — releases the page if the user forgot to call close().
274
+ */
275
+ void Finalize(Napi::Env /*env*/) override { CleanUp(); }
276
+
277
+ void CleanUp() {
567
278
  if (page_) {
568
- alive_->store(false);
569
279
  std::lock_guard<std::mutex> lock(g_pdfium_mutex);
280
+ alive_->store(false);
570
281
  FPDF_ClosePage(page_);
571
282
  page_ = nullptr;
572
283
  doc_ = nullptr;
573
284
  }
574
- return info.Env().Undefined();
575
285
  }
576
286
  };