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 // let the cells adjust themselves
163 MathGridInset::metrics(mi);
171 mi_.base.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
173 if (numberedType()) {
174 //augmentFont(mi_.base.font, "mathbf");
176 for (row_type row = 0; row < nrows(); ++row)
177 l = max(l, mathed_string_width(mi_.base.font, nicelabel(row)));
183 // make it at least as high as the current font
186 math_font_max_dim(mi_.base.font, asc, des);
187 ascent_ = max(ascent_, asc);
188 descent_ = max(descent_, des);
192 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
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
271 objtype_ == LM_OT_ALIGN ||
272 objtype_ == LM_OT_MULTLINE ||
273 objtype_ == LM_OT_GATHER ||
274 objtype_ == LM_OT_ALIGNAT ||
275 objtype_ == LM_OT_XALIGNAT ||
276 objtype_ == LM_OT_XXALIGNAT;
281 bool MathHullInset::display() const
283 return getType() != LM_OT_SIMPLE;
287 vector<string> const MathHullInset::getLabelList() const
290 for (row_type row = 0; row < nrows(); ++row)
291 if (!label_[row].empty() && nonum_[row] != 1)
292 res.push_back(label_[row]);
297 bool MathHullInset::numberedType() const
299 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
301 for (row_type row = 0; row < nrows(); ++row)
308 void MathHullInset::validate(LaTeXFeatures & features) const
311 features.require("amsmath");
314 // Validation is necessary only if not using AMS math.
315 // To be safe, we will always run mathedvalidate.
316 //if (features.amsstyle)
319 features.require("boldsymbol");
320 //features.binom = true;
322 MathNestInset::validate(features);
326 void MathHullInset::header_write(WriteStream & os) const
328 bool n = numberedType();
339 os << "\\begin{equation" << star(n) << "}\n";
345 os << "\\begin{eqnarray" << star(n) << "}\n";
349 os << "\\begin{align" << star(n) << "}\n";
353 os << "\\begin{alignat" << star(n) << "}"
354 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
358 os << "\\begin{xalignat" << star(n) << "}"
359 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
362 case LM_OT_XXALIGNAT:
363 os << "\\begin{xxalignat}"
364 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
368 os << "\\begin{multline}\n";
372 os << "\\begin{gather}\n";
379 os << "\\begin{unknown" << star(n) << "}";
384 void MathHullInset::footer_write(WriteStream & os) const
386 bool n = numberedType();
395 os << "\\end{equation" << star(n) << "}\n";
401 os << "\n\\end{eqnarray" << star(n) << "}\n";
405 os << "\n\\end{align" << star(n) << "}\n";
409 os << "\n\\end{alignat" << star(n) << "}\n";
413 os << "\n\\end{xalignat" << star(n) << "}\n";
416 case LM_OT_XXALIGNAT:
417 os << "\n\\end{xxalignat}\n";
421 os << "\n\\end{multline}\n";
425 os << "\n\\end{gather}\n";
433 os << "\\end{unknown" << star(n) << "}";
438 void MathHullInset::addRow(row_type row)
440 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
441 label_.insert(label_.begin() + row + 1, string());
442 MathGridInset::addRow(row);
446 void MathHullInset::appendRow()
448 nonum_.push_back(!numberedType());
449 label_.push_back(string());
450 MathGridInset::appendRow();
454 void MathHullInset::delRow(row_type row)
456 MathGridInset::delRow(row);
457 nonum_.erase(nonum_.begin() + row);
458 label_.erase(label_.begin() + row);
462 void MathHullInset::addCol(col_type col)
466 mutate(LM_OT_EQNARRAY);
475 mutate(LM_OT_ALIGNAT);
481 case LM_OT_XXALIGNAT:
482 MathGridInset::addCol(col);
483 MathGridInset::addCol(col + 1);
492 void MathHullInset::delCol(col_type col)
497 case LM_OT_XXALIGNAT:
498 MathGridInset::delCol(col + 1);
499 MathGridInset::delCol(col);
507 string MathHullInset::nicelabel(row_type row) const
511 if (label_[row].empty())
512 return string("(#)");
513 return "(" + label_[row] + ")";
517 void MathHullInset::mutate(string const & newtype)
519 if (newtype == "dump") {
523 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
524 mutate(typecode(newtype));
528 void MathHullInset::glueall()
531 for (idx_type i = 0; i < nargs(); ++i)
532 ar.push_back(cell(i));
533 *this = MathHullInset(LM_OT_SIMPLE);
538 MathInsetTypes MathHullInset::getType() const
544 void MathHullInset::setType(MathInsetTypes t)
552 void MathHullInset::mutate(MathInsetTypes newtype)
554 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
556 if (newtype == getType())
561 setType(LM_OT_EQUATION);
569 setType(LM_OT_SIMPLE);
575 case LM_OT_XXALIGNAT: {
577 MathGridInset::addCol(1);
580 pos_type pos = firstRelOp(cell(0));
582 cell(0).erase(pos, cell(0).size());
583 cell(1).erase(0, pos);
584 setType(LM_OT_ALIGN);
591 MathGridInset::addCol(1);
592 MathGridInset::addCol(1);
594 // split it "nicely" on the firest relop
595 pos_type pos = firstRelOp(cell(0));
596 cell(1) = MathArray(cell(0), pos, cell(0).size());
597 cell(0).erase(pos, cell(0).size());
599 if (cell(1).size()) {
600 cell(2) = MathArray(cell(1), 1, cell(1).size());
601 cell(1).erase(1, cell(1).size());
604 setType(LM_OT_EQNARRAY);
613 case LM_OT_EQUATION: {
614 // set correct (no)numbering
615 bool allnonum = true;
616 for (row_type row = 0; row < nrows(); ++row) {
621 // set first non-empty label
623 for (row_type row = 0; row < nrows(); ++row) {
624 if (!label_[row].empty()) {
632 nonum_[0] = allnonum;
641 case LM_OT_XXALIGNAT:
643 for (row_type row = 0; row < nrows(); ++row) {
644 idx_type c = 3 * row + 1;
645 cell(c).push_back(cell(c + 1));
647 MathGridInset::delCol(2);
648 setType(LM_OT_ALIGN);
660 MathGridInset::addCol(1);
661 setType(LM_OT_EQNARRAY);
667 case LM_OT_XXALIGNAT:
672 lyxerr << "mutation from '" << getType()
673 << "' to '" << newtype << "' not implemented"
682 setType(LM_OT_GATHER);
685 lyxerr << "mutation from '" << getType()
686 << "' to '" << newtype << "' not implemented"
694 setType(LM_OT_MULTLINE);
697 lyxerr << "mutation from '" << getType()
698 << "' to '" << newtype << "' not implemented"
704 lyxerr << "mutation from '" << getType()
705 << "' to '" << newtype << "' not implemented"
712 void MathHullInset::write(WriteStream & os) const
716 bool n = numberedType();
718 for (row_type row = 0; row < nrows(); ++row) {
719 for (col_type col = 0; col < ncols(); ++col)
720 os << cell(index(row, col)) << eocString(col);
722 if (!label_[row].empty())
723 os << "\\label{" << label_[row] << "}";
727 os << eolString(row);
734 void MathHullInset::normalize(NormalStream & os) const
736 os << "[formula " << normalName(getType()) << " ";
737 MathGridInset::normalize(os);
742 void MathHullInset::mathmlize(MathMLStream & os) const
744 MathGridInset::mathmlize(os);
748 void MathHullInset::infoize(std::ostream & os) const
750 os << normalName(getType());
753 void MathHullInset::check() const
755 lyx::Assert(nonum_.size() == nrows());
756 lyx::Assert(label_.size() == nrows());