Merge the MDI and common window creation code. Change the way MDI
[wine] / dlls / richedit / reader.c
1 /*
2  * - Need to document error code meanings.
3  * - Need to do something with \* on destinations.
4  * - Make the parameter a long?
5  *
6  * reader.c - RTF file reader.  Release 1.10.
7  *
8  * ASCII 10 (\n) and 13 (\r) are ignored and silently discarded.
9  * Nulls are also discarded.
10  * (although the read hook will still get a look at them.)
11  *
12  * "\:" is not a ":", it's a control symbol.  But some versions of
13  * Word seem to write "\:" for ":".  This reader treats "\:" as a
14  * plain text ":"
15  *
16  * 19 Mar 93
17  * - Add hack to skip "{\*\keycode ... }" group in stylesheet.
18  * This is probably the wrong thing to do, but it's simple.
19  * 13 Jul 93
20  * - Add THINK C awareness to malloc() declaration.  Necessary so
21  * compiler knows the malloc argument is 4 bytes.  Ugh.
22  * 07 Sep 93
23  * - Text characters are mapped onto standard codes, which are placed
24  * in rtfMinor.
25  * - Eliminated use of index() function.
26  * 05 Mar 94
27  * - Added zillions of new symbols (those defined in RTF spec 1.2).
28  * 14 Mar 94
29  * - Public functions RTFMsg() and RTFPanic() now take variable arguments.
30  * This means RTFPanic() now is used in place of what was formerly the
31  * internal function Error().
32  * - 8-bit characters are now legal, so they're not converted to \'xx
33  * hex char representation now.
34  * 01 Apr 94
35  * - Added public variables rtfLineNum and rtfLinePos.
36  * - #include string.h or strings.h, avoiding strncmp() problem where
37  * last argument is treated as zero when prototype isn't available.
38  * 04 Apr 94
39  * - Treat style numbers 222 and 0 properly as "no style" and "normal".
40  *
41  * Author: Paul DuBois  dubois@primate.wisc.edu
42  *
43  * This software may be redistributed without restriction and used for
44  * any purpose whatsoever.
45  */
46
47 # include       <stdio.h>
48 # include       <ctype.h>
49 # include       <string.h>
50 # include       <stdarg.h>
51
52 # include       "rtf.h"
53
54 /*
55  *  include hard coded charsets
56  */
57
58 #include "ansi_gen.h"
59 #include "ansi_sym.h"
60 #include "text_map.h"
61
62 #include <stdlib.h>
63
64 #include "charlist.h"
65 #include "windef.h"
66 #include "winbase.h"
67 #include "wine/debug.h"
68
69 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
70
71 extern HANDLE RICHED32_hHeap;
72
73 /*
74  * Return pointer to new element of type t, or NULL
75  * if no memory available.
76  */
77
78 # define        New(t)  ((t *) RTFAlloc ((int) sizeof (t)))
79
80 /* maximum number of character values representable in a byte */
81
82 # define        charSetSize             256
83
84 /* charset stack size */
85
86 # define        maxCSStack              10
87
88 static int      _RTFGetChar();
89 static void     _RTFGetToken ();
90 static void     _RTFGetToken2 ();
91 static int      GetChar ();
92 static void     ReadFontTbl ();
93 static void     ReadColorTbl ();
94 static void     ReadStyleSheet ();
95 static void     ReadInfoGroup ();
96 static void     ReadPictGroup ();
97 static void     ReadObjGroup ();
98 static void     LookupInit ();
99 static void     Lookup ();
100 static int      Hash ();
101
102 static void     CharSetInit ();
103 static void     ReadCharSetMaps ();
104
105
106 /*
107  * Public variables (listed in rtf.h)
108  */
109
110 int     rtfClass;
111 int     rtfMajor;
112 int     rtfMinor;
113 int     rtfParam;
114 int     rtfFormat;
115 char    *rtfTextBuf = (char *) NULL;
116 int     rtfTextLen;
117
118 long    rtfLineNum;
119 int     rtfLinePos;
120
121
122 /*
123  * Private stuff
124  */
125
126 static int      pushedChar;     /* pushback char if read too far */
127
128 static int      pushedClass;    /* pushed token info for RTFUngetToken() */
129 static int      pushedMajor;
130 static int      pushedMinor;
131 static int      pushedParam;
132 static char     *pushedTextBuf = (char *) NULL;
133
134 static int      prevChar;
135 static int      bumpLine;
136
137 static RTFFont  *fontList = (RTFFont *) NULL;   /* these lists MUST be */
138 static RTFColor *colorList = (RTFColor *) NULL; /* initialized to NULL */
139 static RTFStyle *styleList = (RTFStyle *) NULL;
140
141 static char     *inputName = (char *) NULL;
142 static char     *outputName = (char *) NULL;
143
144 static EDITSTREAM editstream;
145 static CHARLIST inputCharList = {0, NULL, NULL};
146
147 /*
148  * This array is used to map standard character names onto their numeric codes.
149  * The position of the name within the array is the code.
150  * stdcharnames.h is generated in the ../h directory.
151  */
152
153 #include "stdcharnames.h"
154
155 /*
156  * These arrays are used to map RTF input character values onto the standard
157  * character names represented by the values.  Input character values are
158  * used as indices into the arrays to produce standard character codes.
159  */
160
161
162 static char     *genCharSetFile = (char *) NULL;
163 static int      genCharCode[charSetSize];       /* general */
164 static int      haveGenCharSet = 0;
165
166 static char     *symCharSetFile = (char *) NULL;
167 static int      symCharCode[charSetSize];       /* symbol */
168 static int      haveSymCharSet = 0;
169
170 static int      curCharSet = rtfCSGeneral;
171 static int      *curCharCode = genCharCode;
172
173 /*
174  * By default, the reader is configured to handle charset mapping invisibly,
175  * including reading the charset files and switching charset maps as necessary
176  * for Symbol font.
177  */
178
179 static int      autoCharSetFlags;
180
181 /*
182  * Stack for keeping track of charset map on group begin/end.  This is
183  * necessary because group termination reverts the font to the previous
184  * value, which may implicitly change it.
185  */
186
187 static int      csStack[maxCSStack];
188 static int      csTop = 0;
189
190 /*
191  * Get a char from the charlist. The charlist is used to store characters
192  * from the editstream.
193  *
194  */
195
196 int _RTFGetChar(void)
197 {
198     char myChar;
199
200     TRACE("\n");
201
202     if(CHARLIST_GetNbItems(&inputCharList) == 0)
203     {
204         char buff[10];
205         long pcb;
206         editstream.pfnCallback(editstream.dwCookie, buff, 1, &pcb);
207         if(pcb == 0)
208            return EOF;
209         else
210            CHARLIST_Enqueue(&inputCharList, buff[0]);
211     }
212     myChar = CHARLIST_Dequeue(&inputCharList);
213     return (int) myChar;
214 }
215
216 void RTFSetEditStream(EDITSTREAM *es)
217 {
218     TRACE("\n");
219
220     editstream.dwCookie = es->dwCookie;
221     editstream.dwError  = es->dwError;
222     editstream.pfnCallback = es->pfnCallback;
223 }
224
225 /*
226  * Initialize the reader.  This may be called multiple times,
227  * to read multiple files.  The only thing not reset is the input
228  * stream; that must be done with RTFSetStream().
229  */
230
231 void RTFInit(void)
232 {
233 int     i;
234 RTFColor        *cp;
235 RTFFont         *fp;
236 RTFStyle        *sp;
237 RTFStyleElt     *eltList, *ep;
238
239     TRACE("\n");
240
241         if (rtfTextBuf == (char *) NULL)        /* initialize the text buffers */
242         {
243                 rtfTextBuf = RTFAlloc (rtfBufSiz);
244                 pushedTextBuf = RTFAlloc (rtfBufSiz);
245                 if (rtfTextBuf == (char *) NULL
246                         || pushedTextBuf == (char *) NULL)
247                         RTFPanic ("Cannot allocate text buffers.");
248                 rtfTextBuf[0] = pushedTextBuf[0] = '\0';
249         }
250
251         RTFFree (inputName);
252         RTFFree (outputName);
253         inputName = outputName = (char *) NULL;
254
255         /* initialize lookup table */
256         LookupInit ();
257
258         for (i = 0; i < rtfMaxClass; i++)
259                 RTFSetClassCallback (i, (RTFFuncPtr) NULL);
260         for (i = 0; i < rtfMaxDestination; i++)
261                 RTFSetDestinationCallback (i, (RTFFuncPtr) NULL);
262
263         /* install built-in destination readers */
264         RTFSetDestinationCallback (rtfFontTbl, ReadFontTbl);
265         RTFSetDestinationCallback (rtfColorTbl, ReadColorTbl);
266         RTFSetDestinationCallback (rtfStyleSheet, ReadStyleSheet);
267         RTFSetDestinationCallback (rtfInfo, ReadInfoGroup);
268         RTFSetDestinationCallback (rtfPict, ReadPictGroup);
269         RTFSetDestinationCallback (rtfObject, ReadObjGroup);
270
271
272         RTFSetReadHook ((RTFFuncPtr) NULL);
273
274         /* dump old lists if necessary */
275
276         while (fontList != (RTFFont *) NULL)
277         {
278                 fp = fontList->rtfNextFont;
279                 RTFFree (fontList->rtfFName);
280                 RTFFree ((char *) fontList);
281                 fontList = fp;
282         }
283         while (colorList != (RTFColor *) NULL)
284         {
285                 cp = colorList->rtfNextColor;
286                 RTFFree ((char *) colorList);
287                 colorList = cp;
288         }
289         while (styleList != (RTFStyle *) NULL)
290         {
291                 sp = styleList->rtfNextStyle;
292                 eltList = styleList->rtfSSEList;
293                 while (eltList != (RTFStyleElt *) NULL)
294                 {
295                         ep = eltList->rtfNextSE;
296                         RTFFree (eltList->rtfSEText);
297                         RTFFree ((char *) eltList);
298                         eltList = ep;
299                 }
300                 RTFFree (styleList->rtfSName);
301                 RTFFree ((char *) styleList);
302                 styleList = sp;
303         }
304
305         rtfClass = -1;
306         pushedClass = -1;
307         pushedChar = EOF;
308
309         rtfLineNum = 0;
310         rtfLinePos = 0;
311         prevChar = EOF;
312         bumpLine = 0;
313
314         CharSetInit ();
315         csTop = 0;
316 }
317
318 /*
319  * Set or get the input or output file name.  These are never guaranteed
320  * to be accurate, only insofar as the calling program makes them so.
321  */
322
323 void RTFSetInputName(char *name)
324 {
325     TRACE("\n");
326
327         if ((inputName = RTFStrSave (name)) == (char *) NULL)
328                 RTFPanic ("RTFSetInputName: out of memory");
329 }
330
331
332 char *RTFGetInputName(void)
333 {
334         return (inputName);
335 }
336
337
338 void RTFSetOutputName(char *name)
339 {
340     TRACE("\n");
341
342         if ((outputName = RTFStrSave (name)) == (char *) NULL)
343                 RTFPanic ("RTFSetOutputName: out of memory");
344 }
345
346
347 char *RTFGetOutputName(void)
348 {
349         return (outputName);
350 }
351
352
353
354 /* ---------------------------------------------------------------------- */
355
356 /*
357  * Callback table manipulation routines
358  */
359
360
361 /*
362  * Install or return a writer callback for a token class
363  */
364
365
366 static RTFFuncPtr       ccb[rtfMaxClass];               /* class callbacks */
367
368
369 void RTFSetClassCallback(int class, RTFFuncPtr callback)
370 {
371         if (class >= 0 && class < rtfMaxClass)
372                 ccb[class] = callback;
373 }
374
375
376 RTFFuncPtr RTFGetClassCallback(int class)
377 {
378         if (class >= 0 && class < rtfMaxClass)
379                 return (ccb[class]);
380         return ((RTFFuncPtr) NULL);
381 }
382
383
384 /*
385  * Install or return a writer callback for a destination type
386  */
387
388 static RTFFuncPtr       dcb[rtfMaxDestination]; /* destination callbacks */
389
390
391 void RTFSetDestinationCallback(int dest, RTFFuncPtr callback)
392 {
393         if (dest >= 0 && dest < rtfMaxDestination)
394                 dcb[dest] = callback;
395 }
396
397
398 RTFFuncPtr RTFGetDestinationCallback(int dest)
399 {
400         if (dest >= 0 && dest < rtfMaxDestination)
401                 return (dcb[dest]);
402         return ((RTFFuncPtr) NULL);
403 }
404
405
406 /* ---------------------------------------------------------------------- */
407
408 /*
409  * Token reading routines
410  */
411
412
413 /*
414  * Read the input stream, invoking the writer's callbacks
415  * where appropriate.
416  */
417
418 void RTFRead(void)
419 {
420         while (RTFGetToken () != rtfEOF)
421                 RTFRouteToken ();
422 }
423
424
425 /*
426  * Route a token.  If it's a destination for which a reader is
427  * installed, process the destination internally, otherwise
428  * pass the token to the writer's class callback.
429  */
430
431 void RTFRouteToken(void)
432 {
433 RTFFuncPtr      p;
434
435     TRACE("\n");
436
437         if (rtfClass < 0 || rtfClass >= rtfMaxClass)    /* watchdog */
438         {
439                 RTFPanic ("Unknown class %d: %s (reader malfunction)",
440                                                         rtfClass, rtfTextBuf);
441         }
442         if (RTFCheckCM (rtfControl, rtfDestination))
443         {
444                 /* invoke destination-specific callback if there is one */
445                 if ((p = RTFGetDestinationCallback (rtfMinor))
446                                                         != (RTFFuncPtr) NULL)
447                 {
448                         (*p) ();
449                         return;
450                 }
451         }
452         /* invoke class callback if there is one */
453         if ((p = RTFGetClassCallback (rtfClass)) != (RTFFuncPtr) NULL)
454                 (*p) ();
455 }
456
457
458 /*
459  * Skip to the end of the current group.  When this returns,
460  * writers that maintain a state stack may want to call their
461  * state unstacker; global vars will still be set to the group's
462  * closing brace.
463  */
464
465 void RTFSkipGroup(void)
466 {
467 int     level = 1;
468     TRACE("\n");
469
470         while (RTFGetToken () != rtfEOF)
471         {
472                 if (rtfClass == rtfGroup)
473                 {
474                         if (rtfMajor == rtfBeginGroup)
475                                 ++level;
476                         else if (rtfMajor == rtfEndGroup)
477                         {
478                                 if (--level < 1)
479                                         break;  /* end of initial group */
480                         }
481                 }
482         }
483 }
484
485
486 /*
487  * Read one token.  Call the read hook if there is one.  The
488  * token class is the return value.  Returns rtfEOF when there
489  * are no more tokens.
490  */
491
492 int RTFGetToken(void)
493 {
494 RTFFuncPtr      p;
495     TRACE("\n");
496
497         for (;;)
498         {
499                 _RTFGetToken ();
500                 if ((p = RTFGetReadHook ()) != (RTFFuncPtr) NULL)
501                         (*p) ();        /* give read hook a look at token */
502
503                 /* Silently discard newlines, carriage returns, nulls.  */
504                 if (!(rtfClass == rtfText && rtfFormat != SF_TEXT
505                         && (rtfMajor == '\r' || rtfMajor == '\n' || rtfMajor == '\0')))
506                         break;
507         }
508         return (rtfClass);
509 }
510
511
512 /*
513  * Install or return a token reader hook.
514  */
515
516 static RTFFuncPtr       readHook;
517
518
519 void RTFSetReadHook(RTFFuncPtr f)
520 {
521         readHook = f;
522 }
523
524
525 RTFFuncPtr RTFGetReadHook(void)
526 {
527         return (readHook);
528 }
529
530
531 void RTFUngetToken(void)
532 {
533     TRACE("\n");
534
535         if (pushedClass >= 0)   /* there's already an ungotten token */
536                 RTFPanic ("cannot unget two tokens");
537         if (rtfClass < 0)
538                 RTFPanic ("no token to unget");
539         pushedClass = rtfClass;
540         pushedMajor = rtfMajor;
541         pushedMinor = rtfMinor;
542         pushedParam = rtfParam;
543         (void) strcpy (pushedTextBuf, rtfTextBuf);
544 }
545
546
547 int RTFPeekToken(void)
548 {
549         _RTFGetToken ();
550         RTFUngetToken ();
551         return (rtfClass);
552 }
553
554
555 static void _RTFGetToken(void)
556 {
557 RTFFont *fp;
558
559     TRACE("\n");
560
561         if (rtfFormat == SF_TEXT) {
562             rtfMajor = GetChar ();
563             rtfMinor = rtfSC_nothing;
564             rtfParam = rtfNoParam;
565             rtfTextBuf[rtfTextLen = 0] = '\0';
566             if (rtfMajor == EOF)
567                 rtfClass = rtfEOF;
568             else
569                 rtfClass = rtfText;
570             return;
571         }
572
573         /* first check for pushed token from RTFUngetToken() */
574
575         if (pushedClass >= 0)
576         {
577                 rtfClass = pushedClass;
578                 rtfMajor = pushedMajor;
579                 rtfMinor = pushedMinor;
580                 rtfParam = pushedParam;
581                 (void) strcpy (rtfTextBuf, pushedTextBuf);
582                 rtfTextLen = strlen (rtfTextBuf);
583                 pushedClass = -1;
584                 return;
585         }
586
587         /*
588          * Beyond this point, no token is ever seen twice, which is
589          * important, e.g., for making sure no "}" pops the font stack twice.
590          */
591
592         _RTFGetToken2 ();
593         if (rtfClass == rtfText)        /* map RTF char to standard code */
594                 rtfMinor = RTFMapChar (rtfMajor);
595
596         /*
597          * If auto-charset stuff is activated, see if anything needs doing,
598          * like reading the charset maps or switching between them.
599          */
600
601         if (autoCharSetFlags == 0)
602                 return;
603
604         if ((autoCharSetFlags & rtfReadCharSet)
605                 && RTFCheckCM (rtfControl, rtfCharSet))
606         {
607                 ReadCharSetMaps ();
608         }
609         else if ((autoCharSetFlags & rtfSwitchCharSet)
610                 && RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
611         {
612                 if ((fp = RTFGetFont (rtfParam)) != (RTFFont *) NULL)
613                 {
614                         if (strncmp (fp->rtfFName, "Symbol", 6) == 0)
615                                 curCharSet = rtfCSSymbol;
616                         else
617                                 curCharSet = rtfCSGeneral;
618                         RTFSetCharSet (curCharSet);
619                 }
620         }
621         else if ((autoCharSetFlags & rtfSwitchCharSet) && rtfClass == rtfGroup)
622         {
623                 switch (rtfMajor)
624                 {
625                 case rtfBeginGroup:
626                         if (csTop >= maxCSStack)
627                                 RTFPanic ("_RTFGetToken: stack overflow");
628                         csStack[csTop++] = curCharSet;
629                         break;
630                 case rtfEndGroup:
631                         if (csTop <= 0)
632                                 RTFPanic ("_RTFGetToken: stack underflow");
633                         curCharSet = csStack[--csTop];
634                         RTFSetCharSet (curCharSet);
635                         break;
636                 }
637         }
638 }
639
640
641 /* this shouldn't be called anywhere but from _RTFGetToken() */
642
643 static void _RTFGetToken2(void)
644 {
645 int     sign;
646 int     c;
647
648     TRACE("\n");
649
650         /* initialize token vars */
651
652         rtfClass = rtfUnknown;
653         rtfParam = rtfNoParam;
654         rtfTextBuf[rtfTextLen = 0] = '\0';
655
656         /* get first character, which may be a pushback from previous token */
657
658         if (pushedChar != EOF)
659         {
660                 c = pushedChar;
661                 rtfTextBuf[rtfTextLen++] = c;
662                 rtfTextBuf[rtfTextLen] = '\0';
663                 pushedChar = EOF;
664         }
665         else if ((c = GetChar ()) == EOF)
666         {
667                 rtfClass = rtfEOF;
668                 return;
669         }
670
671         if (c == '{')
672         {
673                 rtfClass = rtfGroup;
674                 rtfMajor = rtfBeginGroup;
675                 return;
676         }
677         if (c == '}')
678         {
679                 rtfClass = rtfGroup;
680                 rtfMajor = rtfEndGroup;
681                 return;
682         }
683         if (c != '\\')
684         {
685                 /*
686                  * Two possibilities here:
687                  * 1) ASCII 9, effectively like \tab control symbol
688                  * 2) literal text char
689                  */
690                 if (c == '\t')                  /* ASCII 9 */
691                 {
692                         rtfClass = rtfControl;
693                         rtfMajor = rtfSpecialChar;
694                         rtfMinor = rtfTab;
695                 }
696                 else
697                 {
698                         rtfClass = rtfText;
699                         rtfMajor = c;
700                 }
701                 return;
702         }
703         if ((c = GetChar ()) == EOF)
704         {
705                 /* early eof, whoops (class is rtfUnknown) */
706                 return;
707         }
708         if (!isalpha (c))
709         {
710                 /*
711                  * Three possibilities here:
712                  * 1) hex encoded text char, e.g., \'d5, \'d3
713                  * 2) special escaped text char, e.g., \{, \}
714                  * 3) control symbol, e.g., \_, \-, \|, \<10>
715                  */
716                 if (c == '\'')                          /* hex char */
717                 {
718                 int     c2;
719
720                         if ((c = GetChar ()) != EOF && (c2 = GetChar ()) != EOF)
721                         {
722                                 /* should do isxdigit check! */
723                                 rtfClass = rtfText;
724                                 rtfMajor = RTFCharToHex (c) * 16
725                                                 + RTFCharToHex (c2);
726                                 return;
727                         }
728                         /* early eof, whoops (class is rtfUnknown) */
729                         return;
730                 }
731
732                 /* escaped char */
733                 /*if (index (":{}\\", c) != (char *) NULL)*/ /* escaped char */
734                 if (c == ':' || c == '{' || c == '}' || c == '\\')
735                 {
736                         rtfClass = rtfText;
737                         rtfMajor = c;
738                         return;
739                 }
740
741                 /* control symbol */
742                 Lookup (rtfTextBuf);    /* sets class, major, minor */
743                 return;
744         }
745         /* control word */
746         while (isalpha (c))
747         {
748                 if ((c = GetChar ()) == EOF)
749                         break;
750         }
751
752         /*
753          * At this point, the control word is all collected, so the
754          * major/minor numbers are determined before the parameter
755          * (if any) is scanned.  There will be one too many characters
756          * in the buffer, though, so fix up before and restore after
757          * looking up.
758          */
759
760         if (c != EOF)
761                 rtfTextBuf[rtfTextLen-1] = '\0';
762         Lookup (rtfTextBuf);    /* sets class, major, minor */
763         if (c != EOF)
764                 rtfTextBuf[rtfTextLen-1] = c;
765
766         /*
767          * Should be looking at first digit of parameter if there
768          * is one, unless it's negative.  In that case, next char
769          * is '-', so need to gobble next char, and remember sign.
770          */
771
772         sign = 1;
773         if (c == '-')
774         {
775                 sign = -1;
776                 c = GetChar ();
777         }
778         if (c != EOF && isdigit (c))
779         {
780                 rtfParam = 0;
781                 while (isdigit (c))     /* gobble parameter */
782                 {
783                         rtfParam = rtfParam * 10 + c - '0';
784                         if ((c = GetChar ()) == EOF)
785                                 break;
786                 }
787                 rtfParam *= sign;
788         }
789         /*
790          * If control symbol delimiter was a blank, gobble it.
791          * Otherwise the character is first char of next token, so
792          * push it back for next call.  In either case, delete the
793          * delimiter from the token buffer.
794          */
795         if (c != EOF)
796         {
797                 if (c != ' ')
798                         pushedChar = c;
799                 rtfTextBuf[--rtfTextLen] = '\0';
800         }
801 }
802
803
804 /*
805  * Read the next character from the input.  This handles setting the
806  * current line and position-within-line variables.  Those variable are
807  * set correctly whether lines end with CR, LF, or CRLF (the last being
808  * the tricky case).
809  *
810  * bumpLine indicates whether the line number should be incremented on
811  * the *next* input character.
812  */
813
814
815 static int GetChar(void)
816 {
817 int     c;
818 int     oldBumpLine;
819
820     TRACE("\n");
821
822         if ((c = _RTFGetChar()) != EOF)
823         {
824                 rtfTextBuf[rtfTextLen++] = c;
825                 rtfTextBuf[rtfTextLen] = '\0';
826         }
827         if (prevChar == EOF)
828                 bumpLine = 1;
829         oldBumpLine = bumpLine; /* non-zero if prev char was line ending */
830         bumpLine = 0;
831         if (c == '\r')
832                 bumpLine = 1;
833         else if (c == '\n')
834         {
835                 bumpLine = 1;
836                 if (prevChar == '\r')           /* oops, previous \r wasn't */
837                         oldBumpLine = 0;        /* really a line ending */
838         }
839         ++rtfLinePos;
840         if (oldBumpLine)        /* were we supposed to increment the */
841         {                       /* line count on this char? */
842                 ++rtfLineNum;
843                 rtfLinePos = 1;
844         }
845         prevChar = c;
846         return (c);
847 }
848
849
850 /*
851  * Synthesize a token by setting the global variables to the
852  * values supplied.  Typically this is followed with a call
853  * to RTFRouteToken().
854  *
855  * If a param value other than rtfNoParam is passed, it becomes
856  * part of the token text.
857  */
858
859 void RTFSetToken(int class, int major, int minor, int param, char *text)
860 {
861     TRACE("\n");
862
863         rtfClass = class;
864         rtfMajor = major;
865         rtfMinor = minor;
866         rtfParam = param;
867         if (param == rtfNoParam)
868                 (void) strcpy (rtfTextBuf, text);
869         else
870                 sprintf (rtfTextBuf, "%s%d", text, param);
871         rtfTextLen = strlen (rtfTextBuf);
872 }
873
874
875 /* ---------------------------------------------------------------------- */
876
877 /*
878  * Routines to handle mapping of RTF character sets
879  * onto standard characters.
880  *
881  * RTFStdCharCode(name) given char name, produce numeric code
882  * RTFStdCharName(code) given char code, return name
883  * RTFMapChar(c)        map input (RTF) char code to std code
884  * RTFSetCharSet(id)    select given charset map
885  * RTFGetCharSet()      get current charset map
886  *
887  * See ../h/README for more information about charset names and codes.
888  */
889
890
891 /*
892  * Initialize charset stuff.
893  */
894
895 static void CharSetInit(void)
896 {
897     TRACE("\n");
898
899         autoCharSetFlags = (rtfReadCharSet | rtfSwitchCharSet);
900         RTFFree (genCharSetFile);
901         genCharSetFile = (char *) NULL;
902         haveGenCharSet = 0;
903         RTFFree (symCharSetFile);
904         symCharSetFile = (char *) NULL;
905         haveSymCharSet = 0;
906         curCharSet = rtfCSGeneral;
907         curCharCode = genCharCode;
908 }
909
910
911 /*
912  * Specify the name of a file to be read when auto-charset-file reading is
913  * done.
914  */
915
916 void RTFSetCharSetMap (char *name, int csId)
917 {
918     TRACE("\n");
919
920         if ((name = RTFStrSave (name)) == (char *) NULL)        /* make copy */
921                 RTFPanic ("RTFSetCharSetMap: out of memory");
922         switch (csId)
923         {
924         case rtfCSGeneral:
925                 RTFFree (genCharSetFile);       /* free any previous value */
926                 genCharSetFile = name;
927                 break;
928         case rtfCSSymbol:
929                 RTFFree (symCharSetFile);       /* free any previous value */
930                 symCharSetFile = name;
931                 break;
932         }
933 }
934
935
936 /*
937  * Do auto-charset-file reading.
938  * will always use the ansi charset no mater what the value
939  * of the rtfTextBuf is.
940  *
941  * TODO: add support for other charset in the future.
942  *
943  */
944
945 static void ReadCharSetMaps(void)
946 {
947 char    buf[rtfBufSiz];
948
949     TRACE("\n");
950
951         if (genCharSetFile != (char *) NULL)
952                 (void) strcpy (buf, genCharSetFile);
953         else
954                 sprintf (buf, "%s-gen", &rtfTextBuf[1]);
955         if (RTFReadCharSetMap (rtfCSGeneral) == 0)
956                 RTFPanic ("ReadCharSetMaps: Cannot read charset map %s", buf);
957         if (symCharSetFile != (char *) NULL)
958             (void) strcpy (buf, symCharSetFile);
959         else
960                 sprintf (buf, "%s-sym", &rtfTextBuf[1]);
961         if (RTFReadCharSetMap (rtfCSSymbol) == 0)
962                 RTFPanic ("ReadCharSetMaps: Cannot read charset map %s", buf);
963 }
964
965
966
967 /*
968  * Convert a CaracterSetMap (caracter_name, caracter) into
969  * this form : array[caracter_ident] = caracter;
970  */
971
972 int RTFReadCharSetMap(int csId)
973 {
974         int     *stdCodeArray;
975         int i;
976
977     TRACE("\n");
978
979         switch (csId)
980         {
981         default:
982                 return (0);     /* illegal charset id */
983         case rtfCSGeneral:
984
985                 haveGenCharSet = 1;
986                 stdCodeArray = genCharCode;
987                 for (i = 0; i < charSetSize; i++)
988                 {
989                     stdCodeArray[i] = rtfSC_nothing;
990                 }
991
992                 for ( i = 0 ; i< sizeof(ansi_gen)/(sizeof(int));i+=2)
993                 {
994                     stdCodeArray[ ansi_gen[i+1] ] = ansi_gen[i];
995                 }
996                 break;
997
998         case rtfCSSymbol:
999
1000                 haveSymCharSet = 1;
1001                 stdCodeArray = symCharCode;
1002                 for (i = 0; i < charSetSize; i++)
1003                 {
1004                     stdCodeArray[i] = rtfSC_nothing;
1005                 }
1006
1007                 for ( i = 0 ; i< sizeof(ansi_sym)/(sizeof(int));i+=2)
1008                 {
1009                     stdCodeArray[ ansi_sym[i+1] ] = ansi_sym[i];
1010                 }
1011                 break;
1012         }
1013
1014         return (1);
1015 }
1016
1017
1018 /*
1019  * Given a standard character name (a string), find its code (a number).
1020  * Return -1 if name is unknown.
1021  */
1022
1023 int RTFStdCharCode(char *name)
1024 {
1025 int     i;
1026
1027     TRACE("\n");
1028
1029         for (i = 0; i < rtfSC_MaxChar; i++)
1030         {
1031                 if (strcmp (name, stdCharName[i]) == 0)
1032                         return (i);
1033         }
1034         return (-1);
1035 }
1036
1037
1038 /*
1039  * Given a standard character code (a number), find its name (a string).
1040  * Return NULL if code is unknown.
1041  */
1042
1043 char *RTFStdCharName(int code)
1044 {
1045         if (code < 0 || code >= rtfSC_MaxChar)
1046                 return ((char *) NULL);
1047         return (stdCharName[code]);
1048 }
1049
1050
1051 /*
1052  * Given an RTF input character code, find standard character code.
1053  * The translator should read the appropriate charset maps when it finds a
1054  * charset control.  However, the file might not contain one.  In this
1055  * case, no map will be available.  When the first attempt is made to
1056  * map a character under these circumstances, RTFMapChar() assumes ANSI
1057  * and reads the map as necessary.
1058  */
1059
1060 int RTFMapChar(int c)
1061 {
1062     TRACE("\n");
1063
1064         switch (curCharSet)
1065         {
1066         case rtfCSGeneral:
1067                 if (!haveGenCharSet)
1068                 {
1069                         if (RTFReadCharSetMap (rtfCSGeneral) == 0)
1070                                 RTFPanic ("RTFMapChar: cannot read ansi-gen");
1071                 }
1072                 break;
1073         case rtfCSSymbol:
1074                 if (!haveSymCharSet)
1075                 {
1076                         if (RTFReadCharSetMap (rtfCSSymbol) == 0)
1077                                 RTFPanic ("RTFMapChar: cannot read ansi-sym");
1078                 }
1079                 break;
1080         }
1081         if (c < 0 || c >= charSetSize)
1082                 return (rtfSC_nothing);
1083         return (curCharCode[c]);
1084 }
1085
1086
1087 /*
1088  * Set the current character set.  If csId is illegal, uses general charset.
1089  */
1090
1091 void RTFSetCharSet(int csId)
1092 {
1093     TRACE("\n");
1094
1095         switch (csId)
1096         {
1097         default:                /* use general if csId unknown */
1098         case rtfCSGeneral:
1099                 curCharCode = genCharCode;
1100                 curCharSet = csId;
1101                 break;
1102         case rtfCSSymbol:
1103                 curCharCode = symCharCode;
1104                 curCharSet = csId;
1105                 break;
1106         }
1107 }
1108
1109
1110 int RTFGetCharSet(void)
1111 {
1112         return (curCharSet);
1113 }
1114
1115
1116 /* ---------------------------------------------------------------------- */
1117
1118 /*
1119  * Special destination readers.  They gobble the destination so the
1120  * writer doesn't have to deal with them.  That's wrong for any
1121  * translator that wants to process any of these itself.  In that
1122  * case, these readers should be overridden by installing a different
1123  * destination callback.
1124  *
1125  * NOTE: The last token read by each of these reader will be the
1126  * destination's terminating '}', which will then be the current token.
1127  * That '}' token is passed to RTFRouteToken() - the writer has already
1128  * seen the '{' that began the destination group, and may have pushed a
1129  * state; it also needs to know at the end of the group that a state
1130  * should be popped.
1131  *
1132  * It's important that rtf.h and the control token lookup table list
1133  * as many symbols as possible, because these destination readers
1134  * unfortunately make strict assumptions about the input they expect,
1135  * and a token of class rtfUnknown will throw them off easily.
1136  */
1137
1138
1139 /*
1140  * Read { \fonttbl ... } destination.  Old font tables don't have
1141  * braces around each table entry; try to adjust for that.
1142  */
1143
1144 static void ReadFontTbl(void)
1145 {
1146 RTFFont *fp = NULL;
1147 char    buf[rtfBufSiz], *bp;
1148 int     old = -1;
1149 char    *fn = "ReadFontTbl";
1150
1151     TRACE("\n");
1152
1153         for (;;)
1154         {
1155                 (void) RTFGetToken ();
1156                 if (RTFCheckCM (rtfGroup, rtfEndGroup))
1157                         break;
1158                 if (old < 0)            /* first entry - determine tbl type */
1159                 {
1160                         if (RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
1161                                 old = 1;        /* no brace */
1162                         else if (RTFCheckCM (rtfGroup, rtfBeginGroup))
1163                                 old = 0;        /* brace */
1164                         else                    /* can't tell! */
1165                                 RTFPanic ("%s: Cannot determine format", fn);
1166                 }
1167                 if (old == 0)           /* need to find "{" here */
1168                 {
1169                         if (!RTFCheckCM (rtfGroup, rtfBeginGroup))
1170                                 RTFPanic ("%s: missing \"{\"", fn);
1171                         (void) RTFGetToken ();  /* yes, skip to next token */
1172                 }
1173                 if ((fp = New (RTFFont)) == (RTFFont *) NULL)
1174                         RTFPanic ("%s: cannot allocate font entry", fn);
1175
1176                 fp->rtfNextFont = fontList;
1177                 fontList = fp;
1178
1179                 fp->rtfFName = (char *) NULL;
1180                 fp->rtfFAltName = (char *) NULL;
1181                 fp->rtfFNum = -1;
1182                 fp->rtfFFamily = 0;
1183                 fp->rtfFCharSet = 0;
1184                 fp->rtfFPitch = 0;
1185                 fp->rtfFType = 0;
1186                 fp->rtfFCodePage = 0;
1187
1188                 while (rtfClass != rtfEOF
1189                        && !RTFCheckCM (rtfText, ';')
1190                        && !RTFCheckCM (rtfGroup, rtfEndGroup))
1191                 {
1192                         if (rtfClass == rtfControl)
1193                         {
1194                                 switch (rtfMajor)
1195                                 {
1196                                 default:
1197                                         /* ignore token but announce it */
1198                                         RTFMsg ("%s: unknown token \"%s\"\n",
1199                                                         fn, rtfTextBuf);
1200                                         break;
1201                                 case rtfFontFamily:
1202                                         fp->rtfFFamily = rtfMinor;
1203                                         break;
1204                                 case rtfCharAttr:
1205                                         switch (rtfMinor)
1206                                         {
1207                                         default:
1208                                                 break;  /* ignore unknown? */
1209                                         case rtfFontNum:
1210                                                 fp->rtfFNum = rtfParam;
1211                                                 break;
1212                                         }
1213                                         break;
1214                                 case rtfFontAttr:
1215                                         switch (rtfMinor)
1216                                         {
1217                                         default:
1218                                                 break;  /* ignore unknown? */
1219                                         case rtfFontCharSet:
1220                                                 fp->rtfFCharSet = rtfParam;
1221                                                 break;
1222                                         case rtfFontPitch:
1223                                                 fp->rtfFPitch = rtfParam;
1224                                                 break;
1225                                         case rtfFontCodePage:
1226                                                 fp->rtfFCodePage = rtfParam;
1227                                                 break;
1228                                         case rtfFTypeNil:
1229                                         case rtfFTypeTrueType:
1230                                                 fp->rtfFType = rtfParam;
1231                                                 break;
1232                                         }
1233                                         break;
1234                                 }
1235                         }
1236                         else if (RTFCheckCM (rtfGroup, rtfBeginGroup))  /* dest */
1237                         {
1238                                 RTFSkipGroup ();        /* ignore for now */
1239                         }
1240                         else if (rtfClass == rtfText)   /* font name */
1241                         {
1242                                 bp = buf;
1243                                 while (rtfClass == rtfText
1244                                         && !RTFCheckCM (rtfText, ';'))
1245                                 {
1246                                         *bp++ = rtfMajor;
1247                                         (void) RTFGetToken ();
1248                                 }
1249
1250                                 /* FIX: in some cases the <fontinfo> isn't finished with a semi-column */
1251                                 if(RTFCheckCM (rtfGroup, rtfEndGroup))
1252                                 {
1253                                   RTFUngetToken ();
1254                                 }
1255                                 *bp = '\0';
1256                                 fp->rtfFName = RTFStrSave (buf);
1257                                 if (fp->rtfFName == (char *) NULL)
1258                                         RTFPanic ("%s: cannot allocate font name", fn);
1259                                 /* already have next token; don't read one */
1260                                 /* at bottom of loop */
1261                                 continue;
1262                         }
1263                         else
1264                         {
1265                                 /* ignore token but announce it */
1266                                 RTFMsg ("%s: unknown token \"%s\"\n",
1267                                                         fn, rtfTextBuf);
1268                         }
1269                         (void) RTFGetToken ();
1270                 }
1271                 if (old == 0)   /* need to see "}" here */
1272                 {
1273                         (void) RTFGetToken ();
1274                         if (!RTFCheckCM (rtfGroup, rtfEndGroup))
1275                                 RTFPanic ("%s: missing \"}\"", fn);
1276                 }
1277         }
1278         if (fp->rtfFNum == -1)
1279                 RTFPanic ("%s: missing font number", fn);
1280 /*
1281  * Could check other pieces of structure here, too, I suppose.
1282  */
1283         RTFRouteToken ();       /* feed "}" back to router */
1284 }
1285
1286
1287 /*
1288  * The color table entries have color values of -1 if
1289  * the default color should be used for the entry (only
1290  * a semi-colon is given in the definition, no color values).
1291  * There will be a problem if a partial entry (1 or 2 but
1292  * not 3 color values) is given.  The possibility is ignored
1293  * here.
1294  */
1295
1296 static void ReadColorTbl(void)
1297 {
1298 RTFColor        *cp;
1299 int             cnum = 0;
1300 char            *fn = "ReadColorTbl";
1301
1302     TRACE("\n");
1303
1304         for (;;)
1305         {
1306                 (void) RTFGetToken ();
1307                 if (RTFCheckCM (rtfGroup, rtfEndGroup))
1308                         break;
1309                 if ((cp = New (RTFColor)) == (RTFColor *) NULL)
1310                         RTFPanic ("%s: cannot allocate color entry", fn);
1311                 cp->rtfCNum = cnum++;
1312                 cp->rtfCRed = cp->rtfCGreen = cp->rtfCBlue = -1;
1313                 cp->rtfNextColor = colorList;
1314                 colorList = cp;
1315                 while (RTFCheckCM (rtfControl, rtfColorName))
1316                 {
1317                         switch (rtfMinor)
1318                         {
1319                         case rtfRed:    cp->rtfCRed = rtfParam; break;
1320                         case rtfGreen:  cp->rtfCGreen = rtfParam; break;
1321                         case rtfBlue:   cp->rtfCBlue = rtfParam; break;
1322                         }
1323                         RTFGetToken ();
1324                 }
1325                 if (!RTFCheckCM (rtfText, (int) ';'))
1326                         RTFPanic ("%s: malformed entry", fn);
1327         }
1328         RTFRouteToken ();       /* feed "}" back to router */
1329 }
1330
1331
1332 /*
1333  * The "Normal" style definition doesn't contain any style number,
1334  * all others do.  Normal style is given style rtfNormalStyleNum.
1335  */
1336
1337 static void ReadStyleSheet(void)
1338 {
1339 RTFStyle        *sp;
1340 RTFStyleElt     *sep, *sepLast;
1341 char            buf[rtfBufSiz], *bp;
1342 char            *fn = "ReadStyleSheet";
1343
1344     TRACE("\n");
1345
1346         for (;;)
1347         {
1348                 (void) RTFGetToken ();
1349                 if (RTFCheckCM (rtfGroup, rtfEndGroup))
1350                         break;
1351                 if ((sp = New (RTFStyle)) == (RTFStyle *) NULL)
1352                         RTFPanic ("%s: cannot allocate stylesheet entry", fn);
1353                 sp->rtfSName = (char *) NULL;
1354                 sp->rtfSNum = -1;
1355                 sp->rtfSType = rtfParStyle;
1356                 sp->rtfSAdditive = 0;
1357                 sp->rtfSBasedOn = rtfNoStyleNum;
1358                 sp->rtfSNextPar = -1;
1359                 sp->rtfSSEList = sepLast = (RTFStyleElt *) NULL;
1360                 sp->rtfNextStyle = styleList;
1361                 sp->rtfExpanding = 0;
1362                 styleList = sp;
1363                 if (!RTFCheckCM (rtfGroup, rtfBeginGroup))
1364                         RTFPanic ("%s: missing \"{\"", fn);
1365                 for (;;)
1366                 {
1367                         (void) RTFGetToken ();
1368                         if (rtfClass == rtfEOF
1369                                 || RTFCheckCM (rtfText, ';'))
1370                                 break;
1371                         if (rtfClass == rtfControl)
1372                         {
1373                                 if (RTFCheckMM (rtfSpecialChar, rtfOptDest))
1374                                         continue;       /* ignore "\*" */
1375                                 if (RTFCheckMM (rtfParAttr, rtfStyleNum))
1376                                 {
1377                                         sp->rtfSNum = rtfParam;
1378                                         sp->rtfSType = rtfParStyle;
1379                                         continue;
1380                                 }
1381                                 if (RTFCheckMM (rtfCharAttr, rtfCharStyleNum))
1382                                 {
1383                                         sp->rtfSNum = rtfParam;
1384                                         sp->rtfSType = rtfCharStyle;
1385                                         continue;
1386                                 }
1387                                 if (RTFCheckMM (rtfSectAttr, rtfSectStyleNum))
1388                                 {
1389                                         sp->rtfSNum = rtfParam;
1390                                         sp->rtfSType = rtfSectStyle;
1391                                         continue;
1392                                 }
1393                                 if (RTFCheckMM (rtfStyleAttr, rtfBasedOn))
1394                                 {
1395                                         sp->rtfSBasedOn = rtfParam;
1396                                         continue;
1397                                 }
1398                                 if (RTFCheckMM (rtfStyleAttr, rtfAdditive))
1399                                 {
1400                                         sp->rtfSAdditive = 1;
1401                                         continue;
1402                                 }
1403                                 if (RTFCheckMM (rtfStyleAttr, rtfNext))
1404                                 {
1405                                         sp->rtfSNextPar = rtfParam;
1406                                         continue;
1407                                 }
1408                                 if ((sep = New (RTFStyleElt)) == (RTFStyleElt *) NULL)
1409                                         RTFPanic ("%s: cannot allocate style element", fn);
1410                                 sep->rtfSEClass = rtfClass;
1411                                 sep->rtfSEMajor = rtfMajor;
1412                                 sep->rtfSEMinor = rtfMinor;
1413                                 sep->rtfSEParam = rtfParam;
1414                                 if ((sep->rtfSEText = RTFStrSave (rtfTextBuf))
1415                                                                 == (char *) NULL)
1416                                         RTFPanic ("%s: cannot allocate style element text", fn);
1417                                 if (sepLast == (RTFStyleElt *) NULL)
1418                                         sp->rtfSSEList = sep;   /* first element */
1419                                 else                            /* add to end */
1420                                         sepLast->rtfNextSE = sep;
1421                                 sep->rtfNextSE = (RTFStyleElt *) NULL;
1422                                 sepLast = sep;
1423                         }
1424                         else if (RTFCheckCM (rtfGroup, rtfBeginGroup))
1425                         {
1426                                 /*
1427                                  * This passes over "{\*\keycode ... }, among
1428                                  * other things. A temporary (perhaps) hack.
1429                                  */
1430                                 RTFSkipGroup ();
1431                                 continue;
1432                         }
1433                         else if (rtfClass == rtfText)   /* style name */
1434                         {
1435                                 bp = buf;
1436                                 while (rtfClass == rtfText)
1437                                 {
1438                                         if (rtfMajor == ';')
1439                                         {
1440                                                 /* put back for "for" loop */
1441                                                 (void) RTFUngetToken ();
1442                                                 break;
1443                                         }
1444                                         *bp++ = rtfMajor;
1445                                         (void) RTFGetToken ();
1446                                 }
1447                                 *bp = '\0';
1448                                 if ((sp->rtfSName = RTFStrSave (buf)) == (char *) NULL)
1449                                         RTFPanic ("%s: cannot allocate style name", fn);
1450                         }
1451                         else            /* unrecognized */
1452                         {
1453                                 /* ignore token but announce it */
1454                                 RTFMsg ("%s: unknown token \"%s\"\n",
1455                                                         fn, rtfTextBuf);
1456                         }
1457                 }
1458                 (void) RTFGetToken ();
1459                 if (!RTFCheckCM (rtfGroup, rtfEndGroup))
1460                         RTFPanic ("%s: missing \"}\"", fn);
1461
1462                 /*
1463                  * Check over the style structure.  A name is a must.
1464                  * If no style number was specified, check whether it's the
1465                  * Normal style (in which case it's given style number
1466                  * rtfNormalStyleNum).  Note that some "normal" style names
1467                  * just begin with "Normal" and can have other stuff following,
1468                  * e.g., "Normal,Times 10 point".  Ugh.
1469                  *
1470                  * Some German RTF writers use "Standard" instead of "Normal".
1471                  */
1472                 if (sp->rtfSName == (char *) NULL)
1473                         RTFPanic ("%s: missing style name", fn);
1474                 if (sp->rtfSNum < 0)
1475                 {
1476                         if (strncmp (buf, "Normal", 6) != 0
1477                                 && strncmp (buf, "Standard", 8) != 0)
1478                                 RTFPanic ("%s: missing style number", fn);
1479                         sp->rtfSNum = rtfNormalStyleNum;
1480                 }
1481                 if (sp->rtfSNextPar == -1)      /* if \snext not given, */
1482                         sp->rtfSNextPar = sp->rtfSNum;  /* next is itself */
1483         }
1484         RTFRouteToken ();       /* feed "}" back to router */
1485 }
1486
1487
1488 static void ReadInfoGroup(void)
1489 {
1490         RTFSkipGroup ();
1491         RTFRouteToken ();       /* feed "}" back to router */
1492 }
1493
1494
1495 static void ReadPictGroup(void)
1496 {
1497         RTFSkipGroup ();
1498         RTFRouteToken ();       /* feed "}" back to router */
1499 }
1500
1501
1502 static void ReadObjGroup(void)
1503 {
1504         RTFSkipGroup ();
1505         RTFRouteToken ();       /* feed "}" back to router */
1506 }
1507
1508
1509 /* ---------------------------------------------------------------------- */
1510
1511 /*
1512  * Routines to return pieces of stylesheet, or font or color tables.
1513  * References to style 0 are mapped onto the Normal style.
1514  */
1515
1516
1517 RTFStyle *RTFGetStyle(int num)
1518 {
1519 RTFStyle        *s;
1520
1521         if (num == -1)
1522                 return (styleList);
1523         for (s = styleList; s != (RTFStyle *) NULL; s = s->rtfNextStyle)
1524         {
1525                 if (s->rtfSNum == num)
1526                         break;
1527         }
1528         return (s);             /* NULL if not found */
1529 }
1530
1531
1532 RTFFont *RTFGetFont(int num)
1533 {
1534 RTFFont *f;
1535
1536         if (num == -1)
1537                 return (fontList);
1538         for (f = fontList; f != (RTFFont *) NULL; f = f->rtfNextFont)
1539         {
1540                 if (f->rtfFNum == num)
1541                         break;
1542         }
1543         return (f);             /* NULL if not found */
1544 }
1545
1546
1547 RTFColor *RTFGetColor(int num)
1548 {
1549 RTFColor        *c;
1550
1551         if (num == -1)
1552                 return (colorList);
1553         for (c = colorList; c != (RTFColor *) NULL; c = c->rtfNextColor)
1554         {
1555                 if (c->rtfCNum == num)
1556                         break;
1557         }
1558         return (c);             /* NULL if not found */
1559 }
1560
1561
1562 /* ---------------------------------------------------------------------- */
1563
1564
1565 /*
1566  * Expand style n, if there is such a style.
1567  */
1568
1569 void RTFExpandStyle(int n)
1570 {
1571 RTFStyle        *s;
1572 RTFStyleElt     *se;
1573
1574     TRACE("\n");
1575
1576         if (n == -1 || (s = RTFGetStyle (n)) == (RTFStyle *) NULL)
1577                 return;
1578         if (s->rtfExpanding != 0)
1579                 RTFPanic ("Style expansion loop, style %d", n);
1580         s->rtfExpanding = 1;    /* set expansion flag for loop detection */
1581         /*
1582          * Expand "based-on" style (unless it's the same as the current
1583          * style -- Normal style usually gives itself as its own based-on
1584          * style).  Based-on style expansion is done by synthesizing
1585          * the token that the writer needs to see in order to trigger
1586          * another style expansion, and feeding to token back through
1587          * the router so the writer sees it.
1588          */
1589         if (n != s->rtfSBasedOn)
1590         {
1591                 RTFSetToken (rtfControl, rtfParAttr, rtfStyleNum,
1592                                                         s->rtfSBasedOn, "\\s");
1593                 RTFRouteToken ();
1594         }
1595         /*
1596          * Now route the tokens unique to this style.  RTFSetToken()
1597          * isn't used because it would add the param value to the end
1598          * of the token text, which already has it in.
1599          */
1600         for (se = s->rtfSSEList; se != (RTFStyleElt *) NULL; se = se->rtfNextSE)
1601         {
1602                 rtfClass = se->rtfSEClass;
1603                 rtfMajor = se->rtfSEMajor;
1604                 rtfMinor = se->rtfSEMinor;
1605                 rtfParam = se->rtfSEParam;
1606                 (void) strcpy (rtfTextBuf, se->rtfSEText);
1607                 rtfTextLen = strlen (rtfTextBuf);
1608                 RTFRouteToken ();
1609         }
1610         s->rtfExpanding = 0;    /* done - clear expansion flag */
1611 }
1612
1613
1614 /* ---------------------------------------------------------------------- */
1615
1616 /*
1617  * Control symbol lookup routines
1618  */
1619
1620
1621 typedef struct RTFKey   RTFKey;
1622
1623 struct RTFKey
1624 {
1625         int     rtfKMajor;      /* major number */
1626         int     rtfKMinor;      /* minor number */
1627         char    *rtfKStr;       /* symbol name */
1628         int     rtfKHash;       /* symbol name hash value */
1629 };
1630
1631 /*
1632  * A minor number of -1 means the token has no minor number
1633  * (all valid minor numbers are >= 0).
1634  */
1635
1636 static RTFKey   rtfKey[] =
1637 {
1638         /*
1639          * Special characters
1640          */
1641
1642         { rtfSpecialChar,       rtfIIntVersion,         "vern",         0 },
1643         { rtfSpecialChar,       rtfICreateTime,         "creatim",      0 },
1644         { rtfSpecialChar,       rtfIRevisionTime,       "revtim",       0 },
1645         { rtfSpecialChar,       rtfIPrintTime,          "printim",      0 },
1646         { rtfSpecialChar,       rtfIBackupTime,         "buptim",       0 },
1647         { rtfSpecialChar,       rtfIEditTime,           "edmins",       0 },
1648         { rtfSpecialChar,       rtfIYear,               "yr",           0 },
1649         { rtfSpecialChar,       rtfIMonth,              "mo",           0 },
1650         { rtfSpecialChar,       rtfIDay,                "dy",           0 },
1651         { rtfSpecialChar,       rtfIHour,               "hr",           0 },
1652         { rtfSpecialChar,       rtfIMinute,             "min",          0 },
1653         { rtfSpecialChar,       rtfISecond,             "sec",          0 },
1654         { rtfSpecialChar,       rtfINPages,             "nofpages",     0 },
1655         { rtfSpecialChar,       rtfINWords,             "nofwords",     0 },
1656         { rtfSpecialChar,       rtfINChars,             "nofchars",     0 },
1657         { rtfSpecialChar,       rtfIIntID,              "id",           0 },
1658
1659         { rtfSpecialChar,       rtfCurHeadDate,         "chdate",       0 },
1660         { rtfSpecialChar,       rtfCurHeadDateLong,     "chdpl",        0 },
1661         { rtfSpecialChar,       rtfCurHeadDateAbbrev,   "chdpa",        0 },
1662         { rtfSpecialChar,       rtfCurHeadTime,         "chtime",       0 },
1663         { rtfSpecialChar,       rtfCurHeadPage,         "chpgn",        0 },
1664         { rtfSpecialChar,       rtfSectNum,             "sectnum",      0 },
1665         { rtfSpecialChar,       rtfCurFNote,            "chftn",        0 },
1666         { rtfSpecialChar,       rtfCurAnnotRef,         "chatn",        0 },
1667         { rtfSpecialChar,       rtfFNoteSep,            "chftnsep",     0 },
1668         { rtfSpecialChar,       rtfFNoteCont,           "chftnsepc",    0 },
1669         { rtfSpecialChar,       rtfCell,                "cell",         0 },
1670         { rtfSpecialChar,       rtfRow,                 "row",          0 },
1671         { rtfSpecialChar,       rtfPar,                 "par",          0 },
1672         /* newline and carriage return are synonyms for */
1673         /* \par when they are preceded by a \ character */
1674         { rtfSpecialChar,       rtfPar,                 "\n",           0 },
1675         { rtfSpecialChar,       rtfPar,                 "\r",           0 },
1676         { rtfSpecialChar,       rtfSect,                "sect",         0 },
1677         { rtfSpecialChar,       rtfPage,                "page",         0 },
1678         { rtfSpecialChar,       rtfColumn,              "column",       0 },
1679         { rtfSpecialChar,       rtfLine,                "line",         0 },
1680         { rtfSpecialChar,       rtfSoftPage,            "softpage",     0 },
1681         { rtfSpecialChar,       rtfSoftColumn,          "softcol",      0 },
1682         { rtfSpecialChar,       rtfSoftLine,            "softline",     0 },
1683         { rtfSpecialChar,       rtfSoftLineHt,          "softlheight",  0 },
1684         { rtfSpecialChar,       rtfTab,                 "tab",          0 },
1685         { rtfSpecialChar,       rtfEmDash,              "emdash",       0 },
1686         { rtfSpecialChar,       rtfEnDash,              "endash",       0 },
1687         { rtfSpecialChar,       rtfEmSpace,             "emspace",      0 },
1688         { rtfSpecialChar,       rtfEnSpace,             "enspace",      0 },
1689         { rtfSpecialChar,       rtfBullet,              "bullet",       0 },
1690         { rtfSpecialChar,       rtfLQuote,              "lquote",       0 },
1691         { rtfSpecialChar,       rtfRQuote,              "rquote",       0 },
1692         { rtfSpecialChar,       rtfLDblQuote,           "ldblquote",    0 },
1693         { rtfSpecialChar,       rtfRDblQuote,           "rdblquote",    0 },
1694         { rtfSpecialChar,       rtfFormula,             "|",            0 },
1695         { rtfSpecialChar,       rtfNoBrkSpace,          "~",            0 },
1696         { rtfSpecialChar,       rtfNoReqHyphen,         "-",            0 },
1697         { rtfSpecialChar,       rtfNoBrkHyphen,         "_",            0 },
1698         { rtfSpecialChar,       rtfOptDest,             "*",            0 },
1699         { rtfSpecialChar,       rtfLTRMark,             "ltrmark",      0 },
1700         { rtfSpecialChar,       rtfRTLMark,             "rtlmark",      0 },
1701         { rtfSpecialChar,       rtfNoWidthJoiner,       "zwj",          0 },
1702         { rtfSpecialChar,       rtfNoWidthNonJoiner,    "zwnj",         0 },
1703         /* is this valid? */
1704         { rtfSpecialChar,       rtfCurHeadPict,         "chpict",       0 },
1705
1706         /*
1707          * Character formatting attributes
1708          */
1709
1710         { rtfCharAttr,  rtfPlain,               "plain",        0 },
1711         { rtfCharAttr,  rtfBold,                "b",            0 },
1712         { rtfCharAttr,  rtfAllCaps,             "caps",         0 },
1713         { rtfCharAttr,  rtfDeleted,             "deleted",      0 },
1714         { rtfCharAttr,  rtfSubScript,           "dn",           0 },
1715         { rtfCharAttr,  rtfSubScrShrink,        "sub",          0 },
1716         { rtfCharAttr,  rtfNoSuperSub,          "nosupersub",   0 },
1717         { rtfCharAttr,  rtfExpand,              "expnd",        0 },
1718         { rtfCharAttr,  rtfExpandTwips,         "expndtw",      0 },
1719         { rtfCharAttr,  rtfKerning,             "kerning",      0 },
1720         { rtfCharAttr,  rtfFontNum,             "f",            0 },
1721         { rtfCharAttr,  rtfFontSize,            "fs",           0 },
1722         { rtfCharAttr,  rtfItalic,              "i",            0 },
1723         { rtfCharAttr,  rtfOutline,             "outl",         0 },
1724         { rtfCharAttr,  rtfRevised,             "revised",      0 },
1725         { rtfCharAttr,  rtfRevAuthor,           "revauth",      0 },
1726         { rtfCharAttr,  rtfRevDTTM,             "revdttm",      0 },
1727         { rtfCharAttr,  rtfSmallCaps,           "scaps",        0 },
1728         { rtfCharAttr,  rtfShadow,              "shad",         0 },
1729         { rtfCharAttr,  rtfStrikeThru,          "strike",       0 },
1730         { rtfCharAttr,  rtfUnderline,           "ul",           0 },
1731         { rtfCharAttr,  rtfDotUnderline,        "uld",          0 },
1732         { rtfCharAttr,  rtfDbUnderline,         "uldb",         0 },
1733         { rtfCharAttr,  rtfNoUnderline,         "ulnone",       0 },
1734         { rtfCharAttr,  rtfWordUnderline,       "ulw",          0 },
1735         { rtfCharAttr,  rtfSuperScript,         "up",           0 },
1736         { rtfCharAttr,  rtfSuperScrShrink,      "super",        0 },
1737         { rtfCharAttr,  rtfInvisible,           "v",            0 },
1738         { rtfCharAttr,  rtfForeColor,           "cf",           0 },
1739         { rtfCharAttr,  rtfBackColor,           "cb",           0 },
1740         { rtfCharAttr,  rtfRTLChar,             "rtlch",        0 },
1741         { rtfCharAttr,  rtfLTRChar,             "ltrch",        0 },
1742         { rtfCharAttr,  rtfCharStyleNum,        "cs",           0 },
1743         { rtfCharAttr,  rtfCharCharSet,         "cchs",         0 },
1744         { rtfCharAttr,  rtfLanguage,            "lang",         0 },
1745         /* this has disappeared from spec 1.2 */
1746         { rtfCharAttr,  rtfGray,                "gray",         0 },
1747
1748         /*
1749          * Paragraph formatting attributes
1750          */
1751
1752         { rtfParAttr,   rtfParDef,              "pard",         0 },
1753         { rtfParAttr,   rtfStyleNum,            "s",            0 },
1754         { rtfParAttr,   rtfHyphenate,           "hyphpar",      0 },
1755         { rtfParAttr,   rtfInTable,             "intbl",        0 },
1756         { rtfParAttr,   rtfKeep,                "keep",         0 },
1757         { rtfParAttr,   rtfNoWidowControl,      "nowidctlpar",  0 },
1758         { rtfParAttr,   rtfKeepNext,            "keepn",        0 },
1759         { rtfParAttr,   rtfOutlineLevel,        "level",        0 },
1760         { rtfParAttr,   rtfNoLineNum,           "noline",       0 },
1761         { rtfParAttr,   rtfPBBefore,            "pagebb",       0 },
1762         { rtfParAttr,   rtfSideBySide,          "sbys",         0 },
1763         { rtfParAttr,   rtfQuadLeft,            "ql",           0 },
1764         { rtfParAttr,   rtfQuadRight,           "qr",           0 },
1765         { rtfParAttr,   rtfQuadJust,            "qj",           0 },
1766         { rtfParAttr,   rtfQuadCenter,          "qc",           0 },
1767         { rtfParAttr,   rtfFirstIndent,         "fi",           0 },
1768         { rtfParAttr,   rtfLeftIndent,          "li",           0 },
1769         { rtfParAttr,   rtfRightIndent,         "ri",           0 },
1770         { rtfParAttr,   rtfSpaceBefore,         "sb",           0 },
1771         { rtfParAttr,   rtfSpaceAfter,          "sa",           0 },
1772         { rtfParAttr,   rtfSpaceBetween,        "sl",           0 },
1773         { rtfParAttr,   rtfSpaceMultiply,       "slmult",       0 },
1774
1775         { rtfParAttr,   rtfSubDocument,         "subdocument",  0 },
1776
1777         { rtfParAttr,   rtfRTLPar,              "rtlpar",       0 },
1778         { rtfParAttr,   rtfLTRPar,              "ltrpar",       0 },
1779
1780         { rtfParAttr,   rtfTabPos,              "tx",           0 },
1781         /*
1782          * FrameMaker writes \tql (to mean left-justified tab, apparently)
1783          * although it's not in the spec.  It's also redundant, since lj
1784          * tabs are the default.
1785          */
1786         { rtfParAttr,   rtfTabLeft,             "tql",          0 },
1787         { rtfParAttr,   rtfTabRight,            "tqr",          0 },
1788         { rtfParAttr,   rtfTabCenter,           "tqc",          0 },
1789         { rtfParAttr,   rtfTabDecimal,          "tqdec",        0 },
1790         { rtfParAttr,   rtfTabBar,              "tb",           0 },
1791         { rtfParAttr,   rtfLeaderDot,           "tldot",        0 },
1792         { rtfParAttr,   rtfLeaderHyphen,        "tlhyph",       0 },
1793         { rtfParAttr,   rtfLeaderUnder,         "tlul",         0 },
1794         { rtfParAttr,   rtfLeaderThick,         "tlth",         0 },
1795         { rtfParAttr,   rtfLeaderEqual,         "tleq",         0 },
1796
1797         { rtfParAttr,   rtfParLevel,            "pnlvl",        0 },
1798         { rtfParAttr,   rtfParBullet,           "pnlvlblt",     0 },
1799         { rtfParAttr,   rtfParSimple,           "pnlvlbody",    0 },
1800         { rtfParAttr,   rtfParNumCont,          "pnlvlcont",    0 },
1801         { rtfParAttr,   rtfParNumOnce,          "pnnumonce",    0 },
1802         { rtfParAttr,   rtfParNumAcross,        "pnacross",     0 },
1803         { rtfParAttr,   rtfParHangIndent,       "pnhang",       0 },
1804         { rtfParAttr,   rtfParNumRestart,       "pnrestart",    0 },
1805         { rtfParAttr,   rtfParNumCardinal,      "pncard",       0 },
1806         { rtfParAttr,   rtfParNumDecimal,       "pndec",        0 },
1807         { rtfParAttr,   rtfParNumULetter,       "pnucltr",      0 },
1808         { rtfParAttr,   rtfParNumURoman,        "pnucrm",       0 },
1809         { rtfParAttr,   rtfParNumLLetter,       "pnlcltr",      0 },
1810         { rtfParAttr,   rtfParNumLRoman,        "pnlcrm",       0 },
1811         { rtfParAttr,   rtfParNumOrdinal,       "pnord",        0 },
1812         { rtfParAttr,   rtfParNumOrdinalText,   "pnordt",       0 },
1813         { rtfParAttr,   rtfParNumBold,          "pnb",          0 },
1814         { rtfParAttr,   rtfParNumItalic,        "pni",          0 },
1815         { rtfParAttr,   rtfParNumAllCaps,       "pncaps",       0 },
1816         { rtfParAttr,   rtfParNumSmallCaps,     "pnscaps",      0 },
1817         { rtfParAttr,   rtfParNumUnder,         "pnul",         0 },
1818         { rtfParAttr,   rtfParNumDotUnder,      "pnuld",        0 },
1819         { rtfParAttr,   rtfParNumDbUnder,       "pnuldb",       0 },
1820         { rtfParAttr,   rtfParNumNoUnder,       "pnulnone",     0 },
1821         { rtfParAttr,   rtfParNumWordUnder,     "pnulw",        0 },
1822         { rtfParAttr,   rtfParNumStrikethru,    "pnstrike",     0 },
1823         { rtfParAttr,   rtfParNumForeColor,     "pncf",         0 },
1824         { rtfParAttr,   rtfParNumFont,          "pnf",          0 },
1825         { rtfParAttr,   rtfParNumFontSize,      "pnfs",         0 },
1826         { rtfParAttr,   rtfParNumIndent,        "pnindent",     0 },
1827         { rtfParAttr,   rtfParNumSpacing,       "pnsp",         0 },
1828         { rtfParAttr,   rtfParNumInclPrev,      "pnprev",       0 },
1829         { rtfParAttr,   rtfParNumCenter,        "pnqc",         0 },
1830         { rtfParAttr,   rtfParNumLeft,          "pnql",         0 },
1831         { rtfParAttr,   rtfParNumRight,         "pnqr",         0 },
1832         { rtfParAttr,   rtfParNumStartAt,       "pnstart",      0 },
1833
1834         { rtfParAttr,   rtfBorderTop,           "brdrt",        0 },
1835         { rtfParAttr,   rtfBorderBottom,        "brdrb",        0 },
1836         { rtfParAttr,   rtfBorderLeft,          "brdrl",        0 },
1837         { rtfParAttr,   rtfBorderRight,         "brdrr",        0 },
1838         { rtfParAttr,   rtfBorderBetween,       "brdrbtw",      0 },
1839         { rtfParAttr,   rtfBorderBar,           "brdrbar",      0 },
1840         { rtfParAttr,   rtfBorderBox,           "box",          0 },
1841         { rtfParAttr,   rtfBorderSingle,        "brdrs",        0 },
1842         { rtfParAttr,   rtfBorderThick,         "brdrth",       0 },
1843         { rtfParAttr,   rtfBorderShadow,        "brdrsh",       0 },
1844         { rtfParAttr,   rtfBorderDouble,        "brdrdb",       0 },
1845         { rtfParAttr,   rtfBorderDot,           "brdrdot",      0 },
1846         { rtfParAttr,   rtfBorderDot,           "brdrdash",     0 },
1847         { rtfParAttr,   rtfBorderHair,          "brdrhair",     0 },
1848         { rtfParAttr,   rtfBorderWidth,         "brdrw",        0 },
1849         { rtfParAttr,   rtfBorderColor,         "brdrcf",       0 },
1850         { rtfParAttr,   rtfBorderSpace,         "brsp",         0 },
1851
1852         { rtfParAttr,   rtfShading,             "shading",      0 },
1853         { rtfParAttr,   rtfBgPatH,              "bghoriz",      0 },
1854         { rtfParAttr,   rtfBgPatV,              "bgvert",       0 },
1855         { rtfParAttr,   rtfFwdDiagBgPat,        "bgfdiag",      0 },
1856         { rtfParAttr,   rtfBwdDiagBgPat,        "bgbdiag",      0 },
1857         { rtfParAttr,   rtfHatchBgPat,          "bgcross",      0 },
1858         { rtfParAttr,   rtfDiagHatchBgPat,      "bgdcross",     0 },
1859         { rtfParAttr,   rtfDarkBgPatH,          "bgdkhoriz",    0 },
1860         { rtfParAttr,   rtfDarkBgPatV,          "bgdkvert",     0 },
1861         { rtfParAttr,   rtfFwdDarkBgPat,        "bgdkfdiag",    0 },
1862         { rtfParAttr,   rtfBwdDarkBgPat,        "bgdkbdiag",    0 },
1863         { rtfParAttr,   rtfDarkHatchBgPat,      "bgdkcross",    0 },
1864         { rtfParAttr,   rtfDarkDiagHatchBgPat,  "bgdkdcross",   0 },
1865         { rtfParAttr,   rtfBgPatLineColor,      "cfpat",        0 },
1866         { rtfParAttr,   rtfBgPatColor,          "cbpat",        0 },
1867
1868         /*
1869          * Section formatting attributes
1870          */
1871
1872         { rtfSectAttr,  rtfSectDef,             "sectd",        0 },
1873         { rtfSectAttr,  rtfENoteHere,           "endnhere",     0 },
1874         { rtfSectAttr,  rtfPrtBinFirst,         "binfsxn",      0 },
1875         { rtfSectAttr,  rtfPrtBin,              "binsxn",       0 },
1876         { rtfSectAttr,  rtfSectStyleNum,        "ds",           0 },
1877
1878         { rtfSectAttr,  rtfNoBreak,             "sbknone",      0 },
1879         { rtfSectAttr,  rtfColBreak,            "sbkcol",       0 },
1880         { rtfSectAttr,  rtfPageBreak,           "sbkpage",      0 },
1881         { rtfSectAttr,  rtfEvenBreak,           "sbkeven",      0 },
1882         { rtfSectAttr,  rtfOddBreak,            "sbkodd",       0 },
1883
1884         { rtfSectAttr,  rtfColumns,             "cols",         0 },
1885         { rtfSectAttr,  rtfColumnSpace,         "colsx",        0 },
1886         { rtfSectAttr,  rtfColumnNumber,        "colno",        0 },
1887         { rtfSectAttr,  rtfColumnSpRight,       "colsr",        0 },
1888         { rtfSectAttr,  rtfColumnWidth,         "colw",         0 },
1889         { rtfSectAttr,  rtfColumnLine,          "linebetcol",   0 },
1890
1891         { rtfSectAttr,  rtfLineModulus,         "linemod",      0 },
1892         { rtfSectAttr,  rtfLineDist,            "linex",        0 },
1893         { rtfSectAttr,  rtfLineStarts,          "linestarts",   0 },
1894         { rtfSectAttr,  rtfLineRestart,         "linerestart",  0 },
1895         { rtfSectAttr,  rtfLineRestartPg,       "lineppage",    0 },
1896         { rtfSectAttr,  rtfLineCont,            "linecont",     0 },
1897
1898         { rtfSectAttr,  rtfSectPageWid,         "pgwsxn",       0 },
1899         { rtfSectAttr,  rtfSectPageHt,          "pghsxn",       0 },
1900         { rtfSectAttr,  rtfSectMarginLeft,      "marglsxn",     0 },
1901         { rtfSectAttr,  rtfSectMarginRight,     "margrsxn",     0 },
1902         { rtfSectAttr,  rtfSectMarginTop,       "margtsxn",     0 },
1903         { rtfSectAttr,  rtfSectMarginBottom,    "margbsxn",     0 },
1904         { rtfSectAttr,  rtfSectMarginGutter,    "guttersxn",    0 },
1905         { rtfSectAttr,  rtfSectLandscape,       "lndscpsxn",    0 },
1906         { rtfSectAttr,  rtfTitleSpecial,        "titlepg",      0 },
1907         { rtfSectAttr,  rtfHeaderY,             "headery",      0 },
1908         { rtfSectAttr,  rtfFooterY,             "footery",      0 },
1909
1910         { rtfSectAttr,  rtfPageStarts,          "pgnstarts",    0 },
1911         { rtfSectAttr,  rtfPageCont,            "pgncont",      0 },
1912         { rtfSectAttr,  rtfPageRestart,         "pgnrestart",   0 },
1913         { rtfSectAttr,  rtfPageNumRight,        "pgnx",         0 },
1914         { rtfSectAttr,  rtfPageNumTop,          "pgny",         0 },
1915         { rtfSectAttr,  rtfPageDecimal,         "pgndec",       0 },
1916         { rtfSectAttr,  rtfPageURoman,          "pgnucrm",      0 },
1917         { rtfSectAttr,  rtfPageLRoman,          "pgnlcrm",      0 },
1918         { rtfSectAttr,  rtfPageULetter,         "pgnucltr",     0 },
1919         { rtfSectAttr,  rtfPageLLetter,         "pgnlcltr",     0 },
1920         { rtfSectAttr,  rtfPageNumHyphSep,      "pgnhnsh",      0 },
1921         { rtfSectAttr,  rtfPageNumSpaceSep,     "pgnhnsp",      0 },
1922         { rtfSectAttr,  rtfPageNumColonSep,     "pgnhnsc",      0 },
1923         { rtfSectAttr,  rtfPageNumEmdashSep,    "pgnhnsm",      0 },
1924         { rtfSectAttr,  rtfPageNumEndashSep,    "pgnhnsn",      0 },
1925
1926         { rtfSectAttr,  rtfTopVAlign,           "vertalt",      0 },
1927         /* misspelled as "vertal" in specification 1.0 */
1928         { rtfSectAttr,  rtfBottomVAlign,        "vertalb",      0 },
1929         { rtfSectAttr,  rtfCenterVAlign,        "vertalc",      0 },
1930         { rtfSectAttr,  rtfJustVAlign,          "vertalj",      0 },
1931
1932         { rtfSectAttr,  rtfRTLSect,             "rtlsect",      0 },
1933         { rtfSectAttr,  rtfLTRSect,             "ltrsect",      0 },
1934
1935         /* I've seen these in an old spec, but not in real files... */
1936         /*rtfSectAttr,  rtfNoBreak,             "nobreak",      0,*/
1937         /*rtfSectAttr,  rtfColBreak,            "colbreak",     0,*/
1938         /*rtfSectAttr,  rtfPageBreak,           "pagebreak",    0,*/
1939         /*rtfSectAttr,  rtfEvenBreak,           "evenbreak",    0,*/
1940         /*rtfSectAttr,  rtfOddBreak,            "oddbreak",     0,*/
1941
1942         /*
1943          * Document formatting attributes
1944          */
1945
1946         { rtfDocAttr,   rtfDefTab,              "deftab",       0 },
1947         { rtfDocAttr,   rtfHyphHotZone,         "hyphhotz",     0 },
1948         { rtfDocAttr,   rtfHyphConsecLines,     "hyphconsec",   0 },
1949         { rtfDocAttr,   rtfHyphCaps,            "hyphcaps",     0 },
1950         { rtfDocAttr,   rtfHyphAuto,            "hyphauto",     0 },
1951         { rtfDocAttr,   rtfLineStart,           "linestart",    0 },
1952         { rtfDocAttr,   rtfFracWidth,           "fracwidth",    0 },
1953         /* \makeback was given in old version of spec, it's now */
1954         /* listed as \makebackup */
1955         { rtfDocAttr,   rtfMakeBackup,          "makeback",     0 },
1956         { rtfDocAttr,   rtfMakeBackup,          "makebackup",   0 },
1957         { rtfDocAttr,   rtfRTFDefault,          "defformat",    0 },
1958         { rtfDocAttr,   rtfPSOverlay,           "psover",       0 },
1959         { rtfDocAttr,   rtfDocTemplate,         "doctemp",      0 },
1960         { rtfDocAttr,   rtfDefLanguage,         "deflang",      0 },
1961
1962         { rtfDocAttr,   rtfFENoteType,          "fet",          0 },
1963         { rtfDocAttr,   rtfFNoteEndSect,        "endnotes",     0 },
1964         { rtfDocAttr,   rtfFNoteEndDoc,         "enddoc",       0 },
1965         { rtfDocAttr,   rtfFNoteText,           "ftntj",        0 },
1966         { rtfDocAttr,   rtfFNoteBottom,         "ftnbj",        0 },
1967         { rtfDocAttr,   rtfENoteEndSect,        "aendnotes",    0 },
1968         { rtfDocAttr,   rtfENoteEndDoc,         "aenddoc",      0 },
1969         { rtfDocAttr,   rtfENoteText,           "aftntj",       0 },
1970         { rtfDocAttr,   rtfENoteBottom,         "aftnbj",       0 },
1971         { rtfDocAttr,   rtfFNoteStart,          "ftnstart",     0 },
1972         { rtfDocAttr,   rtfENoteStart,          "aftnstart",    0 },
1973         { rtfDocAttr,   rtfFNoteRestartPage,    "ftnrstpg",     0 },
1974         { rtfDocAttr,   rtfFNoteRestart,        "ftnrestart",   0 },
1975         { rtfDocAttr,   rtfFNoteRestartCont,    "ftnrstcont",   0 },
1976         { rtfDocAttr,   rtfENoteRestart,        "aftnrestart",  0 },
1977         { rtfDocAttr,   rtfENoteRestartCont,    "aftnrstcont",  0 },
1978         { rtfDocAttr,   rtfFNoteNumArabic,      "ftnnar",       0 },
1979         { rtfDocAttr,   rtfFNoteNumLLetter,     "ftnnalc",      0 },
1980         { rtfDocAttr,   rtfFNoteNumULetter,     "ftnnauc",      0 },
1981         { rtfDocAttr,   rtfFNoteNumLRoman,      "ftnnrlc",      0 },
1982         { rtfDocAttr,   rtfFNoteNumURoman,      "ftnnruc",      0 },
1983         { rtfDocAttr,   rtfFNoteNumChicago,     "ftnnchi",      0 },
1984         { rtfDocAttr,   rtfENoteNumArabic,      "aftnnar",      0 },
1985         { rtfDocAttr,   rtfENoteNumLLetter,     "aftnnalc",     0 },
1986         { rtfDocAttr,   rtfENoteNumULetter,     "aftnnauc",     0 },
1987         { rtfDocAttr,   rtfENoteNumLRoman,      "aftnnrlc",     0 },
1988         { rtfDocAttr,   rtfENoteNumURoman,      "aftnnruc",     0 },
1989         { rtfDocAttr,   rtfENoteNumChicago,     "aftnnchi",     0 },
1990
1991         { rtfDocAttr,   rtfPaperWidth,          "paperw",       0 },
1992         { rtfDocAttr,   rtfPaperHeight,         "paperh",       0 },
1993         { rtfDocAttr,   rtfPaperSize,           "psz",          0 },
1994         { rtfDocAttr,   rtfLeftMargin,          "margl",        0 },
1995         { rtfDocAttr,   rtfRightMargin,         "margr",        0 },
1996         { rtfDocAttr,   rtfTopMargin,           "margt",        0 },
1997         { rtfDocAttr,   rtfBottomMargin,        "margb",        0 },
1998         { rtfDocAttr,   rtfFacingPage,          "facingp",      0 },
1999         { rtfDocAttr,   rtfGutterWid,           "gutter",       0 },
2000         { rtfDocAttr,   rtfMirrorMargin,        "margmirror",   0 },
2001         { rtfDocAttr,   rtfLandscape,           "landscape",    0 },
2002         { rtfDocAttr,   rtfPageStart,           "pgnstart",     0 },
2003         { rtfDocAttr,   rtfWidowCtrl,           "widowctrl",    0 },
2004
2005         { rtfDocAttr,   rtfLinkStyles,          "linkstyles",   0 },
2006
2007         { rtfDocAttr,   rtfNoAutoTabIndent,     "notabind",     0 },
2008         { rtfDocAttr,   rtfWrapSpaces,          "wraptrsp",     0 },
2009         { rtfDocAttr,   rtfPrintColorsBlack,    "prcolbl",      0 },
2010         { rtfDocAttr,   rtfNoExtraSpaceRL,      "noextrasprl",  0 },
2011         { rtfDocAttr,   rtfNoColumnBalance,     "nocolbal",     0 },
2012         { rtfDocAttr,   rtfCvtMailMergeQuote,   "cvmme",        0 },
2013         { rtfDocAttr,   rtfSuppressTopSpace,    "sprstsp",      0 },
2014         { rtfDocAttr,   rtfSuppressPreParSpace, "sprsspbf",     0 },
2015         { rtfDocAttr,   rtfCombineTblBorders,   "otblrul",      0 },
2016         { rtfDocAttr,   rtfTranspMetafiles,     "transmf",      0 },
2017         { rtfDocAttr,   rtfSwapBorders,         "swpbdr",       0 },
2018         { rtfDocAttr,   rtfShowHardBreaks,      "brkfrm",       0 },
2019
2020         { rtfDocAttr,   rtfFormProtected,       "formprot",     0 },
2021         { rtfDocAttr,   rtfAllProtected,        "allprot",      0 },
2022         { rtfDocAttr,   rtfFormShading,         "formshade",    0 },
2023         { rtfDocAttr,   rtfFormDisplay,         "formdisp",     0 },
2024         { rtfDocAttr,   rtfPrintData,           "printdata",    0 },
2025
2026         { rtfDocAttr,   rtfRevProtected,        "revprot",      0 },
2027         { rtfDocAttr,   rtfRevisions,           "revisions",    0 },
2028         { rtfDocAttr,   rtfRevDisplay,          "revprop",      0 },
2029         { rtfDocAttr,   rtfRevBar,              "revbar",       0 },
2030
2031         { rtfDocAttr,   rtfAnnotProtected,      "annotprot",    0 },
2032
2033         { rtfDocAttr,   rtfRTLDoc,              "rtldoc",       0 },
2034         { rtfDocAttr,   rtfLTRDoc,              "ltrdoc",       0 },
2035
2036         /*
2037          * Style attributes
2038          */
2039
2040         { rtfStyleAttr, rtfAdditive,            "additive",     0 },
2041         { rtfStyleAttr, rtfBasedOn,             "sbasedon",     0 },
2042         { rtfStyleAttr, rtfNext,                "snext",        0 },
2043
2044         /*
2045          * Picture attributes
2046          */
2047
2048         { rtfPictAttr,  rtfMacQD,               "macpict",      0 },
2049         { rtfPictAttr,  rtfPMMetafile,          "pmmetafile",   0 },
2050         { rtfPictAttr,  rtfWinMetafile,         "wmetafile",    0 },
2051         { rtfPictAttr,  rtfDevIndBitmap,        "dibitmap",     0 },
2052         { rtfPictAttr,  rtfWinBitmap,           "wbitmap",      0 },
2053         { rtfPictAttr,  rtfPixelBits,           "wbmbitspixel", 0 },
2054         { rtfPictAttr,  rtfBitmapPlanes,        "wbmplanes",    0 },
2055         { rtfPictAttr,  rtfBitmapWid,           "wbmwidthbytes", 0 },
2056
2057         { rtfPictAttr,  rtfPicWid,              "picw",         0 },
2058         { rtfPictAttr,  rtfPicHt,               "pich",         0 },
2059         { rtfPictAttr,  rtfPicGoalWid,          "picwgoal",     0 },
2060         { rtfPictAttr,  rtfPicGoalHt,           "pichgoal",     0 },
2061         /* these two aren't in the spec, but some writers emit them */
2062         { rtfPictAttr,  rtfPicGoalWid,          "picwGoal",     0 },
2063         { rtfPictAttr,  rtfPicGoalHt,           "pichGoal",     0 },
2064         { rtfPictAttr,  rtfPicScaleX,           "picscalex",    0 },
2065         { rtfPictAttr,  rtfPicScaleY,           "picscaley",    0 },
2066         { rtfPictAttr,  rtfPicScaled,           "picscaled",    0 },
2067         { rtfPictAttr,  rtfPicCropTop,          "piccropt",     0 },
2068         { rtfPictAttr,  rtfPicCropBottom,       "piccropb",     0 },
2069         { rtfPictAttr,  rtfPicCropLeft,         "piccropl",     0 },
2070         { rtfPictAttr,  rtfPicCropRight,        "piccropr",     0 },
2071
2072         { rtfPictAttr,  rtfPicMFHasBitmap,      "picbmp",       0 },
2073         { rtfPictAttr,  rtfPicMFBitsPerPixel,   "picbpp",       0 },
2074
2075         { rtfPictAttr,  rtfPicBinary,           "bin",          0 },
2076
2077         /*
2078          * NeXT graphic attributes
2079          */
2080
2081         { rtfNeXTGrAttr,        rtfNeXTGWidth,          "width",        0 },
2082         { rtfNeXTGrAttr,        rtfNeXTGHeight,         "height",       0 },
2083
2084         /*
2085          * Destinations
2086          */
2087
2088         { rtfDestination,       rtfFontTbl,             "fonttbl",      0 },
2089         { rtfDestination,       rtfFontAltName,         "falt",         0 },
2090         { rtfDestination,       rtfEmbeddedFont,        "fonteb",       0 },
2091         { rtfDestination,       rtfFontFile,            "fontfile",     0 },
2092         { rtfDestination,       rtfFileTbl,             "filetbl",      0 },
2093         { rtfDestination,       rtfFileInfo,            "file",         0 },
2094         { rtfDestination,       rtfColorTbl,            "colortbl",     0 },
2095         { rtfDestination,       rtfStyleSheet,          "stylesheet",   0 },
2096         { rtfDestination,       rtfKeyCode,             "keycode",      0 },
2097         { rtfDestination,       rtfRevisionTbl,         "revtbl",       0 },
2098         { rtfDestination,       rtfInfo,                "info",         0 },
2099         { rtfDestination,       rtfITitle,              "title",        0 },
2100         { rtfDestination,       rtfISubject,            "subject",      0 },
2101         { rtfDestination,       rtfIAuthor,             "author",       0 },
2102         { rtfDestination,       rtfIOperator,           "operator",     0 },
2103         { rtfDestination,       rtfIKeywords,           "keywords",     0 },
2104         { rtfDestination,       rtfIComment,            "comment",      0 },
2105         { rtfDestination,       rtfIVersion,            "version",      0 },
2106         { rtfDestination,       rtfIDoccomm,            "doccomm",      0 },
2107         /* \verscomm may not exist -- was seen in earlier spec version */
2108         { rtfDestination,       rtfIVerscomm,           "verscomm",     0 },
2109         { rtfDestination,       rtfNextFile,            "nextfile",     0 },
2110         { rtfDestination,       rtfTemplate,            "template",     0 },
2111         { rtfDestination,       rtfFNSep,               "ftnsep",       0 },
2112         { rtfDestination,       rtfFNContSep,           "ftnsepc",      0 },
2113         { rtfDestination,       rtfFNContNotice,        "ftncn",        0 },
2114         { rtfDestination,       rtfENSep,               "aftnsep",      0 },
2115         { rtfDestination,       rtfENContSep,           "aftnsepc",     0 },
2116         { rtfDestination,       rtfENContNotice,        "aftncn",       0 },
2117         { rtfDestination,       rtfPageNumLevel,        "pgnhn",        0 },
2118         { rtfDestination,       rtfParNumLevelStyle,    "pnseclvl",     0 },
2119         { rtfDestination,       rtfHeader,              "header",       0 },
2120         { rtfDestination,       rtfFooter,              "footer",       0 },
2121         { rtfDestination,       rtfHeaderLeft,          "headerl",      0 },
2122         { rtfDestination,       rtfHeaderRight,         "headerr",      0 },
2123         { rtfDestination,       rtfHeaderFirst,         "headerf",      0 },
2124         { rtfDestination,       rtfFooterLeft,          "footerl",      0 },
2125         { rtfDestination,       rtfFooterRight,         "footerr",      0 },
2126         { rtfDestination,       rtfFooterFirst,         "footerf",      0 },
2127         { rtfDestination,       rtfParNumText,          "pntext",       0 },
2128         { rtfDestination,       rtfParNumbering,        "pn",           0 },
2129         { rtfDestination,       rtfParNumTextAfter,     "pntexta",      0 },
2130         { rtfDestination,       rtfParNumTextBefore,    "pntextb",      0 },
2131         { rtfDestination,       rtfBookmarkStart,       "bkmkstart",    0 },
2132         { rtfDestination,       rtfBookmarkEnd,         "bkmkend",      0 },
2133         { rtfDestination,       rtfPict,                "pict",         0 },
2134         { rtfDestination,       rtfObject,              "object",       0 },
2135         { rtfDestination,       rtfObjClass,            "objclass",     0 },
2136         { rtfDestination,       rtfObjName,             "objname",      0 },
2137         { rtfObjAttr,   rtfObjTime,             "objtime",      0 },
2138         { rtfDestination,       rtfObjData,             "objdata",      0 },
2139         { rtfDestination,       rtfObjAlias,            "objalias",     0 },
2140         { rtfDestination,       rtfObjSection,          "objsect",      0 },
2141         /* objitem and objtopic aren't documented in the spec! */
2142         { rtfDestination,       rtfObjItem,             "objitem",      0 },
2143         { rtfDestination,       rtfObjTopic,            "objtopic",     0 },
2144         { rtfDestination,       rtfObjResult,           "result",       0 },
2145         { rtfDestination,       rtfDrawObject,          "do",           0 },
2146         { rtfDestination,       rtfFootnote,            "footnote",     0 },
2147         { rtfDestination,       rtfAnnotRefStart,       "atrfstart",    0 },
2148         { rtfDestination,       rtfAnnotRefEnd,         "atrfend",      0 },
2149         { rtfDestination,       rtfAnnotID,             "atnid",        0 },
2150         { rtfDestination,       rtfAnnotAuthor,         "atnauthor",    0 },
2151         { rtfDestination,       rtfAnnotation,          "annotation",   0 },
2152         { rtfDestination,       rtfAnnotRef,            "atnref",       0 },
2153         { rtfDestination,       rtfAnnotTime,           "atntime",      0 },
2154         { rtfDestination,       rtfAnnotIcon,           "atnicn",       0 },
2155         { rtfDestination,       rtfField,               "field",        0 },
2156         { rtfDestination,       rtfFieldInst,           "fldinst",      0 },
2157         { rtfDestination,       rtfFieldResult,         "fldrslt",      0 },
2158         { rtfDestination,       rtfDataField,           "datafield",    0 },
2159         { rtfDestination,       rtfIndex,               "xe",           0 },
2160         { rtfDestination,       rtfIndexText,           "txe",          0 },
2161         { rtfDestination,       rtfIndexRange,          "rxe",          0 },
2162         { rtfDestination,       rtfTOC,                 "tc",           0 },
2163         { rtfDestination,       rtfNeXTGraphic,         "NeXTGraphic",  0 },
2164
2165         /*
2166          * Font families
2167          */
2168
2169         { rtfFontFamily,        rtfFFNil,               "fnil",         0 },
2170         { rtfFontFamily,        rtfFFRoman,             "froman",       0 },
2171         { rtfFontFamily,        rtfFFSwiss,             "fswiss",       0 },
2172         { rtfFontFamily,        rtfFFModern,            "fmodern",      0 },
2173         { rtfFontFamily,        rtfFFScript,            "fscript",      0 },
2174         { rtfFontFamily,        rtfFFDecor,             "fdecor",       0 },
2175         { rtfFontFamily,        rtfFFTech,              "ftech",        0 },
2176         { rtfFontFamily,        rtfFFBidirectional,     "fbidi",        0 },
2177
2178         /*
2179          * Font attributes
2180          */
2181
2182         { rtfFontAttr,  rtfFontCharSet,         "fcharset",     0 },
2183         { rtfFontAttr,  rtfFontPitch,           "fprq",         0 },
2184         { rtfFontAttr,  rtfFontCodePage,        "cpg",          0 },
2185         { rtfFontAttr,  rtfFTypeNil,            "ftnil",        0 },
2186         { rtfFontAttr,  rtfFTypeTrueType,       "fttruetype",   0 },
2187
2188         /*
2189          * File table attributes
2190          */
2191
2192         { rtfFileAttr,  rtfFileNum,             "fid",          0 },
2193         { rtfFileAttr,  rtfFileRelPath,         "frelative",    0 },
2194         { rtfFileAttr,  rtfFileOSNum,           "fosnum",       0 },
2195
2196         /*
2197          * File sources
2198          */
2199
2200         { rtfFileSource,        rtfSrcMacintosh,        "fvalidmac",    0 },
2201         { rtfFileSource,        rtfSrcDOS,              "fvaliddos",    0 },
2202         { rtfFileSource,        rtfSrcNTFS,             "fvalidntfs",   0 },
2203         { rtfFileSource,        rtfSrcHPFS,             "fvalidhpfs",   0 },
2204         { rtfFileSource,        rtfSrcNetwork,          "fnetwork",     0 },
2205
2206         /*
2207          * Color names
2208          */
2209
2210         { rtfColorName, rtfRed,                 "red",          0 },
2211         { rtfColorName, rtfGreen,               "green",        0 },
2212         { rtfColorName, rtfBlue,                "blue",         0 },
2213
2214         /*
2215          * Charset names
2216          */
2217
2218         { rtfCharSet,   rtfMacCharSet,          "mac",          0 },
2219         { rtfCharSet,   rtfAnsiCharSet,         "ansi",         0 },
2220         { rtfCharSet,   rtfPcCharSet,           "pc",           0 },
2221         { rtfCharSet,   rtfPcaCharSet,          "pca",          0 },
2222
2223         /*
2224          * Table attributes
2225          */
2226
2227         { rtfTblAttr,   rtfRowDef,              "trowd",        0 },
2228         { rtfTblAttr,   rtfRowGapH,             "trgaph",       0 },
2229         { rtfTblAttr,   rtfCellPos,             "cellx",        0 },
2230         { rtfTblAttr,   rtfMergeRngFirst,       "clmgf",        0 },
2231         { rtfTblAttr,   rtfMergePrevious,       "clmrg",        0 },
2232
2233         { rtfTblAttr,   rtfRowLeft,             "trql",         0 },
2234         { rtfTblAttr,   rtfRowRight,            "trqr",         0 },
2235         { rtfTblAttr,   rtfRowCenter,           "trqc",         0 },
2236         { rtfTblAttr,   rtfRowLeftEdge,         "trleft",       0 },
2237         { rtfTblAttr,   rtfRowHt,               "trrh",         0 },
2238         { rtfTblAttr,   rtfRowHeader,           "trhdr",        0 },
2239         { rtfTblAttr,   rtfRowKeep,             "trkeep",       0 },
2240
2241         { rtfTblAttr,   rtfRTLRow,              "rtlrow",       0 },
2242         { rtfTblAttr,   rtfLTRRow,              "ltrrow",       0 },
2243
2244         { rtfTblAttr,   rtfRowBordTop,          "trbrdrt",      0 },
2245         { rtfTblAttr,   rtfRowBordLeft,         "trbrdrl",      0 },
2246         { rtfTblAttr,   rtfRowBordBottom,       "trbrdrb",      0 },
2247         { rtfTblAttr,   rtfRowBordRight,        "trbrdrr",      0 },
2248         { rtfTblAttr,   rtfRowBordHoriz,        "trbrdrh",      0 },
2249         { rtfTblAttr,   rtfRowBordVert,         "trbrdrv",      0 },
2250
2251         { rtfTblAttr,   rtfCellBordBottom,      "clbrdrb",      0 },
2252         { rtfTblAttr,   rtfCellBordTop,         "clbrdrt",      0 },
2253         { rtfTblAttr,   rtfCellBordLeft,        "clbrdrl",      0 },
2254         { rtfTblAttr,   rtfCellBordRight,       "clbrdrr",      0 },
2255
2256         { rtfTblAttr,   rtfCellShading,         "clshdng",      0 },
2257         { rtfTblAttr,   rtfCellBgPatH,          "clbghoriz",    0 },
2258         { rtfTblAttr,   rtfCellBgPatV,          "clbgvert",     0 },
2259         { rtfTblAttr,   rtfCellFwdDiagBgPat,    "clbgfdiag",    0 },
2260         { rtfTblAttr,   rtfCellBwdDiagBgPat,    "clbgbdiag",    0 },
2261         { rtfTblAttr,   rtfCellHatchBgPat,      "clbgcross",    0 },
2262         { rtfTblAttr,   rtfCellDiagHatchBgPat,  "clbgdcross",   0 },
2263         /*
2264          * The spec lists "clbgdkhor", but the corresponding non-cell
2265          * control is "bgdkhoriz".  At any rate Macintosh Word seems
2266          * to accept both "clbgdkhor" and "clbgdkhoriz".
2267          */
2268         { rtfTblAttr,   rtfCellDarkBgPatH,      "clbgdkhoriz",  0 },
2269         { rtfTblAttr,   rtfCellDarkBgPatH,      "clbgdkhor",    0 },
2270         { rtfTblAttr,   rtfCellDarkBgPatV,      "clbgdkvert",   0 },
2271         { rtfTblAttr,   rtfCellFwdDarkBgPat,    "clbgdkfdiag",  0 },
2272         { rtfTblAttr,   rtfCellBwdDarkBgPat,    "clbgdkbdiag",  0 },
2273         { rtfTblAttr,   rtfCellDarkHatchBgPat,  "clbgdkcross",  0 },
2274         { rtfTblAttr,   rtfCellDarkDiagHatchBgPat, "clbgdkdcross",      0 },
2275         { rtfTblAttr,   rtfCellBgPatLineColor, "clcfpat",       0 },
2276         { rtfTblAttr,   rtfCellBgPatColor,      "clcbpat",      0 },
2277
2278         /*
2279          * Field attributes
2280          */
2281
2282         { rtfFieldAttr, rtfFieldDirty,          "flddirty",     0 },
2283         { rtfFieldAttr, rtfFieldEdited,         "fldedit",      0 },
2284         { rtfFieldAttr, rtfFieldLocked,         "fldlock",      0 },
2285         { rtfFieldAttr, rtfFieldPrivate,        "fldpriv",      0 },
2286         { rtfFieldAttr, rtfFieldAlt,            "fldalt",       0 },
2287
2288         /*
2289          * Positioning attributes
2290          */
2291
2292         { rtfPosAttr,   rtfAbsWid,              "absw",         0 },
2293         { rtfPosAttr,   rtfAbsHt,               "absh",         0 },
2294
2295         { rtfPosAttr,   rtfRPosMargH,           "phmrg",        0 },
2296         { rtfPosAttr,   rtfRPosPageH,           "phpg",         0 },
2297         { rtfPosAttr,   rtfRPosColH,            "phcol",        0 },
2298         { rtfPosAttr,   rtfPosX,                "posx",         0 },
2299         { rtfPosAttr,   rtfPosNegX,             "posnegx",      0 },
2300         { rtfPosAttr,   rtfPosXCenter,          "posxc",        0 },
2301         { rtfPosAttr,   rtfPosXInside,          "posxi",        0 },
2302         { rtfPosAttr,   rtfPosXOutSide,         "posxo",        0 },
2303         { rtfPosAttr,   rtfPosXRight,           "posxr",        0 },
2304         { rtfPosAttr,   rtfPosXLeft,            "posxl",        0 },
2305
2306         { rtfPosAttr,   rtfRPosMargV,           "pvmrg",        0 },
2307         { rtfPosAttr,   rtfRPosPageV,           "pvpg",         0 },
2308         { rtfPosAttr,   rtfRPosParaV,           "pvpara",       0 },
2309         { rtfPosAttr,   rtfPosY,                "posy",         0 },
2310         { rtfPosAttr,   rtfPosNegY,             "posnegy",      0 },
2311         { rtfPosAttr,   rtfPosYInline,          "posyil",       0 },
2312         { rtfPosAttr,   rtfPosYTop,             "posyt",        0 },
2313         { rtfPosAttr,   rtfPosYCenter,          "posyc",        0 },
2314         { rtfPosAttr,   rtfPosYBottom,          "posyb",        0 },
2315
2316         { rtfPosAttr,   rtfNoWrap,              "nowrap",       0 },
2317         { rtfPosAttr,   rtfDistFromTextAll,     "dxfrtext",     0 },
2318         { rtfPosAttr,   rtfDistFromTextX,       "dfrmtxtx",     0 },
2319         { rtfPosAttr,   rtfDistFromTextY,       "dfrmtxty",     0 },
2320         /* \dyfrtext no longer exists in spec 1.2, apparently */
2321         /* replaced by \dfrmtextx and \dfrmtexty. */
2322         { rtfPosAttr,   rtfTextDistY,           "dyfrtext",     0 },
2323
2324         { rtfPosAttr,   rtfDropCapLines,        "dropcapli",    0 },
2325         { rtfPosAttr,   rtfDropCapType,         "dropcapt",     0 },
2326
2327         /*
2328          * Object controls
2329          */
2330
2331         { rtfObjAttr,   rtfObjEmb,              "objemb",       0 },
2332         { rtfObjAttr,   rtfObjLink,             "objlink",      0 },
2333         { rtfObjAttr,   rtfObjAutoLink,         "objautlink",   0 },
2334         { rtfObjAttr,   rtfObjSubscriber,       "objsub",       0 },
2335         { rtfObjAttr,   rtfObjPublisher,        "objpub",       0 },
2336         { rtfObjAttr,   rtfObjICEmb,            "objicemb",     0 },
2337
2338         { rtfObjAttr,   rtfObjLinkSelf,         "linkself",     0 },
2339         { rtfObjAttr,   rtfObjLock,             "objupdate",    0 },
2340         { rtfObjAttr,   rtfObjUpdate,           "objlock",      0 },
2341
2342         { rtfObjAttr,   rtfObjHt,               "objh",         0 },
2343         { rtfObjAttr,   rtfObjWid,              "objw",         0 },
2344         { rtfObjAttr,   rtfObjSetSize,          "objsetsize",   0 },
2345         { rtfObjAttr,   rtfObjAlign,            "objalign",     0 },
2346         { rtfObjAttr,   rtfObjTransposeY,       "objtransy",    0 },
2347         { rtfObjAttr,   rtfObjCropTop,          "objcropt",     0 },
2348         { rtfObjAttr,   rtfObjCropBottom,       "objcropb",     0 },
2349         { rtfObjAttr,   rtfObjCropLeft,         "objcropl",     0 },
2350         { rtfObjAttr,   rtfObjCropRight,        "objcropr",     0 },
2351         { rtfObjAttr,   rtfObjScaleX,           "objscalex",    0 },
2352         { rtfObjAttr,   rtfObjScaleY,           "objscaley",    0 },
2353
2354         { rtfObjAttr,   rtfObjResRTF,           "rsltrtf",      0 },
2355         { rtfObjAttr,   rtfObjResPict,          "rsltpict",     0 },
2356         { rtfObjAttr,   rtfObjResBitmap,        "rsltbmp",      0 },
2357         { rtfObjAttr,   rtfObjResText,          "rslttxt",      0 },
2358         { rtfObjAttr,   rtfObjResMerge,         "rsltmerge",    0 },
2359
2360         { rtfObjAttr,   rtfObjBookmarkPubObj,   "bkmkpub",      0 },
2361         { rtfObjAttr,   rtfObjPubAutoUpdate,    "pubauto",      0 },
2362
2363         /*
2364          * Associated character formatting attributes
2365          */
2366
2367         { rtfACharAttr, rtfACBold,              "ab",           0 },
2368         { rtfACharAttr, rtfACAllCaps,           "caps",         0 },
2369         { rtfACharAttr, rtfACForeColor,         "acf",          0 },
2370         { rtfACharAttr, rtfACSubScript,         "adn",          0 },
2371         { rtfACharAttr, rtfACExpand,            "aexpnd",       0 },
2372         { rtfACharAttr, rtfACFontNum,           "af",           0 },
2373         { rtfACharAttr, rtfACFontSize,          "afs",          0 },
2374         { rtfACharAttr, rtfACItalic,            "ai",           0 },
2375         { rtfACharAttr, rtfACLanguage,          "alang",        0 },
2376         { rtfACharAttr, rtfACOutline,           "aoutl",        0 },
2377         { rtfACharAttr, rtfACSmallCaps,         "ascaps",       0 },
2378         { rtfACharAttr, rtfACShadow,            "ashad",        0 },
2379         { rtfACharAttr, rtfACStrikeThru,        "astrike",      0 },
2380         { rtfACharAttr, rtfACUnderline,         "aul",          0 },
2381         { rtfACharAttr, rtfACDotUnderline,      "auld",         0 },
2382         { rtfACharAttr, rtfACDbUnderline,       "auldb",        0 },
2383         { rtfACharAttr, rtfACNoUnderline,       "aulnone",      0 },
2384         { rtfACharAttr, rtfACWordUnderline,     "aulw",         0 },
2385         { rtfACharAttr, rtfACSuperScript,       "aup",          0 },
2386
2387         /*
2388          * Footnote attributes
2389          */
2390
2391         { rtfFNoteAttr, rtfFNAlt,               "ftnalt",       0 },
2392
2393         /*
2394          * Key code attributes
2395          */
2396
2397         { rtfKeyCodeAttr,       rtfAltKey,              "alt",          0 },
2398         { rtfKeyCodeAttr,       rtfShiftKey,            "shift",        0 },
2399         { rtfKeyCodeAttr,       rtfControlKey,          "ctrl",         0 },
2400         { rtfKeyCodeAttr,       rtfFunctionKey,         "fn",           0 },
2401
2402         /*
2403          * Bookmark attributes
2404          */
2405
2406         { rtfBookmarkAttr, rtfBookmarkFirstCol, "bkmkcolf",     0 },
2407         { rtfBookmarkAttr, rtfBookmarkLastCol,  "bkmkcoll",     0 },
2408
2409         /*
2410          * Index entry attributes
2411          */
2412
2413         { rtfIndexAttr, rtfIndexNumber,         "xef",          0 },
2414         { rtfIndexAttr, rtfIndexBold,           "bxe",          0 },
2415         { rtfIndexAttr, rtfIndexItalic,         "ixe",          0 },
2416
2417         /*
2418          * Table of contents attributes
2419          */
2420
2421         { rtfTOCAttr,   rtfTOCType,             "tcf",          0 },
2422         { rtfTOCAttr,   rtfTOCLevel,            "tcl",          0 },
2423
2424         /*
2425          * Drawing object attributes
2426          */
2427
2428         { rtfDrawAttr,  rtfDrawLock,            "dolock",       0 },
2429         { rtfDrawAttr,  rtfDrawPageRelX,        "doxpage",      0 },
2430         { rtfDrawAttr,  rtfDrawColumnRelX,      "dobxcolumn",   0 },
2431         { rtfDrawAttr,  rtfDrawMarginRelX,      "dobxmargin",   0 },
2432         { rtfDrawAttr,  rtfDrawPageRelY,        "dobypage",     0 },
2433         { rtfDrawAttr,  rtfDrawColumnRelY,      "dobycolumn",   0 },
2434         { rtfDrawAttr,  rtfDrawMarginRelY,      "dobymargin",   0 },
2435         { rtfDrawAttr,  rtfDrawHeight,          "dobhgt",       0 },
2436
2437         { rtfDrawAttr,  rtfDrawBeginGroup,      "dpgroup",      0 },
2438         { rtfDrawAttr,  rtfDrawGroupCount,      "dpcount",      0 },
2439         { rtfDrawAttr,  rtfDrawEndGroup,        "dpendgroup",   0 },
2440         { rtfDrawAttr,  rtfDrawArc,             "dparc",        0 },
2441         { rtfDrawAttr,  rtfDrawCallout,         "dpcallout",    0 },
2442         { rtfDrawAttr,  rtfDrawEllipse,         "dpellipse",    0 },
2443         { rtfDrawAttr,  rtfDrawLine,            "dpline",       0 },
2444         { rtfDrawAttr,  rtfDrawPolygon,         "dppolygon",    0 },
2445         { rtfDrawAttr,  rtfDrawPolyLine,        "dppolyline",   0 },
2446         { rtfDrawAttr,  rtfDrawRect,            "dprect",       0 },
2447         { rtfDrawAttr,  rtfDrawTextBox,         "dptxbx",       0 },
2448
2449         { rtfDrawAttr,  rtfDrawOffsetX,         "dpx",          0 },
2450         { rtfDrawAttr,  rtfDrawSizeX,           "dpxsize",      0 },
2451         { rtfDrawAttr,  rtfDrawOffsetY,         "dpy",          0 },
2452         { rtfDrawAttr,  rtfDrawSizeY,           "dpysize",      0 },
2453
2454         { rtfDrawAttr,  rtfCOAngle,             "dpcoa",        0 },
2455         { rtfDrawAttr,  rtfCOAccentBar,         "dpcoaccent",   0 },
2456         { rtfDrawAttr,  rtfCOBestFit,           "dpcobestfit",  0 },
2457         { rtfDrawAttr,  rtfCOBorder,            "dpcoborder",   0 },
2458         { rtfDrawAttr,  rtfCOAttachAbsDist,     "dpcodabs",     0 },
2459         { rtfDrawAttr,  rtfCOAttachBottom,      "dpcodbottom",  0 },
2460         { rtfDrawAttr,  rtfCOAttachCenter,      "dpcodcenter",  0 },
2461         { rtfDrawAttr,  rtfCOAttachTop,         "dpcodtop",     0 },
2462         { rtfDrawAttr,  rtfCOLength,            "dpcolength",   0 },
2463         { rtfDrawAttr,  rtfCONegXQuadrant,      "dpcominusx",   0 },
2464         { rtfDrawAttr,  rtfCONegYQuadrant,      "dpcominusy",   0 },
2465         { rtfDrawAttr,  rtfCOOffset,            "dpcooffset",   0 },
2466         { rtfDrawAttr,  rtfCOAttachSmart,       "dpcosmarta",   0 },
2467         { rtfDrawAttr,  rtfCODoubleLine,        "dpcotdouble",  0 },
2468         { rtfDrawAttr,  rtfCORightAngle,        "dpcotright",   0 },
2469         { rtfDrawAttr,  rtfCOSingleLine,        "dpcotsingle",  0 },
2470         { rtfDrawAttr,  rtfCOTripleLine,        "dpcottriple",  0 },
2471
2472         { rtfDrawAttr,  rtfDrawTextBoxMargin,   "dptxbxmar",    0 },
2473         { rtfDrawAttr,  rtfDrawTextBoxText,     "dptxbxtext",   0 },
2474         { rtfDrawAttr,  rtfDrawRoundRect,       "dproundr",     0 },
2475
2476         { rtfDrawAttr,  rtfDrawPointX,          "dpptx",        0 },
2477         { rtfDrawAttr,  rtfDrawPointY,          "dppty",        0 },
2478         { rtfDrawAttr,  rtfDrawPolyCount,       "dppolycount",  0 },
2479
2480         { rtfDrawAttr,  rtfDrawArcFlipX,        "dparcflipx",   0 },
2481         { rtfDrawAttr,  rtfDrawArcFlipY,        "dparcflipy",   0 },
2482
2483         { rtfDrawAttr,  rtfDrawLineBlue,        "dplinecob",    0 },
2484         { rtfDrawAttr,  rtfDrawLineGreen,       "dplinecog",    0 },
2485         { rtfDrawAttr,  rtfDrawLineRed,         "dplinecor",    0 },
2486         { rtfDrawAttr,  rtfDrawLinePalette,     "dplinepal",    0 },
2487         { rtfDrawAttr,  rtfDrawLineDashDot,     "dplinedado",   0 },
2488         { rtfDrawAttr,  rtfDrawLineDashDotDot,  "dplinedadodo", 0 },
2489         { rtfDrawAttr,  rtfDrawLineDash,        "dplinedash",   0 },
2490         { rtfDrawAttr,  rtfDrawLineDot,         "dplinedot",    0 },
2491         { rtfDrawAttr,  rtfDrawLineGray,        "dplinegray",   0 },
2492         { rtfDrawAttr,  rtfDrawLineHollow,      "dplinehollow", 0 },
2493         { rtfDrawAttr,  rtfDrawLineSolid,       "dplinesolid",  0 },
2494         { rtfDrawAttr,  rtfDrawLineWidth,       "dplinew",      0 },
2495
2496         { rtfDrawAttr,  rtfDrawHollowEndArrow,  "dpaendhol",    0 },
2497         { rtfDrawAttr,  rtfDrawEndArrowLength,  "dpaendl",      0 },
2498         { rtfDrawAttr,  rtfDrawSolidEndArrow,   "dpaendsol",    0 },
2499         { rtfDrawAttr,  rtfDrawEndArrowWidth,   "dpaendw",      0 },
2500         { rtfDrawAttr,  rtfDrawHollowStartArrow,"dpastarthol",  0 },
2501         { rtfDrawAttr,  rtfDrawStartArrowLength,"dpastartl",    0 },
2502         { rtfDrawAttr,  rtfDrawSolidStartArrow, "dpastartsol",  0 },
2503         { rtfDrawAttr,  rtfDrawStartArrowWidth, "dpastartw",    0 },
2504
2505         { rtfDrawAttr,  rtfDrawBgFillBlue,      "dpfillbgcb",   0 },
2506         { rtfDrawAttr,  rtfDrawBgFillGreen,     "dpfillbgcg",   0 },
2507         { rtfDrawAttr,  rtfDrawBgFillRed,       "dpfillbgcr",   0 },
2508         { rtfDrawAttr,  rtfDrawBgFillPalette,   "dpfillbgpal",  0 },
2509         { rtfDrawAttr,  rtfDrawBgFillGray,      "dpfillbggray", 0 },
2510         { rtfDrawAttr,  rtfDrawFgFillBlue,      "dpfillfgcb",   0 },
2511         { rtfDrawAttr,  rtfDrawFgFillGreen,     "dpfillfgcg",   0 },
2512         { rtfDrawAttr,  rtfDrawFgFillRed,       "dpfillfgcr",   0 },
2513         { rtfDrawAttr,  rtfDrawFgFillPalette,   "dpfillfgpal",  0 },
2514         { rtfDrawAttr,  rtfDrawFgFillGray,      "dpfillfggray", 0 },
2515         { rtfDrawAttr,  rtfDrawFillPatIndex,    "dpfillpat",    0 },
2516
2517         { rtfDrawAttr,  rtfDrawShadow,          "dpshadow",     0 },
2518         { rtfDrawAttr,  rtfDrawShadowXOffset,   "dpshadx",      0 },
2519         { rtfDrawAttr,  rtfDrawShadowYOffset,   "dpshady",      0 },
2520
2521         { rtfVersion,   -1,                     "rtf",          0 },
2522         { rtfDefFont,   -1,                     "deff",         0 },
2523
2524         { 0,            -1,                     (char *) NULL,  0 }
2525 };
2526
2527
2528 /*
2529  * Initialize lookup table hash values.  Only need to do this once.
2530  */
2531
2532 static void LookupInit(void)
2533 {
2534 static int      inited = 0;
2535 RTFKey  *rp;
2536
2537         if (inited == 0)
2538         {
2539                 for (rp = rtfKey; rp->rtfKStr != (char *) NULL; rp++)
2540                         rp->rtfKHash = Hash (rp->rtfKStr);
2541                 ++inited;
2542         }
2543 }
2544
2545
2546 /*
2547  * Determine major and minor number of control token.  If it's
2548  * not found, the class turns into rtfUnknown.
2549  */
2550
2551 static void Lookup(char *s)
2552 {
2553 RTFKey  *rp;
2554 int     hash;
2555
2556         TRACE("\n");
2557         ++s;                    /* skip over the leading \ character */
2558         hash = Hash (s);
2559         for (rp = rtfKey; rp->rtfKStr != (char *) NULL; rp++)
2560         {
2561                 if (hash == rp->rtfKHash && strcmp (s, rp->rtfKStr) == 0)
2562                 {
2563                         rtfClass = rtfControl;
2564                         rtfMajor = rp->rtfKMajor;
2565                         rtfMinor = rp->rtfKMinor;
2566                         return;
2567                 }
2568         }
2569         rtfClass = rtfUnknown;
2570 }
2571
2572
2573 /*
2574  * Compute hash value of symbol
2575  */
2576
2577 static int Hash(char *s)
2578 {
2579 char    c;
2580 int     val = 0;
2581
2582         while ((c = *s++) != '\0')
2583                 val += (int) c;
2584         return (val);
2585 }
2586
2587
2588 /* ---------------------------------------------------------------------- */
2589
2590 /*
2591  * Memory allocation routines
2592  */
2593
2594
2595 /*
2596  * Return pointer to block of size bytes, or NULL if there's
2597  * not enough memory available.
2598  *
2599  * This is called through RTFAlloc(), a define which coerces the
2600  * argument to int.  This avoids the persistent problem of allocation
2601  * failing under THINK C when a long is passed.
2602  */
2603
2604 char *_RTFAlloc(int size)
2605 {
2606         return HeapAlloc(RICHED32_hHeap, 0, size);
2607 }
2608
2609
2610 /*
2611  * Saves a string on the heap and returns a pointer to it.
2612  */
2613
2614
2615 char *RTFStrSave(char *s)
2616 {
2617 char    *p;
2618
2619         if ((p = RTFAlloc ((int) (strlen (s) + 1))) == (char *) NULL)
2620                 return ((char *) NULL);
2621         return (strcpy (p, s));
2622 }
2623
2624
2625 void RTFFree(char *p)
2626 {
2627         if (p != (char *) NULL)
2628                 HeapFree(RICHED32_hHeap, 0, p);
2629 }
2630
2631
2632 /* ---------------------------------------------------------------------- */
2633
2634
2635 /*
2636  * Token comparison routines
2637  */
2638
2639 int RTFCheckCM(int class, int major)
2640 {
2641         return (rtfClass == class && rtfMajor == major);
2642 }
2643
2644
2645 int RTFCheckCMM(int class, int major, int minor)
2646 {
2647         return (rtfClass == class && rtfMajor == major && rtfMinor == minor);
2648 }
2649
2650
2651 int RTFCheckMM(int major, int minor)
2652 {
2653         return (rtfMajor == major && rtfMinor == minor);
2654 }
2655
2656
2657 /* ---------------------------------------------------------------------- */
2658
2659
2660 int RTFCharToHex(char c)
2661 {
2662         if (isupper (c))
2663                 c = tolower (c);
2664         if (isdigit (c))
2665                 return (c - '0');       /* '0'..'9' */
2666         return (c - 'a' + 10);          /* 'a'..'f' */
2667 }
2668
2669
2670 int RTFHexToChar(int i)
2671 {
2672         if (i < 10)
2673                 return (i + '0');
2674         return (i - 10 + 'a');
2675 }
2676
2677
2678 /* ---------------------------------------------------------------------- */
2679
2680 /*
2681  * RTFReadOutputMap() -- Read output translation map
2682  */
2683
2684 /*
2685  * Read in an array describing the relation between the standard character set
2686  * and an RTF translator's corresponding output sequences.  Each line consists
2687  * of a standard character name and the output sequence for that character.
2688  *
2689  * outMap is an array of strings into which the sequences should be placed.
2690  * It should be declared like this in the calling program:
2691  *
2692  *      char *outMap[rtfSC_MaxChar];
2693  *
2694  * reinit should be non-zero if outMap should be initialized
2695  * zero otherwise.
2696  *
2697  */
2698
2699 int RTFReadOutputMap(char *outMap[], int reinit)
2700 {
2701     int  i;
2702     int  stdCode;
2703     char *name, *seq;
2704
2705     if (reinit)
2706     {
2707         for (i = 0; i < rtfSC_MaxChar; i++)
2708         {
2709             outMap[i] = (char *) NULL;
2710         }
2711     }
2712
2713     for (i=0 ;i< sizeof(text_map)/sizeof(char*); i+=2)
2714     {
2715         name = text_map[i];
2716         seq  = text_map[i+1];
2717         stdCode = RTFStdCharCode( name );
2718         outMap[stdCode] = seq;
2719     }
2720
2721     return (1);
2722 }
2723
2724 /* ---------------------------------------------------------------------- */
2725
2726 /*
2727  * Open a library file.
2728  */
2729
2730
2731 static FILE     *(*libFileOpen) () = NULL;
2732
2733
2734
2735 void RTFSetOpenLibFileProc(FILE *(*proc)())
2736 {
2737     libFileOpen = proc;
2738 }
2739
2740
2741 FILE *RTFOpenLibFile (char *file, char *mode)
2742 {
2743         if (libFileOpen == NULL)
2744                 return ((FILE *) NULL);
2745         return ((*libFileOpen) (file, mode));
2746 }
2747
2748
2749 /* ---------------------------------------------------------------------- */
2750
2751 /*
2752  * Print message.  Default is to send message to stderr
2753  * but this may be overridden with RTFSetMsgProc().
2754  *
2755  * Message should include linefeeds as necessary.  If the default
2756  * function is overridden, the overriding function may want to
2757  * map linefeeds to another line ending character or sequence if
2758  * the host system doesn't use linefeeds.
2759  */
2760
2761
2762 static void DefaultMsgProc(char *s)
2763 {
2764     MESSAGE( "%s", s);
2765 }
2766
2767
2768 static RTFFuncPtr       msgProc = DefaultMsgProc;
2769
2770
2771 void RTFSetMsgProc(RTFFuncPtr proc)
2772 {
2773         msgProc = proc;
2774 }
2775
2776
2777 void RTFMsg (char *fmt, ...)
2778 {
2779 char    buf[rtfBufSiz];
2780
2781         va_list args;
2782         va_start (args,fmt);
2783         vsprintf (buf, fmt, args);
2784         va_end (args);
2785         (*msgProc) (buf);
2786 }
2787
2788
2789 /* ---------------------------------------------------------------------- */
2790
2791
2792 /*
2793  * Process termination.  Print error message and exit.  Also prints
2794  * current token, and current input line number and position within
2795  * line if any input has been read from the current file.  (No input
2796  * has been read if prevChar is EOF).
2797  */
2798
2799 static void DefaultPanicProc(char *s)
2800 {
2801     MESSAGE( "%s", s);
2802         /*exit (1);*/
2803 }
2804
2805
2806 static RTFFuncPtr       panicProc = DefaultPanicProc;
2807
2808
2809 void RTFSetPanicProc(RTFFuncPtr proc)
2810 {
2811         panicProc = proc;
2812 }
2813
2814
2815 void RTFPanic(char *fmt, ...)
2816 {
2817 char    buf[rtfBufSiz];
2818
2819         va_list args;
2820         va_start (args,fmt);
2821         vsprintf (buf, fmt, args);
2822         va_end (args);
2823         (void) strcat (buf, "\n");
2824         if (prevChar != EOF && rtfTextBuf != (char *) NULL)
2825         {
2826                 sprintf (buf + strlen (buf),
2827                         "Last token read was \"%s\" near line %ld, position %d.\n",
2828                         rtfTextBuf, rtfLineNum, rtfLinePos);
2829         }
2830         (*panicProc) (buf);
2831 }