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