| 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 ([email protected]) |
| 11 | * Copyright (C) 2006, 2007 Andrew Ziem |
| 12 | * Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 13 | * Copyright (C) 2004 Marc Maurer ([email protected]) |
| 14 | * Copyright (C) 2003-2005 William Lachance ([email protected]) |
| 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 | { |
| 35 | propList.insert("text:min-label-width", m_labelWidth); |
| 36 | propList.insert("text:space-before", m_labelIndent); |
| 37 | switch (m_type) |
| 38 | { |
| 39 | case libwps::BULLET: |
| 40 | if (m_bullet.len()) |
| 41 | propList.insert("text:bullet-char", m_bullet.cstr()); |
| 42 | else |
| 43 | { |
| 44 | WPS_DEBUG_MSG(("WPSList::Level::addTo: the bullet char is not defined\n")); |
| 45 | propList.insert("text:bullet-char", "*"); |
| 46 | } |
| 47 | break; |
| 48 | case libwps::ARABIC: |
| 49 | case libwps::LOWERCASE: |
| 50 | case libwps::UPPERCASE: |
| 51 | case libwps::LOWERCASE_ROMAN: |
| 52 | case libwps::UPPERCASE_ROMAN: |
| 53 | if (m_prefix.len()) propList.insert("style:num-prefix",m_prefix); |
| 54 | if (m_suffix.len()) propList.insert("style:num-suffix", m_suffix); |
| 55 | propList.insert("style:num-format", libwps::numberingTypeToString(m_type).c_str()); |
| 56 | propList.insert("text:start-value", startVal); |
| 57 | break; |
| 58 | case libwps::NONE: |
| 59 | break; |
| 60 | default: |
| 61 | WPS_DEBUG_MSG(("WPSList::Level::addTo: the level type is not set\n")); |
| 62 | } |
| 63 | |
| 64 | m_sendToInterface = true; |
| 65 | } |
| 66 | |
| 67 | int WPSList::Level::cmpType(WPSList::Level const &levl) const |
| 68 | { |
| 69 | int diff = int(m_type)-int(levl.m_type); |
| 70 | if (diff) return diff; |
| 71 | diff = strcmp(m_prefix.cstr(),levl.m_prefix.cstr()); |
| 72 | if (diff) return diff; |
| 73 | diff = strcmp(m_suffix.cstr(),levl.m_suffix.cstr()); |
| 74 | if (diff) return diff; |
| 75 | diff = strcmp(m_bullet.cstr(),levl.m_bullet.cstr()); |
| 76 | if (diff) return diff; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int WPSList::Level::cmp(WPSList::Level const &levl) const |
| 81 | { |
| 82 | int diff = cmpType(levl); |
| 83 | if (diff) return diff; |
| 84 | double fDiff = m_labelIndent-levl.m_labelIndent; |
| 85 | if (fDiff < 0) return -1; |
| 86 | if (fDiff > 0) return 1; |
| 87 | fDiff = m_labelWidth-levl.m_labelWidth; |
| 88 | if (fDiff < 0) return -1; |
| 89 | if (fDiff > 0) return 1; |
| 90 | diff = strcmp(m_bullet.cstr(),levl.m_bullet.cstr()); |
| 91 | if (diff) return diff; |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | std::ostream &operator<<(std::ostream &o, WPSList::Level const &ft) |
| 96 | { |
| 97 | o << "ListLevel["; |
| 98 | switch (ft.m_type) |
| 99 | { |
| 100 | case libwps::BULLET: |
| 101 | o << "bullet='" << ft.m_bullet.cstr() <<"'"; |
| 102 | break; |
| 103 | case libwps::ARABIC: |
| 104 | o << "decimal"; |
| 105 | break; |
| 106 | case libwps::LOWERCASE: |
| 107 | o << "alpha"; |
| 108 | break; |
| 109 | case libwps::UPPERCASE: |
| 110 | o << "ALPHA"; |
| 111 | break; |
| 112 | case libwps::LOWERCASE_ROMAN: |
| 113 | o << "roman"; |
| 114 | break; |
| 115 | case libwps::UPPERCASE_ROMAN: |
| 116 | o << "ROMAN"; |
| 117 | break; |
| 118 | case libwps::NONE: |
| 119 | default: |
| 120 | o << "####"; |
| 121 | } |
| 122 | if (ft.m_type != libwps::BULLET && ft.m_startValue >= 0) |
| 123 | o << ",startVal= " << ft.m_startValue; |
| 124 | if (ft.m_prefix.len()) o << ", prefix='" << ft.m_prefix.cstr()<<"'"; |
| 125 | if (ft.m_suffix.len()) o << ", suffix='" << ft.m_suffix.cstr()<<"'"; |
| 126 | if (ft.m_labelIndent<0||ft.m_labelIndent>0) |
| 127 | o << ", indent=" << ft.m_labelIndent; |
| 128 | if (ft.m_labelWidth<0||ft.m_labelWidth>0) |
| 129 | o << ", width=" << ft.m_labelWidth; |
| 130 | o << "]"; |
| 131 | return o; |
| 132 | } |
| 133 | |
| 134 | void WPSList::setId(int newId) |
| 135 | { |
| 136 | if (m_id == newId) return; |
| 137 | m_previousId = m_id; |
| 138 | m_id = newId; |
| 139 | for (auto const &level : m_levels) |
| 140 | level.resetSendToInterface(); |
| 141 | } |
| 142 | |
| 143 | bool WPSList::mustSendLevel(int level) const |
| 144 | { |
| 145 | if (level <= 0 || level > int(m_levels.size()) || |
| 146 | m_levels[size_t(level-1)].isDefault()) |
| 147 | { |
| 148 | WPS_DEBUG_MSG(("WPSList::mustResentLevel: level %d is not defined\n",level)); |
| 149 | return false; |
| 150 | } |
| 151 | if (!m_levels[size_t(level-1)].isSendToInterface()) return true; |
| 152 | |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | void WPSList::addLevelTo(int level, librevenge::RVNGPropertyList &propList) const |
| 157 | { |
| 158 | if (level <= 0 || level > int(m_levels.size()) || |
| 159 | m_levels[size_t(level-1)].isDefault()) |
| 160 | { |
| 161 | WPS_DEBUG_MSG(("WPSList::addLevelTo: level %d is not defined\n",level)); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | if (m_id==-1) |
| 166 | { |
| 167 | WPS_DEBUG_MSG(("WPSList::addLevelTo: the list id is not set\n")); |
| 168 | static int falseId = 1000; |
| 169 | m_id = falseId++; |
| 170 | } |
| 171 | propList.insert("librevenge:list-id", m_id); |
| 172 | propList.insert("librevenge:level", level); |
| 173 | m_levels[size_t(level-1)].addTo(propList,m_actualIndices[size_t(level-1)]); |
| 174 | } |
| 175 | |
| 176 | void WPSList::set(int levl, Level const &level) |
| 177 | { |
| 178 | if (levl < 1) |
| 179 | { |
| 180 | WPS_DEBUG_MSG(("WPSList::set: can not set level %d\n",levl)); |
| 181 | return; |
| 182 | } |
| 183 | if (levl > int(m_levels.size())) |
| 184 | { |
| 185 | m_levels.resize(size_t(levl)); |
| 186 | m_actualIndices.resize(size_t(levl), 0); |
| 187 | m_nextIndices.resize(size_t(levl), 1); |
| 188 | } |
| 189 | int needReplace = m_levels[size_t(levl-1)].cmp(level) != 0 || |
| 190 | (level.m_startValue>=0 && m_nextIndices[size_t(levl-1)] !=level.getStartValue()); |
| 191 | if (level.m_startValue >= 0 || level.cmpType(m_levels[size_t(levl-1)])) |
| 192 | { |
| 193 | m_nextIndices[size_t(levl-1)]=level.getStartValue(); |
| 194 | } |
| 195 | if (needReplace) m_levels[size_t(levl-1)] = level; |
| 196 | } |
| 197 | |
| 198 | void WPSList::setLevel(int levl) const |
| 199 | { |
| 200 | if (levl < 1 || levl > int(m_levels.size())) |
| 201 | { |
| 202 | WPS_DEBUG_MSG(("WPSList::setLevel: can not set level %d\n",levl)); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | if (levl < int(m_levels.size())) |
| 207 | m_actualIndices[size_t(levl)]= |
| 208 | (m_nextIndices[size_t(levl)]=m_levels[size_t(levl)].getStartValue())-1; |
| 209 | |
| 210 | m_actLevel=levl-1; |
| 211 | } |
| 212 | |
| 213 | void WPSList::openElement() const |
| 214 | { |
| 215 | if (m_actLevel < 0 || m_actLevel >= int(m_levels.size())) |
| 216 | { |
| 217 | WPS_DEBUG_MSG(("WPSList::openElement: can not set level %d\n",m_actLevel)); |
| 218 | return; |
| 219 | } |
| 220 | if (m_levels[size_t(m_actLevel)].isNumeric()) |
| 221 | m_actualIndices[size_t(m_actLevel)]=m_nextIndices[size_t(m_actLevel)]++; |
| 222 | } |
| 223 | |
| 224 | bool WPSList::isNumeric(int levl) const |
| 225 | { |
| 226 | if (levl < 1 || levl > int(m_levels.size())) |
| 227 | { |
| 228 | WPS_DEBUG_MSG(("WPSList::isNumeric: the level does not exist\n")); |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | return m_levels[size_t(levl-1)].isNumeric(); |
| 233 | } |
| 234 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |