Coverage Report

Created: 2026-09-06 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwps/src/lib/WPSList.cpp
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* libwps
3
 * Version: MPL 2.0 / LGPLv2.1+
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
 * Major Contributor(s):
10
 * Copyright (C) 2009, 2011 Alonso Laurent (alonso@loria.fr)
11
 * Copyright (C) 2006, 2007 Andrew Ziem
12
 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
13
 * Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
14
 * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
15
 *
16
 * For minor contributions see the git repository.
17
 *
18
 * Alternatively, the contents of this file may be used under the terms
19
 * of the GNU Lesser General Public License Version 2.1 or later
20
 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21
 * applicable instead of those above.
22
 *
23
 * For further information visit http://libwps.sourceforge.net
24
 */
25
26
#include <librevenge/librevenge.h>
27
28
#include "WPSList.h"
29
30
#include <iostream>
31
#include <cstring>
32
33
void WPSList::Level::addTo(librevenge::RVNGPropertyList &propList, int startVal) const
34
17.6k
{
35
17.6k
  propList.insert("text:min-label-width", m_labelWidth);
36
17.6k
  propList.insert("text:space-before", m_labelIndent);
37
17.6k
  switch (m_type)
38
17.6k
  {
39
572
  case libwps::BULLET:
40
572
    if (m_bullet.len())
41
572
      propList.insert("text:bullet-char", m_bullet.cstr());
42
0
    else
43
0
    {
44
0
      WPS_DEBUG_MSG(("WPSList::Level::addTo: the bullet char is not defined\n"));
45
0
      propList.insert("text:bullet-char", "*");
46
0
    }
47
572
    break;
48
5.81k
  case libwps::ARABIC:
49
8.21k
  case libwps::LOWERCASE:
50
11.3k
  case libwps::UPPERCASE:
51
13.2k
  case libwps::LOWERCASE_ROMAN:
52
17.0k
  case libwps::UPPERCASE_ROMAN:
53
17.0k
    if (m_prefix.len()) propList.insert("style:num-prefix",m_prefix);
54
17.0k
    if (m_suffix.len()) propList.insert("style:num-suffix", m_suffix);
55
17.0k
    propList.insert("style:num-format", libwps::numberingTypeToString(m_type).c_str());
56
17.0k
    propList.insert("text:start-value", startVal);
57
17.0k
    break;
58
0
  case libwps::NONE:
59
0
    break;
60
0
  default:
61
0
    WPS_DEBUG_MSG(("WPSList::Level::addTo: the level type is not set\n"));
62
17.6k
  }
63
64
17.6k
  m_sendToInterface = true;
65
17.6k
}
66
67
int WPSList::Level::cmpType(WPSList::Level const &levl) const
68
73.0k
{
69
73.0k
  int diff = int(m_type)-int(levl.m_type);
70
73.0k
  if (diff) return diff;
71
47.7k
  diff = strcmp(m_prefix.cstr(),levl.m_prefix.cstr());
72
47.7k
  if (diff) return diff;
73
40.3k
  diff = strcmp(m_suffix.cstr(),levl.m_suffix.cstr());
74
40.3k
  if (diff) return diff;
75
36.1k
  diff = strcmp(m_bullet.cstr(),levl.m_bullet.cstr());
76
36.1k
  if (diff) return diff;
77
27.3k
  return 0;
78
36.1k
}
79
80
int WPSList::Level::cmp(WPSList::Level const &levl) const
81
36.5k
{
82
36.5k
  int diff = cmpType(levl);
83
36.5k
  if (diff) return diff;
84
13.6k
  double fDiff = m_labelIndent-levl.m_labelIndent;
85
13.6k
  if (fDiff < 0) return -1;
86
13.6k
  if (fDiff > 0) return 1;
87
13.6k
  fDiff = m_labelWidth-levl.m_labelWidth;
88
13.6k
  if (fDiff < 0) return -1;
89
12.2k
  if (fDiff > 0) return 1;
90
11.0k
  diff = strcmp(m_bullet.cstr(),levl.m_bullet.cstr());
91
11.0k
  if (diff) return diff;
92
11.0k
  return 0;
93
11.0k
}
94
95
std::ostream &operator<<(std::ostream &o, WPSList::Level const &ft)
96
0
{
97
0
  o << "ListLevel[";
98
0
  switch (ft.m_type)
99
0
  {
100
0
  case libwps::BULLET:
101
0
    o << "bullet='" << ft.m_bullet.cstr() <<"'";
102
0
    break;
103
0
  case libwps::ARABIC:
104
0
    o << "decimal";
105
0
    break;
106
0
  case libwps::LOWERCASE:
107
0
    o << "alpha";
108
0
    break;
109
0
  case libwps::UPPERCASE:
110
0
    o << "ALPHA";
111
0
    break;
112
0
  case libwps::LOWERCASE_ROMAN:
113
0
    o << "roman";
114
0
    break;
115
0
  case libwps::UPPERCASE_ROMAN:
116
0
    o << "ROMAN";
117
0
    break;
118
0
  case libwps::NONE:
119
0
  default:
120
0
    o << "####";
121
0
  }
122
0
  if (ft.m_type != libwps::BULLET && ft.m_startValue >= 0)
123
0
    o << ",startVal= " << ft.m_startValue;
124
0
  if (ft.m_prefix.len()) o << ", prefix='" << ft.m_prefix.cstr()<<"'";
125
0
  if (ft.m_suffix.len()) o << ", suffix='" << ft.m_suffix.cstr()<<"'";
126
0
  if (ft.m_labelIndent<0||ft.m_labelIndent>0)
127
0
    o << ", indent=" << ft.m_labelIndent;
128
0
  if (ft.m_labelWidth<0||ft.m_labelWidth>0)
129
0
    o << ", width=" << ft.m_labelWidth;
130
0
  o << "]";
131
0
  return o;
132
0
}
133
134
void WPSList::setId(int newId)
135
29.1k
{
136
29.1k
  if (m_id == newId) return;
137
29.1k
  m_previousId = m_id;
138
29.1k
  m_id = newId;
139
29.1k
  for (auto const &level : m_levels)
140
29.1k
    level.resetSendToInterface();
141
29.1k
}
142
143
bool WPSList::mustSendLevel(int level) const
144
20.8k
{
145
20.8k
  if (level <= 0 || level > int(m_levels.size()) ||
146
20.8k
          m_levels[size_t(level-1)].isDefault())
147
32
  {
148
32
    WPS_DEBUG_MSG(("WPSList::mustResentLevel: level %d is not defined\n",level));
149
32
    return false;
150
32
  }
151
20.8k
  if (!m_levels[size_t(level-1)].isSendToInterface()) return true;
152
153
4.18k
  return false;
154
20.8k
}
155
156
void WPSList::addLevelTo(int level, librevenge::RVNGPropertyList &propList) const
157
17.6k
{
158
17.6k
  if (level <= 0 || level > int(m_levels.size()) ||
159
17.6k
          m_levels[size_t(level-1)].isDefault())
160
32
  {
161
32
    WPS_DEBUG_MSG(("WPSList::addLevelTo: level %d is not defined\n",level));
162
32
    return;
163
32
  }
164
165
17.6k
  if (m_id==-1)
166
0
  {
167
0
    WPS_DEBUG_MSG(("WPSList::addLevelTo: the list id is not set\n"));
168
0
    static int falseId = 1000;
169
0
    m_id = falseId++;
170
0
  }
171
17.6k
  propList.insert("librevenge:list-id", m_id);
172
17.6k
  propList.insert("librevenge:level", level);
173
17.6k
  m_levels[size_t(level-1)].addTo(propList,m_actualIndices[size_t(level-1)]);
174
17.6k
}
175
176
void WPSList::set(int levl, Level const &level)
177
36.5k
{
178
36.5k
  if (levl < 1)
179
0
  {
180
0
    WPS_DEBUG_MSG(("WPSList::set: can not set level %d\n",levl));
181
0
    return;
182
0
  }
183
36.5k
  if (levl > int(m_levels.size()))
184
12.5k
  {
185
12.5k
    m_levels.resize(size_t(levl));
186
12.5k
    m_actualIndices.resize(size_t(levl), 0);
187
12.5k
    m_nextIndices.resize(size_t(levl), 1);
188
12.5k
  }
189
36.5k
  int needReplace = m_levels[size_t(levl-1)].cmp(level) != 0 ||
190
11.0k
                    (level.m_startValue>=0 && m_nextIndices[size_t(levl-1)] !=level.getStartValue());
191
36.5k
  if (level.m_startValue >= 0 || level.cmpType(m_levels[size_t(levl-1)]))
192
22.8k
  {
193
22.8k
    m_nextIndices[size_t(levl-1)]=level.getStartValue();
194
22.8k
  }
195
36.5k
  if (needReplace) m_levels[size_t(levl-1)] = level;
196
36.5k
}
197
198
void WPSList::setLevel(int levl) const
199
20.8k
{
200
20.8k
  if (levl < 1 || levl > int(m_levels.size()))
201
0
  {
202
0
    WPS_DEBUG_MSG(("WPSList::setLevel: can not set level %d\n",levl));
203
0
    return;
204
0
  }
205
206
20.8k
  if (levl < int(m_levels.size()))
207
0
    m_actualIndices[size_t(levl)]=
208
0
        (m_nextIndices[size_t(levl)]=m_levels[size_t(levl)].getStartValue())-1;
209
210
20.8k
  m_actLevel=levl-1;
211
20.8k
}
212
213
void WPSList::openElement() const
214
20.8k
{
215
20.8k
  if (m_actLevel < 0 || m_actLevel >= int(m_levels.size()))
216
0
  {
217
0
    WPS_DEBUG_MSG(("WPSList::openElement: can not set level %d\n",m_actLevel));
218
0
    return;
219
0
  }
220
20.8k
  if (m_levels[size_t(m_actLevel)].isNumeric())
221
17.3k
    m_actualIndices[size_t(m_actLevel)]=m_nextIndices[size_t(m_actLevel)]++;
222
20.8k
}
223
224
bool WPSList::isNumeric(int levl) const
225
17.6k
{
226
17.6k
  if (levl < 1 || levl > int(m_levels.size()))
227
0
  {
228
0
    WPS_DEBUG_MSG(("WPSList::isNumeric: the level does not exist\n"));
229
0
    return false;
230
0
  }
231
232
17.6k
  return m_levels[size_t(levl-1)].isNumeric();
233
17.6k
}
234
/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */