]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parinset.C
move inlines out of line
[lyx.git] / src / mathed / math_parinset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_parinset.h"
8 #include "math_iter.h"
9 #include "array.h"
10 #include "math_xiter.h"
11 #include "LColor.h"
12 #include "mathed/support.h"
13 #include "Painter.h"
14 #include "math_parser.h"
15 #include "math_rowst.h"
16 #include "math_parinset.h"
17 #include "debug.h"
18
19 using std::endl;
20
21 extern int number_of_newlines;
22
23
24 MathedRowSt * MathParInset::getRowSt() const
25 {
26         return 0;
27 }
28
29
30 MathParInset::MathParInset(short st, string const & nm, short ot)
31         : MathedInset(nm, ot, st)
32 {
33         ascent = 8;
34         width = 4;
35         descent = 0;
36         flag = 1;
37         if (objtype == LM_OT_SCRIPT)
38                 flag |= LMPF_SCRIPT;
39 }
40
41
42 MathedInset * MathParInset::Clone()
43 {
44         return new MathParInset(*this);
45 }
46
47
48 void MathParInset::setData(MathedArray const & a)
49 {
50         array = a;
51         
52         // A standard paragraph shouldn't have any tabs nor CRs.
53         MathedIter it(&array);
54         while (it.OK()) {
55                 char c = it.GetChar();
56                 if (c == LM_TC_TAB || c == LM_TC_CR) 
57                         it.Delete();
58                 else
59                         it.Next();
60         }
61 }
62
63
64 void 
65 MathParInset::draw(Painter & pain, int x, int y)
66 {
67         byte cxp = 0;
68         int xp = 0;
69         int asc = df_asc;
70         int des = 0;
71         bool limits = false;
72         
73         xo_ = x;
74         yo_ = y;
75         MathedXIter data(this);
76         if (array.empty()) {
77                 //MathedXIter data(this);
78                 data.GetPos(x, y);
79                 pain.rectangle(x, y - df_asc, df_width, df_asc,
80                                LColor::mathline);
81                 return;
82         }
83         //MathedXIter data(this);
84         data.GoBegin();
85         while (data.OK()) {
86                 data.GetPos(x, y);
87                 byte const cx = data.GetChar();
88                 if (cx >= ' ') {
89                         string const s = data.GetString();
90                         drawStr(pain, data.fcode(), size(), x, y, s);
91                         mathed_char_height(LM_TC_CONST, size(), 'y', asc, des);
92                         limits = false;
93                 } else if (cx == 0) {
94                         break;
95                 } else if (MathIsInset(cx)) {
96                         int yy = y;
97                         MathedInset * p = data.GetInset();
98                         if (cx == LM_TC_UP) {
99                                 if (limits) {
100                                         x -= (xp > p->Width()) ?
101                                                 p->Width() + (xp - p->Width()) / 2 : xp;  
102                                         yy -= (asc + p->Descent() + 4);
103                                 } else
104                                         yy -= (p->Descent() > asc) ?
105                                                 p->Descent() + 4 : asc;
106                         } else if (cx == LM_TC_DOWN) {
107                                 if (limits) {
108                                         x -= (xp > p->Width()) ?
109                                                 p->Width() + (xp - p->Width()) / 2 : xp;
110                                         yy += des + p->Ascent() + 2;
111                                 } else
112                                         yy += des + p->Ascent() / 2;
113                         } else {
114                                 asc = p->Ascent();
115                                 des = p->Descent();
116                         }
117                         p->draw(pain, x, yy);
118                         if (cx != LM_TC_UP && cx != LM_TC_DOWN) {
119                                 limits = p->GetLimits();
120                                 if (limits)
121                                         xp = p->Width();
122                         }
123                         data.Next();
124                 } else if (cx == LM_TC_TAB) {
125                         if (cxp == cx
126                             || cxp == LM_TC_CR || data.IsFirst()) {
127                                 pain.rectangle(x, y - df_asc,
128                                                df_width, df_asc,
129                                                LColor::mathline);
130                         }
131                         data.Next();
132                         limits = false;
133                 } else if (cx == LM_TC_CR) {
134                         if (cxp == LM_TC_TAB
135                             || cxp == LM_TC_CR || data.IsFirst()) {
136                                 pain.rectangle(x, y - df_asc,
137                                                df_width, df_asc,
138                                                LColor::mathline);
139                         }
140                         data.Next();
141                         limits = false;
142                 } else {         
143                         lyxerr << "GMathed Error: Unrecognized code[" << cx << "]" << endl;
144                         break;
145                 }
146         
147                 cxp = cx;
148         }
149         if (cxp == LM_TC_TAB || cxp == LM_TC_CR) { 
150                 data.GetPos(x, y);
151                 pain.rectangle(x, y - df_asc, df_width, df_asc,
152                                LColor::mathline);
153         }
154 }
155
156
157 void 
158 MathParInset::Metrics()
159 {
160         byte cx;
161         byte cxp = 0;
162         int ls;
163         int asc = df_asc;
164         int des = 0;
165         int tb = 0;
166         int tab = 0;
167         
168         bool limits = false;
169         
170         ascent = df_asc;//mathed_char_height(LM_TC_VAR, size, 'I', asc, des); 
171         width = df_width;
172         descent = 0;
173         if (array.empty()) return;
174         
175         ascent = 0;
176         MathedXIter data(this);
177         data.GoBegin();
178         while (data.OK()) {
179                 cx = data.GetChar();      
180                 if (cx >= ' ') {
181                         string s = data.GetString();
182                         mathed_string_height(data.fcode(),
183                                              size(), s, asc, des);
184                         if (asc > ascent) ascent = asc;
185                         if (des > descent) descent = des;
186                         limits = false;
187                         mathed_char_height(LM_TC_CONST, size(), 'y', asc, des);
188                 } else
189                         if (MathIsInset(cx)) {
190                                 MathedInset * p = data.GetInset();
191                                 p->SetStyle(size());   
192                                 p->Metrics();
193                                 if (cx == LM_TC_UP) {
194                                         asc += (limits) ? p->Height() + 4: p->Ascent() + 
195                                                 ((p->Descent()>asc) ? p->Descent() - asc + 4: 0);
196                                 } else
197                                         if (cx == LM_TC_DOWN) {
198                                                 des += ((limits) ? p->Height() + 4: p->Height() - p->Ascent() / 2);
199                                         } else {
200                                                 asc = p->Ascent();
201                                                 des = p->Descent();
202                                         }
203                                 if (asc > ascent) ascent = asc;
204                                 if (des > descent) descent = des;
205                                 if (cx!= LM_TC_UP && cx!= LM_TC_DOWN)
206                                         limits = p->GetLimits();
207                                 data.Next();
208                         } else 
209                                 if (cx == LM_TC_TAB) {
210                                         int x, y;
211                                         data.GetIncPos(x, y);
212                                         if (data.IsFirst() || cxp == LM_TC_TAB || cxp == LM_TC_CR) {
213                                                 if (ascent<df_asc) ascent = df_asc;
214                                                 tb = x;
215                                         }
216                                         data.setTab(x-tb, tab);
217                                         tb = x;
218                                         ++tab;
219                                         limits = false;                   
220                                         data.Next();
221                                 } else
222                                         if (cx == LM_TC_CR) {
223                                                 if (tb > 0) {
224                                                         int x, y;
225                                                         data.GetIncPos(x, y);
226                                                         if (data.IsFirst() || cxp == LM_TC_TAB || cxp == LM_TC_CR) {
227                                                                 if (ascent<df_asc) ascent = df_asc;
228                                                                 tb = x;
229                                                         } 
230                                                         data.setTab(x - tb, tab);
231                                                 } else //if (GetColumns() == 1) 
232                                                         {
233                                                                 int x, y;
234                                                                 data.GetIncPos(x, y);
235                                                                 data.setTab(x, tab);
236                                                                 if (ascent<df_asc) ascent = df_asc;
237                                                         } 
238                                                 tb = tab = 0;
239                                                 data.subMetrics(ascent, descent);
240                                                 ascent = df_asc;   
241                                                 descent = 0;
242                                                 data.Next();
243                                         } else {
244                                                 lyxerr << "Mathed Error: Unrecognized code[" << cx
245                                                        << "]" << endl;
246                                                 break;
247                                         }       
248                 cxp = cx;
249         }
250         data.GetIncPos(width, ls);
251         
252         // No matter how simple is a matrix, it is NOT a subparagraph
253         if (isMatrix()) {
254                 if (cxp == LM_TC_TAB) {
255                         if (ascent<df_asc) ascent = df_asc;
256                         data.setTab(0, tab);
257                 } else {
258                         data.setTab(width - tb, tab);
259                 }
260         }
261         
262         data.subMetrics(ascent, descent);
263 }
264
265
266
267 void MathParInset::Write(ostream & os, bool fragile)
268 {
269         if (array.empty()) return;
270         int brace = 0;
271         latexkeys const * l;
272         MathedIter data(&array);
273         // hack
274         MathedRowSt const * crow = getRowSt();   
275         data.Reset();
276         
277         if (!Permit(LMPF_FIXED_SIZE)) { 
278                 l = lm_get_key_by_id(size(), LM_TK_STY);
279                 if (l) {
280                         os << '\\' << l->name << ' ';
281                 }
282         }
283         while (data.OK()) {
284                 byte cx = data.GetChar();
285                 if (cx >= ' ') {
286                         string str = data.GetString();
287                         
288                         if (data.fcode() >= LM_TC_RM && data.fcode() <= LM_TC_TEXTRM) {
289                                 os << '\\' << math_font_name[data.fcode()-LM_TC_RM] << '{';
290                         }
291                         for (string::const_iterator s = str.begin();
292                              s != str.end(); ++s) {
293                                 byte c = *s;
294                                 if (MathIsSymbol(data.fcode())) {
295                                         l = lm_get_key_by_id(c, (data.fcode() == LM_TC_BSYM) ?
296                                                              LM_TK_BIGSYM : LM_TK_SYM);
297                                         if (l) {
298                                                 os << '\\' << l->name << ' ';
299                                         } else {
300 #warning this does not compile on gcc 2.97
301                                                 //lyxerr << "Illegal symbol code[" << c
302                                                 //   << " " << str.end() - s << " " << data.fcode() << "]";
303                                         }
304                                 } else {
305                                         // Is there a standard logical XOR?
306                                         if ((data.fcode() == LM_TC_TEX && c != '{' && c != '}') ||
307                                             (data.fcode() == LM_TC_SPECIAL))
308                                                 os << '\\';
309                                         else {
310                                                 if (c == '{') ++brace;
311                                                 if (c == '}') --brace;
312                                         }
313                                         if (c == '}' && data.fcode() == LM_TC_TEX && brace < 0) 
314                                                 lyxerr <<"Math warning: Unexpected closing brace."
315                                                        << endl;
316                                         else           
317                                                 os << char(c);
318                                 }
319                         }
320                         if (data.fcode()>= LM_TC_RM && data.fcode()<= LM_TC_TEXTRM)
321                                 os << '}';
322                 } else     
323                         if (MathIsInset(cx)) {
324                                 MathedInset * p = data.GetInset();
325                                 if (cx == LM_TC_UP)
326                                         os << "^{";
327                                 if (cx == LM_TC_DOWN)
328                                         os << "_{";
329                                 p->Write(os, fragile);
330                                 if (cx == LM_TC_UP || cx == LM_TC_DOWN)
331                                         os << '}';
332                                 data.Next();
333                         } else
334                                 switch (cx) {
335                                 case LM_TC_TAB:
336                                 {
337                                         os << " & ";
338                                         data.Next();
339                                         break;
340                                 }
341                                 case LM_TC_CR:
342                                 {
343                                         if (crow) {
344                                                 if (!crow->isNumbered()) {  
345                                                         os << "\\nonumber ";
346                                                 }
347                                                 if (!crow->getLabel().empty()) {
348                                                         os << "\\label{"
349                                                            << crow->getLabel()
350                                                            << "} ";
351                                                 }
352                                                 crow = crow->getNext();
353                                         }
354                                         if (fragile)
355                                                 os << "\\protect";
356                                         os << "\\\\\n";
357                                         ++number_of_newlines;
358                                         data.Next();
359                                         break;
360                                 }
361                                 default:
362                                         lyxerr << "WMath Error: unrecognized code[" << cx << "]";
363                                         return;
364                                 }     
365         }
366         
367         if (crow) {
368                 if (!crow->isNumbered()) {
369                         os << "\\nonumber ";
370                 }
371                 if (!crow->getLabel().empty()) {
372                         os << "\\label{"
373                            << crow->getLabel()
374                            << "} ";
375                 }
376         }
377
378         if (brace > 0)
379                 os << string(brace, '}');
380 }
381
382
383 void MathParInset::clear()
384 {
385         array.clear();
386 }
387
388 bool MathParInset::Inside(int x, int y) 
389 {
390   return (x >= xo() && x <= xo() + width
391           && y <= yo() + descent && y >= yo() - ascent);
392 }
393
394
395 void MathParInset::GetXY(int & x, int & y) const
396 {
397    x = xo();
398    y = yo();
399 }
400
401
402 void MathParInset::UserSetSize(short sz)
403 {
404    if (sz >= 0) {
405        size(sz);      
406        flag = flag & ~LMPF_FIXED_SIZE;
407    }
408 }
409
410
411 void MathParInset::SetStyle(short sz) 
412 {
413     if (Permit(LMPF_FIXED_SIZE)) {
414         if (Permit(LMPF_SCRIPT)) 
415           sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
416         if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
417             ++sz;
418         } 
419         MathedInset::SetStyle(sz);
420     }
421 }
422
423
424 bool MathParInset::Permit(short f) const
425 {
426         return bool(f & flag);
427 }
428
429
430 MathedArray & MathParInset::GetData()
431 {
432         return array;
433 }
434
435
436 MathedArray const & MathParInset::GetData() const
437 {
438         return array;
439 }
440
441
442 void MathParInset::setXY(int x, int y)
443 {
444         xo_ = x;
445         yo_ = y;
446 }