METHOD_RETURN_TYPE DictionaryContextDriver::WriteLiteralStringValue(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    if(!(args.Length() == 1) ||
       (!args[0]->IsString() && !args[0]->IsArray()))
    {
		THROW_EXCEPTION("wrong arguments, pass 1 argument that is a literal string (string) or an array");
		SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        
    }

    DictionaryContextDriver* driver = ObjectWrap::Unwrap<DictionaryContextDriver>(args.This());
    
    if(!driver->DictionaryContextInstance)
    {
		THROW_EXCEPTION("dictinoarycontext object not initialized, create using objectscontext.startDictionary");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    }

	if(args[0]->IsArray())
	{
		std::string string;
		unsigned long arrayLength = (args[0]->ToObject()->Get(NEW_STRING("length")))->ToObject()->Uint32Value();
		for(unsigned long i=0;i<arrayLength;++i)
			string.push_back((unsigned char)args[0]->ToObject()->Get(i)->ToNumber()->Value());
		driver->DictionaryContextInstance->WriteLiteralStringValue(string);
	}
	else
    {
		driver->DictionaryContextInstance->WriteLiteralStringValue(*String::Utf8Value(args[0]->ToString()));
	}
    SET_FUNCTION_RETURN_VALUE(args.This());
}
METHOD_RETURN_TYPE DictionaryContextDriver::WriteNameValue(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    if(!(args.Length() == 1) ||
       !args[0]->IsString())
    {
		THROW_EXCEPTION("Wrong arguments, provide a string to write");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        
    }
    
    DictionaryContextDriver* driver = ObjectWrap::Unwrap<DictionaryContextDriver>(args.This());
    
    if(!driver->DictionaryContextInstance)
    {
		THROW_EXCEPTION("dictinoarycontext object not initialized, create using objectscontext.startDictionary");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    }
    
    driver->DictionaryContextInstance->WriteNameValue(*String::Utf8Value(args[0]->ToString()));
    
    SET_FUNCTION_RETURN_VALUE(args.This());
}
METHOD_RETURN_TYPE ByteWriterWithPositionDriver::New(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    ByteWriterWithPositionDriver* driver = new ByteWriterWithPositionDriver();
    driver->Wrap(args.This());
	SET_FUNCTION_RETURN_VALUE(args.This());
}
METHOD_RETURN_TYPE PDFNullDriver::New(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    PDFNullDriver* driver = new PDFNullDriver();
    driver->Wrap(args.This());
	SET_FUNCTION_RETURN_VALUE(args.This());
}
METHOD_RETURN_TYPE DocumentContextDriver::New(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    DocumentContextDriver* objectsContext = new DocumentContextDriver();
    objectsContext->Wrap(args.This());
    
    SET_FUNCTION_RETURN_VALUE(args.This());
}
Exemple #6
0
METHOD_RETURN_TYPE PDFRealDriver::New(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    EXPOSE_EXTERNAL_ARGS(ConstructorsHolder, externalHolder)
    PDFRealDriver* driver = new PDFRealDriver();
	driver->holder = externalHolder;
    driver->Wrap(args.This());
	SET_FUNCTION_RETURN_VALUE( args.This())
}
METHOD_RETURN_TYPE ResourcesDictionaryDriver::New(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
    CREATE_ESCAPABLE_SCOPE;

    ResourcesDictionaryDriver* form = new ResourcesDictionaryDriver();
    form->Wrap(args.This());

    SET_FUNCTION_RETURN_VALUE(args.This());
}
METHOD_RETURN_TYPE DictionaryContextDriver::New(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    DictionaryContextDriver* driver = new DictionaryContextDriver();
    driver->Wrap(args.This());
    
	SET_FUNCTION_RETURN_VALUE(args.This());
}
METHOD_RETURN_TYPE DictionaryContextDriver::WriteRectangleValue(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    // can accept array or four numbers
    if( (args.Length() != 1 && args.Length() != 4) ||
       (args.Length() == 1 && !args[0]->IsArray()) ||
       (args.Length() == 4 && (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsNumber() || !args[3]->IsNumber())))
    {
		THROW_EXCEPTION("Wrong arguments, provide an array of 4 numbers, or 4 numbers");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        
    }
    
    DictionaryContextDriver* driver = ObjectWrap::Unwrap<DictionaryContextDriver>(args.This());
    
    if(!driver->DictionaryContextInstance)
    {
		THROW_EXCEPTION("dictinoarycontext object not initialized, create using objectscontext.startDictionary");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    }
    
    if(args.Length() == 1)
    {
        // array version. verify that there are 4 numbers
        if(args[0]->ToObject()->Get(NEW_STRING("length"))->ToObject()->Uint32Value() != 4)
        {
            THROW_EXCEPTION("Wrong arguments, provide an array of 4 numbers, or 4 numbers");
            SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        }
        
        driver->DictionaryContextInstance->WriteRectangleValue(PDFRectangle(
                                                      args[0]->ToObject()->Get(0)->ToNumber()->Value(),
                                                      args[0]->ToObject()->Get(1)->ToNumber()->Value(),
                                                      args[0]->ToObject()->Get(2)->ToNumber()->Value(),
                                                      args[0]->ToObject()->Get(3)->ToNumber()->Value()));
        
    }
    else
    {
        // 4 numbers version
        driver->DictionaryContextInstance->WriteRectangleValue(PDFRectangle(
                                                                            args[0]->ToNumber()->Value(),
                                                                            args[1]->ToNumber()->Value(),
                                                                            args[2]->ToNumber()->Value(),
                                                                            args[3]->ToNumber()->Value()));
    }
    
    
    SET_FUNCTION_RETURN_VALUE(args.This());
}
METHOD_RETURN_TYPE InputFileDriver::New(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    InputFileDriver* inputFile = new InputFileDriver();
    
    if(args.Length() == 1 && args[0]->IsString())
        inputFile->OpenFile(*String::Utf8Value(args[0]->ToString()));
    
    inputFile->Wrap(args.This());
	SET_FUNCTION_RETURN_VALUE(args.This());
}
METHOD_RETURN_TYPE InputFileDriver::New(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    EXPOSE_EXTERNAL_ARGS(ConstructorsHolder, externalHolder)
    
    InputFileDriver* inputFile = new InputFileDriver();

    inputFile->holder = externalHolder;
    
    if(args.Length() == 1 && args[0]->IsString())
        inputFile->OpenFile(*UTF_8_VALUE(args[0]->TO_STRING()));
    
    inputFile->Wrap(args.This());
	SET_FUNCTION_RETURN_VALUE(args.This())
}
METHOD_RETURN_TYPE InputFileDriver::GetInputStream(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    InputFileDriver* driver = ObjectWrap::Unwrap<InputFileDriver>(args.This());
    
    if(!driver)
    {
		THROW_EXCEPTION("no driver created...please create one through Hummus");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        
    }
    
    if(driver->mInputFileInstance && driver->mInputFileInstance->GetInputStream())
    {
        Handle<Value> result = ByteReaderWithPositionDriver::GetNewInstance(args);
        
        ObjectWrap::Unwrap<ByteReaderWithPositionDriver>(result->ToObject())->SetStream(driver->mInputFileInstance->GetInputStream(), false);
        
        SET_FUNCTION_RETURN_VALUE(result);
    }
    else
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    
}
METHOD_RETURN_TYPE PDFDictionaryDriver::QueryObject(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    if(args.Length() != 1 || !args[0]->IsString())
    {
		THROW_EXCEPTION("wrong arguments, pass 1 argument which is a string key");
		SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        
    }
    
    std::string key = *String::Utf8Value(args[0]->ToString());

    PDFDictionaryDriver* driver = ObjectWrap::Unwrap<PDFDictionaryDriver>(args.This());
    
    if(!driver->TheObject->Exists(key))
    {
		THROW_EXCEPTION("key not found");
		SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    }
    
    RefCountPtr<PDFObject> anObject = driver->TheObject->QueryDirectObject(key);
    Handle<Value> result = PDFObjectDriver::CreateDriver(anObject.GetPtr());
    
    SET_FUNCTION_RETURN_VALUE(result);
}
METHOD_RETURN_TYPE InputFileDriver::OpenFile(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
 
    if(args.Length() != 1 || !args[0]->IsString())
    {
		THROW_EXCEPTION("wrong arguments. please provide a string for the file path");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    }

    InputFileDriver* driver = ObjectWrap::Unwrap<InputFileDriver>(args.This());

    
    if(!driver)
    {
		THROW_EXCEPTION("no driver created...please create one through Hummus");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        
    }
    
    if(driver->OpenFile(*String::Utf8Value(args[0]->ToString())) != PDFHummus::eSuccess)
    {
		THROW_EXCEPTION("can't open file. make sure path exists");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    }
    
    SET_FUNCTION_RETURN_VALUE(UNDEFINED);
}
METHOD_RETURN_TYPE ByteReaderDriver::Read(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    // k. i'll just read the number of bytes and return an array of them
    if(args.Length() != 1 ||
       !args[0]->IsNumber())
    {
		THROW_EXCEPTION("Wrong arguments. pass the number of bytes to read");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    }
    
    ByteReaderDriver* element = ObjectWrap::Unwrap<ByteReaderDriver>(args.This());
    IOBasicTypes::LongBufferSizeType bufferSize = args[0]->ToNumber()->Uint32Value();
    Byte* buffer = new Byte[bufferSize];
    
    bufferSize = element->mInstance->Read(buffer,(int)bufferSize); // reading int cause that's the maximum that can read (use should read till notended anyways)

    Local<Array> outBuffer = NEW_ARRAY((int)bufferSize);
    
    for(LongBufferSizeType i=0;i<bufferSize;++i)
		outBuffer->Set(NEW_NUMBER(i), NEW_NUMBER(buffer[i]));
    
    delete[] buffer;
    
    SET_FUNCTION_RETURN_VALUE(outBuffer);
}
METHOD_RETURN_TYPE ByteReaderDriver::NotEnded(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    ByteReaderDriver* element = ObjectWrap::Unwrap<ByteReaderDriver>(args.This());
    
	SET_FUNCTION_RETURN_VALUE(NEW_BOOLEAN(element->mInstance->NotEnded()));
}
METHOD_RETURN_TYPE PDFArrayDriver::GetLength(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    PDFArrayDriver* arrayDriver = ObjectWrap::Unwrap<PDFArrayDriver>(args.This());
    
    Local<Number> result = NEW_NUMBER(arrayDriver->TheObject->GetLength());
    
    SET_FUNCTION_RETURN_VALUE(result);
}
METHOD_RETURN_TYPE PDFObjectDriver::ToPDFArray(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    PDFObject* anObject = ObjectWrap::Unwrap<PDFObjectDriver>(args.This())->GetObject();
    if(anObject->GetType() != PDFObject::ePDFObjectArray)
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    
	Handle<Value> newInstance = PDFArrayDriver::GetNewInstance();
    ObjectWrap::Unwrap<PDFArrayDriver>(newInstance->ToObject())->TheObject = anObject;
    SET_FUNCTION_RETURN_VALUE(newInstance);
}
METHOD_RETURN_TYPE PDFStreamDriver::GetWriteStream(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    PDFStreamDriver* stream = ObjectWrap::Unwrap<PDFStreamDriver>(args.This());


    Local<Value> result = stream->holder->GetNewByteWriter(args);
    
    ObjectWrap::Unwrap<ByteWriterDriver>(result->TO_OBJECT())->SetStream(stream->PDFStreamInstance->GetWriteStream(), false);
    
	SET_FUNCTION_RETURN_VALUE(result)
}
METHOD_RETURN_TYPE PDFDictionaryDriver::ToJSObject(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    PDFDictionaryDriver* driver = ObjectWrap::Unwrap<PDFDictionaryDriver>(args.This());
    
    Local<Object> result = NEW_OBJECT;
    
	MapIterator<PDFNameToPDFObjectMap> it = driver->TheObject->GetIterator();
    
    while(it.MoveNext())
        result->Set(NEW_STRING(it.GetKey()->GetValue().c_str()),PDFObjectDriver::CreateDriver(it.GetValue()));
    
    SET_FUNCTION_RETURN_VALUE(result);
}
METHOD_RETURN_TYPE PDFArrayDriver::ToJSArray(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    PDFArrayDriver* arrayDriver = ObjectWrap::Unwrap<PDFArrayDriver>(args.This());
    
    Local<Array> result = NEW_ARRAY((int)arrayDriver->TheObject->GetLength());
    
    for(unsigned long i=0; i < arrayDriver->TheObject->GetLength();++i)
    {
        RefCountPtr<PDFObject> anObject(arrayDriver->TheObject->QueryObject(i));
        result->Set(NEW_NUMBER(i),PDFObjectDriver::CreateDriver(anObject.GetPtr()));
    }

    SET_FUNCTION_RETURN_VALUE(result);
}
METHOD_RETURN_TYPE ByteReaderWithPositionDriver::SetPositionFromEnd(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    if(args.Length() != 1 ||
       !args[0]->IsNumber())
    {
		THROW_EXCEPTION("Wrong arguments. pass the position");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    }
    
    
    ByteReaderWithPositionDriver* element = ObjectWrap::Unwrap<ByteReaderWithPositionDriver>(args.This());
    element->mInstance->SetPositionFromEnd(args[0]->ToNumber()->Uint32Value());
    
    SET_FUNCTION_RETURN_VALUE(args.This());
}
METHOD_RETURN_TYPE PDFObjectDriver::ToNumber(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    PDFObject* anObject = ObjectWrap::Unwrap<PDFObjectDriver>(args.This())->GetObject();
    if(anObject->GetType() == PDFObject::ePDFObjectInteger)
    {
        SET_FUNCTION_RETURN_VALUE(NEW_NUMBER(((PDFInteger*)anObject)->GetValue()));
    }
    else if(anObject->GetType() == PDFObject::ePDFObjectReal)
    {
        SET_FUNCTION_RETURN_VALUE(NEW_NUMBER(((PDFReal*)anObject)->GetValue()));
        
    }
    else
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
}
METHOD_RETURN_TYPE DocumentContextDriver::GetInfoDictionary(const ARGS_TYPE& args)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

    DocumentContextDriver* driver = ObjectWrap::Unwrap<DocumentContextDriver>(args.This());
    if(!driver->DocumentContextInstance)
    {
		THROW_EXCEPTION("document context driver not initialized. use the pdfwriter to get the current document context");
		SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    }
    
    Handle<Value> infoDictionaryDriverObject = InfoDictionaryDriver::GetNewInstance();
    InfoDictionaryDriver* infoDictDriver = ObjectWrap::Unwrap<InfoDictionaryDriver>(infoDictionaryDriverObject->ToObject());
    infoDictDriver->InfoDictionaryInstance = &(driver->DocumentContextInstance->GetTrailerInformation().GetInfo());
    
	SET_FUNCTION_RETURN_VALUE(infoDictionaryDriverObject);
}
METHOD_RETURN_TYPE PDFDictionaryDriver::Exists(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    PDFDictionaryDriver* driver = ObjectWrap::Unwrap<PDFDictionaryDriver>(args.This());

    if(args.Length() != 1 || !args[0]->IsString())
    {
		THROW_EXCEPTION("wrong arguments, pass 1 argument which is a string key");
		SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        
    }
    
    Handle<Boolean> result = NEW_BOOLEAN(driver->TheObject->Exists(*String::Utf8Value(args[0]->ToString())));
    
    SET_FUNCTION_RETURN_VALUE(result);
}
METHOD_RETURN_TYPE InputFileDriver::CloseFile(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    InputFileDriver* driver = ObjectWrap::Unwrap<InputFileDriver>(args.This());
    
    if(!driver)
    {
		THROW_EXCEPTION("no driver created...please create one through Hummus");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        
    }
    
    if(driver->mInputFileInstance)
        driver->mInputFileInstance->CloseFile();
    
    SET_FUNCTION_RETURN_VALUE(UNDEFINED);
}
METHOD_RETURN_TYPE InputFileDriver::GetFilePath(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    InputFileDriver* driver = ObjectWrap::Unwrap<InputFileDriver>(args.This());
    
    if(!driver)
    {
		THROW_EXCEPTION("no driver created...please create one through Hummus");
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        
    }
    
    if(driver->mInputFileInstance && driver->mInputFileInstance->GetInputStream())
        SET_FUNCTION_RETURN_VALUE(NEW_STRING(driver->mInputFileInstance->GetFilePath().c_str()));
    else
        SET_FUNCTION_RETURN_VALUE(UNDEFINED);
}
METHOD_RETURN_TYPE PDFObjectDriver::ToString(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    PDFObject* anObject = ObjectWrap::Unwrap<PDFObjectDriver>(args.This())->GetObject();
    std::string result;
    
    switch(anObject->GetType())
    {
        case PDFObject::ePDFObjectName:
            result = ((PDFName*)anObject)->GetValue();
            break;
        case PDFObject::ePDFObjectLiteralString:
            result = ((PDFLiteralString*)anObject)->GetValue();
            break;
        case PDFObject::ePDFObjectHexString:
            result = ((PDFHexString*)anObject)->GetValue();
            break;
        case PDFObject::ePDFObjectReal:
            result = Double(((PDFReal*)anObject)->GetValue()).ToString();
            break;
        case PDFObject::ePDFObjectInteger:
            result = LongLong(((PDFInteger*)anObject)->GetValue()).ToString();
            break;
        case PDFObject::ePDFObjectSymbol:
            result = ((PDFSymbol*)anObject)->GetValue();
            break;
        case PDFObject::ePDFObjectBoolean:
            result = ((PDFBoolean*)anObject)->GetValue() ? "true":"false";
            break;
        default:
            result = PDFObject::scPDFObjectTypeLabel[anObject->GetType()];
    }
    SET_FUNCTION_RETURN_VALUE(NEW_STRING(result.c_str()));

}
METHOD_RETURN_TYPE PDFArrayDriver::QueryObject(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    
    if(args.Length() != 1 || !args[0]->IsNumber())
    {
		THROW_EXCEPTION("wrong arguments, pass 1 argument which is an index in the array");
		SET_FUNCTION_RETURN_VALUE(UNDEFINED);
        
    }
    
    PDFArrayDriver* arrayDriver = ObjectWrap::Unwrap<PDFArrayDriver>(args.This());
    if(args[0]->ToNumber()->Uint32Value() >= arrayDriver->TheObject->GetLength())
    {
		THROW_EXCEPTION("wrong arguments, pass 1 argument which is a valid index in the array");
		SET_FUNCTION_RETURN_VALUE(UNDEFINED);
    }
    
    RefCountPtr<PDFObject> anObject = arrayDriver->TheObject->QueryObject(args[0]->ToNumber()->Uint32Value());
    Handle<Value> result = PDFObjectDriver::CreateDriver(anObject.GetPtr());
    
    SET_FUNCTION_RETURN_VALUE(result);
}
METHOD_RETURN_TYPE PDFObjectDriver::GetType(const ARGS_TYPE& args)
{
    CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;
    Local<Number> result = NEW_NUMBER((unsigned long)ObjectWrap::Unwrap<PDFObjectDriver>(args.This())->GetObject()->GetType());
    SET_FUNCTION_RETURN_VALUE(result);
}