UNPKG

8.5 kBtext/x-cView Raw
1/*
2 Source File : InputFileDriver.h
3
4
5 Copyright 2013 Gal Kahana HummusJS
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18
19 */
20#include "EStatusCode.h"
21#include "PDFPageModifierDriver.h"
22#include "PDFModifiedPage.h"
23#include "PDFWriter.h"
24#include "PDFWriterDriver.h"
25#include "XObjectContentContextDriver.h"
26#include "PDFFormXObject.h"
27#include "ConstructorsHolder.h"
28
29
30using namespace v8;
31using namespace PDFHummus;
32
33
34
35PDFPageModifierDriver::PDFPageModifierDriver(PDFWriter* inWriter,unsigned long inPageIndex,bool inEnsureContentEncapsulation)
36{
37 mModifierPageInstance = new PDFModifiedPage(inWriter,inPageIndex,inEnsureContentEncapsulation);
38}
39
40PDFPageModifierDriver::~PDFPageModifierDriver()
41{
42 delete mModifierPageInstance;
43}
44
45DEF_SUBORDINATE_INIT(PDFPageModifierDriver::Init)
46{
47 CREATE_ISOLATE_CONTEXT;
48
49 Local<FunctionTemplate> t = NEW_FUNCTION_TEMPLATE_EXTERNAL(New);
50
51 t->SetClassName(NEW_STRING("PDFPageModifier"));
52 t->InstanceTemplate()->SetInternalFieldCount(1);
53
54 SET_PROTOTYPE_METHOD(t, "startContext", StartContext);
55 SET_PROTOTYPE_METHOD(t, "getContext", GetContext);
56 SET_PROTOTYPE_METHOD(t, "endContext", EndContext);
57 SET_PROTOTYPE_METHOD(t, "attachURLLinktoCurrentPage", AttachURLLinktoCurrentPage);
58 SET_PROTOTYPE_METHOD(t, "writePage", WritePage);
59
60 SET_CONSTRUCTOR_EXPORT("PDFPageModifier", t);
61
62 // save in factory
63 EXPOSE_EXTERNAL_FOR_INIT(ConstructorsHolder, holder)
64 SET_CONSTRUCTOR(holder->PDFPageModifier_constructor, t);
65 SET_CONSTRUCTOR_TEMPLATE(holder->PDFPageModifier_constructor_template, t);
66}
67
68METHOD_RETURN_TYPE PDFPageModifierDriver::New(const ARGS_TYPE& args)
69{
70 CREATE_ISOLATE_CONTEXT;
71 CREATE_ESCAPABLE_SCOPE;
72 EXPOSE_EXTERNAL_ARGS(ConstructorsHolder, externalHolder)
73
74 PDFWriter* writer = NULL;
75 unsigned long pageIndex = 0;
76 bool ensureContentEncapsulation = false;
77
78 if(
79 args.Length() < 1 ||
80 !args[0]->IsObject() ||
81 (args.Length() >= 2 && !args[1]->IsNumber()) ||
82 (args.Length() >= 3 && !args[2]->IsBoolean())
83 ) {
84 THROW_EXCEPTION("Wrong arguments, perovide a: PDFWriter of the pdf that page you want modified, page index of that page, and boolean indicating if you want to ensure encapsulation. last one defaults to false");
85 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
86 }
87
88 PDFWriterDriver* writerDriver = ObjectWrap::Unwrap<PDFWriterDriver>(args[0]->TO_OBJECT());
89 if(!writerDriver)
90 {
91 THROW_EXCEPTION("Wrong arguments, provide a PDFWriter as the first object");
92 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
93 }
94 else
95 writer = writerDriver->GetWriter();
96
97 if(args.Length() >= 2)
98 {
99 pageIndex = (unsigned long)TO_NUMBER(args[1])->Value();
100 }
101
102 if(args.Length() >= 3)
103 {
104 ensureContentEncapsulation = args[2]->TO_BOOLEAN()->Value();
105 }
106
107
108 PDFPageModifierDriver* driver = new PDFPageModifierDriver(writer,pageIndex,ensureContentEncapsulation);
109
110 driver->holder = externalHolder;
111 driver->Wrap(args.This());
112 SET_FUNCTION_RETURN_VALUE(args.This())
113}
114
115METHOD_RETURN_TYPE PDFPageModifierDriver::StartContext(const ARGS_TYPE& args)
116{
117 CREATE_ISOLATE_CONTEXT;
118 CREATE_ESCAPABLE_SCOPE;
119
120 PDFPageModifierDriver* driver = ObjectWrap::Unwrap<PDFPageModifierDriver>(args.This());
121
122 do {
123 if(!driver)
124 {
125 THROW_EXCEPTION("no driver created...please create one through Hummus");
126 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
127 break;
128 }
129
130 if(!driver->mModifierPageInstance->StartContentContext()) {
131 THROW_EXCEPTION("context not created, page index is either wrong, or page is null");
132 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
133 break;
134 }
135 SET_FUNCTION_RETURN_VALUE(args.This())
136 }while(false);
137
138
139}
140
141METHOD_RETURN_TYPE PDFPageModifierDriver::GetContext(const ARGS_TYPE& args)
142{
143 CREATE_ISOLATE_CONTEXT;
144 CREATE_ESCAPABLE_SCOPE;
145
146 PDFPageModifierDriver* driver = ObjectWrap::Unwrap<PDFPageModifierDriver>(args.This());
147
148
149 do {
150 if(!driver)
151 {
152 THROW_EXCEPTION("no driver created...please create one through Hummus");
153 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
154 break;
155 }
156
157 if(!driver->mModifierPageInstance->GetCurrentFormContext()) {
158 THROW_EXCEPTION("No context created, please create one with startContext");
159 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
160 break;
161
162 }
163
164 Local<Value> newInstance = driver->holder->GetNewXObjectContentContext(args);
165 XObjectContentContextDriver* contentContextDriver = ObjectWrap::Unwrap<XObjectContentContextDriver>(newInstance->TO_OBJECT());
166 contentContextDriver->ContentContext =
167 driver->mModifierPageInstance->GetCurrentFormContext() ?
168 driver->mModifierPageInstance->GetCurrentFormContext()->GetContentContext():
169 NULL;
170 contentContextDriver->FormOfContext = driver->mModifierPageInstance->GetCurrentFormContext();
171 contentContextDriver->SetResourcesDictionary(driver->mModifierPageInstance->GetCurrentResourcesDictionary());
172
173 SET_FUNCTION_RETURN_VALUE(newInstance)
174 }while(false);
175}
176
177METHOD_RETURN_TYPE PDFPageModifierDriver::EndContext(const ARGS_TYPE& args)
178{
179 CREATE_ISOLATE_CONTEXT;
180 CREATE_ESCAPABLE_SCOPE;
181
182 PDFPageModifierDriver* driver = ObjectWrap::Unwrap<PDFPageModifierDriver>(args.This());
183
184
185 if(!driver)
186 {
187 THROW_EXCEPTION("no driver created...please create one through Hummus");
188 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
189
190 }
191
192 driver->mModifierPageInstance->EndContentContext();
193 SET_FUNCTION_RETURN_VALUE(args.This())
194}
195
196METHOD_RETURN_TYPE PDFPageModifierDriver::AttachURLLinktoCurrentPage(const ARGS_TYPE& args)
197{
198 CREATE_ISOLATE_CONTEXT;
199 CREATE_ESCAPABLE_SCOPE;
200
201 if(args.Length() != 5 ||
202 !args[0]->IsString() ||
203 !args[1]->IsNumber() ||
204 !args[2]->IsNumber() ||
205 !args[3]->IsNumber() ||
206 !args[4]->IsNumber())
207 {
208 THROW_EXCEPTION("wrong arguments, pass a url, and 4 numbers (left,bottom,right,top) for the rectangle valid for clicking");
209 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
210 }
211
212 PDFPageModifierDriver* driver = ObjectWrap::Unwrap<PDFPageModifierDriver>(args.This());
213
214 EStatusCode status = driver->mModifierPageInstance->AttachURLLinktoCurrentPage(*UTF_8_VALUE(args[0]->TO_STRING()),
215 PDFRectangle(TO_NUMBER(args[1])->Value(),
216 TO_NUMBER(args[2])->Value(),
217 TO_NUMBER(args[3])->Value(),
218 TO_NUMBER(args[4])->Value()));
219 if(status != eSuccess)
220 {
221 THROW_EXCEPTION("unable to attach link to current page. will happen if the input URL may not be encoded to ascii7");
222 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
223 }
224
225 SET_FUNCTION_RETURN_VALUE(args.This())
226}
227
228METHOD_RETURN_TYPE PDFPageModifierDriver::WritePage(const ARGS_TYPE& args)
229{
230 CREATE_ISOLATE_CONTEXT;
231 CREATE_ESCAPABLE_SCOPE;
232
233 PDFPageModifierDriver* driver = ObjectWrap::Unwrap<PDFPageModifierDriver>(args.This());
234
235
236 if(!driver)
237 {
238 THROW_EXCEPTION("no driver created...please create one through Hummus");
239 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
240
241 }
242
243 if(driver->mModifierPageInstance->WritePage() != PDFHummus::eSuccess)
244 {
245 THROW_EXCEPTION("Unable to write page");
246 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
247 }
248
249 SET_FUNCTION_RETURN_VALUE(args.This())
250}
\No newline at end of file