1 /***************************************************************************
2 * Copyright (C) 2006 by Dominik Seichter *
3 * domseichter@web.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 * *
20 * In addition, as a special exception, the copyright holders give *
21 * permission to link the code of portions of this program with the *
22 * OpenSSL library under certain conditions as described in each *
23 * individual source file, and distribute linked combinations *
24 * including the two. *
25 * You must obey the GNU General Public License in all respects *
26 * for all of the code used other than OpenSSL. If you modify *
27 * file(s) with this exception, you may extend this exception to your *
28 * version of the file(s), but you are not obligated to do so. If you *
29 * do not wish to do so, delete this exception statement from your *
30 * version. If you delete this exception statement from all source *
31 * files in the program, then also delete it here. *
32 ***************************************************************************/
33
34 #include "PdfContents.h"
35
36 #include "base/PdfDefinesPrivate.h"
37 #include "base/PdfArray.h"
38 #include "base/PdfDictionary.h"
39 #include "base/PdfName.h"
40 #include "base/PdfOutputDevice.h"
41
42 #include "PdfDocument.h"
43 #include "PdfPage.h"
44
45 #include <iostream>
46
47 namespace PoDoFo {
48
PdfContents(PdfDocument * pParent)49 PdfContents::PdfContents( PdfDocument* pParent )
50 : PdfElement( NULL, pParent )
51 {
52 mContObj = this->GetObject();
53 }
54
PdfContents(PdfVecObjects * pParent)55 PdfContents::PdfContents( PdfVecObjects* pParent )
56 : PdfElement( NULL, pParent )
57 {
58 mContObj = this->GetObject();
59 }
60
PdfContents(PdfObject * inObj)61 PdfContents::PdfContents( PdfObject* inObj )
62 // A PdfElement expects normally a dictionary
63 // But we may get here, a reference, a dictionary
64 // or an array. Therefore, tell PdfElement
65 // that we also want to accept the datatype of
66 // the object we send in.
67 : PdfElement( inObj->GetDataType(), inObj )
68 {
69 if ( this->GetObject()->GetDataType() == ePdfDataType_Reference )
70 mContObj = inObj->GetOwner()->MustGetObject( this->GetObject()->GetReference() );
71 else
72 mContObj = this->GetObject();
73 }
74
PdfContents(PdfPage * pParent)75 PdfContents::PdfContents( PdfPage* pParent )
76 : PdfElement( NULL, pParent->GetObject()->GetOwner() )
77 {
78 // TODO: Maybe create this only on demand
79 pParent->GetObject()->GetDictionary().AddKey( "Contents", this->GetObject()->Reference() );
80 mContObj = this->GetObject();
81 }
82
GetContentsForAppending() const83 PdfObject* PdfContents::GetContentsForAppending() const
84 {
85 // if ( mContObj->GetDataType() == ePdfDataType_Stream ||
86 // mContObj->GetDataType() == ePdfDataType_Dictionary ) {
87
88 // Use PdfObject::HasStream() instead of the datatype ePdfDataType_Stream
89 // as large parts of the code rely on all PdfObjects having the datatype
90 // ePdfDataType_Dictionary wether they have a stream or not
91 if( mContObj->GetDataType() == ePdfDataType_Dictionary ) {
92 return mContObj; // just return the stream itself
93 } else if ( mContObj->GetDataType() == ePdfDataType_Array ) {
94 /*
95 Create a new stream, add it to the array, return it
96 */
97 PdfObject* newStm = mContObj->GetOwner()->CreateObject();
98 newStm->GetStream();
99 PdfReference pdfr( newStm->Reference().ObjectNumber(), newStm->Reference().GenerationNumber() );
100
101 PdfArray& cArr = mContObj->GetArray();
102 cArr.push_back( pdfr );
103 return newStm;
104 } else {
105 PODOFO_RAISE_ERROR( ePdfError_InvalidDataType );
106 }
107 }
108
109 };
110
111