Debian Codegraph @

src/lib/MWAWParagraph.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <map>
35
36 #include <librevenge/librevenge.h>
37
38 #include "libmwaw_internal.hxx"
39
40 #include "MWAWList.hxx"
41 #include "MWAWPosition.hxx"
42
43 #include "MWAWParagraph.hxx"
44
45 ////////////////////////////////////////////////////////////
46 // tabstop
47 ////////////////////////////////////////////////////////////
48 void MWAWTabStop::addTo(librevenge::RVNGPropertyListVector &propList, double decalX) const
49 {
50 librevenge::RVNGPropertyList tab;
51
52 // type
53 switch (m_alignment) {
54 case RIGHT:
55 tab.insert("style:type", "right");
56 break;
57 case CENTER:
58 tab.insert("style:type", "center");
59 break;
60 case DECIMAL:
61 tab.insert("style:type", "char");
62 if (m_decimalCharacter) {
63 librevenge::RVNGString sDecimal;
64 libmwaw::appendUnicode(m_decimalCharacter, sDecimal);
65 tab.insert("style:char", sDecimal);
66 }
67 else
68 tab.insert("style:char", ".");
69 break;
70 case LEFT:
71 case BAR: // BAR is not handled in OO
72 #if !defined(__clang__)
73 default:
74 #endif
75 break;
76 }
77
78 // leader character
79 if (m_leaderCharacter != 0x0000) {
80 librevenge::RVNGString sLeader;
81 libmwaw::appendUnicode(m_leaderCharacter, sLeader);
82 tab.insert("style:leader-text", sLeader);
83 tab.insert("style:leader-style", "solid");
84 }
85
86 // position
87 double position = m_position+decalX;
88 if (position < 0.00005 && position > -0.00005)
89 position = 0.0;
90 tab.insert("style:position", position, librevenge::RVNG_INCH);
91
92 propList.append(tab);
93 }
94
95 int MWAWTabStop::cmp(MWAWTabStop const &tabs) const
96 {
97 if (m_position < tabs.m_position) return -1;
98 if (m_position > tabs.m_position) return 1;
99 if (m_alignment < tabs.m_alignment) return -1;
100 if (m_alignment > tabs.m_alignment) return 1;
101 if (m_leaderCharacter < tabs.m_leaderCharacter) return -1;
102 if (m_leaderCharacter > tabs.m_leaderCharacter) return 1;
103 if (m_decimalCharacter < tabs.m_decimalCharacter) return -1;
104 if (m_decimalCharacter > tabs.m_decimalCharacter) return 1;
105 return 0;
106 }
107
108 // operator<<
109 std::ostream &operator<<(std::ostream &o, MWAWTabStop const &tab)
110 {
111 o << tab.m_position;
112
113 switch (tab.m_alignment) {
114 case MWAWTabStop::LEFT:
115 o << "L";
116 break;
117 case MWAWTabStop::CENTER:
118 o << "C";
119 break;
120 case MWAWTabStop::RIGHT:
121 o << "R";
122 break;
123 case MWAWTabStop::DECIMAL:
124 o << ":decimal";
125 break;
126 case MWAWTabStop::BAR:
127 o << ":bar";
128 break;
129 #if !defined(__clang__)
130 default:
131 o << ":#type=" << int(tab.m_alignment);
132 break;
133 #endif
134 }
135 if (tab.m_leaderCharacter != '\0')
136 o << ":sep='"<< char(tab.m_leaderCharacter) << "'";
137 if (tab.m_decimalCharacter && tab.m_decimalCharacter != '.')
138 o << ":dec='" << char(tab.m_decimalCharacter) << "'";
139 return o;
140 }
141
142 ////////////////////////////////////////////////////////////
143 // paragraph
144 ////////////////////////////////////////////////////////////
145 MWAWParagraph::MWAWParagraph()
146 : m_marginsUnit(librevenge::RVNG_INCH)
147 , m_spacingsInterlineUnit(librevenge::RVNG_PERCENT)
148 , m_spacingsInterlineType(Fixed)
149 , m_tabs()
150 , m_tabsRelativeToLeftMargin(false)
151 , m_justify(JustificationLeft)
152 , m_breakStatus(0)
153 , m_writingMode(libmwaw::WritingInherited)
154 , m_listLevelIndex(0)
155 , m_listId(-1)
156 , m_listStartValue(-1)
157 , m_listLevel()
158 , m_backgroundColor(MWAWColor::white())
159 , m_borders()
160
161 , m_dropNumCharacters(0)
162 , m_dropNumLines(0)
163
164 , m_styleName("")
165 , m_extra("")
166 {
167 for (auto &margin : m_margins) {
168 margin=0;
169 margin.setSet(false);
170 }
171 for (auto &spacing : m_spacings) {
172 spacing=0;
173 spacing.setSet(false);
174 }
175 m_spacings[0] = 1.0; // interline normal
176 m_spacings[0].setSet(false);
177 }
178
179 MWAWParagraph::~MWAWParagraph()
180 {
181 }
182
183 int MWAWParagraph::cmp(MWAWParagraph const &para) const
184 {
185 for (int i = 0; i < 3; i++) {
186 if (*(m_margins[i]) < *(para.m_margins[i])) return -1;
187 if (*(m_margins[i]) > *(para.m_margins[i])) return 1;
188 if (*(m_spacings[i]) < *(para.m_spacings[i])) return -1;
189 if (*(m_spacings[i]) > *(para.m_spacings[i])) return 1;
190 }
191 if (*m_justify < *para.m_justify) return -1;
192 if (*m_justify > *para.m_justify) return 1;
193 if (*m_marginsUnit < *para.m_marginsUnit) return -1;
194 if (*m_marginsUnit > *para.m_marginsUnit) return 1;
195 if (*m_spacingsInterlineUnit < *para.m_spacingsInterlineUnit) return -1;
196 if (*m_spacingsInterlineUnit > *para.m_spacingsInterlineUnit) return 1;
197 if (*m_spacingsInterlineType < *para.m_spacingsInterlineType) return -1;
198 if (*m_spacingsInterlineType > *para.m_spacingsInterlineType) return 1;
199 if (*m_tabsRelativeToLeftMargin < *para.m_tabsRelativeToLeftMargin) return -1;
200 if (*m_tabsRelativeToLeftMargin > *para.m_tabsRelativeToLeftMargin) return 1;
201
202 if (m_tabs->size() < para.m_tabs->size()) return -1;
203 if (m_tabs->size() > para.m_tabs->size()) return 1;
204
205 for (size_t i=0; i < m_tabs->size(); i++) {
206 int diff=(*m_tabs)[i].cmp((*para.m_tabs)[i]);
207 if (diff) return diff;
208 }
209 if (*m_breakStatus < *para.m_breakStatus) return -1;
210 if (*m_breakStatus > *para.m_breakStatus) return 1;
211 if (*m_writingMode < *para.m_writingMode) return -1;
212 if (*m_writingMode > *para.m_writingMode) return 1;
213 if (*m_listLevelIndex < *para.m_listLevelIndex) return -1;
214 if (*m_listLevelIndex > *para.m_listLevelIndex) return 1;
215 if (*m_listId < *para.m_listId) return -1;
216 if (*m_listId > *para.m_listId) return 1;
217 if (*m_listStartValue < *para.m_listStartValue) return -1;
218 if (*m_listStartValue > *para.m_listStartValue) return 1;
219 int diff=m_listLevel->cmp(*para.m_listLevel);
220 if (diff) return diff;
221 if (*m_backgroundColor < *para.m_backgroundColor) return -1;
222 if (*m_backgroundColor > *para.m_backgroundColor) return 1;
223
224 if (m_borders.size() < para.m_borders.size()) return -1;
225 if (m_borders.size() > para.m_borders.size()) return 1;
226 for (size_t i=0; i < m_borders.size(); i++) {
227 if (m_borders[i].isSet() != para.m_borders[i].isSet())
228 return m_borders[i].isSet() ? 1 : -1;
229 diff = m_borders[i]->compare(*(para.m_borders[i]));
230 if (diff) return diff;
231 }
232
233 if (*m_dropNumCharacters < *para.m_dropNumCharacters) return -1;
234 if (*m_dropNumCharacters > *para.m_dropNumCharacters) return 1;
235 if (*m_dropNumLines < *para.m_dropNumLines) return -1;
236 if (*m_dropNumLines > *para.m_dropNumLines) return 1;
237 if (m_styleName<para.m_styleName) return -1;
238 if (m_styleName>para.m_styleName) return 1;
239 return 0;
240 }
241
242 void MWAWParagraph::insert(MWAWParagraph const &para)
243 {
244 for (int i = 0; i < 3; i++) {
245 m_margins[i].insert(para.m_margins[i]);
246 m_spacings[i].insert(para.m_spacings[i]);
247 }
248 m_marginsUnit.insert(para.m_marginsUnit);
249 m_spacingsInterlineUnit.insert(para.m_spacingsInterlineUnit);
250 m_spacingsInterlineType.insert(para.m_spacingsInterlineType);
251 if (para.m_tabs.isSet() && m_tabs.isSet()) {
252 std::map<double, MWAWTabStop> all;
253 for (size_t t=0; t <m_tabs->size(); ++t)
254 all[(*m_tabs)[t].m_position]=(*m_tabs)[t];
255 for (size_t t=0; t <para.m_tabs->size(); ++t)
256 all[(*para.m_tabs)[t].m_position]=(*para.m_tabs)[t];
257
258 m_tabs->resize(0);
259 for (auto const &it : all)
260 m_tabs->push_back(it.second);
261 }
262 else if (para.m_tabs.isSet())
263 m_tabs=para.m_tabs;
264 m_tabsRelativeToLeftMargin.insert(para.m_tabsRelativeToLeftMargin);
265 m_justify.insert(para.m_justify);
266 m_breakStatus.insert(para.m_breakStatus);
267 m_writingMode.insert(para.m_writingMode);
268 m_listLevelIndex.insert(para.m_listLevelIndex);
269 m_listId.insert(para.m_listId);
270 m_listStartValue.insert(para.m_listStartValue);
271 m_listLevel.insert(para.m_listLevel);
272 m_backgroundColor.insert(para.m_backgroundColor);
273 if (m_borders.size() < para.m_borders.size())
274 m_borders.resize(para.m_borders.size());
275 for (size_t i = 0; i < para.m_borders.size(); i++)
276 m_borders[i].insert(para.m_borders[i]);
277 m_dropNumCharacters.insert(para.m_dropNumCharacters);
278 m_dropNumLines.insert(para.m_dropNumLines);
279 m_styleName=para.m_styleName; // checkme
280 m_extra += para.m_extra;
281 }
282
283 bool MWAWParagraph::hasBorders() const
284 {
285 for (size_t i = 0; i < m_borders.size() && i < 4; i++) {
286 if (!m_borders[i].isSet())
287 continue;
288 if (!m_borders[i]->isEmpty())
289 return true;
290 }
291 return false;
292 }
293
294 bool MWAWParagraph::hasDifferentBorders() const
295 {
296 if (!hasBorders()) return false;
297 if (m_borders.size() < 4) return true;
298 for (size_t i = 1; i < m_borders.size(); i++) {
299 if (m_borders[i].isSet() != m_borders[0].isSet())
300 return true;
301 if (*(m_borders[i]) != *(m_borders[0]))
302 return true;
303 }
304 return false;
305 }
306
307 double MWAWParagraph::getMarginsWidth() const
308 {
309 auto factor = double(MWAWPosition::getScaleFactor(*m_marginsUnit, librevenge::RVNG_INCH));
310 return factor*(*(m_margins[1])+*(m_margins[2]));
311 }
312
313 void MWAWParagraph::addTo(librevenge::RVNGPropertyList &propList, bool inTable) const
314 {
315 switch (*m_justify) {
316 case JustificationLeft:
317 // doesn't require a paragraph prop - it is the default
318 propList.insert("fo:text-align", "left");
319 break;
320 case JustificationCenter:
321 propList.insert("fo:text-align", "center");
322 break;
323 case JustificationRight:
324 propList.insert("fo:text-align", "end");
325 break;
326 case JustificationFull:
327 propList.insert("fo:text-align", "justify");
328 break;
329 case JustificationFullAllLines:
330 propList.insert("fo:text-align", "justify");
331 propList.insert("fo:text-align-last", "justify");
332 break;
333 #if !defined(__clang__)
334 default:
335 break;
336 #endif
337 }
338 if (!inTable) {
339 propList.insert("fo:margin-left", *m_margins[1], *m_marginsUnit);
340 propList.insert("fo:text-indent", *m_margins[0], *m_marginsUnit);
341 propList.insert("fo:margin-right", *m_margins[2], *m_marginsUnit);
342 if (!m_styleName.empty())
343 propList.insert("style:display-name", m_styleName.c_str());
344 if (!m_backgroundColor->isWhite())
345 propList.insert("fo:background-color", m_backgroundColor->str().c_str());
346 if (hasBorders()) {
347 bool setAll = !hasDifferentBorders();
348 for (size_t w = 0; w < m_borders.size() && w < 4; w++) {
349 if (w && setAll)
350 break;
351 if (!m_borders[w].isSet())
352 continue;
353 MWAWBorder const &border = *(m_borders[w]);
354 if (border.isEmpty())
355 continue;
356 if (setAll) {
357 border.addTo(propList);
358 break;
359 }
360 switch (w) {
361 case libmwaw::Left:
362 border.addTo(propList,"left");
363 break;
364 case libmwaw::Right:
365 border.addTo(propList,"right");
366 break;
367 case libmwaw::Top:
368 border.addTo(propList,"top");
369 break;
370 case libmwaw::Bottom:
371 border.addTo(propList,"bottom");
372 break;
373 #if !defined(__clang__)
374 default:
375 MWAW_DEBUG_MSG(("MWAWParagraph::addTo: can not send %d border\n",int(w)));
376 break;
377 #endif
378 }
379 }
380 }
381 }
382 propList.insert("fo:margin-top", *(m_spacings[1]), librevenge::RVNG_INCH);
383 propList.insert("fo:margin-bottom", *(m_spacings[2]), librevenge::RVNG_INCH);
384 switch (*m_spacingsInterlineType) {
385 case Fixed:
386 propList.insert("fo:line-height", *(m_spacings[0]), *m_spacingsInterlineUnit);
387 break;
388 case AtLeast:
389 if (*(m_spacings[0]) <= 0 && *(m_spacings[0]) >= 0)
390 break;
391 if (*(m_spacings[0]) < 0) {
392 static bool first = true;
393 if (first) {
394 MWAW_DEBUG_MSG(("MWAWParagraph::addTo: interline spacing seems bad\n"));
395 first = false;
396 }
397 }
398 else if (*m_spacingsInterlineUnit != librevenge::RVNG_PERCENT)
399 propList.insert("style:line-height-at-least", *(m_spacings[0]), *m_spacingsInterlineUnit);
400 else {
401 propList.insert("style:line-height-at-least", *(m_spacings[0])*12.0, librevenge::RVNG_POINT);
402 static bool first = true;
403 if (first) {
404 first = false;
405 MWAW_DEBUG_MSG(("MWAWParagraph::addTo: assume height=12 to set line spacing at least with percent type\n"));
406 }
407 }
408 break;
409 #if !defined(__clang__)
410 default:
411 MWAW_DEBUG_MSG(("MWAWParagraph::addTo: can not set line spacing type: %d\n",int(*m_spacingsInterlineType)));
412 break;
413 #endif
414 }
415 if (*m_breakStatus & NoBreakBit)
416 propList.insert("fo:keep-together", "always");
417 if (*m_breakStatus & NoBreakWithNextBit)
418 propList.insert("fo:keep-with-next", "always");
419 if (*m_writingMode!=libmwaw::WritingInherited)
420 propList.insert("style:writing-mode", libmwaw::writingModeToString(*m_writingMode).c_str());
421
422 if (!m_tabs->empty()) {
423 double decalX=0;
424 librevenge::RVNGPropertyListVector tabs;
425 if (!*m_tabsRelativeToLeftMargin) {
426 // tabs are absolute, we must remove left margin
427 auto factor = double(MWAWPosition::getScaleFactor(*m_marginsUnit, librevenge::RVNG_INCH));
428 decalX -= m_margins[1].get()*factor;
429 }
430
431 for (auto const &tab : *m_tabs)
432 tab.addTo(tabs, decalX);
433 propList.insert("style:tab-stops", tabs);
434 }
435
436 if (m_dropNumCharacters.get()>0 && m_dropNumLines.get()>1) {
437 librevenge::RVNGPropertyList cap;
438 //addme cap.insert("style:distance", )
439 cap.insert("style:length", *m_dropNumCharacters);
440 cap.insert("style:lines", *m_dropNumLines);
441 librevenge::RVNGPropertyListVector capVector;
442 capVector.append(cap);
443 propList.insert("style:drop-cap", capVector);
444 }
445 }
446
447 std::ostream &operator<<(std::ostream &o, MWAWParagraph const &pp)
448 {
449 if (!pp.m_styleName.empty())
450 o << "style=\"" << pp.m_styleName << "\",";
451 if (pp.m_margins[0].get()<0||pp.m_margins[0].get()>0)
452 o << "textIndent=" << pp.m_margins[0].get() << ",";
453 if (pp.m_margins[1].get()<0||pp.m_margins[1].get()>0)
454 o << "leftMarg=" << pp.m_margins[1].get() << ",";
455 if (pp.m_margins[2].get()<0||pp.m_margins[2].get()>0)
456 o << "rightMarg=" << pp.m_margins[2].get() << ",";
457
458 if (pp.m_spacingsInterlineUnit.get()==librevenge::RVNG_PERCENT) {
459 if (pp.m_spacings[0].get() < 1.0 || pp.m_spacings[0].get() > 1.0) {
460 o << "interLineSpacing=" << pp.m_spacings[0].get() << "%";
461 if (pp.m_spacingsInterlineType.get()==MWAWParagraph::AtLeast)
462 o << "[atLeast]";
463 o << ",";
464 }
465 }
466 else if (pp.m_spacings[0].get() > 0.0) {
467 o << "interLineSpacing=" << pp.m_spacings[0].get();
468 if (pp.m_spacingsInterlineType.get()==MWAWParagraph::AtLeast)
469 o << "[atLeast]";
470 o << ",";
471 }
472 if (pp.m_spacings[1].get()<0||pp.m_spacings[1].get()>0)
473 o << "befSpacing=" << pp.m_spacings[1].get() << ",";
474 if (pp.m_spacings[2].get()<0||pp.m_spacings[2].get()>0)
475 o << "aftSpacing=" << pp.m_spacings[2].get() << ",";
476
477 if (pp.m_breakStatus.get() & MWAWParagraph::NoBreakBit) o << "dontbreak,";
478 if (pp.m_breakStatus.get() & MWAWParagraph::NoBreakWithNextBit) o << "dontbreakafter,";
479 if (pp.m_writingMode.get()!=libmwaw::WritingInherited)
480 o << "writing=" << libmwaw::writingModeToString(pp.m_writingMode.get()) << ",";
481
482 switch (pp.m_justify.get()) {
483 case MWAWParagraph::JustificationLeft:
484 break;
485 case MWAWParagraph::JustificationCenter:
486 o << "just=centered, ";
487 break;
488 case MWAWParagraph::JustificationRight:
489 o << "just=right, ";
490 break;
491 case MWAWParagraph::JustificationFull:
492 o << "just=full, ";
493 break;
494 case MWAWParagraph::JustificationFullAllLines:
495 o << "just=fullAllLines, ";
496 break;
497 #if !defined(__clang__)
498 default:
499 o << "just=" << pp.m_justify.get() << ", ";
500 break;
501 #endif
502 }
503
504 if (pp.m_tabs->size()) {
505 o << "tabs=(";
506 for (auto const &tab : *pp.m_tabs)
507 o << tab << ",";
508 o << "),";
509 }
510 if (!pp.m_backgroundColor.get().isWhite())
511 o << "backgroundColor=" << pp.m_backgroundColor.get() << ",";
512 if (*pp.m_listId >= 0) o << "listId=" << *pp.m_listId << ",";
513 if (pp.m_listLevelIndex.get() >= 1)
514 o << pp.m_listLevel.get() << ":" << pp.m_listLevelIndex.get() <<",";
515
516 for (size_t i = 0; i < pp.m_borders.size(); i++) {
517 if (!pp.m_borders[i].isSet())
518 continue;
519 MWAWBorder const &border = pp.m_borders[i].get();
520 if (border.isEmpty())
521 continue;
522 o << "bord";
523 if (i < 6) {
524 static char const *wh[] = { "L", "R", "T", "B", "MiddleH", "MiddleV" };
525 o << wh[i];
526 }
527 else o << "[#wh=" << i << "]";
528 o << "=" << border << ",";
529 }
530
531 if (pp.m_dropNumCharacters.get()>0 && pp.m_dropNumLines.get()>1)
532 o << "drop=" << *pp.m_dropNumCharacters << "[" << *pp.m_dropNumLines << "l],";
533 if (!pp.m_extra.empty()) o << "extras=(" << pp.m_extra << ")";
534 return o;
535 }
536
537 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: