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); }
PDFObject* PDFObjectParser::ParseDictionary(IPDFParserExtender* inParserExtender) { PDFDictionary* aDictionary = new PDFDictionary(); bool dictionaryEndEncountered = false; std::string token; EStatusCode status = PDFHummus::eSuccess; while(GetNextToken(token) && PDFHummus::eSuccess == status) { dictionaryEndEncountered = (scDoubleRightAngle == token); if(dictionaryEndEncountered) break; ReturnTokenToBuffer(token); // Parse Key PDFObjectCastPtr<PDFName> aKey(ParseNewObject(inParserExtender)); if(!aKey) { status = PDFHummus::eFailure; TRACE_LOG1("PDFObjectParser::ParseDictionary, failure to parse key for a dictionary. token = %s",token.c_str()); break; } // i'll consider duplicate keys as failure if(aDictionary->Exists(aKey->GetValue())) { status = PDFHummus::eFailure; TRACE_LOG1("PDFObjectParser::ParseDictionary, failure to parse key for a dictionary, key already exists. key = %s",aKey->GetValue().c_str()); break; } // Parse Value RefCountPtr<PDFObject> aValue = ParseNewObject(inParserExtender); if(!aValue) { status = PDFHummus::eFailure; TRACE_LOG1("PDFObjectParser::ParseDictionary, failure to parse value for a dictionary. token = %s",token.c_str()); break; } // all well, add the two items to the dictionary aDictionary->Insert(aKey.GetPtr(),aValue.GetPtr()); } if(dictionaryEndEncountered && PDFHummus::eSuccess == status) { return aDictionary; } else { delete aDictionary; TRACE_LOG1("PDFObjectParser::ParseDictionary, failure to parse dictionary, didn't find end of array or failure to parse dictionary member object. token = %s",token.c_str()); return NULL; } }
void showPageContent(PDFParser& parser, RefCountPtr<PDFObject> contents, InputFile& pdfFile) { if(contents->GetType() == ePDFObjectArray) { PDFObjectCastPtr<PDFIndirectObjectReference> streamReferences; SingleValueContainerIterator<PDFObjectVector> itContents = ((PDFArray*)contents.GetPtr())->GetIterator(); // array of streams while(itContents.MoveNext()) { streamReferences = itContents.GetItem(); PDFObjectCastPtr<PDFStreamInput> stream = parser.ParseNewObject(streamReferences->mObjectID); showContentStream(stream.GetPtr(),pdfFile.GetInputStream(),parser); } } else { // stream showContentStream((PDFStreamInput*)contents.GetPtr(),pdfFile.GetInputStream(),parser); } }
PDFObject* PDFObjectParser::ParseDictionary() { PDFDictionary* aDictionary = new PDFDictionary(); bool dictionaryEndEncountered = false; std::string token; EStatusCode status = PDFHummus::eSuccess; while(GetNextToken(token) && PDFHummus::eSuccess == status) { dictionaryEndEncountered = (scDoubleRightAngle == token); if(dictionaryEndEncountered) break; ReturnTokenToBuffer(token); // Parse Key PDFObjectCastPtr<PDFName> aKey(ParseNewObject()); if(!aKey) { status = PDFHummus::eFailure; TRACE_LOG1("PDFObjectParser::ParseDictionary, failure to parse key for a dictionary. token = %s",token.substr(0, MAX_TRACE_SIZE - 200).c_str()); break; } // Parse Value RefCountPtr<PDFObject> aValue = ParseNewObject(); if(!aValue) { status = PDFHummus::eFailure; TRACE_LOG1("PDFObjectParser::ParseDictionary, failure to parse value for a dictionary. token = %s",token.substr(0, MAX_TRACE_SIZE - 200).c_str()); break; } // all good. i'm gonna be forgiving here and allow skipping duplicate keys. cause it happens if(!aDictionary->Exists(aKey->GetValue())) aDictionary->Insert(aKey.GetPtr(),aValue.GetPtr()); } if(dictionaryEndEncountered && PDFHummus::eSuccess == status) { return aDictionary; } else { delete aDictionary; TRACE_LOG1("PDFObjectParser::ParseDictionary, failure to parse dictionary, didn't find end of array or failure to parse dictionary member object. token = %s",token.substr(0, MAX_TRACE_SIZE - 200).c_str()); return NULL; } }
void checkXObjectRef(PDFParser& parser,RefCountPtr<PDFDictionary> page) { PDFObjectCastPtr<PDFDictionary> resources(parser.QueryDictionaryObject(page.GetPtr(),"Resources")); if(!resources) { wcout << "No XObject in this page\n"; return; } PDFObjectCastPtr<PDFDictionary> xobjects(parser.QueryDictionaryObject(resources.GetPtr(),"XObject")); if(!xobjects) { wcout << "No XObject in this page\n"; return; } cout << "Displaying XObjects information for this page:\n"; showXObjectsPerPageInfo(parser,xobjects); }
Handle<Value> PDFArrayDriver::QueryObject(const Arguments& args) { HandleScope scope; if(args.Length() != 1 || !args[0]->IsNumber()) { ThrowException(Exception::TypeError(String::New("wrong arguments, pass 1 argument which is an index in the array"))); return scope.Close(Undefined()); } PDFArrayDriver* arrayDriver = ObjectWrap::Unwrap<PDFArrayDriver>(args.This()); if(args[0]->ToNumber()->Uint32Value() >= arrayDriver->TheObject->GetLength()) { ThrowException(Exception::Error(String::New("wrong arguments, pass 1 argument which is a valid index in the array"))); return scope.Close(Undefined()); } RefCountPtr<PDFObject> anObject = arrayDriver->TheObject->QueryObject(args[0]->ToNumber()->Uint32Value()); Handle<Value> result = PDFObjectDriver::CreateDriver(anObject.GetPtr()); return scope.Close(result); }
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); }
ObjectIDType DCTDecodeFilterTest::FindDCTDecodedImageObject(PDFParser* inParser) { ObjectIDType imageObject = 0; do { // find image by looking for the first image in the first page RefCountPtr<PDFDictionary> firstPage = inParser->ParsePage(0); if(!firstPage) break; PDFObjectCastPtr<PDFDictionary> resourceDictionary(inParser->QueryDictionaryObject(firstPage.GetPtr(),"Resources")); if(!resourceDictionary) break; PDFObjectCastPtr<PDFDictionary> xobjectDictionary(inParser->QueryDictionaryObject(resourceDictionary.GetPtr(), "XObject")); if(!xobjectDictionary) break; MapIterator<PDFNameToPDFObjectMap> it = xobjectDictionary->GetIterator(); while(it.MoveNext()) { if(it.GetValue()->GetType() == PDFObject::ePDFObjectIndirectObjectReference) { PDFObjectCastPtr<PDFStreamInput> image( inParser->ParseNewObject(((PDFIndirectObjectReference*)it.GetValue())->mObjectID)); RefCountPtr<PDFDictionary> imageDictionary = image->QueryStreamDictionary(); PDFObjectCastPtr<PDFName> objectType = imageDictionary->QueryDirectObject("Subtype"); if(!objectType || objectType->GetValue() != "Image") continue; RefCountPtr<PDFObject> filters = imageDictionary->QueryDirectObject("Filter"); if(!filters) break; if(filters->GetType() == PDFObject::ePDFObjectName && ((PDFName*)filters.GetPtr())->GetValue() == "DCTDecode") { imageObject = ((PDFIndirectObjectReference*)it.GetValue())->mObjectID; break; } PDFArray* filtersArray = (PDFArray*)filters.GetPtr(); if(filtersArray->GetLength() == 1) { PDFObjectCastPtr<PDFName> firstDecoder(filtersArray->QueryObject(0)); if(firstDecoder->GetValue() == "DCTDecode") { imageObject = ((PDFIndirectObjectReference*)it.GetValue())->mObjectID; break; } } } } } while (false); return imageObject; }