2 * text-writer -- RTF-to-text translation writer code.
4 * Read RTF input, write text of document (text extraction).
6 * Wrapper must call WriterInit() once before processing any files,
7 * then set up input and call BeginFile() for each input file.
9 * This installs callbacks for the text and control token classes.
10 * The control class is necessary so that special characters such as
11 * \par, \tab, \sect, etc. can be converted.
13 * It's problematic what to do with text in headers and footers, and
14 * what to do about tables.
16 * This really is quite a stupid program, for instance, it could keep
17 * track of the current leader character and dump that out when a tab
20 * 04 Feb 91 Paul DuBois dubois@primate.wisc.edu
22 * This software may be redistributed without restriction and used for
23 * any purpose whatsoever.
28 * - Updated for distribution 1.05.
30 * - Updated to compile under THINK C 6.0.
32 * - Added Mike Sendall's entries for Macintosh char map.
34 * - Uses charset map and output sequence map for character translation.
36 * - Updated for 1.10 distribution.
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
48 static void TextClass (RTF_Info *info);
49 static void ControlClass (RTF_Info *info);
50 static void Destination (RTF_Info *info);
51 static void SpecialChar (RTF_Info *info);
52 static void PutStdChar (RTF_Info *info, int stdCode);
53 static void PutLitChar (RTF_Info *info, int c);
54 static void PutLitStr (RTF_Info *info, char *s);
57 static char *outMap[rtfSC_MaxChar];
59 static CHARLIST charlist = {0, NULL, NULL};
62 /*int RTFToBuffer(char* pBuffer, int nBufferSize); */
63 int RTFToBuffer(RTF_Info *info, char* pBuffer, int nBufferSize)
66 /* check if the buffer is big enough to hold all characters */
67 /* we require one more for the '\0' */
71 if(nBufferSize < info->charlist.nCount + 1) {
72 return info->charlist.nCount + CHARLIST_CountChar(&info->charlist, '\n') + 1;
75 while(info->charlist.nCount)
77 *pBuffer = CHARLIST_Dequeue(&info->charlist);
93 * Initialize the writer.
97 WriterInit (RTF_Info *info )
99 RTFReadOutputMap (info, info->outMap,1);
104 BeginFile (RTF_Info *info )
106 /* install class callbacks */
108 RTFSetClassCallback (info, rtfText, TextClass);
109 RTFSetClassCallback (info, rtfControl, ControlClass);
116 * Write out a character. rtfMajor contains the input character, rtfMinor
117 * contains the corresponding standard character code.
119 * If the input character isn't in the charset map, try to print some
120 * representation of it.
124 TextClass (RTF_Info *info)
130 if (info->rtfFormat == SF_TEXT)
131 PutLitChar (info, info->rtfMajor);
132 else if (info->rtfMinor != rtfSC_nothing)
133 PutStdChar (info, info->rtfMinor);
136 if (info->rtfMajor < 128) /* in ASCII range */
137 sprintf (buf, "[[%c]]", info->rtfMajor);
139 sprintf (buf, "[[\\'%02x]]", info->rtfMajor);
140 PutLitStr (info, buf);
146 ControlClass (RTF_Info *info)
149 switch (info->rtfMajor)
162 * This function notices destinations that should be ignored
163 * and skips to their ends. This keeps, for instance, picture
164 * data from being considered as plain text.
168 Destination (RTF_Info *info)
173 switch (info->rtfMinor)
177 case rtfFNContNotice:
195 * The reason these use the rtfSC_xxx thingies instead of just writing
196 * out ' ', '-', '"', etc., is so that the mapping for these characters
197 * can be controlled by the text-map file.
200 void SpecialChar (RTF_Info *info)
205 switch (info->rtfMinor)
212 PutLitChar (info, '\n');
215 PutStdChar (info, rtfSC_space); /* make sure cells are separated */
218 PutStdChar (info, rtfSC_nobrkspace);
221 PutLitChar (info, '\t');
224 PutStdChar (info, rtfSC_nobrkhyphen);
227 PutStdChar (info, rtfSC_bullet);
230 PutStdChar (info, rtfSC_emdash);
233 PutStdChar (info, rtfSC_endash);
236 PutStdChar (info, rtfSC_quoteleft);
239 PutStdChar (info, rtfSC_quoteright);
242 PutStdChar (info, rtfSC_quotedblleft);
245 PutStdChar (info, rtfSC_quotedblright);
252 * Eventually this should keep track of the destination of the
253 * current state and only write text when in the initial state.
255 * If the output sequence is unspecified in the output map, write
256 * the character's standard name instead. This makes map deficiencies
257 * obvious and provides incentive to fix it. :-)
260 void PutStdChar (RTF_Info *info, int stdCode)
263 char *oStr = (char *) NULL;
266 /* if (stdCode == rtfSC_nothing)
267 RTFPanic ("Unknown character code, logic error\n");
271 oStr = info->outMap[stdCode];
272 if (oStr == (char *) NULL) /* no output sequence in map */
274 sprintf (buf, "[[%s]]", RTFStdCharName (info, stdCode));
277 PutLitStr (info, oStr);
281 void PutLitChar (RTF_Info *info, int c)
283 CHARLIST_Enqueue(&info->charlist, (char) c);
284 /* fputc (c, ostream); */
288 static void PutLitStr (RTF_Info *info, char *s)
292 CHARLIST_Enqueue(&info->charlist, *s);
294 /* fputs (s, ostream); */