5 #include "mathed/support.h"
11 #include "symbol_def.h"
13 extern LyXFont WhichFont(short type, int size);
15 char const * math_font_name[] = {
26 char const * latex_mathspace[] = {
27 "!", ",", ":", ";", "quad", "qquad"
31 * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
32 * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4= square polyline
37 float parenthHigh[] = {
38 2.0, 13.0, 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772, 0.2540,
39 0.1278, 0.1746, 0.1966, 0.0952, 0.3300, 0.0950, 0.5000, 0.0952, 0.6700,
40 0.1746, 0.8034, 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677, 0.9840,
45 static float parenth[] = {
47 0.9930, 0.0071, 0.7324, 0.0578, 0.5141, 0.1126, 0.3380, 0.1714,
48 0.2183, 0.2333, 0.0634, 0.3621, 0.0141, 0.5000, 0.0563, 0.6369,
49 0.2113, 0.7647, 0.3310, 0.8276, 0.5070, 0.8864, 0.7254, 0.9412,
55 static float brace[] = {
57 0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243, 0.5819, 0.0527,
58 0.4859, 0.0892, 0.4463, 0.1278, 0.4463, 0.3732, 0.4011, 0.4199,
59 0.2712, 0.4615, 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
60 0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268, 0.4463, 0.8722,
61 0.4859, 0.9108, 0.5819, 0.9473, 0.7458, 0.9757, 0.9379, 0.9980,
66 static float arrow[] = {
68 0.015, 0.7500, 0.2, 0.6, 0.35, 0.35, 0.5, 0.05,
69 0.65, 0.35, 0.8, 0.6, 0.95, 0.7500,
70 3, 0.5, 0.15, 0.5, 0.95,
74 static float Arrow[] = {
76 0.015, 0.7500, 0.2, 0.6, 0.35, 0.35, 0.5, 0.05,
77 0.65, 0.35, 0.8, 0.6, 0.95, 0.7500,
78 3, 0.35, 0.5, 0.35, 0.95,
79 3, 0.65, 0.5, 0.65, 0.95,
83 static float udarrow[] = {
85 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
87 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
88 1, 0.5, 0.2, 0.5, 0.8,
92 static float Udarrow[] = {
94 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
96 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
97 1, 0.35, 0.2, 0.35, 0.8,
98 1, 0.65, 0.2, 0.65, 0.8,
102 static float brack[] = {
104 0.95, 0.05, 0.05, 0.05, 0.05, 0.95, 0.95, 0.95,
108 static float corner[] = {
110 0.95, 0.05, 0.05, 0.05, 0.05, 0.95,
114 static float angle[] = {
116 1, 0, 0.05, 0.5, 1, 1,
120 static float slash[] = {
121 1, 0.95, 0.05, 0.05, 0.95,
125 static float hline[] = {
126 1, 0.05, 0.5, 0.95, 0.5,
131 static float hline2[] = {
132 1, 0.1, 0.5, 0.3, 0.5,
133 1, 0.7, 0.5, 0.9, 0.5,
137 static float hline3[] = {
139 1, 0.475, 0, 0.525, 0,
145 static float dline3[] = {
146 1, 0.1, 0.1, 0.15, 0.15,
147 1, 0.475, 0.475, 0.525, 0.525,
148 1, 0.85, 0.85, 0.9, 0.9,
152 static float hlinesmall[] = {
153 1, 0.4, 0.5, 0.6, 0.5,
157 static float vert[] = {
158 1, 0.5, 0.05, 0.5, 0.95,
162 static float Vert[] = {
163 1, 0.3, 0.05, 0.3, 0.95,
164 1, 0.7, 0.05, 0.7, 0.95,
168 static float tilde[] = {
170 0.05, 0.8, 0.25, 0.2, 0.75, 0.8, 0.95, 0.2,
176 math_deco_struct math_deco_table[] = {
179 { LM_widehat, &angle[0], 3 },
180 { LM_widetilde, &tilde[0], 0 },
181 { LM_underline, &hline[0], 0 },
182 { LM_overline, &hline[0], 0 },
183 { LM_underbrace, &brace[0], 1 },
184 { LM_overbrace, &brace[0], 3 },
185 { LM_overleftarrow, &arrow[0], 1 },
186 { LM_overightarrow, &arrow[0], 3 },
189 { '(', &parenth[0], 0 },
190 { ')', &parenth[0], 2 },
191 { '{', &brace[0], 0 },
192 { '}', &brace[0], 2 },
193 { '[', &brack[0], 0 },
194 { ']', &brack[0], 2 },
195 { '|', &vert[0], 0 },
196 { '/', &slash[0], 0 },
197 { LM_Vert, &Vert[0], 0 },
198 { LM_backslash, &slash[0], 1 },
199 { LM_langle, &angle[0], 0 },
200 { LM_lceil, &corner[0], 0 },
201 { LM_lfloor, &corner[0], 1 },
202 { LM_rangle, &angle[0], 2 },
203 { LM_rceil, &corner[0], 3 },
204 { LM_rfloor, &corner[0], 2 },
205 { LM_downarrow, &arrow[0], 2 },
206 { LM_Downarrow, &Arrow[0], 2 },
207 { LM_uparrow, &arrow[0], 0 },
208 { LM_Uparrow, &Arrow[0], 0 },
209 { LM_updownarrow, &udarrow[0], 0 },
210 { LM_Updownarrow, &Udarrow[0], 0 },
213 { LM_ddot, &hline2[0], 0 },
214 { LM_hat, &angle[0], 3 },
215 { LM_grave, &slash[0], 1 },
216 { LM_acute, &slash[0], 0 },
217 { LM_tilde, &tilde[0], 0 },
218 { LM_bar, &hline[0], 0 },
219 { LM_dot, &hlinesmall[0], 0 },
220 { LM_check, &angle[0], 1 },
221 { LM_breve, &parenth[0], 1 },
222 { LM_vec, &arrow[0], 3 },
223 { LM_not, &slash[0], 0 },
226 { LM_ldots, &hline3[0], 0 },
227 { LM_cdots, &hline3[0], 0 },
228 { LM_vdots, &hline3[0], 1 },
229 { LM_ddots, &dline3[0], 0 }
233 struct math_deco_compare {
234 /// for use by sort and lower_bound
236 int operator()(math_deco_struct const & a,
237 math_deco_struct const & b) const {
238 return a.code < b.code;
244 int const math_deco_table_size = sizeof(math_deco_table) /sizeof(math_deco_struct);
246 class init_deco_table {
250 sort(math_deco_table,
251 math_deco_table + math_deco_table_size,
252 math_deco_compare());
253 init_deco_table::init = true;
260 bool init_deco_table::init = false;
261 static init_deco_table idt;
264 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
266 LyXFont font = WhichFont(type, size);
267 des = lyxfont::descent(c, font);
268 asc = lyxfont::ascent(c, font);
273 int mathed_char_width(short type, int size, byte c)
275 if (MathIsBinary(type)) {
278 return mathed_string_width(type, size, s);
281 return lyxfont::width(c, WhichFont(type, size));
285 int mathed_string_height(short type, int size, string const & s,
286 int & asc, int & des)
288 LyXFont font = WhichFont(type, size);
290 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
291 des = max(des, lyxfont::descent(*it, font));
292 asc = max(asc, lyxfont::ascent(*it, font));
298 int mathed_string_width(short type, int size, string const & s)
301 if (MathIsBinary(type))
302 for (string::const_iterator it = s.begin();
303 it != s.end(); ++it) {
311 LyXFont const f = WhichFont(type, size);
312 return lyxfont::width(st, f);
316 LyXFont mathed_get_font(short type, int size)
318 LyXFont f = WhichFont(type, size);
319 if (type == LM_TC_TEX) {
320 f.setLatex(LyXFont::ON);
326 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
337 math_deco_struct mds;
339 mds = search_deco(code);
341 catch (deco_not_found) {
342 // Should this ever happen?
343 lyxerr << "Deco was not found. Programming error?" << endl;
348 float * d = mds.data;
350 if (h > 70 && (mds.code == int('(')
351 || mds.code == int(')')))
354 math_deco_struct const * mds = search_deco(code);
356 // Should this ever happen?
357 lyxerr << "Deco was not found. Programming error?" << endl;
363 float * d = mds->data;
365 if (h > 70 && (mds->code == int('(')
366 || mds->code == int(')')))
373 int n = (w < h) ? w: h;
376 if (r > 0 && r < 3) y += h;
385 xx = d[i++]; yy = d[i++];
386 x2 = d[i++]; y2 = d[i++];
388 sqmt.transf(xx, yy, xx, yy);
390 mt.transf(xx, yy, xx, yy);
391 mt.transf(x2, y2, x2, y2);
392 pain.line(x + int(xx), y + int(yy),
393 x + int(x2), y + int(y2),
402 for (int j = 0; j < n; ++j) {
403 xx = d[i++]; yy = d[i++];
404 // lyxerr << " " << xx << " " << yy << " ";
406 sqmt.transf(xx, yy, xx, yy);
408 mt.transf(xx, yy, xx, yy);
411 // lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
413 pain.lines(xp, yp, n, LColor::mathline);
420 #define USE_EXCEPTIONS 0
422 struct deco_not_found {};
425 math_deco_struct const & search_deco(int code)
427 math_deco_struct * res =
428 lower_bound(math_deco_table,
429 math_deco_table + math_deco_table_size,
430 code, math_deco_compare());
431 if (res != math_deco_table + math_deco_table_size &&
434 throw deco_not_found();
440 math_deco_struct const * search_deco(int code)
442 math_deco_struct search_elem = { code, 0, 0 };
444 math_deco_struct * res =
445 lower_bound(math_deco_table,
446 math_deco_table + math_deco_table_size,
447 search_elem, math_deco_compare());
448 if (res != math_deco_table + math_deco_table_size &&