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
163 // let the cells adjust themselves
164 MathGridInset::metrics(mi);
171 if (numberedType()) {
172 //augmentFont(mi_.base.font, "mathbf");
174 for (row_type row = 0; row < nrows(); ++row)
175 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
181 // make it at least as high as the current font
184 math_font_max_dim(mi.base.font, asc, des);
185 ascent_ = max(ascent_, asc);
186 descent_ = max(descent_, des);
190 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
192 MathGridInset::draw(pi, x, y);
194 if (numberedType()) {
195 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
196 for (row_type row = 0; row < nrows(); ++row) {
197 int const yy = y + rowinfo_[row].offset_;
198 drawStrBlack(pi, xx, yy, nicelabel(row));
204 void MathHullInset::metricsT(TextMetricsInfo const &) const
208 MathGridInset::metricsT(mi);
213 WriteStream wi(os, false, true);
215 width_ = os.str().size();
222 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
226 MathGridInset::drawT(pain, x, y);
231 WriteStream wi(os, false, true);
233 pain.draw(x, y, os.str().c_str());
238 string MathHullInset::label(row_type row) const
240 row_type n = nrows();
241 lyx::Assert(row < n);
246 void MathHullInset::label(row_type row, string const & label)
252 void MathHullInset::numbered(row_type row, bool num)
258 bool MathHullInset::numbered(row_type row) const
264 bool MathHullInset::ams() const
267 objtype_ == LM_OT_ALIGN ||
268 objtype_ == LM_OT_MULTLINE ||
269 objtype_ == LM_OT_GATHER ||
270 objtype_ == LM_OT_ALIGNAT ||
271 objtype_ == LM_OT_XALIGNAT ||
272 objtype_ == LM_OT_XXALIGNAT;
276 bool MathHullInset::display() const
278 return getType() != LM_OT_SIMPLE;
282 vector<string> const MathHullInset::getLabelList() const
285 for (row_type row = 0; row < nrows(); ++row)
286 if (!label_[row].empty() && nonum_[row] != 1)
287 res.push_back(label_[row]);
292 bool MathHullInset::numberedType() const
294 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
296 for (row_type row = 0; row < nrows(); ++row)
303 void MathHullInset::validate(LaTeXFeatures & features) const
306 features.require("amsmath");
309 // Validation is necessary only if not using AMS math.
310 // To be safe, we will always run mathedvalidate.
311 //if (features.amsstyle)
314 features.require("boldsymbol");
315 //features.binom = true;
317 MathNestInset::validate(features);
321 void MathHullInset::header_write(WriteStream & os) const
323 bool n = numberedType();
334 os << "\\begin{equation" << star(n) << "}\n";
340 os << "\\begin{eqnarray" << star(n) << "}\n";
344 os << "\\begin{align" << star(n) << "}\n";
348 os << "\\begin{alignat" << star(n) << "}"
349 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
353 os << "\\begin{xalignat" << star(n) << "}"
354 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
357 case LM_OT_XXALIGNAT:
358 os << "\\begin{xxalignat}"
359 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
363 os << "\\begin{multline}\n";
367 os << "\\begin{gather}\n";
374 os << "\\begin{unknown" << star(n) << "}";
379 void MathHullInset::footer_write(WriteStream & os) const
381 bool n = numberedType();
390 os << "\\end{equation" << star(n) << "}\n";
396 os << "\n\\end{eqnarray" << star(n) << "}\n";
400 os << "\n\\end{align" << star(n) << "}\n";
404 os << "\n\\end{alignat" << star(n) << "}\n";
408 os << "\n\\end{xalignat" << star(n) << "}\n";
411 case LM_OT_XXALIGNAT:
412 os << "\n\\end{xxalignat}\n";
416 os << "\n\\end{multline}\n";
420 os << "\n\\end{gather}\n";
428 os << "\\end{unknown" << star(n) << "}";
433 void MathHullInset::addRow(row_type row)
435 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
436 label_.insert(label_.begin() + row + 1, string());
437 MathGridInset::addRow(row);
441 void MathHullInset::delRow(row_type row)
443 MathGridInset::delRow(row);
444 nonum_.erase(nonum_.begin() + row);
445 label_.erase(label_.begin() + row);
449 void MathHullInset::addFancyCol(col_type col)
453 mutate(LM_OT_EQNARRAY);
462 mutate(LM_OT_ALIGNAT);
468 case LM_OT_XXALIGNAT:
469 MathGridInset::addCol(col);
470 MathGridInset::addCol(col + 1);
479 void MathHullInset::delFancyCol(col_type col)
484 case LM_OT_XXALIGNAT:
485 MathGridInset::delCol(col + 1);
486 MathGridInset::delCol(col);
494 string MathHullInset::nicelabel(row_type row) const
498 if (label_[row].empty())
499 return string("(#)");
500 return "(" + label_[row] + ")";
504 void MathHullInset::mutate(string const & newtype)
506 if (newtype == "dump") {
510 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
511 mutate(typecode(newtype));
515 void MathHullInset::glueall()
518 for (idx_type i = 0; i < nargs(); ++i)
519 ar.push_back(cell(i));
520 *this = MathHullInset(LM_OT_SIMPLE);
525 MathInsetTypes MathHullInset::getType() const
531 void MathHullInset::setType(MathInsetTypes t)
539 void MathHullInset::mutate(MathInsetTypes newtype)
541 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
543 if (newtype == getType())
548 setType(LM_OT_EQUATION);
556 setType(LM_OT_SIMPLE);
562 case LM_OT_XXALIGNAT: {
564 MathGridInset::addCol(1);
567 pos_type pos = firstRelOp(cell(0));
569 cell(0).erase(pos, cell(0).size());
570 cell(1).erase(0, pos);
571 setType(LM_OT_ALIGN);
578 MathGridInset::addCol(1);
579 MathGridInset::addCol(1);
581 // split it "nicely" on the firest relop
582 pos_type pos = firstRelOp(cell(0));
583 cell(1) = MathArray(cell(0), pos, cell(0).size());
584 cell(0).erase(pos, cell(0).size());
586 if (cell(1).size()) {
587 cell(2) = MathArray(cell(1), 1, cell(1).size());
588 cell(1).erase(1, cell(1).size());
591 setType(LM_OT_EQNARRAY);
600 case LM_OT_EQUATION: {
601 // set correct (no)numbering
602 bool allnonum = true;
603 for (row_type row = 0; row < nrows(); ++row) {
608 // set first non-empty label
610 for (row_type row = 0; row < nrows(); ++row) {
611 if (!label_[row].empty()) {
619 nonum_[0] = allnonum;
628 case LM_OT_XXALIGNAT:
630 for (row_type row = 0; row < nrows(); ++row) {
631 idx_type c = 3 * row + 1;
632 cell(c).push_back(cell(c + 1));
634 MathGridInset::delCol(2);
635 setType(LM_OT_ALIGN);
647 MathGridInset::addCol(1);
648 setType(LM_OT_EQNARRAY);
654 case LM_OT_XXALIGNAT:
659 lyxerr << "mutation from '" << getType()
660 << "' to '" << newtype << "' not implemented"
669 setType(LM_OT_GATHER);
672 lyxerr << "mutation from '" << getType()
673 << "' to '" << newtype << "' not implemented"
681 setType(LM_OT_MULTLINE);
684 lyxerr << "mutation from '" << getType()
685 << "' to '" << newtype << "' not implemented"
691 lyxerr << "mutation from '" << getType()
692 << "' to '" << newtype << "' not implemented"
699 void MathHullInset::write(WriteStream & os) const
703 bool n = numberedType();
705 for (row_type row = 0; row < nrows(); ++row) {
706 for (col_type col = 0; col < ncols(); ++col)
707 os << cell(index(row, col)) << eocString(col);
709 if (!label_[row].empty())
710 os << "\\label{" << label_[row] << "}";
714 os << eolString(row);
721 void MathHullInset::normalize(NormalStream & os) const
723 os << "[formula " << normalName(getType()) << " ";
724 MathGridInset::normalize(os);
729 void MathHullInset::mathmlize(MathMLStream & os) const
731 MathGridInset::mathmlize(os);
735 void MathHullInset::infoize(std::ostream & os) const
737 os << normalName(getType());
740 void MathHullInset::check() const
742 lyx::Assert(nonum_.size() == nrows());
743 lyx::Assert(label_.size() == nrows());