Debian Codegraph @

src/lib/ABWStylesCollector.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libabw 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 <limits>
11 #include <vector>
12
13 #include <boost/algorithm/string.hpp>
14
15 #include <librevenge/librevenge.h>
16 #include "ABWStylesCollector.h"
17
18 #define ABW_EPSILON 1.0E-06
19
20 namespace libabw
21 {
22
23 namespace
24 {
25
26 enum ABWListType
27 {
28 NUMBERED_LIST = 0,
29 LOWERCASE_LIST = 1,
30 UPPERCASE_LIST = 2,
31 LOWERROMAN_LIST = 3,
32 UPPERROMAN_LIST = 4,
33
34 BULLETED_LIST = 5,
35 DASHED_LIST = 6,
36 SQUARE_LIST = 7,
37 TRIANGLE_LIST = 8,
38 DIAMOND_LIST = 9,
39 STAR_LIST = 10,
40 IMPLIES_LIST = 11,
41 TICK_LIST = 12,
42 BOX_LIST = 13,
43 HAND_LIST = 14,
44 HEART_LIST = 15,
45 ARROWHEAD_LIST = 16,
46
47 LAST_BULLETED_LIST = 17,
48 OTHER_NUMBERED_LISTS = 0x7f,
49 ARABICNUMBERED_LIST = 0x80,
50 HEBREW_LIST = 0x81,
51 NOT_A_LIST = 0xff
52 };
53
54 static int abw_unichar_to_utf8(unsigned c, char *outbuf)
55 {
56 unsigned char len = 1;
57 unsigned char first = 0;
58
59 if (c < 0x80)
60 {
61 first = 0;
62 len = 1;
63 }
64 else if (c < 0x800)
65 {
66 first = 0xc0;
67 len = 2;
68 }
69 else if (c < 0x10000)
70 {
71 first = 0xe0;
72 len = 3;
73 }
74 else if (c < 0x200000)
75 {
76 first = 0xf0;
77 len = 4;
78 }
79 else if (c < 0x4000000)
80 {
81 first = 0xf8;
82 len = 5;
83 }
84 else
85 {
86 first = 0xfc;
87 len = 6;
88 }
89
90 if (outbuf)
91 {
92 for (auto i = (unsigned char)(len - 1); i > 0; --i)
93 {
94 outbuf[i] = (char)((c & 0x3f) | 0x80);
95 c >>= 6;
96 }
97 outbuf[0] = (char)(c | first);
98 }
99
100 return len;
101 }
102
103 static void appendUCS4(librevenge::RVNGString &str, unsigned ucs4)
104 {
105 int charLength = abw_unichar_to_utf8(ucs4, nullptr);
106 std::vector<char> utf8(charLength+1);
107 utf8[charLength] = '\0';
108 abw_unichar_to_utf8(ucs4, utf8.data());
109 str.append(utf8.data());
110 }
111
112 } // anonymous namespace
113
114 } // namespace libabw
115
116 libabw::ABWStylesTableState::ABWStylesTableState() :
117 m_currentCellProperties(),
118 m_currentTableWidth(0),
119 m_currentTableRow(-1),
120 m_currentTableId(-1) {}
121
122 libabw::ABWStylesTableState::ABWStylesTableState(const ABWStylesTableState &ts) :
123 m_currentCellProperties(ts.m_currentCellProperties),
124 m_currentTableWidth(ts.m_currentTableWidth),
125 m_currentTableRow(ts.m_currentTableRow),
126 m_currentTableId(ts.m_currentTableId) {}
127
128 libabw::ABWStylesTableState::~ABWStylesTableState() {}
129
130 libabw::ABWStylesParsingState::ABWStylesParsingState() :
131 m_tableStates() {}
132
133 libabw::ABWStylesParsingState::ABWStylesParsingState(const ABWStylesParsingState &ps) :
134 m_tableStates(ps.m_tableStates) {}
135
136 libabw::ABWStylesParsingState::~ABWStylesParsingState() {}
137
138 libabw::ABWStylesCollector::ABWStylesCollector(std::map<int, int> &tableSizes,
139 std::map<std::string, ABWData> &data,
140 std::map<int, std::shared_ptr<ABWListElement>> &listElements) :
141 m_ps(new ABWStylesParsingState),
142 m_tableSizes(tableSizes),
143 m_data(data),
144 m_tableCounter(0),
145 m_listElements(listElements) {}
146
147 libabw::ABWStylesCollector::~ABWStylesCollector()
148 {
149 }
150
151 void libabw::ABWStylesCollector::openTable(const char *)
152 {
153 m_ps->m_tableStates.push(ABWStylesTableState());
154 m_ps->m_tableStates.top().m_currentTableId = m_tableCounter++;
155 m_ps->m_tableStates.top().m_currentTableRow = -1;
156 m_ps->m_tableStates.top().m_currentTableWidth = 0;
157 }
158
159 void libabw::ABWStylesCollector::closeTable()
160 {
161 if (!m_ps->m_tableStates.empty())
162 {
163 auto curWidth = m_ps->m_tableStates.top().m_currentTableWidth;
164 m_tableSizes[m_ps->m_tableStates.top().m_currentTableId] = curWidth > 0 ? curWidth : 0;
165 m_ps->m_tableStates.pop();
166 }
167 }
168
169 void libabw::ABWStylesCollector::openCell(const char *props)
170 {
171 if (!m_ps->m_tableStates.empty())
172 {
173 if (props)
174 parsePropString(props, m_ps->m_tableStates.top().m_currentCellProperties);
175 int currentRow(0);
176 if (!findInt(_findCellProperty("top-attach"), currentRow))
177 {
178 currentRow = m_ps->m_tableStates.top().m_currentTableRow;
179 if (currentRow < std::numeric_limits<int>::max())
180 ++currentRow;
181 }
182 if (m_ps->m_tableStates.top().m_currentTableRow < currentRow)
183 m_ps->m_tableStates.top().m_currentTableRow = currentRow;
184
185 if (0 == m_ps->m_tableStates.top().m_currentTableRow)
186 {
187 int leftAttach(0);
188 int rightAttach(0);
189 if (findInt(_findCellProperty("left-attach"), leftAttach)
190 && findInt(_findCellProperty("right-attach"), rightAttach)
191 && leftAttach >= 0
192 && rightAttach > leftAttach
193 && rightAttach - leftAttach < std::numeric_limits<int>::max() - m_ps->m_tableStates.top().m_currentTableWidth
194 )
195 m_ps->m_tableStates.top().m_currentTableWidth += rightAttach - leftAttach;
196 else
197 m_ps->m_tableStates.top().m_currentTableWidth++;
198 }
199 }
200 }
201
202 void libabw::ABWStylesCollector::closeCell()
203 {
204 if (!m_ps->m_tableStates.empty())
205 m_ps->m_tableStates.top().m_currentCellProperties.clear();
206 }
207
208 std::string libabw::ABWStylesCollector::_findCellProperty(const char *name)
209 {
210 ABWPropertyMap::const_iterator iter = m_ps->m_tableStates.top().m_currentCellProperties.find(name);
211 if (iter != m_ps->m_tableStates.top().m_currentCellProperties.end())
212 return iter->second;
213 return std::string();
214 }
215
216 void libabw::ABWStylesCollector::collectData(const char *name, const char *mimeType, const librevenge::RVNGBinaryData &data)
217 {
218 if (!name)
219 return;
220 m_data[name] = ABWData(mimeType ? mimeType : "", data);
221 }
222
223 void libabw::ABWStylesCollector::_processList(int id, const char *listDelim, int parentid, int startValue, int type)
224 {
225 using namespace boost;
226 using namespace boost::algorithm;
227
228 if ((type >= BULLETED_LIST && type < LAST_BULLETED_LIST) || type == NOT_A_LIST)
229 {
230 auto tmpElement = std::make_shared<ABWUnorderedListElement>();
231 switch (type)
232 {
233 case BULLETED_LIST:
234 appendUCS4(tmpElement->m_bulletChar, 0x2022);
235 break;
236 case DASHED_LIST:
237 appendUCS4(tmpElement->m_bulletChar, 0x002D);
238 break;
239 case SQUARE_LIST:
240 appendUCS4(tmpElement->m_bulletChar, 0x25A0);
241 break;
242 case TRIANGLE_LIST:
243 appendUCS4(tmpElement->m_bulletChar, 0x25B2);
244 break;
245 case DIAMOND_LIST:
246 appendUCS4(tmpElement->m_bulletChar, 0x2666);
247 break;
248 case STAR_LIST:
249 appendUCS4(tmpElement->m_bulletChar, 0x2733);
250 break;
251 case IMPLIES_LIST:
252 appendUCS4(tmpElement->m_bulletChar, 0x21D2);
253 break;
254 case TICK_LIST:
255 appendUCS4(tmpElement->m_bulletChar, 0x2713);
256 break;
257 case BOX_LIST:
258 appendUCS4(tmpElement->m_bulletChar, 0x2752);
259 break;
260 case HAND_LIST:
261 appendUCS4(tmpElement->m_bulletChar, 0x261E);
262 break;
263 case HEART_LIST:
264 appendUCS4(tmpElement->m_bulletChar, 0x2665);
265 break;
266 case ARROWHEAD_LIST:
267 appendUCS4(tmpElement->m_bulletChar, 0x27A3);
268 break;
269 default:
270 tmpElement->m_bulletChar = ""; // for the while
271 break;
272 }
273 m_listElements[id] = tmpElement;
274 }
275 else
276 {
277 auto tmpElement = std::make_shared<ABWOrderedListElement>();
278 switch (type)
279 {
280 case NUMBERED_LIST:
281 tmpElement->m_numFormat = "1";
282 break;
283 case LOWERCASE_LIST:
284 tmpElement->m_numFormat = "a";
285 break;
286 case UPPERCASE_LIST:
287 tmpElement->m_numFormat = "A";
288 break;
289 case LOWERROMAN_LIST:
290 tmpElement->m_numFormat = "i";
291 break;
292 case UPPERROMAN_LIST:
293 tmpElement->m_numFormat = "I";
294 break;
295 default:
296 tmpElement->m_numFormat = "1";
297 break;
298 }
299 tmpElement->m_startValue = startValue;
300
301 // get prefix and suffix by splitting the listDelim
302 if (listDelim)
303 {
304 std::string delim(listDelim);
305 std::vector<librevenge::RVNGString> strVec;
306
307 for (split_iterator<std::string::iterator> It =
308 make_split_iterator(delim, first_finder("%L", is_iequal()));
309 It != split_iterator<std::string::iterator>(); ++It)
310 {
311 strVec.push_back(copy_range<std::string>(*It).c_str());
312 }
313 if (2 <= strVec.size())
314 {
315 tmpElement->m_numPrefix = strVec[0];
316 tmpElement->m_numSuffix = strVec[1];
317 }
318 }
319 m_listElements[id] = tmpElement;
320 }
321 if (parentid)
322 m_listElements[id]->m_parentId = parentid;
323 }
324
325 void libabw::ABWStylesCollector::collectList(const char *id, const char *, const char *listDelim,
326 const char *parentid, const char *startValue, const char *type)
327 {
328 int intId(0);
329 if (!id || !findInt(id, intId) || intId < 0)
330 intId = 0;
331 if (!intId)
332 return;
333 if (m_listElements[intId])
334 m_listElements[intId].reset();
335 int intType(0);
336 if (!type || !findInt(type, intType) || intType < 0)
337 intType = 5;
338 int intParentId(0);
339 if (!parentid || !findInt(parentid, intParentId) || intParentId < 0)
340 intParentId = 0;
341 int intStartValue(0);
342 if (!startValue || !findInt(startValue, intStartValue) || intStartValue < 0)
343 intStartValue = 0;
344 _processList(intId, listDelim, intParentId, intStartValue, intType);
345 }
346
347 void libabw::ABWStylesCollector::collectParagraphProperties(const char *level, const char *listid, const char *parentid, const char * /* style */, const char *props)
348 {
349 ABWPropertyMap properties;
350 if (props)
351 parsePropString(props, properties);
352
353 int intParentId(0);
354 if (!parentid || !findInt(parentid, intParentId) || intParentId < 0)
355 intParentId = 0;
356 int intListId(0);
357 if (!listid || !findInt(listid, intListId) || intListId < 0)
358 intListId = 0;
359
360 auto iter = m_listElements.find(intListId);
361 if (iter == m_listElements.end() || !iter->second)
362 {
363 ABWPropertyMap::const_iterator i = properties.find("list-style");
364 int listStyle(NOT_A_LIST);
365 if (i != properties.end())
366 {
367 if (i->second == "Numbered List")
368 listStyle = NUMBERED_LIST;
369 else if (i->second == "Lower Case List")
370 listStyle = LOWERCASE_LIST;
371 else if (i->second == "Upper Case List")
372 listStyle = UPPERCASE_LIST;
373 else if (i->second == "Lower Roman List")
374 listStyle = LOWERROMAN_LIST;
375 else if (i->second == "Upper Roman List")
376 listStyle = UPPERROMAN_LIST;
377 else if (i->second == "Hebrew List")
378 listStyle = HEBREW_LIST;
379 else if (i->second == "Arabic List")
380 listStyle = ARABICNUMBERED_LIST;
381 else if (i->second == "Bullet List")
382 listStyle = BULLETED_LIST;
383 else if (i->second == "Dashed List")
384 listStyle = DASHED_LIST;
385 else if (i->second == "Square List")
386 listStyle = SQUARE_LIST;
387 else if (i->second == "Triangle List")
388 listStyle = TRIANGLE_LIST;
389 else if (i->second == "Diamond List")
390 listStyle = DIAMOND_LIST;
391 else if (i->second == "Star List")
392 listStyle = STAR_LIST;
393 else if (i->second == "Implies List")
394 listStyle = IMPLIES_LIST;
395 else if (i->second == "Tick List")
396 listStyle = TICK_LIST;
397 else if (i->second == "Box List")
398 listStyle = BOX_LIST;
399 else if (i->second == "Hand List")
400 listStyle = HAND_LIST;
401 else if (i->second == "Heart List")
402 listStyle = HEART_LIST;
403 else if (i->second == "Arrowhead List")
404 listStyle = ARROWHEAD_LIST;
405 else
406 listStyle = NOT_A_LIST;
407 }
408 i = properties.find("start-value");
409 std::string startValue;
410 if (i != properties.end())
411 startValue = i->second;
412 int intStartValue(0);
413 if (startValue.empty() || findInt(startValue, intStartValue) || intStartValue < 0)
414 intStartValue = 0;
415 _processList(intListId, "%L", intParentId, intStartValue, listStyle);
416 iter = m_listElements.find(intListId);
417 }
418 if (iter != m_listElements.end() && iter->second)
419 {
420 const auto &listElement = iter->second;
421
422 if (!level || !findInt(level, listElement->m_listLevel) || listElement->m_listLevel < 0)
423 listElement->m_listLevel = 0;
424
425 ABWPropertyMap::const_iterator i = properties.find("margin-left");
426 ABWUnit unit(ABW_NONE);
427 double marginLeft(0.0);
428 if (i == properties.end() || !findDouble(i->second, marginLeft, unit) || unit != ABW_IN)
429 marginLeft = 0.0;
430 i = properties.find("text-indent");
431 double textIndent(0.0);
432 if (i == properties.end() || !findDouble(i->second, textIndent, unit) || unit != ABW_IN)
433 textIndent = 0.0;
434 listElement->m_minLabelWidth = -textIndent;
435 listElement->m_spaceBefore = marginLeft + textIndent;
436 }
437 }
438
439 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */