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
269 objtype_ == LM_OT_ALIGN ||
270 objtype_ == LM_OT_MULTLINE ||
271 objtype_ == LM_OT_GATHER ||
272 objtype_ == LM_OT_ALIGNAT ||
273 objtype_ == LM_OT_XALIGNAT ||
274 objtype_ == LM_OT_XXALIGNAT;
278 bool MathHullInset::display() const
280 return getType() != LM_OT_SIMPLE;
284 vector<string> const MathHullInset::getLabelList() const
287 for (row_type row = 0; row < nrows(); ++row)
288 if (!label_[row].empty() && nonum_[row] != 1)
289 res.push_back(label_[row]);
294 bool MathHullInset::numberedType() const
296 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
298 for (row_type row = 0; row < nrows(); ++row)
305 void MathHullInset::validate(LaTeXFeatures & features) const
308 features.require("amsmath");
311 // Validation is necessary only if not using AMS math.
312 // To be safe, we will always run mathedvalidate.
313 //if (features.amsstyle)
316 features.require("boldsymbol");
317 //features.binom = true;
319 MathNestInset::validate(features);
323 void MathHullInset::header_write(WriteStream & os) const
325 bool n = numberedType();
336 os << "\\begin{equation" << star(n) << "}\n";
342 os << "\\begin{eqnarray" << star(n) << "}\n";
346 os << "\\begin{align" << star(n) << "}\n";
350 os << "\\begin{alignat" << star(n) << "}"
351 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
355 os << "\\begin{xalignat" << star(n) << "}"
356 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
359 case LM_OT_XXALIGNAT:
360 os << "\\begin{xxalignat}"
361 << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
365 os << "\\begin{multline}\n";
369 os << "\\begin{gather}\n";
376 os << "\\begin{unknown" << star(n) << "}";
381 void MathHullInset::footer_write(WriteStream & os) const
383 bool n = numberedType();
392 os << "\\end{equation" << star(n) << "}\n";
398 os << "\n\\end{eqnarray" << star(n) << "}\n";
402 os << "\n\\end{align" << star(n) << "}\n";
406 os << "\n\\end{alignat" << star(n) << "}\n";
410 os << "\n\\end{xalignat" << star(n) << "}\n";
413 case LM_OT_XXALIGNAT:
414 os << "\n\\end{xxalignat}\n";
418 os << "\n\\end{multline}\n";
422 os << "\n\\end{gather}\n";
430 os << "\\end{unknown" << star(n) << "}";
435 void MathHullInset::addRow(row_type row)
437 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
438 label_.insert(label_.begin() + row + 1, string());
439 MathGridInset::addRow(row);
443 void MathHullInset::appendRow()
445 nonum_.push_back(!numberedType());
446 label_.push_back(string());
447 MathGridInset::appendRow();
451 void MathHullInset::delRow(row_type row)
453 MathGridInset::delRow(row);
454 nonum_.erase(nonum_.begin() + row);
455 label_.erase(label_.begin() + row);
459 void MathHullInset::addCol(col_type col)
463 mutate(LM_OT_EQNARRAY);
472 mutate(LM_OT_ALIGNAT);
478 case LM_OT_XXALIGNAT:
479 MathGridInset::addCol(col);
480 MathGridInset::addCol(col + 1);
489 void MathHullInset::delCol(col_type col)
494 case LM_OT_XXALIGNAT:
495 MathGridInset::delCol(col + 1);
496 MathGridInset::delCol(col);
504 string MathHullInset::nicelabel(row_type row) const
508 if (label_[row].empty())
509 return string("(#)");
510 return "(" + label_[row] + ")";
514 void MathHullInset::mutate(string const & newtype)
516 if (newtype == "dump") {
520 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
521 mutate(typecode(newtype));
525 void MathHullInset::glueall()
528 for (idx_type i = 0; i < nargs(); ++i)
529 ar.push_back(cell(i));
530 *this = MathHullInset(LM_OT_SIMPLE);
535 MathInsetTypes MathHullInset::getType() const
541 void MathHullInset::setType(MathInsetTypes t)
549 void MathHullInset::mutate(MathInsetTypes newtype)
551 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
553 if (newtype == getType())
558 setType(LM_OT_EQUATION);
566 setType(LM_OT_SIMPLE);
572 case LM_OT_XXALIGNAT: {
574 MathGridInset::addCol(1);
577 pos_type pos = firstRelOp(cell(0));
579 cell(0).erase(pos, cell(0).size());
580 cell(1).erase(0, pos);
581 setType(LM_OT_ALIGN);
588 MathGridInset::addCol(1);
589 MathGridInset::addCol(1);
591 // split it "nicely" on the firest relop
592 pos_type pos = firstRelOp(cell(0));
593 cell(1) = MathArray(cell(0), pos, cell(0).size());
594 cell(0).erase(pos, cell(0).size());
596 if (cell(1).size()) {
597 cell(2) = MathArray(cell(1), 1, cell(1).size());
598 cell(1).erase(1, cell(1).size());
601 setType(LM_OT_EQNARRAY);
610 case LM_OT_EQUATION: {
611 // set correct (no)numbering
612 bool allnonum = true;
613 for (row_type row = 0; row < nrows(); ++row) {
618 // set first non-empty label
620 for (row_type row = 0; row < nrows(); ++row) {
621 if (!label_[row].empty()) {
629 nonum_[0] = allnonum;
638 case LM_OT_XXALIGNAT:
640 for (row_type row = 0; row < nrows(); ++row) {
641 idx_type c = 3 * row + 1;
642 cell(c).push_back(cell(c + 1));
644 MathGridInset::delCol(2);
645 setType(LM_OT_ALIGN);
657 MathGridInset::addCol(1);
658 setType(LM_OT_EQNARRAY);
664 case LM_OT_XXALIGNAT:
669 lyxerr << "mutation from '" << getType()
670 << "' to '" << newtype << "' not implemented"
679 setType(LM_OT_GATHER);
682 lyxerr << "mutation from '" << getType()
683 << "' to '" << newtype << "' not implemented"
691 setType(LM_OT_MULTLINE);
694 lyxerr << "mutation from '" << getType()
695 << "' to '" << newtype << "' not implemented"
701 lyxerr << "mutation from '" << getType()
702 << "' to '" << newtype << "' not implemented"
709 void MathHullInset::write(WriteStream & os) const
713 bool n = numberedType();
715 for (row_type row = 0; row < nrows(); ++row) {
716 for (col_type col = 0; col < ncols(); ++col)
717 os << cell(index(row, col)) << eocString(col);
719 if (!label_[row].empty())
720 os << "\\label{" << label_[row] << "}";
724 os << eolString(row);
731 void MathHullInset::normalize(NormalStream & os) const
733 os << "[formula " << normalName(getType()) << " ";
734 MathGridInset::normalize(os);
739 void MathHullInset::mathmlize(MathMLStream & os) const
741 MathGridInset::mathmlize(os);
745 void MathHullInset::infoize(std::ostream & os) const
747 os << normalName(getType());
750 void MathHullInset::check() const
752 lyx::Assert(nonum_.size() == nrows());
753 lyx::Assert(label_.size() == nrows());