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"
28 using namespace lyx::support;
39 Counter::Counter(docstring const & mc, docstring const & ls,
40 docstring const & lsa)
41 : master_(mc), labelstring_(ls), labelstringappendix_(lsa)
47 bool Counter::read(Lexer & lex)
52 CT_LABELSTRING_APPENDIX,
56 LexerKeyword counterTags[] = {
58 { "labelstring", CT_LABELSTRING },
59 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
60 { "within", CT_WITHIN }
63 lex.pushTable(counterTags);
66 while (!getout && lex.isOK()) {
69 case Lexer::LEX_UNDEF:
70 lex.printError("Unknown counter tag `$$Token'");
78 master_ = lex.getDocString();
79 if (master_ == "none")
84 labelstring_ = lex.getDocString();
85 labelstringappendix_ = labelstring_;
87 case CT_LABELSTRING_APPENDIX:
89 labelstringappendix_ = lex.getDocString();
97 // Here if have a full counter if getout == true
99 LYXERR0("No End tag found for counter!");
104 void Counter::set(int v)
110 void Counter::addto(int v)
116 int Counter::value() const
128 void Counter::reset()
134 docstring const & Counter::master() const
140 docstring const & Counter::labelString(bool in_appendix) const
142 return in_appendix ? labelstringappendix_ : labelstring_;
146 docstring const & Counter::flatLabelString(bool in_appendix) const
148 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
152 docstring const & Counter::setFlatLabelStrings(docstring const & fls,
153 docstring const & flsa)
155 flatlabelstring_ = fls;
156 flatlabelstringappendix_ = flsa;
160 void Counters::newCounter(docstring const & newc,
161 docstring const & masterc,
162 docstring const & ls,
163 docstring const & lsa)
165 if (!masterc.empty() && !hasCounter(masterc)) {
166 lyxerr << "Master counter does not exist: "
171 counterList_[newc] = Counter(masterc, ls, lsa);
175 bool Counters::hasCounter(docstring const & c) const
177 return counterList_.find(c) != counterList_.end();
181 bool Counters::read(Lexer & lex, docstring const & name)
183 if (hasCounter(name)) {
184 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
185 return counterList_[name].read(lex);
187 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
189 bool success = cnt.read(lex);
191 counterList_[name] = cnt;
193 LYXERR0("Error reading counter `" << name << "'!");
198 void Counters::set(docstring const & ctr, int const val)
200 CounterList::iterator const it = counterList_.find(ctr);
201 if (it == counterList_.end()) {
202 lyxerr << "set: Counter does not exist: "
203 << to_utf8(ctr) << endl;
210 void Counters::addto(docstring const & ctr, int const val)
212 CounterList::iterator const it = counterList_.find(ctr);
213 if (it == counterList_.end()) {
214 lyxerr << "addto: Counter does not exist: "
215 << to_utf8(ctr) << endl;
218 it->second.addto(val);
222 int Counters::value(docstring const & ctr) const
224 CounterList::const_iterator const cit = counterList_.find(ctr);
225 if (cit == counterList_.end()) {
226 lyxerr << "value: Counter does not exist: "
227 << to_utf8(ctr) << endl;
230 return cit->second.value();
234 void Counters::step(docstring const & ctr)
236 CounterList::iterator it = counterList_.find(ctr);
237 if (it == counterList_.end()) {
238 lyxerr << "step: Counter does not exist: "
239 << to_utf8(ctr) << endl;
244 it = counterList_.begin();
245 CounterList::iterator const end = counterList_.end();
246 for (; it != end; ++it) {
247 if (it->second.master() == ctr) {
254 void Counters::reset()
258 current_float_.erase();
259 CounterList::iterator it = counterList_.begin();
260 CounterList::iterator const end = counterList_.end();
261 std::vector<docstring> callers;
262 for (; it != end; ++it) {
264 // Compute the explicit counter labels without any
265 // \thexxx strings, in order to avoid recursion.
266 // It only needs to be done when the textclass is
267 // updated, but in practice the extra work is probably
268 // not noticeable (JMarc)
269 docstring const & fls = flattenLabelString(it->first, false, callers);
270 docstring const & flsa = flattenLabelString(it->first, true, callers);
271 it->second.setFlatLabelStrings(fls, flsa);
276 void Counters::reset(docstring const & match)
278 LASSERT(!match.empty(), /**/);
280 CounterList::iterator it = counterList_.begin();
281 CounterList::iterator end = counterList_.end();
282 for (; it != end; ++it) {
283 if (it->first.find(match) != string::npos)
289 void Counters::copy(Counters & from, Counters & to, docstring const & match)
291 CounterList::iterator it = counterList_.begin();
292 CounterList::iterator end = counterList_.end();
293 for (; it != end; ++it) {
294 if (it->first.find(match) != string::npos || match == "") {
295 to.set(it->first, from.value(it->first));
303 char loweralphaCounter(int const n)
311 char alphaCounter(int const n)
319 char hebrewCounter(int const n)
321 static const char hebrew[22] = {
322 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
323 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
324 '\xf7', '\xf8', '\xf9', '\xfa'
329 return hebrew[n - 1];
334 //On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
335 //and for a list of roman numerals up to and including 3999, see
336 //http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
338 docstring const romanCounter(int const n)
340 static char const * const ones[9] = {
341 "I", "II", "III", "IV", "V",
342 "VI", "VII", "VIII", "IX"
345 static char const * const tens[9] = {
346 "X", "XX", "XXX", "XL", "L",
347 "LX", "LXX", "LXXX", "XC"
350 static char const * const hunds[9] = {
351 "C", "CC", "CCC", "CD", "D",
352 "DC", "DCC", "DCCC", "CM"
355 if (n > 1000 || n < 1)
356 return from_ascii("??");
370 int hundreds = val / 100;
371 roman = hunds[hundreds - 1];
378 roman = roman + "XC";
382 int tensnum = val / 10;
383 roman = roman + tens[tensnum - 1];
388 roman = roman + ones[val -1];
390 return from_ascii(roman);
394 docstring const lowerromanCounter(int const n)
396 return lowercase(romanCounter(n));
402 docstring Counters::labelItem(docstring const & ctr,
403 docstring const & numbertype) const
405 CounterList::const_iterator const cit = counterList_.find(ctr);
406 if (cit == counterList_.end()) {
409 << " does not exist." << endl;
413 int val = cit->second.value();
415 if (numbertype == "hebrew")
416 return docstring(1, hebrewCounter(val));
418 if (numbertype == "alph")
419 return docstring(1, loweralphaCounter(val));
421 if (numbertype == "Alph")
422 return docstring(1, alphaCounter(val));
424 if (numbertype == "roman")
425 return lowerromanCounter(val);
427 if (numbertype == "Roman")
428 return romanCounter(val);
430 return convert<docstring>(val);
434 docstring Counters::theCounter(docstring const & counter) const
436 CounterList::const_iterator it = counterList_.find(counter);
437 if (it == counterList_.end())
438 return from_ascii("??");
439 return counterLabel(it->second.flatLabelString(appendix()));
443 docstring Counters::flattenLabelString(docstring const & counter, bool in_appendix,
444 vector<docstring> & callers) const
448 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
449 // recursion detected
450 lyxerr << "Warning: Recursion in label for counter `"
451 << counter << "' detected"
453 return from_ascii("??");
456 CounterList::const_iterator it = counterList_.find(counter);
457 if (it == counterList_.end())
458 return from_ascii("??");
459 Counter const & c = it->second;
461 docstring ls = c.labelString(in_appendix);
463 callers.push_back(counter);
465 if (!c.master().empty())
466 ls = flattenLabelString(c.master(), in_appendix, callers)
469 return ls + from_ascii("\\arabic{") + counter + "}";
473 //lyxerr << "ls=" << to_utf8(ls) << endl;
474 size_t const i = ls.find(from_ascii("\\the"), 0);
475 if (i == docstring::npos)
477 size_t const j = i + 4;
479 while (k < ls.size() && lowercase(ls[k]) >= 'a'
480 && lowercase(ls[k]) <= 'z')
482 docstring const & newc = ls.substr(j, k - j);
483 docstring const & repl = flattenLabelString(newc, in_appendix, callers);
484 ls.replace(i, k - j + 4, repl);
492 docstring Counters::counterLabel(docstring const & format) const
494 docstring label = format;
496 // FIXME: Using regexps would be better, but we compile boost without
497 // wide regexps currently.
499 //lyxerr << "label=" << to_utf8(label) << endl;
500 size_t const i = label.find(from_ascii("\\the"), 0);
501 if (i == docstring::npos)
503 size_t const j = i + 4;
505 while (k < label.size() && lowercase(label[k]) >= 'a'
506 && lowercase(label[k]) <= 'z')
508 docstring const & newc = label.substr(j, k - j);
509 docstring const & repl = theCounter(newc);
510 label.replace(i, k - j + 4, repl);
513 //lyxerr << "label=" << to_utf8(label) << endl;
515 size_t const i = label.find('\\', 0);
516 if (i == docstring::npos)
518 size_t const j = label.find('{', i + 1);
519 if (j == docstring::npos)
521 size_t const k = label.find('}', j + 1);
522 if (k == docstring::npos)
524 docstring const numbertype(label, i + 1, j - i - 1);
525 docstring const counter(label, j + 1, k - j - 1);
526 docstring const & rep = labelItem(counter, numbertype);
527 label = docstring(label, 0, i) + rep
528 + docstring(label, k + 1, docstring::npos);
530 //lyxerr << "DONE! label=" << to_utf8(label) << endl;