3 #include "math_xiter.h"
4 #include "math_parinset.h"
5 #include "math_rowst.h"
7 #include "mathed/support.h"
12 string MathedXIter::error_label = "$mathed-error$";
14 MathedXIter::MathedXIter()
15 : MathedIter(), size_(0), x_(0), y_(0), p_(0), sx_(0), sw_(0),
18 // should limits_ be initialized?
22 MathedXIter::MathedXIter(MathParInset * pp)
23 : x_(0), y_(0), p_(pp), sx_(0), sw_(0), limits_(false)
35 MathParInset * MathedXIter::getPar() const
41 void MathedXIter::GetPos(int & xx, int & yy) const
54 int MathedXIter::GetX() const
63 int MathedXIter::GetY() const
72 void MathedXIter::GetIncPos(int & xx, int & yy) const
79 void MathedXIter::getAD(int & a, int & d) const
94 void MathedXIter::Clean(int pos2)
97 lyxerr << "Math error: Attempting to clean a void array." << endl;
105 while (pos < pos2 && OK()) {
113 while (OK() && pos < pos2) {
115 MathedInset * inset = GetInset();
117 if (inset->GetType()!= LM_OT_MACRO_ARG)
123 MathedRowSt * r = crow_->getNext();
125 crow_->setNext(r->getNext());
134 if (pos2 <= array->last()) {
142 void MathedXIter::Merge(MathedArray * a0)
145 lyxerr[Debug::MATHED]
146 << "Math error: Attempting to merge a void array." << endl;
150 // All insets must be clonned
151 MathedArray * a = new MathedArray(*a0);
154 array->insert(array->begin() + pos,
155 a->begin(), a->end());
157 // make room for the data
159 array->mergeF(a, pos, a->last());
162 int pos2 = pos + a->last();
167 while (pos < pos2 && OK()) {
169 if (p_ && p_->Permit(LMPF_ALLOW_CR)) {
170 MathedRowSt * r = new MathedRowSt(ncols + 1);
172 r->setNext(crow_->getNext());
194 void MathedXIter::SetData(MathParInset * pp)
198 array = &p_->GetData();
199 ncols = p_->GetColumns();
200 crow_ = p_->getRowSt();
201 if (p_->Permit(LMPF_ALLOW_CR))
203 if (p_->Permit(LMPF_ALLOW_TAB))
207 x_ = crow_->getTab(0);
208 y_ = crow_->getBaseline();
211 array = new MathedArray; // this leaks
214 size_ = p_->GetStyle();
219 string const MathedXIter::GetString() const
221 string const s = MathedIter::GetString();
222 x_ += mathed_string_width(fcode(), size_, s);
227 bool MathedXIter::Next()
229 // lyxerr << "Ne[" << pos << "]";
230 if (!OK()) return false;
234 MathedInset * px = GetInset();
236 if (px->GetType() == LM_OT_SCRIPT) {
237 if (w > sw_) sw_ = w;
240 sx_ = (px->GetLimits()) ? w : 0;
244 // lyxerr << "WD[" << fcode << " " << size << " " << c << endl;
245 w = mathed_char_width(fcode(), size_, c);
247 if (c == LM_TC_TAB && p_) {
248 // w = p->GetTab(col + 1);
249 w = (crow_) ? crow_->getTab(col + 1) : 0;
250 //lyxerr << "WW[" << w << "]";
252 if (c == LM_TC_CR && p_) {
254 if (crow_ && crow_->getNext()) {
255 crow_ = crow_->getNext();
256 y_ = crow_->getBaseline();
257 w = crow_->getTab(0);
259 // lyxerr << "WW[" << col " " << row << "|" << w << "]";
261 lyxerr << "No hubo w[" << c << "]!";
263 if (MathedIter::Next()) {
264 // lyxerr <<"LNX " << pos << endl;
265 // if (sw>0 && GetChar()!= LM_TC_UP && GetChar()!= LM_TC_DOWN) {
266 // w = (sx>sw) ? 0: sw-sx;
267 if ((sw_ > 0 || sx_ > 0)
268 && GetChar() != LM_TC_UP && GetChar() != LM_TC_DOWN) {
270 w = (sx_ > sw_) ? 0 : sw_ - sx_;
280 void MathedXIter::GoBegin()
286 crow_ = p_->getRowSt();
288 x_ = crow_->getTab(0);
289 y_ = crow_->getBaseline();
295 void MathedXIter::GoLast()
301 void MathedXIter::Adjust()
305 while (posx > pos && OK()) Next();
309 bool MathedXIter::Prev()
311 if (pos == 0 || (pos == 1 && GetChar() >= ' '))
314 int pos2 = pos; // pos1
326 void MathedXIter::goNextColumn()
330 while (Next() && col == colp)
333 //return (col != colp + 1 || rowp != row);
337 bool MathedXIter::Up()
339 if (row == 0) return false;
344 while (row < rowp - 1) Next();
345 while (x_ < xp && OK() && !IsCR()) {
349 if (col > colp) // || (stck.col == colp && stck.x<= xp && x>xp))
356 bool MathedXIter::Down()
362 bool res = (IsCR()) ? true : goNextCode(LM_TC_CR);
366 while (x_ < xp && OK()) {
370 if (col > colp || (stck.col == colp && stck.x <= xp && x_ > xp))
378 void MathedXIter::addRow()
381 lyxerr[Debug::MATHED] << "MathErr: Attempt to insert new"
382 " line in a subparagraph. " << this << endl;
386 // Create new item for the structure
387 MathedRowSt * r = new MathedRowSt(ncols + 1);
389 r->setNext(crow_->getNext());
395 // Fill missed tabs in current row
396 while (col < ncols - 1)
397 insert('T', LM_TC_TAB);
399 insert('K', LM_TC_CR);
403 goNextCode(LM_TC_CR);
405 // Fill missed tabs in new row
406 while (col < ncols - 1)
407 insert('T', LM_TC_TAB);
412 void MathedXIter::delRow()
415 lyxerr[Debug::MATHED] << "MathErr: Attempt to delete a line in a subparagraph." << endl;
418 bool line_empty = true;
424 } else if (!IsTab()) {
428 int const p1 = getPos();
433 MathedRowSt * r = crow_->getNext();
435 crow_->setNext(r->getNext());
447 void MathedXIter::ipush()
455 void MathedXIter::ipop()
461 crow_ = p_->getRowSt();
463 for (int i = 0; i < row; ++i)
464 crow_ = crow_->getNext();
469 void MathedXIter::fitCoord(int /*xx*/, int yy)
477 // first fit vertically
478 while (crow_ && OK()) {
479 if (yy >= yo + y_ - crow_->ascent()
480 && yy <= yo + y_ + crow_->descent())
482 goNextCode(LM_TC_CR);
486 // while (x<xx && Next());
490 void MathedXIter::setTab(int tx, int tab)
492 if (crow_ && tab <= ncols) {
493 crow_->setTab(tab, tx);
495 lyxerr << "MathErr: No tabs allowed here" << endl;
499 void MathedXIter::subMetrics(int a, int d)
502 lyxerr[Debug::MATHED]
503 << "MathErr: Attempt to submetric a subparagraph." << endl;
511 // This function is not recursive, as MathPar::Metrics is
512 void MathedXIter::IMetrics(int pos2, int & width, int & ascent, int & descent)
520 descent = ascent = width = 0;
522 if (array->empty()) return;
523 // if (pos2 > array->last) return;
528 mathed_char_height(fcode(), size_, cx, asc, des);
529 if (asc > ascent) ascent = asc;
530 if (des > descent) descent = des;
533 if (MathIsInset(cx)) {
534 MathedInset * pp = GetInset();
535 if (cx == LM_TC_UP) {
540 static_cast<MathParInset*>(pp)->GetXY(xx, asc);
543 asc += ((limits_) ? pp->Height() + 4 : pp->Ascent());
544 } else if (cx == LM_TC_DOWN) {
549 static_cast<MathParInset*>(pp)->GetXY(xx, des);
550 if (des - pp->Height() < yy && !asc)
551 asc = yy - (des - pp->Height());
554 des += (limit ? pp->Height()+4: pp->Height()-pp->Ascent()/2);
559 if (asc > ascent) ascent = asc;
560 if (des > descent) descent = des;
561 if (cx != LM_TC_UP && cx != LM_TC_DOWN)
562 limit = pp->GetLimits();
563 } else if (cx == LM_TC_TAB) {
566 lyxerr[Debug::MATHED]
567 << "Mathed Sel-Error: Unrecognized code["
568 << cx << ']' << endl;
571 if (pos < pos2) Next();
577 bool MathedXIter::setNumbered(bool numb)
580 crow_->setNumbered(numb);
588 bool MathedXIter::setLabel(string const & label)
591 crow_->setLabel(label);
599 MathedRowSt * MathedXIter::adjustVerticalSt()
603 // lyxerr << " CRW" << ncols << " ";
604 crow_ = new MathedRowSt(ncols + 1); // this leaks
606 // lyxerr<< " CRW[" << crow_ << "] ";
607 MathedRowSt * mrow = crow_;
610 if (col >= ncols) ncols = col + 1;
611 MathedRowSt * r = new MathedRowSt(ncols + 1); // this leaks
612 // r->next = crow_->next;
615 // lyxerr << " CX[" << crow_ << "]";
623 string const & MathedXIter::getLabel() const
625 return crow_ ? crow_->getLabel() : error_label;