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