5 #include "mathed/support.h"
11 #include "symbol_def.h"
14 using std::lower_bound;
18 extern LyXFont WhichFont(short type, int size);
20 char const * math_font_name[] = {
31 char const * latex_mathspace[] = {
32 "!", ",", ":", ";", "quad", "qquad"
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
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,
50 static float parenth[] = {
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,
60 static float brace[] = {
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,
71 static float arrow[] = {
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,
79 static float Arrow[] = {
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,
88 static float udarrow[] = {
90 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
92 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
93 1, 0.5, 0.2, 0.5, 0.8,
97 static float Udarrow[] = {
99 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
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,
107 static float brack[] = {
109 0.95, 0.05, 0.05, 0.05, 0.05, 0.95, 0.95, 0.95,
113 static float corner[] = {
115 0.95, 0.05, 0.05, 0.05, 0.05, 0.95,
119 static float angle[] = {
121 1, 0, 0.05, 0.5, 1, 1,
125 static float slash[] = {
126 1, 0.95, 0.05, 0.05, 0.95,
130 static float hline[] = {
131 1, 0.05, 0.5, 0.95, 0.5,
136 static float hline2[] = {
137 1, 0.1, 0.5, 0.3, 0.5,
138 1, 0.7, 0.5, 0.9, 0.5,
142 static float hline3[] = {
144 1, 0.475, 0, 0.525, 0,
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,
157 static float hlinesmall[] = {
158 1, 0.4, 0.5, 0.6, 0.5,
162 static float vert[] = {
163 1, 0.5, 0.05, 0.5, 0.95,
167 static float Vert[] = {
168 1, 0.3, 0.05, 0.3, 0.95,
169 1, 0.7, 0.05, 0.7, 0.95,
173 static float tilde[] = {
175 0.05, 0.8, 0.25, 0.2, 0.75, 0.8, 0.95, 0.2,
181 math_deco_struct math_deco_table[] = {
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 },
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 },
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 },
231 { LM_ldots, &hline3[0], 0 },
232 { LM_cdots, &hline3[0], 0 },
233 { LM_vdots, &hline3[0], 1 },
234 { LM_ddots, &dline3[0], 0 }
238 struct math_deco_compare {
239 /// for use by sort and lower_bound
241 int operator()(math_deco_struct const & a,
242 math_deco_struct const & b) const {
243 return a.code < b.code;
249 int const math_deco_table_size = sizeof(math_deco_table) /sizeof(math_deco_struct);
251 class init_deco_table {
255 sort(math_deco_table,
256 math_deco_table + math_deco_table_size,
257 math_deco_compare());
258 init_deco_table::init = true;
265 bool init_deco_table::init = false;
266 static init_deco_table idt;
269 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
271 LyXFont font = WhichFont(type, size);
272 des = lyxfont::descent(c, font);
273 asc = lyxfont::ascent(c, font);
278 int mathed_char_width(short type, int size, byte c)
280 if (MathIsBinary(type)) {
283 return mathed_string_width(type, size, s);
286 return lyxfont::width(c, WhichFont(type, size));
290 int mathed_string_height(short type, int size, string const & s,
291 int & asc, int & des)
293 LyXFont font = WhichFont(type, size);
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));
303 int mathed_string_width(short type, int size, string const & s)
306 if (MathIsBinary(type))
307 for (string::const_iterator it = s.begin();
308 it != s.end(); ++it) {
316 LyXFont const f = WhichFont(type, size);
317 return lyxfont::width(st, f);
321 LyXFont mathed_get_font(short type, int size)
323 LyXFont f = WhichFont(type, size);
324 if (type == LM_TC_TEX) {
325 f.setLatex(LyXFont::ON);
331 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
342 math_deco_struct mds;
344 mds = search_deco(code);
346 catch (deco_not_found) {
347 // Should this ever happen?
348 lyxerr << "Deco was not found. Programming error?" << endl;
353 float * d = mds.data;
355 if (h > 70 && (mds.code == int('(')
356 || mds.code == int(')')))
359 math_deco_struct const * mds = search_deco(code);
361 // Should this ever happen?
362 lyxerr << "Deco was not found. Programming error?" << endl;
368 float * d = mds->data;
370 if (h > 70 && (mds->code == int('(')
371 || mds->code == int(')')))
378 int n = (w < h) ? w: h;
381 if (r > 0 && r < 3) y += h;
390 xx = d[i++]; yy = d[i++];
391 x2 = d[i++]; y2 = d[i++];
393 sqmt.transf(xx, yy, xx, yy);
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),
407 for (int j = 0; j < n; ++j) {
408 xx = d[i++]; yy = d[i++];
409 // lyxerr << " " << xx << " " << yy << " ";
411 sqmt.transf(xx, yy, xx, yy);
413 mt.transf(xx, yy, xx, yy);
416 // lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
418 pain.lines(xp, yp, n, LColor::mathline);
425 #define USE_EXCEPTIONS 0
427 struct deco_not_found {};
430 math_deco_struct const & search_deco(int code)
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 &&
439 throw deco_not_found();
445 math_deco_struct const * search_deco(int code)
447 math_deco_struct search_elem = { code, 0, 0 };
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 &&