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.
20 #include "support/convert.h"
21 #include "support/debug.h"
22 #include "support/lstrings.h"
24 #include "support/lassert.h"
29 using namespace lyx::support;
40 Counter::Counter(docstring const & mc, docstring const & ls,
41 docstring const & lsa)
42 : master_(mc), labelstring_(ls), labelstringappendix_(lsa)
48 bool Counter::read(Lexer & lex)
53 CT_LABELSTRING_APPENDIX,
57 LexerKeyword counterTags[] = {
59 { "labelstring", CT_LABELSTRING },
60 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
61 { "within", CT_WITHIN }
64 lex.pushTable(counterTags);
67 while (!getout && lex.isOK()) {
70 case Lexer::LEX_UNDEF:
71 lex.printError("Unknown counter tag `$$Token'");
79 master_ = lex.getDocString();
80 if (master_ == "none")
85 labelstring_ = lex.getDocString();
86 labelstringappendix_ = labelstring_;
88 case CT_LABELSTRING_APPENDIX:
90 labelstringappendix_ = lex.getDocString();
98 // Here if have a full counter if getout == true
100 LYXERR0("No End tag found for counter!");
105 void Counter::set(int v)
111 void Counter::addto(int v)
117 int Counter::value() const
129 void Counter::reset()
135 docstring const & Counter::master() const
141 docstring const & Counter::labelString(bool in_appendix) const
143 return in_appendix ? labelstringappendix_ : labelstring_;
147 docstring const & Counter::flatLabelString(bool in_appendix) const
149 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
153 docstring const & Counter::setFlatLabelStrings(docstring const & fls,
154 docstring const & flsa)
156 flatlabelstring_ = fls;
157 flatlabelstringappendix_ = flsa;
161 void Counters::newCounter(docstring const & newc,
162 docstring const & masterc,
163 docstring const & ls,
164 docstring const & lsa)
166 if (!masterc.empty() && !hasCounter(masterc)) {
167 lyxerr << "Master counter does not exist: "
172 counterList_[newc] = Counter(masterc, ls, lsa);
176 bool Counters::hasCounter(docstring const & c) const
178 return counterList_.find(c) != counterList_.end();
182 bool Counters::read(Lexer & lex, docstring const & name)
184 if (hasCounter(name)) {
185 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
186 return counterList_[name].read(lex);
188 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
190 bool success = cnt.read(lex);
192 counterList_[name] = cnt;
194 LYXERR0("Error reading counter `" << name << "'!");
199 void Counters::set(docstring const & ctr, int const val)
201 CounterList::iterator const it = counterList_.find(ctr);
202 if (it == counterList_.end()) {
203 lyxerr << "set: Counter does not exist: "
204 << to_utf8(ctr) << endl;
211 void Counters::addto(docstring const & ctr, int const val)
213 CounterList::iterator const it = counterList_.find(ctr);
214 if (it == counterList_.end()) {
215 lyxerr << "addto: Counter does not exist: "
216 << to_utf8(ctr) << endl;
219 it->second.addto(val);
223 int Counters::value(docstring const & ctr) const
225 CounterList::const_iterator const cit = counterList_.find(ctr);
226 if (cit == counterList_.end()) {
227 lyxerr << "value: Counter does not exist: "
228 << to_utf8(ctr) << endl;
231 return cit->second.value();
235 void Counters::step(docstring const & ctr)
237 CounterList::iterator it = counterList_.find(ctr);
238 if (it == counterList_.end()) {
239 lyxerr << "step: Counter does not exist: "
240 << to_utf8(ctr) << endl;
245 it = counterList_.begin();
246 CounterList::iterator const end = counterList_.end();
247 for (; it != end; ++it) {
248 if (it->second.master() == ctr) {
255 void Counters::reset()
259 current_float_.erase();
260 CounterList::iterator it = counterList_.begin();
261 CounterList::iterator const end = counterList_.end();
262 std::vector<docstring> callers;
263 for (; it != end; ++it) {
265 // Compute the explicit counter labels without any
266 // \thexxx strings, in order to avoid recursion.
267 // It only needs to be done when the textclass is
268 // updated, but in practice the extra work is probably
269 // not noticeable (JMarc)
270 docstring const fls = flattenLabelString(it->first, false, callers);
271 docstring const flsa = flattenLabelString(it->first, true, callers);
272 it->second.setFlatLabelStrings(fls, flsa);
277 void Counters::reset(docstring const & match)
279 LASSERT(!match.empty(), /**/);
281 CounterList::iterator it = counterList_.begin();
282 CounterList::iterator end = counterList_.end();
283 for (; it != end; ++it) {
284 if (it->first.find(match) != string::npos)
290 void Counters::copy(Counters & from, Counters & to, docstring const & match)
292 CounterList::iterator it = counterList_.begin();
293 CounterList::iterator end = counterList_.end();
294 for (; it != end; ++it) {
295 if (it->first.find(match) != string::npos || match == "") {
296 to.set(it->first, from.value(it->first));
304 char loweralphaCounter(int const n)
312 char alphaCounter(int const n)
320 char hebrewCounter(int const n)
322 static const char hebrew[22] = {
323 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
324 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
325 '\xf7', '\xf8', '\xf9', '\xfa'
330 return hebrew[n - 1];
335 //On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
336 //and for a list of roman numerals up to and including 3999, see
337 //http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
339 docstring const romanCounter(int const n)
341 static char const * const ones[9] = {
342 "I", "II", "III", "IV", "V",
343 "VI", "VII", "VIII", "IX"
346 static char const * const tens[9] = {
347 "X", "XX", "XXX", "XL", "L",
348 "LX", "LXX", "LXXX", "XC"
351 static char const * const hunds[9] = {
352 "C", "CC", "CCC", "CD", "D",
353 "DC", "DCC", "DCCC", "CM"
356 if (n > 1000 || n < 1)
357 return from_ascii("??");
371 int hundreds = val / 100;
372 roman = hunds[hundreds - 1];
379 roman = roman + "XC";
383 int tensnum = val / 10;
384 roman = roman + tens[tensnum - 1];
389 roman = roman + ones[val -1];
391 return from_ascii(roman);
395 docstring const lowerromanCounter(int const n)
397 return lowercase(romanCounter(n));
403 docstring Counters::labelItem(docstring const & ctr,
404 docstring const & numbertype) const
406 CounterList::const_iterator const cit = counterList_.find(ctr);
407 if (cit == counterList_.end()) {
410 << " does not exist." << endl;
414 int val = cit->second.value();
416 if (numbertype == "hebrew")
417 return docstring(1, hebrewCounter(val));
419 if (numbertype == "alph")
420 return docstring(1, loweralphaCounter(val));
422 if (numbertype == "Alph")
423 return docstring(1, alphaCounter(val));
425 if (numbertype == "roman")
426 return lowerromanCounter(val);
428 if (numbertype == "Roman")
429 return romanCounter(val);
431 return convert<docstring>(val);
435 docstring Counters::theCounter(docstring const & counter) const
437 CounterList::const_iterator it = counterList_.find(counter);
438 if (it == counterList_.end())
439 return from_ascii("??");
440 return counterLabel(it->second.flatLabelString(appendix()));
444 docstring Counters::flattenLabelString(docstring const & counter, bool in_appendix,
445 vector<docstring> & callers) const
449 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
450 // recursion detected
451 lyxerr << "Warning: Recursion in label for counter `"
452 << counter << "' detected"
454 return from_ascii("??");
457 CounterList::const_iterator it = counterList_.find(counter);
458 if (it == counterList_.end())
459 return from_ascii("??");
460 Counter const & c = it->second;
462 docstring ls = c.labelString(in_appendix);
464 callers.push_back(counter);
466 if (!c.master().empty())
467 ls = flattenLabelString(c.master(), in_appendix, callers)
470 return ls + from_ascii("\\arabic{") + counter + "}";
474 //lyxerr << "ls=" << to_utf8(ls) << endl;
475 size_t const i = ls.find(from_ascii("\\the"), 0);
476 if (i == docstring::npos)
478 size_t const j = i + 4;
480 while (k < ls.size() && lowercase(ls[k]) >= 'a'
481 && lowercase(ls[k]) <= 'z')
483 docstring const newc = ls.substr(j, k - j);
484 docstring const repl = flattenLabelString(newc, in_appendix, callers);
485 ls.replace(i, k - j + 4, repl);
493 docstring Counters::counterLabel(docstring const & format) const
495 docstring label = format;
497 // FIXME: Using regexps would be better, but we compile boost without
498 // wide regexps currently.
500 //lyxerr << "label=" << to_utf8(label) << endl;
501 size_t const i = label.find(from_ascii("\\the"), 0);
502 if (i == docstring::npos)
504 size_t const j = i + 4;
506 while (k < label.size() && lowercase(label[k]) >= 'a'
507 && lowercase(label[k]) <= 'z')
509 docstring const newc = label.substr(j, k - j);
510 docstring const repl = theCounter(newc);
511 label.replace(i, k - j + 4, repl);
514 //lyxerr << "label=" << to_utf8(label) << endl;
516 size_t const i = label.find('\\', 0);
517 if (i == docstring::npos)
519 size_t const j = label.find('{', i + 1);
520 if (j == docstring::npos)
522 size_t const k = label.find('}', j + 1);
523 if (k == docstring::npos)
525 docstring const numbertype(label, i + 1, j - i - 1);
526 docstring const counter(label, j + 1, k - j - 1);
527 docstring const rep = labelItem(counter, numbertype);
528 label = docstring(label, 0, i) + rep
529 + docstring(label, k + 1, docstring::npos);
531 //lyxerr << "DONE! label=" << to_utf8(label) << endl;