Visual C++ backend for rbuild (for now just a hacked mingw backend) and related compi...
[reactos.git] / dll / win32 / riched20 / writer.c
1 /*
2 * RichEdit - RTF writer module
3 *
4 * Copyright 2005 by Phil Krylov
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include "editor.h"
25 #include "rtf.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
28
29
30 static BOOL
31 ME_StreamOutRTFText(ME_OutStream *pStream, const WCHAR *text, LONG nChars);
32
33
34 static ME_OutStream*
35 ME_StreamOutInit(ME_TextEditor *editor, EDITSTREAM *stream)
36 {
37 ME_OutStream *pStream = ALLOC_OBJ(ME_OutStream);
38 pStream->stream = stream;
39 pStream->stream->dwError = 0;
40 pStream->pos = 0;
41 pStream->written = 0;
42 pStream->nFontTblLen = 0;
43 pStream->nColorTblLen = 1;
44 pStream->nNestingLevel = 0;
45 return pStream;
46 }
47
48
49 static BOOL
50 ME_StreamOutFlush(ME_OutStream *pStream)
51 {
52 LONG nStart = 0;
53 LONG nWritten = 0;
54 LONG nRemaining = 0;
55 EDITSTREAM *stream = pStream->stream;
56
57 do {
58 TRACE("sending %u bytes\n", pStream->pos - nStart);
59 /* Some apps seem not to set *pcb unless a problem arises, relying
60 on initial random nWritten value, which is usually >STREAMOUT_BUFFER_SIZE */
61 nRemaining = pStream->pos - nStart;
62 nWritten = 0xDEADBEEF;
63 stream->dwError = stream->pfnCallback(stream->dwCookie, (LPBYTE)pStream->buffer + nStart,
64 pStream->pos - nStart, &nWritten);
65 TRACE("error=%u written=%u\n", stream->dwError, nWritten);
66 if (nWritten > (pStream->pos - nStart) || nWritten<0) {
67 FIXME("Invalid returned written size *pcb: 0x%x (%d) instead of %d\n",
68 (unsigned)nWritten, nWritten, nRemaining);
69 nWritten = nRemaining;
70 }
71 if (nWritten == 0 || stream->dwError)
72 return FALSE;
73 pStream->written += nWritten;
74 nStart += nWritten;
75 } while (nStart < pStream->pos);
76 pStream->pos = 0;
77 return TRUE;
78 }
79
80
81 static LONG
82 ME_StreamOutFree(ME_OutStream *pStream)
83 {
84 LONG written = pStream->written;
85 TRACE("total length = %u\n", written);
86
87 FREE_OBJ(pStream);
88 return written;
89 }
90
91
92 static BOOL
93 ME_StreamOutMove(ME_OutStream *pStream, const char *buffer, int len)
94 {
95 while (len) {
96 int space = STREAMOUT_BUFFER_SIZE - pStream->pos;
97 int fit = min(space, len);
98
99 TRACE("%u:%u:%s\n", pStream->pos, fit, debugstr_an(buffer,fit));
100 memmove(pStream->buffer + pStream->pos, buffer, fit);
101 len -= fit;
102 buffer += fit;
103 pStream->pos += fit;
104 if (pStream->pos == STREAMOUT_BUFFER_SIZE) {
105 if (!ME_StreamOutFlush(pStream))
106 return FALSE;
107 }
108 }
109 return TRUE;
110 }
111
112
113 static BOOL
114 ME_StreamOutPrint(ME_OutStream *pStream, const char *format, ...)
115 {
116 char string[STREAMOUT_BUFFER_SIZE]; /* This is going to be enough */
117 int len;
118 va_list valist;
119
120 va_start(valist, format);
121 len = vsnprintf(string, sizeof(string), format, valist);
122 va_end(valist);
123
124 return ME_StreamOutMove(pStream, string, len);
125 }
126
127
128 static BOOL
129 ME_StreamOutRTFHeader(ME_OutStream *pStream, int dwFormat)
130 {
131 const char *cCharSet = NULL;
132 UINT nCodePage;
133 LANGID language;
134 BOOL success;
135
136 if (dwFormat & SF_USECODEPAGE) {
137 CPINFOEXW info;
138
139 switch (HIWORD(dwFormat)) {
140 case CP_ACP:
141 cCharSet = "ansi";
142 nCodePage = GetACP();
143 break;
144 case CP_OEMCP:
145 nCodePage = GetOEMCP();
146 if (nCodePage == 437)
147 cCharSet = "pc";
148 else if (nCodePage == 850)
149 cCharSet = "pca";
150 else
151 cCharSet = "ansi";
152 break;
153 case CP_UTF8:
154 nCodePage = CP_UTF8;
155 break;
156 default:
157 if (HIWORD(dwFormat) == CP_MACCP) {
158 cCharSet = "mac";
159 nCodePage = 10000; /* MacRoman */
160 } else {
161 cCharSet = "ansi";
162 nCodePage = 1252; /* Latin-1 */
163 }
164 if (GetCPInfoExW(HIWORD(dwFormat), 0, &info))
165 nCodePage = info.CodePage;
166 }
167 } else {
168 cCharSet = "ansi";
169 /* TODO: If the original document contained an \ansicpg value, retain it.
170 * Otherwise, M$ richedit emits a codepage number determined from the
171 * charset of the default font here. Anyway, this value is not used by
172 * the reader... */
173 nCodePage = GetACP();
174 }
175 if (nCodePage == CP_UTF8)
176 success = ME_StreamOutPrint(pStream, "{\\urtf");
177 else
178 success = ME_StreamOutPrint(pStream, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet, nCodePage);
179
180 if (!success)
181 return FALSE;
182
183 pStream->nDefaultCodePage = nCodePage;
184
185 /* FIXME: This should be a document property */
186 /* TODO: handle SFF_PLAINRTF */
187 language = GetUserDefaultLangID();
188 if (!ME_StreamOutPrint(pStream, "\\deff0\\deflang%u\\deflangfe%u", language, language))
189 return FALSE;
190
191 /* FIXME: This should be a document property */
192 pStream->nDefaultFont = 0;
193
194 return TRUE;
195 }
196
197
198 static BOOL
199 ME_StreamOutRTFFontAndColorTbl(ME_OutStream *pStream, ME_DisplayItem *pFirstRun, const ME_DisplayItem *pLastRun)
200 {
201 ME_DisplayItem *item = pFirstRun;
202 ME_FontTableItem *table = pStream->fonttbl;
203 int i;
204
205 do {
206 CHARFORMAT2W *fmt = &item->member.run.style->fmt;
207 COLORREF crColor;
208
209 if (fmt->dwMask & CFM_FACE) {
210 WCHAR *face = fmt->szFaceName;
211 BYTE bCharSet = (fmt->dwMask & CFM_CHARSET) ? fmt->bCharSet : DEFAULT_CHARSET;
212
213 for (i = 0; i < pStream->nFontTblLen; i++)
214 if (table[i].bCharSet == bCharSet
215 && (table[i].szFaceName == face || !lstrcmpW(table[i].szFaceName, face)))
216 break;
217 if (i == pStream->nFontTblLen) {
218 table[i].bCharSet = bCharSet;
219 table[i].szFaceName = face;
220 pStream->nFontTblLen++;
221 }
222 }
223
224 if (fmt->dwMask & CFM_COLOR && !(fmt->dwEffects & CFE_AUTOCOLOR)) {
225 crColor = fmt->crTextColor;
226 for (i = 1; i < pStream->nColorTblLen; i++)
227 if (pStream->colortbl[i] == crColor)
228 break;
229 if (i == pStream->nColorTblLen) {
230 pStream->colortbl[i] = crColor;
231 pStream->nColorTblLen++;
232 }
233 }
234 if (fmt->dwMask & CFM_BACKCOLOR && !(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
235 crColor = fmt->crBackColor;
236 for (i = 1; i < pStream->nColorTblLen; i++)
237 if (pStream->colortbl[i] == crColor)
238 break;
239 if (i == pStream->nColorTblLen) {
240 pStream->colortbl[i] = crColor;
241 pStream->nColorTblLen++;
242 }
243 }
244
245 if (item == pLastRun)
246 break;
247 item = ME_FindItemFwd(item, diRun);
248 } while (item);
249
250 if (!ME_StreamOutPrint(pStream, "{\\fonttbl"))
251 return FALSE;
252
253 for (i = 0; i < pStream->nFontTblLen; i++) {
254 if (table[i].bCharSet != DEFAULT_CHARSET) {
255 if (!ME_StreamOutPrint(pStream, "{\\f%u\\fcharset%u ", i, table[i].bCharSet))
256 return FALSE;
257 } else {
258 if (!ME_StreamOutPrint(pStream, "{\\f%u ", i))
259 return FALSE;
260 }
261 if (!ME_StreamOutRTFText(pStream, table[i].szFaceName, -1))
262 return FALSE;
263 if (!ME_StreamOutPrint(pStream, ";}\r\n"))
264 return FALSE;
265 }
266 if (!ME_StreamOutPrint(pStream, "}"))
267 return FALSE;
268
269 /* Output colors table if not empty */
270 if (pStream->nColorTblLen > 1) {
271 if (!ME_StreamOutPrint(pStream, "{\\colortbl;"))
272 return FALSE;
273 for (i = 1; i < pStream->nColorTblLen; i++) {
274 if (!ME_StreamOutPrint(pStream, "\\red%u\\green%u\\blue%u;",
275 pStream->colortbl[i] & 0xFF,
276 (pStream->colortbl[i] >> 8) & 0xFF,
277 (pStream->colortbl[i] >> 16) & 0xFF))
278 return FALSE;
279 }
280 if (!ME_StreamOutPrint(pStream, "}"))
281 return FALSE;
282 }
283
284 return TRUE;
285 }
286
287 static BOOL
288 ME_StreamOutRTFTableProps(ME_TextEditor *editor, ME_OutStream *pStream,
289 const ME_DisplayItem *para)
290 {
291 ME_DisplayItem *cell;
292 char props[STREAMOUT_BUFFER_SIZE] = "";
293
294 if (!ME_StreamOutPrint(pStream, "\\trowd"))
295 return FALSE;
296 if (!editor->bEmulateVersion10) { /* v4.1 */
297 assert(para->member.para.nFlags & MEPF_ROWSTART);
298 cell = para->member.para.next_para->member.para.pCell;
299 assert(cell);
300 do {
301 sprintf(props + strlen(props), "\\cellx%d", cell->member.cell.nRightBoundary);
302 cell = cell->member.cell.next_cell;
303 } while (cell->member.cell.next_cell);
304 } else { /* v1.0 - 3.0 */
305 PARAFORMAT2 *pFmt = para->member.para.pFmt;
306 int i;
307
308 assert(!(para->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND|MEPF_CELL)));
309 if (pFmt->dxOffset)
310 sprintf(props + strlen(props), "\\trgaph%d", pFmt->dxOffset);
311 if (pFmt->dxStartIndent)
312 sprintf(props + strlen(props), "\\trleft%d", pFmt->dxStartIndent);
313 for (i = 0; i < pFmt->cTabCount; i++)
314 {
315 sprintf(props + strlen(props), "\\cellx%d", pFmt->rgxTabs[i] & 0x00FFFFFF);
316 }
317 }
318 if (!ME_StreamOutPrint(pStream, props))
319 return FALSE;
320 props[0] = '\0';
321 return TRUE;
322 }
323
324 static BOOL
325 ME_StreamOutRTFParaProps(ME_TextEditor *editor, ME_OutStream *pStream,
326 const ME_DisplayItem *para)
327 {
328 PARAFORMAT2 *fmt = para->member.para.pFmt;
329 char props[STREAMOUT_BUFFER_SIZE] = "";
330 int i;
331
332 /* TODO: Don't emit anything if the last PARAFORMAT2 is inherited */
333 if (!ME_StreamOutPrint(pStream, "\\pard"))
334 return FALSE;
335
336 if (!editor->bEmulateVersion10) { /* v4.1 */
337 if (pStream->nNestingLevel > 0)
338 strcat(props, "\\intbl");
339 if (pStream->nNestingLevel > 1)
340 sprintf(props + strlen(props), "\\itap%d", pStream->nNestingLevel);
341 } else { /* v1.0 - 3.0 */
342 if (fmt->dwMask & PFM_TABLE && fmt->wEffects & PFE_TABLE)
343 strcat(props, "\\intbl");
344 }
345
346 /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
347 * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
348 * set very different from the documentation.
349 * (Tested with RichEdit 5.50.25.0601) */
350
351 if (fmt->dwMask & PFM_ALIGNMENT) {
352 switch (fmt->wAlignment) {
353 case PFA_LEFT:
354 /* Default alignment: not emitted */
355 break;
356 case PFA_RIGHT:
357 strcat(props, "\\qr");
358 break;
359 case PFA_CENTER:
360 strcat(props, "\\qc");
361 break;
362 case PFA_JUSTIFY:
363 strcat(props, "\\qj");
364 break;
365 }
366 }
367
368 if (fmt->dwMask & PFM_LINESPACING) {
369 /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
370 * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
371 switch (fmt->bLineSpacingRule) {
372 case 0: /* Single spacing */
373 strcat(props, "\\sl-240\\slmult1");
374 break;
375 case 1: /* 1.5 spacing */
376 strcat(props, "\\sl-360\\slmult1");
377 break;
378 case 2: /* Double spacing */
379 strcat(props, "\\sl-480\\slmult1");
380 break;
381 case 3:
382 sprintf(props + strlen(props), "\\sl%d\\slmult0", fmt->dyLineSpacing);
383 break;
384 case 4:
385 sprintf(props + strlen(props), "\\sl-%d\\slmult0", fmt->dyLineSpacing);
386 break;
387 case 5:
388 sprintf(props + strlen(props), "\\sl-%d\\slmult1", fmt->dyLineSpacing * 240 / 20);
389 break;
390 }
391 }
392
393 if (fmt->dwMask & PFM_DONOTHYPHEN && fmt->wEffects & PFE_DONOTHYPHEN)
394 strcat(props, "\\hyph0");
395 if (fmt->dwMask & PFM_KEEP && fmt->wEffects & PFE_KEEP)
396 strcat(props, "\\keep");
397 if (fmt->dwMask & PFM_KEEPNEXT && fmt->wEffects & PFE_KEEPNEXT)
398 strcat(props, "\\keepn");
399 if (fmt->dwMask & PFM_NOLINENUMBER && fmt->wEffects & PFE_NOLINENUMBER)
400 strcat(props, "\\noline");
401 if (fmt->dwMask & PFM_NOWIDOWCONTROL && fmt->wEffects & PFE_NOWIDOWCONTROL)
402 strcat(props, "\\nowidctlpar");
403 if (fmt->dwMask & PFM_PAGEBREAKBEFORE && fmt->wEffects & PFE_PAGEBREAKBEFORE)
404 strcat(props, "\\pagebb");
405 if (fmt->dwMask & PFM_RTLPARA && fmt->wEffects & PFE_RTLPARA)
406 strcat(props, "\\rtlpar");
407 if (fmt->dwMask & PFM_SIDEBYSIDE && fmt->wEffects & PFE_SIDEBYSIDE)
408 strcat(props, "\\sbys");
409
410 if (!(editor->bEmulateVersion10 && /* v1.0 - 3.0 */
411 fmt->dwMask & PFM_TABLE && fmt->wEffects & PFE_TABLE))
412 {
413 if (fmt->dwMask & PFM_OFFSET)
414 sprintf(props + strlen(props), "\\li%d", fmt->dxOffset);
415 if (fmt->dwMask & PFM_OFFSETINDENT || fmt->dwMask & PFM_STARTINDENT)
416 sprintf(props + strlen(props), "\\fi%d", fmt->dxStartIndent);
417 if (fmt->dwMask & PFM_RIGHTINDENT)
418 sprintf(props + strlen(props), "\\ri%d", fmt->dxRightIndent);
419 if (fmt->dwMask & PFM_TABSTOPS) {
420 static const char * const leader[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
421
422 for (i = 0; i < fmt->cTabCount; i++) {
423 switch ((fmt->rgxTabs[i] >> 24) & 0xF) {
424 case 1:
425 strcat(props, "\\tqc");
426 break;
427 case 2:
428 strcat(props, "\\tqr");
429 break;
430 case 3:
431 strcat(props, "\\tqdec");
432 break;
433 case 4:
434 /* Word bar tab (vertical bar). Handled below */
435 break;
436 }
437 if (fmt->rgxTabs[i] >> 28 <= 5)
438 strcat(props, leader[fmt->rgxTabs[i] >> 28]);
439 sprintf(props+strlen(props), "\\tx%d", fmt->rgxTabs[i]&0x00FFFFFF);
440 }
441 }
442 }
443 if (fmt->dwMask & PFM_SPACEAFTER)
444 sprintf(props + strlen(props), "\\sa%d", fmt->dySpaceAfter);
445 if (fmt->dwMask & PFM_SPACEBEFORE)
446 sprintf(props + strlen(props), "\\sb%d", fmt->dySpaceBefore);
447 if (fmt->dwMask & PFM_STYLE)
448 sprintf(props + strlen(props), "\\s%d", fmt->sStyle);
449
450 if (fmt->dwMask & PFM_SHADING) {
451 static const char * const style[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
452 "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
453 "\\bghoriz", "\\bgvert", "\\bgfdiag",
454 "\\bgbdiag", "\\bgcross", "\\bgdcross",
455 "", "", "" };
456 if (fmt->wShadingWeight)
457 sprintf(props + strlen(props), "\\shading%d", fmt->wShadingWeight);
458 if (fmt->wShadingStyle & 0xF)
459 strcat(props, style[fmt->wShadingStyle & 0xF]);
460 sprintf(props + strlen(props), "\\cfpat%d\\cbpat%d",
461 (fmt->wShadingStyle >> 4) & 0xF, (fmt->wShadingStyle >> 8) & 0xF);
462 }
463
464 if (*props && !ME_StreamOutPrint(pStream, props))
465 return FALSE;
466
467 return TRUE;
468 }
469
470
471 static BOOL
472 ME_StreamOutRTFCharProps(ME_OutStream *pStream, CHARFORMAT2W *fmt)
473 {
474 char props[STREAMOUT_BUFFER_SIZE] = "";
475 int i;
476
477 if (fmt->dwMask & CFM_ALLCAPS && fmt->dwEffects & CFE_ALLCAPS)
478 strcat(props, "\\caps");
479 if (fmt->dwMask & CFM_ANIMATION)
480 sprintf(props + strlen(props), "\\animtext%u", fmt->bAnimation);
481 if (fmt->dwMask & CFM_BACKCOLOR) {
482 if (!(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
483 for (i = 1; i < pStream->nColorTblLen; i++)
484 if (pStream->colortbl[i] == fmt->crBackColor) {
485 sprintf(props + strlen(props), "\\cb%u", i);
486 break;
487 }
488 }
489 }
490 if (fmt->dwMask & CFM_BOLD && fmt->dwEffects & CFE_BOLD)
491 strcat(props, "\\b");
492 if (fmt->dwMask & CFM_COLOR) {
493 if (!(fmt->dwEffects & CFE_AUTOCOLOR)) {
494 for (i = 1; i < pStream->nColorTblLen; i++)
495 if (pStream->colortbl[i] == fmt->crTextColor) {
496 sprintf(props + strlen(props), "\\cf%u", i);
497 break;
498 }
499 }
500 }
501 /* TODO: CFM_DISABLED */
502 if (fmt->dwMask & CFM_EMBOSS && fmt->dwEffects & CFE_EMBOSS)
503 strcat(props, "\\embo");
504 if (fmt->dwMask & CFM_HIDDEN && fmt->dwEffects & CFE_HIDDEN)
505 strcat(props, "\\v");
506 if (fmt->dwMask & CFM_IMPRINT && fmt->dwEffects & CFE_IMPRINT)
507 strcat(props, "\\impr");
508 if (fmt->dwMask & CFM_ITALIC && fmt->dwEffects & CFE_ITALIC)
509 strcat(props, "\\i");
510 if (fmt->dwMask & CFM_KERNING)
511 sprintf(props + strlen(props), "\\kerning%u", fmt->wKerning);
512 if (fmt->dwMask & CFM_LCID) {
513 /* TODO: handle SFF_PLAINRTF */
514 if (LOWORD(fmt->lcid) == 1024)
515 strcat(props, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
516 else
517 sprintf(props + strlen(props), "\\lang%u", LOWORD(fmt->lcid));
518 }
519 /* CFM_LINK is not streamed out by M$ */
520 if (fmt->dwMask & CFM_OFFSET) {
521 if (fmt->yOffset >= 0)
522 sprintf(props + strlen(props), "\\up%d", fmt->yOffset);
523 else
524 sprintf(props + strlen(props), "\\dn%d", -fmt->yOffset);
525 }
526 if (fmt->dwMask & CFM_OUTLINE && fmt->dwEffects & CFE_OUTLINE)
527 strcat(props, "\\outl");
528 if (fmt->dwMask & CFM_PROTECTED && fmt->dwEffects & CFE_PROTECTED)
529 strcat(props, "\\protect");
530 /* TODO: CFM_REVISED CFM_REVAUTHOR - probably using rsidtbl? */
531 if (fmt->dwMask & CFM_SHADOW && fmt->dwEffects & CFE_SHADOW)
532 strcat(props, "\\shad");
533 if (fmt->dwMask & CFM_SIZE)
534 sprintf(props + strlen(props), "\\fs%d", fmt->yHeight / 10);
535 if (fmt->dwMask & CFM_SMALLCAPS && fmt->dwEffects & CFE_SMALLCAPS)
536 strcat(props, "\\scaps");
537 if (fmt->dwMask & CFM_SPACING)
538 sprintf(props + strlen(props), "\\expnd%u\\expndtw%u", fmt->sSpacing / 5, fmt->sSpacing);
539 if (fmt->dwMask & CFM_STRIKEOUT && fmt->dwEffects & CFE_STRIKEOUT)
540 strcat(props, "\\strike");
541 if (fmt->dwMask & CFM_STYLE) {
542 sprintf(props + strlen(props), "\\cs%u", fmt->sStyle);
543 /* TODO: emit style contents here */
544 }
545 if (fmt->dwMask & (CFM_SUBSCRIPT | CFM_SUPERSCRIPT)) {
546 if (fmt->dwEffects & CFE_SUBSCRIPT)
547 strcat(props, "\\sub");
548 else if (fmt->dwEffects & CFE_SUPERSCRIPT)
549 strcat(props, "\\super");
550 }
551 if (fmt->dwMask & CFM_UNDERLINE || fmt->dwMask & CFM_UNDERLINETYPE) {
552 if (fmt->dwMask & CFM_UNDERLINETYPE)
553 switch (fmt->bUnderlineType) {
554 case CFU_CF1UNDERLINE:
555 case CFU_UNDERLINE:
556 strcat(props, "\\ul");
557 break;
558 case CFU_UNDERLINEDOTTED:
559 strcat(props, "\\uld");
560 break;
561 case CFU_UNDERLINEDOUBLE:
562 strcat(props, "\\uldb");
563 break;
564 case CFU_UNDERLINEWORD:
565 strcat(props, "\\ulw");
566 break;
567 case CFU_UNDERLINENONE:
568 default:
569 strcat(props, "\\ul0");
570 break;
571 }
572 else if (fmt->dwEffects & CFE_UNDERLINE)
573 strcat(props, "\\ul");
574 }
575 /* FIXME: How to emit CFM_WEIGHT? */
576
577 if (fmt->dwMask & CFM_FACE || fmt->dwMask & CFM_CHARSET) {
578 WCHAR *szFaceName;
579
580 if (fmt->dwMask & CFM_FACE)
581 szFaceName = fmt->szFaceName;
582 else
583 szFaceName = pStream->fonttbl[0].szFaceName;
584 for (i = 0; i < pStream->nFontTblLen; i++) {
585 if (szFaceName == pStream->fonttbl[i].szFaceName
586 || !lstrcmpW(szFaceName, pStream->fonttbl[i].szFaceName))
587 if (!(fmt->dwMask & CFM_CHARSET)
588 || fmt->bCharSet == pStream->fonttbl[i].bCharSet)
589 break;
590 }
591 if (i < pStream->nFontTblLen)
592 {
593 if (i != pStream->nDefaultFont)
594 sprintf(props + strlen(props), "\\f%u", i);
595
596 /* In UTF-8 mode, charsets/codepages are not used */
597 if (pStream->nDefaultCodePage != CP_UTF8)
598 {
599 if (pStream->fonttbl[i].bCharSet == DEFAULT_CHARSET)
600 pStream->nCodePage = pStream->nDefaultCodePage;
601 else
602 pStream->nCodePage = RTFCharSetToCodePage(NULL, pStream->fonttbl[i].bCharSet);
603 }
604 }
605 }
606 if (*props)
607 strcat(props, " ");
608 if (!ME_StreamOutPrint(pStream, props))
609 return FALSE;
610 return TRUE;
611 }
612
613
614 static BOOL
615 ME_StreamOutRTFText(ME_OutStream *pStream, const WCHAR *text, LONG nChars)
616 {
617 char buffer[STREAMOUT_BUFFER_SIZE];
618 int pos = 0;
619 int fit, nBytes, i;
620
621 if (nChars == -1)
622 nChars = lstrlenW(text);
623
624 while (nChars) {
625 /* In UTF-8 mode, font charsets are not used. */
626 if (pStream->nDefaultCodePage == CP_UTF8) {
627 /* 6 is the maximum character length in UTF-8 */
628 fit = min(nChars, STREAMOUT_BUFFER_SIZE / 6);
629 nBytes = WideCharToMultiByte(CP_UTF8, 0, text, fit, buffer,
630 STREAMOUT_BUFFER_SIZE, NULL, NULL);
631 nChars -= fit;
632 text += fit;
633 for (i = 0; i < nBytes; i++)
634 if (buffer[i] == '{' || buffer[i] == '}' || buffer[i] == '\\') {
635 if (!ME_StreamOutPrint(pStream, "%.*s\\", i - pos, buffer + pos))
636 return FALSE;
637 pos = i;
638 }
639 if (pos < nBytes)
640 if (!ME_StreamOutMove(pStream, buffer + pos, nBytes - pos))
641 return FALSE;
642 pos = 0;
643 } else if (*text < 128) {
644 if (*text == '{' || *text == '}' || *text == '\\')
645 buffer[pos++] = '\\';
646 buffer[pos++] = (char)(*text++);
647 nChars--;
648 } else {
649 BOOL unknown = FALSE;
650 char letter[3];
651
652 /* FIXME: In the MS docs for WideCharToMultiByte there is a big list of
653 * codepages including CP_SYMBOL for which the last parameter must be set
654 * to NULL for the function to succeed. But in Wine we need to care only
655 * about CP_SYMBOL */
656 nBytes = WideCharToMultiByte(pStream->nCodePage, 0, text, 1,
657 letter, 3, NULL,
658 (pStream->nCodePage == CP_SYMBOL) ? NULL : &unknown);
659 if (unknown)
660 pos += sprintf(buffer + pos, "\\u%d?", (short)*text);
661 else if ((BYTE)*letter < 128) {
662 if (*letter == '{' || *letter == '}' || *letter == '\\')
663 buffer[pos++] = '\\';
664 buffer[pos++] = *letter;
665 } else {
666 for (i = 0; i < nBytes; i++)
667 pos += sprintf(buffer + pos, "\\'%02x", (BYTE)letter[i]);
668 }
669 text++;
670 nChars--;
671 }
672 if (pos >= STREAMOUT_BUFFER_SIZE - 11) {
673 if (!ME_StreamOutMove(pStream, buffer, pos))
674 return FALSE;
675 pos = 0;
676 }
677 }
678 return ME_StreamOutMove(pStream, buffer, pos);
679 }
680
681
682 static BOOL
683 ME_StreamOutRTF(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int nChars, int dwFormat)
684 {
685 ME_DisplayItem *p, *pEnd, *pPara;
686 int nOffset, nEndLen;
687
688 ME_RunOfsFromCharOfs(editor, nStart, &p, &nOffset);
689 ME_RunOfsFromCharOfs(editor, nStart+nChars, &pEnd, &nEndLen);
690
691 pPara = ME_GetParagraph(p);
692
693 if (!ME_StreamOutRTFHeader(pStream, dwFormat))
694 return FALSE;
695
696 if (!ME_StreamOutRTFFontAndColorTbl(pStream, p, pEnd))
697 return FALSE;
698
699 /* TODO: stylesheet table */
700
701 /* FIXME: maybe emit something smarter for the generator? */
702 if (!ME_StreamOutPrint(pStream, "{\\*\\generator Wine Riched20 2.0.????;}"))
703 return FALSE;
704
705 /* TODO: information group */
706
707 /* TODO: document formatting properties */
708
709 /* FIXME: We have only one document section */
710
711 /* TODO: section formatting properties */
712
713 if (!ME_StreamOutRTFParaProps(editor, pStream, ME_GetParagraph(p)))
714 return FALSE;
715
716 while(1)
717 {
718 switch(p->type)
719 {
720 case diParagraph:
721 if (!editor->bEmulateVersion10) { /* v4.1 */
722 if (p->member.para.nFlags & MEPF_ROWSTART) {
723 pStream->nNestingLevel++;
724 if (pStream->nNestingLevel == 1) {
725 if (!ME_StreamOutRTFTableProps(editor, pStream, p))
726 return FALSE;
727 }
728 } else if (p->member.para.nFlags & MEPF_ROWEND) {
729 pStream->nNestingLevel--;
730 if (pStream->nNestingLevel > 1) {
731 if (!ME_StreamOutPrint(pStream, "{\\*\\nesttableprops"))
732 return FALSE;
733 if (!ME_StreamOutRTFTableProps(editor, pStream, p))
734 return FALSE;
735 if (!ME_StreamOutPrint(pStream, "\\nestrow}{\\nonesttables\\par}\r\n"))
736 return FALSE;
737 } else {
738 if (!ME_StreamOutPrint(pStream, "\\row \r\n"))
739 return FALSE;
740 }
741 } else if (!ME_StreamOutRTFParaProps(editor, pStream, p)) {
742 return FALSE;
743 }
744 } else { /* v1.0 - 3.0 */
745 if (p->member.para.pFmt->dwMask & PFM_TABLE &&
746 p->member.para.pFmt->wEffects & PFE_TABLE)
747 {
748 if (!ME_StreamOutRTFTableProps(editor, pStream, p))
749 return FALSE;
750 }
751 if (!ME_StreamOutRTFParaProps(editor, pStream, p))
752 return FALSE;
753 }
754 pPara = p;
755 break;
756 case diRun:
757 if (p == pEnd && !nEndLen)
758 break;
759 TRACE("flags %xh\n", p->member.run.nFlags);
760 /* TODO: emit embedded objects */
761 if (pPara->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
762 break;
763 if (p->member.run.nFlags & MERF_GRAPHICS) {
764 FIXME("embedded objects are not handled\n");
765 } else if (p->member.run.nFlags & MERF_TAB) {
766 if (editor->bEmulateVersion10 && /* v1.0 - 3.0 */
767 pPara->member.para.pFmt->dwMask & PFM_TABLE &&
768 pPara->member.para.pFmt->wEffects & PFE_TABLE)
769 {
770 if (!ME_StreamOutPrint(pStream, "\\cell "))
771 return FALSE;
772 } else {
773 if (!ME_StreamOutPrint(pStream, "\\tab "))
774 return FALSE;
775 }
776 } else if (p->member.run.nFlags & MERF_ENDCELL) {
777 if (pStream->nNestingLevel > 1) {
778 if (!ME_StreamOutPrint(pStream, "\\nestcell "))
779 return FALSE;
780 } else {
781 if (!ME_StreamOutPrint(pStream, "\\cell "))
782 return FALSE;
783 }
784 nChars--;
785 } else if (p->member.run.nFlags & MERF_ENDPARA) {
786 if (pPara->member.para.pFmt->dwMask & PFM_TABLE &&
787 pPara->member.para.pFmt->wEffects & PFE_TABLE &&
788 !(pPara->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND|MEPF_CELL)))
789 {
790 if (!ME_StreamOutPrint(pStream, "\\row \r\n"))
791 return FALSE;
792 } else {
793 if (!ME_StreamOutPrint(pStream, "\r\n\\par"))
794 return FALSE;
795 }
796 /* Skip as many characters as required by current line break */
797 nChars -= (p->member.run.nCR <= nChars) ? p->member.run.nCR : nChars;
798 nChars -= (p->member.run.nLF <= nChars) ? p->member.run.nLF : nChars;
799 } else if (p->member.run.nFlags & MERF_ENDROW) {
800 if (!ME_StreamOutPrint(pStream, "\\line \r\n"))
801 return FALSE;
802 nChars--;
803 } else {
804 int nEnd;
805
806 if (!ME_StreamOutPrint(pStream, "{"))
807 return FALSE;
808 TRACE("style %p\n", p->member.run.style);
809 if (!ME_StreamOutRTFCharProps(pStream, &p->member.run.style->fmt))
810 return FALSE;
811
812 nEnd = (p == pEnd) ? nEndLen : ME_StrLen(p->member.run.strText);
813 if (!ME_StreamOutRTFText(pStream, p->member.run.strText->szData + nOffset, nEnd - nOffset))
814 return FALSE;
815 nOffset = 0;
816 if (!ME_StreamOutPrint(pStream, "}"))
817 return FALSE;
818 }
819 break;
820 default: /* we missed the last item */
821 assert(0);
822 }
823 if (p == pEnd)
824 break;
825 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
826 }
827 if (!ME_StreamOutPrint(pStream, "}"))
828 return FALSE;
829 return TRUE;
830 }
831
832
833 static BOOL
834 ME_StreamOutText(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int nChars, DWORD dwFormat)
835 {
836 /* FIXME: use ME_RunOfsFromCharOfs */
837 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
838 int nLen;
839 UINT nCodePage = CP_ACP;
840 char *buffer = NULL;
841 int nBufLen = 0;
842 BOOL success = TRUE;
843
844 if (!item)
845 return FALSE;
846
847 if (dwFormat & SF_USECODEPAGE)
848 nCodePage = HIWORD(dwFormat);
849
850 /* TODO: Handle SF_TEXTIZED */
851
852 while (success && nChars && item) {
853 nLen = ME_StrLen(item->member.run.strText) - nStart;
854 if (nLen > nChars)
855 nLen = nChars;
856
857 if (item->member.run.nFlags & MERF_ENDPARA) {
858 static const WCHAR szEOL[2] = { '\r', '\n' };
859
860 if (!editor->bEmulateVersion10) {
861 /* richedit 2.0 - all line breaks are \r\n */
862 if (dwFormat & SF_UNICODE)
863 success = ME_StreamOutMove(pStream, (const char *)szEOL, sizeof(szEOL));
864 else
865 success = ME_StreamOutMove(pStream, "\r\n", 2);
866 assert(nLen == 1);
867 } else {
868 int i; int tnLen;
869
870 /* richedit 1.0 - need to honor actual \r and \n amounts */
871 nLen = item->member.run.nCR + item->member.run.nLF;
872 if (nLen > nChars)
873 nLen = nChars;
874 tnLen = nLen;
875
876 i = 0;
877 while (tnLen > 0 && i < item->member.run.nCR) {
878 if (dwFormat & SF_UNICODE)
879 success = ME_StreamOutMove(pStream, (const char *)(&szEOL[0]), sizeof(WCHAR));
880 else
881 success = ME_StreamOutMove(pStream, "\r", 1);
882 tnLen--; i++;
883 }
884 i = 0;
885 while (tnLen > 0 && i < item->member.run.nLF) {
886 if (dwFormat & SF_UNICODE)
887 success = ME_StreamOutMove(pStream, (const char *)(&szEOL[1]), sizeof(WCHAR));
888 else
889 success = ME_StreamOutMove(pStream, "\n", 1);
890 tnLen--; i++;
891 }
892 }
893 } else {
894 if (dwFormat & SF_UNICODE)
895 success = ME_StreamOutMove(pStream, (const char *)(item->member.run.strText->szData + nStart),
896 sizeof(WCHAR) * nLen);
897 else {
898 int nSize;
899
900 nSize = WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
901 nLen, NULL, 0, NULL, NULL);
902 if (nSize > nBufLen) {
903 FREE_OBJ(buffer);
904 buffer = ALLOC_N_OBJ(char, nSize);
905 nBufLen = nSize;
906 }
907 WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
908 nLen, buffer, nSize, NULL, NULL);
909 success = ME_StreamOutMove(pStream, buffer, nSize);
910 }
911 }
912
913 nChars -= nLen;
914 nStart = 0;
915 item = ME_FindItemFwd(item, diRun);
916 }
917
918 FREE_OBJ(buffer);
919 return success;
920 }
921
922
923 LRESULT
924 ME_StreamOutRange(ME_TextEditor *editor, DWORD dwFormat, int nStart, int nTo, EDITSTREAM *stream)
925 {
926 ME_OutStream *pStream = ME_StreamOutInit(editor, stream);
927
928 if (nTo == -1)
929 {
930 nTo = ME_GetTextLength(editor);
931 /* Generate an end-of-paragraph at the end of SCF_ALL RTF output */
932 if (dwFormat & SF_RTF)
933 nTo++;
934 }
935 TRACE("from %d to %d\n", nStart, nTo);
936
937 if (dwFormat & SF_RTF)
938 ME_StreamOutRTF(editor, pStream, nStart, nTo - nStart, dwFormat);
939 else if (dwFormat & SF_TEXT || dwFormat & SF_TEXTIZED)
940 ME_StreamOutText(editor, pStream, nStart, nTo - nStart, dwFormat);
941 if (!pStream->stream->dwError)
942 ME_StreamOutFlush(pStream);
943 return ME_StreamOutFree(pStream);
944 }
945
946 LRESULT
947 ME_StreamOut(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream)
948 {
949 int nStart, nTo;
950
951 if (dwFormat & SFF_SELECTION)
952 ME_GetSelection(editor, &nStart, &nTo);
953 else {
954 nStart = 0;
955 nTo = -1;
956 }
957 return ME_StreamOutRange(editor, dwFormat, nStart, nTo, stream);
958 }