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 dim_.a = max(dim_.a, asc);
191 dim_.d = max(dim_.d, des);
198 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
200 MathFontSetChanger dummy(pi.base, standardFont());
201 MathGridInset::draw(pi, x + 1, y);
203 if (numberedType()) {
204 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
205 for (row_type row = 0; row < nrows(); ++row) {
206 int const yy = y + rowinfo_[row].offset_;
207 MathFontSetChanger dummy(pi.base, "mathrm");
208 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
212 drawMarkers2(pi, x, y);
216 void MathHullInset::metricsT(TextMetricsInfo const & mi) const
219 MathGridInset::metricsT(mi);
222 WriteStream wi(os, false, true);
224 dim_.w = os.str().size();
231 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
234 MathGridInset::drawT(pain, x, y);
237 WriteStream wi(os, false, true);
239 pain.draw(x, y, os.str().c_str());
244 string MathHullInset::label(row_type row) const
246 row_type n = nrows();
247 lyx::Assert(row < n);
252 void MathHullInset::label(row_type row, string const & label)
254 lyxerr << "setting label '" << label << "' for row " << row << endl;
259 void MathHullInset::numbered(row_type row, bool num)
265 bool MathHullInset::numbered(row_type row) const
271 bool MathHullInset::ams() const
275 type_ == "multline" ||
277 type_ == "alignat" ||
278 type_ == "xalignat" ||
279 type_ == "xxalignat";
283 bool MathHullInset::display() const
285 return type_ != "simple" && type_ != "none";
289 vector<string> MathHullInset::getLabelList() const
292 for (row_type row = 0; row < nrows(); ++row)
293 if (!label_[row].empty() && nonum_[row] != 1)
294 res.push_back(label_[row]);
299 bool MathHullInset::numberedType() const
303 if (type_ == "simple")
305 if (type_ == "xxalignat")
307 for (row_type row = 0; row < nrows(); ++row)
314 void MathHullInset::validate(LaTeXFeatures & features) const
317 features.require("amsmath");
320 // Validation is necessary only if not using AMS math.
321 // To be safe, we will always run mathedvalidate.
322 //if (features.amsstyle)
325 features.require("boldsymbol");
326 //features.binom = true;
328 MathNestInset::validate(features);
332 void MathHullInset::header_write(WriteStream & os) const
334 bool n = numberedType();
339 else if (type_ == "simple") {
345 else if (type_ == "equation") {
347 os << "\\begin{equation" << star(n) << "}\n";
352 else if (type_ == "eqnarray" || type_ == "align")
353 os << "\\begin{" << type_ << star(n) << "}\n";
355 else if (type_ == "alignat" || type_ == "xalignat")
356 os << "\\begin{" << type_ << star(n) << "}"
357 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
359 else if (type_ == "xxalignat")
360 os << "\\begin{" << type_ << "}"
361 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
363 else if (type_ == "multline" || type_ == "gather")
364 os << "\\begin{" << type_ << "}\n";
367 os << "\\begin{unknown" << star(n) << "}";
371 void MathHullInset::footer_write(WriteStream & os) const
373 bool n = numberedType();
378 else if (type_ == "simple")
381 else if (type_ == "equation")
383 os << "\\end{equation" << star(n) << "}\n";
387 else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
388 || type_ == "xalignat")
389 os << "\n\\end{" << type_ << star(n) << "}\n";
391 else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
392 os << "\n\\end{" << type_ << "}\n";
395 os << "\\end{unknown" << star(n) << "}";
399 void MathHullInset::addRow(row_type row)
401 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
402 label_.insert(label_.begin() + row + 1, string());
403 MathGridInset::addRow(row);
407 void MathHullInset::delRow(row_type row)
409 MathGridInset::delRow(row);
410 nonum_.erase(nonum_.begin() + row);
411 label_.erase(label_.begin() + row);
415 void MathHullInset::addFancyCol(col_type col)
417 if (type_ == "equation")
420 else if (type_ == "eqnarray") {
425 else if (type_ == "align") {
430 else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
431 MathGridInset::addCol(col);
432 MathGridInset::addCol(col + 1);
437 void MathHullInset::delFancyCol(col_type col)
439 if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
440 MathGridInset::delCol(col + 1);
441 MathGridInset::delCol(col);
446 string MathHullInset::nicelabel(row_type row) const
450 if (label_[row].empty())
451 return string("(#)");
452 return "(" + label_[row] + ")";
456 void MathHullInset::glueall()
459 for (idx_type i = 0; i < nargs(); ++i)
460 ar.push_back(cell(i));
461 *this = MathHullInset("simple");
467 string const & MathHullInset::getType() const
473 void MathHullInset::setType(string const & type)
481 void MathHullInset::mutate(string const & newtype)
483 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
485 // we try to move along the chain
486 // none <-> simple <-> equation <-> eqnarray
488 if (newtype == "dump") {
492 else if (newtype == type_) {
496 else if (type_ == "none") {
502 else if (type_ == "simple") {
503 if (newtype == "none") {
512 else if (type_ == "equation") {
513 if (smaller(newtype, type_)) {
516 } else if (newtype == "eqnarray") {
517 MathGridInset::addCol(1);
518 MathGridInset::addCol(1);
520 // split it "nicely" on the firest relop
521 pos_type pos = firstRelOp(cell(0));
522 cell(1) = MathArray(cell(0), pos, cell(0).size());
523 cell(0).erase(pos, cell(0).size());
525 if (cell(1).size()) {
526 cell(2) = MathArray(cell(1), 1, cell(1).size());
527 cell(1).erase(1, cell(1).size());
532 MathGridInset::addCol(1);
534 pos_type pos = firstRelOp(cell(0));
536 cell(0).erase(pos, cell(0).size());
537 cell(1).erase(0, pos);
543 else if (type_ == "eqnarray") {
544 if (smaller(newtype, type_)) {
545 // set correct (no)numbering
546 bool allnonum = true;
547 for (row_type row = 0; row < nrows(); ++row)
551 // set first non-empty label
553 for (row_type row = 0; row < nrows(); ++row) {
554 if (!label_[row].empty()) {
561 nonum_[0] = allnonum;
564 } else { // align & Co.
565 for (row_type row = 0; row < nrows(); ++row) {
566 idx_type c = 3 * row + 1;
567 cell(c).push_back(cell(c + 1));
569 MathGridInset::delCol(2);
575 else if (type_ == "align") {
576 if (smaller(newtype, type_)) {
577 MathGridInset::addCol(1);
585 else if (type_ == "multline") {
586 if (newtype == "gather") {
589 lyxerr << "mutation from '" << type_
590 << "' to '" << newtype << "' not implemented"
595 else if (type_ == "gather") {
596 if (newtype == "multline") {
599 lyxerr << "mutation from '" << type_
600 << "' to '" << newtype << "' not implemented" << endl;
605 lyxerr << "mutation from '" << type_
606 << "' to '" << newtype << "' not implemented" << endl;
611 void MathHullInset::write(WriteStream & os) const
615 bool n = numberedType();
617 for (row_type row = 0; row < nrows(); ++row) {
618 for (col_type col = 0; col < ncols(); ++col)
619 os << cell(index(row, col)) << eocString(col);
621 if (!label_[row].empty())
622 os << "\\label{" << label_[row] << "}";
626 os << eolString(row);
633 void MathHullInset::normalize(NormalStream & os) const
635 os << "[formula " << type_ << " ";
636 MathGridInset::normalize(os);
641 void MathHullInset::mathmlize(MathMLStream & os) const
643 MathGridInset::mathmlize(os);
647 void MathHullInset::infoize(std::ostream & os) const
649 os << "Type: " << type_;
653 void MathHullInset::check() const
655 lyx::Assert(nonum_.size() == nrows());
656 lyx::Assert(label_.size() == nrows());