7 #include "math_hullinset.h"
8 #include "math_mathmlstream.h"
9 #include "math_streamstr.h"
10 #include "math_support.h"
13 #include "LaTeXFeatures.h"
14 #include "support/LAssert.h"
22 int getCols(MathInsetTypes type)
38 // returns position of first relation operator in the array
39 // used for "intelligent splitting"
40 MathArray::size_type firstRelOp(MathArray const & ar)
42 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
44 return it - ar.begin();
49 char const * star(bool numbered)
51 return numbered ? "" : "*";
54 MathInsetTypes typecode(string const & s)
56 if (s == "equation") return LM_OT_EQUATION;
57 if (s == "display") return LM_OT_EQUATION;
58 if (s == "eqnarray") return LM_OT_EQNARRAY;
59 if (s == "align") return LM_OT_ALIGN;
60 if (s == "alignat") return LM_OT_ALIGNAT;
61 if (s == "xalignat") return LM_OT_XALIGNAT;
62 if (s == "xxalignat") return LM_OT_XXALIGNAT;
63 if (s == "multline") return LM_OT_MULTLINE;
64 if (s == "gather") return LM_OT_GATHER;
69 string normalName(MathInsetTypes t)
72 case LM_OT_EQUATION: return "equation";
73 case LM_OT_EQNARRAY: return "eqnarray";
74 case LM_OT_ALIGN: return "align";
75 case LM_OT_ALIGNAT: return "alignat";
76 case LM_OT_XALIGNAT: return "xalignat";
77 case LM_OT_XXALIGNAT: return "xxalignat";
78 case LM_OT_MULTLINE: return "multline";
79 case LM_OT_GATHER: return "gather";
80 case LM_OT_SIMPLE: return "simple";
86 } // end anon namespace
89 MathHullInset::MathHullInset()
90 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
96 MathHullInset::MathHullInset(MathInsetTypes t)
97 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
103 MathHullInset::MathHullInset(MathInsetTypes t, col_type cols)
104 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
110 MathInset * MathHullInset::clone() const
112 return new MathHullInset(*this);
116 char MathHullInset::defaultColAlign(col_type col)
122 case LM_OT_XXALIGNAT:
123 return "rl"[col & 1];
132 int MathHullInset::defaultColSpace(col_type col)
139 return (col & 1) ? 20 : 0;
140 case LM_OT_XXALIGNAT:
141 return (col & 1) ? 40 : 0;
148 void MathHullInset::metrics(MathMetricsInfo const & mi) const
151 mi_.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
153 // let the cells adjust themselves
154 MathGridInset::metrics(mi_);
161 if (numberedType()) {
163 for (row_type row = 0; row < nrows(); ++row)
164 l = std::max(l, mathed_string_width(LM_TC_BF, mi_, nicelabel(row)));
170 // make it at least as high as the current font
173 math_font_max_dim(LM_TC_TEXTRM, mi_, asc, des);
174 ascent_ = std::max(ascent_, asc);
175 descent_ = std::max(descent_, des);
179 void MathHullInset::draw(Painter & pain, int x, int y) const
181 MathGridInset::draw(pain, x, y);
183 if (numberedType()) {
184 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
185 for (row_type row = 0; row < nrows(); ++row) {
186 int const yy = y + rowinfo_[row].offset_;
187 drawStr(pain, LM_TC_BF, mi_, xx, yy, nicelabel(row));
193 string MathHullInset::label(row_type row) const
195 row_type n = nrows();
196 lyx::Assert(row < n);
201 void MathHullInset::label(row_type row, string const & label)
207 void MathHullInset::numbered(row_type row, bool num)
213 bool MathHullInset::numbered(row_type row) const
219 bool MathHullInset::ams() const
224 objtype_ == LM_OT_ALIGN ||
225 objtype_ == LM_OT_MULTLINE ||
226 objtype_ == LM_OT_GATHER ||
227 objtype_ == LM_OT_ALIGNAT ||
228 objtype_ == LM_OT_XALIGNAT ||
229 objtype_ == LM_OT_XXALIGNAT;
234 bool MathHullInset::display() const
236 return getType() != LM_OT_SIMPLE;
240 std::vector<string> const MathHullInset::getLabelList() const
242 std::vector<string> res;
243 for (row_type row = 0; row < nrows(); ++row)
244 if (!label_[row].empty() && nonum_[row] != 1)
245 res.push_back(label_[row]);
250 bool MathHullInset::numberedType() const
252 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
254 for (row_type row = 0; row < nrows(); ++row)
261 void MathHullInset::validate(LaTeXFeatures & features) const
264 features.require("amsmath");
267 // Validation is necessary only if not using AMS math.
268 // To be safe, we will always run mathedvalidate.
269 //if (features.amsstyle)
272 features.require("boldsymbol");
273 //features.binom = true;
275 MathNestInset::validate(features);
279 void MathHullInset::header_write(WriteStream & os) const
281 bool n = numberedType();
292 os << "\\begin{equation" << star(n) << "}\n";
298 os << "\\begin{eqnarray" << star(n) << "}\n";
302 os << "\\begin{align" << star(n) << "}\n";
306 os << "\\begin{alignat" << star(n) << "}"
307 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
311 os << "\\begin{xalignat" << star(n) << "}"
312 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
315 case LM_OT_XXALIGNAT:
316 os << "\\begin{xxalignat}"
317 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
321 os << "\\begin{multline}\n";
325 os << "\\begin{gather}\n";
329 os << "\\begin{unknown" << star(n) << "}";
334 void MathHullInset::footer_write(WriteStream & os) const
336 bool n = numberedType();
345 os << "\\end{equation" << star(n) << "}\n";
351 os << "\n\\end{eqnarray" << star(n) << "}\n";
355 os << "\n\\end{align" << star(n) << "}\n";
359 os << "\n\\end{alignat" << star(n) << "}\n";
363 os << "\n\\end{xalignat" << star(n) << "}\n";
366 case LM_OT_XXALIGNAT:
367 os << "\n\\end{xxalignat}\n";
371 os << "\n\\end{multline}\n";
375 os << "\n\\end{gather}\n";
379 os << "\\end{unknown" << star(n) << "}";
384 void MathHullInset::addRow(row_type row)
386 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
387 label_.insert(label_.begin() + row + 1, string());
388 MathGridInset::addRow(row);
392 void MathHullInset::appendRow()
394 nonum_.push_back(!numberedType());
395 label_.push_back(string());
396 MathGridInset::appendRow();
400 void MathHullInset::delRow(row_type row)
402 MathGridInset::delRow(row);
403 nonum_.erase(nonum_.begin() + row);
404 label_.erase(label_.begin() + row);
408 void MathHullInset::addCol(col_type col)
412 mutate(LM_OT_EQNARRAY);
421 mutate(LM_OT_ALIGNAT);
427 case LM_OT_XXALIGNAT:
428 MathGridInset::addCol(col);
429 MathGridInset::addCol(col + 1);
438 void MathHullInset::delCol(col_type col)
443 case LM_OT_XXALIGNAT:
444 MathGridInset::delCol(col + 1);
445 MathGridInset::delCol(col);
453 string MathHullInset::nicelabel(row_type row) const
457 if (label_[row].empty())
458 return string("(#)");
459 return "(" + label_[row] + ")";
463 void MathHullInset::mutate(string const & newtype)
465 if (newtype == "dump") {
469 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
470 mutate(typecode(newtype));
474 void MathHullInset::glueall()
477 for (idx_type i = 0; i < nargs(); ++i)
478 ar.push_back(cell(i));
479 *this = MathHullInset(LM_OT_SIMPLE);
484 MathInsetTypes MathHullInset::getType() const
490 void MathHullInset::setType(MathInsetTypes t)
498 void MathHullInset::mutate(MathInsetTypes newtype)
500 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
502 if (newtype == getType())
507 setType(LM_OT_EQUATION);
515 setType(LM_OT_SIMPLE);
521 case LM_OT_XXALIGNAT: {
523 MathGridInset::addCol(1);
526 pos_type pos = firstRelOp(cell(0));
528 cell(0).erase(pos, cell(0).size());
529 cell(1).erase(0, pos);
530 setType(LM_OT_ALIGN);
537 MathGridInset::addCol(1);
538 MathGridInset::addCol(1);
540 // split it "nicely" on the firest relop
541 pos_type pos = firstRelOp(cell(0));
542 cell(1) = MathArray(cell(0), pos, cell(0).size());
543 cell(0).erase(pos, cell(0).size());
545 if (cell(1).size()) {
546 cell(2) = MathArray(cell(1), 1, cell(1).size());
547 cell(1).erase(1, cell(1).size());
550 setType(LM_OT_EQNARRAY);
559 case LM_OT_EQUATION: {
560 // set correct (no)numbering
561 bool allnonum = true;
562 for (row_type row = 0; row < nrows(); ++row) {
567 // set first non-empty label
569 for (row_type row = 0; row < nrows(); ++row) {
570 if (!label_[row].empty()) {
578 nonum_[0] = allnonum;
587 case LM_OT_XXALIGNAT:
589 for (row_type row = 0; row < nrows(); ++row) {
590 idx_type c = 3 * row + 1;
591 cell(c).push_back(cell(c + 1));
593 MathGridInset::delCol(2);
594 setType(LM_OT_ALIGN);
606 MathGridInset::addCol(1);
607 setType(LM_OT_EQNARRAY);
613 case LM_OT_XXALIGNAT:
618 lyxerr << "mutation from '" << getType()
619 << "' to '" << newtype << "' not implemented"
628 setType(LM_OT_GATHER);
631 lyxerr << "mutation from '" << getType()
632 << "' to '" << newtype << "' not implemented"
640 setType(LM_OT_MULTLINE);
643 lyxerr << "mutation from '" << getType()
644 << "' to '" << newtype << "' not implemented"
650 lyxerr << "mutation from '" << getType()
651 << "' to '" << newtype << "' not implemented"
658 void MathHullInset::write(WriteStream & os) const
662 bool n = numberedType();
664 for (row_type row = 0; row < nrows(); ++row) {
665 for (col_type col = 0; col < ncols(); ++col)
666 os << cell(index(row, col)) << eocString(col);
668 if (!label_[row].empty())
669 os << "\\label{" << label_[row] << "}";
673 os << eolString(row);
680 void MathHullInset::normalize(NormalStream & os) const
682 os << "[formula " << normalName(getType()) << " ";
683 MathGridInset::normalize(os);
688 void MathHullInset::mathmlize(MathMLStream & os) const
690 MathGridInset::mathmlize(os);
694 void MathHullInset::check() const
696 lyx::Assert(nonum_.size() == nrows());
697 lyx::Assert(label_.size() == nrows());