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