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"
26 * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
27 * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4= square polyline
30 static float parenth[] = {
32 0.9930, 0.0071, 0.7324, 0.0578, 0.5141, 0.1126, 0.3380, 0.1714,
33 0.2183, 0.2333, 0.0634, 0.3621, 0.0141, 0.5000, 0.0563, 0.6369,
34 0.2113, 0.7647, 0.3310, 0.8276, 0.5070, 0.8864, 0.7254, 0.9412,
39 static float parenthHigh[] = {
40 2.0, 13.0, 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772, 0.2540,
41 0.1278, 0.1746, 0.1966, 0.0952, 0.3300, 0.0950, 0.5000, 0.0952, 0.6700,
42 0.1746, 0.8034, 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677, 0.9840,
46 static float brace[] = {
48 0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243, 0.5819, 0.0527,
49 0.4859, 0.0892, 0.4463, 0.1278, 0.4463, 0.3732, 0.4011, 0.4199,
50 0.2712, 0.4615, 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
51 0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268, 0.4463, 0.8722,
52 0.4859, 0.9108, 0.5819, 0.9473, 0.7458, 0.9757, 0.9379, 0.9980,
57 static float arrow[] = {
59 0.015, 0.7500, 0.2, 0.6, 0.35, 0.35, 0.5, 0.05,
60 0.65, 0.35, 0.8, 0.6, 0.95, 0.7500,
61 3, 0.5, 0.15, 0.5, 0.95,
65 static float Arrow[] = {
67 0.015, 0.7500, 0.2, 0.6, 0.35, 0.35, 0.5, 0.05,
68 0.65, 0.35, 0.8, 0.6, 0.95, 0.7500,
69 3, 0.35, 0.5, 0.35, 0.95,
70 3, 0.65, 0.5, 0.65, 0.95,
74 static float udarrow[] = {
76 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
78 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
79 1, 0.5, 0.2, 0.5, 0.8,
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.35, 0.2, 0.35, 0.8,
89 1, 0.65, 0.2, 0.65, 0.8,
93 static float brack[] = {
95 0.95, 0.05, 0.05, 0.05, 0.05, 0.95, 0.95, 0.95,
99 static float corner[] = {
101 0.95, 0.05, 0.05, 0.05, 0.05, 0.95,
105 static float angle[] = {
107 1, 0, 0.05, 0.5, 1, 1,
111 static float slash[] = {
112 1, 0.95, 0.05, 0.05, 0.95,
116 static float hline[] = {
117 1, 0.05, 0.5, 0.95, 0.5,
122 static float hline2[] = {
123 1, 0.1, 0.5, 0.3, 0.5,
124 1, 0.7, 0.5, 0.9, 0.5,
128 static float hline3[] = {
130 1, 0.475, 0, 0.525, 0,
136 static float dline3[] = {
137 1, 0.1, 0.1, 0.15, 0.15,
138 1, 0.475, 0.475, 0.525, 0.525,
139 1, 0.85, 0.85, 0.9, 0.9,
143 static float hlinesmall[] = {
144 1, 0.4, 0.5, 0.6, 0.5,
148 static float vert[] = {
149 1, 0.5, 0.05, 0.5, 0.95,
153 static float Vert[] = {
154 1, 0.3, 0.05, 0.3, 0.95,
155 1, 0.7, 0.05, 0.7, 0.95,
159 static float tilde[] = {
161 0.05, 0.8, 0.25, 0.2, 0.75, 0.8, 0.95, 0.2,
165 static struct math_deco_struct {
166 int code; float *data; int angle;
167 } math_deco_table[] = {
170 { LM_widehat, &angle[0], 3 },
171 { LM_widetilde, &tilde[0], 0 },
172 { LM_underline, &hline[0], 0 },
173 { LM_overline, &hline[0], 0 },
174 { LM_underbrace, &brace[0], 1 },
175 { LM_overbrace, &brace[0], 3 },
176 { LM_overleftarrow, &arrow[0], 1 },
177 { LM_overightarrow, &arrow[0], 3 },
180 { '(', &parenth[0], 0 },
181 { ')', &parenth[0], 2 },
182 { '{', &brace[0], 0 },
183 { '}', &brace[0], 2 },
184 { '[', &brack[0], 0 },
185 { ']', &brack[0], 2 },
186 { '|', &vert[0], 0 },
187 { '/', &slash[0], 0 },
188 { LM_Vert, &Vert[0], 0 },
189 { LM_backslash, &slash[0], 1 },
190 { LM_langle, &angle[0], 0 },
191 { LM_lceil, &corner[0], 0 },
192 { LM_lfloor, &corner[0], 1 },
193 { LM_rangle, &angle[0], 2 },
194 { LM_rceil, &corner[0], 3 },
195 { LM_rfloor, &corner[0], 2 },
196 { LM_downarrow, &arrow[0], 2 },
197 { LM_Downarrow, &Arrow[0], 2 },
198 { LM_uparrow, &arrow[0], 0 },
199 { LM_Uparrow, &Arrow[0], 0 },
200 { LM_updownarrow, &udarrow[0], 0 },
201 { LM_Updownarrow, &Udarrow[0], 0 },
204 { LM_ddot, &hline2[0], 0 },
205 { LM_hat, &angle[0], 3 },
206 { LM_grave, &slash[0], 1 },
207 { LM_acute, &slash[0], 0 },
208 { LM_tilde, &tilde[0], 0 },
209 { LM_bar, &hline[0], 0 },
210 { LM_dot, &hlinesmall[0], 0 },
211 { LM_check, &angle[0], 1 },
212 { LM_breve, &parenth[0], 1 },
213 { LM_vec, &arrow[0], 3 },
214 { LM_not, &slash[0], 0 },
217 { LM_ldots, &hline3[0], 0 },
218 { LM_cdots, &hline3[0], 0 },
219 { LM_vdots, &hline3[0], 1 },
220 { LM_ddots, &dline3[0], 0 },
226 inline int odd(int x) { return ((x) & 1); }
228 typedef float matriz_data[2][2];
230 const matriz_data MATIDEN= { {1, 0}, {0, 1}};
232 extern void mathed_set_font(short type, int style);
233 extern int mathed_char_width(short type, int style, byte c);
234 extern int mathed_char_height(short, int, byte, int&, int&);
236 #define mateq(m1, m2) memcpy(m1, m2, sizeof(matriz_data))
240 Matriz() { mateq(m, MATIDEN); }
242 void escala(float, float);
243 void transf(float, float, float&, float&);
247 void matmat(matriz_data& a);
251 void Matriz::rota(int code)
257 cs = (odd(code)) ? 0: 1 - code;
258 sn = (odd(code)) ? 2 - code: 0;
259 r[0][0] = cs; r[0][1] = sn;
260 r[1][0] = -r[0][1]; r[1][1] = r[0][0];
264 void Matriz::escala(float x, float y)
268 s[0][0] = x; s[1][1] = y;
273 void Matriz::matmat(matriz_data& a)
276 for (int i = 0;i < 2; ++i) {
277 c[0][i] = a[0][0]*m[0][i] + a[0][1]*m[1][i];
278 c[1][i] = a[1][0]*m[0][i] + a[1][1]*m[1][i];
283 void Matriz::transf(float xp, float yp, float &x, float &y)
285 x = m[0][0]*xp + m[0][1]*yp;
286 y = m[1][0]*xp + m[1][1]*yp;
289 extern GC latexGC, mathGC, mathLineGC, cursorGC;
291 static int search_deco(int code)
295 while (math_deco_table[i].code && math_deco_table[i].code!= code) ++i;
296 if (!math_deco_table[i].code) i = -1;
300 void mathed_draw_deco(Window win, int x, int y, int w, int h, int code)
304 float xx, yy, x2, y2;
307 j = search_deco(code);
310 int r = math_deco_table[j].angle;
311 float * d = math_deco_table[j].data;
313 if (h > 70 && (math_deco_table[j].code == int('(') || math_deco_table[j].code == int(')')))
322 if (r> 0 && r< 3) y += h;
331 xx = d[i++]; yy = d[i++];
332 x2 = d[i++]; y2 = d[i++];
334 sqmt.transf(xx, yy, xx, yy);
336 mt.transf(xx, yy, xx, yy);
337 mt.transf(x2, y2, x2, y2);
338 XDrawLine(fl_display, win, mathGC, x + int(xx), y + int(yy),
339 x + int(x2), y + int(y2));
347 for (j = 0; j < n; ++j) {
348 xx = d[i++]; yy = d[i++];
349 // lyxerr << " " << xx << " " << yy << " ";
351 sqmt.transf(xx, yy, xx, yy);
353 mt.transf(xx, yy, xx, yy);
354 p[j].x = x + int(xx);
355 p[j].y = y + int(yy);
356 // lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
358 XDrawLines(fl_display, win, mathLineGC, p, n, CoordModeOrigin);
366 MathDelimInset::Draw(int x, int y)
369 MathParInset::Draw(x+dw+2, y-dh);
370 //int h= Height(), hg= descent-1;
373 XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
377 mathed_draw_deco(pm, x, y-ascent, dw, Height(), left);
380 XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
384 mathed_draw_deco(pm, x, y-ascent, dw, Height(), right);
388 MathDelimInset::Metrics()
390 MathParInset::Metrics();
393 mathed_char_height(LM_TC_CONST, size, 'I', d, dh);
398 if (dw > 15) dw = 15;
405 MathDecorationInset::Draw(int x, int y)
407 MathParInset::Draw(x+(width-dw)/2, y);
408 mathed_draw_deco(pm, x, y+dy, width, dh, deco);
413 MathDecorationInset::Metrics()
415 int h = 2*mathed_char_height(LM_TC_VAR, size, 'I', ascent, descent);
416 MathParInset::Metrics();
434 MathAccentInset::Draw(int x, int y)
438 mathed_set_font(fn, size);
439 if (MathIsBinary(fn)) {
443 dw = mathed_char_width(fn, size, c);
450 drawStr(fn, size, x, y, &c, 1);
453 x += (code == LM_not) ? (width-dw)/2: 2;
454 mathed_draw_deco(pm, x, y-dy, dw, dh, code);
458 MathAccentInset::Metrics()
463 ascent = inset->Ascent();
464 descent = inset->Descent();
465 width = inset->Width();
468 mathed_char_height(fn, size, c, ascent, descent);
469 width = mathed_char_width(fn, size, c);
472 if (code == LM_not) {
480 // if (MathIsBinary(fn))
481 // width += 2*mathed_char_width(fn, size, ' ');
486 MathDotsInset::Draw(int x, int y)
488 mathed_draw_deco(pm, x + 2, y - dh, width - 2, ascent, code);
489 if (code == LM_vdots || code == LM_ddots) ++x;
490 if (code!= LM_vdots) --y;
491 mathed_draw_deco(pm, x + 2, y - dh, width - 2, ascent, code);
495 MathDotsInset::Metrics()
497 mathed_char_height(LM_TC_VAR, size, 'M', ascent, descent);
498 width = mathed_char_width(LM_TC_VAR, size, 'M');
500 case LM_ldots: dh = 0; break;
501 case LM_cdots: dh = ascent/2; break;
502 case LM_vdots: width /= 2;
503 case LM_ddots: dh = ascent; break;