home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / utils / rtfprsr / trfstack.c < prev   
Text File  |  1995-05-18  |  6KB  |  264 lines

  1. /*
  2.     trf-stack.c - internal and written stack operations.
  3. */
  4.  
  5. # include    <stdio.h>
  6. # include    <sys/types.h>
  7. # include    "rtf.h"
  8. # include    "rtf2troff.h"
  9.  
  10.  
  11. static int    iTopState = 0;        /* current internal state */
  12. static State    iState[maxIStack] =    /* internal state stack */
  13. {
  14. /*
  15.     state 0 = initial state.
  16. */
  17.         rtfNoDestination,    /* destination */
  18.     {
  19.         0,            /* landscape (0 = no) */
  20.         12240.0/rtfTpi,        /* paper width = 8.5i*/
  21.         15840.0/rtfTpi,        /* paper height = 11i */
  22.         1800.0/rtfTpi,        /* left margin = 1.25i */
  23.         1800.0/rtfTpi,        /* right margin = 1.25i */
  24.         1440.0/rtfTpi,        /* top margin = 1i */
  25.         1440.0/rtfTpi,        /* bottom margin = 1i */
  26.         720.0/rtfTpi        /* tab width = .5i */
  27.     },
  28.     {
  29.         rtfPageBreak,        /* section break type */
  30.         1,            /* starting page number */
  31.         0,            /* continuous page numbering */
  32.         1080.0/rtfTpi,        /* header position */
  33.         1080.0/rtfTpi,        /* footer position */
  34.         0            /* title page not special */
  35.     },
  36.     {
  37.         0,            /* first indent */
  38.         0,            /* left indent */
  39.         0,            /* right indent */
  40.         0,            /* space before */
  41.         0,            /* space after */
  42.         .2,            /* space between: 12p * 1.2 = 14.4p */
  43.         0,            /* no tabs set yet */
  44.         0,            /* number of tabs */
  45.         { 0 },            /* tab positions */
  46.         { 0 },            /* tab types */
  47.         rtfLeaderMotion,    /* tab character */
  48.         rtfQuadLeft,        /* justification */
  49.         rtfNoBorderType,    /* no border */
  50.         0            /* draw border nowhere */
  51.     },
  52.     {
  53.         12,            /* font size */
  54.         0,            /* char style (plain, DON'T CHANGE) */
  55.         0,            /* superscript */
  56.         0            /* subscript */
  57.     }
  58. };
  59.  
  60.  
  61. static int    wTopState = 0;        /* current written state */
  62. static State    wState[maxWStack];    /* written state stack */
  63.  
  64.  
  65. /*
  66.     Set up pointers into internal state 0, and equate initial written
  67.     state to internal state (although written state isn't actually
  68.     written until FlushInitialState()).
  69.  
  70.     Initialize the tab type array to left tabs, so that any tab
  71.     positions specified without a type will default to left-justified.
  72. */
  73.  
  74. void InitState ()
  75. {
  76.     is = &iState[0];
  77.     /* initialize state 0 */
  78.     ids = &is->docState;
  79.     iss = &is->sectState;
  80.     ips = &is->parState;
  81.     ics = &is->charState;
  82.     ips->tabFlag = 0;
  83.     InitTabSet ();
  84.     /* sync written state to internal state */
  85.     bcopy ((char *) &iState[0], (char *) &wState[0], (int) sizeof (State));
  86.     ws = &wState[0];
  87.     wds = &ws->docState;
  88.     wss = &ws->sectState;
  89.     wps = &ws->parState;
  90.     wcs = &ws->charState;
  91. }
  92.  
  93.  
  94. void CheckFinalState ()
  95. {
  96.     if (iTopState != 0)
  97.         fprintf (stderr, "Warning: unbalanced brace level\n");
  98.     if (wTopState != 0)
  99.         fprintf (stderr, "Warning: unrestored environment\n");
  100.     if (indirectionLevel > 0)
  101.         fprintf (stderr, "Warning: unrestored indirection\n");
  102. }
  103.  
  104.  
  105. /*
  106.     Push or pop internal state.
  107.  
  108.     On push, initial value of new state is same as current state, so
  109.     no state *change* is involved.  Indicate that no destination or
  110.     tab stops have been specified.
  111.  
  112.     On pop, revert to previous state.  It's just laziness to set the
  113.     state change variables on a state pop, since some or all of them
  114.     may well not have changed at all... but it's safest and easiest
  115.     to do so.
  116. */
  117.  
  118.  
  119. void PushIState ()
  120. {
  121.     if (iTopState >= maxIStack - 1)
  122.     {
  123.         fprintf (stderr, "Internal state stack limit exceeded");
  124.         fprintf (stderr, " maximum level (%d)\n", maxIStack);
  125.         exit (1);
  126.     }
  127.     bcopy ((char *) &iState[iTopState],
  128.         (char *) &iState[iTopState + 1], (int) sizeof (State));
  129.     is = &iState[++iTopState];
  130.     is->destination = rtfNoDestination;
  131.     is->parState.tabFlag = 0;    /* no tabs set in state yet */
  132.     ids = &is->docState;
  133.     iss = &is->sectState;
  134.     ips = &is->parState;
  135.     ics = &is->charState;
  136. }
  137.  
  138.  
  139. void PopIState ()
  140. {
  141.     if (iTopState < 1)
  142.     {
  143.         fprintf (stderr, "Pop error: no internal state to pop");
  144.         fprintf (stderr, " maximum level (%d)\n", maxIStack);
  145.         exit (1);
  146.     }
  147.     is = &iState[--iTopState];
  148.     ids = &is->docState;
  149.     iss = &is->sectState;
  150.     ips = &is->parState;
  151.     ics = &is->charState;
  152.     ++docStateChanged;
  153.     ++sectStateChanged;
  154.     ++parStateChanged;
  155.     ++charStateChanged;
  156. }
  157.  
  158.  
  159. void PushWState ()
  160. {
  161.     if (wTopState >= maxWStack - 1)
  162.     {
  163.         fprintf (stderr, "Written state stack limit exceeded");
  164.         fprintf (stderr, " maximum level (%d)\n", maxWStack);
  165.         exit (1);
  166.     }
  167.     bcopy ((char *) &wState[wTopState],
  168.         (char *) &wState[wTopState + 1], (int) sizeof (State));
  169.     ws = &wState[++wTopState];
  170.     wds = &ws->docState;
  171.     wss = &ws->sectState;
  172.     wps = &ws->parState;
  173.     wcs = &ws->charState;
  174. }
  175.  
  176.  
  177. void PopWState ()
  178. {
  179.     if (wTopState < 1)
  180.     {
  181.         fprintf (stderr, "Pop error: no written state to pop");
  182.         fprintf (stderr, " maximum level (%d)\n", maxWStack);
  183.         exit (1);
  184.     }
  185.     ws = &wState[--wTopState];
  186.     wds = &ws->docState;
  187.     wss = &ws->sectState;
  188.     wps = &ws->parState;
  189.     wcs = &ws->charState;
  190.     ++docStateChanged;
  191.     ++sectStateChanged;
  192.     ++parStateChanged;
  193.     ++charStateChanged;
  194. }
  195.  
  196.  
  197. /*
  198.     Environment switching routines.  When commands are written
  199.     to switch environments, take snapshot of current written state.
  200.     When environment switches back, restore to snapshot state to
  201.     reflect the troff state switch.
  202.  
  203.     Environment switches are saved only when diversions are collected,
  204.     not when they are written out.
  205. */
  206.  
  207.  
  208. void BeginDiversion (name)
  209. char    *name;
  210. {
  211.     Flush ();
  212.     fprintf (f, ".rm %s\n.di %s\n", name, name);
  213.     ++indirectionLevel;
  214.     fprintf (f, ".ev 1\n");
  215.     PushWState ();
  216. }
  217.  
  218.  
  219. void EndDiversion ()
  220. {
  221.     Flush ();
  222.     fprintf (f, ".br\n");
  223.     fprintf (f, ".ev\n");
  224.     PopWState ();
  225.     fprintf (f, ".di\n");
  226.     --indirectionLevel;
  227. }
  228.  
  229.  
  230. /*
  231.     Restore section, paragraph or character defaults, using
  232.     values stored in state 0.
  233.  
  234.     Paragraph defaults are restored by using the state 0 values,
  235.     they applying the "Normal" style (style 0).  The tab flag is reset
  236.     before expanding the style so any inherited tabs will be overridden
  237.     by tabs in the style, and reset after expansion so any tabs in the
  238.     paragraph itself will override inherited or style tabs.
  239. */
  240.  
  241.  
  242. void RestoreSectDefaults ()
  243. {
  244.     bcopy ((char *) &iState[0].sectState, (char *) iss,
  245.                         (int) sizeof (SectState));
  246. }
  247.  
  248.  
  249. void RestoreParDefaults ()
  250. {
  251.     bcopy ((char *) &iState[0].parState, (char *) ips,
  252.                     (int) sizeof (ParState));
  253.     ips->tabFlag = 0;
  254.     RTFExpandStyle (0);
  255.     ips->tabFlag = 0;
  256. }
  257.  
  258.  
  259. void RestoreCharDefaults ()
  260. {
  261.     bcopy ((char *) &iState[0].charState, (char *) ics,
  262.                         (int) sizeof (CharState));
  263. }
  264.