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 void MathHullInset::mathmlize(MathMLStream & os) const
190 MathGridInset::mathmlize(os);
194 string MathHullInset::label(row_type row) const
200 void MathHullInset::label(row_type row, string const & label)
206 void MathHullInset::numbered(row_type row, bool num)
212 bool MathHullInset::numbered(row_type row) const
218 bool MathHullInset::ams() const
223 objtype_ == LM_OT_ALIGN ||
224 objtype_ == LM_OT_MULTLINE ||
225 objtype_ == LM_OT_GATHER ||
226 objtype_ == LM_OT_ALIGNAT ||
227 objtype_ == LM_OT_XALIGNAT ||
228 objtype_ == LM_OT_XXALIGNAT;
232 bool MathHullInset::display() const
234 return getType() != LM_OT_SIMPLE;
238 std::vector<string> const MathHullInset::getLabelList() const
240 std::vector<string> res;
241 for (row_type row = 0; row < nrows(); ++row)
242 if (!label_[row].empty() && nonum_[row] != 1)
243 res.push_back(label_[row]);
248 bool MathHullInset::numberedType() const
250 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
252 for (row_type row = 0; row < nrows(); ++row)
259 void MathHullInset::validate(LaTeXFeatures & features) const
261 features.amsstyle = ams();
263 // Validation is necessary only if not using AMS math.
264 // To be safe, we will always run mathedvalidate.
265 //if (features.amsstyle)
268 features.boldsymbol = true;
269 //features.binom = true;
271 MathNestInset::validate(features);
275 void MathHullInset::header_write(std::ostream & os) const
277 bool n = numberedType();
288 os << "\\begin{equation" << star(n) << "}\n";
294 os << "\\begin{eqnarray" << star(n) << "}\n";
298 os << "\\begin{align" << star(n) << "}\n";
302 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
306 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
309 case LM_OT_XXALIGNAT:
310 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
314 os << "\\begin{multline}\n";
318 os << "\\begin{gather}\n";
322 os << "\\begin{unknown" << star(n) << "}";
327 void MathHullInset::footer_write(std::ostream & os) const
329 bool n = numberedType();
338 os << "\\end{equation" << star(n) << "}\n";
344 os << "\n\\end{eqnarray" << star(n) << "}\n";
348 os << "\n\\end{align" << star(n) << "}\n";
352 os << "\n\\end{alignat" << star(n) << "}\n";
356 os << "\n\\end{xalignat" << star(n) << "}\n";
359 case LM_OT_XXALIGNAT:
360 os << "\n\\end{xxalignat}\n";
364 os << "\n\\end{multline}\n";
368 os << "\n\\end{gather}\n";
372 os << "\\end{unknown" << star(n) << "}";
377 void MathHullInset::addRow(row_type row)
379 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
380 label_.insert(label_.begin() + row + 1, string());
381 MathGridInset::addRow(row);
385 void MathHullInset::appendRow()
387 nonum_.push_back(!numberedType());
388 label_.push_back(string());
389 MathGridInset::appendRow();
393 void MathHullInset::delRow(row_type row)
395 MathGridInset::delRow(row);
396 nonum_.erase(nonum_.begin() + row);
397 label_.erase(label_.begin() + row);
401 void MathHullInset::addCol(col_type col)
405 mutate(LM_OT_EQNARRAY);
414 mutate(LM_OT_ALIGNAT);
420 case LM_OT_XXALIGNAT:
421 MathGridInset::addCol(col);
422 MathGridInset::addCol(col + 1);
431 void MathHullInset::delCol(col_type col)
436 case LM_OT_XXALIGNAT:
437 MathGridInset::delCol(col + 1);
438 MathGridInset::delCol(col);
446 string MathHullInset::nicelabel(row_type row) const
450 if (label_[row].empty())
451 return string("(#)");
452 return "(" + label_[row] + ")";
456 void MathHullInset::mutate(string const & newtype)
458 if (newtype == "dump") {
462 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
463 mutate(typecode(newtype));
467 void MathHullInset::glueall()
470 for (idx_type i = 0; i < nargs(); ++i)
471 ar.push_back(cell(i));
472 *this = MathHullInset(LM_OT_SIMPLE);
477 MathInsetTypes MathHullInset::getType() const
483 void MathHullInset::setType(MathInsetTypes t)
491 void MathHullInset::mutate(MathInsetTypes newtype)
493 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
495 if (newtype == getType())
500 setType(LM_OT_EQUATION);
508 setType(LM_OT_SIMPLE);
514 case LM_OT_XXALIGNAT: {
516 MathGridInset::addCol(1);
519 pos_type pos = firstRelOp(cell(0));
521 cell(0).erase(pos, cell(0).size());
522 cell(1).erase(0, pos);
523 setType(LM_OT_ALIGN);
530 MathGridInset::addCol(1);
531 MathGridInset::addCol(1);
533 // split it "nicely" on the firest relop
534 pos_type pos = firstRelOp(cell(0));
535 cell(1) = MathArray(cell(0), pos, cell(0).size());
536 cell(0).erase(pos, cell(0).size());
538 if (cell(1).size()) {
539 cell(2) = MathArray(cell(1), 1, cell(1).size());
540 cell(1).erase(1, cell(1).size());
543 setType(LM_OT_EQNARRAY);
552 case LM_OT_EQUATION: {
553 // set correct (no)numbering
554 bool allnonum = true;
555 for (row_type row = 0; row < nrows(); ++row) {
560 // set first non-empty label
562 for (row_type row = 0; row < nrows(); ++row) {
563 if (!label_[row].empty()) {
571 nonum_[0] = allnonum;
580 case LM_OT_XXALIGNAT:
582 for (row_type row = 0; row < nrows(); ++row) {
583 idx_type c = 3 * row + 1;
584 cell(c).push_back(cell(c + 1));
586 MathGridInset::delCol(2);
587 setType(LM_OT_ALIGN);
599 MathGridInset::addCol(1);
600 setType(LM_OT_EQNARRAY);
606 case LM_OT_XXALIGNAT:
611 lyxerr << "mutation from '" << getType()
612 << "' to '" << newtype << "' not implemented\n";
620 setType(LM_OT_GATHER);
623 lyxerr << "mutation from '" << getType()
624 << "' to '" << newtype << "' not implemented\n";
631 setType(LM_OT_MULTLINE);
634 lyxerr << "mutation from '" << getType()
635 << "' to '" << newtype << "' not implemented\n";
640 lyxerr << "mutation from '" << getType()
641 << "' to '" << newtype << "' not implemented\n";
646 void MathHullInset::write(WriteStream & os) const
650 bool n = numberedType();
652 for (row_type row = 0; row < nrows(); ++row) {
653 for (col_type col = 0; col < ncols(); ++col)
654 os << cell(index(row, col)) << eocString(col).c_str();
656 if (!label_[row].empty())
657 os << "\\label{" << label_[row].c_str() << "}";
661 os << eolString(row).c_str();
668 void MathHullInset::normalize(NormalStream & os) const
670 os << "[formula " << normalName(getType()).c_str() << " ";
671 MathGridInset::normalize(os);