3 * Purpose: Draw delimiters and decorations
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Vectorial fonts for simple and resizable objets.
8 * Dependencies: Xlib, XForms
10 * Copyright: (c) 1996, Alejandro Aguilar Sierra
12 * Version: 0.8beta, Mathed & Lyx project.
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
20 #include FORMS_H_LOCATION
22 #include "symbol_def.h"
23 #include "math_inset.h"
28 * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
29 * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4= square polyline
32 static float parenth[] = {
34 0.9930, 0.0071, 0.7324, 0.0578, 0.5141, 0.1126, 0.3380, 0.1714,
35 0.2183, 0.2333, 0.0634, 0.3621, 0.0141, 0.5000, 0.0563, 0.6369,
36 0.2113, 0.7647, 0.3310, 0.8276, 0.5070, 0.8864, 0.7254, 0.9412,
41 static float parenthHigh[] = {
42 2.0, 13.0, 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772, 0.2540,
43 0.1278, 0.1746, 0.1966, 0.0952, 0.3300, 0.0950, 0.5000, 0.0952, 0.6700,
44 0.1746, 0.8034, 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677, 0.9840,
48 static float brace[] = {
50 0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243, 0.5819, 0.0527,
51 0.4859, 0.0892, 0.4463, 0.1278, 0.4463, 0.3732, 0.4011, 0.4199,
52 0.2712, 0.4615, 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
53 0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268, 0.4463, 0.8722,
54 0.4859, 0.9108, 0.5819, 0.9473, 0.7458, 0.9757, 0.9379, 0.9980,
59 static float arrow[] = {
61 0.015, 0.7500, 0.2, 0.6, 0.35, 0.35, 0.5, 0.05,
62 0.65, 0.35, 0.8, 0.6, 0.95, 0.7500,
63 3, 0.5, 0.15, 0.5, 0.95,
67 static float Arrow[] = {
69 0.015, 0.7500, 0.2, 0.6, 0.35, 0.35, 0.5, 0.05,
70 0.65, 0.35, 0.8, 0.6, 0.95, 0.7500,
71 3, 0.35, 0.5, 0.35, 0.95,
72 3, 0.65, 0.5, 0.65, 0.95,
76 static float udarrow[] = {
78 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
80 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
81 1, 0.5, 0.2, 0.5, 0.8,
85 static float Udarrow[] = {
87 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
89 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
90 1, 0.35, 0.2, 0.35, 0.8,
91 1, 0.65, 0.2, 0.65, 0.8,
95 static float brack[] = {
97 0.95, 0.05, 0.05, 0.05, 0.05, 0.95, 0.95, 0.95,
101 static float corner[] = {
103 0.95, 0.05, 0.05, 0.05, 0.05, 0.95,
107 static float angle[] = {
109 1, 0, 0.05, 0.5, 1, 1,
113 static float slash[] = {
114 1, 0.95, 0.05, 0.05, 0.95,
118 static float hline[] = {
119 1, 0.05, 0.5, 0.95, 0.5,
124 static float hline2[] = {
125 1, 0.1, 0.5, 0.3, 0.5,
126 1, 0.7, 0.5, 0.9, 0.5,
130 static float hline3[] = {
132 1, 0.475, 0, 0.525, 0,
138 static float dline3[] = {
139 1, 0.1, 0.1, 0.15, 0.15,
140 1, 0.475, 0.475, 0.525, 0.525,
141 1, 0.85, 0.85, 0.9, 0.9,
145 static float hlinesmall[] = {
146 1, 0.4, 0.5, 0.6, 0.5,
150 static float vert[] = {
151 1, 0.5, 0.05, 0.5, 0.95,
155 static float Vert[] = {
156 1, 0.3, 0.05, 0.3, 0.95,
157 1, 0.7, 0.05, 0.7, 0.95,
161 static float tilde[] = {
163 0.05, 0.8, 0.25, 0.2, 0.75, 0.8, 0.95, 0.2,
167 static struct math_deco_struct {
168 int code; float *data; int angle;
169 } math_deco_table[] = {
172 { LM_widehat, &angle[0], 3 },
173 { LM_widetilde, &tilde[0], 0 },
174 { LM_underline, &hline[0], 0 },
175 { LM_overline, &hline[0], 0 },
176 { LM_underbrace, &brace[0], 1 },
177 { LM_overbrace, &brace[0], 3 },
178 { LM_overleftarrow, &arrow[0], 1 },
179 { LM_overightarrow, &arrow[0], 3 },
182 { '(', &parenth[0], 0 },
183 { ')', &parenth[0], 2 },
184 { '{', &brace[0], 0 },
185 { '}', &brace[0], 2 },
186 { '[', &brack[0], 0 },
187 { ']', &brack[0], 2 },
188 { '|', &vert[0], 0 },
189 { '/', &slash[0], 0 },
190 { LM_Vert, &Vert[0], 0 },
191 { LM_backslash, &slash[0], 1 },
192 { LM_langle, &angle[0], 0 },
193 { LM_lceil, &corner[0], 0 },
194 { LM_lfloor, &corner[0], 1 },
195 { LM_rangle, &angle[0], 2 },
196 { LM_rceil, &corner[0], 3 },
197 { LM_rfloor, &corner[0], 2 },
198 { LM_downarrow, &arrow[0], 2 },
199 { LM_Downarrow, &Arrow[0], 2 },
200 { LM_uparrow, &arrow[0], 0 },
201 { LM_Uparrow, &Arrow[0], 0 },
202 { LM_updownarrow, &udarrow[0], 0 },
203 { LM_Updownarrow, &Udarrow[0], 0 },
206 { LM_ddot, &hline2[0], 0 },
207 { LM_hat, &angle[0], 3 },
208 { LM_grave, &slash[0], 1 },
209 { LM_acute, &slash[0], 0 },
210 { LM_tilde, &tilde[0], 0 },
211 { LM_bar, &hline[0], 0 },
212 { LM_dot, &hlinesmall[0], 0 },
213 { LM_check, &angle[0], 1 },
214 { LM_breve, &parenth[0], 1 },
215 { LM_vec, &arrow[0], 3 },
216 { LM_not, &slash[0], 0 },
219 { LM_ldots, &hline3[0], 0 },
220 { LM_cdots, &hline3[0], 0 },
221 { LM_vdots, &hline3[0], 1 },
222 { LM_ddots, &dline3[0], 0 },
229 int odd(int x) { return ((x) & 1); }
231 typedef float matriz_data[2][2];
233 const matriz_data MATIDEN= { {1, 0}, {0, 1}};
235 extern int mathed_char_width(short type, int style, byte c);
236 extern int mathed_char_height(short, int, byte, int &, int &);
238 #define mateq(m1, m2) memcpy(m1, m2, sizeof(matriz_data))
242 Matriz() { mateq(m, MATIDEN); }
244 void escala(float, float);
245 void transf(float, float, float &, float &);
249 void matmat(matriz_data & a);
253 void Matriz::rota(int code)
259 cs = (odd(code)) ? 0: 1 - code;
260 sn = (odd(code)) ? 2 - code: 0;
261 r[0][0] = cs; r[0][1] = sn;
262 r[1][0] = -r[0][1]; r[1][1] = r[0][0];
266 void Matriz::escala(float x, float y)
270 s[0][0] = x; s[1][1] = y;
275 void Matriz::matmat(matriz_data & a)
278 for (int i = 0;i < 2; ++i) {
279 c[0][i] = a[0][0] * m[0][i] + a[0][1] * m[1][i];
280 c[1][i] = a[1][0] * m[0][i] + a[1][1] * m[1][i];
285 void Matriz::transf(float xp, float yp, float & x, float & y)
287 x = m[0][0] * xp + m[0][1] * yp;
288 y = m[1][0] * xp + m[1][1] * yp;
293 int search_deco(int code)
297 while (math_deco_table[i].code && math_deco_table[i].code != code) ++i;
298 if (!math_deco_table[i].code) i = -1;
303 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
306 float xx, yy, x2, y2;
309 int j = search_deco(code);
312 int r = math_deco_table[j].angle;
313 float * d = math_deco_table[j].data;
315 if (h > 70 && (math_deco_table[j].code == int('(')
316 || math_deco_table[j].code == int(')')))
322 int n = (w < h) ? w: h;
325 if (r > 0 && r < 3) y += h;
334 xx = d[i++]; yy = d[i++];
335 x2 = d[i++]; y2 = d[i++];
337 sqmt.transf(xx, yy, xx, yy);
339 mt.transf(xx, yy, xx, yy);
340 mt.transf(x2, y2, x2, y2);
341 pain.line(x + int(xx), y + int(yy),
342 x + int(x2), y + int(y2),
351 for (j = 0; j < n; ++j) {
352 xx = d[i++]; yy = d[i++];
353 // lyxerr << " " << xx << " " << yy << " ";
355 sqmt.transf(xx, yy, xx, yy);
357 mt.transf(xx, yy, xx, yy);
360 // lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
362 pain.lines(xp, yp, n, LColor::mathline);
370 MathDelimInset::draw(Painter & pain, int x, int y)
373 MathParInset::draw(pain, x + dw + 2, y - dh);
376 pain.line(x + 4, yo - ascent,
380 mathed_draw_deco(pain, x, y - ascent, dw, Height(), left);
383 pain.line(x + 4, yo - ascent,
387 mathed_draw_deco(pain, x, y-ascent, dw, Height(), right);
392 MathDelimInset::Metrics()
394 MathParInset::Metrics();
397 mathed_char_height(LM_TC_CONST, size, 'I', d, dh);
402 if (dw > 15) dw = 15;
409 MathDecorationInset::draw(Painter & pain, int x, int y)
411 MathParInset::draw(pain, x + (width - dw) / 2, y);
412 mathed_draw_deco(pain, x, y + dy, width, dh, deco);
417 MathDecorationInset::Metrics()
419 int h = 2*mathed_char_height(LM_TC_VAR, size, 'I', ascent, descent);
420 MathParInset::Metrics();
439 MathAccentInset::draw(Painter & pain, int x, int y)
444 inset->draw(pain, x, y);
446 drawStr(pain, fn, size, x, y, &c, 1);
448 x += (code == LM_not) ? (width-dw) / 2 : 2;
449 mathed_draw_deco(pain, x, y - dy, dw, dh, code);
454 MathAccentInset::Metrics()
459 ascent = inset->Ascent();
460 descent = inset->Descent();
461 width = inset->Width();
464 mathed_char_height(fn, size, c, ascent, descent);
465 width = mathed_char_width(fn, size, c);
468 if (code == LM_not) {
476 // if (MathIsBinary(fn))
477 // width += 2*mathed_char_width(fn, size, ' ');
482 MathDotsInset::draw(Painter & pain, int x, int y)
484 mathed_draw_deco(pain, x + 2, y - dh, width - 2, ascent, code);
485 if (code == LM_vdots || code == LM_ddots) ++x;
486 if (code != LM_vdots) --y;
487 mathed_draw_deco(pain, x + 2, y - dh, width - 2, ascent, code);
492 MathDotsInset::Metrics()
494 mathed_char_height(LM_TC_VAR, size, 'M', ascent, descent);
495 width = mathed_char_width(LM_TC_VAR, size, 'M');
497 case LM_ldots: dh = 0; break;
498 case LM_cdots: dh = ascent/2; break;
499 case LM_vdots: width /= 2;
500 case LM_ddots: dh = ascent; break;