home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / math_delim.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  11KB  |  497 lines

  1.  /* 
  2.  *  File:        math_delim.C
  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.
  7.  *
  8.  *  Dependencies: Xlib, XForms
  9.  *
  10.  *  Copyright: (c) 1996, Alejandro Aguilar Sierra
  11.  *
  12.  *   Version: 0.8beta, Mathed & Lyx project.
  13.  *
  14.  *   You are free to use and modify this code under the terms of
  15.  *   the GNU General Public Licence version 2 or later.
  16.  */
  17.  
  18. #include <config.h>
  19.  
  20. #include FORMS_H_LOCATION
  21. #include <stdlib.h>
  22. #include "symbol_def.h"
  23. #include "math_inset.h"
  24.  
  25. /* 
  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
  28.  */
  29.  
  30. static float parenth[] = {
  31.   2.0, 13.0,
  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,
  35.   0.9930, 0.9919,
  36.   0.0   
  37. };
  38.  
  39. static float brace[] = {
  40.   2.0, 21.0,
  41.   0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243, 0.5819, 0.0527,
  42.   0.4859, 0.0892, 0.4463, 0.1278, 0.4463, 0.3732, 0.4011, 0.4199,
  43.   0.2712, 0.4615, 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
  44.   0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268, 0.4463, 0.8722,
  45.   0.4859, 0.9108, 0.5819, 0.9473, 0.7458, 0.9757, 0.9379, 0.9980,
  46.   0.9492, 0.9980,
  47.   0.0
  48. };
  49.  
  50. static float arrow[] = {
  51.    4, 7,
  52.    0.015, 0.7500,  0.2, 0.6,  0.35, 0.35,  0.5, 0.05,
  53.    0.65, 0.35,  0.8, 0.6,  0.95, 0.7500,
  54.    3, 0.5, 0.15,  0.5, 0.95,
  55.    0.0 
  56. };
  57.  
  58. static float Arrow[] = {
  59.    4, 7,
  60.    0.015, 0.7500,  0.2, 0.6,  0.35, 0.35,  0.5, 0.05,
  61.    0.65, 0.35,  0.8, 0.6,  0.95, 0.7500,
  62.    3, 0.35, 0.5, 0.35, 0.95,
  63.    3, 0.65, 0.5, 0.65, 0.95,
  64.    0.0
  65. };
  66.  
  67. static float udarrow[] = {
  68.    2, 3,
  69.    0.015, 0.25,  0.5, 0.05, 0.95, 0.25,
  70.    2, 3,
  71.    0.015, 0.75,  0.5, 0.95, 0.95, 0.75,  
  72.    1, 0.5, 0.2,  0.5, 0.8,
  73.    0.0 
  74. };
  75.  
  76. static float Udarrow[] = {
  77.    2, 3,
  78.    0.015, 0.25,  0.5, 0.05, 0.95, 0.25,
  79.    2, 3,
  80.    0.015, 0.75,  0.5, 0.95, 0.95, 0.75,  
  81.    1, 0.35, 0.2, 0.35, 0.8,
  82.    1, 0.65, 0.2, 0.65, 0.8,
  83.    0.0 
  84. };
  85.  
  86. static float brack[] = {
  87.    2.0, 4,
  88.    0.95, 0.05,  0.05, 0.05,  0.05, 0.95,  0.95, 0.95,
  89.    0.0
  90. };
  91.  
  92. static float corner[] = {
  93.    2.0, 3,
  94.    0.95, 0.05,  0.05, 0.05,  0.05, 0.95,
  95.    0.0
  96. };
  97.  
  98. static float angle[] = {
  99.    2.0, 3,
  100.    1, 0,  0.05, 0.5,  1, 1,
  101.    0.0
  102. };
  103.  
  104. static float slash[] = {
  105.    1, 0.95, 0.05,  0.05, 0.95, 
  106.    0.0
  107. };
  108.  
  109. static float hline[] = {
  110.    1, 0.05, 0.5,  0.95, 0.5, 
  111.    0.0
  112. };
  113.  
  114.  
  115. static float hline2[] = {
  116.    1, 0.1, 0.5,  0.3, 0.5,
  117.    1, 0.7, 0.5,  0.9, 0.5,
  118.    0.0
  119. }; 
  120.  
  121. static float hline3[] = {
  122.    1, 0.1, 0,  0.15, 0,
  123.    1, 0.475, 0,  0.525, 0,
  124.    1, 0.85, 0,  0.9, 0,  
  125.    0.0
  126. };
  127.  
  128.  
  129. static float dline3[] = {
  130.    1, 0.1, 0.1,  0.15, 0.15,
  131.    1, 0.475, 0.475,  0.525, 0.525,
  132.    1, 0.85, 0.85,  0.9, 0.9,
  133.    0.0
  134. };     
  135.  
  136. static float hlinesmall[] = {
  137.    1, 0.4, 0.5,  0.6, 0.5, 
  138.    0.0
  139. };
  140.  
  141. static float vert[] = {
  142.    1, 0.5, 0.05,  0.5, 0.95, 
  143.    0.0
  144. };
  145.  
  146. static float Vert[] = {
  147.    1, 0.3, 0.05,  0.3, 0.95, 
  148.    1, 0.7, 0.05,  0.7, 0.95,
  149.    0.0
  150. };
  151.  
  152. static float tilde[] = {
  153.    2.0, 4,
  154.    0.05, 0.8,  0.25, 0.2,  0.75, 0.8,  0.95, 0.2,
  155.    0.0
  156. };
  157.  
  158. static struct math_deco_struct {
  159.     int code; float *data; int angle;
  160. } math_deco_table[] = {   
  161.  
  162.    // Decorations
  163.   { LM_widehat, &angle[0], 3 },
  164.   { LM_widetilde, &tilde[0], 0 },
  165.   { LM_underline, &hline[0], 0 },
  166.   { LM_overline, &hline[0], 0 },
  167.   { LM_underbrace, &brace[0], 1 },
  168.   { LM_overbrace,  &brace[0], 3 },
  169.   { LM_overleftarrow, &arrow[0], 1 },
  170.   { LM_overightarrow, &arrow[0], 3 },
  171.      
  172.   // Delimiters
  173.   { '(', &parenth[0], 0 },
  174.   { ')', &parenth[0], 2 },
  175.   { '{', &brace[0], 0 },
  176.   { '}', &brace[0], 2 },
  177.   { '[', &brack[0], 0 },
  178.   { ']', &brack[0], 2 },
  179.   { '|', &vert[0], 0 },
  180.   { '/', &slash[0], 0 },
  181.   { LM_Vert, &Vert[0], 0 },
  182.   { LM_backslash, &slash[0], 1 },
  183.   { LM_langle, &angle[0], 0 },
  184.   { LM_lceil, &corner[0], 0 }, 
  185.   { LM_lfloor, &corner[0], 1 },  
  186.   { LM_rangle, &angle[0], 2 }, 
  187.   { LM_rceil, &corner[0], 3 }, 
  188.   { LM_rfloor, &corner[0], 2 },
  189.   { LM_downarrow, &arrow[0], 2 },
  190.   { LM_Downarrow, &Arrow[0], 2 }, 
  191.   { LM_uparrow, &arrow[0], 0 },
  192.   { LM_Uparrow, &Arrow[0], 0 },
  193.   { LM_updownarrow, &udarrow[0], 0 },
  194.   { LM_Updownarrow, &Udarrow[0], 0 },     
  195.  
  196.   // Accents   
  197.   { LM_ddot, &hline2[0], 0 },
  198.   { LM_hat, &angle[0], 3 },
  199.   { LM_grave, &slash[0], 1 },
  200.   { LM_acute, &slash[0], 0 },
  201.   { LM_tilde, &tilde[0], 0 },
  202.   { LM_bar, &hline[0], 0 },
  203.   { LM_dot, &hlinesmall[0], 0 },
  204.   { LM_check, &angle[0], 1 },
  205.   { LM_breve, &parenth[0], 1 },
  206.   { LM_vec, &arrow[0], 3 },
  207.   { LM_not, &slash[0], 0 },  
  208.  
  209.   // Dots
  210.   { LM_ldots, &hline3[0], 0 }, 
  211.   { LM_cdots, &hline3[0], 0 },
  212.   { LM_vdots, &hline3[0], 1 },
  213.   { LM_ddots, &dline3[0], 0 },
  214.      
  215.   { 0, 0, 0 }
  216. };
  217.  
  218.  
  219. inline int odd(int x) { return ((x) & 1); }
  220.  
  221. typedef float matriz_data[2][2];
  222.  
  223. const matriz_data MATIDEN={ {1,0}, {0,1}};
  224.  
  225. extern void mathed_set_font(short type, int style);
  226. extern int mathed_char_width(short type, int style, byte c);
  227. extern int mathed_char_height(short, int, byte, int&, int&);
  228.  
  229. #define mateq(m1,m2)  memcpy(m1,m2,sizeof(matriz_data))
  230.  
  231. class Matriz {
  232.  public: 
  233.    Matriz() { mateq(m,MATIDEN); }
  234.    void rota(int);
  235.    void escala(float, float);
  236.    void transf(float, float, float&, float&);
  237.    
  238.  protected:
  239.    matriz_data m;
  240.    void matmat(matriz_data& a);
  241. };
  242.  
  243.  
  244. void Matriz::rota(int code)
  245. {
  246.    float cs, sn;
  247.    
  248.    matriz_data r;
  249.    mateq(r,MATIDEN);
  250.    cs = (odd(code)) ? 0: 1 - code;
  251.    sn = (odd(code)) ? 2 - code: 0;
  252.    r[0][0] = cs;         r[0][1] = sn;
  253.    r[1][0] = -r[0][1];   r[1][1] = r[0][0];
  254.    matmat(r);
  255. }
  256.  
  257. void Matriz::escala(float x, float y)
  258. {
  259.    matriz_data s;
  260.    mateq(s,MATIDEN);
  261.    s[0][0] = x;  s[1][1] = y;
  262.    matmat(s);
  263. }
  264.  
  265.  
  266. void Matriz::matmat(matriz_data& a)
  267. {
  268.    int i;
  269.    matriz_data c;   
  270.    for (i=0;i<2; i++) {
  271.       c[0][i] = a[0][0]*m[0][i] + a[0][1]*m[1][i];
  272.       c[1][i] = a[1][0]*m[0][i] + a[1][1]*m[1][i];
  273.    }
  274.    mateq(m, c);
  275. }
  276.  
  277. void Matriz::transf(float xp, float yp, float &x, float &y)
  278. {
  279.    x = m[0][0]*xp + m[0][1]*yp;
  280.    y = m[1][0]*xp + m[1][1]*yp;
  281. }
  282.  
  283. extern GC latexGC, mathGC, mathLineGC, cursorGC;
  284.  
  285. static int search_deco(int code)
  286. {
  287.    int i=0;
  288.    
  289.    while (math_deco_table[i].code &&  math_deco_table[i].code!=code) i++;
  290.    if (!math_deco_table[i].code) i = -1;
  291.    return i;
  292. }
  293.       
  294. void mathed_draw_deco(Window win, int x, int y, int w, int h, int code)
  295. {
  296.    Matriz mt, sqmt;
  297.    XPoint p[32];
  298.    float *d, xx, yy, x2, y2;
  299.    int i=0,j, n, r;
  300.    
  301.    j = search_deco(code);   
  302.    if (j<0) return;
  303.    
  304.    r = math_deco_table[j].angle;
  305.    d = math_deco_table[j].data;
  306.    
  307.    mt.rota(r);
  308.    mt.escala(w, h);
  309.    
  310.    n = (w < h) ? w: h;
  311.    sqmt.rota(r);
  312.    sqmt.escala(n, n);
  313.    if (r> 0 && r< 3) y += h;   
  314.    if (r>=2) x += w;   
  315.    do {
  316.       code = (int)d[i++];
  317.       switch (code) {
  318.        case 0: break;
  319.        case 1: 
  320.        case 3:
  321.        {
  322.       xx = d[i++]; yy = d[i++];
  323.       x2 = d[i++]; y2 = d[i++];
  324.       if (code==3) 
  325.         sqmt.transf(xx, yy, xx, yy);
  326.       else
  327.         mt.transf(xx, yy, xx, yy);
  328.       mt.transf(x2, y2, x2, y2);
  329.       XDrawLine(fl_display, win, mathGC, x+(int)xx, y+(int)yy,
  330.             x+(int)x2, y+(int)y2);
  331.       XFlush(fl_display);
  332.       break;
  333.        }     
  334.        case 2: 
  335.        case 4:
  336.        {
  337.       n = (int)d[i++];
  338.       for (j=0; j<n; j++) {
  339.          xx = d[i++]; yy = d[i++];
  340. //         fprintf(stderr, " %g %g ", xx, yy);
  341.          if (code==4) 
  342.            sqmt.transf(xx, yy, xx, yy);
  343.          else
  344.            mt.transf(xx, yy, xx, yy);
  345.          p[j].x = x+(int)xx;
  346.          p[j].y = y+(int)yy;
  347.          //      fprintf(stderr, "P[%d %g %g %d %d]", j, xx, yy, x, y);
  348.       }
  349.       XDrawLines(fl_display, win, mathLineGC, p, n, CoordModeOrigin);
  350.       XFlush(fl_display);
  351.        }
  352.       }
  353.    } while (code);
  354. }
  355.  
  356. void
  357. MathDelimInset::Draw(int x, int y)
  358.    xo = x;  yo = y; 
  359.    MathParInset::Draw(x+dw+2, y-dh); 
  360.    //int h=Height(), hg=descent-1;  
  361.  
  362.    if (left=='.') {
  363.      XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
  364.      XFlush(fl_display);
  365.    }
  366.    else
  367.      mathed_draw_deco(pm, x, y-ascent, dw, Height(), left);
  368.    x += Width()-dw-2;
  369.    if (right=='.') {
  370.      XDrawLine(fl_display, pm, cursorGC, x+4, yo-ascent, x+4, yo+descent);
  371.      XFlush(fl_display);
  372.    }
  373.    else
  374.      mathed_draw_deco(pm, x, y-ascent, dw, Height(), right);
  375. }
  376.  
  377. void
  378. MathDelimInset::Metrics()
  379. {
  380.    MathParInset::Metrics();
  381.    int d;
  382.    
  383.    mathed_char_height(LM_TC_CONST, size, 'I', d, dh);
  384.    dh /= 2;
  385.    ascent += 2 + dh;
  386.    descent += 2 - dh;
  387.    dw = Height()/4;
  388.    if (dw<6) dw = 6;
  389.    width += 2*dw+4;
  390. }
  391.  
  392.  
  393. void
  394. MathDecorationInset::Draw(int x, int y)
  395.    MathParInset::Draw(x+(width-dw)/2, y);
  396.    mathed_draw_deco(pm, x, y+dy, width, dh, deco);
  397. }
  398.  
  399.  
  400. void
  401. MathDecorationInset::Metrics()
  402. {
  403.    int h = 2*mathed_char_height(LM_TC_VAR, size, 'I', ascent, descent);  
  404.    MathParInset::Metrics();
  405.    int w = Width()+4;
  406.    if (w<16) w = 16;
  407.    dh = w/5;
  408.    if (dh>h) dh = h;
  409.  
  410.    if (upper) {
  411.       ascent += dh+2;
  412.       dy = -ascent;
  413.    } else {
  414.       dy = descent+2;
  415.       descent += dh+4;
  416.    }
  417.    dw = width;
  418.    width = w;
  419. }
  420.  
  421. void
  422. MathAccentInset::Draw(int x, int y)
  423. {
  424.     int dw = width-2;
  425. /*    char s[8];
  426.     mathed_set_font(fn, size);
  427.     if (MathIsBinary(fn)) {
  428.     s[0] = s[2] = ' '; 
  429.     s[1] = (char)c;
  430.     ns = 3;
  431.     dw = mathed_char_width(fn, size, c);
  432.     } else
  433.       s[0] = (char)c;
  434. */
  435.     if (inset) {
  436.     inset->Draw(x, y);
  437.     } else {
  438.     drawStr(fn, size, x, y, &c, 1);
  439.     XFlush(fl_display);
  440.     }
  441.     x += (code==LM_not) ? (width-dw)/2: 2;
  442.     mathed_draw_deco(pm, x, y-dy, dw, dh, code);
  443. }
  444.  
  445. void
  446. MathAccentInset::Metrics()
  447. {
  448.     
  449.     if (inset) {
  450.     inset->Metrics();
  451.     ascent = inset->Ascent();
  452.     descent = inset->Descent();
  453.     width = inset->Width();
  454.     dh = ascent;
  455.     } else {
  456.     mathed_char_height(fn, size, c, ascent, descent);
  457.     width = mathed_char_width(fn, size, c);
  458.     dh = (width-2)/2; 
  459.     }
  460.     if (code==LM_not) {
  461.     ascent += dh;
  462.     descent += dh;
  463.     dh = Height();
  464.     } else 
  465.       ascent += dh+2;
  466.         
  467.     dy = ascent;
  468. //    if (MathIsBinary(fn))
  469. //    width += 2*mathed_char_width(fn, size, ' ');    
  470. }
  471.  
  472.  
  473. void
  474. MathDotsInset::Draw(int x, int y)
  475. {
  476.    mathed_draw_deco(pm, x+2, y-dh, width-2, ascent, code);
  477.    if (code==LM_vdots||code==LM_ddots) x++; 
  478.    if (code!=LM_vdots) y--;
  479.    mathed_draw_deco(pm, x+2, y-dh, width-2, ascent, code);
  480. }     
  481.  
  482. void
  483. MathDotsInset::Metrics()
  484. {
  485.    mathed_char_height(LM_TC_VAR, size, 'M', ascent, descent);
  486.    width = mathed_char_width(LM_TC_VAR, size, 'M');   
  487.    switch (code) {
  488.     case LM_ldots: dh = 0; break;
  489.     case LM_cdots: dh = ascent/2; break;
  490.     case LM_vdots: width /=2;
  491.     case LM_ddots: dh = ascent; break;
  492.    }
  493.  
  494.