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) << "}" << "{" << ncols()/2 << "}\n";
302 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
305 case LM_OT_XXALIGNAT:
306 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
310 os << "\\begin{multline}\n";
314 os << "\\begin{gather}\n";
318 os << "\\begin{unknown" << star(n) << "}";
323 void MathHullInset::footer_write(WriteStream & os) const
325 bool n = numberedType();
334 os << "\\end{equation" << star(n) << "}\n";
340 os << "\n\\end{eqnarray" << star(n) << "}\n";
344 os << "\n\\end{align" << star(n) << "}\n";
348 os << "\n\\end{alignat" << star(n) << "}\n";
352 os << "\n\\end{xalignat" << star(n) << "}\n";
355 case LM_OT_XXALIGNAT:
356 os << "\n\\end{xxalignat}\n";
360 os << "\n\\end{multline}\n";
364 os << "\n\\end{gather}\n";
368 os << "\\end{unknown" << star(n) << "}";
373 void MathHullInset::addRow(row_type row)
375 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
376 label_.insert(label_.begin() + row + 1, string());
377 MathGridInset::addRow(row);
381 void MathHullInset::appendRow()
383 nonum_.push_back(!numberedType());
384 label_.push_back(string());
385 MathGridInset::appendRow();
389 void MathHullInset::delRow(row_type row)
391 MathGridInset::delRow(row);
392 nonum_.erase(nonum_.begin() + row);
393 label_.erase(label_.begin() + row);
397 void MathHullInset::addCol(col_type col)
401 mutate(LM_OT_EQNARRAY);
410 mutate(LM_OT_ALIGNAT);
416 case LM_OT_XXALIGNAT:
417 MathGridInset::addCol(col);
418 MathGridInset::addCol(col + 1);
427 void MathHullInset::delCol(col_type col)
432 case LM_OT_XXALIGNAT:
433 MathGridInset::delCol(col + 1);
434 MathGridInset::delCol(col);
442 string MathHullInset::nicelabel(row_type row) const
446 if (label_[row].empty())
447 return string("(#)");
448 return "(" + label_[row] + ")";
452 void MathHullInset::mutate(string const & newtype)
454 if (newtype == "dump") {
458 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
459 mutate(typecode(newtype));
463 void MathHullInset::glueall()
466 for (idx_type i = 0; i < nargs(); ++i)
467 ar.push_back(cell(i));
468 *this = MathHullInset(LM_OT_SIMPLE);
473 MathInsetTypes MathHullInset::getType() const
479 void MathHullInset::setType(MathInsetTypes t)
487 void MathHullInset::mutate(MathInsetTypes newtype)
489 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
491 if (newtype == getType())
496 setType(LM_OT_EQUATION);
504 setType(LM_OT_SIMPLE);
510 case LM_OT_XXALIGNAT: {
512 MathGridInset::addCol(1);
515 pos_type pos = firstRelOp(cell(0));
517 cell(0).erase(pos, cell(0).size());
518 cell(1).erase(0, pos);
519 setType(LM_OT_ALIGN);
526 MathGridInset::addCol(1);
527 MathGridInset::addCol(1);
529 // split it "nicely" on the firest relop
530 pos_type pos = firstRelOp(cell(0));
531 cell(1) = MathArray(cell(0), pos, cell(0).size());
532 cell(0).erase(pos, cell(0).size());
534 if (cell(1).size()) {
535 cell(2) = MathArray(cell(1), 1, cell(1).size());
536 cell(1).erase(1, cell(1).size());
539 setType(LM_OT_EQNARRAY);
548 case LM_OT_EQUATION: {
549 // set correct (no)numbering
550 bool allnonum = true;
551 for (row_type row = 0; row < nrows(); ++row) {
556 // set first non-empty label
558 for (row_type row = 0; row < nrows(); ++row) {
559 if (!label_[row].empty()) {
567 nonum_[0] = allnonum;
576 case LM_OT_XXALIGNAT:
578 for (row_type row = 0; row < nrows(); ++row) {
579 idx_type c = 3 * row + 1;
580 cell(c).push_back(cell(c + 1));
582 MathGridInset::delCol(2);
583 setType(LM_OT_ALIGN);
595 MathGridInset::addCol(1);
596 setType(LM_OT_EQNARRAY);
602 case LM_OT_XXALIGNAT:
607 lyxerr << "mutation from '" << getType()
608 << "' to '" << newtype << "' not implemented\n";
616 setType(LM_OT_GATHER);
619 lyxerr << "mutation from '" << getType()
620 << "' to '" << newtype << "' not implemented\n";
627 setType(LM_OT_MULTLINE);
630 lyxerr << "mutation from '" << getType()
631 << "' to '" << newtype << "' not implemented\n";
636 lyxerr << "mutation from '" << getType()
637 << "' to '" << newtype << "' not implemented\n";
642 void MathHullInset::write(WriteStream & os) const
646 bool n = numberedType();
648 for (row_type row = 0; row < nrows(); ++row) {
649 for (col_type col = 0; col < ncols(); ++col)
650 os << cell(index(row, col)) << eocString(col).c_str();
652 if (!label_[row].empty())
653 os << "\\label{" << label_[row].c_str() << "}";
657 os << eolString(row).c_str();
664 void MathHullInset::normalize(NormalStream & os) const
666 os << "[formula " << normalName(getType()).c_str() << " ";
667 MathGridInset::normalize(os);
672 void MathHullInset::mathmlize(MathMLStream & os) const
674 MathGridInset::mathmlize(os);