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