Debian Codegraph @

src/lib/ABWContentCollector.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 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <cassert>
15 #include <locale>
16 #include <memory>
17 #include <sstream>
18
19 #include <boost/spirit/include/qi.hpp>
20 #include <boost/algorithm/string.hpp>
21 #include <boost/optional.hpp>
22 #include <librevenge/librevenge.h>
23 #include "ABWContentCollector.h"
24 #include "libabw_internal.h"
25
26 #define ABW_EPSILON 1.0E-06
27 #define MAX_LIST_LEVEL 64 // a safeguard against damaged files
28 #define MAX_TABLE_ROW (1 << 16) // a safeguard against damaged top-attach
29
30 using boost::optional;
31
32 namespace libabw
33 {
34
35 namespace
36 {
37
38 static std::string getColor(const std::string &s)
39 {
40 if (s.empty())
41 return s;
42
43 if (s[0] == '#')
44 {
45 if (s.length() != 7)
46 return std::string();
47 else
48 return s;
49 }
50 else if (s.length() != 6)
51 return std::string();
52
53 std::string out = ("#");
54 out.append(s);
55 return out;
56 }
57
58 static void separateTabsAndInsertText(ABWOutputElements &outputElements, const librevenge::RVNGString &text)
59 {
60 if (text.empty())
61 return;
62 librevenge::RVNGString tmpText;
63 librevenge::RVNGString::Iter i(text);
64 for (i.rewind(); i.next();)
65 {
66 if (*(i()) == '\t')
67 {
68 if (!tmpText.empty())
69 {
70 outputElements.addInsertText(tmpText);
71 tmpText.clear();
72 }
73 outputElements.addInsertTab();
74 }
75 else if (*(i()) == '\n'|| *(i()) == (char)0x0a)
76 {
77 if (!tmpText.empty())
78 {
79 outputElements.addInsertText(tmpText);
80 tmpText.clear();
81 }
82 outputElements.addInsertLineBreak();
83 }
84 else
85 {
86 tmpText.append(i());
87 }
88 }
89 if (!tmpText.empty())
90 outputElements.addInsertText(tmpText);
91 }
92
93 static void separateSpacesAndInsertText(ABWOutputElements &outputElements, const librevenge::RVNGString &text)
94 {
95 if (text.empty())
96 {
97 outputElements.addInsertText(text);
98 return;
99 }
100 librevenge::RVNGString tmpText;
101 int numConsecutiveSpaces = 0;
102 librevenge::RVNGString::Iter i(text);
103 for (i.rewind(); i.next();)
104 {
105 if (*(i()) == ' ')
106 numConsecutiveSpaces++;
107 else
108 numConsecutiveSpaces = 0;
109
110 if (numConsecutiveSpaces > 1)
111 {
112 if (!tmpText.empty())
113 {
114 separateTabsAndInsertText(outputElements, tmpText);
115 tmpText.clear();
116 }
117 outputElements.addInsertSpace();
118 }
119 else
120 tmpText.append(i());
121 }
122 separateTabsAndInsertText(outputElements, tmpText);
123 }
124
125 void parseTableColumns(const std::string &str, librevenge::RVNGPropertyListVector &columns)
126 {
127 if (str.empty())
128 return;
129
130 std::string propString(boost::trim_copy_if(str, boost::is_any_of("/ ")));
131 std::vector<std::string> strVec;
132
133 boost::algorithm::split(strVec, propString, boost::is_any_of("/"), boost::token_compress_on);
134 for (auto &i : strVec)
135 {
136 ABWUnit unit(ABW_NONE);
137 double value(0.0);
138 boost::algorithm::trim(i);
139 if (findDouble(i, value, unit) || ABW_IN != unit)
140 {
141 librevenge::RVNGPropertyList propList;
142 propList.insert("style:column-width", value);
143 columns.append(propList);
144 }
145 }
146 }
147
148 bool parseTabStop(const std::string &str, librevenge::RVNGPropertyList &tabStop)
149 {
150 if (str.empty())
151 return false;
152 std::string sTabStop(boost::trim_copy_if(str, boost::is_any_of("/ ")));
153 std::vector<std::string> strVec;
154 boost::algorithm::split(strVec, sTabStop, boost::is_any_of("/"), boost::token_compress_on);
155 if (strVec.size() < 2)
156 return false;
157 boost::algorithm::trim(strVec[0]);
158 ABWUnit unit(ABW_NONE);
159 double value(0.0);
160 if (!findDouble(strVec[0], value, unit) || ABW_IN != unit)
161 return false;
162 tabStop.insert("style:position", value);
163
164 boost::algorithm::trim(strVec[1]);
165 if (!strVec[1].empty())
166 {
167 switch (strVec[1][0])
168 {
169 case 'L':
170 tabStop.insert("style:type", "left");
171 break;
172 case 'C':
173 tabStop.insert("style:type", "center");
174 break;
175 case 'D':
176 tabStop.insert("style:type", "char");
177 break;
178 case 'R':
179 tabStop.insert("style:type", "right");
180 break;
181 default:
182 tabStop.insert("style:type", "left");
183 break;
184 }
185 }
186
187 if (strVec[1].size() > 1)
188 {
189 switch (strVec[1][1])
190 {
191 case '3':
192 tabStop.insert("style:leader-text", "_");
193 break;
194 case '2':
195 tabStop.insert("style:leader-text", "-");
196 break;
197 case '1':
198 tabStop.insert("style:leader-text", ".");
199 break;
200 default:
201 break;
202 }
203 }
204
205 return true;
206 }
207
208 void parseTabStops(const std::string &str, librevenge::RVNGPropertyListVector &tabStops)
209 {
210 if (str.empty())
211 return;
212 std::string sTabStops(boost::trim_copy_if(str, boost::is_any_of(", ")));
213 std::vector<std::string> strVec;
214 boost::algorithm::split(strVec, sTabStops, boost::is_any_of(","), boost::token_compress_on);
215 for (auto &i : strVec)
216 {
217 boost::trim(i);
218 librevenge::RVNGPropertyList tabStop;
219 if (parseTabStop(i, tabStop))
220 tabStops.append(tabStop);
221 }
222 }
223
224 void parseLang(const std::string &langStr, optional<std::string> &lang, optional<std::string> &country, optional<std::string> &script)
225 {
226 std::vector<std::string> tags;
227 tags.reserve(3);
228 boost::split(tags, langStr, boost::is_any_of("-_"), boost::token_compress_off);
229
230 if ((tags.size() > 0) && boost::all(tags[0], boost::is_lower()) && (2 <= tags[0].size()) && (3 >= tags[0].size()))
231 {
232 lang = tags[0];
233
234 if (tags.size() > 1)
235 {
236 if (boost::all(tags[1], boost::is_upper()) && (2 == tags[1].size()))
237 country = tags[1];
238 else
239 script = tags[1];
240 }
241
242 if ((tags.size() > 2) && bool(script))
243 {
244 if (boost::all(tags[2], boost::is_upper()) && (2 == tags[2].size()))
245 country = tags[2];
246 }
247 }
248 }
249
250 static std::string decodeUrl(const std::string &str)
251 {
252 using namespace boost::spirit::qi;
253
254 if (str.empty())
255 return str;
256
257 // look for a hexadecimal number of 2 digits
258 uint_parser<char,16,2,2> urlhex;
259 std::string decoded_string;
260 auto it = str.cbegin();
261 if (parse(it, str.cend(),
262 +(
263 (lit('%') >> (char_('%') | urlhex))
264 | (!lit('%') >> char_)
265 ),
266 decoded_string)
267 && it == str.cend())
268 return decoded_string;
269
270 return str;
271 }
272
273 std::string findProperty(const ABWPropertyMap &propMap, const char *const name)
274 {
275 if (!name)
276 return std::string();
277 auto iter = propMap.find(name);
278 if (iter != propMap.end())
279 return iter->second;
280 return std::string();
281 }
282
283 } // anonymous namespace
284
285 } // namespace libabw
286
287 libabw::ABWContentTableState::ABWContentTableState() :
288 m_currentTableProperties(),
289 m_currentCellProperties(),
290
291 m_currentTableCol(-1),
292 m_currentTableRow(-1),
293 m_currentTableCellNumberInRow(-1),
294 m_currentTableId(-1),
295 m_isTableRowOpened(false),
296 m_isTableColumnOpened(false),
297 m_isTableCellOpened(false),
298 m_isCellWithoutParagraph(false),
299 m_isRowWithoutCell(false)
300 {
301 }
302
303 libabw::ABWContentTableState::ABWContentTableState(const ABWContentTableState &ts) :
304 m_currentTableProperties(ts.m_currentTableProperties),
305 m_currentCellProperties(ts.m_currentCellProperties),
306
307 m_currentTableCol(ts.m_currentTableCol),
308 m_currentTableRow(ts.m_currentTableRow),
309 m_currentTableCellNumberInRow(ts.m_currentTableCellNumberInRow),
310 m_currentTableId(ts.m_currentTableId),
311 m_isTableRowOpened(ts.m_isTableRowOpened),
312 m_isTableColumnOpened(ts.m_isTableColumnOpened),
313 m_isTableCellOpened(ts.m_isTableCellOpened),
314 m_isCellWithoutParagraph(ts.m_isCellWithoutParagraph),
315 m_isRowWithoutCell(ts.m_isRowWithoutCell)
316 {
317 }
318
319 libabw::ABWContentTableState::~ABWContentTableState()
320 {
321 }
322
323 libabw::ABWContentParsingState::ABWContentParsingState() :
324 m_isDocumentStarted(false),
325 m_isPageSpanOpened(false),
326 m_isSectionOpened(false),
327 m_isHeaderOpened(false),
328 m_isFooterOpened(false),
329
330 m_isPageFrame(false),
331
332 m_isSpanOpened(false),
333 m_isParagraphOpened(false),
334 m_isListElementOpened(false),
335 m_inParagraphOrListElement(false),
336
337 m_currentSectionStyle(),
338 m_currentParagraphStyle(),
339 m_currentCharacterStyle(),
340
341 m_pageWidth(0.0),
342 m_pageHeight(0.0),
343 m_pageMarginTop(0.0),
344 m_pageMarginBottom(0.0),
345 m_pageMarginLeft(0.0),
346 m_pageMarginRight(0.0),
347 m_footerId(-1),
348 m_footerLeftId(-1),
349 m_footerFirstId(-1),
350 m_footerLastId(-1),
351 m_headerId(-1),
352 m_headerLeftId(-1),
353 m_headerFirstId(-1),
354 m_headerLastId(-1),
355 m_currentHeaderFooterId(-1),
356 m_currentHeaderFooterOccurrence(),
357 m_parsingContext(ABW_SECTION),
358
359 m_deferredPageBreak(false),
360 m_deferredColumnBreak(false),
361
362 m_isNote(false),
363 m_currentListLevel(0),
364 m_currentListId(0),
365 m_isFirstTextInListElement(false),
366
367 m_tableStates(),
368 m_listLevels()
369 {
370 }
371
372 libabw::ABWContentParsingState::ABWContentParsingState(const ABWContentParsingState &ps) :
373 m_isDocumentStarted(ps.m_isDocumentStarted),
374 m_isPageSpanOpened(ps.m_isPageSpanOpened),
375 m_isSectionOpened(ps.m_isSectionOpened),
376 m_isHeaderOpened(ps.m_isHeaderOpened),
377 m_isFooterOpened(ps.m_isFooterOpened),
378
379 m_isPageFrame(ps.m_isPageFrame),
380
381 m_isSpanOpened(ps.m_isSpanOpened),
382 m_isParagraphOpened(ps.m_isParagraphOpened),
383 m_isListElementOpened(ps.m_isListElementOpened),
384 m_inParagraphOrListElement(ps.m_inParagraphOrListElement),
385
386 m_currentSectionStyle(ps.m_currentSectionStyle),
387 m_currentParagraphStyle(ps.m_currentParagraphStyle),
388 m_currentCharacterStyle(ps.m_currentCharacterStyle),
389
390 m_pageWidth(ps.m_pageWidth),
391 m_pageHeight(ps.m_pageHeight),
392 m_pageMarginTop(ps.m_pageMarginTop),
393 m_pageMarginBottom(ps.m_pageMarginBottom),
394 m_pageMarginLeft(ps.m_pageMarginLeft),
395 m_pageMarginRight(ps.m_pageMarginRight),
396 m_footerId(ps.m_footerId),
397 m_footerLeftId(ps.m_footerLeftId),
398 m_footerFirstId(ps.m_footerFirstId),
399 m_footerLastId(ps.m_footerLastId),
400 m_headerId(ps.m_headerId),
401 m_headerLeftId(ps.m_headerLeftId),
402 m_headerFirstId(ps.m_headerFirstId),
403 m_headerLastId(ps.m_headerLastId),
404 m_currentHeaderFooterId(ps.m_currentHeaderFooterId),
405 m_currentHeaderFooterOccurrence(ps.m_currentHeaderFooterOccurrence),
406 m_parsingContext(ps.m_parsingContext),
407
408 m_deferredPageBreak(ps.m_deferredPageBreak),
409 m_deferredColumnBreak(ps.m_deferredColumnBreak),
410
411 m_isNote(ps.m_isNote),
412 m_currentListLevel(ps.m_currentListLevel),
413 m_currentListId(ps.m_currentListId),
414 m_isFirstTextInListElement(ps.m_isFirstTextInListElement),
415
416 m_tableStates(ps.m_tableStates),
417 m_listLevels(ps.m_listLevels)
418 {
419 }
420
421 libabw::ABWContentParsingState::~ABWContentParsingState()
422 {
423 }
424
425 libabw::ABWContentCollector::ABWContentCollector(librevenge::RVNGTextInterface *iface, const std::map<int, int> &tableSizes,
426 const std::map<std::string, ABWData> &data,
427 const std::map<int, std::shared_ptr<ABWListElement>> &listElements) :
428 m_ps(new ABWContentParsingState),
429 m_iface(iface),
430 m_parsingStates(),
431 m_dontLoop(),
432 m_textStyles(),
433 m_documentStyle(),
434 m_metadata(),
435 m_data(data),
436 m_tableSizes(tableSizes),
437 m_tableCounter(0),
438 m_outputElements(),
439 m_pageOutputElements(),
440 m_listElements(listElements),
441 m_dummyListElements()
442 {
443 }
444
445 libabw::ABWContentCollector::~ABWContentCollector()
446 {
447 }
448
449 void libabw::ABWContentCollector::collectTextStyle(const char *name, const char *basedon, const char *followedby, const char *props)
450 {
451 ABWStyle style;
452 style.basedon = basedon ? basedon : std::string();
453 style.followedby = followedby ? followedby : std::string();
454 if (props)
455 parsePropString(props, style.properties);
456 if (name)
457 m_textStyles[name] = style;
458 }
459
460 void libabw::ABWContentCollector::_recurseTextProperties(const char *name, ABWPropertyMap &styleProps)
461 {
462 if (name)
463 {
464 m_dontLoop.insert(name);
465 std::map<std::string, ABWStyle>::const_iterator iter = m_textStyles.find(name);
466 if (iter != m_textStyles.end() && !(iter->second.basedon.empty()) && !m_dontLoop.count(iter->second.basedon))
467 _recurseTextProperties(iter->second.basedon.c_str(), styleProps);
468 if (iter != m_textStyles.end())
469 {
470 for (const auto &propertie : iter->second.properties)
471 styleProps[propertie.first] = propertie.second;
472 }
473
474 // Styles based on "Heading X" style are recognized as headings.
475 if (boost::starts_with(name, "Heading "))
476 {
477 int level = 0;
478 const std::string levelStr = std::string(name).substr(8);
479 if (findInt(levelStr, level))
480 {
481 // Abiword only has 4 levels of headings, but allow some more
482 if ((0 < level) && (10 > level))
483 styleProps["libabw:outline-level"] = levelStr;
484 }
485 }
486 }
487 if (!m_dontLoop.empty())
488 m_dontLoop.clear();
489 }
490
491 std::string libabw::ABWContentCollector::_findDocumentProperty(const char *const name)
492 {
493 return findProperty(m_documentStyle, name);
494 }
495
496 std::string libabw::ABWContentCollector::_findParagraphProperty(const char *name)
497 {
498 return findProperty(m_ps->m_currentParagraphStyle, name);
499 }
500
501 std::string libabw::ABWContentCollector::_findTableProperty(const char *name)
502 {
503 assert(!m_ps->m_tableStates.empty());
504 return findProperty(m_ps->m_tableStates.top().m_currentTableProperties, name);
505 }
506
507 std::string libabw::ABWContentCollector::_findCellProperty(const char *name)
508 {
509 assert(!m_ps->m_tableStates.empty());
510 return findProperty(m_ps->m_tableStates.top().m_currentCellProperties, name);
511 }
512
513 std::string libabw::ABWContentCollector::_findSectionProperty(const char *name)
514 {
515 return findProperty(m_ps->m_currentSectionStyle, name);
516 }
517
518 std::string libabw::ABWContentCollector::_findCharacterProperty(const char *name)
519 {
520 std::string prop = findProperty(m_ps->m_currentCharacterStyle, name);
521 if (prop.empty())
522 prop = findProperty(m_ps->m_currentParagraphStyle, name);
523 return prop;
524 }
525
526 std::string libabw::ABWContentCollector::_findMetadataEntry(const char *const name)
527 {
528 return findProperty(m_metadata, name);
529 }
530
531 void libabw::ABWContentCollector::collectDocumentProperties(const char *const props)
532 {
533 if (props)
534 parsePropString(props, m_documentStyle);
535 }
536
537 void libabw::ABWContentCollector::_addBorderProperties(const std::map<std::string, std::string> &map, librevenge::RVNGPropertyList &propList, const std::string &defaultUndefBorderProp)
538 {
539 int setBorders=0;
540 static char const *odtWh[4]= {"fo:border-left", "fo:border-right", "fo:border-top", "fo:border-bottom"};
541 for (int i=0, depl=1; i<4; ++i, depl*=2)
542 {
543 static char const *wh[4]= {"left", "right", "top", "bot"};
544 std::string whString(wh[i]);
545 std::string tmp=whString+"-color";
546 if (map.find(tmp)==map.end()) continue;
547 std::string color=getColor(map.find(tmp)->second);
548 if (color.empty())
549 continue;
550 int style;
551 tmp=whString+"-style";
552 if (map.find(tmp)==map.end() || !findInt(map.find(tmp)->second, style))
553 style=1;
554 else if (style<=0 || style>=4)
555 {
556 if (style==0) setBorders |= depl;
557 continue;
558 }
559 ABWUnit unit(ABW_NONE);
560 double width(0.0);
561 tmp=whString+"-thickness";
562 if (map.find(tmp)==map.end() || !findDouble(map.find(tmp)->second, width, unit))
563 width=0.01;
564 else if (width<=0 || unit != ABW_IN)
565 continue;
566 std::stringstream s;
567 s.imbue(std::locale("C")); // be sure that we use standart double
568 s << width << "in ";
569 if (style==2) s << "dotted ";
570 else if (style==3) s << "dashed ";
571 else s << "solid ";
572 s << color;
573 propList.insert(odtWh[i], s.str().c_str());
574 setBorders|=depl;
575 }
576 if (defaultUndefBorderProp.empty()) return;
577 for (int i=0, depl=1; i<4; ++i, depl*=2)
578 {
579 if (setBorders&depl) continue;
580 propList.insert(odtWh[i], defaultUndefBorderProp.c_str());
581 }
582 }
583
584 void libabw::ABWContentCollector::collectParagraphProperties(const char *level, const char *listid, const char * /*parentid*/, const char *style, const char *props)
585 {
586 _closeBlock();
587 if (!level || !findInt(level, m_ps->m_currentListLevel) || m_ps->m_currentListLevel < 1)
588 m_ps->m_currentListLevel = 0;
589 if (m_ps->m_currentListLevel > MAX_LIST_LEVEL)
590 m_ps->m_currentListLevel = MAX_LIST_LEVEL;
591 if (!listid || !findInt(listid, m_ps->m_currentListId) || m_ps->m_currentListId < 0)
592 m_ps->m_currentListId = 0;
593
594 m_ps->m_currentParagraphStyle.clear();
595 if (style)
596 _recurseTextProperties(style, m_ps->m_currentParagraphStyle);
597 else
598 _recurseTextProperties("Normal", m_ps->m_currentParagraphStyle);
599
600 ABWPropertyMap tmpProps;
601 if (props)
602 parsePropString(props, tmpProps);
603 for (ABWPropertyMap::const_iterator iter = tmpProps.begin(); iter != tmpProps.end(); ++iter)
604 m_ps->m_currentParagraphStyle[iter->first] = iter->second;
605 m_ps->m_inParagraphOrListElement = true;
606 }
607
608 void libabw::ABWContentCollector::collectCharacterProperties(const char *style, const char *props)
609 {
610 // We started a new span without closing the last one. That can actually happen, because <p>
611 // allows mixed content, so there can be text spans not enclosed in <c>.
612 if (m_ps->m_isSpanOpened)
613 _closeSpan();
614
615 m_ps->m_currentCharacterStyle.clear();
616 if (style)
617 _recurseTextProperties(style, m_ps->m_currentCharacterStyle);
618
619 ABWPropertyMap tmpProps;
620 if (props)
621 parsePropString(props, tmpProps);
622 for (ABWPropertyMap::const_iterator iter = tmpProps.begin(); iter != tmpProps.end(); ++iter)
623 m_ps->m_currentCharacterStyle[iter->first] = iter->second;
624 }
625
626 void libabw::ABWContentCollector::collectSectionProperties(const char *footer, const char *footerLeft, const char *footerFirst, const char *footerLast,
627 const char *header, const char *headerLeft, const char *headerFirst, const char *headerLast,
628 const char *props)
629 {
630 _closeHeader();
631 _closeFooter();
632 _closeSection();
633 double pageMarginLeft = m_ps->m_pageMarginLeft;
634 double pageMarginRight = m_ps->m_pageMarginRight;
635 double pageMarginTop = m_ps->m_pageMarginTop;
636 double pageMarginBottom = m_ps->m_pageMarginBottom;
637 int headerId = m_ps->m_headerId;
638 int headerLeftId = m_ps->m_headerLeftId;
639 int headerFirstId = m_ps->m_headerFirstId;
640 int headerLastId = m_ps->m_headerLastId;
641 int footerId = m_ps->m_footerId;
642 int footerLeftId = m_ps->m_footerLeftId;
643 int footerFirstId = m_ps->m_footerFirstId;
644 int footerLastId = m_ps->m_footerLastId;
645
646 m_ps->m_currentSectionStyle.clear();
647 ABWPropertyMap tmpProps;
648 if (props)
649 parsePropString(props, tmpProps);
650
651 ABWUnit unit(ABW_NONE);
652 double value(0.0);
653 for (ABWPropertyMap::const_iterator iter = tmpProps.begin(); iter != tmpProps.end(); ++iter)
654 {
655 if (iter->first == "page-margin-right" && !iter->second.empty() && fabs(m_ps->m_pageMarginRight) < ABW_EPSILON)
656 {
657 if (findDouble(iter->second, value, unit) && unit == ABW_IN && value > 0.0 && fabs(value) > ABW_EPSILON)
658 m_ps->m_pageMarginRight = value;
659 }
660 else if (iter->first == "page-margin-left" && !iter->second.empty() && fabs(m_ps->m_pageMarginLeft) < ABW_EPSILON)
661 {
662 if (findDouble(iter->second, value, unit) && unit == ABW_IN && value > 0.0 && fabs(value) > ABW_EPSILON)
663 m_ps->m_pageMarginLeft = value;
664 }
665 else if (iter->first == "page-margin-top" && !iter->second.empty() && fabs(m_ps->m_pageMarginTop) < ABW_EPSILON)
666 {
667 if (findDouble(iter->second, value, unit) && unit == ABW_IN && value > 0.0 && fabs(value) > ABW_EPSILON)
668 m_ps->m_pageMarginTop = value;
669 }
670 else if (iter->first == "page-margin-bottom" && !iter->second.empty() && fabs(m_ps->m_pageMarginBottom) < ABW_EPSILON)
671 {
672 if (findDouble(iter->second, value, unit) && unit == ABW_IN && value > 0.0 && fabs(value) > ABW_EPSILON)
673 m_ps->m_pageMarginBottom = value;
674 }
675 m_ps->m_currentSectionStyle[iter->first] = iter->second;
676 }
677
678 int intValue(0);
679 if (footer && findInt(footer, intValue) && intValue >= 0)
680 m_ps->m_footerId = intValue;
681 else
682 m_ps->m_footerId = -1;
683
684 if (footerLeft && findInt(footerLeft, intValue) && intValue >= 0)
685 m_ps->m_footerLeftId = intValue;
686 else
687 m_ps->m_footerLeftId = -1;
688
689 if (footerFirst && findInt(footerFirst, intValue) && intValue >= 0)
690 m_ps->m_footerFirstId = intValue;
691 else
692 m_ps->m_footerFirstId = -1;
693
694 if (footerLast && findInt(footerLast, intValue) && intValue >= 0)
695 m_ps->m_footerLastId = intValue;
696 else
697 m_ps->m_footerLastId = -1;
698
699 if (header && findInt(header, intValue) && intValue >= 0)
700 m_ps->m_headerId = intValue;
701 else
702 m_ps->m_headerId = -1;
703
704 if (headerLeft && findInt(headerLeft, intValue) && intValue >= 0)
705 m_ps->m_headerLeftId = intValue;
706 else
707 m_ps->m_headerLeftId = -1;
708
709 if (headerFirst && findInt(headerFirst, intValue) && intValue >= 0)
710 m_ps->m_headerFirstId = intValue;
711 else
712 m_ps->m_headerFirstId = -1;
713
714 if (headerLast && findInt(headerLast, intValue) && intValue >= 0)
715 m_ps->m_headerLastId = intValue;
716 else
717 m_ps->m_headerLastId = -1;
718
719 if (fabs(m_ps->m_pageMarginRight) < ABW_EPSILON)
720 m_ps->m_pageMarginRight = 1.0;
721 if (fabs(m_ps->m_pageMarginLeft) < ABW_EPSILON)
722 m_ps->m_pageMarginLeft = 1.0;
723 if (fabs(m_ps->m_pageMarginTop) < ABW_EPSILON)
724 m_ps->m_pageMarginTop = 1.0;
725 if (fabs(m_ps->m_pageMarginBottom) < ABW_EPSILON)
726 m_ps->m_pageMarginBottom = 1.0;
727
728 if (fabs(pageMarginLeft-m_ps->m_pageMarginLeft) > ABW_EPSILON ||
729 fabs(pageMarginRight-m_ps->m_pageMarginRight) > ABW_EPSILON ||
730 fabs(pageMarginTop-m_ps->m_pageMarginTop) > ABW_EPSILON ||
731 fabs(pageMarginBottom-m_ps->m_pageMarginBottom) > ABW_EPSILON ||
732 footerId != m_ps->m_footerId || footerLeftId != m_ps->m_footerLeftId ||
733 footerFirstId != m_ps->m_footerFirstId || footerLastId != m_ps->m_footerLastId ||
734 headerId != m_ps->m_headerId || headerLeftId != m_ps->m_headerLeftId ||
735 headerFirstId != m_ps->m_headerFirstId || headerLastId != m_ps->m_headerLastId)
736 {
737 _closePageSpan();
738 }
739 }
740
741 void libabw::ABWContentCollector::collectHeaderFooter(const char *id, const char *type)
742 {
743 if (!id || !findInt(id, m_ps->m_currentHeaderFooterId))
744 m_ps->m_currentHeaderFooterId = -1;
745
746 if (!type)
747 m_ps->m_currentHeaderFooterId = -1;
748
749 std::string sType(type ? type : "");
750 boost::trim(sType);
751 std::vector<std::string> strVec;
752 boost::algorithm::split(strVec, sType, boost::is_any_of("-"), boost::token_compress_on);
753 if (strVec.size() >= 2)
754 m_ps->m_currentHeaderFooterOccurrence = strVec[1].c_str();
755 else
756 m_ps->m_currentHeaderFooterOccurrence = "all";
757 if (!strVec.empty())
758 {
759 if (strVec[0] == "header")
760 m_ps->m_parsingContext = ABW_HEADER;
761 else if (strVec[0] == "footer")
762 m_ps->m_parsingContext = ABW_FOOTER;
763 else
764 m_ps->m_parsingContext = ABW_SECTION;
765 }
766 else
767 m_ps->m_parsingContext = ABW_SECTION;
768 }
769
770 void libabw::ABWContentCollector::collectPageSize(const char *width, const char *height, const char *units, const char * /* pageScale */)
771 {
772 std::string widthStr(width ? width : "");
773 std::string heightStr(height ? height : "");
774 if (units)
775 {
776 widthStr.append(units);
777 heightStr.append(units);
778 }
779 ABWUnit unit;
780 double value;
781 if (findDouble(widthStr, value, unit) && unit == ABW_IN)
782 m_ps->m_pageWidth = value;
783 if (findDouble(heightStr, value, unit) && unit == ABW_IN)
784 m_ps->m_pageHeight = value;
785 }
786
787 void libabw::ABWContentCollector::startDocument()
788 {
789 if (!m_ps->m_isNote && m_ps->m_tableStates.empty())
790 {
791
792 if (m_iface && !m_ps->m_isDocumentStarted)
793 {
794 m_iface->startDocument(librevenge::RVNGPropertyList());
795 _setMetadata();
796 }
797
798 m_ps->m_isDocumentStarted = true;
799 }
800 }
801
802 void libabw::ABWContentCollector::endDocument()
803 {
804 if (!m_ps->m_isNote)
805 {
806 if (!m_ps->m_isPageSpanOpened)
807 _openPageSpan();
808
809 _closeBlock();
810
811 m_ps->m_currentListLevel = 0;
812 _changeList(); // flush the list
813
814 // close the document nice and tight
815 _closeSection();
816 _closeHeader();
817 _closeFooter();
818
819 _closePageSpan();
820
821 if (m_iface)
822 {
823 m_pageOutputElements.write(m_iface);
824 m_outputElements.write(m_iface);
825 m_iface->endDocument();
826 }
827 }
828 }
829
830 void libabw::ABWContentCollector::_setMetadata()
831 {
832 librevenge::RVNGPropertyList propList;
833
834 const std::string dcKeys[] = { "language", "publisher", "source", "subject", "title", "type" };
835
836 for (std::size_t i = 0; i != ABW_NUM_ELEMENTS(dcKeys); ++i)
837 {
838 const std::string abwKey = "dc." + dcKeys[i];
839 const std::string rvngKey = "dc:" + dcKeys[i];
840 const std::string prop = _findMetadataEntry(abwKey.c_str());
841 if (!prop.empty())
842 propList.insert(rvngKey.c_str(), prop.c_str());
843 }
844
845 std::string prop = _findMetadataEntry("abiword.keywords");
846 if (!prop.empty())
847 propList.insert("meta:keyword", prop.c_str());
848
849 prop = _findMetadataEntry("dc.creator");
850 if (!prop.empty())
851 propList.insert("meta:initial-creator", prop.c_str());
852
853 #ifdef VERSION
854 const std::string version(VERSION);
855 #else
856 const std::string version("unknown");
857 #endif
858 std::string generator = "libabw/" + version;
859 propList.insert("meta:generator", generator.c_str());
860 if (m_iface)
861 m_iface->setDocumentMetaData(propList);
862 }
863
864 void libabw::ABWContentCollector::endSection()
865 {
866 m_ps->m_currentListLevel = 0;
867 _changeList(); // flush the list exterior
868 _closeHeader();
869 _closeFooter();
870 _closeSection();
871 }
872
873 void libabw::ABWContentCollector::closeParagraphOrListElement()
874 {
875 // we have an empty paragraph, insert it
876 if (!m_ps->m_isParagraphOpened && !m_ps->m_isListElementOpened)
877 _openSpan();
878 _closeBlock();
879 m_ps->m_currentParagraphStyle.clear();
880 m_ps->m_inParagraphOrListElement = false;
881 }
882
883 void libabw::ABWContentCollector::openLink(const char *href)
884 {
885 if (m_ps->m_isSpanOpened)
886 _closeSpan();
887 _openBlock();
888 librevenge::RVNGPropertyList propList;
889 if (href)
890 propList.insert("xlink:href", decodeUrl(href).c_str());
891 m_outputElements.addOpenLink(propList);
892 if (!m_ps->m_isSpanOpened)
893 _openSpan();
894 }
895
896 void libabw::ABWContentCollector::closeLink()
897 {
898 if (m_ps->m_isSpanOpened)
899 _closeSpan();
900 m_outputElements.addCloseLink();
901 }
902
903 void libabw::ABWContentCollector::closeSpan()
904 {
905 _closeSpan();
906 m_ps->m_currentCharacterStyle.clear();
907 }
908
909 void libabw::ABWContentCollector::insertLineBreak()
910 {
911 if (!m_ps->m_isSpanOpened)
912 _openSpan();
913
914 m_outputElements.addInsertLineBreak();
915 }
916
917 void libabw::ABWContentCollector::insertColumnBreak()
918 {
919 _closeBlock();
920 m_ps->m_deferredColumnBreak = true;
921 }
922
923 void libabw::ABWContentCollector::insertPageBreak()
924 {
925 _closeBlock();
926 m_ps->m_deferredPageBreak = true;
927 }
928
929 void libabw::ABWContentCollector::insertText(const char *text)
930 {
931 if (!m_ps->m_inParagraphOrListElement)
932 return;
933 if (m_ps->m_isFirstTextInListElement && text && text[0]==' ' && text[1]==0)
934 return;
935 if (!m_ps->m_isSpanOpened)
936 _openSpan();
937 if (!text)
938 return;
939 if (m_ps->m_isFirstTextInListElement && text[0] == '\t')
940 separateSpacesAndInsertText(m_outputElements, text+1);
941 else
942 separateSpacesAndInsertText(m_outputElements, text);
943 m_ps->m_isFirstTextInListElement = false;
944 }
945
946 void libabw::ABWContentCollector::_openPageSpan()
947 {
948 if (!m_ps->m_isPageSpanOpened && !m_ps->m_isNote && m_ps->m_tableStates.empty())
949 {
950
951 if (!m_ps->m_isDocumentStarted)
952 startDocument();
953
954 librevenge::RVNGPropertyList propList;
955 // assume default page size is A4
956 propList.insert("fo:page-width", m_ps->m_pageWidth>0 ? m_ps->m_pageWidth : 8.27);
957 propList.insert("fo:page-height", m_ps->m_pageHeight>0 ? m_ps->m_pageHeight : 11.7);
958 propList.insert("fo:margin-left", m_ps->m_pageMarginLeft);
959 propList.insert("fo:margin-right", m_ps->m_pageMarginRight);
960 propList.insert("fo:margin-top", m_ps->m_pageMarginTop);
961 propList.insert("fo:margin-bottom", m_ps->m_pageMarginBottom);
962
963 if (!m_ps->m_isPageSpanOpened)
964 m_outputElements.addOpenPageSpan(propList,
965 m_ps->m_footerId, m_ps->m_footerLeftId,
966 m_ps->m_footerFirstId, m_ps->m_footerLastId,
967 m_ps->m_headerId, m_ps->m_headerLeftId,
968 m_ps->m_headerFirstId, m_ps->m_headerLastId);
969 }
970 m_ps->m_isPageSpanOpened = true;
971 }
972
973 void libabw::ABWContentCollector::_closePageSpan()
974 {
975 if (m_ps->m_isPageSpanOpened)
976 {
977 _closeHeader();
978 _closeFooter();
979 _closeSection();
980
981 m_outputElements.addClosePageSpan();
982 }
983 m_ps->m_isPageSpanOpened = false;
984 }
985
986 void libabw::ABWContentCollector::_openSection()
987 {
988 if (!m_ps->m_isSectionOpened && !m_ps->m_isNote && m_ps->m_tableStates.empty())
989 {
990 if (!m_ps->m_isPageSpanOpened)
991 _openPageSpan();
992
993 librevenge::RVNGPropertyList propList;
994
995 ABWUnit unit(ABW_NONE);
996 double value(0.0);
997 if (findDouble(_findSectionProperty("page-margin-right"), value, unit) && unit == ABW_IN)
998 propList.insert("fo:margin-right", value - m_ps->m_pageMarginRight);
999
1000 if (findDouble(_findSectionProperty("page-margin-left"), value, unit) && unit == ABW_IN)
1001 propList.insert("fo:margin-left", value - m_ps->m_pageMarginLeft);
1002
1003 if (findDouble(_findSectionProperty("section-space-after"), value, unit) && unit == ABW_IN)
1004 propList.insert("librevenge:margin-bottom", value);
1005
1006 std::string sValue = _findSectionProperty("dom-dir");
1007 if (sValue.empty()) // try document default
1008 sValue = _findDocumentProperty("dom-dir");
1009 if (sValue == "ltr")
1010 propList.insert("style:writing-mode", "lr-tb");
1011 else if (sValue == "rtl")
1012 propList.insert("style:writing-mode", "rl-tb");
1013
1014 int intValue(0);
1015 if (findInt(_findSectionProperty("columns"), intValue) && intValue > 1)
1016 {
1017 librevenge::RVNGPropertyListVector columns;
1018 for (int i = 0; i < intValue; ++i)
1019 {
1020 librevenge::RVNGPropertyList column;
1021 column.insert("style:rel-width", 1.0 / (double)intValue, librevenge::RVNG_PERCENT);
1022 columns.append(column);
1023 }
1024 if (columns.count())
1025 {
1026 propList.insert("style:columns", columns);
1027 propList.insert("text:dont-balance-text-columns", true);
1028 }
1029 }
1030 m_outputElements.addOpenSection(propList);
1031 }
1032 m_ps->m_isSectionOpened = true;
1033 }
1034
1035 void libabw::ABWContentCollector::_openFooter()
1036 {
1037 if (!m_ps->m_isFooterOpened && !m_ps->m_isNote && m_ps->m_tableStates.empty())
1038 {
1039 librevenge::RVNGPropertyList propList;
1040 propList.insert("librevenge:occurrence", m_ps->m_currentHeaderFooterOccurrence);
1041
1042 m_outputElements.addOpenFooter(propList, m_ps->m_currentHeaderFooterId);
1043 }
1044 m_ps->m_isFooterOpened = true;
1045 }
1046
1047 void libabw::ABWContentCollector::_openHeader()
1048 {
1049 if (!m_ps->m_isHeaderOpened && !m_ps->m_isNote && m_ps->m_tableStates.empty())
1050 {
1051 librevenge::RVNGPropertyList propList;
1052 propList.insert("librevenge:occurrence", m_ps->m_currentHeaderFooterOccurrence);
1053
1054 m_outputElements.addOpenHeader(propList, m_ps->m_currentHeaderFooterId);
1055 }
1056 m_ps->m_isHeaderOpened = true;
1057 }
1058
1059 void libabw::ABWContentCollector::_fillParagraphProperties(librevenge::RVNGPropertyList &propList,
1060 bool isListElement)
1061 {
1062 ABWUnit unit(ABW_NONE);
1063 double value(0.0);
1064 int intValue(0);
1065 std::string sValue;
1066
1067 if (findDouble(_findParagraphProperty("margin-right"), value, unit) && unit == ABW_IN)
1068 propList.insert("fo:margin-right", value);
1069
1070 if (findDouble(_findParagraphProperty("margin-top"), value, unit) && unit == ABW_IN)
1071 propList.insert("fo:margin-top", value);
1072
1073 if (findDouble(_findParagraphProperty("margin-bottom"), value, unit) && unit == ABW_IN)
1074 propList.insert("fo:margin-bottom", value);
1075
1076 if (!isListElement)
1077 {
1078 if (findDouble(_findParagraphProperty("margin-left"), value, unit) && unit == ABW_IN)
1079 propList.insert("fo:margin-left", value);
1080
1081 if (findDouble(_findParagraphProperty("text-indent"), value, unit) && unit == ABW_IN)
1082 propList.insert("fo:text-indent", value);
1083
1084 // TODO: Numbered headings should probably not be handled as lists.
1085 // Just do not make them headings for now.
1086 sValue = _findParagraphProperty("libabw:outline-level");
1087 if (!sValue.empty())
1088 propList.insert("text:outline-level", sValue.c_str());
1089 }
1090
1091 sValue = _findParagraphProperty("text-align");
1092 if (!sValue.empty())
1093 {
1094 if (sValue == "left")
1095 propList.insert("fo:text-align", "start");
1096 else if (sValue == "right")
1097 propList.insert("fo:text-align", "end");
1098 else
1099 propList.insert("fo:text-align", sValue.c_str());
1100 }
1101
1102 sValue = _findParagraphProperty("line-height");
1103 if (!sValue.empty())
1104 {
1105 std::string propName("fo:line-height");
1106 size_t position = sValue.find_last_of('+');
1107 if (position && position != std::string::npos)
1108 {
1109 propName = "style:line-height-at-least";
1110 sValue.erase(position);
1111 }
1112 if (findDouble(sValue, value, unit))
1113 {
1114 if (ABW_IN == unit)
1115 propList.insert(propName.c_str(), value);
1116 else if (ABW_PERCENT == unit)
1117 propList.insert(propName.c_str(), value, librevenge::RVNG_PERCENT);
1118 }
1119 }
1120
1121 if (findInt(_findParagraphProperty("orphans"), intValue))
1122 propList.insert("fo:orphans", intValue);
1123
1124 if (findInt(_findParagraphProperty("widows"), intValue))
1125 propList.insert("fo:widows", intValue);
1126
1127 librevenge::RVNGPropertyListVector tabStops;
1128 parseTabStops(_findParagraphProperty("tabstops"), tabStops);
1129
1130 if (tabStops.count())
1131 propList.insert("style:tab-stops", tabStops);
1132
1133 sValue = _findParagraphProperty("dom-dir");
1134 if (sValue == "ltr")
1135 propList.insert("style:writing-mode", "lr-tb");
1136 else if (sValue == "rtl")
1137 propList.insert("style:writing-mode", "rl-tb");
1138
1139 if (m_ps->m_deferredPageBreak)
1140 propList.insert("fo:break-before", "page");
1141 else if (m_ps->m_deferredColumnBreak)
1142 propList.insert("fo:break-before", "column");
1143 _addBorderProperties(m_ps->m_currentParagraphStyle, propList);
1144 m_ps->m_deferredPageBreak = false;
1145 m_ps->m_deferredColumnBreak = false;
1146 }
1147
1148 void libabw::ABWContentCollector::_openBlock()
1149 {
1150 if (m_ps->m_isParagraphOpened || m_ps->m_isListElementOpened)
1151 return;
1152 if (m_ps->m_currentListLevel == 0)
1153 _openParagraph();
1154 else
1155 _openListElement();
1156 }
1157
1158 void libabw::ABWContentCollector::_closeBlock()
1159 {
1160 if (!m_ps->m_isParagraphOpened && !m_ps->m_isListElementOpened)
1161 return;
1162 if (m_ps->m_isSpanOpened)
1163 _closeSpan();
1164 if (m_ps->m_isParagraphOpened)
1165 _closeParagraph();
1166 if (m_ps->m_isListElementOpened)
1167 _closeListElement();
1168 }
1169
1170 void libabw::ABWContentCollector::_openParagraph()
1171 {
1172 if (!m_ps->m_isParagraphOpened)
1173 {
1174 switch (m_ps->m_parsingContext)
1175 {
1176 case ABW_HEADER:
1177 if (!m_ps->m_isHeaderOpened)
1178 _openHeader();
1179 break;
1180 case ABW_FOOTER:
1181 if (!m_ps->m_isFooterOpened)
1182 _openFooter();
1183 break;
1184 case ABW_SECTION:
1185 default:
1186 if (!m_ps->m_isSectionOpened)
1187 _openSection();
1188 break;
1189 case ABW_FRAME_IMAGE:
1190 ABW_DEBUG_MSG(("libabw::ABWContentCollector::_openParagraph: can not open a paragraph\n"));
1191 m_ps->m_parsingContext=ABW_FRAME_UNKNOWN;
1192 break;
1193 case ABW_FRAME_TEXTBOX:
1194 case ABW_FRAME_UNKNOWN:
1195 break;
1196 }
1197
1198 if (!m_ps->m_tableStates.empty() && !m_ps->m_tableStates.top().m_isTableCellOpened)
1199 _openTableCell();
1200
1201 _changeList();
1202
1203 librevenge::RVNGPropertyList propList;
1204 _fillParagraphProperties(propList, false);
1205
1206 m_ps->m_deferredPageBreak = false;
1207 m_ps->m_deferredColumnBreak = false;
1208
1209 m_outputElements.addOpenParagraph(propList);
1210
1211 m_ps->m_isParagraphOpened = true;
1212 if (!m_ps->m_tableStates.empty())
1213 m_ps->m_tableStates.top().m_isCellWithoutParagraph = false;
1214 }
1215 }
1216
1217 void libabw::ABWContentCollector::_openListElement()
1218 {
1219 if (!m_ps->m_isListElementOpened)
1220 {
1221 switch (m_ps->m_parsingContext)
1222 {
1223 case ABW_HEADER:
1224 if (!m_ps->m_isHeaderOpened)
1225 _openHeader();
1226 break;
1227 case ABW_FOOTER:
1228 if (!m_ps->m_isFooterOpened)
1229 _openFooter();
1230 break;
1231 case ABW_SECTION:
1232 default:
1233 if (!m_ps->m_isSectionOpened)
1234 _openSection();
1235 break;
1236 case ABW_FRAME_IMAGE:
1237 ABW_DEBUG_MSG(("libabw::ABWContentCollector::_openListElement: can not open a list\n"));
1238 m_ps->m_parsingContext=ABW_FRAME_UNKNOWN;
1239 break;
1240 case ABW_FRAME_TEXTBOX:
1241 case ABW_FRAME_UNKNOWN:
1242 break;
1243 }
1244
1245 if (!m_ps->m_tableStates.empty() && !m_ps->m_tableStates.top().m_isTableCellOpened)
1246 _openTableCell();
1247
1248 _changeList();
1249
1250 librevenge::RVNGPropertyList propList;
1251 _fillParagraphProperties(propList, true);
1252
1253 m_outputElements.addOpenListElement(propList);
1254
1255 m_ps->m_isListElementOpened = true;
1256 if (!m_ps->m_tableStates.empty())
1257 m_ps->m_tableStates.top().m_isCellWithoutParagraph = false;
1258 m_ps->m_isFirstTextInListElement = true;
1259 }
1260 }
1261
1262 void libabw::ABWContentCollector::_openSpan()
1263 {
1264 if (!m_ps->m_isSpanOpened)
1265 {
1266 _openBlock();
1267
1268 librevenge::RVNGPropertyList propList;
1269 ABWUnit unit(ABW_NONE);
1270 double value(0.0);
1271
1272 if (findDouble(_findCharacterProperty("font-size"), value, unit) && unit == ABW_IN)
1273 propList.insert("fo:font-size", value);
1274
1275 std::string sValue = _findCharacterProperty("font-family");
1276 if (!sValue.empty())
1277 propList.insert("style:font-name", sValue.c_str());
1278
1279 sValue = _findCharacterProperty("font-style");
1280 if (!sValue.empty() && sValue != "normal")
1281 propList.insert("fo:font-style", sValue.c_str());
1282
1283 sValue = _findCharacterProperty("font-weight");
1284 if (!sValue.empty() && sValue != "normal")
1285 propList.insert("fo:font-weight", sValue.c_str());
1286
1287 sValue = _findCharacterProperty("display");
1288 if (!sValue.empty() && sValue == "none")
1289 propList.insert("text:display", "none");
1290
1291 sValue = _findCharacterProperty("dir-override");
1292 if (!sValue.empty() && sValue == "rtl")
1293 propList.insert("style:writing-mode", "rl-tb");
1294
1295 sValue = _findCharacterProperty("text-decoration");
1296 std::vector<std::string> listDecorations;
1297 boost::split(listDecorations, sValue, boost::is_any_of(" "), boost::token_compress_on);
1298 for (const auto &decoration : listDecorations)
1299 {
1300 if (decoration == "underline")
1301 {
1302 propList.insert("style:text-underline-type", "single");
1303 propList.insert("style:text-underline-style", "solid");
1304 }
1305 else if (decoration == "line-through")
1306 {
1307 propList.insert("style:text-line-through-type", "single");
1308 propList.insert("style:text-line-through-style", "solid");
1309 }
1310 else if (decoration == "overline")
1311 {
1312 propList.insert("style:text-overline-type", "single");
1313 propList.insert("style:text-overline-style", "solid");
1314 }
1315 }
1316 sValue = getColor(_findCharacterProperty("color"));
1317 if (!sValue.empty())
1318 propList.insert("fo:color", sValue.c_str());
1319
1320 sValue = getColor(_findCharacterProperty("bgcolor"));
1321 if (!sValue.empty())
1322 propList.insert("fo:background-color", sValue.c_str());
1323
1324 sValue = _findCharacterProperty("text-position");
1325 if (sValue == "subscript")
1326 propList.insert("style:text-position", "sub");
1327 else if (sValue == "superscript")
1328 propList.insert("style:text-position", "super");
1329
1330 sValue = _findCharacterProperty("lang");
1331 if (sValue.empty()) // try document default
1332 sValue = _findDocumentProperty("lang");
1333
1334 if (!sValue.empty())
1335 {
1336 optional<std::string> lang;
1337 optional<std::string> country;
1338 optional<std::string> script;
1339
1340 parseLang(sValue, lang, country, script);
1341
1342 if (bool(lang))
1343 propList.insert("fo:language", get(lang).c_str());
1344 if (bool(country))
1345 propList.insert("fo:country", get(country).c_str());
1346 if (bool(script))
1347 propList.insert("fo:script", get(script).c_str());
1348 }
1349
1350 // do we need to check "font-stretch" here or it is always equal to normal ?
1351 m_outputElements.addOpenSpan(propList);
1352 }
1353 m_ps->m_isSpanOpened = true;
1354 }
1355
1356 void libabw::ABWContentCollector::_closeSection()
1357 {
1358 if (m_ps->m_isSectionOpened)
1359 {
1360 while (!m_ps->m_tableStates.empty())
1361 _closeTable();
1362
1363 _closeBlock();
1364 m_ps->m_currentListLevel = 0;
1365 _changeList();
1366
1367 m_outputElements.addCloseSection();
1368
1369 m_ps->m_isSectionOpened = false;
1370 }
1371 }
1372
1373 void libabw::ABWContentCollector::_closeHeader()
1374 {
1375 if (m_ps->m_isHeaderOpened)
1376 {
1377 while (!m_ps->m_tableStates.empty())
1378 _closeTable();
1379
1380 _closeBlock();
1381 m_ps->m_currentListLevel = 0;
1382 _changeList();
1383
1384 m_outputElements.addCloseHeader();
1385
1386 m_ps->m_isHeaderOpened = false;
1387 }
1388 m_ps->m_currentHeaderFooterId = -1;
1389 m_ps->m_currentHeaderFooterOccurrence.clear();
1390 }
1391
1392 void libabw::ABWContentCollector::_closeFooter()
1393 {
1394 if (m_ps->m_isFooterOpened)
1395 {
1396 while (!m_ps->m_tableStates.empty())
1397 _closeTable();
1398
1399 _closeBlock();
1400 m_ps->m_currentListLevel = 0;
1401 _changeList();
1402
1403 m_outputElements.addCloseFooter();
1404
1405 m_ps->m_isFooterOpened = false;
1406 }
1407 m_ps->m_currentHeaderFooterId = -1;
1408 m_ps->m_currentHeaderFooterOccurrence.clear();
1409 }
1410
1411 void libabw::ABWContentCollector::_closeParagraph()
1412 {
1413 if (m_ps->m_isParagraphOpened)
1414 {
1415 if (m_ps->m_isSpanOpened)
1416 _closeSpan();
1417
1418 m_outputElements.addCloseParagraph();
1419 }
1420 m_ps->m_isParagraphOpened = false;
1421 }
1422
1423 void libabw::ABWContentCollector::_closeSpan()
1424 {
1425 if (m_ps->m_isSpanOpened)
1426 m_outputElements.addCloseSpan();
1427
1428 m_ps->m_isSpanOpened = false;
1429 }
1430
1431 void libabw::ABWContentCollector::_openTable()
1432 {
1433 switch (m_ps->m_parsingContext)
1434 {
1435 case ABW_HEADER:
1436 if (!m_ps->m_isHeaderOpened)
1437 _openHeader();
1438 break;
1439 case ABW_FOOTER:
1440 if (!m_ps->m_isFooterOpened)
1441 _openFooter();
1442 break;
1443 case ABW_SECTION:
1444 default:
1445 if (!m_ps->m_isSectionOpened)
1446 _openSection();
1447 break;
1448 case ABW_FRAME_IMAGE:
1449 case ABW_FRAME_TEXTBOX:
1450 case ABW_FRAME_UNKNOWN:
1451 break;
1452 }
1453
1454 librevenge::RVNGPropertyList propList;
1455 if (m_ps->m_deferredPageBreak)
1456 propList.insert("fo:break-before", "page");
1457 else if (m_ps->m_deferredColumnBreak)
1458 propList.insert("fo:break-before", "column");
1459 m_ps->m_deferredPageBreak = false;
1460 m_ps->m_deferredColumnBreak = false;
1461
1462 librevenge::RVNGPropertyListVector tmpColumns;
1463 parseTableColumns(_findTableProperty("table-column-props"), tmpColumns);
1464 auto numColumns = unsigned(tmpColumns.count());
1465 auto iter = m_tableSizes.find(m_ps->m_tableStates.top().m_currentTableId);
1466 if (iter != m_tableSizes.end())
1467 numColumns = unsigned(iter->second);
1468 librevenge::RVNGPropertyListVector columns;
1469 for (unsigned j = 0; j < numColumns; ++j)
1470 {
1471 if (j < tmpColumns.count())
1472 columns.append(tmpColumns[j]);
1473 else
1474 columns.append(librevenge::RVNGPropertyList());
1475 }
1476 if (columns.count())
1477 propList.insert("librevenge:table-columns", columns);
1478
1479 ABWUnit unit(ABW_NONE);
1480 double value(0.0);
1481 if (findDouble(_findTableProperty("table-column-leftpos"), value, unit) && unit == ABW_IN)
1482 {
1483 propList.insert("fo:margin-left", value);
1484 propList.insert("table:align", "margins");
1485 }
1486 else
1487 propList.insert("table:align", "left");
1488
1489 m_outputElements.addOpenTable(propList);
1490
1491 m_ps->m_tableStates.top().m_currentTableRow = (-1);
1492 m_ps->m_tableStates.top().m_currentTableCol = (-1);
1493 m_ps->m_tableStates.top().m_currentTableCellNumberInRow = (-1);
1494 }
1495
1496 void libabw::ABWContentCollector::_closeTable()
1497 {
1498 if (!m_ps->m_tableStates.empty())
1499 {
1500 if (m_ps->m_tableStates.top().m_isTableRowOpened)
1501 _closeTableRow();
1502
1503 m_outputElements.addCloseTable();
1504
1505 m_ps->m_tableStates.pop();
1506 }
1507 }
1508
1509 void libabw::ABWContentCollector::_openTableRow()
1510 {
1511 if (m_ps->m_tableStates.top().m_isTableRowOpened)
1512 _closeTableRow();
1513
1514 m_ps->m_tableStates.top().m_currentTableCol = 0;
1515 m_ps->m_tableStates.top().m_currentTableCellNumberInRow = 0;
1516
1517 m_outputElements.addOpenTableRow(librevenge::RVNGPropertyList());
1518
1519 m_ps->m_tableStates.top().m_isTableRowOpened = true;
1520 m_ps->m_tableStates.top().m_isRowWithoutCell = true;
1521 m_ps->m_tableStates.top().m_currentTableRow++;
1522 }
1523
1524 void libabw::ABWContentCollector::_closeTableRow()
1525 {
1526 if (m_ps->m_tableStates.top().m_isTableRowOpened)
1527 {
1528 if (m_ps->m_tableStates.top().m_isTableCellOpened)
1529 _closeTableCell();
1530
1531 if (m_ps->m_tableStates.top().m_isRowWithoutCell)
1532 {
1533 m_ps->m_tableStates.top().m_isRowWithoutCell = false;
1534 m_outputElements.addInsertCoveredTableCell(librevenge::RVNGPropertyList());
1535 }
1536 m_outputElements.addCloseTableRow();
1537 }
1538 m_ps->m_tableStates.top().m_isTableRowOpened = false;
1539 }
1540
1541 void libabw::ABWContentCollector::_openTableCell()
1542 {
1543 librevenge::RVNGPropertyList propList;
1544 propList.insert("librevenge:column", m_ps->m_tableStates.top().m_currentTableCol);
1545 propList.insert("librevenge:row", m_ps->m_tableStates.top().m_currentTableRow);
1546
1547 int rightAttach(0);
1548 if (findInt(_findCellProperty("right-attach"), rightAttach))
1549 propList.insert("table:number-columns-spanned", rightAttach - m_ps->m_tableStates.top().m_currentTableCol);
1550
1551 int botAttach(0);
1552 if (findInt(_findCellProperty("bot-attach"), botAttach))
1553 propList.insert("table:number-rows-spanned", botAttach - m_ps->m_tableStates.top().m_currentTableRow);
1554
1555 std::string bgColor = getColor(_findCellProperty("background-color"));
1556 if (!bgColor.empty())
1557 propList.insert("fo:background-color", bgColor.c_str());
1558
1559 // by default, table cells have a small border
1560 _addBorderProperties(m_ps->m_tableStates.top().m_currentCellProperties, propList,"0.01in solid #000000");
1561 m_outputElements.addOpenTableCell(propList);
1562
1563 m_ps->m_tableStates.top().m_currentTableCellNumberInRow++;
1564 m_ps->m_tableStates.top().m_isTableCellOpened = true;
1565 m_ps->m_tableStates.top().m_isCellWithoutParagraph = true;
1566 m_ps->m_tableStates.top().m_isRowWithoutCell = false;
1567 }
1568
1569 void libabw::ABWContentCollector::_closeTableCell()
1570 {
1571 if (m_ps->m_tableStates.top().m_isTableCellOpened)
1572 {
1573 if (m_ps->m_tableStates.top().m_isCellWithoutParagraph)
1574 _openSpan();
1575 _closeBlock();
1576 m_ps->m_currentListLevel = 0;
1577 _changeList();
1578
1579 m_outputElements.addCloseTableCell();
1580 }
1581 m_ps->m_tableStates.top().m_isTableCellOpened = false;
1582 }
1583
1584 void libabw::ABWContentCollector::openFoot(const char *id)
1585 {
1586 if (!m_ps->m_isParagraphOpened && !m_ps->m_isListElementOpened)
1587 _openSpan();
1588 _closeSpan();
1589
1590 librevenge::RVNGPropertyList propList;
1591 if (id)
1592 propList.insert("librevenge:number", id);
1593 m_outputElements.addOpenFootnote(propList);
1594
1595 m_parsingStates.push(m_ps);
1596 m_ps = std::make_shared<ABWContentParsingState>();
1597
1598 m_ps->m_isNote = true;
1599 }
1600
1601 void libabw::ABWContentCollector::closeFoot()
1602 {
1603 _closeBlock();
1604 m_ps->m_currentListLevel = 0;
1605 _changeList();
1606
1607 m_outputElements.addCloseFootnote();
1608
1609 if (!m_parsingStates.empty())
1610 {
1611 m_ps = m_parsingStates.top();
1612 m_parsingStates.pop();
1613 }
1614 }
1615
1616 void libabw::ABWContentCollector::openEndnote(const char *id)
1617 {
1618 if (!m_ps->m_isParagraphOpened && !m_ps->m_isListElementOpened)
1619 _openSpan();
1620 _closeSpan();
1621
1622 librevenge::RVNGPropertyList propList;
1623 if (id)
1624 propList.insert("librevenge:number", id);
1625 m_outputElements.addOpenEndnote(propList);
1626
1627 m_parsingStates.push(m_ps);
1628 m_ps = std::make_shared<ABWContentParsingState>();
1629
1630 m_ps->m_isNote = true;
1631 }
1632
1633 void libabw::ABWContentCollector::closeEndnote()
1634 {
1635 _closeBlock();
1636 m_ps->m_currentListLevel = 0;
1637 _changeList();
1638
1639 m_outputElements.addCloseEndnote();
1640
1641 if (!m_parsingStates.empty())
1642 {
1643 m_ps = m_parsingStates.top();
1644 m_parsingStates.pop();
1645 }
1646 }
1647
1648 void libabw::ABWContentCollector::openField(const char *type, const char * /*id*/)
1649 {
1650 if (!type || type[0]==0)
1651 {
1652 ABW_DEBUG_MSG(("libabw::ABWContentCollector::openField: called without type\n"));
1653 return;
1654 }
1655 if (!m_ps->m_inParagraphOrListElement)
1656 return;
1657 librevenge::RVNGPropertyList propList;
1658 std::string typ(type);
1659 size_t len=typ.length();
1660 // see fp_Fields.h
1661 switch (typ[0])
1662 {
1663 case 'a':
1664 if (len>4 && typ.substr(0,4)=="app_")
1665 {
1666 // app_ver, app_compiledate, app_compiletime, app_id, app_target, app_options
1667 return;
1668 }
1669 break;
1670 case 'c':
1671 if (len==10 && typ=="char_count")
1672 propList.insert("librevenge:field-type", "text:character-count");
1673 break;
1674 case 'd':
1675 if (len==4 && typ=="date")
1676 {
1677 propList.insert("librevenge:field-type", "text:date");
1678 propList.insert("number:automatic-order", "true");
1679 librevenge::RVNGPropertyListVector pVect;
1680 _convertFieldDTFormat("%A, %B %d,%Y",pVect);
1681 propList.insert("librevenge:value-type", "date");
1682 propList.insert("librevenge:format", pVect);
1683 break;
1684 }
1685 if (len>5 && typ.substr(0,5)=="date_")
1686 {
1687 propList.insert("librevenge:field-type", "text:date");
1688 propList.insert("number:automatic-order", "true");
1689 librevenge::RVNGPropertyListVector pVect;
1690 if (len==10 && typ=="date_ntdlf") // default
1691 ;
1692 else if (len==11 && typ=="date_mmddyy")
1693 _convertFieldDTFormat("%m/%d/%y",pVect);
1694 else if (len==11 && typ=="date_ddmmyy")
1695 _convertFieldDTFormat("%d/%m/%y",pVect);
1696 else if (len==8 && typ=="date_mdy")
1697 _convertFieldDTFormat("%B %d,%Y",pVect);
1698 else if (len==10 && typ=="date_mthdy")
1699 _convertFieldDTFormat("%b %d,%Y",pVect);
1700 else if (len==8 && typ=="date_dfl")
1701 _convertFieldDTFormat("%a %b %d %H:%M:%S %Y",pVect);
1702 else if (len==10 && typ=="date_wkday")
1703 _convertFieldDTFormat("%A",pVect);
1704 else if (len==8 && typ=="date_doy") // normally this is the day of the year
1705 _convertFieldDTFormat("%d",pVect);
1706 if (!pVect.empty())
1707 {
1708 propList.insert("librevenge:value-type", "date");
1709 propList.insert("librevenge:format", pVect);
1710 }
1711 break;
1712 }
1713 if (len==15 && typ=="datetime_custom") // TODO add format
1714 {
1715 propList.insert("librevenge:field-type", "text:date");
1716 propList.insert("number:automatic-order", "true");
1717 librevenge::RVNGPropertyListVector pVect;
1718 _convertFieldDTFormat("%d/%m/%y %H:%M:%S",pVect);
1719 propList.insert("librevenge:value-type", "date");
1720 propList.insert("librevenge:format", pVect);
1721 break;
1722 }
1723 break;
1724 case 'e':
1725 if (len==12 && typ=="endnote_anch")
1726 return;
1727 if (len==11 && typ=="endnote_ref")
1728 return;
1729 break;
1730 case 'f':
1731 if (len==9 && typ=="file_name")
1732 {
1733 propList.insert("librevenge:field-type", "text:file-name");
1734 propList.insert("text:display", "full");
1735 break;
1736 }
1737 if (len==13 && typ=="footnote_anch")
1738 return;
1739 if (len==12 && typ=="footnote_ref")
1740 return;
1741 break;
1742 case 'l':
1743 if (len==10 && typ=="list_label")
1744 return;
1745 // line_count
1746 break;
1747 case 'm':
1748 if (len>5 && typ.substr(0,5)=="meta_")
1749 {
1750 if (len==10 && typ=="meta_title")
1751 propList.insert("librevenge:field-type", "text:title");
1752 else if (len==12 && typ=="meta_subject")
1753 propList.insert("librevenge:field-type", "text:subject");
1754 else if (len==12 && typ=="meta_creator")
1755 propList.insert("librevenge:field-type", "text:creator");
1756 else if (len==14 && typ=="meta_publisher")
1757 propList.insert("librevenge:field-type", "text:printed-by");
1758 //else if (len==16 && typ=="meta_contributor")
1759 // else if (len==9 && typ=="meta_type")
1760 else if (len==13 && typ=="meta_keywords")
1761 propList.insert("librevenge:field-type", "text:keywords");
1762 //else if (len==13 && typ=="meta_language")
1763 else if (len==16 && typ=="meta_description")
1764 propList.insert("librevenge:field-type", "text:description");
1765 //else if (len==13 && typ=="meta_coverage")
1766 //else if (len==11 && typ=="meta_rights")
1767 else if (len==9 && typ=="meta_date")
1768 propList.insert("librevenge:field-type", "text:creation-date");
1769 else if (len==22 && typ=="meta_date_last_changed")
1770 propList.insert("librevenge:field-type", "text:modification-date");
1771 break;
1772 }
1773 if (len==10 && typ=="mail_merge") // a datafield?
1774 return;
1775 break;
1776 case 'n':
1777 // nbsp_count
1778 break;
1779 case 'p':
1780 if (len>5 && typ.substr(0,5)=="page_")
1781 {
1782 if (len==11 && typ=="page_number")
1783 propList.insert("librevenge:field-type", "text:page-number");
1784 else if (len==10 && typ=="page_count")
1785 propList.insert("librevenge:field-type", "text:page-count");
1786 // page_ref ?
1787 break;
1788 }
1789 if (len>5 && typ.substr(0,5)=="para_")
1790 {
1791 if (len==10 && typ=="para_count")
1792 propList.insert("librevenge:field-type", "text:paragraph-count");
1793 break;
1794 }
1795 break;
1796 case 's':
1797 if (len==15 && typ=="short_file_name")
1798 {
1799 propList.insert("librevenge:field-type", "text:file-name");
1800 propList.insert("text:display", "full"); // checkme
1801 break;
1802 }
1803 if (len>4 && typ.substr(0,4)=="sum_")
1804 {
1805 // sum_cols, sum_rows
1806 break;
1807 }
1808 break;
1809 case 't':
1810 if (len==4 && typ=="time")
1811 {
1812 propList.insert("librevenge:field-type", "text:time");
1813 propList.insert("number:automatic-order", "true");
1814 break;
1815 }
1816 if (len>5 && typ.substr(0,5)=="time_") // TODO add format
1817 {
1818 propList.insert("librevenge:field-type", "text:time");
1819 propList.insert("number:automatic-order", "true");
1820 librevenge::RVNGPropertyListVector pVect;
1821 if (len==9 && typ=="time_ampm")
1822 _convertFieldDTFormat("%I:%M:%S %p",pVect);
1823 if (len==9 && typ=="time_zone") // CEST, ...
1824 return;
1825 // if (len==10 && typ=="time_epoch") //second since ...
1826 if (len==12 && typ=="time_miltime") // ""
1827 return;
1828 if (!pVect.empty())
1829 {
1830 propList.insert("librevenge:value-type", "time");
1831 propList.insert("librevenge:format", pVect);
1832 }
1833 break;
1834 }
1835 if (len>4 && typ.substr(0,4)=="toc_")
1836 {
1837 if (len==14 && typ=="toc_list_label")
1838 return;
1839 // toc_page_number
1840 break;
1841 }
1842 break;
1843 case 'w':
1844 if (len==11 && typ=="word_count")
1845 propList.insert("librevenge:field-type", "text:word-count");
1846 break;
1847 default:
1848 break;
1849 }
1850 if (propList.empty())
1851 {
1852 ABW_DEBUG_MSG(("libabw::ABWContentCollector::openField: sorry, unknown type=%s\n", type));
1853 }
1854 else
1855 {
1856 if (!m_ps->m_isSpanOpened)
1857 _openSpan();
1858 m_outputElements.addInsertField(propList);
1859 m_ps->m_isFirstTextInListElement = false;
1860 }
1861 }
1862
1863 bool libabw::ABWContentCollector::_convertFieldDTFormat(std::string const &dtFormat, librevenge::RVNGPropertyListVector &propVect)
1864 {
1865 propVect.clear();
1866 size_t len=dtFormat.size();
1867 std::string text("");
1868 librevenge::RVNGPropertyList list;
1869 for (size_t c=0; c < len; ++c)
1870 {
1871 if (dtFormat[c]!='%' || c+1==len)
1872 {
1873 text+=dtFormat[c];
1874 continue;
1875 }
1876 char ch=dtFormat[++c];
1877 if (ch=='%')
1878 {
1879 text += '%';
1880 continue;
1881 }
1882 if (!text.empty())
1883 {
1884 list.clear();
1885 list.insert("librevenge:value-type", "text");
1886 list.insert("librevenge:text", text.c_str());
1887 propVect.append(list);
1888 text.clear();
1889 }
1890 list.clear();
1891 switch (ch)
1892 {
1893 case 'Y':
1894 list.insert("number:style", "long");
1895 /* FALLTHRU */
1896 case 'y':
1897 list.insert("librevenge:value-type", "year");
1898 propVect.append(list);
1899 break;
1900 case 'B':
1901 list.insert("number:style", "long");
1902 /* FALLTHRU */
1903 case 'b':
1904 case 'h':
1905 list.insert("librevenge:value-type", "month");
1906 list.insert("number:textual", true);
1907 propVect.append(list);
1908 break;
1909 case 'm':
1910 list.insert("librevenge:value-type", "month");
1911 propVect.append(list);
1912 break;
1913 case 'e':
1914 list.insert("number:style", "long");
1915 /* FALLTHRU */
1916 case 'd':
1917 list.insert("librevenge:value-type", "day");
1918 propVect.append(list);
1919 break;
1920 case 'A':
1921 list.insert("number:style", "long");
1922 /* FALLTHRU */
1923 case 'a':
1924 list.insert("librevenge:value-type", "day-of-week");
1925 propVect.append(list);
1926 break;
1927
1928 case 'H':
1929 list.insert("number:style", "long");
1930 /* FALLTHRU */
1931 case 'I':
1932 list.insert("librevenge:value-type", "hours");
1933 propVect.append(list);
1934 break;
1935 case 'M':
1936 list.insert("librevenge:value-type", "minutes");
1937 list.insert("number:style", "long");
1938 propVect.append(list);
1939 break;
1940 case 'S':
1941 list.insert("librevenge:value-type", "seconds");
1942 list.insert("number:style", "long");
1943 propVect.append(list);
1944 break;
1945 case 'p':
1946 list.clear();
1947 list.insert("librevenge:value-type", "am-pm");
1948 propVect.append(list);
1949 break;
1950 #if !defined(__clang__)
1951 default:
1952 ABW_DEBUG_MSG(("libabw::ABWContentCollector::_convertFieldDTFormat: find unimplement command %c(ignored)\n", ch));
1953 #endif
1954 }
1955 }
1956 if (!text.empty())
1957 {
1958 list.clear();
1959 list.insert("librevenge:value-type", "text");
1960 list.insert("librevenge:text", text.c_str());
1961 propVect.append(list);
1962 }
1963 return propVect.count()!=0;
1964 }
1965
1966 void libabw::ABWContentCollector::closeField()
1967 {
1968 }
1969
1970 void libabw::ABWContentCollector::openTable(const char *props)
1971 {
1972 _closeBlock();
1973 m_ps->m_currentListLevel = 0;
1974 _changeList();
1975
1976 if (m_ps->m_tableStates.empty())
1977 {
1978 switch (m_ps->m_parsingContext)
1979 {
1980 case ABW_HEADER:
1981 if (!m_ps->m_isHeaderOpened)
1982 _openHeader();
1983 break;
1984 case ABW_FOOTER:
1985 if (!m_ps->m_isFooterOpened)
1986 _openFooter();
1987 break;
1988 case ABW_SECTION:
1989 default:
1990 if (!m_ps->m_isSectionOpened)
1991 _openSection();
1992 break;
1993 case ABW_FRAME_IMAGE:
1994 case ABW_FRAME_TEXTBOX:
1995 case ABW_FRAME_UNKNOWN:
1996 break;
1997 }
1998 }
1999
2000 m_ps->m_tableStates.push(ABWContentTableState());
2001 m_ps->m_tableStates.top().m_currentTableId = m_tableCounter++;
2002 if (props)
2003 parsePropString(props, m_ps->m_tableStates.top().m_currentTableProperties);
2004
2005 _openTable();
2006 }
2007
2008 void libabw::ABWContentCollector::closeTable()
2009 {
2010 _closeBlock();
2011 m_ps->m_currentListLevel = 0;
2012 _changeList();
2013 _closeTable();
2014 }
2015
2016 void libabw::ABWContentCollector::openCell(const char *props)
2017 {
2018 if (!m_ps->m_tableStates.empty())
2019 {
2020 if (props)
2021 parsePropString(props, m_ps->m_tableStates.top().m_currentCellProperties);
2022 int currentRow(getCellPos("top-attach", "bottom-attach", m_ps->m_tableStates.top().m_currentTableRow + 1));
2023 if (currentRow > MAX_TABLE_ROW)
2024 currentRow = MAX_TABLE_ROW;
2025
2026 while (m_ps->m_tableStates.top().m_currentTableRow < currentRow)
2027 {
2028 if (m_ps->m_tableStates.top().m_currentTableRow >= 0)
2029 _closeTableRow();
2030 _openTableRow();
2031 }
2032
2033 m_ps->m_tableStates.top().m_currentTableCol =
2034 getCellPos("left-attach", "right-attach", m_ps->m_tableStates.top().m_currentTableCol + 1);
2035 }
2036 }
2037
2038 int libabw::ABWContentCollector::getCellPos(const char *startProp, const char *endProp, int defStart)
2039 {
2040 int startAttach(0);
2041 const bool haveStart(findInt(_findCellProperty(startProp), startAttach));
2042 int endAttach(0);
2043 const bool haveEnd(findInt(_findCellProperty(endProp), endAttach));
2044
2045 int newStartAttach(startAttach);
2046
2047 if (haveStart && haveEnd)
2048 {
2049 if (endAttach <= startAttach && endAttach > 0)
2050 newStartAttach = endAttach - 1;
2051 }
2052 else if (haveStart && !haveEnd)
2053 {
2054 if (startAttach / 1000 > defStart) // likely a damaged input
2055 newStartAttach = defStart;
2056 }
2057 else if (!haveStart && haveEnd)
2058 {
2059 if (endAttach <= 0 || endAttach / 1000 > defStart) // likely a damaged input
2060 newStartAttach = defStart;
2061 else
2062 newStartAttach = endAttach - 1;
2063 }
2064 else
2065 {
2066 newStartAttach = defStart;
2067 }
2068
2069 return newStartAttach;
2070 }
2071
2072 void libabw::ABWContentCollector::closeCell()
2073 {
2074 if (!m_ps->m_tableStates.empty())
2075 {
2076 _closeTableCell();
2077 m_ps->m_tableStates.top().m_currentCellProperties.clear();
2078 }
2079 }
2080
2081 void libabw::ABWContentCollector::openFrame(const char *props, const char *imageId, const char */*title*/, const char */*alt*/)
2082 {
2083 ABWPropertyMap propMap;
2084 if (props)
2085 parsePropString(props, propMap);
2086 ABWPropertyMap::const_iterator iter;
2087
2088 librevenge::RVNGPropertyList propList;
2089 ABWUnit unit(ABW_NONE);
2090 double value(0.0);
2091 // size
2092 iter = propMap.find("frame-height");
2093 if (iter != propMap.end() && findDouble(iter->second, value, unit) && ABW_IN == unit)
2094 propList.insert("svg:height", value);
2095 iter = propMap.find("frame-width");
2096 if (iter != propMap.end() && findDouble(iter->second, value, unit) && ABW_IN == unit)
2097 propList.insert("svg:width", value);
2098 // position
2099 bool isParagraph=true;
2100 iter = propMap.find("position-to");
2101 if (iter != propMap.end())
2102 {
2103 if (iter->second=="page-above-text")
2104 isParagraph=false;
2105 else if (iter->second=="column-above-text")
2106 /* unsure how to retrieve that, so check if the page positions
2107 are defined, if yes, use a page anchor. */
2108 isParagraph=(propMap.find("frame-page-ypos")==propMap.end());
2109 else if (iter->second!="block-above-text")
2110 {
2111 ABW_DEBUG_MSG(("libabw::ABWContentCollector::openFrame: sorry, unknown pos: %s asume paragraph\n", iter->second.c_str()));
2112 }
2113 }
2114 iter = propMap.find(isParagraph ? "xpos" : "frame-page-xpos");
2115 if (iter != propMap.end() && findDouble(iter->second, value, unit) && ABW_IN == unit)
2116 propList.insert("svg:x", value);
2117 iter = propMap.find(isParagraph ? "ypos" : "frame-page-ypos");
2118 if (iter != propMap.end() && findDouble(iter->second, value, unit) && ABW_IN == unit)
2119 propList.insert("svg:y", value);
2120 if (!isParagraph)
2121 {
2122 propList.insert("style:vertical-rel", "page");
2123 propList.insert("style:horizontal-rel", "page");
2124 }
2125 if (!isParagraph)
2126 {
2127 iter = propMap.find("frame-pref-page");
2128 int page=0;
2129 if (iter != propMap.end() && findInt(iter->second, page))
2130 propList.insert("text:anchor-page-number", page+1);
2131 }
2132 // style
2133 int intValue;
2134 iter = propMap.find("bg-style"); // 0: none, 1: color=background-color
2135 if (iter != propMap.end() && findInt(iter->second, intValue) && intValue==1)
2136 {
2137 iter = propMap.find("background-color");
2138 if (iter != propMap.end())
2139 {
2140 std::string color("#");
2141 color+=iter->second;
2142 propList.insert("fo:background-color", color.c_str());
2143 }
2144 }
2145 propList.insert("text:anchor-type", isParagraph ? "paragraph" : "page");
2146 iter = propMap.find("wrap-mode");
2147 if (iter != propMap.end())
2148 {
2149 if (iter->second=="wrapped-to-left")
2150 propList.insert("style:wrap", "left");
2151 else if (iter->second=="wrapped-to-right")
2152 propList.insert("style:wrap", "right");
2153 else if (iter->second=="wrapped-to-both")
2154 propList.insert("style:wrap", "parallel");
2155 else if (iter->second=="above-text")
2156 {
2157 propList.insert("style:wrap", "dynamic");
2158 propList.insert("style:run-through", "foreground");
2159 }
2160 else if (iter->second=="below-text")
2161 {
2162 propList.insert("style:wrap", "dynamic");
2163 propList.insert("style:run-through", "background");
2164 }
2165 else
2166 {
2167 ABW_DEBUG_MSG(("libabw::ABWContentCollector::openFrame: sorry, unknown wrap mode: %s\n", iter->second.c_str()));
2168 }
2169 }
2170 m_ps->m_isPageFrame=!isParagraph;
2171 m_outputElements.addOpenFrame(propList);
2172
2173 iter = propMap.find("frame-type");
2174 if (iter==propMap.end())
2175 {
2176 ABW_DEBUG_MSG(("libabw::ABWContentCollector::openFrame: can not find the frame type\n"));
2177 }
2178 else if (iter->second=="image")
2179 {
2180 m_ps->m_parsingContext=ABW_FRAME_IMAGE;
2181 auto imIter = m_data.end();
2182 if (imageId) imIter= m_data.find(imageId);
2183 if (imIter==m_data.end())
2184 {
2185 ABW_DEBUG_MSG(("libabw::ABWContentCollector::openFrame: can not find the image\n"));
2186 return;
2187 }
2188
2189 propList.clear();
2190 propList.insert("librevenge:mime-type", imIter->second.m_mimeType);
2191 propList.insert("office:binary-data", imIter->second.m_binaryData);
2192 m_outputElements.addInsertBinaryObject(propList);
2193
2194 return;
2195 }
2196 else if (iter->second=="textbox")
2197 {
2198 m_ps->m_parsingContext=ABW_FRAME_TEXTBOX;
2199 propList.clear();
2200 m_outputElements.addOpenTextBox(propList);
2201 return;
2202 }
2203 else
2204 {
2205 m_ps->m_parsingContext=ABW_FRAME_UNKNOWN;
2206 ABW_DEBUG_MSG(("libabw::ABWContentCollector::openFrame: sorry, unknown frame type: %s\n", iter->second.c_str()));
2207 }
2208 }
2209
2210 void libabw::ABWContentCollector::closeFrame(libabw::ABWOutputElements *(&elements), bool &pageFrame)
2211 {
2212 elements=nullptr;
2213 pageFrame=false;
2214 if (m_ps->m_isNote)
2215 {
2216 ABW_DEBUG_MSG(("libabw::ABWContentCollector::closeFrame: sorry, oops, sorry, a note is not closed\n"));
2217 return;
2218 }
2219 if (m_ps->m_parsingContext!=ABW_FRAME_IMAGE && m_ps->m_parsingContext!=ABW_FRAME_TEXTBOX)
2220 return;
2221
2222 while (!m_ps->m_tableStates.empty())
2223 _closeTable();
2224 _closeBlock();
2225 m_ps->m_currentListLevel = 0;
2226 _changeList(); // flush the list
2227
2228 if (m_ps->m_parsingContext==ABW_FRAME_TEXTBOX)
2229 m_outputElements.addCloseTextBox();
2230 m_outputElements.addCloseFrame();
2231 elements=&m_outputElements;
2232 pageFrame=m_ps->m_isPageFrame;
2233 }
2234
2235 void libabw::ABWContentCollector::addFrameElements(ABWOutputElements &elements, bool pageFrame)
2236 {
2237 if (pageFrame)
2238 m_pageOutputElements.splice(elements);
2239 else
2240 {
2241 _openBlock();
2242 m_outputElements.splice(elements);
2243 }
2244 }
2245
2246 void libabw::ABWContentCollector::collectData(const char *, const char *, const librevenge::RVNGBinaryData &)
2247 {
2248 }
2249
2250 void libabw::ABWContentCollector::insertImage(const char *dataid, const char *props)
2251 {
2252 if (!m_ps->m_isSpanOpened)
2253 _openSpan();
2254
2255 ABWPropertyMap properties;
2256 if (props)
2257 parsePropString(props, properties);
2258 if (dataid)
2259 {
2260 auto iter = m_data.find(dataid);
2261 if (iter != m_data.end())
2262 {
2263 librevenge::RVNGPropertyList propList;
2264 ABWUnit unit(ABW_NONE);
2265 double value(0.0);
2266 ABWPropertyMap::const_iterator i = properties.find("height");
2267 if (i != properties.end() && findDouble(i->second, value, unit) && ABW_IN == unit)
2268 propList.insert("svg:height", value);
2269 else
2270 propList.insert("fo:min-height", 1.0);
2271 i = properties.find("width");
2272 if (i != properties.end() && findDouble(i->second, value, unit) && ABW_IN == unit)
2273 propList.insert("svg:width", value);
2274 else
2275 propList.insert("fo:min-width", 1.0);
2276 propList.insert("text:anchor-type", "as-char");
2277
2278 m_outputElements.addOpenFrame(propList);
2279
2280 propList.clear();
2281 propList.insert("librevenge:mime-type", iter->second.m_mimeType);
2282 propList.insert("office:binary-data", iter->second.m_binaryData);
2283 m_outputElements.addInsertBinaryObject(propList);
2284
2285 m_outputElements.addCloseFrame();
2286 }
2287 }
2288 }
2289
2290 void libabw::ABWContentCollector::_handleListChange()
2291 {
2292 int oldListLevel;
2293 if (m_ps->m_listLevels.empty())
2294 oldListLevel = 0;
2295 else
2296 oldListLevel = m_ps->m_listLevels.top().first;
2297
2298 if (m_ps->m_currentListLevel > oldListLevel)
2299 {
2300 if (!m_ps->m_isSectionOpened)
2301 _openSection();
2302 _recurseListLevels(oldListLevel, m_ps->m_currentListLevel, m_ps->m_currentListId);
2303 }
2304 else if (m_ps->m_currentListLevel < oldListLevel)
2305 {
2306 while (!m_ps->m_listLevels.empty() && m_ps->m_listLevels.top().first > m_ps->m_currentListLevel)
2307 {
2308 if (!m_ps->m_listLevels.top().second || m_ps->m_listLevels.top().second->getType() == ABW_UNORDERED)
2309 m_outputElements.addCloseUnorderedListLevel();
2310 else
2311 m_outputElements.addCloseOrderedListLevel();
2312 ABW_DEBUG_MSG(("Popped level %i off the list level stack\n", m_ps->m_listLevels.top().first));
2313 m_ps->m_listLevels.pop();
2314 }
2315 }
2316 }
2317
2318 void libabw::ABWContentCollector::_writeOutDummyListLevels(int oldLevel, int newLevel)
2319 {
2320 if (oldLevel < newLevel)
2321 {
2322 _writeOutDummyListLevels(oldLevel, newLevel-1);
2323 m_dummyListElements.push_back(std::make_shared<ABWUnorderedListElement>());
2324 m_dummyListElements.back()->m_listLevel = newLevel;
2325 m_ps->m_listLevels.push(std::make_pair(newLevel, m_dummyListElements.back()));
2326 librevenge::RVNGPropertyList propList;
2327 m_dummyListElements.back()->writeOut(propList);
2328 m_outputElements.addOpenUnorderedListLevel(propList);
2329 }
2330 }
2331
2332 void libabw::ABWContentCollector::_recurseListLevels(int oldLevel, int newLevel, int newListId)
2333 {
2334 if (oldLevel >= newLevel)
2335 return;
2336 const auto iter = m_listElements.find(newListId);
2337 if (iter != m_listElements.end() && iter->second)
2338 {
2339 if (iter->second->m_parentId)
2340 _recurseListLevels(oldLevel, newLevel-1, iter->second->m_parentId);
2341 else
2342 _writeOutDummyListLevels(oldLevel, newLevel-1);
2343 m_ps->m_listLevels.push(std::make_pair(newLevel, iter->second));
2344 librevenge::RVNGPropertyList propList;
2345 iter->second->writeOut(propList);
2346 // osnola: use the element list id if set, if not use newListId
2347 propList.insert("librevenge:list-id",
2348 iter->second->m_listId ? iter->second->m_listId : newListId);
2349 if (iter->second->getType() == ABW_UNORDERED)
2350 m_outputElements.addOpenUnorderedListLevel(propList);
2351 else
2352 m_outputElements.addOpenOrderedListLevel(propList);
2353 }
2354 }
2355
2356 void libabw::ABWContentCollector::_changeList()
2357 {
2358 _closeBlock();
2359 _handleListChange();
2360 }
2361
2362 void libabw::ABWContentCollector::_closeListElement()
2363 {
2364 if (m_ps->m_isListElementOpened)
2365 {
2366 if (m_ps->m_isSpanOpened)
2367 _closeSpan();
2368
2369 m_outputElements.addCloseListElement();
2370 }
2371 m_ps->m_isListElementOpened = false;
2372 m_ps->m_isFirstTextInListElement = false;
2373 }
2374
2375 void libabw::ABWContentCollector::addMetadataEntry(const char *const key, const char *const value)
2376 {
2377 assert(key);
2378 assert(value);
2379
2380 m_metadata[key] = value;
2381 }
2382
2383 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */