7 #include "math_hullinset.h"
8 #include "math_support.h"
11 #include "LaTeXFeatures.h"
12 #include "math_mathmlstream.h"
17 int getCols(MathInsetTypes type)
33 // returns position of first relation operator in the array
34 // used for "intelligent splitting"
35 int firstRelOp(MathArray const & ar)
37 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
39 return it - ar.begin();
44 char const * star(bool numbered)
46 return numbered ? "" : "*";
49 MathInsetTypes typecode(string const & s)
51 if (s == "equation") return LM_OT_EQUATION;
52 if (s == "display") return LM_OT_EQUATION;
53 if (s == "eqnarray") return LM_OT_EQNARRAY;
54 if (s == "align") return LM_OT_ALIGN;
55 if (s == "alignat") return LM_OT_ALIGNAT;
56 if (s == "xalignat") return LM_OT_XALIGNAT;
57 if (s == "xxalignat") return LM_OT_XXALIGNAT;
58 if (s == "multline") return LM_OT_MULTLINE;
59 if (s == "gather") return LM_OT_GATHER;
64 string normalName(MathInsetTypes t)
67 case LM_OT_EQUATION: return "equation";
68 case LM_OT_EQNARRAY: return "eqnarray";
69 case LM_OT_ALIGN: return "align";
70 case LM_OT_ALIGNAT: return "alignat";
71 case LM_OT_XALIGNAT: return "xalignat";
72 case LM_OT_XXALIGNAT: return "xxalignat";
73 case LM_OT_MULTLINE: return "multline";
74 case LM_OT_GATHER: return "gather";
75 case LM_OT_SIMPLE: return "simple";
81 } // end anon namespace
84 MathHullInset::MathHullInset()
85 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
91 MathHullInset::MathHullInset(MathInsetTypes t)
92 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
98 MathHullInset::MathHullInset(MathInsetTypes t, col_type cols)
99 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
105 MathInset * MathHullInset::clone() const
107 return new MathHullInset(*this);
111 char MathHullInset::defaultColAlign(col_type col)
117 case LM_OT_XXALIGNAT:
118 return "rl"[col & 1];
127 int MathHullInset::defaultColSpace(col_type col)
134 return (col & 1) ? 20 : 0;
135 case LM_OT_XXALIGNAT:
136 return (col & 1) ? 40 : 0;
143 void MathHullInset::metrics(MathMetricsInfo const & mi) const
146 mi_.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
148 // let the cells adjust themselves
149 MathGridInset::metrics(mi_);
156 if (numberedType()) {
158 for (row_type row = 0; row < nrows(); ++row)
159 l = std::max(l, mathed_string_width(LM_TC_BF, mi_, nicelabel(row)));
165 // make it at least as high as the current font
168 math_font_max_dim(LM_TC_TEXTRM, mi_, asc, des);
169 ascent_ = std::max(ascent_, asc);
170 descent_ = std::max(descent_, des);
174 void MathHullInset::draw(Painter & pain, int x, int y) const
176 MathGridInset::draw(pain, x, y);
178 if (numberedType()) {
179 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
180 for (row_type row = 0; row < nrows(); ++row) {
181 int const yy = y + rowinfo_[row].offset_;
182 drawStr(pain, LM_TC_BF, mi_, xx, yy, nicelabel(row));
188 string MathHullInset::label(row_type row) const
194 void MathHullInset::label(row_type row, string const & label)
200 void MathHullInset::numbered(row_type row, bool num)
206 bool MathHullInset::numbered(row_type row) const
212 bool MathHullInset::ams() const
217 objtype_ == LM_OT_ALIGN ||
218 objtype_ == LM_OT_MULTLINE ||
219 objtype_ == LM_OT_GATHER ||
220 objtype_ == LM_OT_ALIGNAT ||
221 objtype_ == LM_OT_XALIGNAT ||
222 objtype_ == LM_OT_XXALIGNAT;
226 bool MathHullInset::display() const
228 return getType() != LM_OT_SIMPLE;
232 std::vector<string> const MathHullInset::getLabelList() const
234 std::vector<string> res;
235 for (row_type row = 0; row < nrows(); ++row)
236 if (!label_[row].empty() && nonum_[row] != 1)
237 res.push_back(label_[row]);
242 bool MathHullInset::numberedType() const
244 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
246 for (row_type row = 0; row < nrows(); ++row)
253 void MathHullInset::validate(LaTeXFeatures & features) const
257 features.require("amsstyle");
260 // Validation is necessary only if not using AMS math.
261 // To be safe, we will always run mathedvalidate.
262 //if (features.amsstyle)
265 features.require("boldsymbol");
266 //features.binom = true;
268 MathNestInset::validate(features);
272 void MathHullInset::header_write(WriteStream & os) const
274 bool n = numberedType();
285 os << "\\begin{equation" << star(n) << "}\n";
291 os << "\\begin{eqnarray" << star(n) << "}\n";
295 os << "\\begin{align" << star(n) << "}\n";
299 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
303 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
306 case LM_OT_XXALIGNAT:
307 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
311 os << "\\begin{multline}\n";
315 os << "\\begin{gather}\n";
319 os << "\\begin{unknown" << star(n) << "}";
324 void MathHullInset::footer_write(WriteStream & os) const
326 bool n = numberedType();
335 os << "\\end{equation" << star(n) << "}\n";
341 os << "\n\\end{eqnarray" << star(n) << "}\n";
345 os << "\n\\end{align" << star(n) << "}\n";
349 os << "\n\\end{alignat" << star(n) << "}\n";
353 os << "\n\\end{xalignat" << star(n) << "}\n";
356 case LM_OT_XXALIGNAT:
357 os << "\n\\end{xxalignat}\n";
361 os << "\n\\end{multline}\n";
365 os << "\n\\end{gather}\n";
369 os << "\\end{unknown" << star(n) << "}";
374 void MathHullInset::addRow(row_type row)
376 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
377 label_.insert(label_.begin() + row + 1, string());
378 MathGridInset::addRow(row);
382 void MathHullInset::appendRow()
384 nonum_.push_back(!numberedType());
385 label_.push_back(string());
386 MathGridInset::appendRow();
390 void MathHullInset::delRow(row_type row)
392 MathGridInset::delRow(row);
393 nonum_.erase(nonum_.begin() + row);
394 label_.erase(label_.begin() + row);
398 void MathHullInset::addCol(col_type col)
402 mutate(LM_OT_EQNARRAY);
411 mutate(LM_OT_ALIGNAT);
417 case LM_OT_XXALIGNAT:
418 MathGridInset::addCol(col);
419 MathGridInset::addCol(col + 1);
428 void MathHullInset::delCol(col_type col)
433 case LM_OT_XXALIGNAT:
434 MathGridInset::delCol(col + 1);
435 MathGridInset::delCol(col);
443 string MathHullInset::nicelabel(row_type row) const
447 if (label_[row].empty())
448 return string("(#)");
449 return "(" + label_[row] + ")";
453 void MathHullInset::mutate(string const & newtype)
455 if (newtype == "dump") {
459 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
460 mutate(typecode(newtype));
464 void MathHullInset::glueall()
467 for (idx_type i = 0; i < nargs(); ++i)
468 ar.push_back(cell(i));
469 *this = MathHullInset(LM_OT_SIMPLE);
474 MathInsetTypes MathHullInset::getType() const
480 void MathHullInset::setType(MathInsetTypes t)
488 void MathHullInset::mutate(MathInsetTypes newtype)
490 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
492 if (newtype == getType())
497 setType(LM_OT_EQUATION);
505 setType(LM_OT_SIMPLE);
511 case LM_OT_XXALIGNAT: {
513 MathGridInset::addCol(1);
516 pos_type pos = firstRelOp(cell(0));
518 cell(0).erase(pos, cell(0).size());
519 cell(1).erase(0, pos);
520 setType(LM_OT_ALIGN);
527 MathGridInset::addCol(1);
528 MathGridInset::addCol(1);
530 // split it "nicely" on the firest relop
531 pos_type pos = firstRelOp(cell(0));
532 cell(1) = MathArray(cell(0), pos, cell(0).size());
533 cell(0).erase(pos, cell(0).size());
535 if (cell(1).size()) {
536 cell(2) = MathArray(cell(1), 1, cell(1).size());
537 cell(1).erase(1, cell(1).size());
540 setType(LM_OT_EQNARRAY);
549 case LM_OT_EQUATION: {
550 // set correct (no)numbering
551 bool allnonum = true;
552 for (row_type row = 0; row < nrows(); ++row) {
557 // set first non-empty label
559 for (row_type row = 0; row < nrows(); ++row) {
560 if (!label_[row].empty()) {
568 nonum_[0] = allnonum;
577 case LM_OT_XXALIGNAT:
579 for (row_type row = 0; row < nrows(); ++row) {
580 idx_type c = 3 * row + 1;
581 cell(c).push_back(cell(c + 1));
583 MathGridInset::delCol(2);
584 setType(LM_OT_ALIGN);
596 MathGridInset::addCol(1);
597 setType(LM_OT_EQNARRAY);
603 case LM_OT_XXALIGNAT:
608 lyxerr << "mutation from '" << getType()
609 << "' to '" << newtype << "' not implemented\n";
617 setType(LM_OT_GATHER);
620 lyxerr << "mutation from '" << getType()
621 << "' to '" << newtype << "' not implemented\n";
628 setType(LM_OT_MULTLINE);
631 lyxerr << "mutation from '" << getType()
632 << "' to '" << newtype << "' not implemented\n";
637 lyxerr << "mutation from '" << getType()
638 << "' to '" << newtype << "' not implemented\n";
643 void MathHullInset::write(WriteStream & os) const
647 bool n = numberedType();
649 for (row_type row = 0; row < nrows(); ++row) {
650 for (col_type col = 0; col < ncols(); ++col)
651 os << cell(index(row, col)) << eocString(col).c_str();
653 if (!label_[row].empty())
654 os << "\\label{" << label_[row].c_str() << "}";
658 os << eolString(row).c_str();
665 void MathHullInset::normalize(NormalStream & os) const
667 os << "[formula " << normalName(getType()).c_str() << " ";
668 MathGridInset::normalize(os);
673 void MathHullInset::mathmlize(MathMLStream & os) const
675 MathGridInset::mathmlize(os);