forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.cpp
More file actions
executable file
·164 lines (140 loc) · 4.44 KB
/
Cell.cpp
File metadata and controls
executable file
·164 lines (140 loc) · 4.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
//
// Cell.cpp
//
#include "Poco/PDF/Cell.h"
namespace Poco {
namespace PDF {
Cell::Cell(const AttributedString& content, const std::string& name, FontMapPtr pFontMap):
_content(content),
_name(name),
_outline(OUTLINE_NONE),
_lineWidth(1.0f),
_encoding("UTF-8"),
_trueType(true),
_widthAsPct(-1)
{
setFonts(pFontMap);
}
Cell::Cell(const AttributedString& content, FontMapPtr pFontMap, const std::string& encoding, bool trueType, int widthAsPct):
_content(content),
_outline(OUTLINE_NONE),
_lineWidth(1.0f),
_encoding(encoding),
_trueType(trueType),
_widthAsPct(widthAsPct)
{
setFonts(pFontMap);
}
Cell::~Cell()
{
}
void Cell::setFonts(FontMapPtr pFontMap)
{
_pFontMap = pFontMap;
if (_pFontMap) poco_assert(_pFontMap->size() == 4);
}
void Cell::borderTopBottom(bool show)
{
borderTop(show);
borderBottom(show);
}
void Cell::borderLeftRight(bool show)
{
borderLeft(show);
borderRight(show);
}
void Cell::borderAll(bool show)
{
borderTop(show);
borderBottom(show);
borderLeft(show);
borderRight(show);
}
void Cell::draw(Page& page, float x, float y, float width, float height)
{
// uncomment to force showing of the cell outline regardless of settings
//_outline = 15;
if (_outline != OUTLINE_NONE)
{
page.setLineWidth(_lineWidth);
page.moveTo(x, y);
if (_outline & OUTLINE_LEFT ) page.lineTo(x, y+height);
page.moveTo(x, y+height);
if (_outline & OUTLINE_TOP ) page.lineTo(x+width, y+height);
page.moveTo(x+width, y+height);
if (_outline & OUTLINE_RIGHT ) page.lineTo(x+width, y );
page.moveTo(x+width, y);
if (_outline & OUTLINE_BOTTOM) page.lineTo(x, y );
page.stroke();
}
std::string text = _content;
if (!text.empty())
{
Font originalFont = page.getFont();
float originalSize = page.getFontSize();
try
{
// font names hard coded here, needs more work on the library level
int fontStyle = _content.getAttribute(AttributedString::ATTR_STYLE);
float fontSize = _content.getAttribute(AttributedString::ATTR_SIZE);
if (fontStyle == AttributedString::STYLE_PLAIN)
{
if (!_pFontMap) page.setFont("Helvetica", fontSize);
else if (_trueType) page.setTTFont((*_pFontMap)[AttributedString::STYLE_PLAIN], fontSize, _encoding);
else page.setFont((*_pFontMap)[AttributedString::STYLE_PLAIN], fontSize, _encoding);
}
else if (fontStyle | AttributedString::STYLE_BOLD)
{
if (!_pFontMap) page.setFont("Helvetica-Bold", fontSize);
else if (_trueType) page.setTTFont((*_pFontMap)[AttributedString::STYLE_BOLD], fontSize, _encoding);
else page.setFont((*_pFontMap)[AttributedString::STYLE_BOLD], fontSize, _encoding);
}
else if (fontStyle | AttributedString::STYLE_ITALIC)
{
if (!_pFontMap) page.setFont("Helvetica-Oblique", fontSize);
else if (_trueType) page.setTTFont((*_pFontMap)[AttributedString::STYLE_ITALIC], fontSize, _encoding);
else page.setFont((*_pFontMap)[AttributedString::STYLE_ITALIC], fontSize, _encoding);
}
else if ((fontStyle & AttributedString::STYLE_BOLD) && (fontStyle & AttributedString::STYLE_ITALIC))
{
if (!_pFontMap) page.setFont("Helvetica-BoldOblique", fontSize);
else if (_trueType) page.setTTFont((*_pFontMap)[AttributedString::STYLE_BOLD | AttributedString::STYLE_ITALIC], fontSize, _encoding);
else page.setFont((*_pFontMap)[AttributedString::STYLE_BOLD | AttributedString::STYLE_ITALIC], fontSize, _encoding);
}
else
{
throw NotFoundException(format("Unknown font style: %d", fontStyle));
}
float tw = page.textWidth(text);
// trim text that does not fit
while (tw > width && text.size())
{
text = text.substr(0, text.size() - 1);
tw = page.textWidth(text);
}
float th = page.getFontSize();
float yPos = (height <= th) ? y : y + (height - th) / 2;
int align = _content.getAttribute(AttributedString::ATTR_ALIGN);
switch (align)
{
case AttributedString::ALIGN_LEFT:
page.writeOnce(x + 5, yPos, text); break;
case AttributedString::ALIGN_CENTER:
page.writeOnce(x + (width - tw) / 2, yPos, text); break;
break;
case AttributedString::ALIGN_RIGHT:
page.writeOnce(x + (width - tw), yPos, text); break;
break;
default:
throw NotFoundException(format("Unknown alignment mode: %d", align));
}
}
catch (Poco::Exception&)
{
page.setFont(originalFont, originalSize);
throw;
}
page.setFont(originalFont, originalSize);
}
}
} } // namespace Poco::PDF