]> git.lyx.org Git - lyx.git/blob - src/mathed/support.C
first go at mathed file cleanup
[lyx.git] / src / mathed / support.C
1 #include <config.h>
2
3 #include <algorithm>
4
5 #include "mathed/support.h"
6 #include "lyxfont.h"
7 #include "font.h"
8 #include "math_defs.h"
9 #include "Painter.h"
10 #include "matriz.h"
11 #include "symbol_def.h"
12
13 extern LyXFont WhichFont(short type, int size);
14
15 char const * math_font_name[] = {
16    "mathrm",
17    "mathcal",
18    "mathbf",
19    "mathsf",
20    "mathtt",
21    "mathit",
22    "textrm"
23 };
24
25
26 char const * latex_mathspace[] = {
27    "!", ",", ":", ";", "quad", "qquad"
28 };
29
30 /* 
31  * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
32  * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4= square polyline
33  */
34
35
36 static
37 float parenthHigh[] = {
38     2.0, 13.0, 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772, 0.2540, 
39     0.1278, 0.1746, 0.1966, 0.0952, 0.3300, 0.0950, 0.5000, 0.0952, 0.6700, 
40     0.1746, 0.8034, 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677, 0.9840, 
41     0.9986, 0.0 
42 };
43
44
45 static float parenth[] = {
46   2.0, 13.0,
47   0.9930, 0.0071,  0.7324, 0.0578,  0.5141, 0.1126,  0.3380, 0.1714,
48   0.2183, 0.2333,  0.0634, 0.3621,  0.0141, 0.5000,  0.0563, 0.6369,
49   0.2113, 0.7647,  0.3310, 0.8276,  0.5070, 0.8864,  0.7254, 0.9412,
50   0.9930, 0.9919,
51   0.0   
52 };
53
54
55 static float brace[] = {
56   2.0, 21.0,
57   0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243, 0.5819, 0.0527,
58   0.4859, 0.0892, 0.4463, 0.1278, 0.4463, 0.3732, 0.4011, 0.4199,
59   0.2712, 0.4615, 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
60   0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268, 0.4463, 0.8722,
61   0.4859, 0.9108, 0.5819, 0.9473, 0.7458, 0.9757, 0.9379, 0.9980,
62   0.9492, 0.9980,
63   0.0
64 };
65
66 static float arrow[] = {
67    4, 7,
68    0.015, 0.7500,  0.2, 0.6,  0.35, 0.35,  0.5, 0.05,
69    0.65, 0.35,  0.8, 0.6,  0.95, 0.7500,
70    3, 0.5, 0.15,  0.5, 0.95,
71    0.0 
72 };
73
74 static float Arrow[] = {
75    4, 7,
76    0.015, 0.7500,  0.2, 0.6,  0.35, 0.35,  0.5, 0.05,
77    0.65, 0.35,  0.8, 0.6,  0.95, 0.7500,
78    3, 0.35, 0.5, 0.35, 0.95,
79    3, 0.65, 0.5, 0.65, 0.95,
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.5, 0.2,  0.5, 0.8,
89    0.0 
90 };
91
92 static float Udarrow[] = {
93    2, 3,
94    0.015, 0.25,  0.5, 0.05, 0.95, 0.25,
95    2, 3,
96    0.015, 0.75,  0.5, 0.95, 0.95, 0.75,  
97    1, 0.35, 0.2, 0.35, 0.8,
98    1, 0.65, 0.2, 0.65, 0.8,
99    0.0 
100 };
101
102 static float brack[] = {
103    2.0, 4,
104    0.95, 0.05,  0.05, 0.05,  0.05, 0.95,  0.95, 0.95,
105    0.0
106 };
107
108 static float corner[] = {
109    2.0, 3,
110    0.95, 0.05,  0.05, 0.05,  0.05, 0.95,
111    0.0
112 };
113
114 static float angle[] = {
115    2.0, 3,
116    1, 0,  0.05, 0.5,  1, 1,
117    0.0
118 };
119
120 static float slash[] = {
121    1, 0.95, 0.05,  0.05, 0.95, 
122    0.0
123 };
124
125 static float hline[] = {
126    1, 0.05, 0.5,  0.95, 0.5, 
127    0.0
128 };
129
130
131 static float hline2[] = {
132    1, 0.1, 0.5,  0.3, 0.5,
133    1, 0.7, 0.5,  0.9, 0.5,
134    0.0
135 }; 
136
137 static float hline3[] = {
138    1, 0.1, 0,  0.15, 0,
139    1, 0.475, 0,  0.525, 0,
140    1, 0.85, 0,  0.9, 0,  
141    0.0
142 };
143
144
145 static float dline3[] = {
146    1, 0.1, 0.1,  0.15, 0.15,
147    1, 0.475, 0.475,  0.525, 0.525,
148    1, 0.85, 0.85,  0.9, 0.9,
149    0.0
150 };     
151
152 static float hlinesmall[] = {
153    1, 0.4, 0.5,  0.6, 0.5, 
154    0.0
155 };
156
157 static float vert[] = {
158    1, 0.5, 0.05,  0.5, 0.95, 
159    0.0
160 };
161
162 static float Vert[] = {
163    1, 0.3, 0.05,  0.3, 0.95, 
164    1, 0.7, 0.05,  0.7, 0.95,
165    0.0
166 };
167
168 static float tilde[] = {
169    2.0, 4,
170    0.05, 0.8,  0.25, 0.2,  0.75, 0.8,  0.95, 0.2,
171    0.0
172 };
173
174
175 static
176 math_deco_struct math_deco_table[] = {   
177
178    // Decorations
179   { LM_widehat, &angle[0], 3 },
180   { LM_widetilde, &tilde[0], 0 },
181   { LM_underline, &hline[0], 0 },
182   { LM_overline, &hline[0], 0 },
183   { LM_underbrace, &brace[0], 1 },
184   { LM_overbrace,  &brace[0], 3 },
185   { LM_overleftarrow, &arrow[0], 1 },
186   { LM_overightarrow, &arrow[0], 3 },
187      
188   // Delimiters
189   { '(', &parenth[0], 0 },
190   { ')', &parenth[0], 2 },
191   { '{', &brace[0], 0 },
192   { '}', &brace[0], 2 },
193   { '[', &brack[0], 0 },
194   { ']', &brack[0], 2 },
195   { '|', &vert[0], 0 },
196   { '/', &slash[0], 0 },
197   { LM_Vert, &Vert[0], 0 },
198   { LM_backslash, &slash[0], 1 },
199   { LM_langle, &angle[0], 0 },
200   { LM_lceil, &corner[0], 0 }, 
201   { LM_lfloor, &corner[0], 1 },  
202   { LM_rangle, &angle[0], 2 }, 
203   { LM_rceil, &corner[0], 3 }, 
204   { LM_rfloor, &corner[0], 2 },
205   { LM_downarrow, &arrow[0], 2 },
206   { LM_Downarrow, &Arrow[0], 2 }, 
207   { LM_uparrow, &arrow[0], 0 },
208   { LM_Uparrow, &Arrow[0], 0 },
209   { LM_updownarrow, &udarrow[0], 0 },
210   { LM_Updownarrow, &Udarrow[0], 0 },    
211
212   // Accents   
213   { LM_ddot, &hline2[0], 0 },
214   { LM_hat, &angle[0], 3 },
215   { LM_grave, &slash[0], 1 },
216   { LM_acute, &slash[0], 0 },
217   { LM_tilde, &tilde[0], 0 },
218   { LM_bar, &hline[0], 0 },
219   { LM_dot, &hlinesmall[0], 0 },
220   { LM_check, &angle[0], 1 },
221   { LM_breve, &parenth[0], 1 },
222   { LM_vec, &arrow[0], 3 },
223   { LM_not, &slash[0], 0 },  
224
225   // Dots
226   { LM_ldots, &hline3[0], 0 }, 
227   { LM_cdots, &hline3[0], 0 },
228   { LM_vdots, &hline3[0], 1 },
229   { LM_ddots, &dline3[0], 0 }
230 };
231
232
233 struct math_deco_compare {
234         /// for use by sort and lower_bound
235         inline
236         int operator()(math_deco_struct const & a,
237                        math_deco_struct const & b) const {
238                 return a.code < b.code;
239         }
240 };
241
242
243 static
244 int const math_deco_table_size = sizeof(math_deco_table) /sizeof(math_deco_struct);
245
246 class init_deco_table {
247 public:
248         init_deco_table() {
249                 if (!init) {
250                         sort(math_deco_table,
251                              math_deco_table + math_deco_table_size,
252                              math_deco_compare());
253                         init_deco_table::init = true;
254                 }
255         }
256 private:
257         static bool init;
258 };
259
260 bool init_deco_table::init = false;
261 static init_deco_table idt;
262
263
264 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
265 {
266    LyXFont font = WhichFont(type, size);
267    des = lyxfont::descent(c, font);
268    asc = lyxfont::ascent(c, font);
269    return asc + des;
270 }
271
272
273 int mathed_char_width(short type, int size, byte c)
274 {
275         if (MathIsBinary(type)) {
276                 string s;
277                 s += c;
278                 return mathed_string_width(type, size, s);
279         }
280         else
281     return lyxfont::width(c, WhichFont(type, size));
282 }
283
284
285 int mathed_string_height(short type, int size, string const & s,
286                          int & asc, int & des)
287 {
288         LyXFont font = WhichFont(type, size);
289         asc = des = 0;
290         for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
291                 des = max(des, lyxfont::descent(*it, font));
292                 asc = max(asc, lyxfont::ascent(*it, font));
293         }
294         return asc + des;
295 }
296
297
298 int mathed_string_width(short type, int size, string const & s)
299 {
300         string st;
301         if (MathIsBinary(type))
302                 for (string::const_iterator it = s.begin();
303                      it != s.end(); ++it) {
304                         st += ' ';
305                         st += *it;
306                         st += ' ';
307                 }
308         else
309                 st = s;
310
311         LyXFont const f = WhichFont(type, size);
312         return lyxfont::width(st, f);
313 }
314
315
316 LyXFont mathed_get_font(short type, int size)
317 {
318         LyXFont f = WhichFont(type, size);
319         if (type == LM_TC_TEX) {
320                 f.setLatex(LyXFont::ON);
321         }
322         return f;
323 }
324
325
326 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
327 {
328         Matriz mt;
329         Matriz sqmt;
330         float xx;
331         float yy;
332         float x2;
333         float y2;
334         int i = 0;
335
336 #if USE_EXCEPTIONS
337         math_deco_struct mds;
338         try {
339                 mds = search_deco(code);
340         }
341         catch (deco_not_found) {
342                 // Should this ever happen?
343                 lyxerr << "Deco was not found. Programming error?" << endl;
344                 return;
345         }
346    
347         int r = mds.angle;
348         float * d = mds.data;
349         
350         if (h > 70 && (mds.code == int('(')
351                        || mds.code == int(')')))
352                 d = parenthHigh;
353 #else
354         math_deco_struct const * mds = search_deco(code);
355         if (!mds) {
356                 // Should this ever happen?
357                 lyxerr << "Deco was not found. Programming error?" << endl;
358                 return;
359         }
360         
361    
362         int r = mds->angle;
363         float * d = mds->data;
364         
365         if (h > 70 && (mds->code == int('(')
366                        || mds->code == int(')')))
367                 d = parenthHigh;
368 #endif
369         
370         mt.rota(r);
371         mt.escala(w, h);
372    
373         int n = (w < h) ? w: h;
374         sqmt.rota(r);
375         sqmt.escala(n, n);
376         if (r > 0 && r < 3) y += h;   
377         if (r >= 2) x += w;   
378         do {
379                 code = int(d[i++]);
380                 switch (code) {
381                 case 0: break;
382                 case 1: 
383                 case 3:
384                 {
385                         xx = d[i++]; yy = d[i++];
386                         x2 = d[i++]; y2 = d[i++];
387                         if (code == 3) 
388                                 sqmt.transf(xx, yy, xx, yy);
389                         else
390                                 mt.transf(xx, yy, xx, yy);
391                         mt.transf(x2, y2, x2, y2);
392                         pain.line(x + int(xx), y + int(yy),
393                                   x + int(x2), y + int(y2),
394                                   LColor::mathline);
395                         break;
396                 }        
397                 case 2: 
398                 case 4:
399                 {
400                         int xp[32], yp[32];
401                         n = int(d[i++]);
402                         for (int j = 0; j < n; ++j) {
403                                 xx = d[i++]; yy = d[i++];
404 //           lyxerr << " " << xx << " " << yy << " ";
405                                 if (code == 4) 
406                                         sqmt.transf(xx, yy, xx, yy);
407                                 else
408                                         mt.transf(xx, yy, xx, yy);
409                                 xp[j] = x + int(xx);
410                                 yp[j] = y + int(yy);
411                                 //  lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
412                         }
413                         pain.lines(xp, yp, n, LColor::mathline);
414                 }
415                 }
416         } while (code);
417 }
418
419
420 #define USE_EXCEPTIONS 0
421 #if USE_EXCEPTIONS
422 struct deco_not_found {};
423
424
425 math_deco_struct const & search_deco(int code)
426 {
427         math_deco_struct * res =
428                 lower_bound(math_deco_table,
429                             math_deco_table + math_deco_table_size,
430                             code, math_deco_compare());
431         if (res != math_deco_table + math_deco_table_size &&
432             res->code == code)
433                 return *res;
434         throw deco_not_found();
435 }
436
437 #else
438
439
440 math_deco_struct const * search_deco(int code)
441 {
442         math_deco_struct search_elem = { code, 0, 0 };
443         
444         math_deco_struct * res =
445                 lower_bound(math_deco_table,
446                             math_deco_table + math_deco_table_size,
447                             search_elem, math_deco_compare());
448         if (res != math_deco_table + math_deco_table_size &&
449             res->code == code)
450                 return res;
451         return 0;
452 }
453 #endif