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"
24 int getCols(MathInsetTypes type)
40 // returns position of first relation operator in the array
41 // used for "intelligent splitting"
42 MathArray::size_type firstRelOp(MathArray const & ar)
44 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
46 return it - ar.begin();
51 char const * star(bool numbered)
53 return numbered ? "" : "*";
56 MathInsetTypes typecode(string const & s)
58 if (s == "equation") return LM_OT_EQUATION;
59 if (s == "display") return LM_OT_EQUATION;
60 if (s == "eqnarray") return LM_OT_EQNARRAY;
61 if (s == "align") return LM_OT_ALIGN;
62 if (s == "alignat") return LM_OT_ALIGNAT;
63 if (s == "xalignat") return LM_OT_XALIGNAT;
64 if (s == "xxalignat") return LM_OT_XXALIGNAT;
65 if (s == "multline") return LM_OT_MULTLINE;
66 if (s == "gather") return LM_OT_GATHER;
71 string normalName(MathInsetTypes t)
74 case LM_OT_EQUATION: return "equation";
75 case LM_OT_EQNARRAY: return "eqnarray";
76 case LM_OT_ALIGN: return "align";
77 case LM_OT_ALIGNAT: return "alignat";
78 case LM_OT_XALIGNAT: return "xalignat";
79 case LM_OT_XXALIGNAT: return "xxalignat";
80 case LM_OT_MULTLINE: return "multline";
81 case LM_OT_GATHER: return "gather";
82 case LM_OT_SIMPLE: return "simple";
88 } // end anon namespace
91 MathHullInset::MathHullInset()
92 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
98 MathHullInset::MathHullInset(MathInsetTypes t)
99 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
105 MathHullInset::MathHullInset(MathInsetTypes t, MathGridInset const & grid)
106 : MathGridInset(grid), objtype_(t), nonum_(1), label_(1)
112 MathHullInset::MathHullInset(MathInsetTypes t, col_type cols)
113 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
119 MathInset * MathHullInset::clone() const
121 return new MathHullInset(*this);
125 char MathHullInset::defaultColAlign(col_type col)
131 case LM_OT_XXALIGNAT:
132 return "rl"[col & 1];
141 int MathHullInset::defaultColSpace(col_type col)
148 return (col & 1) ? 20 : 0;
149 case LM_OT_XXALIGNAT:
150 return (col & 1) ? 40 : 0;
157 void MathHullInset::metrics(MathMetricsInfo const & mi) const
160 mi_.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
162 // let the cells adjust themselves
163 MathGridInset::metrics(mi_);
170 if (numberedType()) {
172 for (row_type row = 0; row < nrows(); ++row)
173 l = max(l, mathed_string_width(LM_TC_BF, mi_, nicelabel(row)));
179 // make it at least as high as the current font
182 math_font_max_dim(LM_TC_TEXTRM, mi_, asc, des);
183 ascent_ = max(ascent_, asc);
184 descent_ = max(descent_, des);
188 void MathHullInset::draw(Painter & pain, int x, int y) const
190 MathGridInset::draw(pain, x, y);
192 if (numberedType()) {
193 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
194 for (row_type row = 0; row < nrows(); ++row) {
195 int const yy = y + rowinfo_[row].offset_;
196 drawStr(pain, LM_TC_BF, mi_, xx, yy, nicelabel(row));
202 string MathHullInset::label(row_type row) const
204 row_type n = nrows();
205 lyx::Assert(row < n);
210 void MathHullInset::label(row_type row, string const & label)
216 void MathHullInset::numbered(row_type row, bool num)
222 bool MathHullInset::numbered(row_type row) const
228 bool MathHullInset::ams() const
233 objtype_ == LM_OT_ALIGN ||
234 objtype_ == LM_OT_MULTLINE ||
235 objtype_ == LM_OT_GATHER ||
236 objtype_ == LM_OT_ALIGNAT ||
237 objtype_ == LM_OT_XALIGNAT ||
238 objtype_ == LM_OT_XXALIGNAT;
243 bool MathHullInset::display() const
245 return getType() != LM_OT_SIMPLE;
249 vector<string> const MathHullInset::getLabelList() const
252 for (row_type row = 0; row < nrows(); ++row)
253 if (!label_[row].empty() && nonum_[row] != 1)
254 res.push_back(label_[row]);
259 bool MathHullInset::numberedType() const
261 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
263 for (row_type row = 0; row < nrows(); ++row)
270 void MathHullInset::validate(LaTeXFeatures & features) const
273 features.require("amsmath");
276 // Validation is necessary only if not using AMS math.
277 // To be safe, we will always run mathedvalidate.
278 //if (features.amsstyle)
281 features.require("boldsymbol");
282 //features.binom = true;
284 MathNestInset::validate(features);
288 void MathHullInset::header_write(WriteStream & os) const
290 bool n = numberedType();
301 os << "\\begin{equation" << star(n) << "}\n";
307 os << "\\begin{eqnarray" << star(n) << "}\n";
311 os << "\\begin{align" << star(n) << "}\n";
315 os << "\\begin{alignat" << star(n) << "}"
316 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
320 os << "\\begin{xalignat" << star(n) << "}"
321 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
324 case LM_OT_XXALIGNAT:
325 os << "\\begin{xxalignat}"
326 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
330 os << "\\begin{multline}\n";
334 os << "\\begin{gather}\n";
338 os << "\\begin{unknown" << star(n) << "}";
343 void MathHullInset::footer_write(WriteStream & os) const
345 bool n = numberedType();
354 os << "\\end{equation" << star(n) << "}\n";
360 os << "\n\\end{eqnarray" << star(n) << "}\n";
364 os << "\n\\end{align" << star(n) << "}\n";
368 os << "\n\\end{alignat" << star(n) << "}\n";
372 os << "\n\\end{xalignat" << star(n) << "}\n";
375 case LM_OT_XXALIGNAT:
376 os << "\n\\end{xxalignat}\n";
380 os << "\n\\end{multline}\n";
384 os << "\n\\end{gather}\n";
388 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::appendRow()
403 nonum_.push_back(!numberedType());
404 label_.push_back(string());
405 MathGridInset::appendRow();
409 void MathHullInset::delRow(row_type row)
411 MathGridInset::delRow(row);
412 nonum_.erase(nonum_.begin() + row);
413 label_.erase(label_.begin() + row);
417 void MathHullInset::addCol(col_type col)
421 mutate(LM_OT_EQNARRAY);
430 mutate(LM_OT_ALIGNAT);
436 case LM_OT_XXALIGNAT:
437 MathGridInset::addCol(col);
438 MathGridInset::addCol(col + 1);
447 void MathHullInset::delCol(col_type col)
452 case LM_OT_XXALIGNAT:
453 MathGridInset::delCol(col + 1);
454 MathGridInset::delCol(col);
462 string MathHullInset::nicelabel(row_type row) const
466 if (label_[row].empty())
467 return string("(#)");
468 return "(" + label_[row] + ")";
472 void MathHullInset::mutate(string const & newtype)
474 if (newtype == "dump") {
478 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
479 mutate(typecode(newtype));
483 void MathHullInset::glueall()
486 for (idx_type i = 0; i < nargs(); ++i)
487 ar.push_back(cell(i));
488 *this = MathHullInset(LM_OT_SIMPLE);
493 MathInsetTypes MathHullInset::getType() const
499 void MathHullInset::setType(MathInsetTypes t)
507 void MathHullInset::mutate(MathInsetTypes newtype)
509 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
511 if (newtype == getType())
516 setType(LM_OT_EQUATION);
524 setType(LM_OT_SIMPLE);
530 case LM_OT_XXALIGNAT: {
532 MathGridInset::addCol(1);
535 pos_type pos = firstRelOp(cell(0));
537 cell(0).erase(pos, cell(0).size());
538 cell(1).erase(0, pos);
539 setType(LM_OT_ALIGN);
546 MathGridInset::addCol(1);
547 MathGridInset::addCol(1);
549 // split it "nicely" on the firest relop
550 pos_type pos = firstRelOp(cell(0));
551 cell(1) = MathArray(cell(0), pos, cell(0).size());
552 cell(0).erase(pos, cell(0).size());
554 if (cell(1).size()) {
555 cell(2) = MathArray(cell(1), 1, cell(1).size());
556 cell(1).erase(1, cell(1).size());
559 setType(LM_OT_EQNARRAY);
568 case LM_OT_EQUATION: {
569 // set correct (no)numbering
570 bool allnonum = true;
571 for (row_type row = 0; row < nrows(); ++row) {
576 // set first non-empty label
578 for (row_type row = 0; row < nrows(); ++row) {
579 if (!label_[row].empty()) {
587 nonum_[0] = allnonum;
596 case LM_OT_XXALIGNAT:
598 for (row_type row = 0; row < nrows(); ++row) {
599 idx_type c = 3 * row + 1;
600 cell(c).push_back(cell(c + 1));
602 MathGridInset::delCol(2);
603 setType(LM_OT_ALIGN);
615 MathGridInset::addCol(1);
616 setType(LM_OT_EQNARRAY);
622 case LM_OT_XXALIGNAT:
627 lyxerr << "mutation from '" << getType()
628 << "' to '" << newtype << "' not implemented"
637 setType(LM_OT_GATHER);
640 lyxerr << "mutation from '" << getType()
641 << "' to '" << newtype << "' not implemented"
649 setType(LM_OT_MULTLINE);
652 lyxerr << "mutation from '" << getType()
653 << "' to '" << newtype << "' not implemented"
659 lyxerr << "mutation from '" << getType()
660 << "' to '" << newtype << "' not implemented"
667 void MathHullInset::write(WriteStream & os) const
671 bool n = numberedType();
673 for (row_type row = 0; row < nrows(); ++row) {
674 for (col_type col = 0; col < ncols(); ++col)
675 os << cell(index(row, col)) << eocString(col);
677 if (!label_[row].empty())
678 os << "\\label{" << label_[row] << "}";
682 os << eolString(row);
689 void MathHullInset::normalize(NormalStream & os) const
691 os << "[formula " << normalName(getType()) << " ";
692 MathGridInset::normalize(os);
697 void MathHullInset::mathmlize(MathMLStream & os) const
699 MathGridInset::mathmlize(os);
703 void MathHullInset::check() const
705 lyx::Assert(nonum_.size() == nrows());
706 lyx::Assert(label_.size() == nrows());