X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FCounters.cpp;h=305cb17a2c344456a85c8561d561ade27a935127;hb=8a69ffd3bfaa4ab23b281a38b9a65f0a8550c997;hp=20654b7e8e5340830387be39aec386a2a915325b;hpb=f630be890494c849981e4fb52ea4740506e92bed;p=lyx.git diff --git a/src/Counters.cpp b/src/Counters.cpp index 20654b7e8e..305cb17a2c 100644 --- a/src/Counters.cpp +++ b/src/Counters.cpp @@ -6,6 +6,7 @@ * \author Lars Gullik Bjønnes * \author Martin Vermeer * \author André Pönitz + * \author Richard Heck (roman numerals) * * Full author contact details are available in file CREDITS. */ @@ -13,22 +14,20 @@ #include #include "Counters.h" -#include "debug.h" -#include "support/lstrings.h" #include "support/convert.h" +#include "support/debug.h" +#include "support/lstrings.h" -#include +#include "support/assert.h" #include +using namespace std; +using namespace lyx::support; namespace lyx { -using std::endl; -using std::ostringstream; -using std::string; - Counter::Counter() { @@ -36,6 +35,14 @@ Counter::Counter() } +Counter::Counter(docstring const & mc, docstring const & ls, + docstring const & lsa) + : master_(mc), labelstring_(ls), labelstringappendix_(lsa) +{ + reset(); +} + + void Counter::set(int v) { value_ = v; @@ -72,50 +79,36 @@ docstring const & Counter::master() const } -void Counter::setMaster(docstring const & m) +docstring const & Counter::labelString() const { - master_ = m; + return labelstring_; } -void Counters::newCounter(docstring const & newc) +docstring const & Counter::labelStringAppendix() const { - // First check if newc already exist - CounterList::iterator const cit = counterList.find(newc); - // if already exist give warning and return - if (cit != counterList.end()) { - lyxerr << "New counter already exists: " - << to_utf8(newc) - << endl; - return; - } - counterList[newc]; + return labelstringappendix_; } void Counters::newCounter(docstring const & newc, - docstring const & masterc) + docstring const & masterc, + docstring const & ls, + docstring const & lsa) { - // First check if newc already exists - CounterList::iterator const cit = counterList.find(newc); - // if already existant give warning and return - if (cit != counterList.end()) { - lyxerr << "New counter already exists: " - << to_utf8(newc) - << endl; - return; - } - // then check if masterc exists - CounterList::iterator const it = counterList.find(masterc); - // if not give warning and return - if (it == counterList.end()) { + if (!masterc.empty() && !hasCounter(masterc)) { lyxerr << "Master counter does not exist: " << to_utf8(masterc) << endl; return; } + counterList[newc] = Counter(masterc, ls, lsa); +} + - counterList[newc].setMaster(masterc); +bool Counters::hasCounter(docstring const & c) const +{ + return counterList.find(c) != counterList.end(); } @@ -177,6 +170,9 @@ void Counters::step(docstring const & ctr) void Counters::reset() { + appendix_ = false; + subfloat_ = false; + current_float_.erase(); CounterList::iterator it = counterList.begin(); CounterList::iterator const end = counterList.end(); for (; it != end; ++it) { @@ -187,7 +183,7 @@ void Counters::reset() void Counters::reset(docstring const & match) { - BOOST_ASSERT(!match.empty()); + LASSERT(!match.empty(), /**/); CounterList::iterator it = counterList.begin(); CounterList::iterator end = counterList.end(); @@ -231,9 +227,9 @@ char alphaCounter(int const n) char hebrewCounter(int const n) { static const char hebrew[22] = { - 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', - 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö', - '÷', 'ø', 'ù', 'ú' + '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8', + '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6', + '\xf7', '\xf8', '\xf9', '\xfa' }; if (n < 1 || n > 22) @@ -242,33 +238,70 @@ char hebrewCounter(int const n) } -docstring const lowerromanCounter(int const n) + +//On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html +//and for a list of roman numerals up to and including 3999, see +//http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost +//for this info.) +docstring const romanCounter(int const n) { - static char const * const roman[20] = { - "i", "ii", "iii", "iv", "v", - "vi", "vii", "viii", "ix", "x", - "xi", "xii", "xiii", "xiv", "xv", - "xvi", "xvii", "xviii", "xix", "xx" + static char const * const ones[9] = { + "I", "II", "III", "IV", "V", + "VI", "VII", "VIII", "IX" }; - - if (n < 1 || n > 20) + + static char const * const tens[9] = { + "X", "XX", "XXX", "XL", "L", + "LX", "LXX", "LXXX", "XC" + }; + + static char const * const hunds[9] = { + "C", "CC", "CCC", "CD", "D", + "DC", "DCC", "DCCC", "CM" + }; + + if (n > 1000 || n < 1) return from_ascii("??"); - return from_ascii(roman[n - 1]); + + int val = n; + string roman; + switch (n) { + //special cases + case 900: + roman = "CM"; + break; + case 400: + roman = "CD"; + break; + default: + if (val >= 100) { + int hundreds = val / 100; + roman = hunds[hundreds - 1]; + val = val % 100; + } + if (val >= 10) { + switch (val) { + //special case + case 90: + roman = roman + "XC"; + val = 0; //skip next + break; + default: + int tensnum = val / 10; + roman = roman + tens[tensnum - 1]; + val = val % 10; + } // end switch + } // end tens + if (val > 0) + roman = roman + ones[val -1]; + } + return from_ascii(roman); } -docstring const romanCounter(int const n) +docstring const lowerromanCounter(int const n) { - static char const * const roman[20] = { - "I", "II", "III", "IV", "V", - "VI", "VII", "VIII", "IX", "X", - "XI", "XII", "XIII", "XIV", "XV", - "XVI", "XVII", "XVIII", "XIX", "XX" - }; - - if (n < 1 || n > 20) - return from_ascii("??"); - return from_ascii(roman[n - 1]); + return lowercase(romanCounter(n)); } } // namespace anon @@ -306,13 +339,73 @@ docstring Counters::labelItem(docstring const & ctr, } -docstring Counters::counterLabel(docstring const & format) +docstring Counters::theCounter(docstring const & counter) +{ + std::set callers; + return theCounter(counter, callers); +} + +docstring Counters::theCounter(docstring const & counter, + std::set & callers) +{ + if (!hasCounter(counter)) + return from_ascii("??"); + + docstring label; + + if (callers.find(counter) == callers.end()) { + + pair::iterator, bool> result = callers.insert(counter); + + Counter const & c = counterList[counter]; + docstring ls = appendix() ? c.labelStringAppendix() : c.labelString(); + + if (ls.empty()) { + if (!c.master().empty()) + ls = from_ascii("\\the") + c.master() + from_ascii("."); + ls += from_ascii("\\arabic{") + counter + "}"; + } + + label = counterLabel(ls, &callers); + + callers.erase(result.first); + } else { + // recursion detected + lyxerr << "Warning: Recursion in label for counter `" + << counter << "' detected" + << endl; + } + + return label; +} + + +docstring Counters::counterLabel(docstring const & format, + std::set * callers) { docstring label = format; + + // FIXME: Using regexps would be better, but we compile boost without + // wide regexps currently. + + while (true) { + //lyxerr << "label=" << to_utf8(label) << endl; + size_t const i = label.find(from_ascii("\\the"), 0); + if (i == docstring::npos) + break; + size_t j = i + 4; + size_t k = j; + while (k < label.size() && lowercase(label[k]) >= 'a' + && lowercase(label[k]) <= 'z') + ++k; + docstring counter = label.substr(j, k - j); + docstring repl = callers? theCounter(counter, *callers): + theCounter(counter); + label.replace(i, k - j + 4, repl); + } + while (true) { -#ifdef WITH_WARNINGS -#warning Using boost::regex or boost::spirit would make this code a lot simpler... (Lgb) -#endif + //lyxerr << "label=" << to_utf8(label) << endl; size_t const i = label.find('\\', 0); if (i == docstring::npos) @@ -326,12 +419,10 @@ docstring Counters::counterLabel(docstring const & format) docstring const numbertype(label, i + 1, j - i - 1); docstring const counter(label, j + 1, k - j - 1); docstring const rep = labelItem(counter, numbertype); - label = docstring(label, 0, i) + rep + label = docstring(label, 0, i) + rep + docstring(label, k + 1, docstring::npos); - //lyxerr << " : " << " (" << counter << "," - // << numbertype << ") -> " << label << endl; } - //lyxerr << "counterLabel: " << format << " -> " << label << endl; + //lyxerr << "DONE! label=" << to_utf8(label) << endl; return label; }