home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / utils / rtfprsr / trf_char.c < prev    next >
C/C++ Source or Header  |  1995-05-18  |  9KB  |  365 lines

  1. /*
  2.     trf-charmap.c - Unnecessarily complex routines for mapping special
  3.     characters in the non-ASCII range (128..255) onto troff equivalents.
  4.  
  5.     This file may require modification for local troff versions.
  6.  
  7.     Tries to handle the four different character sets allowed in the
  8.     RTF specification (ANSI, Macintosh, PC and PCA).  That part in itself
  9.     is easy, but the complexity stems from trying to allow for local
  10.     variations in troff versions (not all troff's understand the same
  11.     set of special characters) and for particular macro packages (-me,
  12.     -me, -ms).  The last one is silly at this point since no other part
  13.     of rtf2troff knows anything about writing macro-package-specific
  14.     output.  Maybe someday.
  15.  
  16.     SelectFormatterMaps () - selects character maps specific to the given
  17.         formatter, e.g., xroff, pstroff, eroff.
  18.     SelectMacPackMaps () - select override maps for the given macro
  19.         package.
  20.     SelectCharSetMaps () - select maps for particular character set.
  21.     CharMapping () - return troff equivalent for a special character.
  22. */
  23.  
  24. # include    <stdio.h>
  25. # include    <sys/types.h>
  26. # include    "rtf.h"
  27. # include    "rtf2troff.h"
  28.  
  29.  
  30. /*
  31.     Structure for mapping character values >= 128 to troff strings
  32.     for different character sets.
  33.  
  34.     The reason char value/string pairs are used instead of a 128-entry
  35.     table is that while the latter would be faster, it's much easier
  36.     to build an incomplete table with the former, and the entries don't
  37.     need to be in any particular order.
  38. */
  39.  
  40. typedef struct CharMap    CharMap;
  41.  
  42. struct CharMap
  43. {
  44.     int    charVal;
  45.     char    *charStr;
  46. };
  47.  
  48.  
  49. /*
  50.     Structure for associating default and override maps with
  51.     particular versions of troff.
  52. */
  53.  
  54. # define    ansiMap        0
  55. # define    macMap        1
  56. # define    pcMap        2
  57. # define    pcaMap        3
  58. # define    mapTypes    4
  59.  
  60. typedef struct CharMapTable    CharMapTable;
  61.  
  62. struct CharMapTable
  63. {
  64.     char    *formatter;
  65.     CharMap    *defCharMap[mapTypes];
  66.     CharMap    *meCharMap[mapTypes];
  67.     CharMap    *mmCharMap[mapTypes];
  68.     CharMap    *msCharMap[mapTypes];
  69. };
  70.  
  71.  
  72. /*
  73.     ANSI char map is taken from Windows 3.0 and Word for
  74.     Windows 1.1 manuals.  Note that these conflict, so there
  75.     are some guesses.
  76.     PC char map is taken from Windows 3.0 manual.
  77. */
  78.  
  79. static CharMap    ansiCharMap [] =
  80. {
  81.     0x91,    "`",        /* left curly single quote */
  82.     0x92,    "'",        /* right curly single quote */
  83.     0x93,    "``",        /* left curly double quote */
  84.     0x94,    "''",        /* right curly double quote */
  85.     0x95,    "o",        /* ?? */
  86.     0xa2,    "\\(ct",    /* cent */
  87.     0xa6,    "|",        /* vert. bar */
  88.     0xa7,    "\\(sc",    /* section */
  89.     0xa9,    "\\(co",    /* copyright */
  90.     0xab,    "<<",        /* alternate quote */
  91.     0xac,    "\\(no",    /* not */
  92.     0xad,    "\\(hy",    /* short dash */
  93.     0xae,    "\\(rg",    /* registered */
  94.     0xaf,    "\\(em",    /* long dash */
  95.     0xb0,    "\\(de",    /* degree */
  96.     0xb1,    "\\(+-",    /* plus or minus */
  97.     0xb4,    "\\(aa",    /* acute accent */
  98.     0xb5,    "\\(*m",    /* micro (use greek mu) */
  99.     0xbb,    ">>",        /* alternate end-quote*/
  100.     0xbc,    "\\(14",    /* one-fourth */
  101.     0xbd,    "\\(12",    /* one-half */
  102.     0xbe,    "\\(34",    /* three-fourths */
  103.     0xc6,    "AE",        /* joined A-E */
  104.     0xd7,    "\\(mu",    /* math multiply */
  105.     0xdf,    "\\(*b",    /* German B? (fake with greek beta) */
  106.     0xe6,    "ae",        /* joined a-e */
  107.     0xf7,    "\\(di",    /* divide */
  108.     0,    NULL
  109. };
  110.  
  111.  
  112. static CharMap    macCharMap [] =
  113. {
  114.     0xa0,    "\\(dg",    /* dagger */
  115.     0xa1,    "\\(de",    /* degree */
  116.     0xa2,    "\\(ct",    /* cent */
  117.     0xa4,    "\\(sc",    /* section */
  118.     0xa5,    "\\(bu",    /* bullet */
  119.     0xa7,    "\\(*b",    /* German B? (fake with greek beta) */
  120.     0xa8,    "\\(rg",    /* registered */
  121.     0xa9,    "\\(co",    /* copyright */
  122.     0xaa,    "(TM)",        /* trademark */
  123.     0xab,    "\\(aa",    /* acute accent */
  124.     0xad,    "\\(!=",    /* not equal */
  125.     0xae,    "AE",        /* joined A-E */
  126.     0xb0,    "\\(if",    /* infinity */
  127.     0xb1,    "\\(+-",    /* plus or minus */
  128.     0xb2,    "\\(<=",    /* less than or equal */
  129.     0xb3,    "\\(>=",    /* greater than or equal */
  130.     0xb5,    "\\(*m",    /* micro (use greek mu) */
  131.     0xb6,    "\\(*d",    /* delta */
  132.     0xb7,    "\\(*S",    /* summation (Sigma) */
  133.     0xb8,    "\\(*P",    /* Pi */
  134.     0xb9,    "\\(*p",    /* pi */
  135.     0xba,    "\\(is",    /* integral sign */
  136.     0xbd,    "\\(*W",    /* ohm-sign (greek Omega) */
  137.     0xbe,    "ae",        /* joined a-e */
  138.     0xc2,    "\\(no",    /* not */
  139.     0xc3,    "\\(sr",    /* square root */
  140.     0xc5,    "\\(ap",    /* approximately */
  141.     0xc6,    "\\(*D",    /* Delta */
  142.     0xc7,    "<<",        /* alternate quote */
  143.     0xc8,    ">>",        /* alternate end-quote*/
  144.     0xc9,    "...",        /* ellipsis */
  145.     0xca,    "\\ ",        /* unbreakable space */
  146.     0xd0,    "\\(hy",    /* short dash */
  147.     0xd1,    "\\(em",    /* long dash */
  148.     0xd2,    "``",        /* left curly double quote */
  149.     0xd3,    "''",        /* right curly double quote */
  150.     0xd4,    "`",        /* left curly single quote */
  151.     0xd5,    "'",        /* right curly single quote */
  152.     0xd6,    "\\(di",    /* divide */
  153.     0,    NULL
  154. };
  155.  
  156.  
  157. static CharMap    pcCharMap [] =
  158. {
  159.     0x91,    "ae",        /* joined a-e */
  160.     0x9b,    "\\(ct",    /* cent */
  161.     0xa9,    "\\(em",    /* long dash */
  162.     0xaa,    "\\(no",    /* not */
  163.     0xab,    "\\(12",    /* one-half */
  164.     0xac,    "\\(14",    /* one-fourth */
  165.     0xae,    "<<",        /* alternate quote */
  166.     0xaf,    ">>",        /* alternate end-quote*/
  167.     0xf1,    "\\(+-",    /* plus or minus */
  168.     0xf8,    "\\(de",    /* degree */
  169.     0,    NULL
  170. };
  171.  
  172.  
  173. static CharMap    pcaCharMap [] =
  174. {
  175.     0,    NULL
  176. };
  177.  
  178.  
  179. /*
  180.     These -me maps are just here for example
  181. */
  182.  
  183.  
  184. static CharMap    meAnsiCharMap [] =
  185. {
  186.     0,    NULL
  187. };
  188.  
  189.  
  190. static CharMap    meMacCharMap [] =
  191. {
  192.     0,    NULL
  193. };
  194.  
  195.  
  196. static CharMap    mePcCharMap [] =
  197. {
  198.     0,    NULL
  199. };
  200.  
  201.  
  202. static CharMap    mePcaCharMap [] =
  203. {
  204.     0,    NULL
  205. };
  206.  
  207.  
  208. static CharMapTable charMapTable [] =
  209. {
  210.     {
  211.     "troff",
  212.     { ansiCharMap, macCharMap, pcCharMap, pcaCharMap },    /* defaults */
  213.     { NULL, NULL, NULL, NULL },            /* -me overrides */
  214.     { NULL, NULL, NULL, NULL },            /* -mm overrides */
  215.     { NULL, NULL, NULL, NULL }            /* -ms overrides */
  216.     },
  217.     {
  218.     "xroff",
  219.     { ansiCharMap, macCharMap, pcCharMap, pcaCharMap },    /* defaults */
  220.     { NULL, NULL, NULL, NULL },            /* -me overrides */
  221.     { NULL, NULL, NULL, NULL },            /* -mm overrides */
  222.     { NULL, NULL, NULL, NULL }            /* -ms overrides */
  223.     },
  224.     {
  225.     "pstroff",
  226.     { ansiCharMap, macCharMap, pcCharMap, pcaCharMap },    /* defaults */
  227.     { NULL, NULL, NULL, NULL },            /* -me overrides */
  228.     { NULL, NULL, NULL, NULL },            /* -mm overrides */
  229.     { NULL, NULL, NULL, NULL }            /* -ms overrides */
  230.     },
  231.     {
  232.     /*
  233.         This should be the last entry.  Do not change it.
  234.         Put new entries before this one.
  235.     */
  236.     NULL
  237.     }
  238. };
  239.  
  240.  
  241. /*
  242.     curCharMaps points to current entry in character map table.
  243.     macroMaps points to current set of override maps (default NULL
  244.     until SelectMacPackMaps() is called).  defaultMap points to the
  245.     default map within curCharMaps (default is the ANSI map).
  246.     overrideMap points to the map within the macro package override
  247.     maps (default is the ANSI map after SelectMacPackMaps() is called).
  248. */
  249.  
  250. static CharMapTable    *curCharMaps = &charMapTable[0];
  251. static CharMap        **macroMaps = NULL;
  252. static CharMap        *defaultMap = ansiCharMap;
  253. static CharMap        *overrideMap = NULL;
  254.  
  255.  
  256. void SelectFormatterMaps (name)
  257. char    *name;
  258. {
  259. CharMapTable    *cmtp;
  260.  
  261.     for (cmtp = charMapTable; cmtp->formatter != NULL; cmtp++)
  262.     {
  263.         if (strcmp (name, cmtp->formatter) == 0)
  264.         {
  265.             curCharMaps = cmtp;
  266.             macroMaps = NULL;
  267.             defaultMap = curCharMaps->defCharMap[ansiMap];
  268.             overrideMap = NULL;
  269.             return;
  270.         }
  271.     }
  272.     fprintf (stderr, "Unknown formatter: %s\n", name);
  273.     exit (1);
  274. }
  275.  
  276.  
  277. void SelectMacPackMaps (name)
  278. char    *name;
  279. {
  280.     if (strcmp (name, "-me") == 0)
  281.         macroMaps = curCharMaps->meCharMap;
  282.     else if (strcmp (name, "-mm") == 0)
  283.         macroMaps = curCharMaps->mmCharMap;
  284.     else if (strcmp (name, "-ms") == 0)
  285.         macroMaps = curCharMaps->msCharMap;
  286.     else
  287.     {
  288.         fprintf (stderr, "Unknown macro package: %s\n", name);
  289.         exit (1);
  290.     }
  291.     if (macroMaps != NULL)
  292.         overrideMap = macroMaps[ansiMap];
  293. }
  294.  
  295.  
  296. /*
  297.     mapSet should be an rtfCharSet minor number
  298. */
  299.  
  300. void SelectCharSetMaps (mapSet)
  301. int    mapSet;
  302. {
  303. int    mapIndex;
  304.  
  305.     switch (mapSet)
  306.     {
  307.     case rtfAnsiCharSet:
  308.         mapIndex = ansiMap;
  309.         break;
  310.     case rtfMacCharSet:
  311.         mapIndex = macMap;
  312.         break;
  313.     case rtfPcCharSet:
  314.         mapIndex = pcMap;
  315.         break;
  316.     case rtfPcaCharSet:
  317.         mapIndex = pcaMap;
  318.         break;
  319.     default:
  320.         fprintf (stderr, "Unknown charset: %d\n", mapSet);
  321.         exit (1);
  322.     }
  323.     defaultMap = curCharMaps->defCharMap[mapIndex];
  324.     if (macroMaps != NULL)
  325.         overrideMap = macroMaps[mapIndex];
  326. }
  327.  
  328.  
  329. /*
  330.     Figure out what string a special (non-ASCII) character should map
  331.     to.  (Determine troff equivalent using "normal" characters.)
  332. */
  333.  
  334. static char *_CharMapping (c, map)
  335. int    c;
  336. CharMap    *map;
  337. {
  338. CharMap    *cmp;
  339. char    *p = NULL;
  340.  
  341.     for (cmp = map; cmp->charStr != NULL; cmp++)
  342.     {
  343.         if (c == cmp->charVal)
  344.         {
  345.             p = cmp->charStr;
  346.             break;
  347.         }
  348.     }
  349.     return (p);
  350. }
  351.  
  352.  
  353. char *CharMapping (c)
  354. int    c;
  355. {
  356. CharMap    *cmp;
  357. char    *p = NULL;
  358.  
  359.     if (overrideMap != NULL)
  360.         p = _CharMapping (c, overrideMap);
  361.     if (p == NULL && defaultMap != NULL)
  362.         p = _CharMapping (c, defaultMap);
  363.     return (p != NULL ? p : "<<UNKNOWN>>");
  364. }
  365.