Release 20030408.
[wine/gsoc-2012-control.git] / dlls / richedit / text-writer.c
blobcad8a919b60efc7b4a31841c4601843e0a71eaa6
1 /*
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
18 * is encountered.
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.
25 * 04 Feb 91
26 * -Created.
27 * 27 Feb 91
28 * - Updated for distribution 1.05.
29 * 13 Jul 93
30 * - Updated to compile under THINK C 6.0.
31 * 31 Aug 93
32 * - Added Mike Sendall's entries for Macintosh char map.
33 * 07 Sep 93
34 * - Uses charset map and output sequence map for character translation.
35 * 11 Mar 94
36 * - Updated for 1.10 distribution.
39 #include <stdio.h>
41 #include "rtf.h"
42 #include "rtf2text.h"
43 #include "charlist.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
48 static void TextClass ();
49 static void ControlClass ();
50 static void Destination ();
51 static void SpecialChar ();
52 static void PutStdChar ();
53 static void PutLitChar ();
54 static void PutLitStr ();
56 static char *outMap[rtfSC_MaxChar];
58 static CHARLIST charlist = {0, NULL, NULL};
60 int RTFToBuffer(char* pBuffer, int nBufferSize);
61 int RTFToBuffer(char* pBuffer, int nBufferSize)
64 /* check if the buffer is big enough to hold all characters */
65 /* we require one more for the '\0' */
67 TRACE("\n");
69 if(nBufferSize < charlist.nCount + 1) {
70 return charlist.nCount + CHARLIST_CountChar(&charlist, '\n') + 1;
73 while(charlist.nCount)
75 *pBuffer = CHARLIST_Dequeue(&charlist);
76 if(*pBuffer=='\n')
78 *pBuffer = '\r';
79 pBuffer++;
80 *pBuffer = '\n';
82 pBuffer++;
84 *pBuffer = '\0';
86 return 0;
91 * Initialize the writer.
94 void
95 WriterInit ()
97 RTFReadOutputMap (outMap,1);
102 BeginFile ()
104 /* install class callbacks */
106 RTFSetClassCallback (rtfText, TextClass);
107 RTFSetClassCallback (rtfControl, ControlClass);
109 return (1);
114 * Write out a character. rtfMajor contains the input character, rtfMinor
115 * contains the corresponding standard character code.
117 * If the input character isn't in the charset map, try to print some
118 * representation of it.
121 static void
122 TextClass ()
124 char buf[rtfBufSiz];
126 TRACE("\n");
128 if (rtfFormat == SF_TEXT)
129 PutLitChar (rtfMajor);
130 else if (rtfMinor != rtfSC_nothing)
131 PutStdChar (rtfMinor);
132 else
134 if (rtfMajor < 128) /* in ASCII range */
135 sprintf (buf, "[[%c]]", rtfMajor);
136 else
137 sprintf (buf, "[[\\'%02x]]", rtfMajor);
138 PutLitStr (buf);
143 static void
144 ControlClass ()
146 TRACE("\n");
147 switch (rtfMajor)
149 case rtfDestination:
150 Destination ();
151 break;
152 case rtfSpecialChar:
153 SpecialChar ();
154 break;
160 * This function notices destinations that should be ignored
161 * and skips to their ends. This keeps, for instance, picture
162 * data from being considered as plain text.
165 static void
166 Destination ()
169 TRACE("\n");
171 switch (rtfMinor)
173 case rtfPict:
174 case rtfFNContSep:
175 case rtfFNContNotice:
176 case rtfInfo:
177 case rtfIndexRange:
178 case rtfITitle:
179 case rtfISubject:
180 case rtfIAuthor:
181 case rtfIOperator:
182 case rtfIKeywords:
183 case rtfIComment:
184 case rtfIVersion:
185 case rtfIDoccomm:
186 RTFSkipGroup ();
187 break;
193 * The reason these use the rtfSC_xxx thingies instead of just writing
194 * out ' ', '-', '"', etc., is so that the mapping for these characters
195 * can be controlled by the text-map file.
198 void SpecialChar ()
201 TRACE("\n");
203 switch (rtfMinor)
205 case rtfPage:
206 case rtfSect:
207 case rtfRow:
208 case rtfLine:
209 case rtfPar:
210 PutLitChar ('\n');
211 break;
212 case rtfCell:
213 PutStdChar (rtfSC_space); /* make sure cells are separated */
214 break;
215 case rtfNoBrkSpace:
216 PutStdChar (rtfSC_nobrkspace);
217 break;
218 case rtfTab:
219 PutLitChar ('\t');
220 break;
221 case rtfNoBrkHyphen:
222 PutStdChar (rtfSC_nobrkhyphen);
223 break;
224 case rtfBullet:
225 PutStdChar (rtfSC_bullet);
226 break;
227 case rtfEmDash:
228 PutStdChar (rtfSC_emdash);
229 break;
230 case rtfEnDash:
231 PutStdChar (rtfSC_endash);
232 break;
233 case rtfLQuote:
234 PutStdChar (rtfSC_quoteleft);
235 break;
236 case rtfRQuote:
237 PutStdChar (rtfSC_quoteright);
238 break;
239 case rtfLDblQuote:
240 PutStdChar (rtfSC_quotedblleft);
241 break;
242 case rtfRDblQuote:
243 PutStdChar (rtfSC_quotedblright);
244 break;
250 * Eventually this should keep track of the destination of the
251 * current state and only write text when in the initial state.
253 * If the output sequence is unspecified in the output map, write
254 * the character's standard name instead. This makes map deficiencies
255 * obvious and provides incentive to fix it. :-)
258 void PutStdChar (int stdCode)
261 char *oStr = (char *) NULL;
262 char buf[rtfBufSiz];
264 /* if (stdCode == rtfSC_nothing)
265 RTFPanic ("Unknown character code, logic error\n");
267 TRACE("\n");
269 oStr = outMap[stdCode];
270 if (oStr == (char *) NULL) /* no output sequence in map */
272 sprintf (buf, "[[%s]]", RTFStdCharName (stdCode));
273 oStr = buf;
275 PutLitStr (oStr);
279 void PutLitChar (int c)
281 CHARLIST_Enqueue(&charlist, (char) c);
282 /* fputc (c, ostream); */
286 static void PutLitStr (char *s)
288 for(;*s;s++)
290 CHARLIST_Enqueue(&charlist, *s);
292 /* fputs (s, ostream); */