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