Debian Codegraph @

src/lib/MWAWList.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 <cstring>
35 #include <iostream>
36
37 #include <librevenge/librevenge.h>
38
39 #include "libmwaw_internal.hxx"
40
41 #include "MWAWFont.hxx"
42 #include "MWAWList.hxx"
43
44 ////////////////////////////////////////////////////////////
45 // list level functions
46 ////////////////////////////////////////////////////////////
47 void MWAWListLevel::addTo(librevenge::RVNGPropertyList &propList, MWAWFontManagerPtr fontManager) const
48 {
49 propList.insert("text:min-label-width", m_labelWidth, librevenge::RVNG_INCH);
50 propList.insert("text:space-before", m_labelBeforeSpace, librevenge::RVNG_INCH);
51 if (m_labelAfterSpace > 0)
52 propList.insert("text:min-label-distance", m_labelAfterSpace, librevenge::RVNG_INCH);
53 if (m_numBeforeLabels)
54 propList.insert("text:display-levels", m_numBeforeLabels+1);
55 switch (m_alignment) {
56 case LEFT:
57 break;
58 case CENTER:
59 propList.insert("fo:text-align", "center");
60 break;
61 case RIGHT:
62 propList.insert("fo:text-align", "end");
63 break;
64 #if !defined(__clang__)
65 default:
66 break;
67 #endif
68 }
69
70 switch (m_type) {
71 case NONE:
72 propList.insert("text:bullet-char", " ");
73 break;
74 case BULLET:
75 if (!m_bullet.empty())
76 propList.insert("text:bullet-char", m_bullet.cstr());
77 else {
78 MWAW_DEBUG_MSG(("MWAWListLevel::addTo: the bullet char is not defined\n"));
79 propList.insert("text:bullet-char", "*");
80 }
81 break;
82 case LABEL:
83 if (!m_label.empty())
84 propList.insert("style:num-suffix", librevenge::RVNGPropertyFactory::newStringProp(m_label));
85 propList.insert("style:num-format", "");
86 break;
87 case DECIMAL:
88 case LOWER_ALPHA:
89 case UPPER_ALPHA:
90 case LOWER_ROMAN:
91 case UPPER_ROMAN:
92 if (!m_prefix.empty())
93 propList.insert("style:num-prefix",librevenge::RVNGPropertyFactory::newStringProp(m_prefix));
94 if (!m_suffix.empty())
95 propList.insert("style:num-suffix", librevenge::RVNGPropertyFactory::newStringProp(m_suffix));
96 if (m_type==DECIMAL) propList.insert("style:num-format", "1");
97 else if (m_type==LOWER_ALPHA) propList.insert("style:num-format", "a");
98 else if (m_type==UPPER_ALPHA) propList.insert("style:num-format", "A");
99 else if (m_type==LOWER_ROMAN) propList.insert("style:num-format", "i");
100 else propList.insert("style:num-format", "I");
101 propList.insert("text:start-value", getStartValue());
102 break;
103 case DEFAULT:
104 #if !defined(__clang__)
105 default:
106 #endif
107 MWAW_DEBUG_MSG(("MWAWListLevel::addTo: the level type is not set\n"));
108 }
109 if (m_spanId>=0 && fontManager) {
110 MWAWFont font;
111 if (fontManager->getFont(m_spanId, font))
112 font.addToListLevel(propList, fontManager->getFontConverter());
113 }
114 }
115
116 int MWAWListLevel::cmp(MWAWListLevel const &levl) const
117 {
118 int diff = int(m_type)-int(levl.m_type);
119 if (diff) return diff;
120 double fDiff = m_labelBeforeSpace-levl.m_labelBeforeSpace;
121 if (fDiff < 0.0) return -1;
122 if (fDiff > 0.0) return 1;
123 fDiff = m_labelWidth-levl.m_labelWidth;
124 if (fDiff < 0.0) return -1;
125 if (fDiff > 0.0) return 1;
126 diff = int(m_alignment)-levl.m_alignment;
127 if (diff) return diff;
128 fDiff = m_labelAfterSpace-levl.m_labelAfterSpace;
129 if (fDiff < 0.0) return -1;
130 if (fDiff > 0.0) return 1;
131 diff = m_numBeforeLabels-levl.m_numBeforeLabels;
132 if (diff) return diff;
133 diff = strcmp(m_label.cstr(),levl.m_label.cstr());
134 if (diff) return diff;
135 diff = strcmp(m_prefix.cstr(),levl.m_prefix.cstr());
136 if (diff) return diff;
137 diff = strcmp(m_suffix.cstr(),levl.m_suffix.cstr());
138 if (diff) return diff;
139 diff = strcmp(m_bullet.cstr(),levl.m_bullet.cstr());
140 if (diff) return diff;
141 diff = m_spanId-levl.m_spanId;
142 if (diff) return diff;
143 return 0;
144 }
145
146 std::ostream &operator<<(std::ostream &o, MWAWListLevel const &level)
147 {
148 o << "ListLevel[";
149 switch (level.m_type) {
150 case MWAWListLevel::BULLET:
151 o << "bullet='" << level.m_bullet.cstr() <<"'";
152 break;
153 case MWAWListLevel::LABEL:
154 o << "text='" << level.m_label.cstr() << "'";
155 break;
156 case MWAWListLevel::DECIMAL:
157 o << "decimal";
158 break;
159 case MWAWListLevel::LOWER_ALPHA:
160 o << "alpha";
161 break;
162 case MWAWListLevel::UPPER_ALPHA:
163 o << "ALPHA";
164 break;
165 case MWAWListLevel::LOWER_ROMAN:
166 o << "roman";
167 break;
168 case MWAWListLevel::UPPER_ROMAN:
169 o << "ROMAN";
170 break;
171 case MWAWListLevel::NONE:
172 break;
173 case MWAWListLevel::DEFAULT:
174 #if !defined(__clang__)
175 default:
176 #endif
177 o << "####type";
178 }
179 switch (level.m_alignment) {
180 case MWAWListLevel::LEFT:
181 break;
182 case MWAWListLevel::CENTER:
183 o << ",center";
184 break;
185 case MWAWListLevel::RIGHT:
186 o << ",right";
187 break;
188 #if !defined(__clang__)
189 default:
190 o << "###align=" << int(level.m_alignment) << ",";
191 #endif
192 }
193 if (level.m_type != MWAWListLevel::BULLET && level.m_startValue)
194 o << ",startVal= " << level.m_startValue;
195 if (!level.m_prefix.empty()) o << ", prefix='" << level.m_prefix.cstr()<<"'";
196 if (!level.m_suffix.empty()) o << ", suffix='" << level.m_suffix.cstr()<<"'";
197 if (level.m_labelBeforeSpace < 0 || level.m_labelBeforeSpace > 0) o << ", indent=" << level.m_labelBeforeSpace;
198 if (level.m_labelWidth < 0 || level.m_labelWidth > 0) o << ", width=" << level.m_labelWidth;
199 if (level.m_labelAfterSpace > 0) o << ", labelTextW=" << level.m_labelAfterSpace;
200 if (level.m_numBeforeLabels) o << ", show=" << level.m_numBeforeLabels << "[before]";
201 if (level.m_spanId>=0) o << ", span[id]='" << level.m_spanId << "'";
202 o << "]";
203 if (level.m_extra.length()) o << ", " << level.m_extra;
204 return o;
205 }
206
207 ////////////////////////////////////////////////////////////
208 // list functions
209 ////////////////////////////////////////////////////////////
210 void MWAWList::resize(int level)
211 {
212 if (level < 0) {
213 MWAW_DEBUG_MSG(("MWAWList::resize: level %d can not be negatif\n",level));
214 return;
215 }
216 if (level == int(m_levels.size()))
217 return;
218 m_levels.resize(size_t(level));
219 m_actualIndices.resize(size_t(level), 0);
220 m_nextIndices.resize(size_t(level), 1);
221 if (m_actLevel >= level)
222 m_actLevel=level-1;
223 m_modifyMarker++;
224 }
225
226 void MWAWList::updateIndicesFrom(MWAWList const &list)
227 {
228 size_t maxLevel=list.m_levels.size();
229 if (maxLevel>m_levels.size())
230 maxLevel=m_levels.size();
231 for (size_t level=0 ; level < maxLevel; level++) {
232 m_actualIndices[size_t(level)]=m_levels[size_t(level)].getStartValue()-1;
233 m_nextIndices[level]=list.m_nextIndices[level];
234 }
235 m_modifyMarker++;
236 }
237
238 bool MWAWList::isCompatibleWith(int levl, MWAWListLevel const &level) const
239 {
240 if (levl < 1) {
241 MWAW_DEBUG_MSG(("MWAWList::isCompatibleWith: level %d must be positive\n",levl));
242 return false;
243 }
244
245 return levl > int(m_levels.size()) ||
246 level.cmp(m_levels[size_t(levl-1)])==0;
247 }
248
249 bool MWAWList::isCompatibleWith(MWAWList const &newList) const
250 {
251 size_t maxLevel=newList.m_levels.size();
252 if (maxLevel>m_levels.size())
253 maxLevel=m_levels.size();
254 for (size_t level=0 ; level < maxLevel; level++) {
255 if (m_levels[level].cmp(newList.m_levels[level])!=0)
256 return false;
257 }
258 return true;
259 }
260
261 void MWAWList::setId(int newId) const
262 {
263 m_id[0] = newId;
264 m_id[1] = newId+1;
265 }
266
267 bool MWAWList::addTo(int level, librevenge::RVNGPropertyList &pList, MWAWFontManagerPtr fontManager) const
268 {
269 if (level <= 0 || level > int(m_levels.size()) ||
270 m_levels[size_t(level-1)].isDefault()) {
271 MWAW_DEBUG_MSG(("MWAWList::addTo: level %d is not defined\n",level));
272 return false;
273 }
274
275 if (getId()==-1) {
276 MWAW_DEBUG_MSG(("MWAWList::addTo: the list id is not set\n"));
277 static int falseId = 1000;
278 setId(falseId+=2);
279 }
280 pList.insert("librevenge:list-id", getId());
281 pList.insert("librevenge:level", level);
282 m_levels[size_t(level-1)].addTo(pList, fontManager);
283 return true;
284 }
285
286 void MWAWList::set(int levl, MWAWListLevel const &level)
287 {
288 if (levl < 1) {
289 MWAW_DEBUG_MSG(("MWAWList::set: can not set level %d\n",levl));
290 return;
291 }
292 if (levl > int(m_levels.size())) resize(levl);
293 int needReplace = m_levels[size_t(levl-1)].cmp(level) != 0 ||
294 (level.m_startValue && m_nextIndices[size_t(levl-1)] !=level.getStartValue());
295 if (level.m_startValue > 0 || level.m_type != m_levels[size_t(levl-1)].m_type) {
296 m_nextIndices[size_t(levl-1)]=level.getStartValue();
297 m_modifyMarker++;
298 }
299 if (needReplace) {
300 m_levels[size_t(levl-1)] = level;
301 m_modifyMarker++;
302 }
303 }
304
305 void MWAWList::setLevel(int levl) const
306 {
307 if (levl < 1 || levl > int(m_levels.size())) {
308 MWAW_DEBUG_MSG(("MWAWList::setLevel: can not set level %d\n",levl));
309 return;
310 }
311
312 if (levl < int(m_levels.size()))
313 m_actualIndices[size_t(levl)]=
314 (m_nextIndices[size_t(levl)]=m_levels[size_t(levl)].getStartValue())-1;
315
316 m_actLevel=levl-1;
317 }
318
319 void MWAWList::setStartValueForNextElement(int value)
320 {
321 if (m_actLevel < 0 || m_actLevel >= int(m_levels.size())) {
322 MWAW_DEBUG_MSG(("MWAWList::setStartValueForNextElement: can not find level %d\n",m_actLevel));
323 return;
324 }
325 if (m_nextIndices[size_t(m_actLevel)]==value)
326 return;
327 m_nextIndices[size_t(m_actLevel)]=value;
328 m_modifyMarker++;
329 }
330
331 int MWAWList::getStartValueForNextElement() const
332 {
333 if (m_actLevel < 0 || m_actLevel >= int(m_levels.size())) {
334 MWAW_DEBUG_MSG(("MWAWList::getStartValueForNextElement: can not find level %d\n",m_actLevel));
335 return -1;
336 }
337 if (!m_levels[size_t(m_actLevel)].isNumeric())
338 return -1;
339 return m_nextIndices[size_t(m_actLevel)];
340 }
341
342 void MWAWList::openElement() const
343 {
344 if (m_actLevel < 0 || m_actLevel >= int(m_levels.size())) {
345 MWAW_DEBUG_MSG(("MWAWList::openElement: can not set level %d\n",m_actLevel));
346 return;
347 }
348 if (m_levels[size_t(m_actLevel)].isNumeric())
349 m_actualIndices[size_t(m_actLevel)]=m_nextIndices[size_t(m_actLevel)]++;
350 }
351
352 bool MWAWList::isNumeric(int levl) const
353 {
354 if (levl < 1 || levl > int(m_levels.size())) {
355 MWAW_DEBUG_MSG(("MWAWList::isNumeric: the level does not exist\n"));
356 return false;
357 }
358
359 return m_levels[size_t(levl-1)].isNumeric();
360 }
361
362 ////////////////////////////////////////////////////////////
363 // list manager function
364 ////////////////////////////////////////////////////////////
365 MWAWListManager::MWAWListManager(MWAWFontManagerPtr const &fontManager) : m_fontManager(fontManager), m_listList(), m_sendIdMarkerList()
366 {
367 }
368
369 MWAWListManager::~MWAWListManager()
370 {
371 }
372
373 bool MWAWListManager::needToSend(int index, std::vector<int> &idMarkerList) const
374 {
375 if (index <= 0) return false;
376 if (index >= int(idMarkerList.size()))
377 idMarkerList.resize(size_t(index)+1,0);
378 size_t mainId=size_t(index-1)/2;
379 if (mainId >= m_listList.size()) {
380 MWAW_DEBUG_MSG(("MWAWListManager::needToSend: can not find list %d\n", index));
381 return false;
382 }
383 MWAWList const &list=m_listList[mainId];
384 if (idMarkerList[size_t(index)] == list.getMarker())
385 return false;
386 idMarkerList[size_t(index)]=list.getMarker();
387 if (list.getId()!=index) list.swapId();
388 return true;
389 }
390
391 std::shared_ptr<MWAWList> MWAWListManager::getList(int index) const
392 {
393 std::shared_ptr<MWAWList> res;
394 if (index <= 0) return res;
395 size_t mainId=size_t(index-1)/2;
396 if (mainId < m_listList.size()) {
397 res.reset(new MWAWList(m_listList[mainId]));
398 if (res->getId()!=index)
399 res->swapId();
400 }
401 return res;
402 }
403
404 std::shared_ptr<MWAWList> MWAWListManager::getNewList(std::shared_ptr<MWAWList> actList, int levl, MWAWListLevel const &level)
405 {
406 if (actList && actList->getId()>=0 && actList->isCompatibleWith(levl, level)) {
407 actList->set(levl, level);
408 int index=actList->getId();
409 size_t mainId=size_t(index-1)/2;
410 if (mainId < m_listList.size() && m_listList[mainId].numLevels() < levl)
411 m_listList[mainId].set(levl, level);
412 return actList;
413 }
414 MWAWList res;
415 if (actList) {
416 res = *actList;
417 res.resize(levl);
418 }
419 size_t numList=m_listList.size();
420 res.setId(int(2*numList+1));
421 res.set(levl, level);
422 for (auto &list : m_listList) {
423 if (!list.isCompatibleWith(res))
424 continue;
425 if (list.numLevels() < levl)
426 list.set(levl, level);
427 std::shared_ptr<MWAWList> copy(new MWAWList(list));
428 copy->updateIndicesFrom(res);
429 return copy;
430 }
431 m_listList.push_back(res);
432 return std::shared_ptr<MWAWList>(new MWAWList(res));
433 }
434
435 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: