This commit was manufactured by cvs2svn to create tag
[lyx.git] / src / mathed / math_delim.C
blobacc7687bccf1d70f4a17af58a02b64ad0f32f7d8
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  */
18 #include <config.h>
20 #include FORMS_H_LOCATION
21 #include <cstdlib>
22 #include "symbol_def.h"
23 #include "math_inset.h"
25 /* 
26  * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
27  * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4= square polyline
28  */
30 static float parenth[] = {
31   2.0, 13.0,
32   0.9930, 0.0071,  0.7324, 0.0578,  0.5141, 0.1126,  0.3380, 0.1714,
33   0.2183, 0.2333,  0.0634, 0.3621,  0.0141, 0.5000,  0.0563, 0.6369,
34   0.2113, 0.7647,  0.3310, 0.8276,  0.5070, 0.8864,  0.7254, 0.9412,
35   0.9930, 0.9919,
36   0.0   
39 static float parenthHigh[] = {
40     2.0, 13.0, 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772, 0.2540, 
41     0.1278, 0.1746, 0.1966, 0.0952, 0.3300, 0.0950, 0.5000, 0.0952, 0.6700, 
42     0.1746, 0.8034, 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677, 0.9840, 
43     0.9986, 0.0 
46 static float brace[] = {
47   2.0, 21.0,
48   0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243, 0.5819, 0.0527,
49   0.4859, 0.0892, 0.4463, 0.1278, 0.4463, 0.3732, 0.4011, 0.4199,
50   0.2712, 0.4615, 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
51   0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268, 0.4463, 0.8722,
52   0.4859, 0.9108, 0.5819, 0.9473, 0.7458, 0.9757, 0.9379, 0.9980,
53   0.9492, 0.9980,
54   0.0
57 static float arrow[] = {
58    4, 7,
59    0.015, 0.7500,  0.2, 0.6,  0.35, 0.35,  0.5, 0.05,
60    0.65, 0.35,  0.8, 0.6,  0.95, 0.7500,
61    3, 0.5, 0.15,  0.5, 0.95,
62    0.0 
65 static float Arrow[] = {
66    4, 7,
67    0.015, 0.7500,  0.2, 0.6,  0.35, 0.35,  0.5, 0.05,
68    0.65, 0.35,  0.8, 0.6,  0.95, 0.7500,
69    3, 0.35, 0.5, 0.35, 0.95,
70    3, 0.65, 0.5, 0.65, 0.95,
71    0.0
74 static float udarrow[] = {
75    2, 3,
76    0.015, 0.25,  0.5, 0.05, 0.95, 0.25,
77    2, 3,
78    0.015, 0.75,  0.5, 0.95, 0.95, 0.75,  
79    1, 0.5, 0.2,  0.5, 0.8,
80    0.0 
83 static float Udarrow[] = {
84    2, 3,
85    0.015, 0.25,  0.5, 0.05, 0.95, 0.25,
86    2, 3,
87    0.015, 0.75,  0.5, 0.95, 0.95, 0.75,  
88    1, 0.35, 0.2, 0.35, 0.8,
89    1, 0.65, 0.2, 0.65, 0.8,
90    0.0 
93 static float brack[] = {
94    2.0, 4,
95    0.95, 0.05,  0.05, 0.05,  0.05, 0.95,  0.95, 0.95,
96    0.0
99 static float corner[] = {
100    2.0, 3,
101    0.95, 0.05,  0.05, 0.05,  0.05, 0.95,
102    0.0
105 static float angle[] = {
106    2.0, 3,
107    1, 0,  0.05, 0.5,  1, 1,
108    0.0
111 static float slash[] = {
112    1, 0.95, 0.05,  0.05, 0.95, 
113    0.0
116 static float hline[] = {
117    1, 0.05, 0.5,  0.95, 0.5, 
118    0.0
122 static float hline2[] = {
123    1, 0.1, 0.5,  0.3, 0.5,
124    1, 0.7, 0.5,  0.9, 0.5,
125    0.0
126 }; 
128 static float hline3[] = {
129    1, 0.1, 0,  0.15, 0,
130    1, 0.475, 0,  0.525, 0,
131    1, 0.85, 0,  0.9, 0,  
132    0.0
136 static float dline3[] = {
137    1, 0.1, 0.1,  0.15, 0.15,
138    1, 0.475, 0.475,  0.525, 0.525,
139    1, 0.85, 0.85,  0.9, 0.9,
140    0.0
141 };     
143 static float hlinesmall[] = {
144    1, 0.4, 0.5,  0.6, 0.5, 
145    0.0
148 static float vert[] = {
149    1, 0.5, 0.05,  0.5, 0.95, 
150    0.0
153 static float Vert[] = {
154    1, 0.3, 0.05,  0.3, 0.95, 
155    1, 0.7, 0.05,  0.7, 0.95,
156    0.0
159 static float tilde[] = {
160    2.0, 4,
161    0.05, 0.8,  0.25, 0.2,  0.75, 0.8,  0.95, 0.2,
162    0.0
165 static struct math_deco_struct {
166     int code; float *data; int angle;
167 } math_deco_table[] = {   
169    // Decorations
170   { LM_widehat, &angle[0], 3 },
171   { LM_widetilde, &tilde[0], 0 },
172   { LM_underline, &hline[0], 0 },
173   { LM_overline, &hline[0], 0 },
174   { LM_underbrace, &brace[0], 1 },
175   { LM_overbrace,  &brace[0], 3 },
176   { LM_overleftarrow, &arrow[0], 1 },
177   { LM_overightarrow, &arrow[0], 3 },
178      
179   // Delimiters
180   { '(', &parenth[0], 0 },
181   { ')', &parenth[0], 2 },
182   { '{', &brace[0], 0 },
183   { '}', &brace[0], 2 },
184   { '[', &brack[0], 0 },
185   { ']', &brack[0], 2 },
186   { '|', &vert[0], 0 },
187   { '/', &slash[0], 0 },
188   { LM_Vert, &Vert[0], 0 },
189   { LM_backslash, &slash[0], 1 },
190   { LM_langle, &angle[0], 0 },
191   { LM_lceil, &corner[0], 0 }, 
192   { LM_lfloor, &corner[0], 1 },  
193   { LM_rangle, &angle[0], 2 }, 
194   { LM_rceil, &corner[0], 3 }, 
195   { LM_rfloor, &corner[0], 2 },
196   { LM_downarrow, &arrow[0], 2 },
197   { LM_Downarrow, &Arrow[0], 2 }, 
198   { LM_uparrow, &arrow[0], 0 },
199   { LM_Uparrow, &Arrow[0], 0 },
200   { LM_updownarrow, &udarrow[0], 0 },
201   { LM_Updownarrow, &Udarrow[0], 0 },    
203   // Accents   
204   { LM_ddot, &hline2[0], 0 },
205   { LM_hat, &angle[0], 3 },
206   { LM_grave, &slash[0], 1 },
207   { LM_acute, &slash[0], 0 },
208   { LM_tilde, &tilde[0], 0 },
209   { LM_bar, &hline[0], 0 },
210   { LM_dot, &hlinesmall[0], 0 },
211   { LM_check, &angle[0], 1 },
212   { LM_breve, &parenth[0], 1 },
213   { LM_vec, &arrow[0], 3 },
214   { LM_not, &slash[0], 0 },  
216   // Dots
217   { LM_ldots, &hline3[0], 0 }, 
218   { LM_cdots, &hline3[0], 0 },
219   { LM_vdots, &hline3[0], 1 },
220   { LM_ddots, &dline3[0], 0 },
221      
222   { 0, 0, 0 }
226 inline int odd(int x) { return ((x) & 1); }
228 typedef float matriz_data[2][2];
230 const matriz_data MATIDEN={ {1,0}, {0,1}};
232 extern void mathed_set_font(short type, int style);
233 extern int mathed_char_width(short type, int style, byte c);
234 extern int mathed_char_height(short, int, byte, int&, int&);
236 #define mateq(m1,m2)  memcpy(m1,m2,sizeof(matriz_data))
238 class Matriz {
239  public: 
240    Matriz() { mateq(m,MATIDEN); }
241    void rota(int);
242    void escala(float, float);
243    void transf(float, float, float&, float&);
244    
245  protected:
246    matriz_data m;
247    void matmat(matriz_data& a);
251 void Matriz::rota(int code)
253    float cs, sn;
254    
255    matriz_data r;
256    mateq(r,MATIDEN);
257    cs = (odd(code)) ? 0: 1 - code;
258    sn = (odd(code)) ? 2 - code: 0;
259    r[0][0] = cs;         r[0][1] = sn;
260    r[1][0] = -r[0][1];   r[1][1] = r[0][0];
261    matmat(r);
264 void Matriz::escala(float x, float y)
266    matriz_data s;
267    mateq(s,MATIDEN);
268    s[0][0] = x;  s[1][1] = y;
269    matmat(s);
273 void Matriz::matmat(matriz_data& a)
275    int i;
276    matriz_data c;   
277    for (i=0;i<2; i++) {
278       c[0][i] = a[0][0]*m[0][i] + a[0][1]*m[1][i];
279       c[1][i] = a[1][0]*m[0][i] + a[1][1]*m[1][i];
280    }
281    mateq(m, c);
284 void Matriz::transf(float xp, float yp, float &x, float &y)
286    x = m[0][0]*xp + m[0][1]*yp;
287    y = m[1][0]*xp + m[1][1]*yp;
290 extern GC latexGC, mathGC, mathLineGC, cursorGC;
292 static int search_deco(int code)
294    int i=0;
295    
296    while (math_deco_table[i].code &&  math_deco_table[i].code!=code) i++;
297    if (!math_deco_table[i].code) i = -1;
298    return i;
300       
301 void mathed_draw_deco(Window win, int x, int y, int w, int h, int code)
303    Matriz mt, sqmt;
304    XPoint p[32];
305    float *d, xx, yy, x2, y2;
306    int i=0,j, n, r;
307    
308    j = search_deco(code);   
309    if (j<0) return;
310    
311    r = math_deco_table[j].angle;
312    d = math_deco_table[j].data;
313    
314    if (h > 70 && (math_deco_table[j].code == (int) '(' || math_deco_table[j].code == (int) ')'))
315       d = parenthHigh;
316     
317    mt.rota(r);
318    mt.escala(w, h);
319    
320    n = (w < h) ? w: h;
321    sqmt.rota(r);
322    sqmt.escala(n, n);
323    if (r> 0 && r< 3) y += h;   
324    if (r>=2) x += w;   
325    do {
326       code = (int)d[i++];
327       switch (code) {
328        case 0: break;
329        case 1: 
330        case 3:
331        {
332           xx = d[i++]; yy = d[i++];
333           x2 = d[i++]; y2 = d[i++];
334           if (code==3) 
335             sqmt.transf(xx, yy, xx, yy);
336           else
337             mt.transf(xx, yy, xx, yy);
338           mt.transf(x2, y2, x2, y2);
339           XDrawLine(fl_display, win, mathGC, x+(int)xx, y+(int)yy,
340                     x+(int)x2, y+(int)y2);
341           XFlush(fl_display);
342           break;
343        }         
344        case 2: 
345        case 4:
346        {
347           n = (int)d[i++];
348           for (j=0; j<n; j++) {
349              xx = d[i++]; yy = d[i++];
350 //           lyxerr << " " << xx << " " << yy << " ";
351              if (code==4) 
352                sqmt.transf(xx, yy, xx, yy);
353              else
354                mt.transf(xx, yy, xx, yy);
355              p[j].x = x+(int)xx;
356              p[j].y = y+(int)yy;
357              //  lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
358           }
359           XDrawLines(fl_display, win, mathLineGC, p, n, CoordModeOrigin);
360           XFlush(fl_display);
361        }
362       }
363    } while (code);
366 void
367 MathDelimInset::Draw(int x, int y)
369    xo = x;  yo = y; 
370    MathParInset::Draw(x+dw+2, y-dh); 
371    //int h=Height(), hg=descent-1;  
373    if (left=='.') {
374      XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
375      XFlush(fl_display);
376    }
377    else
378      mathed_draw_deco(pm, x, y-ascent, dw, Height(), left);
379    x += Width()-dw-2;
380    if (right=='.') {
381      XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
382      XFlush(fl_display);
383    }
384    else
385      mathed_draw_deco(pm, x, y-ascent, dw, Height(), right);
388 void
389 MathDelimInset::Metrics()
391    MathParInset::Metrics();
392    int d;
393    
394    mathed_char_height(LM_TC_CONST, size, 'I', d, dh);
395    dh /= 2;
396    ascent += 2 + dh;
397    descent += 2 - dh;
398    dw = Height()/5;
399    if (dw > 15) dw = 15;
400    if (dw<6) dw = 6;
401    width += 2*dw+4;
405 void
406 MathDecorationInset::Draw(int x, int y)
408    MathParInset::Draw(x+(width-dw)/2, y);
409    mathed_draw_deco(pm, x, y+dy, width, dh, deco);
413 void
414 MathDecorationInset::Metrics()
416    int h = 2*mathed_char_height(LM_TC_VAR, size, 'I', ascent, descent);  
417    MathParInset::Metrics();
418    int w = Width()+4;
419    if (w<16) w = 16;
420    dh = w/5;
421    if (dh>h) dh = h;
423    if (upper) {
424       ascent += dh+2;
425       dy = -ascent;
426    } else {
427       dy = descent+2;
428       descent += dh+4;
429    }
430    dw = width;
431    width = w;
434 void
435 MathAccentInset::Draw(int x, int y)
437     int dw = width-2;
438 /*    char s[8];
439     mathed_set_font(fn, size);
440     if (MathIsBinary(fn)) {
441         s[0] = s[2] = ' '; 
442         s[1] = (char)c;
443         ns = 3;
444         dw = mathed_char_width(fn, size, c);
445     } else
446       s[0] = (char)c;
448     if (inset) {
449         inset->Draw(x, y);
450     } else {
451         drawStr(fn, size, x, y, &c, 1);
452         XFlush(fl_display);
453     }
454     x += (code==LM_not) ? (width-dw)/2: 2;
455     mathed_draw_deco(pm, x, y-dy, dw, dh, code);
458 void
459 MathAccentInset::Metrics()
461     
462     if (inset) {
463         inset->Metrics();
464         ascent = inset->Ascent();
465         descent = inset->Descent();
466         width = inset->Width();
467         dh = ascent;
468     } else {
469         mathed_char_height(fn, size, c, ascent, descent);
470         width = mathed_char_width(fn, size, c);
471         dh = (width-2)/2; 
472     }
473     if (code==LM_not) {
474         ascent += dh;
475         descent += dh;
476         dh = Height();
477     } else 
478       ascent += dh+2;
479             
480     dy = ascent;
481 //    if (MathIsBinary(fn))
482 //      width += 2*mathed_char_width(fn, size, ' ');    
486 void
487 MathDotsInset::Draw(int x, int y)
489    mathed_draw_deco(pm, x+2, y-dh, width-2, ascent, code);
490    if (code==LM_vdots||code==LM_ddots) x++; 
491    if (code!=LM_vdots) y--;
492    mathed_draw_deco(pm, x+2, y-dh, width-2, ascent, code);
493 }     
495 void
496 MathDotsInset::Metrics()
498    mathed_char_height(LM_TC_VAR, size, 'M', ascent, descent);
499    width = mathed_char_width(LM_TC_VAR, size, 'M');   
500    switch (code) {
501     case LM_ldots: dh = 0; break;
502     case LM_cdots: dh = ascent/2; break;
503     case LM_vdots: width /=2;
504     case LM_ddots: dh = ascent; break;
505    }