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, MathGridInset const & grid)
104 : MathGridInset(grid), objtype_(t), nonum_(1), label_(1)
110 MathHullInset::MathHullInset(MathInsetTypes t, col_type cols)
111 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
117 MathInset * MathHullInset::clone() const
119 return new MathHullInset(*this);
123 char MathHullInset::defaultColAlign(col_type col)
129 case LM_OT_XXALIGNAT:
130 return "rl"[col & 1];
139 int MathHullInset::defaultColSpace(col_type col)
146 return (col & 1) ? 20 : 0;
147 case LM_OT_XXALIGNAT:
148 return (col & 1) ? 40 : 0;
155 void MathHullInset::metrics(MathMetricsInfo const & mi) const
158 mi_.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
160 // let the cells adjust themselves
161 MathGridInset::metrics(mi_);
168 if (numberedType()) {
170 for (row_type row = 0; row < nrows(); ++row)
171 l = std::max(l, mathed_string_width(LM_TC_BF, mi_, nicelabel(row)));
177 // make it at least as high as the current font
180 math_font_max_dim(LM_TC_TEXTRM, mi_, asc, des);
181 ascent_ = std::max(ascent_, asc);
182 descent_ = std::max(descent_, des);
186 void MathHullInset::draw(Painter & pain, int x, int y) const
188 MathGridInset::draw(pain, x, y);
190 if (numberedType()) {
191 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
192 for (row_type row = 0; row < nrows(); ++row) {
193 int const yy = y + rowinfo_[row].offset_;
194 drawStr(pain, LM_TC_BF, mi_, xx, yy, nicelabel(row));
200 string MathHullInset::label(row_type row) const
202 row_type n = nrows();
203 lyx::Assert(row < n);
208 void MathHullInset::label(row_type row, string const & label)
214 void MathHullInset::numbered(row_type row, bool num)
220 bool MathHullInset::numbered(row_type row) const
226 bool MathHullInset::ams() const
231 objtype_ == LM_OT_ALIGN ||
232 objtype_ == LM_OT_MULTLINE ||
233 objtype_ == LM_OT_GATHER ||
234 objtype_ == LM_OT_ALIGNAT ||
235 objtype_ == LM_OT_XALIGNAT ||
236 objtype_ == LM_OT_XXALIGNAT;
241 bool MathHullInset::display() const
243 return getType() != LM_OT_SIMPLE;
247 std::vector<string> const MathHullInset::getLabelList() const
249 std::vector<string> res;
250 for (row_type row = 0; row < nrows(); ++row)
251 if (!label_[row].empty() && nonum_[row] != 1)
252 res.push_back(label_[row]);
257 bool MathHullInset::numberedType() const
259 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
261 for (row_type row = 0; row < nrows(); ++row)
268 void MathHullInset::validate(LaTeXFeatures & features) const
271 features.require("amsmath");
274 // Validation is necessary only if not using AMS math.
275 // To be safe, we will always run mathedvalidate.
276 //if (features.amsstyle)
279 features.require("boldsymbol");
280 //features.binom = true;
282 MathNestInset::validate(features);
286 void MathHullInset::header_write(WriteStream & os) const
288 bool n = numberedType();
299 os << "\\begin{equation" << star(n) << "}\n";
305 os << "\\begin{eqnarray" << star(n) << "}\n";
309 os << "\\begin{align" << star(n) << "}\n";
313 os << "\\begin{alignat" << star(n) << "}"
314 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
318 os << "\\begin{xalignat" << star(n) << "}"
319 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
322 case LM_OT_XXALIGNAT:
323 os << "\\begin{xxalignat}"
324 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
328 os << "\\begin{multline}\n";
332 os << "\\begin{gather}\n";
336 os << "\\begin{unknown" << star(n) << "}";
341 void MathHullInset::footer_write(WriteStream & os) const
343 bool n = numberedType();
352 os << "\\end{equation" << star(n) << "}\n";
358 os << "\n\\end{eqnarray" << star(n) << "}\n";
362 os << "\n\\end{align" << star(n) << "}\n";
366 os << "\n\\end{alignat" << star(n) << "}\n";
370 os << "\n\\end{xalignat" << star(n) << "}\n";
373 case LM_OT_XXALIGNAT:
374 os << "\n\\end{xxalignat}\n";
378 os << "\n\\end{multline}\n";
382 os << "\n\\end{gather}\n";
386 os << "\\end{unknown" << star(n) << "}";
391 void MathHullInset::addRow(row_type row)
393 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
394 label_.insert(label_.begin() + row + 1, string());
395 MathGridInset::addRow(row);
399 void MathHullInset::appendRow()
401 nonum_.push_back(!numberedType());
402 label_.push_back(string());
403 MathGridInset::appendRow();
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::addCol(col_type col)
419 mutate(LM_OT_EQNARRAY);
428 mutate(LM_OT_ALIGNAT);
434 case LM_OT_XXALIGNAT:
435 MathGridInset::addCol(col);
436 MathGridInset::addCol(col + 1);
445 void MathHullInset::delCol(col_type col)
450 case LM_OT_XXALIGNAT:
451 MathGridInset::delCol(col + 1);
452 MathGridInset::delCol(col);
460 string MathHullInset::nicelabel(row_type row) const
464 if (label_[row].empty())
465 return string("(#)");
466 return "(" + label_[row] + ")";
470 void MathHullInset::mutate(string const & newtype)
472 if (newtype == "dump") {
476 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
477 mutate(typecode(newtype));
481 void MathHullInset::glueall()
484 for (idx_type i = 0; i < nargs(); ++i)
485 ar.push_back(cell(i));
486 *this = MathHullInset(LM_OT_SIMPLE);
491 MathInsetTypes MathHullInset::getType() const
497 void MathHullInset::setType(MathInsetTypes t)
505 void MathHullInset::mutate(MathInsetTypes newtype)
507 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
509 if (newtype == getType())
514 setType(LM_OT_EQUATION);
522 setType(LM_OT_SIMPLE);
528 case LM_OT_XXALIGNAT: {
530 MathGridInset::addCol(1);
533 pos_type pos = firstRelOp(cell(0));
535 cell(0).erase(pos, cell(0).size());
536 cell(1).erase(0, pos);
537 setType(LM_OT_ALIGN);
544 MathGridInset::addCol(1);
545 MathGridInset::addCol(1);
547 // split it "nicely" on the firest relop
548 pos_type pos = firstRelOp(cell(0));
549 cell(1) = MathArray(cell(0), pos, cell(0).size());
550 cell(0).erase(pos, cell(0).size());
552 if (cell(1).size()) {
553 cell(2) = MathArray(cell(1), 1, cell(1).size());
554 cell(1).erase(1, cell(1).size());
557 setType(LM_OT_EQNARRAY);
566 case LM_OT_EQUATION: {
567 // set correct (no)numbering
568 bool allnonum = true;
569 for (row_type row = 0; row < nrows(); ++row) {
574 // set first non-empty label
576 for (row_type row = 0; row < nrows(); ++row) {
577 if (!label_[row].empty()) {
585 nonum_[0] = allnonum;
594 case LM_OT_XXALIGNAT:
596 for (row_type row = 0; row < nrows(); ++row) {
597 idx_type c = 3 * row + 1;
598 cell(c).push_back(cell(c + 1));
600 MathGridInset::delCol(2);
601 setType(LM_OT_ALIGN);
613 MathGridInset::addCol(1);
614 setType(LM_OT_EQNARRAY);
620 case LM_OT_XXALIGNAT:
625 lyxerr << "mutation from '" << getType()
626 << "' to '" << newtype << "' not implemented"
635 setType(LM_OT_GATHER);
638 lyxerr << "mutation from '" << getType()
639 << "' to '" << newtype << "' not implemented"
647 setType(LM_OT_MULTLINE);
650 lyxerr << "mutation from '" << getType()
651 << "' to '" << newtype << "' not implemented"
657 lyxerr << "mutation from '" << getType()
658 << "' to '" << newtype << "' not implemented"
665 void MathHullInset::write(WriteStream & os) const
669 bool n = numberedType();
671 for (row_type row = 0; row < nrows(); ++row) {
672 for (col_type col = 0; col < ncols(); ++col)
673 os << cell(index(row, col)) << eocString(col);
675 if (!label_[row].empty())
676 os << "\\label{" << label_[row] << "}";
680 os << eolString(row);
687 void MathHullInset::normalize(NormalStream & os) const
689 os << "[formula " << normalName(getType()) << " ";
690 MathGridInset::normalize(os);
695 void MathHullInset::mathmlize(MathMLStream & os) const
697 MathGridInset::mathmlize(os);
701 void MathHullInset::check() const
703 lyx::Assert(nonum_.size() == nrows());
704 lyx::Assert(label_.size() == nrows());