]> git.lyx.org Git - lyx.git/blob - src/mathed/math_delim.C
remove the old painter, remove support for mono_video, reverse_video, fast selection...
[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 int mathed_char_width(short type, int style, byte c);
235 extern int mathed_char_height(short, int, byte, int&, int&);
236
237 #define mateq(m1, m2)  memcpy(m1, m2, sizeof(matriz_data))
238
239 class Matriz {
240  public: 
241    Matriz() { mateq(m, MATIDEN); }
242    void rota(int);
243    void escala(float, float);
244    void transf(float, float, float&, float&);
245    
246  protected:
247    matriz_data m;
248    void matmat(matriz_data& a);
249 };
250
251
252 void Matriz::rota(int code)
253 {
254    float cs, sn;
255    
256    matriz_data r;
257    mateq(r, MATIDEN);
258    cs = (odd(code)) ? 0: 1 - code;
259    sn = (odd(code)) ? 2 - code: 0;
260    r[0][0] = cs;         r[0][1] = sn;
261    r[1][0] = -r[0][1];   r[1][1] = r[0][0];
262    matmat(r);
263 }
264
265 void Matriz::escala(float x, float y)
266 {
267    matriz_data s;
268    mateq(s, MATIDEN);
269    s[0][0] = x;  s[1][1] = y;
270    matmat(s);
271 }
272
273
274 void Matriz::matmat(matriz_data& a)
275 {
276    matriz_data c;   
277    for (int 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);
282 }
283
284 void Matriz::transf(float xp, float yp, float &x, float &y)
285 {
286    x = m[0][0]*xp + m[0][1]*yp;
287    y = m[1][0]*xp + m[1][1]*yp;
288 }
289
290
291 static
292 int search_deco(int code)
293 {
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;
299 }
300
301
302 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
303 {
304         Matriz mt, sqmt;
305         float xx, yy, x2, y2;
306         int i = 0;
307    
308         int j = search_deco(code);   
309         if (j < 0) return;
310    
311         int r = math_deco_table[j].angle;
312         float * 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         int 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                         pain.line(x + int(xx), y + int(yy),
340                                   x + int(x2), y + int(y2),
341                                   LColor::mathline);
342                         break;
343                 }        
344                 case 2: 
345                 case 4:
346                 {
347                         int xp[32], yp[32];
348                         n = int(d[i++]);
349                         for (j = 0; j < n; ++j) {
350                                 xx = d[i++]; yy = d[i++];
351 //           lyxerr << " " << xx << " " << yy << " ";
352                                 if (code == 4) 
353                                         sqmt.transf(xx, yy, xx, yy);
354                                 else
355                                         mt.transf(xx, yy, xx, yy);
356                                 xp[j] = x + int(xx);
357                                 yp[j] = y + int(yy);
358                                 //  lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
359                         }
360                         pain.lines(xp, yp, n, LColor::mathline);
361                 }
362                 }
363         } while (code);
364 }
365
366
367 void
368 MathDelimInset::draw(Painter & pain, int x, int y)
369
370         xo = x;  yo = y; 
371         MathParInset::draw(pain, x + dw + 2, y - dh); 
372         
373         if (left == '.') {
374                 pain.line(x + 4, yo - ascent,
375                           x + 4, yo + descent,
376                           LColor::mathcursor);
377         } else
378                 mathed_draw_deco(pain, x, y - ascent, dw, Height(), left);
379         x += Width()-dw-2;
380         if (right == '.') {
381                 pain.line(x + 4, yo - ascent,
382                           x + 4, yo + descent,
383                           LColor::mathcursor);
384         } else
385                 mathed_draw_deco(pain, x, y-ascent, dw, Height(), right);
386 }
387
388
389 void
390 MathDelimInset::Metrics()
391 {
392    MathParInset::Metrics();
393    int d;
394    
395    mathed_char_height(LM_TC_CONST, size, 'I', d, dh);
396    dh /= 2;
397    ascent += 2 + dh;
398    descent += 2 - dh;
399    dw = Height()/5;
400    if (dw > 15) dw = 15;
401    if (dw<6) dw = 6;
402    width += 2*dw+4;
403 }
404
405
406 void
407 MathDecorationInset::draw(Painter & pain, int x, int y)
408
409    MathParInset::draw(pain, x + (width - dw) / 2, y);
410    mathed_draw_deco(pain, x, y + dy, width, dh, deco);
411 }
412
413
414 void
415 MathDecorationInset::Metrics()
416 {
417    int h = 2*mathed_char_height(LM_TC_VAR, size, 'I', ascent, descent);  
418    MathParInset::Metrics();
419    int w = Width()+4;
420    if (w<16) w = 16;
421    dh = w/5;
422    if (dh>h) dh = h;
423
424    if (upper) {
425       ascent += dh+2;
426       dy = -ascent;
427    } else {
428       dy = descent+2;
429       descent += dh+4;
430    }
431    dw = width;
432    width = w;
433 }
434
435
436 void
437 MathAccentInset::draw(Painter & pain, int x, int y)
438 {
439     int dw = width - 2;
440
441     if (inset) {
442         inset->draw(pain, x, y);
443     } else {
444         drawStr(pain, fn, size, x, y, &c, 1);
445     }
446     x += (code == LM_not) ? (width-dw) / 2 : 2;
447     mathed_draw_deco(pain, x, y - dy, dw, dh, code);
448 }
449
450
451 void
452 MathAccentInset::Metrics()
453 {
454     
455     if (inset) {
456         inset->Metrics();
457         ascent = inset->Ascent();
458         descent = inset->Descent();
459         width = inset->Width();
460         dh = ascent;
461     } else {
462         mathed_char_height(fn, size, c, ascent, descent);
463         width = mathed_char_width(fn, size, c);
464         dh = (width-2)/2; 
465     }
466     if (code == LM_not) {
467         ascent += dh;
468         descent += dh;
469         dh = Height();
470     } else 
471       ascent += dh+2;
472             
473     dy = ascent;
474 //    if (MathIsBinary(fn))
475 //      width += 2*mathed_char_width(fn, size, ' ');    
476 }
477
478
479 void
480 MathDotsInset::draw(Painter & pain, int x, int y)
481 {
482    mathed_draw_deco(pain, x + 2, y - dh, width - 2, ascent, code);
483    if (code == LM_vdots || code == LM_ddots) ++x; 
484    if (code != LM_vdots) --y;
485    mathed_draw_deco(pain, x + 2, y - dh, width - 2, ascent, code);
486 }
487
488
489 void
490 MathDotsInset::Metrics()
491 {
492    mathed_char_height(LM_TC_VAR, size, 'M', ascent, descent);
493    width = mathed_char_width(LM_TC_VAR, size, 'M');   
494    switch (code) {
495     case LM_ldots: dh = 0; break;
496     case LM_cdots: dh = ascent/2; break;
497     case LM_vdots: width /= 2;
498     case LM_ddots: dh = ascent; break;
499    }
500
501