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