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()) {
171 whichFont(mi_.font, LM_TC_BF, mi_);
173 for (row_type row = 0; row < nrows(); ++row)
174 l = max(l, mathed_string_width(mi_.font, nicelabel(row)));
180 // make it at least as high as the current font
183 math_font_max_dim(mi_.font, asc, des);
184 ascent_ = max(ascent_, asc);
185 descent_ = max(descent_, des);
189 void MathHullInset::draw(Painter & pain, int x, int y) const
191 MathGridInset::draw(pain, x, y);
193 if (numberedType()) {
194 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
195 for (row_type row = 0; row < nrows(); ++row) {
196 int const yy = y + rowinfo_[row].offset_;
197 drawStr(pain, mi_.font, xx, yy, nicelabel(row));
203 void MathHullInset::metricsT(TextMetricsInfo const & mi) const
207 width_ = normalName(objtype_).size();
211 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
213 pain.draw(x, y, normalName(objtype_).c_str());
214 MathGridInset::draw(pain, x, y);
219 string MathHullInset::label(row_type row) const
221 row_type n = nrows();
222 lyx::Assert(row < n);
227 void MathHullInset::label(row_type row, string const & label)
233 void MathHullInset::numbered(row_type row, bool num)
239 bool MathHullInset::numbered(row_type row) const
245 bool MathHullInset::ams() const
250 objtype_ == LM_OT_ALIGN ||
251 objtype_ == LM_OT_MULTLINE ||
252 objtype_ == LM_OT_GATHER ||
253 objtype_ == LM_OT_ALIGNAT ||
254 objtype_ == LM_OT_XALIGNAT ||
255 objtype_ == LM_OT_XXALIGNAT;
260 bool MathHullInset::display() const
262 return getType() != LM_OT_SIMPLE;
266 vector<string> const MathHullInset::getLabelList() const
269 for (row_type row = 0; row < nrows(); ++row)
270 if (!label_[row].empty() && nonum_[row] != 1)
271 res.push_back(label_[row]);
276 bool MathHullInset::numberedType() const
278 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
280 for (row_type row = 0; row < nrows(); ++row)
287 void MathHullInset::validate(LaTeXFeatures & features) const
290 features.require("amsmath");
293 // Validation is necessary only if not using AMS math.
294 // To be safe, we will always run mathedvalidate.
295 //if (features.amsstyle)
298 features.require("boldsymbol");
299 //features.binom = true;
301 MathNestInset::validate(features);
305 void MathHullInset::header_write(WriteStream & os) const
307 bool n = numberedType();
318 os << "\\begin{equation" << star(n) << "}\n";
324 os << "\\begin{eqnarray" << star(n) << "}\n";
328 os << "\\begin{align" << star(n) << "}\n";
332 os << "\\begin{alignat" << star(n) << "}"
333 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
337 os << "\\begin{xalignat" << star(n) << "}"
338 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
341 case LM_OT_XXALIGNAT:
342 os << "\\begin{xxalignat}"
343 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
347 os << "\\begin{multline}\n";
351 os << "\\begin{gather}\n";
355 os << "\\begin{unknown" << star(n) << "}";
360 void MathHullInset::footer_write(WriteStream & os) const
362 bool n = numberedType();
371 os << "\\end{equation" << star(n) << "}\n";
377 os << "\n\\end{eqnarray" << star(n) << "}\n";
381 os << "\n\\end{align" << star(n) << "}\n";
385 os << "\n\\end{alignat" << star(n) << "}\n";
389 os << "\n\\end{xalignat" << star(n) << "}\n";
392 case LM_OT_XXALIGNAT:
393 os << "\n\\end{xxalignat}\n";
397 os << "\n\\end{multline}\n";
401 os << "\n\\end{gather}\n";
405 os << "\\end{unknown" << star(n) << "}";
410 void MathHullInset::addRow(row_type row)
412 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
413 label_.insert(label_.begin() + row + 1, string());
414 MathGridInset::addRow(row);
418 void MathHullInset::appendRow()
420 nonum_.push_back(!numberedType());
421 label_.push_back(string());
422 MathGridInset::appendRow();
426 void MathHullInset::delRow(row_type row)
428 MathGridInset::delRow(row);
429 nonum_.erase(nonum_.begin() + row);
430 label_.erase(label_.begin() + row);
434 void MathHullInset::addCol(col_type col)
438 mutate(LM_OT_EQNARRAY);
447 mutate(LM_OT_ALIGNAT);
453 case LM_OT_XXALIGNAT:
454 MathGridInset::addCol(col);
455 MathGridInset::addCol(col + 1);
464 void MathHullInset::delCol(col_type col)
469 case LM_OT_XXALIGNAT:
470 MathGridInset::delCol(col + 1);
471 MathGridInset::delCol(col);
479 string MathHullInset::nicelabel(row_type row) const
483 if (label_[row].empty())
484 return string("(#)");
485 return "(" + label_[row] + ")";
489 void MathHullInset::mutate(string const & newtype)
491 if (newtype == "dump") {
495 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
496 mutate(typecode(newtype));
500 void MathHullInset::glueall()
503 for (idx_type i = 0; i < nargs(); ++i)
504 ar.push_back(cell(i));
505 *this = MathHullInset(LM_OT_SIMPLE);
510 MathInsetTypes MathHullInset::getType() const
516 void MathHullInset::setType(MathInsetTypes t)
524 void MathHullInset::mutate(MathInsetTypes newtype)
526 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
528 if (newtype == getType())
533 setType(LM_OT_EQUATION);
541 setType(LM_OT_SIMPLE);
547 case LM_OT_XXALIGNAT: {
549 MathGridInset::addCol(1);
552 pos_type pos = firstRelOp(cell(0));
554 cell(0).erase(pos, cell(0).size());
555 cell(1).erase(0, pos);
556 setType(LM_OT_ALIGN);
563 MathGridInset::addCol(1);
564 MathGridInset::addCol(1);
566 // split it "nicely" on the firest relop
567 pos_type pos = firstRelOp(cell(0));
568 cell(1) = MathArray(cell(0), pos, cell(0).size());
569 cell(0).erase(pos, cell(0).size());
571 if (cell(1).size()) {
572 cell(2) = MathArray(cell(1), 1, cell(1).size());
573 cell(1).erase(1, cell(1).size());
576 setType(LM_OT_EQNARRAY);
585 case LM_OT_EQUATION: {
586 // set correct (no)numbering
587 bool allnonum = true;
588 for (row_type row = 0; row < nrows(); ++row) {
593 // set first non-empty label
595 for (row_type row = 0; row < nrows(); ++row) {
596 if (!label_[row].empty()) {
604 nonum_[0] = allnonum;
613 case LM_OT_XXALIGNAT:
615 for (row_type row = 0; row < nrows(); ++row) {
616 idx_type c = 3 * row + 1;
617 cell(c).push_back(cell(c + 1));
619 MathGridInset::delCol(2);
620 setType(LM_OT_ALIGN);
632 MathGridInset::addCol(1);
633 setType(LM_OT_EQNARRAY);
639 case LM_OT_XXALIGNAT:
644 lyxerr << "mutation from '" << getType()
645 << "' to '" << newtype << "' not implemented"
654 setType(LM_OT_GATHER);
657 lyxerr << "mutation from '" << getType()
658 << "' to '" << newtype << "' not implemented"
666 setType(LM_OT_MULTLINE);
669 lyxerr << "mutation from '" << getType()
670 << "' to '" << newtype << "' not implemented"
676 lyxerr << "mutation from '" << getType()
677 << "' to '" << newtype << "' not implemented"
684 void MathHullInset::write(WriteStream & os) const
688 bool n = numberedType();
690 for (row_type row = 0; row < nrows(); ++row) {
691 for (col_type col = 0; col < ncols(); ++col)
692 os << cell(index(row, col)) << eocString(col);
694 if (!label_[row].empty())
695 os << "\\label{" << label_[row] << "}";
699 os << eolString(row);
706 void MathHullInset::normalize(NormalStream & os) const
708 os << "[formula " << normalName(getType()) << " ";
709 MathGridInset::normalize(os);
714 void MathHullInset::mathmlize(MathMLStream & os) const
716 MathGridInset::mathmlize(os);
720 void MathHullInset::check() const
722 lyx::Assert(nonum_.size() == nrows());
723 lyx::Assert(label_.size() == nrows());