7 #include "math_hullinset.h"
8 #include "math_support.h"
11 #include "LaTeXFeatures.h"
12 #include "math_mathmlstream.h"
13 #include "math_streamstr.h"
21 int getCols(MathInsetTypes type)
37 // returns position of first relation operator in the array
38 // used for "intelligent splitting"
39 int firstRelOp(MathArray const & ar)
41 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
43 return it - ar.begin();
48 char const * star(bool numbered)
50 return numbered ? "" : "*";
53 MathInsetTypes typecode(string const & s)
55 if (s == "equation") return LM_OT_EQUATION;
56 if (s == "display") return LM_OT_EQUATION;
57 if (s == "eqnarray") return LM_OT_EQNARRAY;
58 if (s == "align") return LM_OT_ALIGN;
59 if (s == "alignat") return LM_OT_ALIGNAT;
60 if (s == "xalignat") return LM_OT_XALIGNAT;
61 if (s == "xxalignat") return LM_OT_XXALIGNAT;
62 if (s == "multline") return LM_OT_MULTLINE;
63 if (s == "gather") return LM_OT_GATHER;
68 string normalName(MathInsetTypes t)
71 case LM_OT_EQUATION: return "equation";
72 case LM_OT_EQNARRAY: return "eqnarray";
73 case LM_OT_ALIGN: return "align";
74 case LM_OT_ALIGNAT: return "alignat";
75 case LM_OT_XALIGNAT: return "xalignat";
76 case LM_OT_XXALIGNAT: return "xxalignat";
77 case LM_OT_MULTLINE: return "multline";
78 case LM_OT_GATHER: return "gather";
79 case LM_OT_SIMPLE: return "simple";
85 } // end anon namespace
88 MathHullInset::MathHullInset()
89 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
95 MathHullInset::MathHullInset(MathInsetTypes t)
96 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
102 MathHullInset::MathHullInset(MathInsetTypes t, col_type cols)
103 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
109 MathInset * MathHullInset::clone() const
111 return new MathHullInset(*this);
115 char MathHullInset::defaultColAlign(col_type col)
121 case LM_OT_XXALIGNAT:
122 return "rl"[col & 1];
131 int MathHullInset::defaultColSpace(col_type col)
138 return (col & 1) ? 20 : 0;
139 case LM_OT_XXALIGNAT:
140 return (col & 1) ? 40 : 0;
147 void MathHullInset::metrics(MathMetricsInfo const & mi) const
150 mi_.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
152 // let the cells adjust themselves
153 MathGridInset::metrics(mi_);
160 if (numberedType()) {
162 for (row_type row = 0; row < nrows(); ++row)
163 l = std::max(l, mathed_string_width(LM_TC_BF, mi_, nicelabel(row)));
169 // make it at least as high as the current font
172 math_font_max_dim(LM_TC_TEXTRM, mi_, asc, des);
173 ascent_ = std::max(ascent_, asc);
174 descent_ = std::max(descent_, des);
178 void MathHullInset::draw(Painter & pain, int x, int y) const
180 MathGridInset::draw(pain, x, y);
182 if (numberedType()) {
183 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
184 for (row_type row = 0; row < nrows(); ++row) {
185 int const yy = y + rowinfo_[row].offset_;
186 drawStr(pain, LM_TC_BF, mi_, xx, yy, nicelabel(row));
192 string MathHullInset::label(row_type row) const
198 void MathHullInset::label(row_type row, string const & label)
204 void MathHullInset::numbered(row_type row, bool num)
210 bool MathHullInset::numbered(row_type row) const
216 bool MathHullInset::ams() const
221 objtype_ == LM_OT_ALIGN ||
222 objtype_ == LM_OT_MULTLINE ||
223 objtype_ == LM_OT_GATHER ||
224 objtype_ == LM_OT_ALIGNAT ||
225 objtype_ == LM_OT_XALIGNAT ||
226 objtype_ == LM_OT_XXALIGNAT;
230 bool MathHullInset::display() const
232 return getType() != LM_OT_SIMPLE;
236 std::vector<string> const MathHullInset::getLabelList() const
238 std::vector<string> res;
239 for (row_type row = 0; row < nrows(); ++row)
240 if (!label_[row].empty() && nonum_[row] != 1)
241 res.push_back(label_[row]);
246 bool MathHullInset::numberedType() const
248 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
250 for (row_type row = 0; row < nrows(); ++row)
257 void MathHullInset::validate(LaTeXFeatures & features) const
260 features.require("amsstyle");
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.require("boldsymbol");
269 //features.binom = true;
271 MathNestInset::validate(features);
275 void MathHullInset::header_write(WriteStream & 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) << "}"
303 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
307 os << "\\begin{xalignat" << star(n) << "}"
308 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
311 case LM_OT_XXALIGNAT:
312 os << "\\begin{xxalignat}"
313 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
317 os << "\\begin{multline}\n";
321 os << "\\begin{gather}\n";
325 os << "\\begin{unknown" << star(n) << "}";
330 void MathHullInset::footer_write(WriteStream & os) const
332 bool n = numberedType();
341 os << "\\end{equation" << star(n) << "}\n";
347 os << "\n\\end{eqnarray" << star(n) << "}\n";
351 os << "\n\\end{align" << star(n) << "}\n";
355 os << "\n\\end{alignat" << star(n) << "}\n";
359 os << "\n\\end{xalignat" << star(n) << "}\n";
362 case LM_OT_XXALIGNAT:
363 os << "\n\\end{xxalignat}\n";
367 os << "\n\\end{multline}\n";
371 os << "\n\\end{gather}\n";
375 os << "\\end{unknown" << star(n) << "}";
380 void MathHullInset::addRow(row_type row)
382 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
383 label_.insert(label_.begin() + row + 1, string());
384 MathGridInset::addRow(row);
388 void MathHullInset::appendRow()
390 nonum_.push_back(!numberedType());
391 label_.push_back(string());
392 MathGridInset::appendRow();
396 void MathHullInset::delRow(row_type row)
398 MathGridInset::delRow(row);
399 nonum_.erase(nonum_.begin() + row);
400 label_.erase(label_.begin() + row);
404 void MathHullInset::addCol(col_type col)
408 mutate(LM_OT_EQNARRAY);
417 mutate(LM_OT_ALIGNAT);
423 case LM_OT_XXALIGNAT:
424 MathGridInset::addCol(col);
425 MathGridInset::addCol(col + 1);
434 void MathHullInset::delCol(col_type col)
439 case LM_OT_XXALIGNAT:
440 MathGridInset::delCol(col + 1);
441 MathGridInset::delCol(col);
449 string MathHullInset::nicelabel(row_type row) const
453 if (label_[row].empty())
454 return string("(#)");
455 return "(" + label_[row] + ")";
459 void MathHullInset::mutate(string const & newtype)
461 if (newtype == "dump") {
465 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
466 mutate(typecode(newtype));
470 void MathHullInset::glueall()
473 for (idx_type i = 0; i < nargs(); ++i)
474 ar.push_back(cell(i));
475 *this = MathHullInset(LM_OT_SIMPLE);
480 MathInsetTypes MathHullInset::getType() const
486 void MathHullInset::setType(MathInsetTypes t)
494 void MathHullInset::mutate(MathInsetTypes newtype)
496 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
498 if (newtype == getType())
503 setType(LM_OT_EQUATION);
511 setType(LM_OT_SIMPLE);
517 case LM_OT_XXALIGNAT: {
519 MathGridInset::addCol(1);
522 pos_type pos = firstRelOp(cell(0));
524 cell(0).erase(pos, cell(0).size());
525 cell(1).erase(0, pos);
526 setType(LM_OT_ALIGN);
533 MathGridInset::addCol(1);
534 MathGridInset::addCol(1);
536 // split it "nicely" on the firest relop
537 pos_type pos = firstRelOp(cell(0));
538 cell(1) = MathArray(cell(0), pos, cell(0).size());
539 cell(0).erase(pos, cell(0).size());
541 if (cell(1).size()) {
542 cell(2) = MathArray(cell(1), 1, cell(1).size());
543 cell(1).erase(1, cell(1).size());
546 setType(LM_OT_EQNARRAY);
555 case LM_OT_EQUATION: {
556 // set correct (no)numbering
557 bool allnonum = true;
558 for (row_type row = 0; row < nrows(); ++row) {
563 // set first non-empty label
565 for (row_type row = 0; row < nrows(); ++row) {
566 if (!label_[row].empty()) {
574 nonum_[0] = allnonum;
583 case LM_OT_XXALIGNAT:
585 for (row_type row = 0; row < nrows(); ++row) {
586 idx_type c = 3 * row + 1;
587 cell(c).push_back(cell(c + 1));
589 MathGridInset::delCol(2);
590 setType(LM_OT_ALIGN);
602 MathGridInset::addCol(1);
603 setType(LM_OT_EQNARRAY);
609 case LM_OT_XXALIGNAT:
614 lyxerr << "mutation from '" << getType()
615 << "' to '" << newtype << "' not implemented"
624 setType(LM_OT_GATHER);
627 lyxerr << "mutation from '" << getType()
628 << "' to '" << newtype << "' not implemented"
636 setType(LM_OT_MULTLINE);
639 lyxerr << "mutation from '" << getType()
640 << "' to '" << newtype << "' not implemented"
646 lyxerr << "mutation from '" << getType()
647 << "' to '" << newtype << "' not implemented"
654 void MathHullInset::write(WriteStream & os) const
658 bool n = numberedType();
660 for (row_type row = 0; row < nrows(); ++row) {
661 for (col_type col = 0; col < ncols(); ++col)
662 os << cell(index(row, col)) << eocString(col);
664 if (!label_[row].empty())
665 os << "\\label{" << label_[row] << "}";
669 os << eolString(row);
676 void MathHullInset::normalize(NormalStream & os) const
678 os << "[formula " << normalName(getType()) << " ";
679 MathGridInset::normalize(os);
684 void MathHullInset::mathmlize(MathMLStream & os) const
686 MathGridInset::mathmlize(os);