7 #include "math_hullinset.h"
8 #include "math_mathmlstream.h"
9 #include "math_streamstr.h"
10 #include "math_support.h"
12 #include "frontends/Painter.h"
13 #include "textpainter.h"
15 #include "LaTeXFeatures.h"
16 #include "support/LAssert.h"
26 int getCols(MathInsetTypes type)
42 // returns position of first relation operator in the array
43 // used for "intelligent splitting"
44 MathArray::size_type firstRelOp(MathArray const & ar)
46 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
48 return it - ar.begin();
53 char const * star(bool numbered)
55 return numbered ? "" : "*";
58 MathInsetTypes typecode(string const & s)
60 if (s == "equation") return LM_OT_EQUATION;
61 if (s == "display") return LM_OT_EQUATION;
62 if (s == "eqnarray") return LM_OT_EQNARRAY;
63 if (s == "align") return LM_OT_ALIGN;
64 if (s == "alignat") return LM_OT_ALIGNAT;
65 if (s == "xalignat") return LM_OT_XALIGNAT;
66 if (s == "xxalignat") return LM_OT_XXALIGNAT;
67 if (s == "multline") return LM_OT_MULTLINE;
68 if (s == "gather") return LM_OT_GATHER;
73 string normalName(MathInsetTypes t)
76 case LM_OT_EQUATION: return "equation";
77 case LM_OT_EQNARRAY: return "eqnarray";
78 case LM_OT_ALIGN: return "align";
79 case LM_OT_ALIGNAT: return "alignat";
80 case LM_OT_XALIGNAT: return "xalignat";
81 case LM_OT_XXALIGNAT: return "xxalignat";
82 case LM_OT_MULTLINE: return "multline";
83 case LM_OT_GATHER: return "gather";
84 case LM_OT_SIMPLE: return "simple";
90 } // end anon namespace
93 MathHullInset::MathHullInset()
94 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
100 MathHullInset::MathHullInset(MathInsetTypes t)
101 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
107 MathHullInset::MathHullInset(MathInsetTypes t, MathGridInset const & grid)
108 : MathGridInset(grid), objtype_(t), nonum_(1), label_(1)
114 MathHullInset::MathHullInset(MathInsetTypes t, col_type cols)
115 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
121 MathInset * MathHullInset::clone() const
123 return new MathHullInset(*this);
127 char MathHullInset::defaultColAlign(col_type col)
133 case LM_OT_XXALIGNAT:
134 return "rl"[col & 1];
143 int MathHullInset::defaultColSpace(col_type col)
150 return (col & 1) ? 20 : 0;
151 case LM_OT_XXALIGNAT:
152 return (col & 1) ? 40 : 0;
159 void MathHullInset::metrics(MathMetricsInfo const & mi) const
162 mi_.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
164 // let the cells adjust themselves
165 MathGridInset::metrics(mi_);
172 if (numberedType()) {
173 whichFont(mi_.font, LM_TC_BF, mi_);
175 for (row_type row = 0; row < nrows(); ++row)
176 l = max(l, mathed_string_width(mi_.font, nicelabel(row)));
182 // make it at least as high as the current font
185 math_font_max_dim(mi_.font, asc, des);
186 ascent_ = max(ascent_, asc);
187 descent_ = max(descent_, des);
191 void MathHullInset::draw(Painter & pain, int x, int y) const
193 MathGridInset::draw(pain, x, y);
195 if (numberedType()) {
196 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
197 for (row_type row = 0; row < nrows(); ++row) {
198 int const yy = y + rowinfo_[row].offset_;
199 drawStr(pain, mi_.font, xx, yy, nicelabel(row));
205 void MathHullInset::metricsT(TextMetricsInfo const &) const
209 MathGridInset::metricsT(mi);
214 WriteStream wi(os, false, true);
216 width_ = os.str().size();
223 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
227 MathGridInset::drawT(pain, x, y);
232 WriteStream wi(os, false, true);
234 pain.draw(x, y, os.str().c_str());
239 string MathHullInset::label(row_type row) const
241 row_type n = nrows();
242 lyx::Assert(row < n);
247 void MathHullInset::label(row_type row, string const & label)
253 void MathHullInset::numbered(row_type row, bool num)
259 bool MathHullInset::numbered(row_type row) const
265 bool MathHullInset::ams() const
270 objtype_ == LM_OT_ALIGN ||
271 objtype_ == LM_OT_MULTLINE ||
272 objtype_ == LM_OT_GATHER ||
273 objtype_ == LM_OT_ALIGNAT ||
274 objtype_ == LM_OT_XALIGNAT ||
275 objtype_ == LM_OT_XXALIGNAT;
280 bool MathHullInset::display() const
282 return getType() != LM_OT_SIMPLE;
286 vector<string> const MathHullInset::getLabelList() const
289 for (row_type row = 0; row < nrows(); ++row)
290 if (!label_[row].empty() && nonum_[row] != 1)
291 res.push_back(label_[row]);
296 bool MathHullInset::numberedType() const
298 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
300 for (row_type row = 0; row < nrows(); ++row)
307 void MathHullInset::validate(LaTeXFeatures & features) const
310 features.require("amsmath");
313 // Validation is necessary only if not using AMS math.
314 // To be safe, we will always run mathedvalidate.
315 //if (features.amsstyle)
318 features.require("boldsymbol");
319 //features.binom = true;
321 MathNestInset::validate(features);
325 void MathHullInset::header_write(WriteStream & os) const
327 bool n = numberedType();
338 os << "\\begin{equation" << star(n) << "}\n";
344 os << "\\begin{eqnarray" << star(n) << "}\n";
348 os << "\\begin{align" << star(n) << "}\n";
352 os << "\\begin{alignat" << star(n) << "}"
353 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
357 os << "\\begin{xalignat" << star(n) << "}"
358 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
361 case LM_OT_XXALIGNAT:
362 os << "\\begin{xxalignat}"
363 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
367 os << "\\begin{multline}\n";
371 os << "\\begin{gather}\n";
375 os << "\\begin{unknown" << star(n) << "}";
380 void MathHullInset::footer_write(WriteStream & os) const
382 bool n = numberedType();
391 os << "\\end{equation" << star(n) << "}\n";
397 os << "\n\\end{eqnarray" << star(n) << "}\n";
401 os << "\n\\end{align" << star(n) << "}\n";
405 os << "\n\\end{alignat" << star(n) << "}\n";
409 os << "\n\\end{xalignat" << star(n) << "}\n";
412 case LM_OT_XXALIGNAT:
413 os << "\n\\end{xxalignat}\n";
417 os << "\n\\end{multline}\n";
421 os << "\n\\end{gather}\n";
425 os << "\\end{unknown" << star(n) << "}";
430 void MathHullInset::addRow(row_type row)
432 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
433 label_.insert(label_.begin() + row + 1, string());
434 MathGridInset::addRow(row);
438 void MathHullInset::appendRow()
440 nonum_.push_back(!numberedType());
441 label_.push_back(string());
442 MathGridInset::appendRow();
446 void MathHullInset::delRow(row_type row)
448 MathGridInset::delRow(row);
449 nonum_.erase(nonum_.begin() + row);
450 label_.erase(label_.begin() + row);
454 void MathHullInset::addCol(col_type col)
458 mutate(LM_OT_EQNARRAY);
467 mutate(LM_OT_ALIGNAT);
473 case LM_OT_XXALIGNAT:
474 MathGridInset::addCol(col);
475 MathGridInset::addCol(col + 1);
484 void MathHullInset::delCol(col_type col)
489 case LM_OT_XXALIGNAT:
490 MathGridInset::delCol(col + 1);
491 MathGridInset::delCol(col);
499 string MathHullInset::nicelabel(row_type row) const
503 if (label_[row].empty())
504 return string("(#)");
505 return "(" + label_[row] + ")";
509 void MathHullInset::mutate(string const & newtype)
511 if (newtype == "dump") {
515 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
516 mutate(typecode(newtype));
520 void MathHullInset::glueall()
523 for (idx_type i = 0; i < nargs(); ++i)
524 ar.push_back(cell(i));
525 *this = MathHullInset(LM_OT_SIMPLE);
530 MathInsetTypes MathHullInset::getType() const
536 void MathHullInset::setType(MathInsetTypes t)
544 void MathHullInset::mutate(MathInsetTypes newtype)
546 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
548 if (newtype == getType())
553 setType(LM_OT_EQUATION);
561 setType(LM_OT_SIMPLE);
567 case LM_OT_XXALIGNAT: {
569 MathGridInset::addCol(1);
572 pos_type pos = firstRelOp(cell(0));
574 cell(0).erase(pos, cell(0).size());
575 cell(1).erase(0, pos);
576 setType(LM_OT_ALIGN);
583 MathGridInset::addCol(1);
584 MathGridInset::addCol(1);
586 // split it "nicely" on the firest relop
587 pos_type pos = firstRelOp(cell(0));
588 cell(1) = MathArray(cell(0), pos, cell(0).size());
589 cell(0).erase(pos, cell(0).size());
591 if (cell(1).size()) {
592 cell(2) = MathArray(cell(1), 1, cell(1).size());
593 cell(1).erase(1, cell(1).size());
596 setType(LM_OT_EQNARRAY);
605 case LM_OT_EQUATION: {
606 // set correct (no)numbering
607 bool allnonum = true;
608 for (row_type row = 0; row < nrows(); ++row) {
613 // set first non-empty label
615 for (row_type row = 0; row < nrows(); ++row) {
616 if (!label_[row].empty()) {
624 nonum_[0] = allnonum;
633 case LM_OT_XXALIGNAT:
635 for (row_type row = 0; row < nrows(); ++row) {
636 idx_type c = 3 * row + 1;
637 cell(c).push_back(cell(c + 1));
639 MathGridInset::delCol(2);
640 setType(LM_OT_ALIGN);
652 MathGridInset::addCol(1);
653 setType(LM_OT_EQNARRAY);
659 case LM_OT_XXALIGNAT:
664 lyxerr << "mutation from '" << getType()
665 << "' to '" << newtype << "' not implemented"
674 setType(LM_OT_GATHER);
677 lyxerr << "mutation from '" << getType()
678 << "' to '" << newtype << "' not implemented"
686 setType(LM_OT_MULTLINE);
689 lyxerr << "mutation from '" << getType()
690 << "' to '" << newtype << "' not implemented"
696 lyxerr << "mutation from '" << getType()
697 << "' to '" << newtype << "' not implemented"
704 void MathHullInset::write(WriteStream & os) const
708 bool n = numberedType();
710 for (row_type row = 0; row < nrows(); ++row) {
711 for (col_type col = 0; col < ncols(); ++col)
712 os << cell(index(row, col)) << eocString(col);
714 if (!label_[row].empty())
715 os << "\\label{" << label_[row] << "}";
719 os << eolString(row);
726 void MathHullInset::normalize(NormalStream & os) const
728 os << "[formula " << normalName(getType()) << " ";
729 MathGridInset::normalize(os);
734 void MathHullInset::mathmlize(MathMLStream & os) const
736 MathGridInset::mathmlize(os);
740 void MathHullInset::check() const
742 lyx::Assert(nonum_.size() == nrows());
743 lyx::Assert(label_.size() == nrows());