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 == "none") return LM_OT_NONE;
61 if (s == "equation") return LM_OT_EQUATION;
62 if (s == "display") return LM_OT_EQUATION;
63 if (s == "eqnarray") return LM_OT_EQNARRAY;
64 if (s == "align") return LM_OT_ALIGN;
65 if (s == "alignat") return LM_OT_ALIGNAT;
66 if (s == "xalignat") return LM_OT_XALIGNAT;
67 if (s == "xxalignat") return LM_OT_XXALIGNAT;
68 if (s == "multline") return LM_OT_MULTLINE;
69 if (s == "gather") return LM_OT_GATHER;
74 string normalName(MathInsetTypes t)
77 case LM_OT_EQUATION: return "equation";
78 case LM_OT_EQNARRAY: return "eqnarray";
79 case LM_OT_ALIGN: return "align";
80 case LM_OT_ALIGNAT: return "alignat";
81 case LM_OT_XALIGNAT: return "xalignat";
82 case LM_OT_XXALIGNAT: return "xxalignat";
83 case LM_OT_MULTLINE: return "multline";
84 case LM_OT_GATHER: return "gather";
85 case LM_OT_SIMPLE: return "simple";
91 } // end anon namespace
94 MathHullInset::MathHullInset()
95 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
101 MathHullInset::MathHullInset(MathInsetTypes t)
102 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
108 MathHullInset::MathHullInset(MathInsetTypes t, MathGridInset const & grid)
109 : MathGridInset(grid), objtype_(t), nonum_(1), label_(1)
115 MathHullInset::MathHullInset(MathInsetTypes t, col_type cols)
116 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
122 MathInset * MathHullInset::clone() const
124 return new MathHullInset(*this);
128 char MathHullInset::defaultColAlign(col_type col)
134 case LM_OT_XXALIGNAT:
135 return "rl"[col & 1];
144 int MathHullInset::defaultColSpace(col_type col)
151 return (col & 1) ? 20 : 0;
152 case LM_OT_XXALIGNAT:
153 return (col & 1) ? 40 : 0;
160 void MathHullInset::metrics(MathMetricsInfo & mi) const
162 MathFontSetChanger dummy(mi.base, "mathnormal");
164 // let the cells adjust themselves
165 MathGridInset::metrics(mi);
172 if (numberedType()) {
173 MathFontSetChanger dummy(mi.base, "mathbf");
175 for (row_type row = 0; row < nrows(); ++row)
176 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
182 // make it at least as high as the current font
185 math_font_max_dim(mi.base.font, asc, des);
186 ascent_ = max(ascent_, asc);
187 descent_ = max(descent_, des);
191 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
193 MathFontSetChanger dummy(pi.base, "mathnormal");
194 MathGridInset::draw(pi, x, y);
196 if (numberedType()) {
197 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
198 for (row_type row = 0; row < nrows(); ++row) {
199 int const yy = y + rowinfo_[row].offset_;
200 drawStrBlack(pi, xx, yy, nicelabel(row));
206 void MathHullInset::metricsT(TextMetricsInfo const &) const
210 MathGridInset::metricsT(mi);
215 WriteStream wi(os, false, true);
217 width_ = os.str().size();
224 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
228 MathGridInset::drawT(pain, x, y);
233 WriteStream wi(os, false, true);
235 pain.draw(x, y, os.str().c_str());
240 string MathHullInset::label(row_type row) const
242 row_type n = nrows();
243 lyx::Assert(row < n);
248 void MathHullInset::label(row_type row, string const & label)
254 void MathHullInset::numbered(row_type row, bool num)
260 bool MathHullInset::numbered(row_type row) const
266 bool MathHullInset::ams() const
269 objtype_ == LM_OT_ALIGN ||
270 objtype_ == LM_OT_MULTLINE ||
271 objtype_ == LM_OT_GATHER ||
272 objtype_ == LM_OT_ALIGNAT ||
273 objtype_ == LM_OT_XALIGNAT ||
274 objtype_ == LM_OT_XXALIGNAT;
278 bool MathHullInset::display() const
280 return getType() != LM_OT_SIMPLE;
284 vector<string> const MathHullInset::getLabelList() const
287 for (row_type row = 0; row < nrows(); ++row)
288 if (!label_[row].empty() && nonum_[row] != 1)
289 res.push_back(label_[row]);
294 bool MathHullInset::numberedType() const
296 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
298 for (row_type row = 0; row < nrows(); ++row)
305 void MathHullInset::validate(LaTeXFeatures & features) const
308 features.require("amsmath");
311 // Validation is necessary only if not using AMS math.
312 // To be safe, we will always run mathedvalidate.
313 //if (features.amsstyle)
316 features.require("boldsymbol");
317 //features.binom = true;
319 MathNestInset::validate(features);
323 void MathHullInset::header_write(WriteStream & os) const
325 bool n = numberedType();
336 os << "\\begin{equation" << star(n) << "}\n";
342 os << "\\begin{eqnarray" << star(n) << "}\n";
346 os << "\\begin{align" << star(n) << "}\n";
350 os << "\\begin{alignat" << star(n) << "}"
351 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
355 os << "\\begin{xalignat" << star(n) << "}"
356 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
359 case LM_OT_XXALIGNAT:
360 os << "\\begin{xxalignat}"
361 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
365 os << "\\begin{multline}\n";
369 os << "\\begin{gather}\n";
376 os << "\\begin{unknown" << star(n) << "}";
381 void MathHullInset::footer_write(WriteStream & os) const
383 bool n = numberedType();
392 os << "\\end{equation" << star(n) << "}\n";
398 os << "\n\\end{eqnarray" << star(n) << "}\n";
402 os << "\n\\end{align" << star(n) << "}\n";
406 os << "\n\\end{alignat" << star(n) << "}\n";
410 os << "\n\\end{xalignat" << star(n) << "}\n";
413 case LM_OT_XXALIGNAT:
414 os << "\n\\end{xxalignat}\n";
418 os << "\n\\end{multline}\n";
422 os << "\n\\end{gather}\n";
430 os << "\\end{unknown" << star(n) << "}";
435 void MathHullInset::addRow(row_type row)
437 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
438 label_.insert(label_.begin() + row + 1, string());
439 MathGridInset::addRow(row);
443 void MathHullInset::delRow(row_type row)
445 MathGridInset::delRow(row);
446 nonum_.erase(nonum_.begin() + row);
447 label_.erase(label_.begin() + row);
451 void MathHullInset::addFancyCol(col_type col)
455 mutate(LM_OT_EQNARRAY);
464 mutate(LM_OT_ALIGNAT);
470 case LM_OT_XXALIGNAT:
471 MathGridInset::addCol(col);
472 MathGridInset::addCol(col + 1);
481 void MathHullInset::delFancyCol(col_type col)
486 case LM_OT_XXALIGNAT:
487 MathGridInset::delCol(col + 1);
488 MathGridInset::delCol(col);
496 string MathHullInset::nicelabel(row_type row) const
500 if (label_[row].empty())
501 return string("(#)");
502 return "(" + label_[row] + ")";
506 void MathHullInset::mutate(string const & newtype)
508 if (newtype == "dump") {
512 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
513 mutate(typecode(newtype));
517 void MathHullInset::glueall()
520 for (idx_type i = 0; i < nargs(); ++i)
521 ar.push_back(cell(i));
522 *this = MathHullInset(LM_OT_SIMPLE);
527 MathInsetTypes MathHullInset::getType() const
533 void MathHullInset::setType(MathInsetTypes t)
541 void MathHullInset::mutate(MathInsetTypes newtype)
543 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
545 if (newtype == getType())
550 setType(LM_OT_EQUATION);
558 setType(LM_OT_SIMPLE);
564 case LM_OT_XXALIGNAT: {
566 MathGridInset::addCol(1);
569 pos_type pos = firstRelOp(cell(0));
571 cell(0).erase(pos, cell(0).size());
572 cell(1).erase(0, pos);
573 setType(LM_OT_ALIGN);
580 MathGridInset::addCol(1);
581 MathGridInset::addCol(1);
583 // split it "nicely" on the firest relop
584 pos_type pos = firstRelOp(cell(0));
585 cell(1) = MathArray(cell(0), pos, cell(0).size());
586 cell(0).erase(pos, cell(0).size());
588 if (cell(1).size()) {
589 cell(2) = MathArray(cell(1), 1, cell(1).size());
590 cell(1).erase(1, cell(1).size());
593 setType(LM_OT_EQNARRAY);
602 case LM_OT_EQUATION: {
603 // set correct (no)numbering
604 bool allnonum = true;
605 for (row_type row = 0; row < nrows(); ++row) {
610 // set first non-empty label
612 for (row_type row = 0; row < nrows(); ++row) {
613 if (!label_[row].empty()) {
621 nonum_[0] = allnonum;
630 case LM_OT_XXALIGNAT:
632 for (row_type row = 0; row < nrows(); ++row) {
633 idx_type c = 3 * row + 1;
634 cell(c).push_back(cell(c + 1));
636 MathGridInset::delCol(2);
637 setType(LM_OT_ALIGN);
649 MathGridInset::addCol(1);
650 setType(LM_OT_EQNARRAY);
656 case LM_OT_XXALIGNAT:
661 lyxerr << "mutation from '" << getType()
662 << "' to '" << newtype << "' not implemented"
671 setType(LM_OT_GATHER);
674 lyxerr << "mutation from '" << getType()
675 << "' to '" << newtype << "' not implemented"
683 setType(LM_OT_MULTLINE);
686 lyxerr << "mutation from '" << getType()
687 << "' to '" << newtype << "' not implemented"
693 lyxerr << "mutation from '" << getType()
694 << "' to '" << newtype << "' not implemented"
701 void MathHullInset::write(WriteStream & os) const
705 bool n = numberedType();
707 for (row_type row = 0; row < nrows(); ++row) {
708 for (col_type col = 0; col < ncols(); ++col)
709 os << cell(index(row, col)) << eocString(col);
711 if (!label_[row].empty())
712 os << "\\label{" << label_[row] << "}";
716 os << eolString(row);
723 void MathHullInset::normalize(NormalStream & os) const
725 os << "[formula " << normalName(getType()) << " ";
726 MathGridInset::normalize(os);
731 void MathHullInset::mathmlize(MathMLStream & os) const
733 MathGridInset::mathmlize(os);
737 void MathHullInset::infoize(std::ostream & os) const
739 os << normalName(getType());
742 void MathHullInset::check() const
744 lyx::Assert(nonum_.size() == nrows());
745 lyx::Assert(label_.size() == nrows());