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" || type_ == "multline" || type_ == "gather")
360 os << "\\begin{" << type_ << "}\n";
363 os << "\\begin{unknown" << star(n) << "}";
367 void MathHullInset::footer_write(WriteStream & os) const
369 bool n = numberedType();
374 else if (type_ == "simple")
377 else if (type_ == "equation")
379 os << "\\end{equation" << star(n) << "}\n";
383 else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
384 || type_ == "xalignat")
385 os << "\n\\end{" << type_ << star(n) << "}\n";
387 else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
388 os << "\n\\end{" << type_ << "}\n";
391 os << "\\end{unknown" << star(n) << "}";
395 void MathHullInset::addRow(row_type row)
397 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
398 label_.insert(label_.begin() + row + 1, string());
399 MathGridInset::addRow(row);
403 void MathHullInset::delRow(row_type row)
405 MathGridInset::delRow(row);
406 nonum_.erase(nonum_.begin() + row);
407 label_.erase(label_.begin() + row);
411 void MathHullInset::addFancyCol(col_type col)
413 if (type_ == "equation")
416 else if (type_ == "eqnarray") {
421 else if (type_ == "align") {
426 else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
427 MathGridInset::addCol(col);
428 MathGridInset::addCol(col + 1);
433 void MathHullInset::delFancyCol(col_type col)
435 if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
436 MathGridInset::delCol(col + 1);
437 MathGridInset::delCol(col);
442 string MathHullInset::nicelabel(row_type row) const
446 if (label_[row].empty())
447 return string("(#)");
448 return "(" + label_[row] + ")";
452 void MathHullInset::glueall()
455 for (idx_type i = 0; i < nargs(); ++i)
456 ar.push_back(cell(i));
457 *this = MathHullInset("simple");
463 string const & MathHullInset::getType() const
469 void MathHullInset::setType(string const & type)
477 void MathHullInset::mutate(string const & newtype)
479 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
481 // we try to move along the chain
482 // none <-> simple <-> equation <-> eqnarray
484 if (newtype == "dump") {
488 else if (newtype == type_) {
492 else if (type_ == "none") {
498 else if (type_ == "simple") {
499 if (newtype == "none") {
508 else if (type_ == "equation") {
509 if (smaller(newtype, type_)) {
512 } else if (newtype == "eqnarray") {
513 MathGridInset::addCol(1);
514 MathGridInset::addCol(1);
516 // split it "nicely" on the firest relop
517 pos_type pos = firstRelOp(cell(0));
518 cell(1) = MathArray(cell(0), pos, cell(0).size());
519 cell(0).erase(pos, cell(0).size());
521 if (cell(1).size()) {
522 cell(2) = MathArray(cell(1), 1, cell(1).size());
523 cell(1).erase(1, cell(1).size());
528 MathGridInset::addCol(1);
530 pos_type pos = firstRelOp(cell(0));
532 cell(0).erase(pos, cell(0).size());
533 cell(1).erase(0, pos);
539 else if (type_ == "eqnarray") {
540 if (smaller(newtype, type_)) {
541 // set correct (no)numbering
542 bool allnonum = true;
543 for (row_type row = 0; row < nrows(); ++row)
547 // set first non-empty label
549 for (row_type row = 0; row < nrows(); ++row) {
550 if (!label_[row].empty()) {
557 nonum_[0] = allnonum;
560 } else { // align & Co.
561 for (row_type row = 0; row < nrows(); ++row) {
562 idx_type c = 3 * row + 1;
563 cell(c).push_back(cell(c + 1));
565 MathGridInset::delCol(2);
571 else if (type_ == "align") {
572 if (smaller(newtype, type_)) {
573 MathGridInset::addCol(1);
581 else if (type_ == "multline") {
582 if (newtype == "gather") {
585 lyxerr << "mutation from '" << type_
586 << "' to '" << newtype << "' not implemented"
591 else if (type_ == "gather") {
592 if (newtype == "multline") {
595 lyxerr << "mutation from '" << type_
596 << "' to '" << newtype << "' not implemented" << endl;
601 lyxerr << "mutation from '" << type_
602 << "' to '" << newtype << "' not implemented" << endl;
607 void MathHullInset::write(WriteStream & os) const
611 bool n = numberedType();
613 for (row_type row = 0; row < nrows(); ++row) {
614 for (col_type col = 0; col < ncols(); ++col)
615 os << cell(index(row, col)) << eocString(col);
617 if (!label_[row].empty())
618 os << "\\label{" << label_[row] << "}";
622 os << eolString(row);
629 void MathHullInset::normalize(NormalStream & os) const
631 os << "[formula " << type_ << " ";
632 MathGridInset::normalize(os);
637 void MathHullInset::mathmlize(MathMLStream & os) const
639 MathGridInset::mathmlize(os);
643 void MathHullInset::infoize(std::ostream & os) const
645 os << "Type: " << type_;
649 void MathHullInset::check() const
651 lyx::Assert(nonum_.size() == nrows());
652 lyx::Assert(label_.size() == nrows());