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 void Counter::setFlatLabelStrings(docstring const & fls, 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 // FIXME: this should get translated.
439 return counterLabel(it->second.flatLabelString(appendix()));
443 docstring Counters::flattenLabelString(docstring const & counter,
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;