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(string const & type)
28 if (type == "eqnarray")
32 if (type == "alignat")
34 if (type == "xalignat")
36 if (type == "xxalignat")
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 ? "" : "*";
59 int typecode(string const & s)
61 if (s == "none") return 0;
62 if (s == "simple") return 1;
63 if (s == "equation") return 2;
64 if (s == "eqnarray") return 3;
65 if (s == "align") return 4;
66 if (s == "alignat") return 5;
67 if (s == "xalignat") return 6;
68 if (s == "xxalignat") return 7;
69 if (s == "multline") return 8;
70 if (s == "gather") return 9;
71 lyxerr << "unknown hull type '" << s << "'\n";
75 bool smaller(string const & s, string const & t)
77 return typecode(s) < typecode(t);
81 } // end anon namespace
84 MathHullInset::MathHullInset()
85 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
91 MathHullInset::MathHullInset(string const & type)
92 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
98 MathHullInset::MathHullInset(string const & type, MathGridInset const & grid)
99 : MathGridInset(grid), type_(type), nonum_(1), label_(1)
105 MathHullInset::MathHullInset(string const & type, col_type cols)
106 : MathGridInset(cols, 1), type_(type), nonum_(1), label_(1)
112 MathInset * MathHullInset::clone() const
114 return new MathHullInset(*this);
118 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
126 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
129 pos = cell(idx).size();
134 char MathHullInset::defaultColAlign(col_type col)
136 if (type_ == "eqnarray")
138 if (typecode(type_) >= typecode("align"))
139 return "rl"[col & 1];
144 int MathHullInset::defaultColSpace(col_type col)
146 if (type_ == "align" || type_ == "alignat")
148 if (type_ == "xalignat")
149 return (col & 1) ? 20 : 0;
150 if (type_ == "xxalignat")
151 return (col & 1) ? 40 : 0;
156 char const * MathHullInset::standardFont() const
159 return "lyxnochange";
164 void MathHullInset::metrics(MathMetricsInfo & mi) const
166 MathFontSetChanger dummy(mi.base, standardFont());
168 // let the cells adjust themselves
169 MathGridInset::metrics(mi);
176 if (numberedType()) {
177 MathFontSetChanger dummy(mi.base, "mathbf");
179 for (row_type row = 0; row < nrows(); ++row)
180 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
186 // make it at least as high as the current font
189 math_font_max_dim(mi.base.font, asc, des);
190 ascent_ = max(ascent_, asc);
191 descent_ = max(descent_, des);
199 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
201 MathFontSetChanger dummy(pi.base, standardFont());
202 MathGridInset::draw(pi, x + 1, y);
204 if (numberedType()) {
205 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
206 for (row_type row = 0; row < nrows(); ++row) {
207 int const yy = y + rowinfo_[row].offset_;
208 MathFontSetChanger dummy(pi.base, "mathrm");
209 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
213 drawMarkers(pi, x, y);
217 void MathHullInset::metricsT(TextMetricsInfo const &) const
221 MathGridInset::metricsT(mi);
226 WriteStream wi(os, false, true);
228 width_ = os.str().size();
235 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
239 MathGridInset::drawT(pain, x, y);
244 WriteStream wi(os, false, true);
246 pain.draw(x, y, os.str().c_str());
251 string MathHullInset::label(row_type row) const
253 row_type n = nrows();
254 lyx::Assert(row < n);
259 void MathHullInset::label(row_type row, string const & label)
265 void MathHullInset::numbered(row_type row, bool num)
271 bool MathHullInset::numbered(row_type row) const
277 bool MathHullInset::ams() const
281 type_ == "multline" ||
283 type_ == "alignat" ||
284 type_ == "xalignat" ||
285 type_ == "xxalignat";
289 bool MathHullInset::display() const
291 return type_ != "simple" && type_ != "none";
295 vector<string> MathHullInset::getLabelList() const
298 for (row_type row = 0; row < nrows(); ++row)
299 if (!label_[row].empty() && nonum_[row] != 1)
300 res.push_back(label_[row]);
305 bool MathHullInset::numberedType() const
309 if (type_ == "simple")
311 if (type_ == "xxalignat")
313 for (row_type row = 0; row < nrows(); ++row)
320 void MathHullInset::validate(LaTeXFeatures & features) const
323 features.require("amsmath");
326 // Validation is necessary only if not using AMS math.
327 // To be safe, we will always run mathedvalidate.
328 //if (features.amsstyle)
331 features.require("boldsymbol");
332 //features.binom = true;
334 MathNestInset::validate(features);
338 void MathHullInset::header_write(WriteStream & os) const
340 bool n = numberedType();
345 else if (type_ == "simple") {
351 else if (type_ == "equation") {
353 os << "\\begin{equation" << star(n) << "}\n";
358 else if (type_ == "eqnarray" || type_ == "align")
359 os << "\\begin{" << type_ << star(n) << "}\n";
361 else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat")
362 os << "\\begin{" << type_ << star(n) << "}"
363 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
365 else if (type_ == "multline" || type_ == "gather")
366 os << "\\begin{" << type_ << "}\n";
369 os << "\\begin{unknown" << star(n) << "}";
373 void MathHullInset::footer_write(WriteStream & os) const
375 bool n = numberedType();
380 else if (type_ == "simple")
383 else if (type_ == "equation")
385 os << "\\end{equation" << star(n) << "}\n";
389 else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
390 || type_ == "xalignat")
391 os << "\n\\end{" << type_ << star(n) << "}\n";
393 else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
394 os << "\n\\end{" << type_ << "}\n";
397 os << "\\end{unknown" << star(n) << "}";
401 void MathHullInset::addRow(row_type row)
403 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
404 label_.insert(label_.begin() + row + 1, string());
405 MathGridInset::addRow(row);
409 void MathHullInset::delRow(row_type row)
411 MathGridInset::delRow(row);
412 nonum_.erase(nonum_.begin() + row);
413 label_.erase(label_.begin() + row);
417 void MathHullInset::addFancyCol(col_type col)
419 if (type_ == "equation")
422 else if (type_ == "eqnarray") {
427 else if (type_ == "align") {
432 else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
433 MathGridInset::addCol(col);
434 MathGridInset::addCol(col + 1);
439 void MathHullInset::delFancyCol(col_type col)
441 if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
442 MathGridInset::delCol(col + 1);
443 MathGridInset::delCol(col);
448 string MathHullInset::nicelabel(row_type row) const
452 if (label_[row].empty())
453 return string("(#)");
454 return "(" + label_[row] + ")";
458 void MathHullInset::glueall()
461 for (idx_type i = 0; i < nargs(); ++i)
462 ar.push_back(cell(i));
463 *this = MathHullInset("simple");
469 string const & MathHullInset::getType() const
475 void MathHullInset::setType(string const & type)
483 void MathHullInset::mutate(string const & newtype)
485 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
487 // we try to move along the chain
488 // none <-> simple <-> equation <-> eqnarray
490 if (newtype == "dump") {
494 else if (newtype == type_) {
498 else if (type_ == "none") {
504 else if (type_ == "simple") {
505 if (newtype == "none") {
514 else if (type_ == "equation") {
515 if (smaller(newtype, type_)) {
518 } else if (newtype == "eqnarray") {
519 MathGridInset::addCol(1);
520 MathGridInset::addCol(1);
522 // split it "nicely" on the firest relop
523 pos_type pos = firstRelOp(cell(0));
524 cell(1) = MathArray(cell(0), pos, cell(0).size());
525 cell(0).erase(pos, cell(0).size());
527 if (cell(1).size()) {
528 cell(2) = MathArray(cell(1), 1, cell(1).size());
529 cell(1).erase(1, cell(1).size());
534 MathGridInset::addCol(1);
536 pos_type pos = firstRelOp(cell(0));
538 cell(0).erase(pos, cell(0).size());
539 cell(1).erase(0, pos);
545 else if (type_ == "eqnarray") {
546 if (smaller(newtype, type_)) {
547 // set correct (no)numbering
548 bool allnonum = true;
549 for (row_type row = 0; row < nrows(); ++row)
553 // set first non-empty label
555 for (row_type row = 0; row < nrows(); ++row) {
556 if (!label_[row].empty()) {
563 nonum_[0] = allnonum;
566 } else { // align & Co.
567 for (row_type row = 0; row < nrows(); ++row) {
568 idx_type c = 3 * row + 1;
569 cell(c).push_back(cell(c + 1));
571 MathGridInset::delCol(2);
577 else if (type_ == "align") {
578 if (smaller(newtype, type_)) {
579 MathGridInset::addCol(1);
587 else if (type_ == "multline") {
588 if (newtype == "gather") {
591 lyxerr << "mutation from '" << type_
592 << "' to '" << newtype << "' not implemented"
597 else if (type_ == "gather") {
598 if (newtype == "multline") {
601 lyxerr << "mutation from '" << type_
602 << "' to '" << newtype << "' not implemented" << endl;
607 lyxerr << "mutation from '" << type_
608 << "' to '" << newtype << "' not implemented" << endl;
613 void MathHullInset::write(WriteStream & os) const
617 bool n = numberedType();
619 for (row_type row = 0; row < nrows(); ++row) {
620 for (col_type col = 0; col < ncols(); ++col)
621 os << cell(index(row, col)) << eocString(col);
623 if (!label_[row].empty())
624 os << "\\label{" << label_[row] << "}";
628 os << eolString(row);
635 void MathHullInset::normalize(NormalStream & os) const
637 os << "[formula " << type_ << " ";
638 MathGridInset::normalize(os);
643 void MathHullInset::mathmlize(MathMLStream & os) const
645 MathGridInset::mathmlize(os);
649 void MathHullInset::infoize(std::ostream & os) const
651 os << "Type: " << type_;
655 void MathHullInset::check() const
657 lyx::Assert(nonum_.size() == nrows());
658 lyx::Assert(label_.size() == nrows());
669 MathParInset::MathParInset()
671 lyxerr << "constructing MathParInset\n";
675 void MathParInset::metrics(MathMetricsInfo & mi) const
677 MathFontSetChanger dummy(mi.base, "textnormal");
678 MathGridInset::metrics(mi);
682 void MathParInset::draw(MathPainterInfo & pi, int x, int y) const
684 MathFontSetChanger dummy(pi.base, "textnormal");
685 MathGridInset::draw(pi, x, y);
689 void MathParInset::write(WriteStream & os) const
691 for (idx_type i = 0; i < nargs(); ++i)
692 os << cell(i) << "\n";
696 void MathParInset::infoize(std::ostream & os) const
698 os << "Type: Paragraph ";