]> git.lyx.org Git - lyx.git/blob - src/mathed/math_delim.C
pos=string::npos for regex not found, use handcoded transform in lstring.C, fix the...
[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
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  */
29
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   
37 };
38
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 
44 };
45
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
55 };
56
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 
63 };
64
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
72 };
73
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 
81 };
82
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 
91 };
92
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
97 };
98
99 static float corner[] = {
100    2.0, 3,
101    0.95, 0.05,  0.05, 0.05,  0.05, 0.95,
102    0.0
103 };
104
105 static float angle[] = {
106    2.0, 3,
107    1, 0,  0.05, 0.5,  1, 1,
108    0.0
109 };
110
111 static float slash[] = {
112    1, 0.95, 0.05,  0.05, 0.95, 
113    0.0
114 };
115
116 static float hline[] = {
117    1, 0.05, 0.5,  0.95, 0.5, 
118    0.0
119 };
120
121
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 }; 
127
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
133 };
134
135
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 };     
142
143 static float hlinesmall[] = {
144    1, 0.4, 0.5,  0.6, 0.5, 
145    0.0
146 };
147
148 static float vert[] = {
149    1, 0.5, 0.05,  0.5, 0.95, 
150    0.0
151 };
152
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
157 };
158
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
163 };
164
165 static struct math_deco_struct {
166     int code; float *data; int angle;
167 } math_deco_table[] = {   
168
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 },    
202
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 },  
215
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 }
223 };
224
225
226 inline int odd(int x) { return ((x) & 1); }
227
228 typedef float matriz_data[2][2];
229
230 const matriz_data MATIDEN= { {1, 0}, {0, 1}};
231
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&);
235
236 #define mateq(m1, m2)  memcpy(m1, m2, sizeof(matriz_data))
237
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);
248 };
249
250
251 void Matriz::rota(int code)
252 {
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);
262 }
263
264 void Matriz::escala(float x, float y)
265 {
266    matriz_data s;
267    mateq(s, MATIDEN);
268    s[0][0] = x;  s[1][1] = y;
269    matmat(s);
270 }
271
272
273 void Matriz::matmat(matriz_data& a)
274 {
275    matriz_data c;   
276    for (int i = 0;i < 2; ++i) {
277       c[0][i] = a[0][0]*m[0][i] + a[0][1]*m[1][i];
278       c[1][i] = a[1][0]*m[0][i] + a[1][1]*m[1][i];
279    }
280    mateq(m, c);
281 }
282
283 void Matriz::transf(float xp, float yp, float &x, float &y)
284 {
285    x = m[0][0]*xp + m[0][1]*yp;
286    y = m[1][0]*xp + m[1][1]*yp;
287 }
288
289 extern GC latexGC, mathGC, mathLineGC, cursorGC;
290
291 static int search_deco(int code)
292 {
293    int i= 0;
294    
295    while (math_deco_table[i].code &&  math_deco_table[i].code!= code) ++i;
296    if (!math_deco_table[i].code) i = -1;
297    return i;
298 }
299       
300 void mathed_draw_deco(Window win, int x, int y, int w, int h, int code)
301 {
302    Matriz mt, sqmt;
303    XPoint p[32];
304    float xx, yy, x2, y2;
305    int i= 0, j, n;
306    
307    j = search_deco(code);   
308    if (j < 0) return;
309    
310    int r = math_deco_table[j].angle;
311    float * d = math_deco_table[j].data;
312    
313    if (h > 70 && (math_deco_table[j].code == int('(') || math_deco_table[j].code == int(')')))
314       d = parenthHigh;
315     
316    mt.rota(r);
317    mt.escala(w, h);
318    
319    n = (w < h) ? w: h;
320    sqmt.rota(r);
321    sqmt.escala(n, n);
322    if (r> 0 && r< 3) y += h;   
323    if (r>= 2) x += w;   
324    do {
325       code = int(d[i++]);
326       switch (code) {
327        case 0: break;
328        case 1: 
329        case 3:
330        {
331           xx = d[i++]; yy = d[i++];
332           x2 = d[i++]; y2 = d[i++];
333           if (code == 3) 
334             sqmt.transf(xx, yy, xx, yy);
335           else
336             mt.transf(xx, yy, xx, yy);
337           mt.transf(x2, y2, x2, y2);
338           XDrawLine(fl_display, win, mathGC, x + int(xx), y + int(yy),
339                     x + int(x2), y + int(y2));
340           XFlush(fl_display);
341           break;
342        }         
343        case 2: 
344        case 4:
345        {
346           n = int(d[i++]);
347           for (j = 0; j < n; ++j) {
348              xx = d[i++]; yy = d[i++];
349 //           lyxerr << " " << xx << " " << yy << " ";
350              if (code == 4) 
351                sqmt.transf(xx, yy, xx, yy);
352              else
353                mt.transf(xx, yy, xx, yy);
354              p[j].x = x + int(xx);
355              p[j].y = y + int(yy);
356              //  lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
357           }
358           XDrawLines(fl_display, win, mathLineGC, p, n, CoordModeOrigin);
359           XFlush(fl_display);
360        }
361       }
362    } while (code);
363 }
364
365 void
366 MathDelimInset::Draw(int x, int y)
367
368    xo = x;  yo = y; 
369    MathParInset::Draw(x+dw+2, y-dh); 
370    //int h= Height(), hg= descent-1;  
371
372    if (left == '.') {
373      XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
374      XFlush(fl_display);
375    }
376    else
377      mathed_draw_deco(pm, x, y-ascent, dw, Height(), left);
378    x += Width()-dw-2;
379    if (right == '.') {
380      XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
381      XFlush(fl_display);
382    }
383    else
384      mathed_draw_deco(pm, x, y-ascent, dw, Height(), right);
385 }
386
387 void
388 MathDelimInset::Metrics()
389 {
390    MathParInset::Metrics();
391    int d;
392    
393    mathed_char_height(LM_TC_CONST, size, 'I', d, dh);
394    dh /= 2;
395    ascent += 2 + dh;
396    descent += 2 - dh;
397    dw = Height()/5;
398    if (dw > 15) dw = 15;
399    if (dw<6) dw = 6;
400    width += 2*dw+4;
401 }
402
403
404 void
405 MathDecorationInset::Draw(int x, int y)
406
407    MathParInset::Draw(x+(width-dw)/2, y);
408    mathed_draw_deco(pm, x, y+dy, width, dh, deco);
409 }
410
411
412 void
413 MathDecorationInset::Metrics()
414 {
415    int h = 2*mathed_char_height(LM_TC_VAR, size, 'I', ascent, descent);  
416    MathParInset::Metrics();
417    int w = Width()+4;
418    if (w<16) w = 16;
419    dh = w/5;
420    if (dh>h) dh = h;
421
422    if (upper) {
423       ascent += dh+2;
424       dy = -ascent;
425    } else {
426       dy = descent+2;
427       descent += dh+4;
428    }
429    dw = width;
430    width = w;
431 }
432
433 void
434 MathAccentInset::Draw(int x, int y)
435 {
436     int dw = width-2;
437 /*    char s[8];
438     mathed_set_font(fn, size);
439     if (MathIsBinary(fn)) {
440         s[0] = s[2] = ' '; 
441         s[1] = (char)c;
442         ns = 3;
443         dw = mathed_char_width(fn, size, c);
444     } else
445       s[0] = (char)c;
446 */
447     if (inset) {
448         inset->Draw(x, y);
449     } else {
450         drawStr(fn, size, x, y, &c, 1);
451         XFlush(fl_display);
452     }
453     x += (code == LM_not) ? (width-dw)/2: 2;
454     mathed_draw_deco(pm, x, y-dy, dw, dh, code);
455 }
456
457 void
458 MathAccentInset::Metrics()
459 {
460     
461     if (inset) {
462         inset->Metrics();
463         ascent = inset->Ascent();
464         descent = inset->Descent();
465         width = inset->Width();
466         dh = ascent;
467     } else {
468         mathed_char_height(fn, size, c, ascent, descent);
469         width = mathed_char_width(fn, size, c);
470         dh = (width-2)/2; 
471     }
472     if (code == LM_not) {
473         ascent += dh;
474         descent += dh;
475         dh = Height();
476     } else 
477       ascent += dh+2;
478             
479     dy = ascent;
480 //    if (MathIsBinary(fn))
481 //      width += 2*mathed_char_width(fn, size, ' ');    
482 }
483
484
485 void
486 MathDotsInset::Draw(int x, int y)
487 {
488    mathed_draw_deco(pm, x + 2, y - dh, width - 2, ascent, code);
489    if (code == LM_vdots || code == LM_ddots) ++x; 
490    if (code!= LM_vdots) --y;
491    mathed_draw_deco(pm, x + 2, y - dh, width - 2, ascent, code);
492 }     
493
494 void
495 MathDotsInset::Metrics()
496 {
497    mathed_char_height(LM_TC_VAR, size, 'M', ascent, descent);
498    width = mathed_char_width(LM_TC_VAR, size, 'M');   
499    switch (code) {
500     case LM_ldots: dh = 0; break;
501     case LM_cdots: dh = ascent/2; break;
502     case LM_vdots: width /= 2;
503     case LM_ddots: dh = ascent; break;
504    }
505
506