diff --git a/package-lock.json b/package-lock.json index 643cc80..c4684d3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4224,6 +4224,9 @@ "irregular-plurals": "1.4.0" } }, + "podofo": { + "version": "github:corymickelson/podofo#46033ca4c0fa0a551330cc0d6cadd79700c780d4" + }, "prebuild-install": { "version": "2.5.3", "resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-2.5.3.tgz", diff --git a/spec/unit/page.ts b/spec/unit/page.ts index 525afc0..e6b4d64 100644 --- a/spec/unit/page.ts +++ b/spec/unit/page.ts @@ -163,8 +163,8 @@ export function runAll() { pageGetAnnotsCount, pageGetAnnot, pageContents, - pageResources //, - // pageAddImg + pageResources, + pageAddImg ].map(i => runTest(i)) } diff --git a/spec/unit/signer.ts b/spec/unit/signer.ts index 791d2be..34add88 100644 --- a/spec/unit/signer.ts +++ b/spec/unit/signer.ts @@ -12,15 +12,8 @@ tap('Signer', sub => { standard.plan(3) if (e instanceof Error) throw e try { - if ((doc.form as IForm).dictionary.hasKey('SigFlags') || - (doc.form as IForm).dictionary.getKey('SigFlags').type !== 'Number' || - (doc.form as IForm).dictionary.getKey('SigFlags').getNumber() !== 3) { - (doc.form as IForm).dictionary.removeKey('SigFlags'); - (doc.form as IForm).dictionary.addKey('SigFlags', 3) - } - if ((doc.form as IForm).needAppearances) - (doc.form as IForm).needAppearances = false - + doc.form.SigFlags = 3 + doc.form.needAppearances = false const rect = new npdf.Rect(0, 0, 10, 10), page = doc.getPage(1), annot = page.createAnnotation(NPDFAnnotation.Widget, rect) @@ -46,15 +39,6 @@ tap('Signer', sub => { standard.assert(signed.getPage(1).getFields().filter(i => i instanceof npdf.SignatureField).length === 1) standard.assert(signed.form.SigFlags === 3) standard.end() - // - // const signedPage = doc.getPage(1), - // fields = signedPage.getFields() - // let signatureFieldCandidates = fields.filter(i => i instanceof npdf.SignatureField) - // if (!signatureFieldCandidates || signatureFieldCandidates.length === 0) standard.fail("signature field not found") - // else if (signatureFieldCandidates.length === 1) { - // standard.pass("signature found") - // } - // else standard.fail("something went wrong") }) } }) diff --git a/src/base/Array.cc b/src/base/Array.cc index bfc2c56..3b0e49b 100644 --- a/src/base/Array.cc +++ b/src/base/Array.cc @@ -25,10 +25,7 @@ using namespace Napi; using namespace PoDoFo; -using std::make_unique; -using std::shared_ptr; using std::string; -using std::unique_ptr; namespace NoPoDoFo { @@ -36,24 +33,17 @@ Napi::FunctionReference Array::constructor; // NOLINT Array::Array(const CallbackInfo& info) : ObjectWrap(info) + , obj(Obj::Unwrap(info[0].As())->GetObject()) +{} + +Array::~Array() { - if (info[0].IsObject() && - info[0].As().InstanceOf(Obj::constructor.Value())) { - PdfObject* obj = Obj::Unwrap(info[0].As())->obj; - // array = make_unique(*obj); - array = &obj->GetArray(); - } else if (info[0].IsExternal()) { - array = info[0].As>().Data(); - // array = - // make_unique(*info[0].As>().Data()); - } else { - TypeError::New( - info.Env(), - "Constructor requires instance of Obj, or an external PdfArray ptr") - .ThrowAsJavaScriptException(); + HandleScope scope(Env()); + obj = nullptr; + for (auto child : children) { + delete child; } } - void Array::Initialize(Napi::Env& env, Napi::Object& target) { @@ -71,33 +61,23 @@ Array::Initialize(Napi::Env& env, Napi::Object& target) InstanceMethod("write", &Array::Write), InstanceMethod("push", &Array::Push), InstanceMethod("pop", &Array::Pop), - InstanceMethod("clear", &Array::Clear), - InstanceMethod("eq", &Array::Eq) }); + InstanceMethod("clear", &Array::Clear) }); constructor = Persistent(ctor); constructor.SuppressDestruct(); target.Set("Array", ctor); } -Napi::Value -Array::Eq(const CallbackInfo& info) -{ - auto wrap = info[0].As(); - if (!wrap.InstanceOf(Array::constructor.Value())) { - throw Error::New(info.Env(), "Must be an instance of NoPoDoFo Obj"); - } - auto value = Array::Unwrap(wrap); - return Boolean::New(info.Env(), value->GetArray() == GetArray()); -} + Napi::Value Array::GetImmutable(const CallbackInfo& info) { - return Boolean::New(info.Env(), GetArray()->GetImmutable()); + return Boolean::New(info.Env(), GetArray().GetImmutable()); } void Array::SetImmutable(const CallbackInfo& info, const Napi::Value& value) { try { - GetArray()->SetImmutable(value.As()); + GetArray().SetImmutable(value.As()); } catch (PdfError& err) { ErrorHandler(err, info); } @@ -105,33 +85,33 @@ Array::SetImmutable(const CallbackInfo& info, const Napi::Value& value) Napi::Value Array::Length(const Napi::CallbackInfo& info) { - return Number::New(info.Env(), GetArray()->size()); + return Number::New(info.Env(), GetArray().size()); } void Array::Write(const CallbackInfo& info) { string output = info[0].As().Utf8Value(); PdfOutputDevice device(output.c_str()); - GetArray()->Write(&device, ePdfWriteMode_Default); + GetArray().Write(&device, ePdfWriteMode_Default); } Napi::Value Array::ContainsString(const CallbackInfo& info) { string searchString = info[0].As().Utf8Value(); - bool match = GetArray()->ContainsString(searchString); + bool match = GetArray().ContainsString(searchString); return Napi::Boolean::New(info.Env(), match); } Napi::Value Array::GetStringIndex(const CallbackInfo& info) { string str = info[0].As().Utf8Value(); - return Napi::Number::New(info.Env(), GetArray()->GetStringIndex(str)); + return Napi::Number::New(info.Env(), GetArray().GetStringIndex(str)); } Napi::Value Array::IsDirty(const CallbackInfo& info) { - return Napi::Boolean::New(info.Env(), GetArray()->IsDirty()); + return Napi::Boolean::New(info.Env(), GetArray().IsDirty()); } Napi::Value @@ -146,7 +126,7 @@ Array::SetDirty(const CallbackInfo& info, const Napi::Value& value) if (!value.IsBoolean()) { throw Napi::Error::New(info.Env(), "dirty must be of type boolean"); } - GetArray()->SetDirty(value.As()); + GetArray().SetDirty(value.As()); } void @@ -158,7 +138,7 @@ Array::Push(const CallbackInfo& info) } try { auto item = Obj::Unwrap(wrapper); - GetArray()->push_back(*item->obj); + GetArray().push_back(*item->GetObject()); } catch (PdfError& err) { ErrorHandler(err, info); @@ -177,14 +157,16 @@ Value Array::GetObjAtIndex(const CallbackInfo& info) { size_t index = info[0].As().Uint32Value(); - if (index > GetArray()->size()) { + if (index > GetArray().size()) { throw Napi::RangeError(); } PdfObject item(GetArray()[index]); if (item.IsReference()) { item = *item.GetOwner()->GetObject(item.GetReference()); } - auto initPtr = Napi::External::New(info.Env(), &item); + auto child = new PdfObject(item); + children.push_back(child); + auto initPtr = Napi::External::New(info.Env(), child); auto instance = Obj::constructor.New({ initPtr }); return instance; } @@ -195,7 +177,7 @@ Array::ToArray(const Napi::CallbackInfo& info) auto js = Napi::Array::New(info.Env()); try { uint32_t counter = 0; - for (auto& it : *GetArray()) { + for (auto& it : GetArray()) { PdfObject* item; if (it.IsReference()) { item = it.GetOwner()->GetObject(it.GetReference()); @@ -219,7 +201,7 @@ void Array::Clear(const CallbackInfo& info) { try { - GetArray()->Clear(); + GetArray().Clear(); } catch (PdfError& err) { ErrorHandler(err, info); } diff --git a/src/base/Array.h b/src/base/Array.h index cd4fb9c..96f4414 100644 --- a/src/base/Array.h +++ b/src/base/Array.h @@ -23,12 +23,14 @@ #include #include +using std::vector; + namespace NoPoDoFo { class Array : public Napi::ObjectWrap { public: explicit Array(const Napi::CallbackInfo&); - ~Array() { array = nullptr; } + ~Array(); static Napi::FunctionReference constructor; static void Initialize(Napi::Env& env, Napi::Object& target); @@ -46,11 +48,11 @@ class Array : public Napi::ObjectWrap Napi::Value Pop(const Napi::CallbackInfo&); void Clear(const Napi::CallbackInfo&); Napi::Value Eq(const Napi::CallbackInfo&); - PoDoFo::PdfArray* GetArray() { return array; } + PoDoFo::PdfArray& GetArray() { return obj->GetArray(); } private: - // std::unique_ptr array; - PoDoFo::PdfArray* array; + PoDoFo::PdfObject* obj; + vector children; Napi::Value GetObjAtIndex(const Napi::CallbackInfo&); }; } diff --git a/src/base/Dictionary.cc b/src/base/Dictionary.cc index 68a812b..44594a6 100644 --- a/src/base/Dictionary.cc +++ b/src/base/Dictionary.cc @@ -28,9 +28,7 @@ using namespace PoDoFo; using std::cout; using std::endl; -using std::make_shared; using std::map; -using std::shared_ptr; using std::string; using std::vector; @@ -45,20 +43,13 @@ FunctionReference Dictionary::constructor; // NOLINT */ Dictionary::Dictionary(const CallbackInfo& info) : ObjectWrap(info) + , obj(info[0].As>().Data()) +{} + +Dictionary::~Dictionary() { - if (info[0].IsExternal()) { - auto iObj = info[0].As>(); - // nObj = Obj::constructor.New( { iObj }); - // obj = Obj::Unwrap(nObj.As())->GetObject(); - obj = iObj.Data(); // make_shared(*iObj.Data()); - } - if (info[0].As().InstanceOf(Obj::constructor.Value())) { - obj = Obj::Unwrap(info[0].As())->obj; - } else { - TypeError::New(info.Env(), "requires Obj as parameter to constructor") - .ThrowAsJavaScriptException(); - cout << "Obj required" << endl; - } + HandleScope scope(Env()); + obj = nullptr; } void @@ -68,9 +59,7 @@ Dictionary::Initialize(Napi::Env& env, Napi::Object& target) Function ctor = DefineClass( env, "Dictionary", - { // StaticMethod("tryGetDictionary", &Dictionary::ResolveDictionary), - // InstanceAccessor("object", &Dictionary::GetObject, nullptr), - InstanceMethod("getKey", &Dictionary::GetKey), + { InstanceMethod("getKey", &Dictionary::GetKey), InstanceMethod("getKeys", &Dictionary::GetKeys), InstanceMethod("hasKey", &Dictionary::HasKey), InstanceMethod("addKey", &Dictionary::AddKey), @@ -89,28 +78,6 @@ Dictionary::Initialize(Napi::Env& env, Napi::Object& target) target.Set("Dictionary", ctor); } -// Value -// Dictionary::ResolveDictionary(const CallbackInfo& info) -//{ -// auto doc = Document::Unwrap(info[0].As()); -// auto value = Obj::Unwrap(info[1].As())->GetObject(); -// if (value->IsReference()) { -// value = doc->GetMemDocument()->GetObjects().GetObject(value->Reference()); -// } -// return Dictionary::constructor.New( -// { External::New(info.Env(), new PdfObject(*value)) }); -//} - -// PdfDictionary& -// Dictionary::Resolve(PdfDocument* doc, PdfObject* candidate) -//{ -// if (candidate->IsDictionary()) { -// return candidate->GetDictionary(); -// } else if (candidate->IsReference()) { -// return Resolve(doc, -// doc->GetObjects()->GetObject(candidate->GetReference())); -// } -//} Napi::Value Dictionary::Eq(const CallbackInfo& info) { @@ -142,7 +109,7 @@ Dictionary::AddKey(const CallbackInfo& info) } else if (v.IsObject() && v.As().InstanceOf(Obj::constructor.Value())) { auto objWrap = info[1].As(); - PdfObject* obj = Obj::Unwrap(objWrap)->obj; + auto obj = Obj::Unwrap(objWrap)->GetObject(); if (obj->IsDictionary()) { GetDictionary().AddKey(key, obj->Reference()); cout << "Adding key: " << key.GetName() << " as reference#" diff --git a/src/base/Dictionary.h b/src/base/Dictionary.h index 345e6d9..284e43b 100644 --- a/src/base/Dictionary.h +++ b/src/base/Dictionary.h @@ -31,7 +31,7 @@ class Dictionary : public Napi::ObjectWrap { public: explicit Dictionary(const Napi::CallbackInfo&); - ~Dictionary() { obj = nullptr; } + ~Dictionary(); static Napi::FunctionReference constructor; static void Initialize(Napi::Env& env, Napi::Object& target); void AddKey(const Napi::CallbackInfo&); @@ -48,13 +48,10 @@ class Dictionary : public Napi::ObjectWrap Napi::Value Write(const Napi::CallbackInfo&); void WriteSync(const Napi::CallbackInfo&); Napi::Value Eq(const Napi::CallbackInfo&); - // Napi::Value GetObject(const Napi::CallbackInfo&) { return nObj; } PoDoFo::PdfDictionary& GetDictionary() { return obj->GetDictionary(); } private: - // std::shared_ptr obj; PoDoFo::PdfObject* obj; - // Napi::Value nObj; }; } #endif diff --git a/src/base/Obj.cc b/src/base/Obj.cc index 5342073..5bc2aeb 100644 --- a/src/base/Obj.cc +++ b/src/base/Obj.cc @@ -28,7 +28,6 @@ using namespace PoDoFo; using std::cout; using std::endl; -using std::make_shared; using std::string; namespace NoPoDoFo { @@ -70,7 +69,8 @@ Obj::Obj(const Napi::CallbackInfo& info) : ObjectWrap(info) , obj(info[0].As>().Data()) { - // obj = make_shared(*info[0].As>().Data()); + // Objects are managed by the Document. + // This class will NOT manage Obj resources. } void @@ -276,7 +276,8 @@ Obj::GetDictionary(const CallbackInfo& info) if (!obj->IsDictionary()) { throw Napi::Error::New(info.Env(), "Obj only accessible as Dictionary"); } - return Dictionary::constructor.New({ this->Value() }); + return Dictionary::constructor.New( + { External::New(info.Env(), obj) }); } Napi::Value @@ -302,7 +303,7 @@ class ObjOffsetAsync : public Napi::AsyncWorker void Execute() override { try { - auto o = obj->obj; + auto o = obj->GetObject(); value = o->GetByteOffset(arg.c_str(), ePdfWriteMode_Default); } catch (PdfError& err) { SetError(ErrorHandler::WriteMsg(err)); @@ -347,7 +348,7 @@ class ObjWriteAsync : public Napi::AsyncWorker { try { PdfOutputDevice device(arg.c_str()); - obj->obj->WriteObject(&device, ePdfWriteMode_Default, nullptr); + obj->GetObject()->WriteObject(&device, ePdfWriteMode_Default, nullptr); } catch (PdfError& err) { SetError(ErrorHandler::WriteMsg(err)); } catch (Napi::Error& err) { diff --git a/src/base/Obj.h b/src/base/Obj.h index 2ac31a9..a2b39fc 100644 --- a/src/base/Obj.h +++ b/src/base/Obj.h @@ -58,12 +58,10 @@ class Obj : public Napi::ObjectWrap void SetImmutable(const Napi::CallbackInfo&, const Napi::Value&); void Clear(const Napi::CallbackInfo&); Napi::Value Eq(const Napi::CallbackInfo&); - - // std::shared_ptr GetObject() { return obj; } - - PoDoFo::PdfObject* obj; + PoDoFo::PdfObject* GetObject() { return &(*obj); } private: + PoDoFo::PdfObject* obj; // std::shared_ptr obj; }; } diff --git a/src/doc/Action.cc b/src/doc/Action.cc index bd45b92..59ea742 100644 --- a/src/doc/Action.cc +++ b/src/doc/Action.cc @@ -5,8 +5,8 @@ #include "Action.h" #include "../base/Dictionary.h" #include "../base/Obj.h" -#include "StreamDocument.h" #include "Document.h" +#include "StreamDocument.h" using namespace PoDoFo; using namespace Napi; @@ -89,11 +89,9 @@ Action::GetObject(const Napi::CallbackInfo& info) { PdfObject* o = GetAction()->GetObject(); if (GetAction()->GetObject()->IsReference()) { - o = doc->GetObjects()->GetObject( - GetAction()->GetObject()->GetReference()); + o = doc->GetObjects()->GetObject(GetAction()->GetObject()->GetReference()); } - return Obj::constructor.New({ External::New( - info.Env(), o) }); + return Obj::constructor.New({ External::New(info.Env(), o) }); } void Action::AddToDictionary(const Napi::CallbackInfo& info) diff --git a/src/doc/BaseDocument.cc b/src/doc/BaseDocument.cc index 6e7cc9e..af19982 100644 --- a/src/doc/BaseDocument.cc +++ b/src/doc/BaseDocument.cc @@ -79,11 +79,6 @@ BaseDocument::BaseDocument(const Napi::CallbackInfo& info) } } -BaseDocument::~BaseDocument() -{ - cout << "Ptr Count" << document.use_count() << endl; - std::cout << "Destructing BaseDocument" << std::endl; -} Napi::Value BaseDocument::GetPageCount(const CallbackInfo& info) @@ -95,10 +90,8 @@ Napi::Value BaseDocument::GetPage(const CallbackInfo& info) { int n = info[0].As(); - auto instance = - Page::constructor.New({ External::New(info.Env(), this), - Number::New(info.Env(), n) }); - return instance; + return Page::constructor.New( + { External::New(info.Env(), document->GetPage(n)) }); } void @@ -279,7 +272,7 @@ BaseDocument::GetObjects(const CallbackInfo& info) Napi::Value BaseDocument::GetObject(const CallbackInfo& info) { - auto ref = Obj::Unwrap(info[0].As())->obj; + auto ref = Obj::Unwrap(info[0].As())->GetObject(); PdfObject* target = document->GetObjects()->GetObject(ref->GetReference()); return Obj::constructor.New({ External::New(info.Env(), target) }); } @@ -505,7 +498,7 @@ BaseDocument::GetAttachment(const CallbackInfo& info) void BaseDocument::AddNamedDestination(const Napi::CallbackInfo& info) { - PdfPage* page = Page::Unwrap(info[0].As())->GetPage(); + PdfPage* page = Page::Unwrap(info[0].As())->page; EPdfDestinationFit fit = static_cast(info[1].As().Int32Value()); string name = info[2].As().Utf8Value(); diff --git a/src/doc/BaseDocument.h b/src/doc/BaseDocument.h index 739126d..7e5667f 100644 --- a/src/doc/BaseDocument.h +++ b/src/doc/BaseDocument.h @@ -20,9 +20,12 @@ #ifndef NPDF_BASEDOCUMENT_H #define NPDF_BASEDOCUMENT_H +#include #include #include +using std::cout; +using std::endl; using std::string; namespace NoPoDoFo { @@ -30,7 +33,6 @@ class BaseDocument { public: explicit BaseDocument(const Napi::CallbackInfo&); - ~BaseDocument(); Napi::Value GetPageCount(const Napi::CallbackInfo&); virtual Napi::Value GetPage(const Napi::CallbackInfo&); Napi::Value GetObjects(const Napi::CallbackInfo&); @@ -69,6 +71,7 @@ class BaseDocument std::shared_ptr GetBaseDocument() { auto shared = document; + cout << "Base Document count: " << document.use_count() << endl; return shared; } bool created() { return create; } diff --git a/src/doc/Button.cc b/src/doc/Button.cc index 6572543..9e10744 100644 --- a/src/doc/Button.cc +++ b/src/doc/Button.cc @@ -33,13 +33,7 @@ using std::shared_ptr; namespace NoPoDoFo { Button::Button(shared_ptr field) { - this->field = field; - button = make_shared(*this->field.get()); -} -Button::~Button() -{ - cout << "Destructing Button" << endl; - cout << "Button field use count: " << field.use_count() << endl; + button = make_shared(*field.get()); } Napi::Value Button::GetCaption(const Napi::CallbackInfo& info) @@ -47,7 +41,7 @@ Button::GetCaption(const Napi::CallbackInfo& info) return String::New(info.Env(), button->GetCaption().GetStringUtf8()); } void -Button::SetCaption(const Napi::CallbackInfo& info, const Napi::Value& value) +Button::SetCaption(const Napi::CallbackInfo&, const Napi::Value& value) { button->SetCaption(PdfString(value.As().Utf8Value())); } diff --git a/src/doc/Button.h b/src/doc/Button.h index 74a67a0..ce7ba23 100644 --- a/src/doc/Button.h +++ b/src/doc/Button.h @@ -29,13 +29,12 @@ namespace NoPoDoFo { class Button { public: - explicit Button(std::shared_ptr field); - ~Button(); + explicit Button(std::shared_ptr); Napi::Value GetCaption(const Napi::CallbackInfo&); void SetCaption(const Napi::CallbackInfo&, const Napi::Value&); std::shared_ptr button; - std::shared_ptr field; + // std::shared_ptr field; }; } #endif // NPDF_BUTTON_H diff --git a/src/doc/Destination.cc b/src/doc/Destination.cc index 27f27ed..d4d9a73 100644 --- a/src/doc/Destination.cc +++ b/src/doc/Destination.cc @@ -17,17 +17,17 @@ * along with this program. If not, see . */ -#include #include "Destination.h" #include "./Page.h" +#include using namespace PoDoFo; using namespace Napi; -using std::string; using std::cout; using std::endl; using std::make_unique; +using std::string; namespace NoPoDoFo { @@ -37,9 +37,10 @@ Destination::Destination(const CallbackInfo& info) : ObjectWrap(info) { if (info.Length() == 1 && info[0].Type() == napi_external) { - destination = make_unique(*info[0].As>().Data()); + destination = make_unique( + *info[0].As>().Data()); } else if (info.Length() == 2 && info[0].IsObject() && info[1].IsNumber()) { - auto page = Page::Unwrap(info[0].As())->GetPage(); + auto page = Page::Unwrap(info[0].As())->page; auto fit = static_cast(info[1].As().Int32Value()); destination = make_unique(page, fit); @@ -72,7 +73,7 @@ Destination::Initialize(Napi::Env& env, Napi::Object& target) Napi::Value Destination::GetPage(const Napi::CallbackInfo& info) { -// destination->GetPage()->GetObject()->GetOwner()->GetParentDocument(); + // destination->GetPage()->GetObject()->GetOwner()->GetParentDocument(); return Value(); } Napi::Value diff --git a/src/doc/Document.h b/src/doc/Document.h index 4aadf9e..34502a5 100644 --- a/src/doc/Document.h +++ b/src/doc/Document.h @@ -40,7 +40,6 @@ class Document { public: static Napi::FunctionReference constructor; - ~Document() { cout << "Destructing Document" << endl; } explicit Document(const Napi::CallbackInfo& callbackInfo); // constructor static void Initialize(Napi::Env& env, Napi::Object& target); Napi::Value Load(const Napi::CallbackInfo&); @@ -60,6 +59,7 @@ class Document std::shared_ptr GetMemDocument() { auto shared = document; + cout << "PdfMemDocument count: " << document.use_count() << endl; return shared; } bool LoadedForIncrementalUpdates() { return loadForIncrementalUpdates; } diff --git a/src/doc/Encrypt.h b/src/doc/Encrypt.h index b4143a2..8f7138d 100644 --- a/src/doc/Encrypt.h +++ b/src/doc/Encrypt.h @@ -32,7 +32,6 @@ class Encrypt : public Napi::ObjectWrap { public: explicit Encrypt(const Napi::CallbackInfo&); - ~Encrypt() { cout << "Destructing Encrypt" << endl; } static Napi::FunctionReference constructor; static void Initialize(Napi::Env& env, Napi::Object& target); static Napi::Value CreateEncrypt(const Napi::CallbackInfo&); diff --git a/src/doc/Field.h b/src/doc/Field.h index a3414ae..370020b 100644 --- a/src/doc/Field.h +++ b/src/doc/Field.h @@ -55,7 +55,6 @@ class Field std::shared_ptr GetField() { auto shared = field; - cout << "Field use count: " << field.use_count() << endl; return shared; } diff --git a/src/doc/FileSpec.cc b/src/doc/FileSpec.cc index 06506e7..2a1fca6 100644 --- a/src/doc/FileSpec.cc +++ b/src/doc/FileSpec.cc @@ -51,7 +51,8 @@ FileSpec::FileSpec(const CallbackInfo& info) { if (info.Length() == 1 && info[0].IsObject() && info[0].As().InstanceOf(Obj::constructor.Value())) { - spec = make_unique(Obj::Unwrap(info[0].As())->obj); + spec = + make_unique(Obj::Unwrap(info[0].As())->GetObject()); } else if (info.Length() == 1 && info[0].Type() == napi_external) { auto pObj = info[0].As>().Data(); spec = make_unique(pObj); diff --git a/src/doc/Font.h b/src/doc/Font.h index cd76419..41e0f05 100644 --- a/src/doc/Font.h +++ b/src/doc/Font.h @@ -32,7 +32,6 @@ class Font : public Napi::ObjectWrap { public: explicit Font(const Napi::CallbackInfo& callbackInfo); - ~Font() { cout << "Destructing Font" << endl; } static Napi::FunctionReference constructor; static void Initialize(Napi::Env& env, Napi::Object& target); Napi::Value GetFontSize(const Napi::CallbackInfo&); diff --git a/src/doc/Form.cc b/src/doc/Form.cc index 01b3989..4d34930 100644 --- a/src/doc/Form.cc +++ b/src/doc/Form.cc @@ -100,9 +100,9 @@ Napi::Value Form::GetFormDictionary(const CallbackInfo& info) { auto obj = doc->GetAcroForm()->GetObject(); - Object nObj = - Obj::constructor.New({ External::New(info.Env(), obj) }); - return Dictionary::constructor.New({ nObj }); + auto ptr = + Dictionary::constructor.New({ External::New(info.Env(), obj) }); + return ptr; } Napi::Value @@ -183,10 +183,8 @@ Form::GetResource(const CallbackInfo& info) if (drObj->IsReference()) { drObj = doc->GetObjects()->GetObject(drObj->GetReference()); } - // auto dr = Dictionary::Resolve(doc->GetDocument(), drObj); - auto nObj = - Obj::constructor.New({ External::New(info.Env(), drObj) }); - return Dictionary::constructor.New({ nObj }); + return Dictionary::constructor.New( + { External::New(info.Env(), drObj) }); } return info.Env().Null(); } @@ -203,14 +201,14 @@ Form::SetResource(const CallbackInfo& info, const Napi::Value& value) try { auto formDict = doc->GetAcroForm()->GetObject()->GetDictionary(); auto dr = Obj::Unwrap(value.As()); - if (dr->obj->GetDataType() != ePdfDataType_Dictionary) + if (dr->GetObject()->GetDataType() != ePdfDataType_Dictionary) TypeError::New( info.Env(), "Default resource must be an instance of NoPoDoFo::Dictionary") .ThrowAsJavaScriptException(); if (formDict.HasKey(Name::DR)) formDict.RemoveKey(Name::DR); - formDict.AddKey(PdfName(Name::DR), dr->obj->Reference()); + formDict.AddKey(PdfName(Name::DR), dr->GetObject()->Reference()); } catch (PdfError& err) { ErrorHandler(err, info); } @@ -238,7 +236,9 @@ Form::GetCalculationOrder(const CallbackInfo& info) js.Set(n, nObj); n++; } else { - js.Set(n, Dictionary::constructor.New({ nObj })); + js.Set(n, + Dictionary::constructor.New( + { External::New(info.Env(), value) })); n++; } } diff --git a/src/doc/Form.h b/src/doc/Form.h index f77fcea..b636f0b 100644 --- a/src/doc/Form.h +++ b/src/doc/Form.h @@ -32,7 +32,6 @@ class Form : public Napi::ObjectWrap
{ public: explicit Form(const Napi::CallbackInfo&); - ~Form() { cout << "Destructing Form" << endl; } static Napi::FunctionReference constructor; static void Initialize(Napi::Env& env, Napi::Object& target); void SetNeedAppearances(const Napi::CallbackInfo&, const Napi::Value&); diff --git a/src/doc/Outline.cc b/src/doc/Outline.cc index bff551a..c77ecd5 100644 --- a/src/doc/Outline.cc +++ b/src/doc/Outline.cc @@ -41,7 +41,8 @@ Outline::Outline(const CallbackInfo& info) make_unique(info[0].As>().Data()); } else if (info[0].IsObject() && info[0].As().InstanceOf(Obj::constructor.Value())) { - outlines = make_unique(Obj::Unwrap(info[0].As())->obj); + outlines = + make_unique(Obj::Unwrap(info[0].As())->GetObject()); } else { TypeError::New(info.Env(), "Outlines requires one of: Obj, External") diff --git a/src/doc/Page.cc b/src/doc/Page.cc index ae875c4..6a0aaf4 100644 --- a/src/doc/Page.cc +++ b/src/doc/Page.cc @@ -43,23 +43,16 @@ FunctionReference Page::constructor; // NOLINT Page::Page(const CallbackInfo& info) : ObjectWrap(info) + , page(info[0].As>().Data()) { - if (info[0].IsObject() && - info[0].As().InstanceOf(Document::constructor.Value()) && - info[1].IsNumber()) { - n = info[1].As(); - page = make_shared(*doc->GetPage(n)); - } else if (info[0].IsExternal() && info.Length() >= 2 && info[1].IsNumber()) { - doc = info[0].As>().Data()->GetBaseDocument(); - n = info[1].As(); - } else if (info[0].IsExternal() && info.Length() == 1) { - page = make_shared(*info[0].As>().Data()); - } else { - TypeError::New(info.Env(), - "Unknown parameter document. Requires " - "BaseDocument::document (PdfDocument)") - .ThrowAsJavaScriptException(); - } + // The page is managed by the document. Do not pass a page pointer to this + // class without first adding it to the document +} + +Page::~Page() +{ + HandleScope scope(Env()); + page = nullptr; } void @@ -95,19 +88,19 @@ Page::Initialize(Napi::Env& env, Napi::Object& target) Napi::Value Page::GetRotation(const CallbackInfo& info) { - return Number::New(info.Env(), GetPage()->GetRotation()); + return Number::New(info.Env(), page->GetRotation()); } Napi::Value Page::GetNumFields(const CallbackInfo& info) { - return Number::New(info.Env(), GetPage()->GetNumFields()); + return Number::New(info.Env(), page->GetNumFields()); } Napi::Value Page::GetField(const CallbackInfo& info) { int index = info[0].As(); - if (GetPage()->GetNumFields() < index || index < 0) { + if (page->GetNumFields() < index || index < 0) { RangeError::New(info.Env(), "index out of range") .ThrowAsJavaScriptException(); return info.Env().Undefined(); @@ -117,35 +110,30 @@ Page::GetField(const CallbackInfo& info) Napi::Value Page::GetField(const Napi::Env& env, int index) { - auto field = new PdfField(page->GetField(index)); - auto destruct = [](Napi::Env env, PdfField* field) { - HandleScope scope(env); - delete field; - }; - EPdfField t = field->GetType(); + auto field = page->GetField(index); + EPdfField t = field.GetType(); switch (t) { case ePdfField_PushButton: return PushButton::constructor.New( - { External::New(env, field, destruct) }); + { External::New(env, &field) }); case ePdfField_CheckBox: return CheckBox::constructor.New( - { External::New(env, field, destruct) }); + { External::New(env, &field) }); case ePdfField_RadioButton: Error::New(env, "RadioButton not yet implemented") .ThrowAsJavaScriptException(); return env.Undefined(); case ePdfField_TextField: return TextField::constructor.New( - { External::New(env, field, destruct) }); + { External::New(env, &field) }); case ePdfField_ComboBox: return ComboBox::constructor.New( - { External::New(env, field, destruct) }); + { External::New(env, &field) }); case ePdfField_ListBox: - return ListBox::constructor.New( - { External::New(env, field, destruct) }); + return ListBox::constructor.New({ External::New(env, &field) }); case ePdfField_Signature: return SignatureField::constructor.New( - { External::New(env, field->GetWidgetAnnotation()) }); + { External::New(env, field.GetWidgetAnnotation()) }); default: Error::New(env, "Unknown field type").ThrowAsJavaScriptException(); return env.Undefined(); @@ -157,7 +145,7 @@ Page::GetFields(const CallbackInfo& info) { auto js = Array::New(info.Env()); uint32_t n = 0; - for (auto i = 0; i < GetPage()->GetNumFields(); ++i) { + for (auto i = 0; i < page->GetNumFields(); ++i) { js.Set(n, GetField(info.Env(), i)); ++n; } @@ -186,7 +174,7 @@ Page::SetRotation(const CallbackInfo& info, const Napi::Value& value) throw Napi::Error::New(info.Env(), "Rotate values must be a value of: 0, 90, 180, 270"); } - GetPage()->SetRotation(rotate); + page->SetRotation(rotate); } Napi::Value @@ -194,8 +182,8 @@ Page::GetFieldIndex(const CallbackInfo& info) { string key = info[0].As().Utf8Value(); int index = -1; - for (int i = 0; i < GetPage()->GetNumFields(); ++i) { - PdfField field = GetPage()->GetField(i); + for (int i = 0; i < page->GetNumFields(); ++i) { + PdfField field = page->GetField(i); string name = field.GetFieldName().GetStringUtf8(); string alternate = field.GetAlternateName().GetStringUtf8(); string mapping = field.GetMappingName().GetStringUtf8(); @@ -215,7 +203,7 @@ Page::SetPageWidth(const CallbackInfo& info, const Napi::Value& value) } try { int width = value.As(); - GetPage()->SetPageWidth(width); + page->SetPageWidth(width); } catch (PdfError& err) { ErrorHandler(err, info); } @@ -229,7 +217,7 @@ Page::SetPageHeight(const CallbackInfo& info, const Napi::Value& value) } try { int height = value.As(); - GetPage()->SetPageWidth(height); + page->SetPageWidth(height); } catch (PdfError& err) { ErrorHandler(err, info); } @@ -238,19 +226,19 @@ Page::SetPageHeight(const CallbackInfo& info, const Napi::Value& value) Napi::Value Page::GetPageWidth(const CallbackInfo& info) { - return Napi::Number::New(info.Env(), GetPage()->GetPageSize().GetWidth()); + return Napi::Number::New(info.Env(), page->GetPageSize().GetWidth()); } Napi::Value Page::GetPageHeight(const CallbackInfo& info) { - return Napi::Number::New(info.Env(), GetPage()->GetPageSize().GetHeight()); + return Napi::Number::New(info.Env(), page->GetPageSize().GetHeight()); } Napi::Value Page::GetTrimBox(const CallbackInfo& info) { - PdfRect trimBox = GetPage()->GetTrimBox(); + PdfRect trimBox = page->GetTrimBox(); return ExtractAndApplyRectValues(info, trimBox); } @@ -263,7 +251,7 @@ Page::SetTrimBox(const CallbackInfo& info, const Napi::Value& value) try { Rect* rect = Rect::Unwrap(value.As()); auto pdfRect = rect->GetRect(); - GetPage()->SetTrimBox(*pdfRect); + page->SetTrimBox(*pdfRect); } catch (PdfError& err) { ErrorHandler(err, info); @@ -273,7 +261,7 @@ Page::SetTrimBox(const CallbackInfo& info, const Napi::Value& value) Napi::Value Page::GetPageNumber(const CallbackInfo& info) { - return Napi::Number::New(info.Env(), GetPage()->GetPageNumber()); + return Napi::Number::New(info.Env(), page->GetPageNumber()); } Napi::Value @@ -281,8 +269,9 @@ Page::GetContents(const CallbackInfo& info) { EscapableHandleScope scope(info.Env()); // bool forAppending = info[0].As(); - PdfObject* contentsObj = GetPage()->GetContents(); - // forAppending ? GetPage()->GetContentsForAppending() : GetPage()->GetContents(); + PdfObject* contentsObj = page->GetContents(); + // forAppending ? page->GetContentsForAppending() : + // page->GetContents(); auto objPtr = External::New(info.Env(), contentsObj); auto instance = Obj::constructor.New({ objPtr }); return instance; @@ -292,7 +281,7 @@ Napi::Value Page::GetResources(const CallbackInfo& info) { EscapableHandleScope scope(info.Env()); - PdfObject* resources = GetPage()->GetResources(); + PdfObject* resources = page->GetResources(); auto objPtr = External::New(info.Env(), resources); auto instance = Obj::constructor.New({ objPtr }); return instance; @@ -301,28 +290,28 @@ Page::GetResources(const CallbackInfo& info) Napi::Value Page::GetMediaBox(const CallbackInfo& info) { - PdfRect mediaBox = GetPage()->GetMediaBox(); + PdfRect mediaBox = page->GetMediaBox(); return ExtractAndApplyRectValues(info, mediaBox); } Napi::Value Page::GetBleedBox(const CallbackInfo& info) { - PdfRect bleedBox = GetPage()->GetBleedBox(); + PdfRect bleedBox = page->GetBleedBox(); return ExtractAndApplyRectValues(info, bleedBox); } Napi::Value Page::GetArtBox(const CallbackInfo& info) { - PdfRect artBox = GetPage()->GetArtBox(); + PdfRect artBox = page->GetArtBox(); return ExtractAndApplyRectValues(info, artBox); } Napi::Value Page::GetNumAnnots(const CallbackInfo& info) { - return Napi::Number::New(info.Env(), GetPage()->GetNumAnnots()); + return Napi::Number::New(info.Env(), page->GetNumAnnots()); } void @@ -330,7 +319,7 @@ Page::DeleteAnnotation(const CallbackInfo& info) { int index = info[0].As(); try { - GetPage()->DeleteAnnotation(index); + page->DeleteAnnotation(index); } catch (PdfError& err) { ErrorHandler(err, info); } @@ -340,7 +329,7 @@ Page::GetAnnotation(const CallbackInfo& info) { EscapableHandleScope scope(info.Env()); int index = info[0].As(); - auto ptr = GetPage()->GetAnnotation(index); + auto ptr = page->GetAnnotation(index); auto instance = External::New(info.Env(), ptr); return Annotation::constructor.New({ instance }); } @@ -352,7 +341,7 @@ Page::CreateAnnotation(const CallbackInfo& info) auto type = static_cast(flag); auto obj = info[1].As(); Rect* rect = Rect::Unwrap(obj); - PdfAnnotation* annot = GetPage()->CreateAnnotation(type, *rect->GetRect()); + PdfAnnotation* annot = page->CreateAnnotation(type, *rect->GetRect()); auto instance = Annotation::constructor.New( { External::New(info.Env(), annot) }); return instance; diff --git a/src/doc/Page.h b/src/doc/Page.h index 51149c9..9234863 100644 --- a/src/doc/Page.h +++ b/src/doc/Page.h @@ -35,7 +35,7 @@ class Page : public Napi::ObjectWrap { public: explicit Page(const Napi::CallbackInfo& callbackInfo); - ~Page() { cout << "Destructing page" << endl; } + ~Page(); static Napi::FunctionReference constructor; static void Initialize(Napi::Env& env, Napi::Object& target); Napi::Value GetRotation(const Napi::CallbackInfo&); @@ -61,15 +61,7 @@ class Page : public Napi::ObjectWrap Napi::Value GetAnnotation(const Napi::CallbackInfo&); Napi::Value GetNumAnnots(const Napi::CallbackInfo&); void DeleteAnnotation(const Napi::CallbackInfo&); - - PoDoFo::PdfPage* GetPage() - { - return page.use_count() > 0 ? page.get() : doc->GetPage(n); - } - - int n = -1; - std::shared_ptr doc; - std::shared_ptr page; + PoDoFo::PdfPage* page; Napi::Object ExtractAndApplyRectValues(const Napi::CallbackInfo&, PoDoFo::PdfRect&); }; diff --git a/src/doc/Painter.cc b/src/doc/Painter.cc index ad11143..465a8ae 100644 --- a/src/doc/Painter.cc +++ b/src/doc/Painter.cc @@ -129,7 +129,7 @@ Painter::SetPage(const Napi::CallbackInfo& info) throw Napi::Error::New(info.Env(), "Page must be an instance of Page."); } auto page = Page::Unwrap(info[0].As()); - painter->SetPage(page->GetPage()); + painter->SetPage(page->page); } void diff --git a/src/doc/Rect.cc b/src/doc/Rect.cc index 6c93b57..7ade3cb 100644 --- a/src/doc/Rect.cc +++ b/src/doc/Rect.cc @@ -2,7 +2,7 @@ * This file is part of the NoPoDoFo (R) project. * Copyright (c) 2017-2018 * Authors: Cory Mickelson, et al. - * + * * NoPoDoFo is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -46,7 +46,7 @@ Rect::Rect(const CallbackInfo& info) "Rect requires Page as constructor parameter"); } Page* page = Page::Unwrap(pageObj); - rect = make_shared(page->GetPage()->GetPageSize()); + rect = make_shared(page->page->GetPageSize()); } if (info.Length() == 4) { double left, bottom, width, height; @@ -76,8 +76,7 @@ Rect::Initialize(Napi::Env& env, Napi::Object& target) InstanceAccessor("bottom", &Rect::GetBottom, &Rect::SetBottom), InstanceAccessor("width", &Rect::GetWidth, &Rect::SetWidth), InstanceAccessor("height", &Rect::GetHeight, &Rect::SetHeight), - InstanceMethod("intersect", &Rect::Intersect) - }); + InstanceMethod("intersect", &Rect::Intersect) }); constructor = Napi::Persistent(ctor); constructor.SuppressDestruct(); diff --git a/src/doc/SimpleTable.cc b/src/doc/SimpleTable.cc index 9399e53..7313729 100644 --- a/src/doc/SimpleTable.cc +++ b/src/doc/SimpleTable.cc @@ -361,7 +361,7 @@ SimpleTable::GetWidth(const CallbackInfo& info) const double posX = info[0].As(); const double posY = info[1].As(); auto page = Page::Unwrap(info[2].As()); - const auto w = table->GetWidth(posX, posY, page->GetPage()); + const auto w = table->GetWidth(posX, posY, page->page); return Number::New(info.Env(), w); } @@ -377,7 +377,7 @@ SimpleTable::GetHeight(const CallbackInfo& info) const double posX = info[0].As(); const double posY = info[1].As(); auto page = Page::Unwrap(info[2].As()); - const auto w = table->GetHeight(posX, posY, page->GetPage()); + const auto w = table->GetHeight(posX, posY, page->page); return Number::New(info.Env(), w); }