1 | // |
2 | // Cell.cpp |
3 | // |
4 | // Library: PDF |
5 | // Package: PDFCore |
6 | // Module: Cell |
7 | // |
8 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/PDF/Cell.h" |
16 | |
17 | |
18 | namespace Poco { |
19 | namespace PDF { |
20 | |
21 | |
22 | Cell::Cell(const AttributedString& content, const std::string& name, FontMapPtr pFontMap): |
23 | _content(content), _name(name) |
24 | { |
25 | setFonts(pFontMap); |
26 | } |
27 | |
28 | |
29 | Cell::Cell(const AttributedString& content, FontMapPtr pFontMap, const std::string& encoding, bool trueType): |
30 | _content(content), _encoding(encoding), _trueType(trueType) |
31 | { |
32 | setFonts(pFontMap); |
33 | } |
34 | |
35 | |
36 | Cell::~Cell() |
37 | { |
38 | } |
39 | |
40 | |
41 | void Cell::setFonts(FontMapPtr pFontMap) |
42 | { |
43 | _pFontMap = pFontMap; |
44 | if (_pFontMap) poco_assert(_pFontMap->size() == 4); |
45 | } |
46 | |
47 | |
48 | void Cell::borderTopBottom(bool show) |
49 | { |
50 | borderTop(show); |
51 | borderBottom(show); |
52 | } |
53 | |
54 | |
55 | void Cell::borderLeftRight(bool show) |
56 | { |
57 | borderLeft(show); |
58 | borderRight(show); |
59 | } |
60 | |
61 | |
62 | void Cell::borderAll(bool show) |
63 | { |
64 | borderTop(show); |
65 | borderBottom(show); |
66 | borderLeft(show); |
67 | borderRight(show); |
68 | } |
69 | |
70 | |
71 | void Cell::draw(Page& page, float x, float y, float width, float height) |
72 | { |
73 | // uncomment to force showing of the cell outline regardless of settings |
74 | // _outline = 15; |
75 | |
76 | if (_outline != OUTLINE_NONE) |
77 | { |
78 | page.setLineWidth(_lineWidth); |
79 | page.moveTo(x, y); |
80 | if (_outline & OUTLINE_LEFT ) page.lineTo(x, y+height); |
81 | if (_outline & OUTLINE_TOP ) page.lineTo(x+width, y+height); |
82 | if (_outline & OUTLINE_RIGHT ) page.lineTo(x+width, y ); |
83 | if (_outline & OUTLINE_BOTTOM) page.lineTo(x, y ); |
84 | page.stroke(); |
85 | } |
86 | |
87 | std::string text = _content; |
88 | if (!text.empty()) |
89 | { |
90 | Font originalFont = page.getFont(); |
91 | float originalSize = page.getFontSize(); |
92 | try |
93 | { |
94 | // font names hard coded here, needs more work on the library level |
95 | int fontStyle = _content.getAttribute(AttributedString::ATTR_STYLE); |
96 | float fontSize = _content.getAttribute(AttributedString::ATTR_SIZE); |
97 | if (fontStyle == AttributedString::STYLE_PLAIN) |
98 | { |
99 | if (!_pFontMap) page.setFont("Helvetica" , fontSize); |
100 | else if (_trueType) page.setTTFont((*_pFontMap)[AttributedString::STYLE_PLAIN], fontSize, _encoding); |
101 | else page.setFont((*_pFontMap)[AttributedString::STYLE_PLAIN], fontSize, _encoding); |
102 | } |
103 | else if (fontStyle | AttributedString::STYLE_BOLD) |
104 | { |
105 | if (!_pFontMap) page.setFont("Helvetica-Bold" , fontSize); |
106 | else if (_trueType) page.setTTFont((*_pFontMap)[AttributedString::STYLE_BOLD], fontSize, _encoding); |
107 | else page.setFont((*_pFontMap)[AttributedString::STYLE_BOLD], fontSize, _encoding); |
108 | } |
109 | else if (fontStyle | AttributedString::STYLE_ITALIC) |
110 | { |
111 | if (!_pFontMap) page.setFont("Helvetica-Oblique" , fontSize); |
112 | else if (_trueType) page.setTTFont((*_pFontMap)[AttributedString::STYLE_ITALIC], fontSize, _encoding); |
113 | else page.setFont((*_pFontMap)[AttributedString::STYLE_ITALIC], fontSize, _encoding); |
114 | } |
115 | else if ((fontStyle & AttributedString::STYLE_BOLD) && (fontStyle & AttributedString::STYLE_ITALIC)) |
116 | { |
117 | if (!_pFontMap) page.setFont("Helvetica-BoldOblique" , fontSize); |
118 | else if (_trueType) page.setTTFont((*_pFontMap)[AttributedString::STYLE_BOLD | AttributedString::STYLE_ITALIC], fontSize, _encoding); |
119 | else page.setFont((*_pFontMap)[AttributedString::STYLE_BOLD | AttributedString::STYLE_ITALIC], fontSize, _encoding); |
120 | } |
121 | else |
122 | { |
123 | throw NotFoundException(format("Unknown font style: %d" , fontStyle)); |
124 | } |
125 | |
126 | float tw = page.textWidth(text); |
127 | // trim text that does not fit |
128 | while (tw > width && text.size()) |
129 | { |
130 | text = text.substr(0, text.size() - 1); |
131 | tw = page.textWidth(text); |
132 | } |
133 | float th = page.getFontSize(); |
134 | float yPos = (height <= th) ? y : y + (height - th) / 2; |
135 | int align = _content.getAttribute(AttributedString::ATTR_ALIGN); |
136 | switch (align) |
137 | { |
138 | case AttributedString::ALIGN_LEFT: |
139 | page.writeOnce(x + 5, yPos, text); break; |
140 | case AttributedString::ALIGN_CENTER: |
141 | page.writeOnce(x + (width - tw) / 2, yPos, text); break; |
142 | break; |
143 | case AttributedString::ALIGN_RIGHT: |
144 | page.writeOnce(x + (width - tw), yPos, text); break; |
145 | break; |
146 | default: |
147 | throw NotFoundException(format("Unknown alignment mode: %d" , align)); |
148 | } |
149 | } |
150 | catch (Poco::Exception&) |
151 | { |
152 | page.setFont(originalFont, originalSize); |
153 | throw; |
154 | } |
155 | page.setFont(originalFont, originalSize); |
156 | } |
157 | } |
158 | |
159 | |
160 | } } // namespace Poco::PDF |
161 | |