3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Martin Vermeer
9 * \author Richard Heck (roman numerals)
11 * Full author contact details are available in file CREDITS.
19 #include "support/convert.h"
20 #include "support/debug.h"
21 #include "support/lassert.h"
22 #include "support/lstrings.h"
27 using namespace lyx::support;
38 Counter::Counter(docstring const & mc, docstring const & ls,
39 docstring const & lsa)
40 : master_(mc), labelstring_(ls), labelstringappendix_(lsa)
46 bool Counter::read(Lexer & lex)
51 CT_LABELSTRING_APPENDIX,
55 LexerKeyword counterTags[] = {
57 { "labelstring", CT_LABELSTRING },
58 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
59 { "within", CT_WITHIN }
62 lex.pushTable(counterTags);
65 while (!getout && lex.isOK()) {
68 case Lexer::LEX_UNDEF:
69 lex.printError("Unknown counter tag `$$Token'");
77 master_ = lex.getDocString();
78 if (master_ == "none")
83 labelstring_ = lex.getDocString();
84 labelstringappendix_ = labelstring_;
86 case CT_LABELSTRING_APPENDIX:
88 labelstringappendix_ = lex.getDocString();
96 // Here if have a full counter if getout == true
98 LYXERR0("No End tag found for counter!");
103 void Counter::set(int v)
109 void Counter::addto(int v)
115 int Counter::value() const
127 void Counter::reset()
133 docstring const & Counter::master() const
139 docstring const & Counter::labelString(bool in_appendix) const
141 return in_appendix ? labelstringappendix_ : labelstring_;
145 docstring const & Counter::flatLabelString(bool in_appendix) const
147 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
151 docstring const & Counter::setFlatLabelStrings(docstring const & fls,
152 docstring const & flsa)
154 flatlabelstring_ = fls;
155 flatlabelstringappendix_ = flsa;
159 void Counters::newCounter(docstring const & newc,
160 docstring const & masterc,
161 docstring const & ls,
162 docstring const & lsa)
164 if (!masterc.empty() && !hasCounter(masterc)) {
165 lyxerr << "Master counter does not exist: "
170 counterList_[newc] = Counter(masterc, ls, lsa);
174 bool Counters::hasCounter(docstring const & c) const
176 return counterList_.find(c) != counterList_.end();
180 bool Counters::read(Lexer & lex, docstring const & name)
182 if (hasCounter(name)) {
183 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
184 return counterList_[name].read(lex);
186 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
188 bool success = cnt.read(lex);
190 counterList_[name] = cnt;
192 LYXERR0("Error reading counter `" << name << "'!");
197 void Counters::set(docstring const & ctr, int const val)
199 CounterList::iterator const it = counterList_.find(ctr);
200 if (it == counterList_.end()) {
201 lyxerr << "set: Counter does not exist: "
202 << to_utf8(ctr) << endl;
209 void Counters::addto(docstring const & ctr, int const val)
211 CounterList::iterator const it = counterList_.find(ctr);
212 if (it == counterList_.end()) {
213 lyxerr << "addto: Counter does not exist: "
214 << to_utf8(ctr) << endl;
217 it->second.addto(val);
221 int Counters::value(docstring const & ctr) const
223 CounterList::const_iterator const cit = counterList_.find(ctr);
224 if (cit == counterList_.end()) {
225 lyxerr << "value: Counter does not exist: "
226 << to_utf8(ctr) << endl;
229 return cit->second.value();
233 void Counters::step(docstring const & ctr)
235 CounterList::iterator it = counterList_.find(ctr);
236 if (it == counterList_.end()) {
237 lyxerr << "step: Counter does not exist: "
238 << to_utf8(ctr) << endl;
243 it = counterList_.begin();
244 CounterList::iterator const end = counterList_.end();
245 for (; it != end; ++it) {
246 if (it->second.master() == ctr) {
253 void Counters::reset()
257 current_float_.erase();
258 CounterList::iterator it = counterList_.begin();
259 CounterList::iterator const end = counterList_.end();
260 std::vector<docstring> callers;
261 for (; it != end; ++it) {
263 // Compute the explicit counter labels without any
264 // \thexxx strings, in order to avoid recursion.
265 // It only needs to be done when the textclass is
266 // updated, but in practice the extra work is probably
267 // not noticeable (JMarc)
268 docstring const & fls = flattenLabelString(it->first, false, callers);
269 docstring const & flsa = flattenLabelString(it->first, true, callers);
270 it->second.setFlatLabelStrings(fls, flsa);
275 void Counters::reset(docstring const & match)
277 LASSERT(!match.empty(), /**/);
279 CounterList::iterator it = counterList_.begin();
280 CounterList::iterator end = counterList_.end();
281 for (; it != end; ++it) {
282 if (it->first.find(match) != string::npos)
288 void Counters::copy(Counters & from, Counters & to, docstring const & match)
290 CounterList::iterator it = counterList_.begin();
291 CounterList::iterator end = counterList_.end();
292 for (; it != end; ++it) {
293 if (it->first.find(match) != string::npos || match == "") {
294 to.set(it->first, from.value(it->first));
302 char loweralphaCounter(int const n)
310 char alphaCounter(int const n)
318 char hebrewCounter(int const n)
320 static const char hebrew[22] = {
321 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
322 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
323 '\xf7', '\xf8', '\xf9', '\xfa'
328 return hebrew[n - 1];
333 //On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
334 //and for a list of roman numerals up to and including 3999, see
335 //http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
337 docstring const romanCounter(int const n)
339 static char const * const ones[9] = {
340 "I", "II", "III", "IV", "V",
341 "VI", "VII", "VIII", "IX"
344 static char const * const tens[9] = {
345 "X", "XX", "XXX", "XL", "L",
346 "LX", "LXX", "LXXX", "XC"
349 static char const * const hunds[9] = {
350 "C", "CC", "CCC", "CD", "D",
351 "DC", "DCC", "DCCC", "CM"
354 if (n > 1000 || n < 1)
355 return from_ascii("??");
369 int hundreds = val / 100;
370 roman = hunds[hundreds - 1];
377 roman = roman + "XC";
381 int tensnum = val / 10;
382 roman = roman + tens[tensnum - 1];
387 roman = roman + ones[val -1];
389 return from_ascii(roman);
393 docstring const lowerromanCounter(int const n)
395 return lowercase(romanCounter(n));
401 docstring Counters::labelItem(docstring const & ctr,
402 docstring const & numbertype) const
404 CounterList::const_iterator const cit = counterList_.find(ctr);
405 if (cit == counterList_.end()) {
408 << " does not exist." << endl;
412 int val = cit->second.value();
414 if (numbertype == "hebrew")
415 return docstring(1, hebrewCounter(val));
417 if (numbertype == "alph")
418 return docstring(1, loweralphaCounter(val));
420 if (numbertype == "Alph")
421 return docstring(1, alphaCounter(val));
423 if (numbertype == "roman")
424 return lowerromanCounter(val);
426 if (numbertype == "Roman")
427 return romanCounter(val);
429 return convert<docstring>(val);
433 docstring Counters::theCounter(docstring const & counter) const
435 CounterList::const_iterator it = counterList_.find(counter);
436 if (it == counterList_.end())
437 return from_ascii("??");
438 return counterLabel(it->second.flatLabelString(appendix()));
442 docstring Counters::flattenLabelString(docstring const & counter, bool in_appendix,
443 vector<docstring> & callers) const
447 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
448 // recursion detected
449 lyxerr << "Warning: Recursion in label for counter `"
450 << counter << "' detected"
452 return from_ascii("??");
455 CounterList::const_iterator it = counterList_.find(counter);
456 if (it == counterList_.end())
457 return from_ascii("??");
458 Counter const & c = it->second;
460 docstring ls = c.labelString(in_appendix);
462 callers.push_back(counter);
464 if (!c.master().empty())
465 ls = flattenLabelString(c.master(), in_appendix, callers)
468 return ls + from_ascii("\\arabic{") + counter + "}";
472 //lyxerr << "ls=" << to_utf8(ls) << endl;
473 size_t const i = ls.find(from_ascii("\\the"), 0);
474 if (i == docstring::npos)
476 size_t const j = i + 4;
478 while (k < ls.size() && lowercase(ls[k]) >= 'a'
479 && lowercase(ls[k]) <= 'z')
481 docstring const & newc = ls.substr(j, k - j);
482 docstring const & repl = flattenLabelString(newc, in_appendix, callers);
483 ls.replace(i, k - j + 4, repl);
491 docstring Counters::counterLabel(docstring const & format) const
493 docstring label = format;
495 // FIXME: Using regexps would be better, but we compile boost without
496 // wide regexps currently.
498 //lyxerr << "label=" << to_utf8(label) << endl;
499 size_t const i = label.find(from_ascii("\\the"), 0);
500 if (i == docstring::npos)
502 size_t const j = i + 4;
504 while (k < label.size() && lowercase(label[k]) >= 'a'
505 && lowercase(label[k]) <= 'z')
507 docstring const & newc = label.substr(j, k - j);
508 docstring const & repl = theCounter(newc);
509 label.replace(i, k - j + 4, repl);
512 //lyxerr << "label=" << to_utf8(label) << endl;
514 size_t const i = label.find('\\', 0);
515 if (i == docstring::npos)
517 size_t const j = label.find('{', i + 1);
518 if (j == docstring::npos)
520 size_t const k = label.find('}', j + 1);
521 if (k == docstring::npos)
523 docstring const numbertype(label, i + 1, j - i - 1);
524 docstring const counter(label, j + 1, k - j - 1);
525 docstring const & rep = labelItem(counter, numbertype);
526 label = docstring(label, 0, i) + rep
527 + docstring(label, k + 1, docstring::npos);
529 //lyxerr << "DONE! label=" << to_utf8(label) << endl;