]> git.lyx.org Git - lyx.git/blob - src/mathed/math_delim.C
new painter,workarea and lcolor. Read the diff/sources and ChangeLog...
[lyx.git] / src / mathed / math_delim.C
1  /* 
2  *  File:        math_delim.C
3  *  Purpose:     Draw delimiters and decorations
4  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
5  *  Created:     January 1996
6  *  Description: Vectorial fonts for simple and resizable objets.
7  *
8  *  Dependencies: Xlib, XForms
9  *
10  *  Copyright: (c) 1996, Alejandro Aguilar Sierra
11  *
12  *   Version: 0.8beta, Mathed & Lyx project.
13  *
14  *   You are free to use and modify this code under the terms of
15  *   the GNU General Public Licence version 2 or later.
16  */
17
18 #include <config.h>
19
20 #include FORMS_H_LOCATION
21 #include <cstdlib>
22 #include "symbol_def.h"
23 #include "math_inset.h"
24 #include "LColor.h"
25 #include "Painter.h"
26
27 /* 
28  * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
29  * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4= square polyline
30  */
31
32 static float parenth[] = {
33   2.0, 13.0,
34   0.9930, 0.0071,  0.7324, 0.0578,  0.5141, 0.1126,  0.3380, 0.1714,
35   0.2183, 0.2333,  0.0634, 0.3621,  0.0141, 0.5000,  0.0563, 0.6369,
36   0.2113, 0.7647,  0.3310, 0.8276,  0.5070, 0.8864,  0.7254, 0.9412,
37   0.9930, 0.9919,
38   0.0   
39 };
40
41 static float parenthHigh[] = {
42     2.0, 13.0, 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772, 0.2540, 
43     0.1278, 0.1746, 0.1966, 0.0952, 0.3300, 0.0950, 0.5000, 0.0952, 0.6700, 
44     0.1746, 0.8034, 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677, 0.9840, 
45     0.9986, 0.0 
46 };
47
48 static float brace[] = {
49   2.0, 21.0,
50   0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243, 0.5819, 0.0527,
51   0.4859, 0.0892, 0.4463, 0.1278, 0.4463, 0.3732, 0.4011, 0.4199,
52   0.2712, 0.4615, 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
53   0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268, 0.4463, 0.8722,
54   0.4859, 0.9108, 0.5819, 0.9473, 0.7458, 0.9757, 0.9379, 0.9980,
55   0.9492, 0.9980,
56   0.0
57 };
58
59 static float arrow[] = {
60    4, 7,
61    0.015, 0.7500,  0.2, 0.6,  0.35, 0.35,  0.5, 0.05,
62    0.65, 0.35,  0.8, 0.6,  0.95, 0.7500,
63    3, 0.5, 0.15,  0.5, 0.95,
64    0.0 
65 };
66
67 static float Arrow[] = {
68    4, 7,
69    0.015, 0.7500,  0.2, 0.6,  0.35, 0.35,  0.5, 0.05,
70    0.65, 0.35,  0.8, 0.6,  0.95, 0.7500,
71    3, 0.35, 0.5, 0.35, 0.95,
72    3, 0.65, 0.5, 0.65, 0.95,
73    0.0
74 };
75
76 static float udarrow[] = {
77    2, 3,
78    0.015, 0.25,  0.5, 0.05, 0.95, 0.25,
79    2, 3,
80    0.015, 0.75,  0.5, 0.95, 0.95, 0.75,  
81    1, 0.5, 0.2,  0.5, 0.8,
82    0.0 
83 };
84
85 static float Udarrow[] = {
86    2, 3,
87    0.015, 0.25,  0.5, 0.05, 0.95, 0.25,
88    2, 3,
89    0.015, 0.75,  0.5, 0.95, 0.95, 0.75,  
90    1, 0.35, 0.2, 0.35, 0.8,
91    1, 0.65, 0.2, 0.65, 0.8,
92    0.0 
93 };
94
95 static float brack[] = {
96    2.0, 4,
97    0.95, 0.05,  0.05, 0.05,  0.05, 0.95,  0.95, 0.95,
98    0.0
99 };
100
101 static float corner[] = {
102    2.0, 3,
103    0.95, 0.05,  0.05, 0.05,  0.05, 0.95,
104    0.0
105 };
106
107 static float angle[] = {
108    2.0, 3,
109    1, 0,  0.05, 0.5,  1, 1,
110    0.0
111 };
112
113 static float slash[] = {
114    1, 0.95, 0.05,  0.05, 0.95, 
115    0.0
116 };
117
118 static float hline[] = {
119    1, 0.05, 0.5,  0.95, 0.5, 
120    0.0
121 };
122
123
124 static float hline2[] = {
125    1, 0.1, 0.5,  0.3, 0.5,
126    1, 0.7, 0.5,  0.9, 0.5,
127    0.0
128 }; 
129
130 static float hline3[] = {
131    1, 0.1, 0,  0.15, 0,
132    1, 0.475, 0,  0.525, 0,
133    1, 0.85, 0,  0.9, 0,  
134    0.0
135 };
136
137
138 static float dline3[] = {
139    1, 0.1, 0.1,  0.15, 0.15,
140    1, 0.475, 0.475,  0.525, 0.525,
141    1, 0.85, 0.85,  0.9, 0.9,
142    0.0
143 };     
144
145 static float hlinesmall[] = {
146    1, 0.4, 0.5,  0.6, 0.5, 
147    0.0
148 };
149
150 static float vert[] = {
151    1, 0.5, 0.05,  0.5, 0.95, 
152    0.0
153 };
154
155 static float Vert[] = {
156    1, 0.3, 0.05,  0.3, 0.95, 
157    1, 0.7, 0.05,  0.7, 0.95,
158    0.0
159 };
160
161 static float tilde[] = {
162    2.0, 4,
163    0.05, 0.8,  0.25, 0.2,  0.75, 0.8,  0.95, 0.2,
164    0.0
165 };
166
167 static struct math_deco_struct {
168     int code; float *data; int angle;
169 } math_deco_table[] = {   
170
171    // Decorations
172   { LM_widehat, &angle[0], 3 },
173   { LM_widetilde, &tilde[0], 0 },
174   { LM_underline, &hline[0], 0 },
175   { LM_overline, &hline[0], 0 },
176   { LM_underbrace, &brace[0], 1 },
177   { LM_overbrace,  &brace[0], 3 },
178   { LM_overleftarrow, &arrow[0], 1 },
179   { LM_overightarrow, &arrow[0], 3 },
180      
181   // Delimiters
182   { '(', &parenth[0], 0 },
183   { ')', &parenth[0], 2 },
184   { '{', &brace[0], 0 },
185   { '}', &brace[0], 2 },
186   { '[', &brack[0], 0 },
187   { ']', &brack[0], 2 },
188   { '|', &vert[0], 0 },
189   { '/', &slash[0], 0 },
190   { LM_Vert, &Vert[0], 0 },
191   { LM_backslash, &slash[0], 1 },
192   { LM_langle, &angle[0], 0 },
193   { LM_lceil, &corner[0], 0 }, 
194   { LM_lfloor, &corner[0], 1 },  
195   { LM_rangle, &angle[0], 2 }, 
196   { LM_rceil, &corner[0], 3 }, 
197   { LM_rfloor, &corner[0], 2 },
198   { LM_downarrow, &arrow[0], 2 },
199   { LM_Downarrow, &Arrow[0], 2 }, 
200   { LM_uparrow, &arrow[0], 0 },
201   { LM_Uparrow, &Arrow[0], 0 },
202   { LM_updownarrow, &udarrow[0], 0 },
203   { LM_Updownarrow, &Udarrow[0], 0 },    
204
205   // Accents   
206   { LM_ddot, &hline2[0], 0 },
207   { LM_hat, &angle[0], 3 },
208   { LM_grave, &slash[0], 1 },
209   { LM_acute, &slash[0], 0 },
210   { LM_tilde, &tilde[0], 0 },
211   { LM_bar, &hline[0], 0 },
212   { LM_dot, &hlinesmall[0], 0 },
213   { LM_check, &angle[0], 1 },
214   { LM_breve, &parenth[0], 1 },
215   { LM_vec, &arrow[0], 3 },
216   { LM_not, &slash[0], 0 },  
217
218   // Dots
219   { LM_ldots, &hline3[0], 0 }, 
220   { LM_cdots, &hline3[0], 0 },
221   { LM_vdots, &hline3[0], 1 },
222   { LM_ddots, &dline3[0], 0 },
223      
224   { 0, 0, 0 }
225 };
226
227
228 inline int odd(int x) { return ((x) & 1); }
229
230 typedef float matriz_data[2][2];
231
232 const matriz_data MATIDEN= { {1, 0}, {0, 1}};
233
234 extern void mathed_set_font(short type, int style);
235 extern int mathed_char_width(short type, int style, byte c);
236 extern int mathed_char_height(short, int, byte, int&, int&);
237
238 #define mateq(m1, m2)  memcpy(m1, m2, sizeof(matriz_data))
239
240 class Matriz {
241  public: 
242    Matriz() { mateq(m, MATIDEN); }
243    void rota(int);
244    void escala(float, float);
245    void transf(float, float, float&, float&);
246    
247  protected:
248    matriz_data m;
249    void matmat(matriz_data& a);
250 };
251
252
253 void Matriz::rota(int code)
254 {
255    float cs, sn;
256    
257    matriz_data r;
258    mateq(r, MATIDEN);
259    cs = (odd(code)) ? 0: 1 - code;
260    sn = (odd(code)) ? 2 - code: 0;
261    r[0][0] = cs;         r[0][1] = sn;
262    r[1][0] = -r[0][1];   r[1][1] = r[0][0];
263    matmat(r);
264 }
265
266 void Matriz::escala(float x, float y)
267 {
268    matriz_data s;
269    mateq(s, MATIDEN);
270    s[0][0] = x;  s[1][1] = y;
271    matmat(s);
272 }
273
274
275 void Matriz::matmat(matriz_data& a)
276 {
277    matriz_data c;   
278    for (int i = 0;i < 2; ++i) {
279       c[0][i] = a[0][0]*m[0][i] + a[0][1]*m[1][i];
280       c[1][i] = a[1][0]*m[0][i] + a[1][1]*m[1][i];
281    }
282    mateq(m, c);
283 }
284
285 void Matriz::transf(float xp, float yp, float &x, float &y)
286 {
287    x = m[0][0]*xp + m[0][1]*yp;
288    y = m[1][0]*xp + m[1][1]*yp;
289 }
290
291 extern GC latexGC, mathGC, mathLineGC, cursorGC;
292
293 static int search_deco(int code)
294 {
295    int i= 0;
296    
297    while (math_deco_table[i].code &&  math_deco_table[i].code!= code) ++i;
298    if (!math_deco_table[i].code) i = -1;
299    return i;
300 }
301
302
303 #ifdef USE_PAINTER
304 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
305 {
306         Matriz mt, sqmt;
307         float xx, yy, x2, y2;
308         int i = 0;
309    
310         int j = search_deco(code);   
311         if (j < 0) return;
312    
313         int r = math_deco_table[j].angle;
314         float * d = math_deco_table[j].data;
315         
316         if (h > 70 && (math_deco_table[j].code == int('(') || math_deco_table[j].code == int(')')))
317                 d = parenthHigh;
318         
319         mt.rota(r);
320         mt.escala(w, h);
321    
322         int n = (w < h) ? w: h;
323         sqmt.rota(r);
324         sqmt.escala(n, n);
325         if (r > 0 && r < 3) y += h;   
326         if (r >= 2) x += w;   
327         do {
328                 code = int(d[i++]);
329                 switch (code) {
330                 case 0: break;
331                 case 1: 
332                 case 3:
333                 {
334                         xx = d[i++]; yy = d[i++];
335                         x2 = d[i++]; y2 = d[i++];
336                         if (code == 3) 
337                                 sqmt.transf(xx, yy, xx, yy);
338                         else
339                                 mt.transf(xx, yy, xx, yy);
340                         mt.transf(x2, y2, x2, y2);
341                         pain.line(x + int(xx), y + int(yy),
342                                   x + int(x2), y + int(y2),
343                                   LColor::mathline);
344                         break;
345                 }        
346                 case 2: 
347                 case 4:
348                 {
349                         int xp[32], yp[32];
350                         n = int(d[i++]);
351                         for (j = 0; j < n; ++j) {
352                                 xx = d[i++]; yy = d[i++];
353 //           lyxerr << " " << xx << " " << yy << " ";
354                                 if (code == 4) 
355                                         sqmt.transf(xx, yy, xx, yy);
356                                 else
357                                         mt.transf(xx, yy, xx, yy);
358                                 xp[j] = x + int(xx);
359                                 yp[j] = y + int(yy);
360                                 //  lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
361                         }
362                         pain.lines(xp, yp, n, LColor::mathline);
363                 }
364                 }
365         } while (code);
366 }
367 #else
368 void mathed_draw_deco(Window win, int x, int y, int w, int h, int code)
369 {
370    Matriz mt, sqmt;
371    XPoint p[32];
372    float xx, yy, x2, y2;
373    int i= 0, j, n;
374    
375    j = search_deco(code);   
376    if (j < 0) return;
377    
378    int r = math_deco_table[j].angle;
379    float * d = math_deco_table[j].data;
380    
381    if (h > 70 && (math_deco_table[j].code == int('(') || math_deco_table[j].code == int(')')))
382       d = parenthHigh;
383     
384    mt.rota(r);
385    mt.escala(w, h);
386    
387    n = (w < h) ? w: h;
388    sqmt.rota(r);
389    sqmt.escala(n, n);
390    if (r> 0 && r< 3) y += h;   
391    if (r>= 2) x += w;   
392    do {
393       code = int(d[i++]);
394       switch (code) {
395        case 0: break;
396        case 1: 
397        case 3:
398        {
399           xx = d[i++]; yy = d[i++];
400           x2 = d[i++]; y2 = d[i++];
401           if (code == 3) 
402             sqmt.transf(xx, yy, xx, yy);
403           else
404             mt.transf(xx, yy, xx, yy);
405           mt.transf(x2, y2, x2, y2);
406           XDrawLine(fl_display, win, mathGC, x + int(xx), y + int(yy),
407                     x + int(x2), y + int(y2));
408           XFlush(fl_display);
409           break;
410        }         
411        case 2: 
412        case 4:
413        {
414           n = int(d[i++]);
415           for (j = 0; j < n; ++j) {
416              xx = d[i++]; yy = d[i++];
417 //           lyxerr << " " << xx << " " << yy << " ";
418              if (code == 4) 
419                sqmt.transf(xx, yy, xx, yy);
420              else
421                mt.transf(xx, yy, xx, yy);
422              p[j].x = x + int(xx);
423              p[j].y = y + int(yy);
424              //  lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
425           }
426           XDrawLines(fl_display, win, mathLineGC, p, n, CoordModeOrigin);
427           XFlush(fl_display);
428        }
429       }
430    } while (code);
431 }
432 #endif
433
434
435 #ifdef USE_PAINTER
436 void
437 MathDelimInset::draw(Painter & pain, int x, int y)
438
439         xo = x;  yo = y; 
440         MathParInset::draw(pain, x + dw + 2, y - dh); 
441         
442         if (left == '.') {
443                 pain.line(x + 4, yo - ascent,
444                           x + 4, yo + descent,
445                           LColor::mathcursor);
446         } else
447                 mathed_draw_deco(pain, x, y - ascent, dw, Height(), left);
448         x += Width()-dw-2;
449         if (right == '.') {
450                 pain.line(x + 4, yo - ascent,
451                           x + 4, yo + descent,
452                           LColor::mathcursor);
453         } else
454                 mathed_draw_deco(pain, x, y-ascent, dw, Height(), right);
455 }
456 #else
457 void
458 MathDelimInset::Draw(int x, int y)
459
460    xo = x;  yo = y; 
461    MathParInset::Draw(x+dw+2, y-dh); 
462    //int h= Height(), hg= descent-1;  
463
464    if (left == '.') {
465      XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
466      XFlush(fl_display);
467    }
468    else
469      mathed_draw_deco(pm, x, y-ascent, dw, Height(), left);
470    x += Width()-dw-2;
471    if (right == '.') {
472      XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
473      XFlush(fl_display);
474    }
475    else
476      mathed_draw_deco(pm, x, y-ascent, dw, Height(), right);
477 }
478 #endif
479
480
481 void
482 MathDelimInset::Metrics()
483 {
484    MathParInset::Metrics();
485    int d;
486    
487    mathed_char_height(LM_TC_CONST, size, 'I', d, dh);
488    dh /= 2;
489    ascent += 2 + dh;
490    descent += 2 - dh;
491    dw = Height()/5;
492    if (dw > 15) dw = 15;
493    if (dw<6) dw = 6;
494    width += 2*dw+4;
495 }
496
497
498 #ifdef USE_PAINTER
499 void
500 MathDecorationInset::draw(Painter & pain, int x, int y)
501
502    MathParInset::draw(pain, x + (width - dw) / 2, y);
503    mathed_draw_deco(pain, x, y + dy, width, dh, deco);
504 }
505 #else
506 void
507 MathDecorationInset::Draw(int x, int y)
508
509    MathParInset::Draw(x+(width-dw)/2, y);
510    mathed_draw_deco(pm, x, y+dy, width, dh, deco);
511 }
512 #endif
513
514
515 void
516 MathDecorationInset::Metrics()
517 {
518    int h = 2*mathed_char_height(LM_TC_VAR, size, 'I', ascent, descent);  
519    MathParInset::Metrics();
520    int w = Width()+4;
521    if (w<16) w = 16;
522    dh = w/5;
523    if (dh>h) dh = h;
524
525    if (upper) {
526       ascent += dh+2;
527       dy = -ascent;
528    } else {
529       dy = descent+2;
530       descent += dh+4;
531    }
532    dw = width;
533    width = w;
534 }
535
536
537 #ifdef USE_PAINTER
538 void
539 MathAccentInset::draw(Painter & pain, int x, int y)
540 {
541     int dw = width - 2;
542
543     if (inset) {
544         inset->draw(pain, x, y);
545     } else {
546         drawStr(pain, fn, size, x, y, &c, 1);
547     }
548     x += (code == LM_not) ? (width-dw) / 2 : 2;
549     mathed_draw_deco(pain, x, y - dy, dw, dh, code);
550 }
551 #else
552 void
553 MathAccentInset::Draw(int x, int y)
554 {
555     int dw = width-2;
556 /*    char s[8];
557     mathed_set_font(fn, size);
558     if (MathIsBinary(fn)) {
559         s[0] = s[2] = ' '; 
560         s[1] = (char)c;
561         ns = 3;
562         dw = mathed_char_width(fn, size, c);
563     } else
564       s[0] = (char)c;
565 */
566     if (inset) {
567         inset->Draw(x, y);
568     } else {
569         drawStr(fn, size, x, y, &c, 1);
570         XFlush(fl_display);
571     }
572     x += (code == LM_not) ? (width-dw)/2: 2;
573     mathed_draw_deco(pm, x, y-dy, dw, dh, code);
574 }
575 #endif
576
577
578 void
579 MathAccentInset::Metrics()
580 {
581     
582     if (inset) {
583         inset->Metrics();
584         ascent = inset->Ascent();
585         descent = inset->Descent();
586         width = inset->Width();
587         dh = ascent;
588     } else {
589         mathed_char_height(fn, size, c, ascent, descent);
590         width = mathed_char_width(fn, size, c);
591         dh = (width-2)/2; 
592     }
593     if (code == LM_not) {
594         ascent += dh;
595         descent += dh;
596         dh = Height();
597     } else 
598       ascent += dh+2;
599             
600     dy = ascent;
601 //    if (MathIsBinary(fn))
602 //      width += 2*mathed_char_width(fn, size, ' ');    
603 }
604
605
606 #ifdef USE_PAINTER
607 void
608 MathDotsInset::draw(Painter & pain, int x, int y)
609 {
610    mathed_draw_deco(pain, x + 2, y - dh, width - 2, ascent, code);
611    if (code == LM_vdots || code == LM_ddots) ++x; 
612    if (code != LM_vdots) --y;
613    mathed_draw_deco(pain, x + 2, y - dh, width - 2, ascent, code);
614 }
615 #else
616 void
617 MathDotsInset::Draw(int x, int y)
618 {
619    mathed_draw_deco(pm, x + 2, y - dh, width - 2, ascent, code);
620    if (code == LM_vdots || code == LM_ddots) ++x; 
621    if (code!= LM_vdots) --y;
622    mathed_draw_deco(pm, x + 2, y - dh, width - 2, ascent, code);
623 }
624 #endif
625
626
627 void
628 MathDotsInset::Metrics()
629 {
630    mathed_char_height(LM_TC_VAR, size, 'M', ascent, descent);
631    width = mathed_char_width(LM_TC_VAR, size, 'M');   
632    switch (code) {
633     case LM_ldots: dh = 0; break;
634     case LM_cdots: dh = ascent/2; break;
635     case LM_vdots: width /= 2;
636     case LM_ddots: dh = ascent; break;
637    }
638
639