Skip to content

Commit

Permalink
Fix Fields, make use of PoDoFo managed resources wherever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
corymickelson committed Jun 8, 2018
1 parent 35410fd commit 17a56ff
Show file tree
Hide file tree
Showing 28 changed files with 141 additions and 241 deletions.
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions spec/unit/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ export function runAll() {
pageGetAnnotsCount,
pageGetAnnot,
pageContents,
pageResources //,
// pageAddImg
pageResources,
pageAddImg
].map(i => runTest(i))
}

20 changes: 2 additions & 18 deletions spec/unit/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
})
}
})
Expand Down
68 changes: 25 additions & 43 deletions src/base/Array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,25 @@
using namespace Napi;
using namespace PoDoFo;

using std::make_unique;
using std::shared_ptr;
using std::string;
using std::unique_ptr;

namespace NoPoDoFo {

Napi::FunctionReference Array::constructor; // NOLINT

Array::Array(const CallbackInfo& info)
: ObjectWrap<Array>(info)
, obj(Obj::Unwrap(info[0].As<Object>())->GetObject())
{}

Array::~Array()
{
if (info[0].IsObject() &&
info[0].As<Object>().InstanceOf(Obj::constructor.Value())) {
PdfObject* obj = Obj::Unwrap(info[0].As<Object>())->obj;
// array = make_unique<PdfArray>(*obj);
array = &obj->GetArray();
} else if (info[0].IsExternal()) {
array = info[0].As<External<PdfArray>>().Data();
// array =
// make_unique<PdfArray>(*info[0].As<External<PdfArray>>().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)
{
Expand All @@ -71,67 +61,57 @@ 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<Object>();
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<Boolean>());
GetArray().SetImmutable(value.As<Boolean>());
} catch (PdfError& err) {
ErrorHandler(err, info);
}
}
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<String>().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<String>().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<String>().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
Expand All @@ -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<Boolean>());
GetArray().SetDirty(value.As<Boolean>());
}

void
Expand All @@ -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);
Expand All @@ -177,14 +157,16 @@ Value
Array::GetObjAtIndex(const CallbackInfo& info)
{
size_t index = info[0].As<Number>().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<PdfObject>::New(info.Env(), &item);
auto child = new PdfObject(item);
children.push_back(child);
auto initPtr = Napi::External<PdfObject>::New(info.Env(), child);
auto instance = Obj::constructor.New({ initPtr });
return instance;
}
Expand All @@ -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());
Expand All @@ -219,7 +201,7 @@ void
Array::Clear(const CallbackInfo& info)
{
try {
GetArray()->Clear();
GetArray().Clear();
} catch (PdfError& err) {
ErrorHandler(err, info);
}
Expand Down
10 changes: 6 additions & 4 deletions src/base/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
#include <napi.h>
#include <podofo/podofo.h>

using std::vector;

namespace NoPoDoFo {
class Array : public Napi::ObjectWrap<Array>
{
public:
explicit Array(const Napi::CallbackInfo&);
~Array() { array = nullptr; }
~Array();
static Napi::FunctionReference constructor;
static void Initialize(Napi::Env& env, Napi::Object& target);

Expand All @@ -46,11 +48,11 @@ class Array : public Napi::ObjectWrap<Array>
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<PoDoFo::PdfArray> array;
PoDoFo::PdfArray* array;
PoDoFo::PdfObject* obj;
vector<PoDoFo::PdfObject*> children;
Napi::Value GetObjAtIndex(const Napi::CallbackInfo&);
};
}
Expand Down
49 changes: 8 additions & 41 deletions src/base/Dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -45,20 +43,13 @@ FunctionReference Dictionary::constructor; // NOLINT
*/
Dictionary::Dictionary(const CallbackInfo& info)
: ObjectWrap(info)
, obj(info[0].As<External<PdfObject>>().Data())
{}

Dictionary::~Dictionary()
{
if (info[0].IsExternal()) {
auto iObj = info[0].As<External<PdfObject>>();
// nObj = Obj::constructor.New( { iObj });
// obj = Obj::Unwrap(nObj.As<Object>())->GetObject();
obj = iObj.Data(); // make_shared<PdfObject>(*iObj.Data());
}
if (info[0].As<Object>().InstanceOf(Obj::constructor.Value())) {
obj = Obj::Unwrap(info[0].As<Object>())->obj;
} else {
TypeError::New(info.Env(), "requires Obj as parameter to constructor")
.ThrowAsJavaScriptException();
cout << "Obj required" << endl;
}
HandleScope scope(Env());
obj = nullptr;
}

void
Expand All @@ -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),
Expand All @@ -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<Object>());
// auto value = Obj::Unwrap(info[1].As<Object>())->GetObject();
// if (value->IsReference()) {
// value = doc->GetMemDocument()->GetObjects().GetObject(value->Reference());
// }
// return Dictionary::constructor.New(
// { External<PdfObject>::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)
{
Expand Down Expand Up @@ -142,7 +109,7 @@ Dictionary::AddKey(const CallbackInfo& info)
} else if (v.IsObject() &&
v.As<Object>().InstanceOf(Obj::constructor.Value())) {
auto objWrap = info[1].As<Object>();
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#"
Expand Down
5 changes: 1 addition & 4 deletions src/base/Dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Dictionary : public Napi::ObjectWrap<Dictionary>
{
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&);
Expand All @@ -48,13 +48,10 @@ class Dictionary : public Napi::ObjectWrap<Dictionary>
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<PoDoFo::PdfObject> obj;
PoDoFo::PdfObject* obj;
// Napi::Value nObj;
};
}
#endif
Loading

0 comments on commit 17a56ff

Please sign in to comment.