summaryrefslogtreecommitdiff
path: root/src/lib/SW602List.cpp
blob: 10e0a668e9cdc3aa77dd2caa4acf0101bdc2d833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * This file is part of the libsw602 project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "SW602List.h"

#include <cstring>
#include <iostream>

#include <librevenge/librevenge.h>

#include "SW602Types.h"

namespace libsw602
{

////////////////////////////////////////////////////////////
// list level functions
////////////////////////////////////////////////////////////
void SW602ListLevel::addTo(librevenge::RVNGPropertyList &propList) const
{
  propList.insert("text:min-label-width", m_labelWidth, librevenge::RVNG_INCH);
  propList.insert("text:space-before", m_labelBeforeSpace, librevenge::RVNG_INCH);
  if (m_labelAfterSpace > 0)
    propList.insert("text:min-label-distance", m_labelAfterSpace, librevenge::RVNG_INCH);
  if (m_numBeforeLabels)
    propList.insert("text:display-levels", m_numBeforeLabels+1);
  switch (m_alignment)
  {
  case LEFT:
    break;
  case CENTER:
    propList.insert("fo:text-align", "center");
    break;
  case RIGHT:
    propList.insert("fo:text-align", "end");
    break;
  default:
    break;
  }

  switch (m_type)
  {
  case NONE:
    propList.insert("text:bullet-char", " ");
    break;
  case BULLET:
    if (m_bullet.len())
      propList.insert("text:bullet-char", m_bullet.cstr());
    else
    {
      SW602_DEBUG_MSG(("SW602ListLevel::addTo: the bullet char is not defined\n"));
      propList.insert("text:bullet-char", "*");
    }
    break;
  case LABEL:
    if (m_label.len())
      propList.insert("style:num-suffix", librevenge::RVNGPropertyFactory::newStringProp(m_label));
    propList.insert("style:num-format", "");
    break;
  case DECIMAL:
  case LOWER_ALPHA:
  case UPPER_ALPHA:
  case LOWER_ROMAN:
  case UPPER_ROMAN:
    if (m_prefix.len())
      propList.insert("style:num-prefix",librevenge::RVNGPropertyFactory::newStringProp(m_prefix));
    if (m_suffix.len())
      propList.insert("style:num-suffix", librevenge::RVNGPropertyFactory::newStringProp(m_suffix));
    if (m_type==DECIMAL) propList.insert("style:num-format", "1");
    else if (m_type==LOWER_ALPHA) propList.insert("style:num-format", "a");
    else if (m_type==UPPER_ALPHA) propList.insert("style:num-format", "A");
    else if (m_type==LOWER_ROMAN) propList.insert("style:num-format", "i");
    else propList.insert("style:num-format", "I");
    propList.insert("text:start-value", getStartValue());
    break;
  case DEFAULT:
  default:
    SW602_DEBUG_MSG(("SW602ListLevel::addTo: the level type is not set\n"));
  }
}

int SW602ListLevel::cmp(SW602ListLevel const &levl) const
{
  int diff = int(m_type)-int(levl.m_type);
  if (diff) return diff;
  double fDiff = m_labelBeforeSpace-levl.m_labelBeforeSpace;
  if (fDiff < 0.0) return -1;
  if (fDiff > 0.0) return 1;
  fDiff = m_labelWidth-levl.m_labelWidth;
  if (fDiff < 0.0) return -1;
  if (fDiff > 0.0) return 1;
  diff = int(m_alignment)-levl.m_alignment;
  if (diff) return diff;
  fDiff = m_labelAfterSpace-levl.m_labelAfterSpace;
  if (fDiff < 0.0) return -1;
  if (fDiff > 0.0) return 1;
  diff = m_numBeforeLabels-levl.m_numBeforeLabels;
  if (diff) return diff;
  diff = strcmp(m_label.cstr(),levl.m_label.cstr());
  if (diff) return diff;
  diff = strcmp(m_prefix.cstr(),levl.m_prefix.cstr());
  if (diff) return diff;
  diff = strcmp(m_suffix.cstr(),levl.m_suffix.cstr());
  if (diff) return diff;
  diff = strcmp(m_bullet.cstr(),levl.m_bullet.cstr());
  if (diff) return diff;
  return 0;
}

std::ostream &operator<<(std::ostream &o, SW602ListLevel const &level)
{
  o << "ListLevel[";
  switch (level.m_type)
  {
  case SW602ListLevel::BULLET:
    o << "bullet='" << level.m_bullet.cstr() <<"'";
    break;
  case SW602ListLevel::LABEL:
    o << "text='" << level.m_label.cstr() << "'";
    break;
  case SW602ListLevel::DECIMAL:
    o << "decimal";
    break;
  case SW602ListLevel::LOWER_ALPHA:
    o << "alpha";
    break;
  case SW602ListLevel::UPPER_ALPHA:
    o << "ALPHA";
    break;
  case SW602ListLevel::LOWER_ROMAN:
    o << "roman";
    break;
  case SW602ListLevel::UPPER_ROMAN:
    o << "ROMAN";
    break;
  case SW602ListLevel::NONE:
    break;
  case SW602ListLevel::DEFAULT:
  default:
    o << "####type";
  }
  switch (level.m_alignment)
  {
  case SW602ListLevel::LEFT:
    break;
  case SW602ListLevel::CENTER:
    o << ",center";
    break;
  case SW602ListLevel::RIGHT:
    o << ",right";
    break;
  default:
    o << "###align=" << int(level.m_alignment) << ",";
  }
  if (level.m_type != SW602ListLevel::BULLET && level.m_startValue)
    o << ",startVal= " << level.m_startValue;
  if (level.m_prefix.len()) o << ", prefix='" << level.m_prefix.cstr()<<"'";
  if (level.m_suffix.len()) o << ", suffix='" << level.m_suffix.cstr()<<"'";
  if (level.m_labelBeforeSpace < 0 || level.m_labelBeforeSpace > 0) o << ", indent=" << level.m_labelBeforeSpace;
  if (level.m_labelWidth < 0 || level.m_labelWidth > 0) o << ", width=" << level.m_labelWidth;
  if (level.m_labelAfterSpace > 0) o << ", labelTextW=" << level.m_labelAfterSpace;
  if (level.m_numBeforeLabels) o << ", show=" << level.m_numBeforeLabels << "[before]";
  o << "]";
  if (level.m_extra.length()) o << ", " << level.m_extra;
  return o;
}

////////////////////////////////////////////////////////////
// list functions
////////////////////////////////////////////////////////////
void SW602List::resize(int level)
{
  if (level < 0)
  {
    SW602_DEBUG_MSG(("SW602List::resize: level %d can not be negatif\n",level));
    return;
  }
  if (level == int(m_levels.size()))
    return;
  m_levels.resize(size_t(level));
  m_actualIndices.resize(size_t(level), 0);
  m_nextIndices.resize(size_t(level), 1);
  if (m_actLevel >= level)
    m_actLevel=level-1;
  m_modifyMarker++;
}

void SW602List::updateIndicesFrom(SW602List const &list)
{
  size_t maxLevel=list.m_levels.size();
  if (maxLevel>m_levels.size())
    maxLevel=m_levels.size();
  for (size_t level=0 ; level < maxLevel; level++)
  {
    m_actualIndices[size_t(level)]=m_levels[size_t(level)].getStartValue()-1;
    m_nextIndices[level]=list.m_nextIndices[level];
  }
  m_modifyMarker++;
}

bool SW602List::isCompatibleWith(int levl, SW602ListLevel const &level) const
{
  if (levl < 1)
  {
    SW602_DEBUG_MSG(("SW602List::isCompatibleWith: level %d must be positive\n",levl));
    return false;
  }

  return levl > int(m_levels.size()) ||
         level.cmp(m_levels[size_t(levl-1)])==0;
}

bool SW602List::isCompatibleWith(SW602List const &newList) const
{
  size_t maxLevel=newList.m_levels.size();
  if (maxLevel>m_levels.size())
    maxLevel=m_levels.size();
  for (size_t level=0 ; level < maxLevel; level++)
  {
    if (m_levels[level].cmp(newList.m_levels[level])!=0)
      return false;
  }
  return true;
}

void SW602List::setId(int newId) const
{
  m_id[0] = newId;
  m_id[1] = newId+1;
}

bool SW602List::addTo(int level, librevenge::RVNGPropertyList &pList) const
{
  if (level <= 0 || level > int(m_levels.size()) ||
      m_levels[size_t(level-1)].isDefault())
  {
    SW602_DEBUG_MSG(("SW602List::addTo: level %d is not defined\n",level));
    return false;
  }

  if (getId()==-1)
  {
    SW602_DEBUG_MSG(("SW602List::addTo: the list id is not set\n"));
    static int falseId = 1000;
    setId(falseId+=2);
  }
  pList.insert("librevenge:list-id", getId());
  pList.insert("librevenge:level", level);
  m_levels[size_t(level-1)].addTo(pList);
  return true;
}

void SW602List::set(int levl, SW602ListLevel const &level)
{
  if (levl < 1)
  {
    SW602_DEBUG_MSG(("SW602List::set: can not set level %d\n",levl));
    return;
  }
  if (levl > int(m_levels.size())) resize(levl);
  int needReplace = m_levels[size_t(levl-1)].cmp(level) != 0 ||
                    (level.m_startValue && m_nextIndices[size_t(levl-1)] !=level.getStartValue());
  if (level.m_startValue > 0 || level.m_type != m_levels[size_t(levl-1)].m_type)
  {
    m_nextIndices[size_t(levl-1)]=level.getStartValue();
    m_modifyMarker++;
  }
  if (needReplace)
  {
    m_levels[size_t(levl-1)] = level;
    m_modifyMarker++;
  }
}

void SW602List::setLevel(int levl) const
{
  if (levl < 1 || levl > int(m_levels.size()))
  {
    SW602_DEBUG_MSG(("SW602List::setLevel: can not set level %d\n",levl));
    return;
  }

  if (levl < int(m_levels.size()))
    m_actualIndices[size_t(levl)]=
      (m_nextIndices[size_t(levl)]=m_levels[size_t(levl)].getStartValue())-1;

  m_actLevel=levl-1;
}

void SW602List::setStartValueForNextElement(int value)
{
  if (m_actLevel < 0 || m_actLevel >= int(m_levels.size()))
  {
    SW602_DEBUG_MSG(("SW602List::setStartValueForNextElement: can not find level %d\n",m_actLevel));
    return;
  }
  if (m_nextIndices[size_t(m_actLevel)]==value)
    return;
  m_nextIndices[size_t(m_actLevel)]=value;
  m_modifyMarker++;
}

int SW602List::getStartValueForNextElement() const
{
  if (m_actLevel < 0 || m_actLevel >= int(m_levels.size()))
  {
    SW602_DEBUG_MSG(("SW602List::getStartValueForNextElement: can not find level %d\n",m_actLevel));
    return -1;
  }
  if (!m_levels[size_t(m_actLevel)].isNumeric())
    return -1;
  return m_nextIndices[size_t(m_actLevel)];
}

void SW602List::openElement() const
{
  if (m_actLevel < 0 || m_actLevel >= int(m_levels.size()))
  {
    SW602_DEBUG_MSG(("SW602List::openElement: can not set level %d\n",m_actLevel));
    return;
  }
  if (m_levels[size_t(m_actLevel)].isNumeric())
    m_actualIndices[size_t(m_actLevel)]=m_nextIndices[size_t(m_actLevel)]++;
}

bool SW602List::isNumeric(int levl) const
{
  if (levl < 1 || levl > int(m_levels.size()))
  {
    SW602_DEBUG_MSG(("SW602List::isNumeric: the level does not exist\n"));
    return false;
  }

  return m_levels[size_t(levl-1)].isNumeric();
}

////////////////////////////////////////////////////////////
// list manager function
////////////////////////////////////////////////////////////
bool SW602ListManager::needToSend(int index, std::vector<int> &idMarkerList) const
{
  if (index <= 0) return false;
  if (index >= int(idMarkerList.size()))
    idMarkerList.resize(size_t(index)+1,0);
  size_t mainId=size_t(index-1)/2;
  if (mainId >= m_listList.size())
  {
    SW602_DEBUG_MSG(("SW602ListManager::needToSend: can not find list %d\n", index));
    return false;
  }
  SW602List const &list=m_listList[mainId];
  if (idMarkerList[size_t(index)] == list.getMarker())
    return false;
  idMarkerList[size_t(index)]=list.getMarker();
  if (list.getId()!=index) list.swapId();
  return true;
}

boost::shared_ptr<SW602List> SW602ListManager::getList(int index) const
{
  boost::shared_ptr<SW602List> res;
  if (index <= 0) return res;
  size_t mainId=size_t(index-1)/2;
  if (mainId < m_listList.size())
  {
    res.reset(new SW602List(m_listList[mainId]));
    if (res->getId()!=index)
      res->swapId();
  }
  return res;
}

boost::shared_ptr<SW602List> SW602ListManager::getNewList(boost::shared_ptr<SW602List> actList, int levl, SW602ListLevel const &level)
{
  if (actList && actList->getId()>=0 && actList->isCompatibleWith(levl, level))
  {
    actList->set(levl, level);
    int index=actList->getId();
    size_t mainId=size_t(index-1)/2;
    if (mainId < m_listList.size() && m_listList[mainId].numLevels() < levl)
      m_listList[mainId].set(levl, level);
    return actList;
  }
  SW602List res;
  if (actList)
  {
    res = *actList;
    res.resize(levl);
  }
  size_t numList=m_listList.size();
  res.setId(int(2*numList+1));
  res.set(levl, level);
  for (size_t l=0; l < numList; l++)
  {
    if (!m_listList[l].isCompatibleWith(res))
      continue;
    if (m_listList[l].numLevels() < levl)
      m_listList[l].set(levl, level);
    boost::shared_ptr<SW602List> copy(new SW602List(m_listList[l]));
    copy->updateIndicesFrom(res);
    return copy;
  }
  m_listList.push_back(res);
  return boost::shared_ptr<SW602List>(new SW602List(res));
}

}

/* vim:set shiftwidth=2 softtabstop=2 expandtab: */