Scribus
Open source desktop publishing at your fingertips
pdfwriter.h
1 //
2 // pdfwriter.h
3 // Scribus
4 //
5 // Created by Andreas Vox on 25.12.14.
6 //
7 //
8 
9 #ifndef Scribus_pdfwriter_h
10 #define Scribus_pdfwriter_h
11 
12 #include <QByteArray>
13 #include <QDataStream>
14 #include <QFile>
15 #include <QDateTime>
16 #include <QList>
17 #include <QRect>
18 #include <QString>
19 
20 #include "pdfoptions.h"
21 #include "pdfstructs.h"
22 #include "scstreamfilter.h"
23 
24 namespace Pdf
25 {
26 
27 
28  bool isWhiteSpace(char c);
29  bool isDelimiter(char c);
30  bool isRegular(char c);
31 
35  QByteArray toPdfDocEncoding(QString s);
36 
40  QByteArray toUTF16(QString s);
41 
45  QByteArray toAscii(QString s);
46 
50  QByteArray toPdf(bool v);
51 
55  QByteArray toPdf(int v);
56 
60  QByteArray toPdf(uint v);
61 
65  QByteArray toPdf(qlonglong v);
66 
70  QByteArray toPdf(qulonglong v);
71 
75  QByteArray toPdf(double v);
76 
80  QByteArray toObjRef(PdfId id);
81 
86  QByteArray toLiteralString(QByteArray s);
87  QByteArray toLiteralString(QString s);
88 
92  QByteArray toHexString(QByteArray s);
93 
97  QByteArray toHexString8(quint8 b);
98 
102  QByteArray toHexString16(quint16 b);
103 
107  QByteArray toHexString32(quint32 b);
108 
114  QByteArray toName(QByteArray s);
115  QByteArray toName(QString s);
116 
120  QByteArray toDateString(QDateTime dt);
121 
125  QByteArray toRectangleArray(QRect r);
126  QByteArray toRectangleArray(QRectF r);
127 
128 
129 
130 
146 class Writer
147 {
148 public:
149  Writer();
150 
151  // file handling
152  bool open (const QString& filename);
153  QDataStream& getOutStream() { return outStream; }
154  bool close(bool aborted);
155  qint64 bytesWritten() { return Spool.pos(); }
156 
157  // encryption
158  void setFileId(const QByteArray& id);
159  void setEncryption(bool keylen16, const QByteArray& ownerKey, const QByteArray& userKey, int permissions);
160  QByteArray encryptBytes(const QByteArray& in, PdfId objNum);
161 
162  QByteArray ComputeRC4Key(PdfId ObjNum);
163 private:
164  void CalcOwnerKey(const QByteArray & Owner, const QByteArray & User);
165  void CalcUserKey(const QByteArray & User, int Permission);
166  QByteArray FitKey(const QByteArray & pass);
167 public:
168 
169  // writing
170  void writeHeader(PDFOptions::PDFVersion vers);
171  void writeXrefAndTrailer();
172  void write(const QByteArray& bytes);
173  void write(const Pdf::ResourceDictionary& dict);
174  void write(const PdfFont font);
175 
176  // objects
177  PdfId reserveObjects(unsigned int n);
178 
179  PdfId newObject() { return reserveObjects(1); }
180 
181  void startObj(PdfId id);
182 
183  PdfId startObj() {
184  PdfId res = newObject(); startObj(res); return res;
185  }
186 
187  void endObj(PdfId id);
188  void endObjectWithStream(bool encrypted, PdfId id, const QByteArray& streamContent);
189  ScStreamFilter* openStreamFilter(bool encrypted, PdfId objId);
190 
191 
192  // private:
193  PdfId CatalogObj; // 1
194  PdfId InfoObj; // 2
195  PdfId OutlinesObj; // 3
196  PdfId PagesObj; // 4
197  PdfId DestsObj; // 5
198  PdfId AcroFormObj; // 6
199  PdfId NamesObj; // 7
200  PdfId ThreadsObj; // 8
201  PdfId OCPropertiesObj; // (9)
202  PdfId OutputIntentObj; // (9 or 10)
203  PdfId EncryptObj;
204  PdfId MetaDataObj;
205  PdfId ResourcesObj;
206 
207 private:
208  PdfId ObjCounter;
209  PdfId CurrentObj;
210 
211  QFile Spool;
212  QDataStream outStream;
213 
214  QList<qint64> XRef;
215 
216  QByteArray KeyGen;
217  QByteArray OwnerKey;
218  QByteArray UserKey;
219  QByteArray FileID;
220  QByteArray EncryKey;
221  int KeyLen;
222 
223 };
224 
225 }
226 #endif
Definition: pdfstructs.h:31
Definition: pdfstructs.h:22
Definition: pdfwriter.h:146
Definition: pdfstructs.h:156
Definition: scstreamfilter.h:14
Defines class PDFOptions, used for loading/saving/passing around PDF options.