7 #include "math_hullinset.h"
8 #include "math_mathmlstream.h"
9 #include "math_streamstr.h"
10 #include "math_support.h"
12 #include "frontends/Painter.h"
13 #include "textpainter.h"
15 #include "LaTeXFeatures.h"
16 #include "support/LAssert.h"
26 int getCols(string const & type)
28 if (type == "eqnarray")
32 if (type == "alignat")
34 if (type == "xalignat")
36 if (type == "xxalignat")
42 // returns position of first relation operator in the array
43 // used for "intelligent splitting"
44 MathArray::size_type firstRelOp(MathArray const & ar)
46 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
48 return it - ar.begin();
53 char const * star(bool numbered)
55 return numbered ? "" : "*";
59 int typecode(string const & s)
61 if (s == "none") return 0;
62 if (s == "simple") return 1;
63 if (s == "equation") return 2;
64 if (s == "eqnarray") return 3;
65 if (s == "align") return 4;
66 if (s == "alignat") return 5;
67 if (s == "xalignat") return 6;
68 if (s == "xxalignat") return 7;
69 if (s == "multline") return 8;
70 if (s == "gather") return 9;
71 lyxerr << "unknown hull type '" << s << "'\n";
75 bool smaller(string const & s, string const & t)
77 return typecode(s) < typecode(t);
81 } // end anon namespace
84 MathHullInset::MathHullInset()
85 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
91 MathHullInset::MathHullInset(string const & type)
92 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
98 MathHullInset::MathHullInset(string const & type, MathGridInset const & grid)
99 : MathGridInset(grid), type_(type), nonum_(1), label_(1)
105 MathHullInset::MathHullInset(string const & type, col_type cols)
106 : MathGridInset(cols, 1), type_(type), nonum_(1), label_(1)
112 MathInset * MathHullInset::clone() const
114 return new MathHullInset(*this);
118 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
126 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
129 pos = cell(idx).size();
134 char MathHullInset::defaultColAlign(col_type col)
136 if (type_ == "eqnarray")
138 if (typecode(type_) >= typecode("align"))
139 return "rl"[col & 1];
144 int MathHullInset::defaultColSpace(col_type col)
146 if (type_ == "align" || type_ == "alignat")
148 if (type_ == "xalignat")
149 return (col & 1) ? 20 : 0;
150 if (type_ == "xxalignat")
151 return (col & 1) ? 40 : 0;
156 char const * MathHullInset::standardFont() const
159 return "lyxnochange";
164 void MathHullInset::metrics(MathMetricsInfo & mi) const
166 MathFontSetChanger dummy(mi.base, standardFont());
168 // let the cells adjust themselves
169 MathGridInset::metrics(mi);
176 if (numberedType()) {
177 MathFontSetChanger dummy(mi.base, "mathbf");
179 for (row_type row = 0; row < nrows(); ++row)
180 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
186 // make it at least as high as the current font
189 math_font_max_dim(mi.base.font, asc, des);
190 ascent_ = max(ascent_, asc);
191 descent_ = max(descent_, des);
200 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
202 MathFontSetChanger dummy(pi.base, standardFont());
203 MathGridInset::draw(pi, x + 1, y);
205 if (numberedType()) {
206 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
207 for (row_type row = 0; row < nrows(); ++row) {
208 int const yy = y + rowinfo_[row].offset_;
209 MathFontSetChanger dummy(pi.base, "mathrm");
210 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
214 drawMarkers2(pi, x, y);
218 void MathHullInset::metricsT(TextMetricsInfo const &) const
222 MathGridInset::metricsT(mi);
227 WriteStream wi(os, false, true);
229 width_ = os.str().size();
236 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
240 MathGridInset::drawT(pain, x, y);
245 WriteStream wi(os, false, true);
247 pain.draw(x, y, os.str().c_str());
252 string MathHullInset::label(row_type row) const
254 row_type n = nrows();
255 lyx::Assert(row < n);
260 void MathHullInset::label(row_type row, string const & label)
262 lyxerr << "setting label '" << label << "' for row " << row << endl;
267 void MathHullInset::numbered(row_type row, bool num)
273 bool MathHullInset::numbered(row_type row) const
279 bool MathHullInset::ams() const
283 type_ == "multline" ||
285 type_ == "alignat" ||
286 type_ == "xalignat" ||
287 type_ == "xxalignat";
291 bool MathHullInset::display() const
293 return type_ != "simple" && type_ != "none";
297 vector<string> MathHullInset::getLabelList() const
300 for (row_type row = 0; row < nrows(); ++row)
301 if (!label_[row].empty() && nonum_[row] != 1)
302 res.push_back(label_[row]);
307 bool MathHullInset::numberedType() const
311 if (type_ == "simple")
313 if (type_ == "xxalignat")
315 for (row_type row = 0; row < nrows(); ++row)
322 void MathHullInset::validate(LaTeXFeatures & features) const
325 features.require("amsmath");
328 // Validation is necessary only if not using AMS math.
329 // To be safe, we will always run mathedvalidate.
330 //if (features.amsstyle)
333 features.require("boldsymbol");
334 //features.binom = true;
336 MathNestInset::validate(features);
340 void MathHullInset::header_write(WriteStream & os) const
342 bool n = numberedType();
347 else if (type_ == "simple") {
353 else if (type_ == "equation") {
355 os << "\\begin{equation" << star(n) << "}\n";
360 else if (type_ == "eqnarray" || type_ == "align")
361 os << "\\begin{" << type_ << star(n) << "}\n";
363 else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat")
364 os << "\\begin{" << type_ << star(n) << "}"
365 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
367 else if (type_ == "multline" || type_ == "gather")
368 os << "\\begin{" << type_ << "}\n";
371 os << "\\begin{unknown" << star(n) << "}";
375 void MathHullInset::footer_write(WriteStream & os) const
377 bool n = numberedType();
382 else if (type_ == "simple")
385 else if (type_ == "equation")
387 os << "\\end{equation" << star(n) << "}\n";
391 else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
392 || type_ == "xalignat")
393 os << "\n\\end{" << type_ << star(n) << "}\n";
395 else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
396 os << "\n\\end{" << type_ << "}\n";
399 os << "\\end{unknown" << star(n) << "}";
403 void MathHullInset::addRow(row_type row)
405 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
406 label_.insert(label_.begin() + row + 1, string());
407 MathGridInset::addRow(row);
411 void MathHullInset::delRow(row_type row)
413 MathGridInset::delRow(row);
414 nonum_.erase(nonum_.begin() + row);
415 label_.erase(label_.begin() + row);
419 void MathHullInset::addFancyCol(col_type col)
421 if (type_ == "equation")
424 else if (type_ == "eqnarray") {
429 else if (type_ == "align") {
434 else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
435 MathGridInset::addCol(col);
436 MathGridInset::addCol(col + 1);
441 void MathHullInset::delFancyCol(col_type col)
443 if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
444 MathGridInset::delCol(col + 1);
445 MathGridInset::delCol(col);
450 string MathHullInset::nicelabel(row_type row) const
454 if (label_[row].empty())
455 return string("(#)");
456 return "(" + label_[row] + ")";
460 void MathHullInset::glueall()
463 for (idx_type i = 0; i < nargs(); ++i)
464 ar.push_back(cell(i));
465 *this = MathHullInset("simple");
471 string const & MathHullInset::getType() const
477 void MathHullInset::setType(string const & type)
485 void MathHullInset::mutate(string const & newtype)
487 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
489 // we try to move along the chain
490 // none <-> simple <-> equation <-> eqnarray
492 if (newtype == "dump") {
496 else if (newtype == type_) {
500 else if (type_ == "none") {
506 else if (type_ == "simple") {
507 if (newtype == "none") {
516 else if (type_ == "equation") {
517 if (smaller(newtype, type_)) {
520 } else if (newtype == "eqnarray") {
521 MathGridInset::addCol(1);
522 MathGridInset::addCol(1);
524 // split it "nicely" on the firest relop
525 pos_type pos = firstRelOp(cell(0));
526 cell(1) = MathArray(cell(0), pos, cell(0).size());
527 cell(0).erase(pos, cell(0).size());
529 if (cell(1).size()) {
530 cell(2) = MathArray(cell(1), 1, cell(1).size());
531 cell(1).erase(1, cell(1).size());
536 MathGridInset::addCol(1);
538 pos_type pos = firstRelOp(cell(0));
540 cell(0).erase(pos, cell(0).size());
541 cell(1).erase(0, pos);
547 else if (type_ == "eqnarray") {
548 if (smaller(newtype, type_)) {
549 // set correct (no)numbering
550 bool allnonum = true;
551 for (row_type row = 0; row < nrows(); ++row)
555 // set first non-empty label
557 for (row_type row = 0; row < nrows(); ++row) {
558 if (!label_[row].empty()) {
565 nonum_[0] = allnonum;
568 } else { // align & Co.
569 for (row_type row = 0; row < nrows(); ++row) {
570 idx_type c = 3 * row + 1;
571 cell(c).push_back(cell(c + 1));
573 MathGridInset::delCol(2);
579 else if (type_ == "align") {
580 if (smaller(newtype, type_)) {
581 MathGridInset::addCol(1);
589 else if (type_ == "multline") {
590 if (newtype == "gather") {
593 lyxerr << "mutation from '" << type_
594 << "' to '" << newtype << "' not implemented"
599 else if (type_ == "gather") {
600 if (newtype == "multline") {
603 lyxerr << "mutation from '" << type_
604 << "' to '" << newtype << "' not implemented" << endl;
609 lyxerr << "mutation from '" << type_
610 << "' to '" << newtype << "' not implemented" << endl;
615 void MathHullInset::write(WriteStream & os) const
619 bool n = numberedType();
621 for (row_type row = 0; row < nrows(); ++row) {
622 for (col_type col = 0; col < ncols(); ++col)
623 os << cell(index(row, col)) << eocString(col);
625 if (!label_[row].empty())
626 os << "\\label{" << label_[row] << "}";
630 os << eolString(row);
637 void MathHullInset::normalize(NormalStream & os) const
639 os << "[formula " << type_ << " ";
640 MathGridInset::normalize(os);
645 void MathHullInset::mathmlize(MathMLStream & os) const
647 MathGridInset::mathmlize(os);
651 void MathHullInset::infoize(std::ostream & os) const
653 os << "Type: " << type_;
657 void MathHullInset::check() const
659 lyx::Assert(nonum_.size() == nrows());
660 lyx::Assert(label_.size() == nrows());