Debian Codegraph @

src/lib/EPUBTableStyleManager.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libepubgen project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include "EPUBTableStyleManager.h"
11
12 #include <numeric>
13 #include <sstream>
14
15 #include <librevenge/librevenge.h>
16
17 #include "libepubgen_utils.h"
18 #include "EPUBCSSContent.h"
19
20 namespace libepubgen
21 {
22
23 using librevenge::RVNGPropertyList;
24 using librevenge::RVNGPropertyListVector;
25 using librevenge::RVNGString;
26
27 void EPUBTableStyleManager::openTable(RVNGPropertyList const &propList)
28 {
29 const librevenge::RVNGPropertyListVector *columns = propList.child("librevenge:table-columns");
30 if (columns)
31 {
32 std::vector<double> colWidths;
33 std::vector<double> relColWidths;
34 for (unsigned long i = 0; i < columns->count(); i++)
35 {
36 RVNGPropertyList const &prop=(*columns)[i];
37 double width=0;
38 if (prop["style:column-width"])
39 {
40 librevenge::RVNGUnit unit=prop["style:column-width"]->getUnit();
41 if (unit==librevenge::RVNG_POINT)
42 width=prop["style:column-width"]->getDouble()/72.;
43 else if (unit==librevenge::RVNG_INCH)
44 width=prop["style:column-width"]->getDouble();
45 else if (unit==librevenge::RVNG_TWIP)
46 width=prop["style:column-width"]->getDouble()/1440.;
47 }
48 colWidths.push_back(width);
49
50 if (prop["style:rel-column-width"])
51 {
52 width = prop["style:rel-column-width"]->getDouble();
53 relColWidths.push_back(width);
54 }
55 }
56 m_columnWidthsStack.push_back(colWidths);
57 m_relColumnWidthsStack.push_back(relColWidths);
58 }
59 }
60
61 void EPUBTableStyleManager::closeTable()
62 {
63 if (!m_columnWidthsStack.size())
64 {
65 EPUBGEN_DEBUG_MSG(("EPUBTableStyleManager::closeTable: can not find the columns width\n"));
66 return;
67 }
68 m_columnWidthsStack.pop_back();
69 }
70
71 namespace
72 {
73 bool extractColumnsWidth(const std::vector< std::vector<double> > &columnWidthsStack, int col, int numSpanned, bool relative, double &w)
74 {
75 if (!columnWidthsStack.size())
76 return false;
77 std::vector<double> const &widths=columnWidthsStack.back();
78 double total = std::accumulate(widths.begin(), widths.end(), static_cast<double>(0));
79 if (col < 0 || size_t(col+numSpanned-1) >= widths.size())
80 {
81 if (!relative)
82 {
83 EPUBGEN_DEBUG_MSG(("EPUBTableStyleManager::getColumnsWidth: can not compute the columns width\n"));
84 }
85 return false;
86 }
87 bool fixed = true;
88 w = 0;
89 for (auto i=size_t(col); i < size_t(col+numSpanned); i++)
90 {
91 if (widths[i] < 0)
92 {
93 w += -widths[i];
94 fixed = false;
95 }
96 else if (widths[i] > 0)
97 w += widths[i];
98 else
99 {
100 w = 0;
101 return true;
102 }
103 }
104 if (!fixed) w = -w;
105 if (relative)
106 // Expected unit is percents.
107 w = w * 100 / total;
108 return true;
109 }
110 }
111
112 bool EPUBTableStyleManager::getColumnsWidth(int col, int numSpanned, double &w) const
113 {
114 return extractColumnsWidth(m_columnWidthsStack, col, numSpanned, false, w);
115 }
116
117 bool EPUBTableStyleManager::getRelColumnsWidth(int col, int numSpanned, double &w) const
118 {
119 return extractColumnsWidth(m_relColumnWidthsStack, col, numSpanned, true, w);
120 }
121
122 std::string EPUBTableStyleManager::getCellClass(RVNGPropertyList const &pList)
123 {
124 EPUBCSSProperties content;
125 extractCellProperties(pList, content);
126 ContentNameMap_t::const_iterator it=m_cellContentNameMap.find(content);
127 if (it != m_cellContentNameMap.end())
128 return it->second;
129 std::stringstream s;
130 s << "cellTable" << m_cellContentNameMap.size();
131 m_cellContentNameMap[content]=s.str();
132 return s.str();
133 }
134
135 std::string EPUBTableStyleManager::getCellStyle(RVNGPropertyList const &pList)
136 {
137 EPUBCSSProperties content;
138 extractCellProperties(pList, content);
139
140 std::stringstream s;
141 for (const auto &property : content)
142 s << property.first << ": " << property.second << "; ";
143 return s.str();
144 }
145
146 std::string EPUBTableStyleManager::getRowClass(RVNGPropertyList const &pList)
147 {
148 EPUBCSSProperties content;
149 extractRowProperties(pList, content);
150 ContentNameMap_t::const_iterator it=m_rowContentNameMap.find(content);
151 if (it != m_rowContentNameMap.end())
152 return it->second;
153 std::stringstream s;
154 s << "rowTable" << m_rowContentNameMap.size();
155 m_rowContentNameMap[content]=s.str();
156 return s.str();
157 }
158
159 std::string EPUBTableStyleManager::getRowStyle(RVNGPropertyList const &pList)
160 {
161 EPUBCSSProperties content;
162 extractRowProperties(pList, content);
163
164 std::stringstream s;
165 for (const auto &property : content)
166 s << property.first << ": " << property.second << "; ";
167 return s.str();
168 }
169
170 std::string EPUBTableStyleManager::getTableClass(RVNGPropertyList const &pList)
171 {
172 EPUBCSSProperties content;
173 extractTableProperties(pList, content);
174 ContentNameMap_t::const_iterator it=m_tableContentNameMap.find(content);
175 if (it != m_tableContentNameMap.end())
176 return it->second;
177 std::stringstream s;
178 s << "table" << m_tableContentNameMap.size();
179 m_tableContentNameMap[content]=s.str();
180 return s.str();
181 }
182
183 std::string EPUBTableStyleManager::getTableStyle(RVNGPropertyList const &pList)
184 {
185 EPUBCSSProperties content;
186 extractTableProperties(pList, content);
187
188 std::stringstream s;
189 for (const auto &property : content)
190 s << property.first << ": " << property.second << "; ";
191 return s.str();
192 }
193
194 void EPUBTableStyleManager::send(EPUBCSSContent &out)
195 {
196 for (ContentNameMap_t::const_iterator it=m_cellContentNameMap.begin(); m_cellContentNameMap.end() != it; ++it)
197 {
198 RVNGPropertyList props;
199 fillPropertyList(it->first, props);
200 out.insertRule(("." + it->second).c_str(), props);
201 }
202
203 for (ContentNameMap_t::const_iterator it=m_rowContentNameMap.begin(); m_rowContentNameMap.end() != it; ++it)
204 {
205 RVNGPropertyList props;
206 fillPropertyList(it->first, props);
207 out.insertRule(("." + it->second).c_str(), props);
208 }
209
210 for (ContentNameMap_t::const_iterator it=m_tableContentNameMap.begin(); m_tableContentNameMap.end() != it; ++it)
211 {
212 RVNGPropertyList props;
213 fillPropertyList(it->first, props);
214 out.insertRule(("." + it->second).c_str(), props);
215 }
216 }
217
218 void EPUBTableStyleManager::extractCellProperties(RVNGPropertyList const &pList, EPUBCSSProperties &cssProps) const
219 {
220 // try to get the cell width
221 if (pList["librevenge:column"])
222 {
223 int c=pList["librevenge:column"]->getInt();
224 int span=1;
225 if (pList["table:number-columns-spanned"])
226 span = pList["table:number-columns-spanned"]->getInt();
227 double w;
228 if (!getColumnsWidth(c,span,w))
229 {
230 EPUBGEN_DEBUG_MSG(("EPUBTableStyleManager::getCellContent: can not find columns witdth for %d[%d]\n", c, span));
231 }
232 else if (w > 0)
233 {
234 std::ostringstream width;
235 width << w << "in";
236 cssProps["width"] = width.str();
237 }
238 else if (w < 0)
239 {
240 std::ostringstream width;
241 width << -w << "in";
242 cssProps["min-width"] = width.str();
243 }
244
245 if (getRelColumnsWidth(c, span, w))
246 {
247 std::ostringstream width;
248 width << w << "%";
249 cssProps["width"] = width.str();
250 }
251 }
252 if (pList["fo:text-align"])
253 {
254 if (pList["fo:text-align"]->getStr() == RVNGString("end")) // stupid OOo convention..
255 cssProps["text-align"] = "right";
256 else
257 cssProps["text-align"] = pList["fo:text-align"]->getStr().cstr();
258 }
259 const librevenge::RVNGProperty *verticalAlign = pList["style:vertical-align"];
260 if (verticalAlign && !verticalAlign->getStr().empty())
261 cssProps["vertical-align"] = verticalAlign->getStr().cstr();
262 else
263 cssProps["vertical-align"] = "top";
264 if (pList["fo:background-color"])
265 cssProps["background-color"] = pList["fo:background-color"]->getStr().cstr();
266
267 static char const *type[] = {"border", "border-left", "border-top", "border-right", "border-bottom" };
268 for (auto &i : type)
269 {
270 std::string field("fo:");
271 field+=i;
272 if (!pList[field.c_str()])
273 continue;
274 cssProps[i] = pList[field.c_str()]->getStr().cstr();
275 }
276 }
277
278 void EPUBTableStyleManager::extractRowProperties(RVNGPropertyList const &pList, EPUBCSSProperties &cssProps) const
279 {
280 if (pList["style:min-row-height"])
281 cssProps["min-height"] = pList["style:min-row-height"]->getStr().cstr();
282 else if (pList["style:row-height"])
283 cssProps["height"] = pList["style:row-height"]->getStr().cstr();
284 }
285
286 void EPUBTableStyleManager::extractTableProperties(RVNGPropertyList const &pList, EPUBCSSProperties &cssProps) const
287 {
288 if (pList["style:rel-width"])
289 cssProps["width"] = pList["style:rel-width"]->getStr().cstr();
290 else if (pList["style:width"])
291 cssProps["width"] = pList["style:width"]->getStr().cstr();
292 }
293
294 }
295
296 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */