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_write.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  9KB  |  459 lines

  1. /*
  2.  *  File:        math_write.h
  3.  *  Purpose:     Write math paragraphs in LaTeX
  4.  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
  5.  *  Created:     January 1996
  6.  *  Description: 
  7.  *
  8.  *  Dependencies: Xlib, XForms
  9.  *
  10.  *  Copyright: (c) 1996, 1997 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. #include "LString.h"
  20. #include "math_inset.h"
  21. #include "math_iter.h"
  22. #include "math_parser.h"
  23.  
  24.  
  25. extern char const *latex_mathenv[];
  26. extern char *latex_mathspace[];
  27.  
  28. // quite a hack i know. Should be done with return values...
  29. static int number_of_newlines;
  30.  
  31. char const *math_font_name[] = {
  32.    "mathrm",
  33.    "mathcal",
  34.    "mathbf",
  35.    "mathsf",
  36.    "mathtt",
  37.    "mathit",
  38.    "textrm"
  39. };
  40.  
  41.  
  42. void
  43. MathSpaceInset::Write(FILE *outf)
  44.    if (space>=0 && space<6) {
  45.        LString output;
  46.        MathSpaceInset::Write(output);
  47.        fprintf(outf, "%s", output.c_str());
  48.    }
  49. }
  50.  
  51. void
  52. MathSpaceInset::Write(LString &outf)
  53.    if (space>=0 && space<6) {
  54.        outf += '\\';
  55.        outf += latex_mathspace[space];
  56.        outf += ' ';
  57.    }
  58. }
  59.  
  60.  
  61. void
  62. MathDotsInset::Write(FILE *outf)
  63. {
  64.    LString output;
  65.    MathDotsInset::Write(output);
  66.    fprintf(outf, "%s", output.c_str());
  67. }   
  68.  
  69. void
  70. MathDotsInset::Write(LString &outf)
  71. {
  72.    outf += '\\';
  73.    outf += name;
  74.    outf += ' ';
  75. }   
  76.  
  77.  
  78. void MathSqrtInset::Write(FILE *outf)
  79.    LString output;
  80.    MathSqrtInset::Write(output);  
  81.    fprintf(outf, "%s", output.c_str());
  82. }
  83.  
  84. void MathSqrtInset::Write(LString &outf)
  85.    outf += '\\';
  86.    outf += name;
  87.    outf += '{';
  88.    MathParInset::Write(outf);  
  89.    outf += '}';
  90. }
  91.  
  92.  
  93. void MathDelimInset::Write(FILE *outf)
  94.     LString output;
  95.     MathDelimInset::Write(output);
  96.     fprintf(outf, "%s", output.c_str());
  97. }
  98.  
  99. void MathDelimInset::Write(LString &outf)
  100.     latexkeys* l = (left != '|') ? lm_get_key_by_id(left, LM_TK_SYM): 0;
  101.     latexkeys* r = (right != '|') ? lm_get_key_by_id(right, LM_TK_SYM): 0;
  102.     outf += "\\left";
  103.     if (l) {
  104.         outf += '\\';
  105.     outf += l->name;
  106.     outf += ' ';
  107.     } else {
  108.         if (left=='{' || left=='}') {
  109.         outf += '\\';
  110.         outf += (char) left;
  111.         outf += ' ';
  112.         } else {
  113.         outf += (char) left;
  114.         outf += ' ';
  115.         }
  116.     }
  117.    MathParInset::Write(outf);  
  118.    outf += "\\right";
  119.    if (r) {
  120.        outf += '\\';
  121.        outf += r->name;
  122.        outf += ' ';
  123.    } else {
  124.        if (right=='{' || right=='}') {
  125.        outf += '\\';
  126.        outf += (char) right;
  127.        outf += ' ';
  128.       } else {
  129.        outf += (char) right;
  130.        outf += ' ';
  131.       }
  132.    }        
  133. }
  134.  
  135.  
  136. void MathDecorationInset::Write(FILE *outf)
  137. {
  138.    LString output;
  139.    MathDecorationInset::Write(output);
  140.    fprintf(outf, "%s", output.c_str());
  141. }
  142.  
  143. void MathDecorationInset::Write(LString &outf)
  144.    latexkeys* l = lm_get_key_by_id(deco, LM_TK_WIDE);
  145.    outf += '\\';
  146.    outf += l->name;
  147.    outf += '{';
  148.    MathParInset::Write(outf);  
  149.    outf += '}';
  150. }
  151.  
  152.  
  153. void MathAccentInset::Write(FILE *outf)
  154.     LString output;
  155.     MathAccentInset::Write(output);
  156.     fprintf(outf, "%s", output.c_str());
  157. }
  158.  
  159. void MathAccentInset::Write(LString &outf)
  160.     latexkeys* l = lm_get_key_by_id(code, LM_TK_ACCENT);
  161.     outf += '\\';
  162.     outf += l->name;
  163.     if (code!=LM_not)
  164.       outf += '{';
  165.     else
  166.       outf += ' ';
  167.  
  168.     if (inset) {
  169.       inset->Write(outf);
  170.     } else {
  171.       if (fn>=LM_TC_RM && fn<=LM_TC_TEXTRM) {
  172.         outf += '\\';
  173.         outf += math_font_name[fn-LM_TC_RM];
  174.         outf += '{';
  175.       }
  176.       if (MathIsSymbol(fn)) {
  177.           latexkeys *l = lm_get_key_by_id(c, LM_TK_SYM);
  178.           if (l) {
  179.         outf += '\\';
  180.         outf += l->name;
  181.         outf += ' ';
  182.           }
  183.       } else
  184.         outf += (char) c;
  185.  
  186.       if (fn>=LM_TC_RM && fn<=LM_TC_TEXTRM)
  187.         outf += '}';
  188.     }
  189.  
  190.     if (code!=LM_not)
  191.       outf += '}';
  192. }
  193.  
  194.  
  195. void MathBigopInset::Write(FILE *outf)
  196.    LString output;
  197.    MathBigopInset::Write(output);
  198.    fprintf(outf, "%s", output.c_str());
  199. }
  200.  
  201. void MathBigopInset::Write(LString &outf)
  202.     bool limp = GetLimits();
  203.     
  204.     outf += '\\';
  205.     outf += name;
  206.     
  207.     if (limp && !(sym!=LM_int && sym!=LM_oint && (GetStyle()==LM_ST_DISPLAY)))
  208.       outf += "\\limits ";
  209.     else 
  210.     if (!limp && (sym!=LM_int && sym!=LM_oint && (GetStyle()==LM_ST_DISPLAY)))
  211.     outf += "\\nolimits ";
  212.     else 
  213.       outf += ' ';
  214. }
  215.  
  216.  
  217. void MathFracInset::Write(FILE *outf)
  218.    LString output;
  219.    MathFracInset::Write(output);  
  220.    fprintf(outf, "%s", output.c_str());
  221. }
  222.  
  223. void MathFracInset::Write(LString &outf)
  224.    outf += '\\';
  225.    outf += name;
  226.    outf += '{';
  227.    MathParInset::Write(outf);  
  228.    outf += "}{";
  229.    den->Write(outf);  
  230.    outf += '}';
  231. }
  232.  
  233.  
  234. void MathParInset::Write(FILE *outf)
  235. {
  236.    if (!array) return;
  237.    LString output;
  238.    MathParInset::Write(output);
  239.    fprintf(outf, "%s", output.c_str());
  240. }
  241.  
  242.  
  243. void MathParInset::Write(LString &outf)
  244. {
  245.    if (!array) return;
  246.    int brace = 0;
  247.    latexkeys *l;
  248.    MathedIter data(array);
  249.    // hack
  250.    MathedRowSt const* crow = getRowSt();   
  251.    data.Reset();
  252.     
  253.    if (!Permit(LMPF_FIXED_SIZE)) { 
  254.        l = lm_get_key_by_id(size, LM_TK_STY);
  255.        if (l) {
  256.        outf += '\\';
  257.        outf += l->name;
  258.        outf += ' ';
  259.        }
  260.    }
  261.    while (data.OK()) {
  262.       byte cx = data.GetChar();
  263.       if (cx>=' ') {
  264.      int ls;
  265.      byte *s = data.GetString(ls);
  266.        
  267.      if (data.FCode()>=LM_TC_RM && data.FCode()<=LM_TC_TEXTRM) {
  268.          outf += '\\';
  269.          outf += math_font_name[data.FCode()-LM_TC_RM];
  270.          outf += '{';
  271.      }
  272.      while (ls>0) {
  273.         if (MathIsSymbol(data.FCode())) {
  274.         l = lm_get_key_by_id(*s,(data.FCode()==LM_TC_BSYM)?LM_TK_BIGSYM:LM_TK_SYM);
  275.            if (l) {
  276.          outf += '\\';
  277.          outf += l->name;
  278.          outf += ' ';
  279.            } else { 
  280.          fprintf(stderr, "Illegal symbol code[%u %d %d]", *s, ls, data.FCode());
  281.            }
  282.         } else {
  283.            // Is there a standard logical XOR?
  284.            if ((data.FCode()==LM_TC_TEX && *s!='{' && *s!='}') ||
  285.            (data.FCode()==LM_TC_SPECIAL))
  286.          outf += '\\';
  287.            else {
  288.           if (*s=='{') brace++;
  289.           if (*s=='}') brace--;
  290.            }
  291.            if (*s=='}' && data.FCode()==LM_TC_TEX && brace<0) 
  292.          fprintf(stderr, "Math warning: Unexpected closing brace.\n");
  293.            else           
  294.          outf += (char) *s;
  295.         }
  296.         s++; ls--;
  297.      }
  298.      if (data.FCode()>=LM_TC_RM && data.FCode()<=LM_TC_TEXTRM)
  299.        outf += '}';
  300.       } else     
  301.       if (MathIsInset(cx)) {
  302.      MathedInset *p = data.GetInset();
  303.      if (cx==LM_TC_UP)
  304.        outf += "^{";
  305.      if (cx==LM_TC_DOWN)
  306.        outf += "_{";
  307.      p->Write(outf);
  308.      if (cx==LM_TC_UP || cx==LM_TC_DOWN)
  309.        outf += '}';
  310.      data.Next();
  311.       } else
  312.           switch(cx) {
  313.      case LM_TC_TAB:
  314.         {
  315.            outf += " & ";
  316.            data.Next();
  317.            break;
  318.         }
  319.      case LM_TC_CR:
  320.         {
  321.         if (crow) {
  322.             if (!crow->isNumbered()) {  
  323.                 outf +="\\nonumber ";
  324.             }
  325.             if (crow->getLabel()) {
  326.                 outf += "\\label{";
  327.                 outf += crow->getLabel();
  328.             outf += "} ";
  329.             }
  330.             crow = crow->getNext();
  331.         }
  332.            outf += "\\\\\n";
  333.            number_of_newlines++;
  334.            data.Next();
  335.            break;
  336.         }
  337.      default:
  338.        fprintf(stderr, "WMath Error: unrecognized code[%d]", cx);
  339.        return;
  340.     }     
  341.    }
  342.     
  343.     if (crow) {
  344.     if (!crow->isNumbered()) {
  345.         outf +="\\nonumber ";
  346.     }
  347.     if (crow->getLabel()) {
  348.         outf += "\\label{";
  349.         outf += crow->getLabel();
  350.         outf += "} ";
  351.     }
  352.     }
  353.    while (brace>0) {
  354.       outf += '}';
  355.       brace--;
  356.    }
  357. }
  358.  
  359.  
  360. void MathMatrixInset::Write(FILE *outf)
  361. {
  362.     LString output;
  363.     MathMatrixInset::Write(output);
  364.     fprintf(outf, "%s", output.c_str());
  365. }
  366.  
  367. void MathMatrixInset::Write(LString &outf)
  368. {
  369.     if (GetType() == LM_OT_MATRIX){
  370.     outf += "\\begin{";
  371.     outf += name;
  372.     outf += '}';
  373.     if (v_align=='t' || v_align=='b') {
  374.         outf += '[';
  375.         outf += (char) v_align;
  376.         outf += ']';
  377.     }
  378.     outf += '{';
  379.     outf += h_align;
  380.     outf += "}\n";
  381.     number_of_newlines++;
  382.     }
  383.     MathParInset::Write(outf);
  384.     if (GetType() == LM_OT_MATRIX){
  385.     outf += "\n\\end{";
  386.     outf += name;
  387.     outf += '}';
  388.     number_of_newlines++;
  389.     }
  390. }
  391.  
  392.  
  393. void mathed_write(MathParInset* p,  FILE* outf, int* newlines,  char fragile, char const* label)
  394. {
  395.    LString output;
  396.    mathed_write(p, output, newlines, fragile, label);
  397.    fprintf(outf, "%s", output.c_str());
  398. }
  399.  
  400. void mathed_write(MathParInset* p, LString& outf, int* newlines,
  401.                   char fragile, char const* label)
  402. {  
  403.    number_of_newlines = 0;
  404.    short mathed_env = p->GetType();
  405.  
  406.    if (mathed_env==LM_EN_INTEXT) {
  407.      if (fragile) outf += "\\protect";
  408.      outf += "\\( "; // changed from " \\( " (Albrecht Dress)
  409.    }
  410.    else if (mathed_env==LM_EN_DISPLAY){
  411.      outf += "\n\\[\n";
  412.      number_of_newlines+=2;
  413.    }
  414.    else {
  415.      outf += "\n\\begin{";
  416.      outf += latex_mathenv[mathed_env];
  417.      outf += "}\n";
  418.      number_of_newlines+=2;
  419.    }
  420.    
  421.    if (label && label[0]>' ' && mathed_env==LM_EN_EQUATION){
  422.      outf += "\\label{";
  423.      outf += label;
  424.      outf += "}\n";
  425.      number_of_newlines++;
  426.    }
  427.  
  428.    p->Write(outf);
  429.    
  430.    if (mathed_env==LM_EN_INTEXT){
  431.      if (fragile) outf += "\\protect";
  432.      outf += " \\)";
  433.    }
  434.    else if (mathed_env==LM_EN_DISPLAY){
  435.      outf += "\\]\n";
  436.      number_of_newlines++;
  437.    }
  438.    else {
  439.      outf += "\n\\end{";
  440.      outf += latex_mathenv[mathed_env];
  441.      outf += "}\n";
  442.      number_of_newlines+=2;
  443.    }
  444.    *newlines = number_of_newlines;
  445. }
  446.