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);
200 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
202 MathFontSetChanger dummy(pi.base, standardFont());
203 MathGridInset::draw(pi, x + 1, y);
205 if (numberedType()) {
206 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
207 for (row_type row = 0; row < nrows(); ++row) {
208 int const yy = y + rowinfo_[row].offset_;
209 MathFontSetChanger dummy(pi.base, "mathrm");
210 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
214 drawMarkers2(pi, x, y);
218 void MathHullInset::metricsT(TextMetricsInfo const &) const
222 MathGridInset::metricsT(mi);
227 WriteStream wi(os, false, true);
229 width_ = os.str().size();
236 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
240 MathGridInset::drawT(pain, x, y);
245 WriteStream wi(os, false, true);
247 pain.draw(x, y, os.str().c_str());
252 string MathHullInset::label(row_type row) const
254 row_type n = nrows();
255 lyx::Assert(row < n);
260 void MathHullInset::label(row_type row, string const & label)
266 void MathHullInset::numbered(row_type row, bool num)
272 bool MathHullInset::numbered(row_type row) const
278 bool MathHullInset::ams() const
282 type_ == "multline" ||
284 type_ == "alignat" ||
285 type_ == "xalignat" ||
286 type_ == "xxalignat";
290 bool MathHullInset::display() const
292 return type_ != "simple" && type_ != "none";
296 vector<string> MathHullInset::getLabelList() const
299 for (row_type row = 0; row < nrows(); ++row)
300 if (!label_[row].empty() && nonum_[row] != 1)
301 res.push_back(label_[row]);
306 bool MathHullInset::numberedType() const
310 if (type_ == "simple")
312 if (type_ == "xxalignat")
314 for (row_type row = 0; row < nrows(); ++row)
321 void MathHullInset::validate(LaTeXFeatures & features) const
324 features.require("amsmath");
327 // Validation is necessary only if not using AMS math.
328 // To be safe, we will always run mathedvalidate.
329 //if (features.amsstyle)
332 features.require("boldsymbol");
333 //features.binom = true;
335 MathNestInset::validate(features);
339 void MathHullInset::header_write(WriteStream & os) const
341 bool n = numberedType();
346 else if (type_ == "simple") {
352 else if (type_ == "equation") {
354 os << "\\begin{equation" << star(n) << "}\n";
359 else if (type_ == "eqnarray" || type_ == "align")
360 os << "\\begin{" << type_ << star(n) << "}\n";
362 else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat")
363 os << "\\begin{" << type_ << star(n) << "}"
364 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
366 else if (type_ == "multline" || type_ == "gather")
367 os << "\\begin{" << type_ << "}\n";
370 os << "\\begin{unknown" << star(n) << "}";
374 void MathHullInset::footer_write(WriteStream & os) const
376 bool n = numberedType();
381 else if (type_ == "simple")
384 else if (type_ == "equation")
386 os << "\\end{equation" << star(n) << "}\n";
390 else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
391 || type_ == "xalignat")
392 os << "\n\\end{" << type_ << star(n) << "}\n";
394 else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
395 os << "\n\\end{" << type_ << "}\n";
398 os << "\\end{unknown" << star(n) << "}";
402 void MathHullInset::addRow(row_type row)
404 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
405 label_.insert(label_.begin() + row + 1, string());
406 MathGridInset::addRow(row);
410 void MathHullInset::delRow(row_type row)
412 MathGridInset::delRow(row);
413 nonum_.erase(nonum_.begin() + row);
414 label_.erase(label_.begin() + row);
418 void MathHullInset::addFancyCol(col_type col)
420 if (type_ == "equation")
423 else if (type_ == "eqnarray") {
428 else if (type_ == "align") {
433 else if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
434 MathGridInset::addCol(col);
435 MathGridInset::addCol(col + 1);
440 void MathHullInset::delFancyCol(col_type col)
442 if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat") {
443 MathGridInset::delCol(col + 1);
444 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::glueall()
462 for (idx_type i = 0; i < nargs(); ++i)
463 ar.push_back(cell(i));
464 *this = MathHullInset("simple");
470 string const & MathHullInset::getType() const
476 void MathHullInset::setType(string const & type)
484 void MathHullInset::mutate(string const & newtype)
486 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
488 // we try to move along the chain
489 // none <-> simple <-> equation <-> eqnarray
491 if (newtype == "dump") {
495 else if (newtype == type_) {
499 else if (type_ == "none") {
505 else if (type_ == "simple") {
506 if (newtype == "none") {
515 else if (type_ == "equation") {
516 if (smaller(newtype, type_)) {
519 } else if (newtype == "eqnarray") {
520 MathGridInset::addCol(1);
521 MathGridInset::addCol(1);
523 // split it "nicely" on the firest relop
524 pos_type pos = firstRelOp(cell(0));
525 cell(1) = MathArray(cell(0), pos, cell(0).size());
526 cell(0).erase(pos, cell(0).size());
528 if (cell(1).size()) {
529 cell(2) = MathArray(cell(1), 1, cell(1).size());
530 cell(1).erase(1, cell(1).size());
535 MathGridInset::addCol(1);
537 pos_type pos = firstRelOp(cell(0));
539 cell(0).erase(pos, cell(0).size());
540 cell(1).erase(0, pos);
546 else if (type_ == "eqnarray") {
547 if (smaller(newtype, type_)) {
548 // set correct (no)numbering
549 bool allnonum = true;
550 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()) {
564 nonum_[0] = allnonum;
567 } else { // align & Co.
568 for (row_type row = 0; row < nrows(); ++row) {
569 idx_type c = 3 * row + 1;
570 cell(c).push_back(cell(c + 1));
572 MathGridInset::delCol(2);
578 else if (type_ == "align") {
579 if (smaller(newtype, type_)) {
580 MathGridInset::addCol(1);
588 else if (type_ == "multline") {
589 if (newtype == "gather") {
592 lyxerr << "mutation from '" << type_
593 << "' to '" << newtype << "' not implemented"
598 else if (type_ == "gather") {
599 if (newtype == "multline") {
602 lyxerr << "mutation from '" << type_
603 << "' to '" << newtype << "' not implemented" << endl;
608 lyxerr << "mutation from '" << type_
609 << "' to '" << newtype << "' not implemented" << endl;
614 void MathHullInset::write(WriteStream & os) const
618 bool n = numberedType();
620 for (row_type row = 0; row < nrows(); ++row) {
621 for (col_type col = 0; col < ncols(); ++col)
622 os << cell(index(row, col)) << eocString(col);
624 if (!label_[row].empty())
625 os << "\\label{" << label_[row] << "}";
629 os << eolString(row);
636 void MathHullInset::normalize(NormalStream & os) const
638 os << "[formula " << type_ << " ";
639 MathGridInset::normalize(os);
644 void MathHullInset::mathmlize(MathMLStream & os) const
646 MathGridInset::mathmlize(os);
650 void MathHullInset::infoize(std::ostream & os) const
652 os << "Type: " << type_;
656 void MathHullInset::check() const
658 lyx::Assert(nonum_.size() == nrows());
659 lyx::Assert(label_.size() == nrows());