Skip to content

Commit

Permalink
Fix fields
Browse files Browse the repository at this point in the history
  • Loading branch information
corymickelson committed Jun 7, 2018
1 parent 61dc7c0 commit 35410fd
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 136 deletions.
39 changes: 15 additions & 24 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"scripts": {
"docs": "typedoc --options ./tsconfig.docs.json --out ./docs",
"coverage": "nyc node --expose-gc ./dist/_all.spec.js && nyc report --reporter=text-lcov | coveralls",
"prepublish": "npm ls",
"test": "tsc -p tsconfig.json && tsc -p tsconfig.spec.json && node --expose-gc ./node_modules/.bin/tape spec/unit/*.js",
"install": "node-pre-gyp install --fallback-to-build && tsc -p tsconfig.json"
"install": "node-pre-gyp install --fallback-to-build && tsc -p tsconfig.json",
"qt:debug": "cmake-js build -D && cp build/Debug/nopodofo.node lib/binding/1/"
},
"nyc": {
"exclude": [
Expand Down
62 changes: 33 additions & 29 deletions spec/unit/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { existsSync, unlinkSync, writeFile } from 'fs'
import { join } from 'path'
import * as test from 'tape'
import { IObj, IPage, npdf } from "../../dist";
if(!global.gc) {
global.gc = () => {}
if (!global.gc) {
global.gc = () => { }
}
const filePath = join(__dirname, '../test-documents/test.pdf'),
outFile = './test.out.pdf',
Expand Down Expand Up @@ -107,37 +107,41 @@ function pageAddImg() {
t.end()
})
}
doc.write('./img.out.pdf', e => {
if (e instanceof Error) t.fail()
doc.body.forEach(o => {
if (o.type === 'Dictionary') {
let objDict = o.getDictionary(),
objType = objDict.hasKey('Type') ? objDict.getKey('Type') : null,
objSubType = objDict.hasKey('SubType') ? objDict.getKey('SubType') : null

if ((objType && objType.type === 'Name') ||
(objSubType && objSubType.type === 'Name')) {

if ((objType && objType.getName() === 'XObject') || (objSubType && objSubType.getName() === 'Image')) {
let imgObj = objDict.hasKey('Filter') ? objDict.getKey('Filter') : null
if (imgObj && imgObj.type === 'Array') {
const imgObjArr = imgObj.getArray()
if (imgObjArr.length === 1) {
if (imgObjArr.at(0).type === 'Name') {
if (imgObjArr.at(0).getName() === 'DCTDecode') {
extractImg(o, true)
return
doc.write((e, d) => {
let sub = new npdf.Document()
sub.load(d, { fromBuffer: true }, e => {

if (e instanceof Error) t.fail()
sub.body.forEach(o => {
if (o.type === 'Dictionary') {
let objDict = o.getDictionary(),
objType = objDict.hasKey('Type') ? objDict.getKey('Type') : null,
objSubType = objDict.hasKey('SubType') ? objDict.getKey('SubType') : null

if ((objType && objType.type === 'Name') ||
(objSubType && objSubType.type === 'Name')) {

if ((objType && objType.getName() === 'XObject') || (objSubType && objSubType.getName() === 'Image')) {
let imgObj = objDict.hasKey('Filter') ? objDict.getKey('Filter') : null
if (imgObj && imgObj.type === 'Array') {
const imgObjArr = imgObj.getArray()
if (imgObjArr.length === 1) {
if (imgObjArr.at(0).type === 'Name') {
if (imgObjArr.at(0).getName() === 'DCTDecode') {
extractImg(o, true)
return
}
}
}
}
}
else if (imgObj && imgObj.type === 'Name' && imgObj.getName() === 'DCTDecode') {
extractImg(o, true)
return
else if (imgObj && imgObj.type === 'Name' && imgObj.getName() === 'DCTDecode') {
extractImg(o, true)
return
}
}
}
}
}
})
})
})

Expand All @@ -159,8 +163,8 @@ export function runAll() {
pageGetAnnotsCount,
pageGetAnnot,
pageContents,
pageResources,
pageAddImg
pageResources //,
// pageAddImg
].map(i => runTest(i))
}

13 changes: 11 additions & 2 deletions src/doc/Button.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@ Button::Button(shared_ptr<PdfField> field)
this->field = field;
button = make_shared<PdfButton>(*this->field.get());
}
Napi::Value Button::GetCaption(const Napi::CallbackInfo &info) {
Button::~Button()
{
cout << "Destructing Button" << endl;
cout << "Button field use count: " << field.use_count() << endl;
}
Napi::Value
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) {
void
Button::SetCaption(const Napi::CallbackInfo& info, const Napi::Value& value)
{
button->SetCaption(PdfString(value.As<String>().Utf8Value()));
}
}
9 changes: 5 additions & 4 deletions src/doc/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@
#ifndef NPDF_BUTTON_H
#define NPDF_BUTTON_H

#include "Field.h"
#include <napi.h>
#include <podofo/podofo.h>
#include "Field.h"

namespace NoPoDoFo {

class Button {
class Button
{
public:
explicit Button(std::shared_ptr<PoDoFo::PdfField> field);
~Button();
Napi::Value GetCaption(const Napi::CallbackInfo&);
void SetCaption(const Napi::CallbackInfo&, const Napi::Value&);

std::shared_ptr<PoDoFo::PdfButton> button;
std::shared_ptr<PoDoFo::PdfField> field;
};

}
#endif //NPDF_BUTTON_H
#endif // NPDF_BUTTON_H
5 changes: 2 additions & 3 deletions src/doc/CheckBox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ CheckBox::CheckBox(const CallbackInfo& info)
, Button(Field::GetField())
{
field = Field::GetField();
checkbox = make_unique<PdfCheckBox>(*field.get());
}

void
Expand Down Expand Up @@ -73,7 +72,7 @@ CheckBox::Initialize(Napi::Env& env, Napi::Object& target)
Napi::Value
CheckBox::IsChecked(const CallbackInfo& info)
{
return Napi::Boolean::New(info.Env(), checkbox->IsChecked());
return Napi::Boolean::New(info.Env(), GetCheckBox().IsChecked());
}

void
Expand All @@ -84,6 +83,6 @@ CheckBox::SetChecked(const CallbackInfo& info, const Napi::Value& value)
"CheckBox.checked requires boolean value");
}
bool checked = value.As<Boolean>();
checkbox->SetChecked(checked);
GetCheckBox().SetChecked(checked);
}
}
7 changes: 5 additions & 2 deletions src/doc/CheckBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#ifndef NPDF_CHECKBOX_H
#define NPDF_CHECKBOX_H

#include "Field.h"
#include "Button.h"
#include "Field.h"

#include <napi.h>
#include <podofo/podofo.h>
Expand All @@ -42,7 +42,10 @@ class CheckBox
Napi::Value IsChecked(const CallbackInfo&);
void SetChecked(const CallbackInfo&, const Napi::Value&);

std::unique_ptr<PoDoFo::PdfCheckBox> checkbox;
PoDoFo::PdfCheckBox GetCheckBox()
{
return PoDoFo::PdfCheckBox(*field.get());
}
std::shared_ptr<PoDoFo::PdfField> field;
};
}
Expand Down
7 changes: 3 additions & 4 deletions src/doc/ComboBox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include "ComboBox.h"
#include "Field.h"
#include <iostream>

using namespace Napi;
using namespace PoDoFo;
Expand All @@ -38,7 +38,6 @@ ComboBox::ComboBox(const Napi::CallbackInfo& info)
, ListField(Field::GetField())
{
field = Field::GetField();
combo = make_unique<PdfComboBox>(*field.get());
}

void
Expand Down Expand Up @@ -92,11 +91,11 @@ ComboBox::Initialize(Napi::Env& env, Napi::Object& target)
void
ComboBox::SetEditable(const Napi::CallbackInfo&, const Napi::Value& value)
{
combo->SetEditable(value.As<Napi::Boolean>());
GetComboBox().SetEditable(value.As<Napi::Boolean>());
}
Napi::Value
ComboBox::GetEditable(const Napi::CallbackInfo& info)
{
return Napi::Boolean::New(info.Env(), combo->IsEditable());
return Napi::Boolean::New(info.Env(), GetComboBox().IsEditable());
}
}
5 changes: 4 additions & 1 deletion src/doc/ComboBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class ComboBox
void SetEditable(const Napi::CallbackInfo&, const Napi::Value&);
Napi::Value GetEditable(const Napi::CallbackInfo&);

std::unique_ptr<PoDoFo::PdfComboBox> combo;
PoDoFo::PdfComboBox GetComboBox()
{
return PoDoFo::PdfComboBox(*field.get());
}
std::shared_ptr<PoDoFo::PdfField> field;
};
}
Expand Down
Loading

0 comments on commit 35410fd

Please sign in to comment.