]> git.lyx.org Git - features.git/blob - src/mathed/math_delim.C
apply the ostream changes to mathed, some other small related things
[features.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
229 int odd(int x) { return ((x) & 1); }
230
231 typedef float matriz_data[2][2];
232
233 const matriz_data MATIDEN= { {1, 0}, {0, 1}};
234
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
292 static
293 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 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
304 {
305         Matriz mt, sqmt;
306         float xx, yy, x2, y2;
307         int i = 0;
308    
309         int j = search_deco(code);   
310         if (j < 0) return;
311    
312         int r = math_deco_table[j].angle;
313         float * d = math_deco_table[j].data;
314         
315         if (h > 70 && (math_deco_table[j].code == int('(')
316                        || 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
368
369 void
370 MathDelimInset::draw(Painter & pain, int x, int y)
371
372         xo = x;  yo = y; 
373         MathParInset::draw(pain, x + dw + 2, y - dh); 
374         
375         if (left == '.') {
376                 pain.line(x + 4, yo - ascent,
377                           x + 4, yo + descent,
378                           LColor::mathcursor);
379         } else
380                 mathed_draw_deco(pain, x, y - ascent, dw, Height(), left);
381         x += Width()-dw-2;
382         if (right == '.') {
383                 pain.line(x + 4, yo - ascent,
384                           x + 4, yo + descent,
385                           LColor::mathcursor);
386         } else
387                 mathed_draw_deco(pain, x, y-ascent, dw, Height(), right);
388 }
389
390
391 void
392 MathDelimInset::Metrics()
393 {
394    MathParInset::Metrics();
395    int d;
396    
397    mathed_char_height(LM_TC_CONST, size, 'I', d, dh);
398    dh /= 2;
399    ascent += 2 + dh;
400    descent += 2 - dh;
401    dw = Height()/5;
402    if (dw > 15) dw = 15;
403    if (dw<6) dw = 6;
404    width += 2*dw+4;
405 }
406
407
408 void
409 MathDecorationInset::draw(Painter & pain, int x, int y)
410
411    MathParInset::draw(pain, x + (width - dw) / 2, y);
412    mathed_draw_deco(pain, x, y + dy, width, dh, deco);
413 }
414
415
416 void
417 MathDecorationInset::Metrics()
418 {
419    int h = 2*mathed_char_height(LM_TC_VAR, size, 'I', ascent, descent);  
420    MathParInset::Metrics();
421    int w = Width()+4;
422    if (w<16) w = 16;
423    dh = w/5;
424    if (dh>h) dh = h;
425
426    if (upper) {
427       ascent += dh+2;
428       dy = -ascent;
429    } else {
430       dy = descent+2;
431       descent += dh+4;
432    }
433    dw = width;
434    width = w;
435 }
436
437
438 void
439 MathAccentInset::draw(Painter & pain, int x, int y)
440 {
441     int dw = width - 2;
442
443     if (inset) {
444         inset->draw(pain, x, y);
445     } else {
446         drawStr(pain, fn, size, x, y, &c, 1);
447     }
448     x += (code == LM_not) ? (width-dw) / 2 : 2;
449     mathed_draw_deco(pain, x, y - dy, dw, dh, code);
450 }
451
452
453 void
454 MathAccentInset::Metrics()
455 {
456     
457     if (inset) {
458         inset->Metrics();
459         ascent = inset->Ascent();
460         descent = inset->Descent();
461         width = inset->Width();
462         dh = ascent;
463     } else {
464         mathed_char_height(fn, size, c, ascent, descent);
465         width = mathed_char_width(fn, size, c);
466         dh = (width-2)/2; 
467     }
468     if (code == LM_not) {
469         ascent += dh;
470         descent += dh;
471         dh = Height();
472     } else 
473       ascent += dh+2;
474             
475     dy = ascent;
476 //    if (MathIsBinary(fn))
477 //      width += 2*mathed_char_width(fn, size, ' ');    
478 }
479
480
481 void
482 MathDotsInset::draw(Painter & pain, int x, int y)
483 {
484    mathed_draw_deco(pain, x + 2, y - dh, width - 2, ascent, code);
485    if (code == LM_vdots || code == LM_ddots) ++x; 
486    if (code != LM_vdots) --y;
487    mathed_draw_deco(pain, x + 2, y - dh, width - 2, ascent, code);
488 }
489
490
491 void
492 MathDotsInset::Metrics()
493 {
494    mathed_char_height(LM_TC_VAR, size, 'M', ascent, descent);
495    width = mathed_char_width(LM_TC_VAR, size, 'M');   
496    switch (code) {
497     case LM_ldots: dh = 0; break;
498     case LM_cdots: dh = ascent/2; break;
499     case LM_vdots: width /= 2;
500     case LM_ddots: dh = ascent; break;
501    }
502
503