]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/MathsSymbols.C
fix starting up which binary is really a symlink; make sure insetinclude file browser...
[lyx.git] / src / frontends / xforms / MathsSymbols.C
1 /**
2  * \file MathsSymbols.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author John Levon
8  */
9
10 #include <config.h>
11 #include <algorithm>
12  
13 #include XPM_H_LOCATION
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "support/LAssert.h" 
20 #include "support/lstrings.h"
21 #include "debug.h"
22 #include "MathsSymbols.h"
23 #include "FormMathsPanel.h"
24  
25 using std::max;
26 using std::endl;
27 using std::ostream;
28
29 #ifndef CXX_GLOBAL_CSTD
30 using std::strstr;
31 #endif
32
33
34 /* Latex code for those bitmaps */
35
36 #include "greek.xbm"
37 #include "arrows.xbm"
38 #include "brel.xbm"
39 #include "bop.xbm"
40 #include "misc.xbm"
41 #include "varsz.xbm"
42 #include "dots.xbm"
43 #include "mathed/math_parser.h"
44 #include "frac.xpm"
45 #include "sub.xpm"
46 #include "super.xpm"
47 #include "style.xpm"
48 #include "sqrt.xpm"
49 #include "delim.xbm"
50 #include "delim.xpm"
51 #include "deco.xbm"
52 #include "deco.xpm"
53 #include "space.xpm"
54 #include "matrix.xpm"
55 #include "equation.xpm"
56
57 char const * function_names[] = {
58         "arccos", "arcsin", "arctan", "arg", "bmod",
59         "cos", "cosh", "cot", "coth", "csc", "deg",
60         "det", "dim", "exp", "gcd", "hom", "inf", "ker",
61         "lg", "lim", "liminf", "limsup", "ln", "log",
62         "max", "min", "sec", "sin", "sinh", "sup",
63         "tan", "tanh"
64 };
65
66 int const nr_function_names = sizeof(function_names) /
67                                      sizeof(char const *);
68
69 char const * latex_arrow[] = {
70         "downarrow", "leftarrow", "Downarrow", "Leftarrow",
71         "hookleftarrow", "rightarrow", "uparrow", "Rightarrow", "Uparrow",
72         "hookrightarrow", "updownarrow", "Leftrightarrow", "leftharpoonup",
73         "rightharpoonup", "rightleftharpoons", "leftrightarrow", "Updownarrow",
74         "leftharpoondown", "rightharpoondown", "mapsto",
75         "Longleftarrow", "Longrightarrow", "Longleftrightarrow",
76         "longleftrightarrow", "longleftarrow", "longrightarrow", "longmapsto",
77         "nwarrow", "nearrow", "swarrow", "searrow",  "",
78 };
79
80 int const nr_latex_arrow = sizeof(latex_arrow) / sizeof(char const *);
81
82 char const * latex_bop[] = {
83         "pm", "cap", "diamond", "oplus",
84         "mp", "cup", "bigtriangleup", "ominus",
85         "times", "uplus", "bigtriangledown", "otimes",
86         "div", "sqcap", "triangleright", "oslash",
87         "cdot", "sqcup", "triangleleft", "odot",
88         "star", "vee", "amalg", "bigcirc",
89         "setminus", "wedge", "dagger", "circ",
90         "bullet", "wr", "ddagger", ""
91 };
92
93 int const nr_latex_bop = sizeof(latex_bop) / sizeof(char const *);
94
95 char const * latex_brel[] = {
96         "leq", "geq", "equiv", "models",
97         "prec", "succ", "sim", "perp",
98         "preceq", "succeq", "simeq", "mid",
99         "ll", "gg", "asymp", "parallel",
100         "subset", "supset", "approx", "smile",
101         "subseteq", "supseteq", "cong", "frown",
102         "sqsubseteq", "sqsupseteq", "doteq", "neq",
103         "in", "ni", "propto", "notin",
104         "vdash", "dashv", "bowtie", ""
105 };
106
107 int const nr_latex_brel = sizeof(latex_brel) / sizeof(char const *);
108
109 char const * latex_dots[] = {
110         "ldots", "cdots", "vdots", "ddots"
111 };
112
113 int const nr_latex_dots = sizeof(latex_dots) / sizeof(char const *);
114
115 char const * latex_greek[] = {
116         "Gamma", "Delta", "Theta", "Lambda", "Xi", "Pi",
117         "Sigma", "Upsilon", "Phi", "Psi", "Omega",
118         "alpha", "beta", "gamma", "delta", "epsilon", "varepsilon", "zeta",
119         "eta", "theta", "vartheta", "iota", "kappa", "lambda", "mu",
120         "nu", "xi", "pi", "varpi", "rho", "sigma", "varsigma",
121         "tau", "upsilon", "phi", "varphi", "chi", "psi", "omega", ""
122 };
123
124 int const nr_latex_greek = sizeof(latex_greek) / sizeof(char const *);
125
126 char const * latex_misc[] = {
127         "nabla", "partial", "infty", "prime", "ell",
128         "emptyset", "exists", "forall", "imath",  "jmath",
129         "Re", "Im", "aleph", "wp", "hbar",
130         "angle", "top", "bot", "Vert", "neg",
131         "flat", "natural", "sharp", "surd", "triangle",
132         "diamondsuit", "heartsuit", "clubsuit", "spadesuit", ""
133 };
134
135 int const nr_latex_misc = sizeof(latex_misc) / sizeof(char const *);
136
137 char const * latex_varsz[] = {
138         "sum", "int", "oint",
139         "prod", "coprod", "bigsqcup",
140         "bigotimes", "bigodot", "bigoplus",
141         "bigcap", "bigcup", "biguplus",
142         "bigvee", "bigwedge", ""
143 };
144
145 int const nr_latex_varsz = sizeof(latex_varsz) / sizeof(char const *);
146
147 static char const ** mathed_get_pixmap_from_icon(int d)
148 {
149         switch (d) {
150                 case MM_FRAC: return frac;
151                 case MM_SQRT: return sqrt_xpm;
152                 case MM_SUPER: return super_xpm;
153                 case MM_SUB: return sub_xpm;
154                 case MM_STYLE: return style_xpm;
155                 case MM_DELIM: return delim;
156                 case MM_MATRIX: return matrix;
157                 case MM_EQU: return equation;
158                 case MM_DECO: return deco;
159                 case MM_SPACE: return space_xpm;
160                 default: return 0;
161         }
162 }
163  
164 static char const ** pixmapFromBitmapData(char const * s, int wx, int hx)
165 {
166         char const ** data = 0;
167
168         int id = -1;
169
170         int i = 0;
171         for (; i < 6; ++i) {
172                 char const ** latex_str = 0;
173                 switch (i) {
174                 case 0: latex_str = latex_greek; break;
175                 case 1: latex_str = latex_bop; break;
176                 case 2: latex_str = latex_brel; break;
177                 case 3: latex_str = latex_arrow; break;
178                 case 4: latex_str = latex_varsz; break;
179                 case 5: latex_str = latex_misc; break;
180                 }
181
182                 for (int k = 0; latex_str[k][0] > ' '; ++k) {
183                         if (compare(latex_str[k], s) == 0) {
184                                 id = k;
185                                 break;
186                         }
187                 }
188                 if (id >= 0) break;
189         }
190         if (i < 6 && id >= 0) {
191                 unsigned char const * bdata = 0;
192                 int w = 0;
193                 int h = 0;
194                 int dw = 0;
195                 int dh = 0;
196
197                 lyxerr[Debug::MATHED] << "Imando " << i << ", " << id << endl;
198                 switch (i) {
199                 case 0:
200                         if (id <= 10) {
201                                 w = Greek_width;
202                                 h = Greek_height;
203                                 bdata = Greek_bits;
204                                 dw = 6;
205                                 dh = 2;
206                         } else {
207                                 w = greek_width;
208                                 h = greek_height;
209                                 bdata = greek_bits;
210                                 dw = 7;
211                                 dh = 4;
212                                 id -= 11;
213                         }
214                         break;
215                 case 1:
216                         w = bop_width;
217                         h = bop_height;
218                         bdata = bop_bits;
219                         dw = 4;
220                         dh = 8;
221                         break;
222                 case 2:
223                         w = brel_width;
224                         h = brel_height;
225                         bdata = brel_bits;
226                         dw = 4;
227                         dh = 9;
228                         break;
229                 case 3:
230                         if (id < 20) {
231                                 w = arrow_width;
232                                 h = arrow_height;
233                                 bdata = arrow_bits;
234                                 dw = 5;
235                                 dh = 4;
236                         } else if (id > 28) {
237                                 w = darrow_width;
238                                 h = darrow_height;
239                                 bdata = darrow_bits;
240                                 dw = 2;
241                                 dh = 2;
242                                 id -= 29;
243                         } else {
244                                 w = larrow_width;
245                                 h = larrow_height;
246                                 bdata = larrow_bits;
247                                 dw = 2;
248                                 dh = 4;
249                                 id -= 20;
250                         }
251                         break;
252                 case 4:
253                         w = varsz_width;
254                         h = varsz_height;
255                         bdata = varsz_bits;
256                         dw = 3;
257                         dh = 5;
258                         break;
259                 case 5:
260                         w = misc_width;
261                         h = misc_height;
262                         bdata = misc_bits;
263                         dw = 5;
264                         dh = 6;
265                         break;
266                 }
267                 int ww = w / dw;
268                 int hh = h / dh;
269
270                 XImage * xima = XCreateImage(fl_get_display(), 0, 1, XYBitmap, 0,
271                                              const_cast<char*>(reinterpret_cast<char const *>(bdata)), w, h, 8, 0);
272                 xima->byte_order = LSBFirst;
273                 xima->bitmap_bit_order = LSBFirst;
274                 int x = (id % dw) * ww;
275                 int y = (id/dw) * hh;
276                 if (ww > wx) ww = wx;
277                 if (hh > hx) hh = hx;
278                 XImage * sbima = XSubImage(xima, x, y, ww, hh);
279                 XpmCreateDataFromImage(fl_get_display(), const_cast<char***>(&data), sbima, sbima, 0);
280
281                 // Dirty hack to get blue symbols quickly
282                 char * sx = const_cast<char*>(strstr(data[2], "FFFFFFFF"));
283                 if (sx) {
284                         for (int k = 0; k < 8; ++k) sx[k] = '0';
285                 }
286
287 //      XDestroyImage(xima);
288         }
289
290         return data;
291 }
292
293  
294 char const ** get_pixmap_from_symbol(char const * arg, int wx, int hx)
295 {
296         lyx::Assert(arg);
297         
298         char const ** data = 0;
299         latexkeys const * l = in_word_set(arg);
300         if (!l)
301                 return 0;
302
303         switch (l->token) {
304         case LM_TK_FRAC:
305                 data = mathed_get_pixmap_from_icon(MM_FRAC);
306                 break;
307         case LM_TK_SQRT:
308                 data = mathed_get_pixmap_from_icon(MM_SQRT);
309                 break;
310         case LM_TK_SYM:
311         case LM_TK_CMR:
312         case LM_TK_CMSY:
313         case LM_TK_CMEX:
314         case LM_TK_CMM:
315         case LM_TK_MSA:
316         case LM_TK_MSB:
317                 // I have to use directly the bitmap data since the
318                 // bitmap tables are not yet created when this
319                 // function is called.
320                 data = pixmapFromBitmapData(arg, wx, hx);
321                 break;
322         default:
323                 break;
324         }
325
326         return data;
327 }