3 * Purpose: Implementation of insets for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
8 * Dependencies: Xlib, XForms
10 * Copyright: (c) 1996, Alejandro Aguilar Sierra
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
21 #pragma implementation "math_iter.h"
24 #include "math_iter.h"
25 #include "math_inset.h"
26 #include "symbol_def.h"
27 #include "support/lstrings.h"
30 const int SizeInset = sizeof(char*) + 2;
31 const int SizeFont = 2;
33 extern int mathed_char_width(short type, int style, byte c);
34 extern int mathed_string_width(short type, int style, byte const* s, int ls);
35 extern int mathed_char_height(short, int, byte, int&, int&);
37 // the builtin memcpy() is broken in egcs and gcc 2.95.x on alpha
38 // stations. We provide a hand-made version instead.
39 inline void my_memcpy( void * ps_in, const void * pt_in, size_t n )
41 char * ps = static_cast<char *>(ps_in);
42 char const * pt = static_cast<char const *>(pt_in);
44 for(size_t i = n; i--;)
47 while (n--) *ps++ = *pt++;
51 void MathedIter::Reset()
53 if (array->last > 0 && MathIsFont(array->bf[0])) {
64 byte MathedIter::GetChar()
67 fcode = array->bf[pos];
70 return array->bf[pos];
74 byte* MathedIter::GetString(int& len)
77 fcode = array->bf[++pos];
80 byte *s = &array->bf[pos];
82 while (array->bf[pos]>= ' ' && pos<array->last) pos++;
88 MathedInset* MathedIter::GetInset()
92 my_memcpy(&p, &array->bf[pos+1], sizeof(p));
95 lyxerr << "Math Error: This is not an inset["
96 << array->bf[pos] << "]" << endl;
101 // An active math inset MUST be derived from MathParInset because it
102 // must have at least one paragraph to edit
103 MathParInset * MathedIter::GetActiveInset()
106 return static_cast<MathParInset*>(GetInset());
109 lyxerr << "Math Error: This is not an active inset" << endl;
113 bool MathedIter::Next()
115 if (!OK()) return false;
117 if (array->bf[pos]<' ') {
127 pos += sizeof(char*) + 2;
132 fcode = array->bf[pos++];
139 bool MathedIter::goNextCode(MathedTextCodes code)
142 if (array->bf[pos] == code)
150 void MathedIter::goPosAbs(int p)
153 while (pos<p && Next());
157 void MathedIter::goPosRel(int dp)
161 // is posx a valid position?
164 while (pos<posx && Next());
168 void MathedIter::Insert(byte c, MathedTextCodes t)
172 if (t == LM_TC_TAB && col>= ncols-1)
175 // Never more than one space // array->bf[pos-1] gives error from purify:
176 // Reading 1 byte from 0x47b857 in the heap.
177 // Address 0x47b857 is 1 byte before start of malloc'd block at 0x47b858 of 16 bytes.
178 if (c == ' ' && (array->bf[pos] == ' ' || array->bf[pos-1] == ' '))
181 if (IsFont() && array->bf[pos] == t) {
185 if (t!= fcode && pos>0 && MathIsFont(array->bf[pos-1])) {
188 for (k= pos-1; k>= 0 && array->bf[k]>= ' '; k--);
189 fcode = (k >= 0 && MathIsFont(array->bf[k])) ? array->bf[k]: -1;
191 short f = (array->bf[pos]<' ') ? 0: fcode;
192 int shift = (t == fcode) ? 1: ((f) ? 3: 2);
194 if (t == LM_TC_TAB || t == LM_TC_CR) {
204 if (pos < array->last)
205 array->Move(pos, shift);
207 if (array->last+shift>= array->maxsize) {
208 array->Resize(array->last+shift);
210 array->last += shift;
211 array->bf[array->last] = '\0';
215 array->bf[pos+shift-1] = fcode;
217 array->bf[pos++] = t;
223 array->bf[pos++] = c;
227 // Prepare to insert a non-char object
228 void MathedIter::split(int shift)
230 if (pos < array->last) {
232 if (array->bf[pos]>= ' ') {
233 if (pos> 0 && MathIsFont(array->bf[pos-1]))
240 array->Move(pos, shift);
241 if (fg) array->bf[pos+shift-1] = fcode;
243 if (array->last+shift>= array->maxsize) {
244 array->Resize(array->last+shift);
246 array->last += shift;
248 array->bf[array->last] = '\0';
252 // I assume that both pos and pos2 are legal positions
253 void MathedIter::join(int pos2)
255 if (!OK() || pos2<= pos)
259 if (pos>0 && array->bf[pos]>= ' ' && MathIsFont(array->bf[pos-1]))
262 if (MathIsFont(array->bf[pos2-1]))
265 if (array->bf[pos2]>= ' ') {
266 for (int p= pos2; p>0; p--)
267 if (MathIsFont(array->bf[p])) {
271 array->bf[pos++] = f;
274 array->Move(pos2, pos-pos2);
277 void MathedIter::Insert(MathedInset* p, int type)
279 int shift = SizeInset;
280 if (!MathIsInset(type))
283 array->bf[pos] = type;
284 my_memcpy(&array->bf[pos+1], &p, sizeof(p));
286 array->bf[pos-1] = type;
287 array->bf[array->last] = '\0';
292 bool MathedIter::Delete()
300 if (MathIsFont(array->bf[pos-1]) && array->bf[pos+1]<' ') {
304 for (i= pos-1; i>0 && !MathIsFont(array->bf[i]); i--);
305 if (i>0 && MathIsFont(array->bf[i]))
306 fcode = array->bf[i];
310 if (MathIsInset(array->bf[pos]))
311 shift = sizeof(char*) + 2;
313 if (c == LM_TC_TAB || c == LM_TC_CR) {
315 // lyxerr <<"Es un tab.";
318 lyxerr << "Math Warning: expected inset." << endl;
323 array->Move(pos+shift, -shift);
324 if (pos>= array->last)
325 pos = (array->last>0) ? array->last: 0;
332 LyxArrayBase *MathedIter::Copy(int pos1, int pos2)
335 // lyxerr << "Math error: Attempting to copy a void array." << endl;
341 LyxArrayBase *t= array, *a;
343 if (pos1>0 || pos2<= array->last) {
345 if (pos1>0 && array->bf[pos1]>' ') {
346 for (int p= pos1; p>= 0; p--)
347 if (MathIsFont(array->bf[p])) {
356 if (pos2>0 && array->bf[pos2]>= ' ' && MathIsFont(array->bf[pos2-1]))
359 int dx = pos2 - pos1;
360 a = new LyxArrayBase(dx+LyxArrayBase::ARRAY_MIN_SIZE);
361 // lyxerr << "VA " << pos2 << " " << pos2 << " " << dx << endl;
362 my_memcpy(&a->bf[(fc) ? 1: 0], &array->bf[pos1], dx);
370 a = new LyxArrayBase(*array);
374 MathedInset* inset = GetInset();
375 inset = inset->Clone();
376 my_memcpy(&array->bf[pos+1], &inset, sizeof(inset));
387 void MathedIter::Clear()
390 lyxerr << "Math error: Attempting to clean a void array." << endl;
396 MathedInset* inset = GetInset();
397 if (inset->GetType()!= LM_OT_MACRO_ARG)
406 // Check consistency of tabs and crs
407 void MathedIter::checkTabs()
411 // MathedIter:Reset();
413 if ((IsTab() && col>= ncols-1) || (IsCR() && !(MthIF_CR&flags))) {
417 if (IsCR() && col<ncols-2) {
418 Insert(' ', LM_TC_TAB);
423 Insert(' ', LM_TC_TAB);
429 // Try to adjust tabs in the expected place, as used in eqnarrays
431 // - If there are a relation operator, put tabs around it
432 // - If tehre are not a relation operator, put everything in the
434 void MathedIter::adjustTabs()
440 void MathedXIter::Clean(int pos2)
443 lyxerr << "Math error: Attempting to clean a void array." << endl;
451 while (pos<pos2 && OK()) { Next();
458 while (OK() && pos<pos2) {
460 MathedInset* inset = GetInset();
462 if (inset->GetType()!= LM_OT_MACRO_ARG)
468 MathedRowSt *r = crow->next;
470 crow->next = r->next;
479 if (pos2<= array->Last()) {
487 void MathedXIter::Merge(LyxArrayBase *a0)
490 lyxerr[Debug::MATHED]
491 << "Math error: Attempting to merge a void array." << endl;
495 // All insets must be clonned
497 LyxArrayBase *a = it.Copy();
499 // make rom for the data
501 array->MergeF(a, pos, a->Last());
503 int pos1= pos, pos2 = pos + a->Last(); // pos3= 0;
508 while (pos<pos2 && OK()) {
510 if (p && p->Permit(LMPF_ALLOW_CR)) {
511 MathedRowSt *r = new MathedRowSt(ncols+1);
513 r->next = crow->next;
538 MathedXIter::MathedXIter(MathParInset* pp): p(pp)
552 void MathedXIter::SetData(MathParInset *pp)
556 array = p->GetData();
557 ncols = p->GetColumns();
558 crow = p->getRowSt();
559 if (p->Permit(LMPF_ALLOW_CR))
561 if (p->Permit(LMPF_ALLOW_TAB))
566 y = crow->getBaseline();
569 array = new LyxArrayBase; // this leaks
572 size = p->GetStyle();
576 byte* MathedXIter::GetString(int& ls)
579 byte const *sxs = MathedIter::GetString(ls);
581 strncpy(reinterpret_cast<char*>(s), reinterpret_cast<const char*>(sxs), ls);
582 x += mathed_string_width(fcode, size, s, ls);
589 bool MathedXIter::Next()
591 // lyxerr << "Ne[" << pos << "]";
592 if (!OK()) return false;
596 MathedInset* px = GetInset();
598 if (px->GetType() == LM_OT_SCRIPT) {
602 sx = (px->GetLimits()) ? w: 0;
606 // lyxerr << "WD[" << fcode << " " << size << " " << c << endl;
607 w = mathed_char_width(fcode, size, c);
609 if (c == LM_TC_TAB && p) {
610 // w = p->GetTab(col+1);
611 w = (crow) ? crow->getTab(col+1): 0;
612 //lyxerr << "WW[" << w << "]";
614 if (c == LM_TC_CR && p) {
616 if (crow && crow->next) {
618 y = crow->getBaseline();
621 // lyxerr << "WW[" << col " " << row << "|" << w << "]";
623 lyxerr << "No hubo w[" << c << "]!";
625 if (MathedIter::Next()) {
626 // lyxerr <<"LNX " << pos << endl;
627 // if (sw>0 && GetChar()!= LM_TC_UP && GetChar()!= LM_TC_DOWN) {
628 // w = (sx>sw) ? 0: sw-sx;
629 if ((sw>0 || sx>0) && GetChar()!= LM_TC_UP && GetChar()!= LM_TC_DOWN) {
631 w = (sx>sw) ? 0: sw-sx;
641 void MathedXIter::GoBegin()
647 crow = p->getRowSt();
650 y = crow->getBaseline();
655 void MathedXIter::GoLast()
661 void MathedXIter::Adjust()
665 while (posx>pos && OK()) Next();
669 bool MathedXIter::Prev()
671 if (pos == 0 || (pos == 1 && GetChar()>= ' '))
674 int pos2 = pos; // pos1
686 bool MathedXIter::goNextColumn()
688 int rowp = row, colp= col;
689 while (Next() && col == colp);
691 return (col!= colp+1 || rowp!= row);
695 bool MathedXIter::Up()
697 if (row == 0) return false;
698 int xp = x, rowp = row, colp= col;
700 while (row<rowp-1) Next();
701 while (x<xp && OK() && !IsCR()) {
705 if (col>colp) // || (stck.col == colp && stck.x<= xp && x>xp))
712 bool MathedXIter::Down()
714 int xp = x, colp= col; // , rowp = row
716 bool res = (IsCR()) ? true: goNextCode(LM_TC_CR);
720 while (x<xp && OK()) {
724 if (col>colp || (stck.col == colp && stck.x<= xp && x>xp))
732 void MathedXIter::addRow()
735 lyxerr[Debug::MATHED] << "MathErr: Attempt to insert new"
736 " line in a subparagraph. " << this << endl;
740 // Create new item for the structure
741 MathedRowSt *r = new MathedRowSt(ncols+1);
743 r->next = crow->next;
749 // Fill missed tabs in current row
751 Insert('T', LM_TC_TAB);
753 Insert('K', LM_TC_CR);
757 goNextCode(LM_TC_CR);
759 // Fill missed tabs in new row
761 Insert('T', LM_TC_TAB);
766 void MathedXIter::delRow()
769 lyxerr[Debug::MATHED] << "MathErr: Attempt to delete a line in a subparagraph." << endl;
772 bool line_empty = true;
778 } else if (!IsTab()) {
787 MathedRowSt *r = crow->next;
789 crow->next = r->next;
800 void MathedXIter::ipush()
808 void MathedXIter::ipop()
814 crow = p->getRowSt();
816 for (int i= 0; i<row; i++)
822 void MathedXIter::fitCoord(int /*xx*/, int yy)
829 // first fit vertically
830 while (crow && OK()) {
831 if (yy>= yo+y-crow->asc && yy<= yo+y+crow->desc)
833 goNextCode(LM_TC_CR);
837 // while (x<xx && Next());
840 void MathedXIter::setTab(int tx, int tab)
842 if (crow && tab<= ncols) {
846 lyxerr << "MathErr: No tabs allowed here" << endl;
850 void MathedXIter::subMetrics(int a, int d)
853 // lyxerr << "MathErr: Attempt to submetric a subparagraph." << endl;
861 // This function is not recursive, as MathPar::Metrics is
862 void MathedXIter::IMetrics(int pos2, int& width, int& ascent, int& descent)
864 byte cx, cxp= 0;// *s;
869 descent = ascent = width = 0;
871 if (array->empty()) return;
872 // if (pos2 > array->last) return;
877 mathed_char_height(FCode(), size, cx, asc, des);
878 if (asc > ascent) ascent = asc;
879 if (des > descent) descent = des;
882 if (MathIsInset(cx)) {
883 MathedInset *pp = GetInset();
884 if (cx == LM_TC_UP) {
888 static_cast<MathParInset*>(pp)->GetXY(xx, asc);
891 asc += ((limits) ? pp->Height()+4: pp->Ascent());
893 if (cx == LM_TC_DOWN) {
897 static_cast<MathParInset*>(pp)->GetXY(xx, des);
898 if (des-pp->Height()<yy && !asc)
899 asc = yy - (des-pp->Height());
902 des += (limit ? pp->Height()+4: pp->Height()-pp->Ascent()/2);
907 if (asc > ascent) ascent = asc;
908 if (des > descent) descent = des;
909 if (cx!= LM_TC_UP && cx!= LM_TC_DOWN)
910 limit = pp->GetLimits();
912 if (cx == LM_TC_TAB) {
916 lyxerr[Debug::MATHED]
917 << "Mathed Sel-Error: Unrecognized code["
918 << cx << ']' << endl;
921 if (pos < pos2) Next();
928 bool MathedXIter::setNumbered(bool numb)
931 crow->setNumbered(numb);
939 bool MathedXIter::setLabel(char* label)
942 crow->setLabel(label);
950 MathedRowSt *MathedXIter::adjustVerticalSt()
954 // lyxerr << " CRW" << ncols << " ";
955 crow = new MathedRowSt(ncols+1); // this leaks
957 // lyxerr<< " CRW[" << crow << "] ";
958 MathedRowSt *mrow = crow;
961 if (col>= ncols) ncols = col+1;
962 MathedRowSt *r = new MathedRowSt(ncols+1); // this leaks
963 // r->next = crow->next;
966 // lyxerr << " CX[" << crow << "]";