pdfium-native 0.1.7 → 0.2.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.
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"),
@@ -83,7 +79,7 @@ private:
83
79
  }
84
80
 
85
81
  /**
86
- * Extracts all text from the page.
82
+ * Extracts all text from the page (async). Returns a Promise<string>.
87
83
  */
88
84
  Napi::Value GetText(const Napi::CallbackInfo &info) {
89
85
  Napi::Env env = info.Env();
@@ -91,34 +87,16 @@ private:
91
87
  if (env.IsExceptionPending())
92
88
  return env.Null();
93
89
 
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);
90
+ auto *worker = new GetTextWorker(env, page_, alive_, docAlive_);
91
+ auto promise = worker->Promise();
92
+ worker->Queue();
93
+ return promise;
116
94
  }
117
95
 
118
96
  /**
119
97
  * Renders the page to a JPEG or PNG buffer (async).
120
98
  */
121
- Napi::Value RenderAsync(const Napi::CallbackInfo &info) {
99
+ Napi::Value Render(const Napi::CallbackInfo &info) {
122
100
  Napi::Env env = info.Env();
123
101
  EnsureOpen(env);
124
102
  if (env.IsExceptionPending())
@@ -190,7 +168,7 @@ private:
190
168
  }
191
169
 
192
170
  /**
193
- * Returns a page object at the given index with type and bounds.
171
+ * Returns a page object at the given index with type and bounds (async).
194
172
  */
195
173
  Napi::Value GetObject(const Napi::CallbackInfo &info) {
196
174
  Napi::Env env = info.Env();
@@ -204,124 +182,15 @@ private:
204
182
  return env.Null();
205
183
  }
206
184
 
207
- std::lock_guard<std::mutex> lock(g_pdfium_mutex);
208
-
209
185
  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;
186
+ auto *worker = new GetObjectWorker(env, page_, idx, alive_, docAlive_);
187
+ auto promise = worker->Promise();
188
+ worker->Queue();
189
+ return promise;
321
190
  }
322
191
 
323
192
  /**
324
- * Returns all links on the page with URL/destination and bounds.
193
+ * Returns all links on the page (async). Returns a Promise<Link[]>.
325
194
  */
326
195
  Napi::Value GetLinks(const Napi::CallbackInfo &info) {
327
196
  Napi::Env env = info.Env();
@@ -329,53 +198,14 @@ private:
329
198
  if (env.IsExceptionPending())
330
199
  return env.Null();
331
200
 
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;
201
+ auto *worker = new GetLinksWorker(env, page_, doc_, alive_, docAlive_);
202
+ auto promise = worker->Promise();
203
+ worker->Queue();
204
+ return promise;
374
205
  }
375
206
 
376
207
  /**
377
- * Searches for text on the page. Returns array of matches with charIndex and
378
- * bounds.
208
+ * Searches for text on the page (async). Returns a Promise<SearchMatch[]>.
379
209
  */
380
210
  Napi::Value Search(const Napi::CallbackInfo &info) {
381
211
  Napi::Env env = info.Env();
@@ -401,52 +231,16 @@ private:
401
231
  flags |= FPDF_MATCHWHOLEWORD;
402
232
  }
403
233
 
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;
234
+ auto *worker = new SearchWorker(env, page_, std::move(needle), flags,
235
+ alive_, docAlive_);
236
+ auto promise = worker->Promise();
237
+ worker->Queue();
238
+ return promise;
446
239
  }
447
240
 
448
241
  /**
449
- * Returns all annotations on the page.
242
+ * Returns all annotations on the page (async). Returns a
243
+ * Promise<Annotation[]>.
450
244
  */
451
245
  Napi::Value GetAnnotations(const Napi::CallbackInfo &info) {
452
246
  Napi::Env env = info.Env();
@@ -454,116 +248,26 @@ private:
454
248
  if (env.IsExceptionPending())
455
249
  return env.Null();
456
250
 
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;
251
+ auto *worker = new GetAnnotationsWorker(env, page_, alive_, docAlive_);
252
+ auto promise = worker->Promise();
253
+ worker->Queue();
254
+ return promise;
561
255
  }
562
256
 
563
257
  /**
564
258
  * Closes the page and releases its resources.
565
259
  */
566
260
  Napi::Value Close(const Napi::CallbackInfo &info) {
261
+ CleanUp();
262
+ return info.Env().Undefined();
263
+ }
264
+
265
+ /**
266
+ * GC destructor — releases the page if the user forgot to call close().
267
+ */
268
+ void Finalize(Napi::Env /*env*/) override { CleanUp(); }
269
+
270
+ void CleanUp() {
567
271
  if (page_) {
568
272
  alive_->store(false);
569
273
  std::lock_guard<std::mutex> lock(g_pdfium_mutex);
@@ -571,6 +275,5 @@ private:
571
275
  page_ = nullptr;
572
276
  doc_ = nullptr;
573
277
  }
574
- return info.Env().Undefined();
575
278
  }
576
279
  };