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: 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"
32 const int SizeInset = sizeof(char*) + 2;
33 const int SizeFont = 2;
35 extern int mathed_char_width(short type, int style, byte c);
36 extern int mathed_string_width(short type, int style, byte const* s, int ls);
37 extern int mathed_char_height(short, int, byte, int&, int&);
39 // the builtin memcpy() is broken in egcs and gcc 2.95.x on alpha
40 // stations. We provide a hand-made version instead.
42 void my_memcpy( void * ps_in, const void * pt_in, size_t n )
44 char * ps = static_cast<char *>(ps_in);
45 char const * pt = static_cast<char const *>(pt_in);
47 for(size_t i = n; i--;)
50 while (n--) *ps++ = *pt++;
54 void MathedIter::Reset()
56 if (array->last > 0 && MathIsFont(array->bf[0])) {
67 byte MathedIter::GetChar()
70 fcode = array->bf[pos];
73 return array->bf[pos];
77 byte* MathedIter::GetString(int& len)
80 fcode = array->bf[++pos];
83 byte *s = &array->bf[pos];
85 while (array->bf[pos]>= ' ' && pos<array->last) pos++;
91 MathedInset* MathedIter::GetInset()
95 my_memcpy(&p, &array->bf[pos+1], sizeof(p));
98 lyxerr << "Math Error: This is not an inset["
99 << array->bf[pos] << "]" << endl;
104 // An active math inset MUST be derived from MathParInset because it
105 // must have at least one paragraph to edit
106 MathParInset * MathedIter::GetActiveInset()
109 return static_cast<MathParInset*>(GetInset());
112 lyxerr << "Math Error: This is not an active inset" << endl;
116 bool MathedIter::Next()
118 if (!OK()) return false;
120 if (array->bf[pos]<' ') {
130 pos += sizeof(char*) + 2;
135 fcode = array->bf[pos++];
142 bool MathedIter::goNextCode(MathedTextCodes code)
145 if (array->bf[pos] == code)
153 void MathedIter::goPosAbs(int p)
156 while (pos<p && Next());
160 void MathedIter::goPosRel(int dp)
164 // is posx a valid position?
167 while (pos<posx && Next());
171 void MathedIter::Insert(byte c, MathedTextCodes t)
175 if (t == LM_TC_TAB && col>= ncols-1)
178 // Never more than one space // array->bf[pos-1] gives error from purify:
179 // Reading 1 byte from 0x47b857 in the heap.
180 // Address 0x47b857 is 1 byte before start of malloc'd block at 0x47b858 of 16 bytes.
181 if (c == ' ' && (array->bf[pos] == ' ' || array->bf[pos-1] == ' '))
184 if (IsFont() && array->bf[pos] == t) {
188 if (t!= fcode && pos>0 && MathIsFont(array->bf[pos-1])) {
191 for (k= pos-1; k>= 0 && array->bf[k]>= ' '; k--);
192 fcode = (k >= 0 && MathIsFont(array->bf[k])) ? array->bf[k]: -1;
194 short f = (array->bf[pos]<' ') ? 0: fcode;
195 int shift = (t == fcode) ? 1: ((f) ? 3: 2);
197 if (t == LM_TC_TAB || t == LM_TC_CR) {
207 if (pos < array->last)
208 array->Move(pos, shift);
210 if (array->last+shift>= array->maxsize) {
211 array->Resize(array->last+shift);
213 array->last += shift;
214 array->bf[array->last] = '\0';
218 array->bf[pos+shift-1] = fcode;
220 array->bf[pos++] = t;
226 array->bf[pos++] = c;
230 // Prepare to insert a non-char object
231 void MathedIter::split(int shift)
233 if (pos < array->last) {
235 if (array->bf[pos]>= ' ') {
236 if (pos> 0 && MathIsFont(array->bf[pos-1]))
243 array->Move(pos, shift);
244 if (fg) array->bf[pos+shift-1] = fcode;
246 if (array->last+shift>= array->maxsize) {
247 array->Resize(array->last+shift);
249 array->last += shift;
251 array->bf[array->last] = '\0';
255 // I assume that both pos and pos2 are legal positions
256 void MathedIter::join(int pos2)
258 if (!OK() || pos2<= pos)
262 if (pos>0 && array->bf[pos]>= ' ' && MathIsFont(array->bf[pos-1]))
265 if (MathIsFont(array->bf[pos2-1]))
268 if (array->bf[pos2]>= ' ') {
269 for (int p= pos2; p>0; p--)
270 if (MathIsFont(array->bf[p])) {
274 array->bf[pos++] = f;
277 array->Move(pos2, pos-pos2);
280 void MathedIter::Insert(MathedInset* p, int type)
282 int shift = SizeInset;
283 if (!MathIsInset(type))
286 array->bf[pos] = type;
287 my_memcpy(&array->bf[pos+1], &p, sizeof(p));
289 array->bf[pos-1] = type;
290 array->bf[array->last] = '\0';
295 bool MathedIter::Delete()
303 if (MathIsFont(array->bf[pos-1]) && array->bf[pos+1]<' ') {
307 for (i= pos-1; i>0 && !MathIsFont(array->bf[i]); i--);
308 if (i>0 && MathIsFont(array->bf[i]))
309 fcode = array->bf[i];
313 if (MathIsInset(array->bf[pos]))
314 shift = sizeof(char*) + 2;
316 if (c == LM_TC_TAB || c == LM_TC_CR) {
318 // lyxerr <<"Es un tab.";
321 lyxerr << "Math Warning: expected inset." << endl;
326 array->Move(pos+shift, -shift);
327 if (pos>= array->last)
328 pos = (array->last>0) ? array->last: 0;
335 LyxArrayBase *MathedIter::Copy(int pos1, int pos2)
338 // lyxerr << "Math error: Attempting to copy a void array." << endl;
344 LyxArrayBase *t= array, *a;
346 if (pos1>0 || pos2<= array->last) {
348 if (pos1>0 && array->bf[pos1]>' ') {
349 for (int p= pos1; p>= 0; p--)
350 if (MathIsFont(array->bf[p])) {
359 if (pos2>0 && array->bf[pos2]>= ' ' && MathIsFont(array->bf[pos2-1]))
362 int dx = pos2 - pos1;
363 a = new LyxArrayBase(dx+LyxArrayBase::ARRAY_MIN_SIZE);
364 // lyxerr << "VA " << pos2 << " " << pos2 << " " << dx << endl;
365 my_memcpy(&a->bf[(fc) ? 1: 0], &array->bf[pos1], dx);
373 a = new LyxArrayBase(*array);
377 MathedInset* inset = GetInset();
378 inset = inset->Clone();
379 my_memcpy(&array->bf[pos+1], &inset, sizeof(inset));
390 void MathedIter::Clear()
393 lyxerr << "Math error: Attempting to clean a void array." << endl;
399 MathedInset* inset = GetInset();
400 if (inset->GetType()!= LM_OT_MACRO_ARG)
409 // Check consistency of tabs and crs
410 void MathedIter::checkTabs()
414 // MathedIter:Reset();
416 if ((IsTab() && col>= ncols-1) || (IsCR() && !(MthIF_CR&flags))) {
420 if (IsCR() && col<ncols-2) {
421 Insert(' ', LM_TC_TAB);
426 Insert(' ', LM_TC_TAB);
432 // Try to adjust tabs in the expected place, as used in eqnarrays
434 // - If there are a relation operator, put tabs around it
435 // - If tehre are not a relation operator, put everything in the
437 void MathedIter::adjustTabs()
443 void MathedXIter::Clean(int pos2)
446 lyxerr << "Math error: Attempting to clean a void array." << endl;
454 while (pos<pos2 && OK()) { Next();
461 while (OK() && pos<pos2) {
463 MathedInset* inset = GetInset();
465 if (inset->GetType()!= LM_OT_MACRO_ARG)
471 MathedRowSt *r = crow->next;
473 crow->next = r->next;
482 if (pos2<= array->Last()) {
490 void MathedXIter::Merge(LyxArrayBase *a0)
493 lyxerr[Debug::MATHED]
494 << "Math error: Attempting to merge a void array." << endl;
498 // All insets must be clonned
500 LyxArrayBase *a = it.Copy();
502 // make rom for the data
504 array->MergeF(a, pos, a->Last());
506 int pos1= pos, pos2 = pos + a->Last(); // pos3= 0;
511 while (pos<pos2 && OK()) {
513 if (p && p->Permit(LMPF_ALLOW_CR)) {
514 MathedRowSt *r = new MathedRowSt(ncols+1);
516 r->next = crow->next;
541 MathedXIter::MathedXIter(MathParInset* pp): p(pp)
555 void MathedXIter::SetData(MathParInset *pp)
559 array = p->GetData();
560 ncols = p->GetColumns();
561 crow = p->getRowSt();
562 if (p->Permit(LMPF_ALLOW_CR))
564 if (p->Permit(LMPF_ALLOW_TAB))
569 y = crow->getBaseline();
572 array = new LyxArrayBase; // this leaks
575 size = p->GetStyle();
579 byte* MathedXIter::GetString(int& ls)
582 byte const *sxs = MathedIter::GetString(ls);
584 strncpy(reinterpret_cast<char*>(s), reinterpret_cast<const char*>(sxs), ls);
585 x += mathed_string_width(fcode, size, s, ls);
592 bool MathedXIter::Next()
594 // lyxerr << "Ne[" << pos << "]";
595 if (!OK()) return false;
599 MathedInset * px = GetInset();
601 if (px->GetType() == LM_OT_SCRIPT) {
605 sx = (px->GetLimits()) ? w : 0;
609 // lyxerr << "WD[" << fcode << " " << size << " " << c << endl;
610 w = mathed_char_width(fcode, size, c);
612 if (c == LM_TC_TAB && p) {
613 // w = p->GetTab(col+1);
614 w = (crow) ? crow->getTab(col + 1) : 0;
615 //lyxerr << "WW[" << w << "]";
617 if (c == LM_TC_CR && p) {
619 if (crow && crow->next) {
621 y = crow->getBaseline();
624 // lyxerr << "WW[" << col " " << row << "|" << w << "]";
626 lyxerr << "No hubo w[" << c << "]!";
628 if (MathedIter::Next()) {
629 // lyxerr <<"LNX " << pos << endl;
630 // if (sw>0 && GetChar()!= LM_TC_UP && GetChar()!= LM_TC_DOWN) {
631 // w = (sx>sw) ? 0: sw-sx;
632 if ((sw > 0 || sx > 0) && GetChar() != LM_TC_UP && GetChar() != LM_TC_DOWN) {
634 w = (sx > sw) ? 0 : sw - sx;
644 void MathedXIter::GoBegin()
650 crow = p->getRowSt();
653 y = crow->getBaseline();
658 void MathedXIter::GoLast()
664 void MathedXIter::Adjust()
668 while (posx>pos && OK()) Next();
672 bool MathedXIter::Prev()
674 if (pos == 0 || (pos == 1 && GetChar()>= ' '))
677 int pos2 = pos; // pos1
689 bool MathedXIter::goNextColumn()
691 int rowp = row, colp= col;
692 while (Next() && col == colp);
694 return (col!= colp+1 || rowp!= row);
698 bool MathedXIter::Up()
700 if (row == 0) return false;
701 int xp = x, rowp = row, colp= col;
703 while (row<rowp-1) Next();
704 while (x<xp && OK() && !IsCR()) {
708 if (col>colp) // || (stck.col == colp && stck.x<= xp && x>xp))
715 bool MathedXIter::Down()
717 int xp = x, colp= col; // , rowp = row
719 bool res = (IsCR()) ? true: goNextCode(LM_TC_CR);
723 while (x<xp && OK()) {
727 if (col>colp || (stck.col == colp && stck.x<= xp && x>xp))
735 void MathedXIter::addRow()
738 lyxerr[Debug::MATHED] << "MathErr: Attempt to insert new"
739 " line in a subparagraph. " << this << endl;
743 // Create new item for the structure
744 MathedRowSt *r = new MathedRowSt(ncols+1);
746 r->next = crow->next;
752 // Fill missed tabs in current row
754 Insert('T', LM_TC_TAB);
756 Insert('K', LM_TC_CR);
760 goNextCode(LM_TC_CR);
762 // Fill missed tabs in new row
764 Insert('T', LM_TC_TAB);
769 void MathedXIter::delRow()
772 lyxerr[Debug::MATHED] << "MathErr: Attempt to delete a line in a subparagraph." << endl;
775 bool line_empty = true;
781 } else if (!IsTab()) {
790 MathedRowSt *r = crow->next;
792 crow->next = r->next;
803 void MathedXIter::ipush()
811 void MathedXIter::ipop()
817 crow = p->getRowSt();
819 for (int i= 0; i<row; i++)
825 void MathedXIter::fitCoord(int /*xx*/, int yy)
832 // first fit vertically
833 while (crow && OK()) {
834 if (yy>= yo+y-crow->asc && yy<= yo+y+crow->desc)
836 goNextCode(LM_TC_CR);
840 // while (x<xx && Next());
843 void MathedXIter::setTab(int tx, int tab)
845 if (crow && tab<= ncols) {
849 lyxerr << "MathErr: No tabs allowed here" << endl;
853 void MathedXIter::subMetrics(int a, int d)
856 // lyxerr << "MathErr: Attempt to submetric a subparagraph." << endl;
864 // This function is not recursive, as MathPar::Metrics is
865 void MathedXIter::IMetrics(int pos2, int& width, int& ascent, int& descent)
867 byte cx, cxp= 0;// *s;
872 descent = ascent = width = 0;
874 if (array->empty()) return;
875 // if (pos2 > array->last) return;
880 mathed_char_height(FCode(), size, cx, asc, des);
881 if (asc > ascent) ascent = asc;
882 if (des > descent) descent = des;
885 if (MathIsInset(cx)) {
886 MathedInset *pp = GetInset();
887 if (cx == LM_TC_UP) {
891 static_cast<MathParInset*>(pp)->GetXY(xx, asc);
894 asc += ((limits) ? pp->Height()+4: pp->Ascent());
896 if (cx == LM_TC_DOWN) {
900 static_cast<MathParInset*>(pp)->GetXY(xx, des);
901 if (des-pp->Height()<yy && !asc)
902 asc = yy - (des-pp->Height());
905 des += (limit ? pp->Height()+4: pp->Height()-pp->Ascent()/2);
910 if (asc > ascent) ascent = asc;
911 if (des > descent) descent = des;
912 if (cx!= LM_TC_UP && cx!= LM_TC_DOWN)
913 limit = pp->GetLimits();
915 if (cx == LM_TC_TAB) {
919 lyxerr[Debug::MATHED]
920 << "Mathed Sel-Error: Unrecognized code["
921 << cx << ']' << endl;
924 if (pos < pos2) Next();
931 bool MathedXIter::setNumbered(bool numb)
934 crow->setNumbered(numb);
942 bool MathedXIter::setLabel(char* label)
945 crow->setLabel(label);
953 MathedRowSt *MathedXIter::adjustVerticalSt()
957 // lyxerr << " CRW" << ncols << " ";
958 crow = new MathedRowSt(ncols+1); // this leaks
960 // lyxerr<< " CRW[" << crow << "] ";
961 MathedRowSt *mrow = crow;
964 if (col>= ncols) ncols = col+1;
965 MathedRowSt *r = new MathedRowSt(ncols+1); // this leaks
966 // r->next = crow->next;
969 // lyxerr << " CX[" << crow << "]";