forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPage.cpp
More file actions
executable file
·219 lines (164 loc) · 5.1 KB
/
Page.cpp
File metadata and controls
executable file
·219 lines (164 loc) · 5.1 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
//
// Page.cpp
//
// Library: PDF
// Package: PDFCore
// Module: Page
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/PDF/Page.h"
#include "Poco/PDF/Document.h"
#include "Poco/PDF/PDFException.h"
#undef min
#undef max
#include <limits>
namespace Poco {
namespace PDF {
Page::Page(Document* pDocument,
const HPDF_Page& page,
Size pageSize,
Orientation orientation):
_pDocument(pDocument),
_page(page),
_size(pageSize),
_orientation(orientation),
_pCurrentFont(0)
{
}
Page::Page(const Page& other):
_pDocument(other._pDocument),
_page(other._page),
_size(other._size),
_orientation(other._orientation),
_pCurrentFont(other._pCurrentFont ? new Font(*other._pCurrentFont) : (Font*)0)
{
}
Page::~Page()
{
delete _pCurrentFont;
}
Page& Page::operator = (const Page& page)
{
Page tmp(page);
swap(tmp);
return *this;
}
bool Page::operator == (const Page& other) const
{
return &_pDocument->handle() == &other._pDocument->handle() && _page == other._page;
}
void Page::swap(Page& other)
{
using std::swap;
swap(_pDocument, other._pDocument);
swap(_page, other._page);
swap(_size, other._size);
swap(_orientation, other._orientation);
swap(_pCurrentFont, other._pCurrentFont);
}
void Page::writeOnce(float xPos, float yPos, const std::string& text)
{
beginText();
write(xPos, yPos, text);
endText();
}
int Page::writeOnceInRectangle(float left,
float top,
float right,
float bottom,
const std::string& text,
TextAlignment align)
{
beginText();
int ret = writeInRectangle(left, top, right, bottom, text, align);
endText();
return ret;
}
float Page::textWidth(const std::string& text)
{
return HPDF_Page_TextWidth(_page, text.c_str());
}
void Page::setFont(const std::string& name, float size, const std::string& encoding)
{
setFont(_pDocument->font(name, encoding), size);
}
void Page::setTTFont(const std::string& name, float size, const std::string& encoding, bool embed)
{
setFont(_pDocument->font(_pDocument->loadTTFont(name, embed), encoding), size);
}
const Font& Page::getFont() const
{
delete _pCurrentFont; _pCurrentFont = 0;
HPDF_Font pCurFont = HPDF_Page_GetCurrentFont(_page);
if (!pCurFont) throw Poco::NullPointerException("PDF::Page has no font set.");
_pCurrentFont = new Font(&_pDocument->handle(), pCurFont);
return *_pCurrentFont;
}
void Page::setRotation(int angle)
{
if (0 != angle % 90 || angle > std::numeric_limits<HPDF_UINT16>::max())
throw InvalidArgumentException("Invalid angle value.");
HPDF_Page_SetRotate(_page, static_cast<HPDF_UINT16>(angle));
}
const Destination& Page::createDestination(const std::string& name)
{
DestinationContainer::iterator it = _destinations.find(name);
if (_destinations.end() != it)
throw InvalidArgumentException("Destination already exists.");
Destination dest(&_pDocument->handle(), HPDF_Page_CreateDestination(_page), name);
std::pair<DestinationContainer::iterator, bool> ret =
_destinations.insert(DestinationContainer::value_type(name, dest));
if (ret.second) return ret.first->second;
throw IllegalStateException("Could not create destination.");
}
const TextAnnotation& Page::createTextAnnotation(const std::string& name,
const Rectangle& rect,
const std::string& text,
const Encoder& encoder)
{
TextAnnotationContainer::iterator it = _textAnnotations.find(name);
if (_textAnnotations.end() != it)
throw InvalidArgumentException("Annotation already exists.");
TextAnnotation ann(&_pDocument->handle(),
HPDF_Page_CreateTextAnnot(_page, rect, text.c_str(), encoder),
name);
std::pair<TextAnnotationContainer::iterator, bool> ret =
_textAnnotations.insert(TextAnnotationContainer::value_type(name, ann));
if (ret.second) return ret.first->second;
throw IllegalStateException("Could not create annotation.");
}
const LinkAnnotation& Page::createLinkAnnotation(const std::string& name,
const Rectangle& rect,
const Destination& dest)
{
LinkAnnotationContainer::iterator it = _linkAnnotations.find(name);
if (_linkAnnotations.end() != it)
throw InvalidArgumentException("Annotation already exists.");
LinkAnnotation ann(&_pDocument->handle(),
HPDF_Page_CreateLinkAnnot(_page, rect, dest),
name);
std::pair<LinkAnnotationContainer::iterator, bool> ret =
_linkAnnotations.insert(LinkAnnotationContainer::value_type(name, ann));
if (ret.second) return ret.first->second;
throw IllegalStateException("Could not create annotation.");
}
const LinkAnnotation& Page::createURILinkAnnotation(const std::string& name,
const Rectangle& rect,
const std::string& uri)
{
LinkAnnotationContainer::iterator it = _linkAnnotations.find(name);
if (_linkAnnotations.end() != it)
throw InvalidArgumentException("Annotation already exists.");
LinkAnnotation ann(&_pDocument->handle(),
HPDF_Page_CreateURILinkAnnot(_page, rect, uri.c_str()),
name);
std::pair<LinkAnnotationContainer::iterator, bool> ret =
_linkAnnotations.insert(LinkAnnotationContainer::value_type(name, ann));
if (ret.second) return ret.first->second;
throw IllegalStateException("Could not create annotation.");
}
} } // namespace Poco::PDF