forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument.cpp
More file actions
366 lines (295 loc) · 9.44 KB
/
Document.cpp
File metadata and controls
366 lines (295 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//
// Document.cpp
//
// $Id: //poco/Main/PDF/src/Document.cpp#2 $
//
// Library: PDF
// Package: PDFCore
// Module: Document
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/PDF/Document.h"
#include "Poco/PDF/PDFException.h"
#include "Poco/File.h"
#include "Poco/Path.h"
#include "Poco/LocalDateTime.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/StringTokenizer.h"
#include "Poco/NumberParser.h"
#include <utility>
namespace Poco {
namespace PDF {
Document::Document(const std::string fileName,
Poco::UInt32 pageCount,
Page::Size pageSize,
Page::Orientation orientation):
_pdf(HPDF_New(HPDF_Error_Handler, 0)),
_fileName(fileName),
_pRawData(0),
_size(0)
{
compression(COMPRESSION_ALL);
for (Poco::UInt32 i = 0; i < pageCount; ++i)
addPage(pageSize, orientation);
}
Document::Document(Poco::UInt32 pageCount,
Page::Size pageSize,
Page::Orientation orientation):
_pdf(HPDF_New(HPDF_Error_Handler, 0)),
_pRawData(0),
_size(0)
{
compression(COMPRESSION_ALL);
for (Poco::UInt32 i = 0; i < pageCount; ++i)
addPage();
}
Document::~Document()
{
HPDF_Free(_pdf);
delete _pRawData;
}
void Document::createNew(bool resetAll)
{
reset(resetAll);
HPDF_NewDoc(_pdf);
}
void Document::save(const std::string fileName)
{
std::string fn = fileName.empty() ? _fileName : fileName;
if (fn.empty())
HPDF_SaveToStream (_pdf);
else
HPDF_SaveToFile(_pdf, fn.c_str());
}
const Document::DataPtr Document::data(SizeType& sz)
{
sz = size();
delete _pRawData;
_pRawData = new HPDF_BYTE[sz];
HPDF_ReadFromStream(_pdf, _pRawData, &sz);
return _pRawData;
}
Document::SizeType Document::size()
{
if (HPDF_Stream_Validate(_pdf->stream)) HPDF_ResetStream(_pdf);
HPDF_SaveToStream(_pdf);
return _size = HPDF_GetStreamSize(_pdf);
}
const Page& Document::addPage(Page::Size pageSize, Page::Orientation orientation)
{
Page page(&_pdf, HPDF_AddPage(_pdf), pageSize);
page.setSizeAndOrientation(pageSize, orientation);
_pages.push_back(page);
return _pages.back();
}
const Page& Document::insertPage(int index,
Page::Size pageSize,
Page::Orientation orientation)
{
poco_assert (index > 0);
poco_assert (index < _pages.size());
HPDF_Page target = *((HPDF_Page*) HPDF_List_ItemAt(_pdf->page_list, static_cast<HPDF_UINT>(index)));
return *_pages.insert(_pages.begin() + index,
Page(&_pdf,
HPDF_InsertPage(_pdf, target),
pageSize,
orientation));
}
const Page& Document::getCurrentPage()
{
Page p(&_pdf, HPDF_GetCurrentPage(_pdf));
PageContainer::iterator it = _pages.begin();
PageContainer::iterator end = _pages.end();
for (;it != end; ++it)
if (*it == p) return *it;
throw NotFoundException("Current page not found.");
}
const Font& Document::loadFont(const std::string& name, const std::string& encoding)
{
Font font(&_pdf, HPDF_GetFont(_pdf, name.c_str(), encoding.empty() ? 0 : encoding.c_str()));
std::pair<FontContainer::iterator, bool> ret =
_fonts.insert(FontContainer::value_type(name, font));
if (ret.second) return ret.first->second;
throw IllegalStateException("Could not create font.");
}
const Font& Document::font(const std::string& name, const std::string& encoding)
{
FontContainer::iterator it = _fonts.find(name);
if (_fonts.end() == it) return loadFont(name, encoding);
throw IllegalStateException("Could not create font.");
}
std::string Document::loadType1Font(const std::string& afmFileName, const std::string& pfmFileName)
{
return HPDF_LoadType1FontFromFile(_pdf, afmFileName.c_str(), pfmFileName.c_str());
}
std::string Document::loadTTFont(const std::string& fileName, bool embedding, int index)
{
if (-1 == index)
{
return HPDF_LoadTTFontFromFile(_pdf,
fileName.c_str(),
embedding ? HPDF_TRUE : HPDF_FALSE);
}
else if (index >= 0)
{
return HPDF_LoadTTFontFromFile2(_pdf,
fileName.c_str(),
static_cast<HPDF_UINT>(index),
embedding ? HPDF_TRUE : HPDF_FALSE);
}
else
throw InvalidArgumentException("Invalid font index.");
}
const Image& Document::loadPNGImageImpl(const std::string& fileName, bool doLoad)
{
Path path(fileName);
if (File(path).exists())
{
std::pair<ImageContainer::iterator, bool> it;
if (doLoad)
{
Image image(&_pdf, HPDF_LoadPngImageFromFile(_pdf, fileName.c_str()));
it = _images.insert(ImageContainer::value_type(path.getBaseName(), image));
}
else
{
Image image(&_pdf, HPDF_LoadPngImageFromFile2(_pdf, fileName.c_str()));
it = _images.insert(ImageContainer::value_type(path.getBaseName(), image));
}
if (it.second) return it.first->second;
else throw IllegalStateException("Could not insert image.");
}
else
throw NotFoundException("File not found: " + fileName);
}
const Image& Document::loadJPEGImage(const std::string& fileName)
{
Path path(fileName);
if (File(path).exists())
{
Image image(&_pdf, HPDF_LoadJpegImageFromFile(_pdf, fileName.c_str()));
std::pair<ImageContainer::iterator, bool> it =
_images.insert(ImageContainer::value_type(path.getBaseName(), image));
if (it.second) return it.first->second;
else throw IllegalStateException("Could not insert image.");
}
else
throw NotFoundException("File not found: " + fileName);
}
void Document::encryption(Encryption mode, Poco::UInt32 keyLength)
{
if (ENCRYPT_R3 == mode && (keyLength < 5 || keyLength > 128))
throw InvalidArgumentException("Invalid key length.");
HPDF_SetEncryptionMode(_pdf,
static_cast<HPDF_EncryptMode>(mode),
static_cast<HPDF_UINT>(keyLength));
}
const Encoder& Document::loadEncoder(const std::string& name)
{
EncoderContainer::iterator it = _encoders.find(name);
if (_encoders.end() == it) return it->second;
Encoder enc(&_pdf, HPDF_GetEncoder(_pdf, name.c_str()), name);
std::pair<EncoderContainer::iterator, bool> ret =
_encoders.insert(EncoderContainer::value_type(name, enc));
if (ret.second) return ret.first->second;
throw IllegalStateException("Could not create encoder.");
}
const Encoder& Document::getCurrentEncoder()
{
HPDF_Encoder enc = HPDF_GetCurrentEncoder(_pdf);
std::string name = enc->name;
EncoderContainer::iterator it = _encoders.find(name);
if (_encoders.end() == it)
{
std::pair<EncoderContainer::iterator, bool> ret =
_encoders.insert(EncoderContainer::value_type(name, Encoder(&_pdf, enc)));
if (ret.second) return ret.first->second;
}
return it->second;
}
const Encoder& Document::setCurrentEncoder(const std::string& name)
{
loadEncoder(name);
HPDF_SetCurrentEncoder(_pdf, name.c_str());
return getCurrentEncoder();
}
const Outline& Document::createOutline(const std::string& title, const Outline& parent, const Encoder& encoder)
{
_outlines.push_back(Outline(&_pdf, HPDF_CreateOutline(_pdf, parent, title.c_str(), encoder)));
return _outlines.back();
}
void Document::setInfo(Info info, const LocalDateTime& dt)
{
if (INFO_CREATION_DATE == info || INFO_MOD_DATE == info)
{
HPDF_Date hpdfDate;
hpdfDate.year = dt.year();
hpdfDate.month = dt.month();
hpdfDate.day = dt.day();
hpdfDate.hour = dt.hour();
hpdfDate.minutes = dt.minute();
hpdfDate.seconds = dt.second();
std::string tzd = DateTimeFormatter::tzdISO(dt.tzd());
StringTokenizer st(tzd, "+-:");
if (st.count() >= 2)
{
hpdfDate.ind = tzd[0];
hpdfDate.off_hour = NumberParser::parse(st[0]);
hpdfDate.off_minutes = NumberParser::parse(st[1]);
}
else hpdfDate.ind = ' ';
HPDF_SetInfoDateAttr(_pdf, static_cast<HPDF_InfoType>(info), hpdfDate);
}
else
throw InvalidArgumentException("Can not set document info.");
}
void Document::setInfo(Info info, const std::string& value)
{
if (INFO_CREATION_DATE == info || INFO_MOD_DATE == info)
throw InvalidArgumentException("Can not set document date.");
HPDF_SetInfoAttr(_pdf, static_cast<HPDF_InfoType>(info), value.c_str());
}
void Document::setPassword(const std::string& ownerPassword, const std::string& userPassword)
{
if (ownerPassword.empty())
throw InvalidArgumentException("Owner password empty.");
HPDF_SetPassword(_pdf, ownerPassword.c_str(), userPassword.c_str());
}
void Document::reset(bool all)
{
if (!all) HPDF_FreeDoc(_pdf);
else
{
_pages.clear();
_fonts.clear();
_encoders.clear();
_outlines.clear();
_images.clear();
HPDF_FreeDocAll(_pdf);
}
}
} } // namespace Poco::PDF