]> git.lyx.org Git - lyx.git/blob - src/mathed/math_delim.C
some small updates to Painter, and make the new painter the default.
[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 #ifndef USE_PAINTER
235 extern void mathed_set_font(short type, int style);
236 #endif
237 extern int mathed_char_width(short type, int style, byte c);
238 extern int mathed_char_height(short, int, byte, int&, int&);
239
240 #define mateq(m1, m2)  memcpy(m1, m2, sizeof(matriz_data))
241
242 class Matriz {
243  public: 
244    Matriz() { mateq(m, MATIDEN); }
245    void rota(int);
246    void escala(float, float);
247    void transf(float, float, float&, float&);
248    
249  protected:
250    matriz_data m;
251    void matmat(matriz_data& a);
252 };
253
254
255 void Matriz::rota(int code)
256 {
257    float cs, sn;
258    
259    matriz_data r;
260    mateq(r, MATIDEN);
261    cs = (odd(code)) ? 0: 1 - code;
262    sn = (odd(code)) ? 2 - code: 0;
263    r[0][0] = cs;         r[0][1] = sn;
264    r[1][0] = -r[0][1];   r[1][1] = r[0][0];
265    matmat(r);
266 }
267
268 void Matriz::escala(float x, float y)
269 {
270    matriz_data s;
271    mateq(s, MATIDEN);
272    s[0][0] = x;  s[1][1] = y;
273    matmat(s);
274 }
275
276
277 void Matriz::matmat(matriz_data& a)
278 {
279    matriz_data c;   
280    for (int i = 0;i < 2; ++i) {
281       c[0][i] = a[0][0]*m[0][i] + a[0][1]*m[1][i];
282       c[1][i] = a[1][0]*m[0][i] + a[1][1]*m[1][i];
283    }
284    mateq(m, c);
285 }
286
287 void Matriz::transf(float xp, float yp, float &x, float &y)
288 {
289    x = m[0][0]*xp + m[0][1]*yp;
290    y = m[1][0]*xp + m[1][1]*yp;
291 }
292
293 #ifndef USE_PAINTER
294 extern GC latexGC, mathGC, mathLineGC, cursorGC;
295 #endif
296
297 static int search_deco(int code)
298 {
299    int i= 0;
300    
301    while (math_deco_table[i].code &&  math_deco_table[i].code!= code) ++i;
302    if (!math_deco_table[i].code) i = -1;
303    return i;
304 }
305
306
307 #ifdef USE_PAINTER
308 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
309 {
310         Matriz mt, sqmt;
311         float xx, yy, x2, y2;
312         int i = 0;
313    
314         int j = search_deco(code);   
315         if (j < 0) return;
316    
317         int r = math_deco_table[j].angle;
318         float * d = math_deco_table[j].data;
319         
320         if (h > 70 && (math_deco_table[j].code == int('(') || math_deco_table[j].code == int(')')))
321                 d = parenthHigh;
322         
323         mt.rota(r);
324         mt.escala(w, h);
325    
326         int n = (w < h) ? w: h;
327         sqmt.rota(r);
328         sqmt.escala(n, n);
329         if (r > 0 && r < 3) y += h;   
330         if (r >= 2) x += w;   
331         do {
332                 code = int(d[i++]);
333                 switch (code) {
334                 case 0: break;
335                 case 1: 
336                 case 3:
337                 {
338                         xx = d[i++]; yy = d[i++];
339                         x2 = d[i++]; y2 = d[i++];
340                         if (code == 3) 
341                                 sqmt.transf(xx, yy, xx, yy);
342                         else
343                                 mt.transf(xx, yy, xx, yy);
344                         mt.transf(x2, y2, x2, y2);
345                         pain.line(x + int(xx), y + int(yy),
346                                   x + int(x2), y + int(y2),
347                                   LColor::mathline);
348                         break;
349                 }        
350                 case 2: 
351                 case 4:
352                 {
353                         int xp[32], yp[32];
354                         n = int(d[i++]);
355                         for (j = 0; j < n; ++j) {
356                                 xx = d[i++]; yy = d[i++];
357 //           lyxerr << " " << xx << " " << yy << " ";
358                                 if (code == 4) 
359                                         sqmt.transf(xx, yy, xx, yy);
360                                 else
361                                         mt.transf(xx, yy, xx, yy);
362                                 xp[j] = x + int(xx);
363                                 yp[j] = y + int(yy);
364                                 //  lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
365                         }
366                         pain.lines(xp, yp, n, LColor::mathline);
367                 }
368                 }
369         } while (code);
370 }
371 #else
372 void mathed_draw_deco(Window win, int x, int y, int w, int h, int code)
373 {
374    Matriz mt, sqmt;
375    XPoint p[32];
376    float xx, yy, x2, y2;
377    int i= 0, j, n;
378    
379    j = search_deco(code);   
380    if (j < 0) return;
381    
382    int r = math_deco_table[j].angle;
383    float * d = math_deco_table[j].data;
384    
385    if (h > 70 && (math_deco_table[j].code == int('(') || math_deco_table[j].code == int(')')))
386       d = parenthHigh;
387     
388    mt.rota(r);
389    mt.escala(w, h);
390    
391    n = (w < h) ? w: h;
392    sqmt.rota(r);
393    sqmt.escala(n, n);
394    if (r> 0 && r< 3) y += h;   
395    if (r>= 2) x += w;   
396    do {
397       code = int(d[i++]);
398       switch (code) {
399        case 0: break;
400        case 1: 
401        case 3:
402        {
403           xx = d[i++]; yy = d[i++];
404           x2 = d[i++]; y2 = d[i++];
405           if (code == 3) 
406             sqmt.transf(xx, yy, xx, yy);
407           else
408             mt.transf(xx, yy, xx, yy);
409           mt.transf(x2, y2, x2, y2);
410           XDrawLine(fl_display, win, mathGC, x + int(xx), y + int(yy),
411                     x + int(x2), y + int(y2));
412           XFlush(fl_display);
413           break;
414        }         
415        case 2: 
416        case 4:
417        {
418           n = int(d[i++]);
419           for (j = 0; j < n; ++j) {
420              xx = d[i++]; yy = d[i++];
421 //           lyxerr << " " << xx << " " << yy << " ";
422              if (code == 4) 
423                sqmt.transf(xx, yy, xx, yy);
424              else
425                mt.transf(xx, yy, xx, yy);
426              p[j].x = x + int(xx);
427              p[j].y = y + int(yy);
428              //  lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
429           }
430           XDrawLines(fl_display, win, mathLineGC, p, n, CoordModeOrigin);
431           XFlush(fl_display);
432        }
433       }
434    } while (code);
435 }
436 #endif
437
438
439 #ifdef USE_PAINTER
440 void
441 MathDelimInset::draw(Painter & pain, int x, int y)
442
443         xo = x;  yo = y; 
444         MathParInset::draw(pain, x + dw + 2, y - dh); 
445         
446         if (left == '.') {
447                 pain.line(x + 4, yo - ascent,
448                           x + 4, yo + descent,
449                           LColor::mathcursor);
450         } else
451                 mathed_draw_deco(pain, x, y - ascent, dw, Height(), left);
452         x += Width()-dw-2;
453         if (right == '.') {
454                 pain.line(x + 4, yo - ascent,
455                           x + 4, yo + descent,
456                           LColor::mathcursor);
457         } else
458                 mathed_draw_deco(pain, x, y-ascent, dw, Height(), right);
459 }
460 #else
461 void
462 MathDelimInset::Draw(int x, int y)
463
464    xo = x;  yo = y; 
465    MathParInset::Draw(x+dw+2, y-dh); 
466    //int h= Height(), hg= descent-1;  
467
468    if (left == '.') {
469      XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
470      XFlush(fl_display);
471    }
472    else
473      mathed_draw_deco(pm, x, y-ascent, dw, Height(), left);
474    x += Width()-dw-2;
475    if (right == '.') {
476      XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
477      XFlush(fl_display);
478    }
479    else
480      mathed_draw_deco(pm, x, y-ascent, dw, Height(), right);
481 }
482 #endif
483
484
485 void
486 MathDelimInset::Metrics()
487 {
488    MathParInset::Metrics();
489    int d;
490    
491    mathed_char_height(LM_TC_CONST, size, 'I', d, dh);
492    dh /= 2;
493    ascent += 2 + dh;
494    descent += 2 - dh;
495    dw = Height()/5;
496    if (dw > 15) dw = 15;
497    if (dw<6) dw = 6;
498    width += 2*dw+4;
499 }
500
501
502 #ifdef USE_PAINTER
503 void
504 MathDecorationInset::draw(Painter & pain, int x, int y)
505
506    MathParInset::draw(pain, x + (width - dw) / 2, y);
507    mathed_draw_deco(pain, x, y + dy, width, dh, deco);
508 }
509 #else
510 void
511 MathDecorationInset::Draw(int x, int y)
512
513    MathParInset::Draw(x+(width-dw)/2, y);
514    mathed_draw_deco(pm, x, y+dy, width, dh, deco);
515 }
516 #endif
517
518
519 void
520 MathDecorationInset::Metrics()
521 {
522    int h = 2*mathed_char_height(LM_TC_VAR, size, 'I', ascent, descent);  
523    MathParInset::Metrics();
524    int w = Width()+4;
525    if (w<16) w = 16;
526    dh = w/5;
527    if (dh>h) dh = h;
528
529    if (upper) {
530       ascent += dh+2;
531       dy = -ascent;
532    } else {
533       dy = descent+2;
534       descent += dh+4;
535    }
536    dw = width;
537    width = w;
538 }
539
540
541 #ifdef USE_PAINTER
542 void
543 MathAccentInset::draw(Painter & pain, int x, int y)
544 {
545     int dw = width - 2;
546
547     if (inset) {
548         inset->draw(pain, x, y);
549     } else {
550         drawStr(pain, fn, size, x, y, &c, 1);
551     }
552     x += (code == LM_not) ? (width-dw) / 2 : 2;
553     mathed_draw_deco(pain, x, y - dy, dw, dh, code);
554 }
555 #else
556 void
557 MathAccentInset::Draw(int x, int y)
558 {
559     int dw = width-2;
560 /*    char s[8];
561     mathed_set_font(fn, size);
562     if (MathIsBinary(fn)) {
563         s[0] = s[2] = ' '; 
564         s[1] = (char)c;
565         ns = 3;
566         dw = mathed_char_width(fn, size, c);
567     } else
568       s[0] = (char)c;
569 */
570     if (inset) {
571         inset->Draw(x, y);
572     } else {
573         drawStr(fn, size, x, y, &c, 1);
574         XFlush(fl_display);
575     }
576     x += (code == LM_not) ? (width-dw)/2: 2;
577     mathed_draw_deco(pm, x, y-dy, dw, dh, code);
578 }
579 #endif
580
581
582 void
583 MathAccentInset::Metrics()
584 {
585     
586     if (inset) {
587         inset->Metrics();
588         ascent = inset->Ascent();
589         descent = inset->Descent();
590         width = inset->Width();
591         dh = ascent;
592     } else {
593         mathed_char_height(fn, size, c, ascent, descent);
594         width = mathed_char_width(fn, size, c);
595         dh = (width-2)/2; 
596     }
597     if (code == LM_not) {
598         ascent += dh;
599         descent += dh;
600         dh = Height();
601     } else 
602       ascent += dh+2;
603             
604     dy = ascent;
605 //    if (MathIsBinary(fn))
606 //      width += 2*mathed_char_width(fn, size, ' ');    
607 }
608
609
610 #ifdef USE_PAINTER
611 void
612 MathDotsInset::draw(Painter & pain, int x, int y)
613 {
614    mathed_draw_deco(pain, x + 2, y - dh, width - 2, ascent, code);
615    if (code == LM_vdots || code == LM_ddots) ++x; 
616    if (code != LM_vdots) --y;
617    mathed_draw_deco(pain, x + 2, y - dh, width - 2, ascent, code);
618 }
619 #else
620 void
621 MathDotsInset::Draw(int x, int y)
622 {
623    mathed_draw_deco(pm, x + 2, y - dh, width - 2, ascent, code);
624    if (code == LM_vdots || code == LM_ddots) ++x; 
625    if (code!= LM_vdots) --y;
626    mathed_draw_deco(pm, x + 2, y - dh, width - 2, ascent, code);
627 }
628 #endif
629
630
631 void
632 MathDotsInset::Metrics()
633 {
634    mathed_char_height(LM_TC_VAR, size, 'M', ascent, descent);
635    width = mathed_char_width(LM_TC_VAR, size, 'M');   
636    switch (code) {
637     case LM_ldots: dh = 0; break;
638     case LM_cdots: dh = ascent/2; break;
639     case LM_vdots: width /= 2;
640     case LM_ddots: dh = ascent; break;
641    }
642
643