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