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)
106 MathInset * MathHullInset::clone() const
108 return new MathHullInset(*this);
112 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
120 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
123 pos = cell(idx).size();
128 char MathHullInset::defaultColAlign(col_type col)
130 if (type_ == "eqnarray")
132 if (typecode(type_) >= typecode("align"))
133 return "rl"[col & 1];
138 int MathHullInset::defaultColSpace(col_type col)
140 if (type_ == "align" || type_ == "alignat")
142 if (type_ == "xalignat")
143 return (col & 1) ? 20 : 0;
144 if (type_ == "xxalignat")
145 return (col & 1) ? 40 : 0;
150 char const * MathHullInset::standardFont() const
153 return "lyxnochange";
158 void MathHullInset::metrics(MathMetricsInfo & mi) const
160 MathFontSetChanger dummy(mi.base, standardFont());
162 // let the cells adjust themselves
163 MathGridInset::metrics(mi);
170 if (numberedType()) {
171 MathFontSetChanger dummy(mi.base, "mathbf");
173 for (row_type row = 0; row < nrows(); ++row)
174 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
180 // make it at least as high as the current font
183 math_font_max_dim(mi.base.font, asc, des);
184 dim_.a = max(dim_.a, asc);
185 dim_.d = max(dim_.d, des);
192 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
194 MathFontSetChanger dummy(pi.base, standardFont());
195 MathGridInset::draw(pi, x + 1, y);
197 if (numberedType()) {
198 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
199 for (row_type row = 0; row < nrows(); ++row) {
200 int const yy = y + rowinfo_[row].offset_;
201 MathFontSetChanger dummy(pi.base, "mathrm");
202 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
206 drawMarkers2(pi, x, y);
210 void MathHullInset::metricsT(TextMetricsInfo const & mi) const
213 MathGridInset::metricsT(mi);
216 WriteStream wi(os, false, true);
218 dim_.w = os.str().size();
225 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
228 MathGridInset::drawT(pain, x, y);
231 WriteStream wi(os, false, true);
233 pain.draw(x, y, os.str().c_str());
238 string MathHullInset::label(row_type row) const
240 row_type n = nrows();
241 lyx::Assert(row < n);
246 void MathHullInset::label(row_type row, string const & label)
248 lyxerr << "setting label '" << label << "' for row " << row << endl;
253 void MathHullInset::numbered(row_type row, bool num)
259 bool MathHullInset::numbered(row_type row) const
265 bool MathHullInset::ams() const
269 type_ == "multline" ||
271 type_ == "alignat" ||
272 type_ == "xalignat" ||
273 type_ == "xxalignat";
277 bool MathHullInset::display() const
279 return type_ != "simple" && type_ != "none";
283 vector<string> MathHullInset::getLabelList() const
286 for (row_type row = 0; row < nrows(); ++row)
287 if (!label_[row].empty() && nonum_[row] != 1)
288 res.push_back(label_[row]);
293 bool MathHullInset::numberedType() const
297 if (type_ == "simple")
299 if (type_ == "xxalignat")
301 for (row_type row = 0; row < nrows(); ++row)
308 void MathHullInset::validate(LaTeXFeatures & features) const
311 features.require("amsmath");
314 // Validation is necessary only if not using AMS math.
315 // To be safe, we will always run mathedvalidate.
316 //if (features.amsstyle)
319 features.require("boldsymbol");
320 //features.binom = true;
322 MathNestInset::validate(features);
326 void MathHullInset::header_write(WriteStream & os) const
328 bool n = numberedType();
333 else if (type_ == "simple") {
339 else if (type_ == "equation") {
341 os << "\\begin{equation" << star(n) << "}\n";
346 else if (type_ == "eqnarray" || type_ == "align")
347 os << "\\begin{" << type_ << star(n) << "}\n";
349 else if (type_ == "alignat" || type_ == "xalignat")
350 os << "\\begin{" << type_ << star(n) << "}"
351 << "{" << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
353 else if (type_ == "xxalignat")
354 os << "\\begin{" << type_ << "}"
355 << "{" << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
357 else if (type_ == "multline" || type_ == "gather")
358 os << "\\begin{" << type_ << "}\n";
361 os << "\\begin{unknown" << star(n) << "}";
365 void MathHullInset::footer_write(WriteStream & os) const
367 bool n = numberedType();
372 else if (type_ == "simple")
375 else if (type_ == "equation")
377 os << "\\end{equation" << star(n) << "}\n";
381 else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
382 || type_ == "xalignat")
383 os << "\n\\end{" << type_ << star(n) << "}\n";
385 else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
386 os << "\n\\end{" << type_ << "}\n";
389 os << "\\end{unknown" << star(n) << "}";
393 void MathHullInset::addRow(row_type row)
395 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
396 label_.insert(label_.begin() + row + 1, string());
397 MathGridInset::addRow(row);
401 void MathHullInset::delRow(row_type row)
403 MathGridInset::delRow(row);
404 nonum_.erase(nonum_.begin() + row);
405 label_.erase(label_.begin() + row);
409 void MathHullInset::addFancyCol(col_type col)
411 if (type_ == "equation")
414 else if (type_ == "eqnarray") {
419 else if (type_ == "align") {
424 else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat")
425 MathGridInset::addCol(col);
429 void MathHullInset::delFancyCol(col_type col)
431 if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat")
432 MathGridInset::delCol(col);
436 string MathHullInset::nicelabel(row_type row) const
440 if (label_[row].empty())
441 return string("(#)");
442 return "(" + label_[row] + ")";
446 void MathHullInset::glueall()
449 for (idx_type i = 0; i < nargs(); ++i)
450 ar.push_back(cell(i));
451 *this = MathHullInset("simple");
457 string const & MathHullInset::getType() const
463 void MathHullInset::setType(string const & type)
471 void MathHullInset::mutate(string const & newtype)
473 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
475 // we try to move along the chain
476 // none <-> simple <-> equation <-> eqnarray
478 if (newtype == "dump") {
482 else if (newtype == type_) {
486 else if (type_ == "none") {
492 else if (type_ == "simple") {
493 if (newtype == "none") {
502 else if (type_ == "equation") {
503 if (smaller(newtype, type_)) {
506 } else if (newtype == "eqnarray") {
507 MathGridInset::addCol(1);
508 MathGridInset::addCol(1);
510 // split it "nicely" on the firest relop
511 pos_type pos = firstRelOp(cell(0));
512 cell(1) = MathArray(cell(0), pos, cell(0).size());
513 cell(0).erase(pos, cell(0).size());
515 if (cell(1).size()) {
516 cell(2) = MathArray(cell(1), 1, cell(1).size());
517 cell(1).erase(1, cell(1).size());
522 MathGridInset::addCol(1);
524 pos_type pos = firstRelOp(cell(0));
526 cell(0).erase(pos, cell(0).size());
527 cell(1).erase(0, pos);
533 else if (type_ == "eqnarray") {
534 if (smaller(newtype, type_)) {
535 // set correct (no)numbering
536 bool allnonum = true;
537 for (row_type row = 0; row < nrows(); ++row)
541 // set first non-empty label
543 for (row_type row = 0; row < nrows(); ++row) {
544 if (!label_[row].empty()) {
551 nonum_[0] = allnonum;
554 } else { // align & Co.
555 for (row_type row = 0; row < nrows(); ++row) {
556 idx_type c = 3 * row + 1;
557 cell(c).push_back(cell(c + 1));
559 MathGridInset::delCol(2);
565 else if (type_ == "align") {
566 if (smaller(newtype, type_)) {
567 MathGridInset::addCol(1);
575 else if (type_ == "multline") {
576 if (newtype == "gather") {
579 lyxerr << "mutation from '" << type_
580 << "' to '" << newtype << "' not implemented"
585 else if (type_ == "gather") {
586 if (newtype == "multline") {
589 lyxerr << "mutation from '" << type_
590 << "' to '" << newtype << "' not implemented" << endl;
595 lyxerr << "mutation from '" << type_
596 << "' to '" << newtype << "' not implemented" << endl;
601 void MathHullInset::write(WriteStream & os) const
605 bool n = numberedType();
607 for (row_type row = 0; row < nrows(); ++row) {
608 for (col_type col = 0; col < ncols(); ++col)
609 os << cell(index(row, col)) << eocString(col);
611 if (!label_[row].empty())
612 os << "\\label{" << label_[row] << "}";
616 os << eolString(row);
623 void MathHullInset::normalize(NormalStream & os) const
625 os << "[formula " << type_ << " ";
626 MathGridInset::normalize(os);
631 void MathHullInset::mathmlize(MathMLStream & os) const
633 MathGridInset::mathmlize(os);
637 void MathHullInset::infoize(std::ostream & os) const
639 os << "Type: " << type_;
643 void MathHullInset::check() const
645 lyx::Assert(nonum_.size() == nrows());
646 lyx::Assert(label_.size() == nrows());