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 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
136 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
139 pos = cell(idx).size();
144 char MathHullInset::defaultColAlign(col_type col)
150 case LM_OT_XXALIGNAT:
151 return "rl"[col & 1];
160 int MathHullInset::defaultColSpace(col_type col)
167 return (col & 1) ? 20 : 0;
168 case LM_OT_XXALIGNAT:
169 return (col & 1) ? 40 : 0;
176 void MathHullInset::metrics(MathMetricsInfo & mi) const
178 MathFontSetChanger dummy(mi.base, "mathnormal");
180 // let the cells adjust themselves
181 MathGridInset::metrics(mi);
188 if (numberedType()) {
189 MathFontSetChanger dummy(mi.base, "mathbf");
191 for (row_type row = 0; row < nrows(); ++row)
192 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
198 // make it at least as high as the current font
201 math_font_max_dim(mi.base.font, asc, des);
202 ascent_ = max(ascent_, asc);
203 descent_ = max(descent_, des);
207 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
209 MathFontSetChanger dummy(pi.base, "mathnormal");
210 MathGridInset::draw(pi, x, y);
212 if (numberedType()) {
213 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
214 for (row_type row = 0; row < nrows(); ++row) {
215 int const yy = y + rowinfo_[row].offset_;
216 drawStrBlack(pi, xx, yy, nicelabel(row));
222 void MathHullInset::metricsT(TextMetricsInfo const &) const
226 MathGridInset::metricsT(mi);
231 WriteStream wi(os, false, true);
233 width_ = os.str().size();
240 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
244 MathGridInset::drawT(pain, x, y);
249 WriteStream wi(os, false, true);
251 pain.draw(x, y, os.str().c_str());
256 string MathHullInset::label(row_type row) const
258 row_type n = nrows();
259 lyx::Assert(row < n);
264 void MathHullInset::label(row_type row, string const & label)
270 void MathHullInset::numbered(row_type row, bool num)
276 bool MathHullInset::numbered(row_type row) const
282 bool MathHullInset::ams() const
285 objtype_ == LM_OT_ALIGN ||
286 objtype_ == LM_OT_MULTLINE ||
287 objtype_ == LM_OT_GATHER ||
288 objtype_ == LM_OT_ALIGNAT ||
289 objtype_ == LM_OT_XALIGNAT ||
290 objtype_ == LM_OT_XXALIGNAT;
294 bool MathHullInset::display() const
296 return getType() != LM_OT_SIMPLE;
300 vector<string> const MathHullInset::getLabelList() const
303 for (row_type row = 0; row < nrows(); ++row)
304 if (!label_[row].empty() && nonum_[row] != 1)
305 res.push_back(label_[row]);
310 bool MathHullInset::numberedType() const
312 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_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();
352 os << "\\begin{equation" << star(n) << "}\n";
358 os << "\\begin{eqnarray" << star(n) << "}\n";
362 os << "\\begin{align" << star(n) << "}\n";
366 os << "\\begin{alignat" << star(n) << "}"
367 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
371 os << "\\begin{xalignat" << star(n) << "}"
372 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
375 case LM_OT_XXALIGNAT:
376 os << "\\begin{xxalignat}"
377 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
381 os << "\\begin{multline}\n";
385 os << "\\begin{gather}\n";
392 os << "\\begin{unknown" << star(n) << "}";
397 void MathHullInset::footer_write(WriteStream & os) const
399 bool n = numberedType();
408 os << "\\end{equation" << star(n) << "}\n";
414 os << "\n\\end{eqnarray" << star(n) << "}\n";
418 os << "\n\\end{align" << star(n) << "}\n";
422 os << "\n\\end{alignat" << star(n) << "}\n";
426 os << "\n\\end{xalignat" << star(n) << "}\n";
429 case LM_OT_XXALIGNAT:
430 os << "\n\\end{xxalignat}\n";
434 os << "\n\\end{multline}\n";
438 os << "\n\\end{gather}\n";
446 os << "\\end{unknown" << star(n) << "}";
451 void MathHullInset::addRow(row_type row)
453 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
454 label_.insert(label_.begin() + row + 1, string());
455 MathGridInset::addRow(row);
459 void MathHullInset::delRow(row_type row)
461 MathGridInset::delRow(row);
462 nonum_.erase(nonum_.begin() + row);
463 label_.erase(label_.begin() + row);
467 void MathHullInset::addFancyCol(col_type col)
471 mutate(LM_OT_EQNARRAY);
480 mutate(LM_OT_ALIGNAT);
486 case LM_OT_XXALIGNAT:
487 MathGridInset::addCol(col);
488 MathGridInset::addCol(col + 1);
497 void MathHullInset::delFancyCol(col_type col)
502 case LM_OT_XXALIGNAT:
503 MathGridInset::delCol(col + 1);
504 MathGridInset::delCol(col);
512 string MathHullInset::nicelabel(row_type row) const
516 if (label_[row].empty())
517 return string("(#)");
518 return "(" + label_[row] + ")";
522 void MathHullInset::mutate(string const & newtype)
524 if (newtype == "dump") {
528 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
529 mutate(typecode(newtype));
533 void MathHullInset::glueall()
536 for (idx_type i = 0; i < nargs(); ++i)
537 ar.push_back(cell(i));
538 *this = MathHullInset(LM_OT_SIMPLE);
543 MathInsetTypes MathHullInset::getType() const
549 void MathHullInset::setType(MathInsetTypes t)
557 void MathHullInset::mutate(MathInsetTypes newtype)
559 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
561 if (newtype == getType())
566 setType(LM_OT_EQUATION);
574 setType(LM_OT_SIMPLE);
580 case LM_OT_XXALIGNAT: {
582 MathGridInset::addCol(1);
585 pos_type pos = firstRelOp(cell(0));
587 cell(0).erase(pos, cell(0).size());
588 cell(1).erase(0, pos);
589 setType(LM_OT_ALIGN);
596 MathGridInset::addCol(1);
597 MathGridInset::addCol(1);
599 // split it "nicely" on the firest relop
600 pos_type pos = firstRelOp(cell(0));
601 cell(1) = MathArray(cell(0), pos, cell(0).size());
602 cell(0).erase(pos, cell(0).size());
604 if (cell(1).size()) {
605 cell(2) = MathArray(cell(1), 1, cell(1).size());
606 cell(1).erase(1, cell(1).size());
609 setType(LM_OT_EQNARRAY);
618 case LM_OT_EQUATION: {
619 // set correct (no)numbering
620 bool allnonum = true;
621 for (row_type row = 0; row < nrows(); ++row) {
626 // set first non-empty label
628 for (row_type row = 0; row < nrows(); ++row) {
629 if (!label_[row].empty()) {
637 nonum_[0] = allnonum;
646 case LM_OT_XXALIGNAT:
648 for (row_type row = 0; row < nrows(); ++row) {
649 idx_type c = 3 * row + 1;
650 cell(c).push_back(cell(c + 1));
652 MathGridInset::delCol(2);
653 setType(LM_OT_ALIGN);
665 MathGridInset::addCol(1);
666 setType(LM_OT_EQNARRAY);
672 case LM_OT_XXALIGNAT:
677 lyxerr << "mutation from '" << getType()
678 << "' to '" << newtype << "' not implemented"
687 setType(LM_OT_GATHER);
690 lyxerr << "mutation from '" << getType()
691 << "' to '" << newtype << "' not implemented"
699 setType(LM_OT_MULTLINE);
702 lyxerr << "mutation from '" << getType()
703 << "' to '" << newtype << "' not implemented"
709 lyxerr << "mutation from '" << getType()
710 << "' to '" << newtype << "' not implemented"
717 void MathHullInset::write(WriteStream & os) const
721 bool n = numberedType();
723 for (row_type row = 0; row < nrows(); ++row) {
724 for (col_type col = 0; col < ncols(); ++col)
725 os << cell(index(row, col)) << eocString(col);
727 if (!label_[row].empty())
728 os << "\\label{" << label_[row] << "}";
732 os << eolString(row);
739 void MathHullInset::normalize(NormalStream & os) const
741 os << "[formula " << normalName(getType()) << " ";
742 MathGridInset::normalize(os);
747 void MathHullInset::mathmlize(MathMLStream & os) const
749 MathGridInset::mathmlize(os);
753 void MathHullInset::infoize(std::ostream & os) const
755 os << normalName(getType());
758 void MathHullInset::check() const
760 lyx::Assert(nonum_.size() == nrows());
761 lyx::Assert(label_.size() == nrows());