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
256 features.require("amsstyle");
259 // Validation is necessary only if not using AMS math.
260 // To be safe, we will always run mathedvalidate.
261 //if (features.amsstyle)
264 features.require("boldsymbol");
265 //features.binom = true;
267 MathNestInset::validate(features);
271 void MathHullInset::header_write(WriteStream & os) const
273 bool n = numberedType();
284 os << "\\begin{equation" << star(n) << "}\n";
290 os << "\\begin{eqnarray" << star(n) << "}\n";
294 os << "\\begin{align" << star(n) << "}\n";
298 os << "\\begin{alignat" << star(n) << "}"
299 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
303 os << "\\begin{xalignat" << star(n) << "}"
304 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
307 case LM_OT_XXALIGNAT:
308 os << "\\begin{xxalignat}"
309 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
313 os << "\\begin{multline}\n";
317 os << "\\begin{gather}\n";
321 os << "\\begin{unknown" << star(n) << "}";
326 void MathHullInset::footer_write(WriteStream & os) const
328 bool n = numberedType();
337 os << "\\end{equation" << star(n) << "}\n";
343 os << "\n\\end{eqnarray" << star(n) << "}\n";
347 os << "\n\\end{align" << star(n) << "}\n";
351 os << "\n\\end{alignat" << star(n) << "}\n";
355 os << "\n\\end{xalignat" << star(n) << "}\n";
358 case LM_OT_XXALIGNAT:
359 os << "\n\\end{xxalignat}\n";
363 os << "\n\\end{multline}\n";
367 os << "\n\\end{gather}\n";
371 os << "\\end{unknown" << star(n) << "}";
376 void MathHullInset::addRow(row_type row)
378 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
379 label_.insert(label_.begin() + row + 1, string());
380 MathGridInset::addRow(row);
384 void MathHullInset::appendRow()
386 nonum_.push_back(!numberedType());
387 label_.push_back(string());
388 MathGridInset::appendRow();
392 void MathHullInset::delRow(row_type row)
394 MathGridInset::delRow(row);
395 nonum_.erase(nonum_.begin() + row);
396 label_.erase(label_.begin() + row);
400 void MathHullInset::addCol(col_type col)
404 mutate(LM_OT_EQNARRAY);
413 mutate(LM_OT_ALIGNAT);
419 case LM_OT_XXALIGNAT:
420 MathGridInset::addCol(col);
421 MathGridInset::addCol(col + 1);
430 void MathHullInset::delCol(col_type col)
435 case LM_OT_XXALIGNAT:
436 MathGridInset::delCol(col + 1);
437 MathGridInset::delCol(col);
445 string MathHullInset::nicelabel(row_type row) const
449 if (label_[row].empty())
450 return string("(#)");
451 return "(" + label_[row] + ")";
455 void MathHullInset::mutate(string const & newtype)
457 if (newtype == "dump") {
461 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
462 mutate(typecode(newtype));
466 void MathHullInset::glueall()
469 for (idx_type i = 0; i < nargs(); ++i)
470 ar.push_back(cell(i));
471 *this = MathHullInset(LM_OT_SIMPLE);
476 MathInsetTypes MathHullInset::getType() const
482 void MathHullInset::setType(MathInsetTypes t)
490 void MathHullInset::mutate(MathInsetTypes newtype)
492 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
494 if (newtype == getType())
499 setType(LM_OT_EQUATION);
507 setType(LM_OT_SIMPLE);
513 case LM_OT_XXALIGNAT: {
515 MathGridInset::addCol(1);
518 pos_type pos = firstRelOp(cell(0));
520 cell(0).erase(pos, cell(0).size());
521 cell(1).erase(0, pos);
522 setType(LM_OT_ALIGN);
529 MathGridInset::addCol(1);
530 MathGridInset::addCol(1);
532 // split it "nicely" on the firest relop
533 pos_type pos = firstRelOp(cell(0));
534 cell(1) = MathArray(cell(0), pos, cell(0).size());
535 cell(0).erase(pos, cell(0).size());
537 if (cell(1).size()) {
538 cell(2) = MathArray(cell(1), 1, cell(1).size());
539 cell(1).erase(1, cell(1).size());
542 setType(LM_OT_EQNARRAY);
551 case LM_OT_EQUATION: {
552 // set correct (no)numbering
553 bool allnonum = true;
554 for (row_type row = 0; row < nrows(); ++row) {
559 // set first non-empty label
561 for (row_type row = 0; row < nrows(); ++row) {
562 if (!label_[row].empty()) {
570 nonum_[0] = allnonum;
579 case LM_OT_XXALIGNAT:
581 for (row_type row = 0; row < nrows(); ++row) {
582 idx_type c = 3 * row + 1;
583 cell(c).push_back(cell(c + 1));
585 MathGridInset::delCol(2);
586 setType(LM_OT_ALIGN);
598 MathGridInset::addCol(1);
599 setType(LM_OT_EQNARRAY);
605 case LM_OT_XXALIGNAT:
610 lyxerr << "mutation from '" << getType()
611 << "' to '" << newtype << "' not implemented\n";
619 setType(LM_OT_GATHER);
622 lyxerr << "mutation from '" << getType()
623 << "' to '" << newtype << "' not implemented\n";
630 setType(LM_OT_MULTLINE);
633 lyxerr << "mutation from '" << getType()
634 << "' to '" << newtype << "' not implemented\n";
639 lyxerr << "mutation from '" << getType()
640 << "' to '" << newtype << "' not implemented\n";
645 void MathHullInset::write(WriteStream & os) const
649 bool n = numberedType();
651 for (row_type row = 0; row < nrows(); ++row) {
652 for (col_type col = 0; col < ncols(); ++col)
653 os << cell(index(row, col)) << eocString(col).c_str();
655 if (!label_[row].empty())
656 os << "\\label{" << label_[row].c_str() << "}";
660 os << eolString(row).c_str();
667 void MathHullInset::normalize(NormalStream & os) const
669 os << "[formula " << normalName(getType()).c_str() << " ";
670 MathGridInset::normalize(os);
675 void MathHullInset::mathmlize(MathMLStream & os) const
677 MathGridInset::mathmlize(os);