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
255 features.amsstyle = ams();
257 // Validation is necessary only if not using AMS math.
258 // To be safe, we will always run mathedvalidate.
259 //if (features.amsstyle)
262 features.boldsymbol = true;
263 //features.binom = true;
265 MathNestInset::validate(features);
269 void MathHullInset::header_write(std::ostream & os) const
271 bool n = numberedType();
282 os << "\\begin{equation" << star(n) << "}\n";
288 os << "\\begin{eqnarray" << star(n) << "}\n";
292 os << "\\begin{align" << star(n) << "}\n";
296 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
300 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
303 case LM_OT_XXALIGNAT:
304 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
308 os << "\\begin{multline}\n";
312 os << "\\begin{gather}\n";
316 os << "\\begin{unknown" << star(n) << "}";
321 void MathHullInset::footer_write(std::ostream & os) const
323 bool n = numberedType();
332 os << "\\end{equation" << star(n) << "}\n";
338 os << "\n\\end{eqnarray" << star(n) << "}\n";
342 os << "\n\\end{align" << star(n) << "}\n";
346 os << "\n\\end{alignat" << star(n) << "}\n";
350 os << "\n\\end{xalignat" << star(n) << "}\n";
353 case LM_OT_XXALIGNAT:
354 os << "\n\\end{xxalignat}\n";
358 os << "\n\\end{multline}\n";
362 os << "\n\\end{gather}\n";
366 os << "\\end{unknown" << star(n) << "}";
371 void MathHullInset::addRow(row_type row)
373 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
374 label_.insert(label_.begin() + row + 1, string());
375 MathGridInset::addRow(row);
379 void MathHullInset::appendRow()
381 nonum_.push_back(!numberedType());
382 label_.push_back(string());
383 MathGridInset::appendRow();
387 void MathHullInset::delRow(row_type row)
389 MathGridInset::delRow(row);
390 nonum_.erase(nonum_.begin() + row);
391 label_.erase(label_.begin() + row);
395 void MathHullInset::addCol(col_type col)
399 mutate(LM_OT_EQNARRAY);
408 mutate(LM_OT_ALIGNAT);
414 case LM_OT_XXALIGNAT:
415 MathGridInset::addCol(col);
416 MathGridInset::addCol(col + 1);
425 void MathHullInset::delCol(col_type col)
430 case LM_OT_XXALIGNAT:
431 MathGridInset::delCol(col + 1);
432 MathGridInset::delCol(col);
440 string MathHullInset::nicelabel(row_type row) const
444 if (label_[row].empty())
445 return string("(#)");
446 return "(" + label_[row] + ")";
450 void MathHullInset::mutate(string const & newtype)
452 if (newtype == "dump") {
456 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
457 mutate(typecode(newtype));
461 void MathHullInset::glueall()
464 for (idx_type i = 0; i < nargs(); ++i)
465 ar.push_back(cell(i));
466 *this = MathHullInset(LM_OT_SIMPLE);
471 MathInsetTypes MathHullInset::getType() const
477 void MathHullInset::setType(MathInsetTypes t)
485 void MathHullInset::mutate(MathInsetTypes newtype)
487 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
489 if (newtype == getType())
494 setType(LM_OT_EQUATION);
502 setType(LM_OT_SIMPLE);
508 case LM_OT_XXALIGNAT: {
510 MathGridInset::addCol(1);
513 pos_type pos = firstRelOp(cell(0));
515 cell(0).erase(pos, cell(0).size());
516 cell(1).erase(0, pos);
517 setType(LM_OT_ALIGN);
524 MathGridInset::addCol(1);
525 MathGridInset::addCol(1);
527 // split it "nicely" on the firest relop
528 pos_type pos = firstRelOp(cell(0));
529 cell(1) = MathArray(cell(0), pos, cell(0).size());
530 cell(0).erase(pos, cell(0).size());
532 if (cell(1).size()) {
533 cell(2) = MathArray(cell(1), 1, cell(1).size());
534 cell(1).erase(1, cell(1).size());
537 setType(LM_OT_EQNARRAY);
546 case LM_OT_EQUATION: {
547 // set correct (no)numbering
548 bool allnonum = true;
549 for (row_type row = 0; row < nrows(); ++row) {
554 // set first non-empty label
556 for (row_type row = 0; row < nrows(); ++row) {
557 if (!label_[row].empty()) {
565 nonum_[0] = allnonum;
574 case LM_OT_XXALIGNAT:
576 for (row_type row = 0; row < nrows(); ++row) {
577 idx_type c = 3 * row + 1;
578 cell(c).push_back(cell(c + 1));
580 MathGridInset::delCol(2);
581 setType(LM_OT_ALIGN);
593 MathGridInset::addCol(1);
594 setType(LM_OT_EQNARRAY);
600 case LM_OT_XXALIGNAT:
605 lyxerr << "mutation from '" << getType()
606 << "' to '" << newtype << "' not implemented\n";
614 setType(LM_OT_GATHER);
617 lyxerr << "mutation from '" << getType()
618 << "' to '" << newtype << "' not implemented\n";
625 setType(LM_OT_MULTLINE);
628 lyxerr << "mutation from '" << getType()
629 << "' to '" << newtype << "' not implemented\n";
634 lyxerr << "mutation from '" << getType()
635 << "' to '" << newtype << "' not implemented\n";
640 void MathHullInset::write(WriteStream & os) const
644 bool n = numberedType();
646 for (row_type row = 0; row < nrows(); ++row) {
647 for (col_type col = 0; col < ncols(); ++col)
648 os << cell(index(row, col)) << eocString(col).c_str();
650 if (!label_[row].empty())
651 os << "\\label{" << label_[row].c_str() << "}";
655 os << eolString(row).c_str();
662 void MathHullInset::normalize(NormalStream & os) const
664 os << "[formula " << normalName(getType()).c_str() << " ";
665 MathGridInset::normalize(os);
670 void MathHullInset::mathmlize(MathMLStream & os) const
672 MathGridInset::mathmlize(os);