7 #include "math_hullinset.h"
8 #include "math_mathmlstream.h"
9 #include "math_streamstr.h"
10 #include "math_support.h"
13 #include "LaTeXFeatures.h"
14 #include "support/LAssert.h"
24 int getCols(MathInsetTypes type)
40 // returns position of first relation operator in the array
41 // used for "intelligent splitting"
42 MathArray::size_type firstRelOp(MathArray const & ar)
44 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
46 return it - ar.begin();
51 char const * star(bool numbered)
53 return numbered ? "" : "*";
56 MathInsetTypes typecode(string const & s)
58 if (s == "equation") return LM_OT_EQUATION;
59 if (s == "display") return LM_OT_EQUATION;
60 if (s == "eqnarray") return LM_OT_EQNARRAY;
61 if (s == "align") return LM_OT_ALIGN;
62 if (s == "alignat") return LM_OT_ALIGNAT;
63 if (s == "xalignat") return LM_OT_XALIGNAT;
64 if (s == "xxalignat") return LM_OT_XXALIGNAT;
65 if (s == "multline") return LM_OT_MULTLINE;
66 if (s == "gather") return LM_OT_GATHER;
71 string normalName(MathInsetTypes t)
74 case LM_OT_EQUATION: return "equation";
75 case LM_OT_EQNARRAY: return "eqnarray";
76 case LM_OT_ALIGN: return "align";
77 case LM_OT_ALIGNAT: return "alignat";
78 case LM_OT_XALIGNAT: return "xalignat";
79 case LM_OT_XXALIGNAT: return "xxalignat";
80 case LM_OT_MULTLINE: return "multline";
81 case LM_OT_GATHER: return "gather";
82 case LM_OT_SIMPLE: return "simple";
88 } // end anon namespace
91 MathHullInset::MathHullInset()
92 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
98 MathHullInset::MathHullInset(MathInsetTypes t)
99 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
105 MathHullInset::MathHullInset(MathInsetTypes t, MathGridInset const & grid)
106 : MathGridInset(grid), objtype_(t), nonum_(1), label_(1)
112 MathHullInset::MathHullInset(MathInsetTypes t, col_type cols)
113 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
119 MathInset * MathHullInset::clone() const
121 return new MathHullInset(*this);
125 char MathHullInset::defaultColAlign(col_type col)
131 case LM_OT_XXALIGNAT:
132 return "rl"[col & 1];
141 int MathHullInset::defaultColSpace(col_type col)
148 return (col & 1) ? 20 : 0;
149 case LM_OT_XXALIGNAT:
150 return (col & 1) ? 40 : 0;
157 void MathHullInset::metrics(MathMetricsInfo const & mi) const
160 mi_.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
162 // let the cells adjust themselves
163 MathGridInset::metrics(mi_);
170 if (numberedType()) {
172 for (row_type row = 0; row < nrows(); ++row)
173 l = max(l, mathed_string_width(LM_TC_BF, mi_, nicelabel(row)));
179 // make it at least as high as the current font
182 math_font_max_dim(LM_TC_TEXTRM, mi_, asc, des);
183 ascent_ = max(ascent_, asc);
184 descent_ = max(descent_, des);
188 void MathHullInset::draw(Painter & pain, int x, int y) const
190 MathGridInset::draw(pain, x, y);
192 if (numberedType()) {
193 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
194 for (row_type row = 0; row < nrows(); ++row) {
195 int const yy = y + rowinfo_[row].offset_;
196 drawStr(pain, LM_TC_BF, mi_, xx, yy, nicelabel(row));
202 void MathHullInset::metrics(TextMetricsInfo const & mi) const
206 width_ = normalName(objtype_).size();
210 void MathHullInset::draw(TextPainter & pain, int x, int y) const
212 pain.draw(x, y, normalName(objtype_).c_str());
216 string MathHullInset::label(row_type row) const
218 row_type n = nrows();
219 lyx::Assert(row < n);
224 void MathHullInset::label(row_type row, string const & label)
230 void MathHullInset::numbered(row_type row, bool num)
236 bool MathHullInset::numbered(row_type row) const
242 bool MathHullInset::ams() const
247 objtype_ == LM_OT_ALIGN ||
248 objtype_ == LM_OT_MULTLINE ||
249 objtype_ == LM_OT_GATHER ||
250 objtype_ == LM_OT_ALIGNAT ||
251 objtype_ == LM_OT_XALIGNAT ||
252 objtype_ == LM_OT_XXALIGNAT;
257 bool MathHullInset::display() const
259 return getType() != LM_OT_SIMPLE;
263 vector<string> const MathHullInset::getLabelList() const
266 for (row_type row = 0; row < nrows(); ++row)
267 if (!label_[row].empty() && nonum_[row] != 1)
268 res.push_back(label_[row]);
273 bool MathHullInset::numberedType() const
275 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
277 for (row_type row = 0; row < nrows(); ++row)
284 void MathHullInset::validate(LaTeXFeatures & features) const
287 features.require("amsmath");
290 // Validation is necessary only if not using AMS math.
291 // To be safe, we will always run mathedvalidate.
292 //if (features.amsstyle)
295 features.require("boldsymbol");
296 //features.binom = true;
298 MathNestInset::validate(features);
302 void MathHullInset::header_write(WriteStream & os) const
304 bool n = numberedType();
315 os << "\\begin{equation" << star(n) << "}\n";
321 os << "\\begin{eqnarray" << star(n) << "}\n";
325 os << "\\begin{align" << star(n) << "}\n";
329 os << "\\begin{alignat" << star(n) << "}"
330 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
334 os << "\\begin{xalignat" << star(n) << "}"
335 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
338 case LM_OT_XXALIGNAT:
339 os << "\\begin{xxalignat}"
340 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
344 os << "\\begin{multline}\n";
348 os << "\\begin{gather}\n";
352 os << "\\begin{unknown" << star(n) << "}";
357 void MathHullInset::footer_write(WriteStream & os) const
359 bool n = numberedType();
368 os << "\\end{equation" << star(n) << "}\n";
374 os << "\n\\end{eqnarray" << star(n) << "}\n";
378 os << "\n\\end{align" << star(n) << "}\n";
382 os << "\n\\end{alignat" << star(n) << "}\n";
386 os << "\n\\end{xalignat" << star(n) << "}\n";
389 case LM_OT_XXALIGNAT:
390 os << "\n\\end{xxalignat}\n";
394 os << "\n\\end{multline}\n";
398 os << "\n\\end{gather}\n";
402 os << "\\end{unknown" << star(n) << "}";
407 void MathHullInset::addRow(row_type row)
409 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
410 label_.insert(label_.begin() + row + 1, string());
411 MathGridInset::addRow(row);
415 void MathHullInset::appendRow()
417 nonum_.push_back(!numberedType());
418 label_.push_back(string());
419 MathGridInset::appendRow();
423 void MathHullInset::delRow(row_type row)
425 MathGridInset::delRow(row);
426 nonum_.erase(nonum_.begin() + row);
427 label_.erase(label_.begin() + row);
431 void MathHullInset::addCol(col_type col)
435 mutate(LM_OT_EQNARRAY);
444 mutate(LM_OT_ALIGNAT);
450 case LM_OT_XXALIGNAT:
451 MathGridInset::addCol(col);
452 MathGridInset::addCol(col + 1);
461 void MathHullInset::delCol(col_type col)
466 case LM_OT_XXALIGNAT:
467 MathGridInset::delCol(col + 1);
468 MathGridInset::delCol(col);
476 string MathHullInset::nicelabel(row_type row) const
480 if (label_[row].empty())
481 return string("(#)");
482 return "(" + label_[row] + ")";
486 void MathHullInset::mutate(string const & newtype)
488 if (newtype == "dump") {
492 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
493 mutate(typecode(newtype));
497 void MathHullInset::glueall()
500 for (idx_type i = 0; i < nargs(); ++i)
501 ar.push_back(cell(i));
502 *this = MathHullInset(LM_OT_SIMPLE);
507 MathInsetTypes MathHullInset::getType() const
513 void MathHullInset::setType(MathInsetTypes t)
521 void MathHullInset::mutate(MathInsetTypes newtype)
523 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
525 if (newtype == getType())
530 setType(LM_OT_EQUATION);
538 setType(LM_OT_SIMPLE);
544 case LM_OT_XXALIGNAT: {
546 MathGridInset::addCol(1);
549 pos_type pos = firstRelOp(cell(0));
551 cell(0).erase(pos, cell(0).size());
552 cell(1).erase(0, pos);
553 setType(LM_OT_ALIGN);
560 MathGridInset::addCol(1);
561 MathGridInset::addCol(1);
563 // split it "nicely" on the firest relop
564 pos_type pos = firstRelOp(cell(0));
565 cell(1) = MathArray(cell(0), pos, cell(0).size());
566 cell(0).erase(pos, cell(0).size());
568 if (cell(1).size()) {
569 cell(2) = MathArray(cell(1), 1, cell(1).size());
570 cell(1).erase(1, cell(1).size());
573 setType(LM_OT_EQNARRAY);
582 case LM_OT_EQUATION: {
583 // set correct (no)numbering
584 bool allnonum = true;
585 for (row_type row = 0; row < nrows(); ++row) {
590 // set first non-empty label
592 for (row_type row = 0; row < nrows(); ++row) {
593 if (!label_[row].empty()) {
601 nonum_[0] = allnonum;
610 case LM_OT_XXALIGNAT:
612 for (row_type row = 0; row < nrows(); ++row) {
613 idx_type c = 3 * row + 1;
614 cell(c).push_back(cell(c + 1));
616 MathGridInset::delCol(2);
617 setType(LM_OT_ALIGN);
629 MathGridInset::addCol(1);
630 setType(LM_OT_EQNARRAY);
636 case LM_OT_XXALIGNAT:
641 lyxerr << "mutation from '" << getType()
642 << "' to '" << newtype << "' not implemented"
651 setType(LM_OT_GATHER);
654 lyxerr << "mutation from '" << getType()
655 << "' to '" << newtype << "' not implemented"
663 setType(LM_OT_MULTLINE);
666 lyxerr << "mutation from '" << getType()
667 << "' to '" << newtype << "' not implemented"
673 lyxerr << "mutation from '" << getType()
674 << "' to '" << newtype << "' not implemented"
681 void MathHullInset::write(WriteStream & os) const
685 bool n = numberedType();
687 for (row_type row = 0; row < nrows(); ++row) {
688 for (col_type col = 0; col < ncols(); ++col)
689 os << cell(index(row, col)) << eocString(col);
691 if (!label_[row].empty())
692 os << "\\label{" << label_[row] << "}";
696 os << eolString(row);
703 void MathHullInset::normalize(NormalStream & os) const
705 os << "[formula " << normalName(getType()) << " ";
706 MathGridInset::normalize(os);
711 void MathHullInset::mathmlize(MathMLStream & os) const
713 MathGridInset::mathmlize(os);
717 void MathHullInset::check() const
719 lyx::Assert(nonum_.size() == nrows());
720 lyx::Assert(label_.size() == nrows());