6cadbd444254e7d9a76a76dfd361888a36957fc2
[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,
200 ME_DisplayItem *pLastRun)
201 {
202 ME_DisplayItem *item = pFirstRun;
203 ME_FontTableItem *table = pStream->fonttbl;
204 unsigned int i;
205 ME_DisplayItem *pLastPara = ME_GetParagraph(pLastRun);
206 ME_DisplayItem *pCell = NULL;
207
208 do {
209 CHARFORMAT2W *fmt = &item->member.run.style->fmt;
210 COLORREF crColor;
211
212 if (fmt->dwMask & CFM_FACE) {
213 WCHAR *face = fmt->szFaceName;
214 BYTE bCharSet = (fmt->dwMask & CFM_CHARSET) ? fmt->bCharSet : DEFAULT_CHARSET;
215
216 for (i = 0; i < pStream->nFontTblLen; i++)
217 if (table[i].bCharSet == bCharSet
218 && (table[i].szFaceName == face || !lstrcmpW(table[i].szFaceName, face)))
219 break;
220 if (i == pStream->nFontTblLen && i < STREAMOUT_FONTTBL_SIZE) {
221 table[i].bCharSet = bCharSet;
222 table[i].szFaceName = face;
223 pStream->nFontTblLen++;
224 }
225 }
226
227 if (fmt->dwMask & CFM_COLOR && !(fmt->dwEffects & CFE_AUTOCOLOR)) {
228 crColor = fmt->crTextColor;
229 for (i = 1; i < pStream->nColorTblLen; i++)
230 if (pStream->colortbl[i] == crColor)
231 break;
232 if (i == pStream->nColorTblLen && i < STREAMOUT_COLORTBL_SIZE) {
233 pStream->colortbl[i] = crColor;
234 pStream->nColorTblLen++;
235 }
236 }
237 if (fmt->dwMask & CFM_BACKCOLOR && !(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
238 crColor = fmt->crBackColor;
239 for (i = 1; i < pStream->nColorTblLen; i++)
240 if (pStream->colortbl[i] == crColor)
241 break;
242 if (i == pStream->nColorTblLen && i < STREAMOUT_COLORTBL_SIZE) {
243 pStream->colortbl[i] = crColor;
244 pStream->nColorTblLen++;
245 }
246 }
247
248 if (item == pLastRun)
249 break;
250 item = ME_FindItemFwd(item, diRun);
251 } while (item);
252 item = ME_GetParagraph(pFirstRun);
253 do {
254 if (item->member.para.pCell && item->member.para.pCell)
255 {
256 pCell = item->member.para.pCell;
257 if (pCell)
258 {
259 ME_Border* borders[4] = { &pCell->member.cell.border.top,
260 &pCell->member.cell.border.left,
261 &pCell->member.cell.border.bottom,
262 &pCell->member.cell.border.right };
263 for (i = 0; i < 4; i++)
264 {
265 if (borders[i]->width > 0)
266 {
267 unsigned int j;
268 COLORREF crColor = borders[i]->colorRef;
269 for (j = 1; j < pStream->nColorTblLen; j++)
270 if (pStream->colortbl[j] == crColor)
271 break;
272 if (j == pStream->nColorTblLen && j < STREAMOUT_COLORTBL_SIZE) {
273 pStream->colortbl[j] = crColor;
274 pStream->nColorTblLen++;
275 }
276 }
277 }
278 }
279 }
280 if (item == pLastPara)
281 break;
282 item = item->member.para.next_para;
283 } while (item);
284
285 if (!ME_StreamOutPrint(pStream, "{\\fonttbl"))
286 return FALSE;
287
288 for (i = 0; i < pStream->nFontTblLen; i++) {
289 if (table[i].bCharSet != DEFAULT_CHARSET) {
290 if (!ME_StreamOutPrint(pStream, "{\\f%u\\fcharset%u ", i, table[i].bCharSet))
291 return FALSE;
292 } else {
293 if (!ME_StreamOutPrint(pStream, "{\\f%u ", i))
294 return FALSE;
295 }
296 if (!ME_StreamOutRTFText(pStream, table[i].szFaceName, -1))
297 return FALSE;
298 if (!ME_StreamOutPrint(pStream, ";}"))
299 return FALSE;
300 }
301 if (!ME_StreamOutPrint(pStream, "}\r\n"))
302 return FALSE;
303
304 /* Output colors table if not empty */
305 if (pStream->nColorTblLen > 1) {
306 if (!ME_StreamOutPrint(pStream, "{\\colortbl;"))
307 return FALSE;
308 for (i = 1; i < pStream->nColorTblLen; i++) {
309 if (!ME_StreamOutPrint(pStream, "\\red%u\\green%u\\blue%u;",
310 pStream->colortbl[i] & 0xFF,
311 (pStream->colortbl[i] >> 8) & 0xFF,
312 (pStream->colortbl[i] >> 16) & 0xFF))
313 return FALSE;
314 }
315 if (!ME_StreamOutPrint(pStream, "}"))
316 return FALSE;
317 }
318
319 return TRUE;
320 }
321
322 static BOOL
323 ME_StreamOutRTFTableProps(ME_TextEditor *editor, ME_OutStream *pStream,
324 ME_DisplayItem *para)
325 {
326 ME_DisplayItem *cell;
327 char props[STREAMOUT_BUFFER_SIZE] = "";
328 int i;
329 const char sideChar[4] = {'t','l','b','r'};
330
331 if (!ME_StreamOutPrint(pStream, "\\trowd"))
332 return FALSE;
333 if (!editor->bEmulateVersion10) { /* v4.1 */
334 PARAFORMAT2 *pFmt = ME_GetTableRowEnd(para)->member.para.pFmt;
335 para = ME_GetTableRowStart(para);
336 cell = para->member.para.next_para->member.para.pCell;
337 assert(cell);
338 if (pFmt->dxOffset)
339 sprintf(props + strlen(props), "\\trgaph%d", pFmt->dxOffset);
340 if (pFmt->dxStartIndent)
341 sprintf(props + strlen(props), "\\trleft%d", pFmt->dxStartIndent);
342 do {
343 ME_Border* borders[4] = { &cell->member.cell.border.top,
344 &cell->member.cell.border.left,
345 &cell->member.cell.border.bottom,
346 &cell->member.cell.border.right };
347 for (i = 0; i < 4; i++)
348 {
349 if (borders[i]->width)
350 {
351 unsigned int j;
352 COLORREF crColor = borders[i]->colorRef;
353 sprintf(props + strlen(props), "\\clbrdr%c", sideChar[i]);
354 sprintf(props + strlen(props), "\\brdrs");
355 sprintf(props + strlen(props), "\\brdrw%d", borders[i]->width);
356 for (j = 1; j < pStream->nColorTblLen; j++) {
357 if (pStream->colortbl[j] == crColor) {
358 sprintf(props + strlen(props), "\\brdrcf%u", j);
359 break;
360 }
361 }
362 }
363 }
364 sprintf(props + strlen(props), "\\cellx%d", cell->member.cell.nRightBoundary);
365 cell = cell->member.cell.next_cell;
366 } while (cell->member.cell.next_cell);
367 } else { /* v1.0 - 3.0 */
368 const ME_Border* borders[4] = { &para->member.para.border.top,
369 &para->member.para.border.left,
370 &para->member.para.border.bottom,
371 &para->member.para.border.right };
372 PARAFORMAT2 *pFmt = para->member.para.pFmt;
373
374 assert(!(para->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND|MEPF_CELL)));
375 if (pFmt->dxOffset)
376 sprintf(props + strlen(props), "\\trgaph%d", pFmt->dxOffset);
377 if (pFmt->dxStartIndent)
378 sprintf(props + strlen(props), "\\trleft%d", pFmt->dxStartIndent);
379 for (i = 0; i < 4; i++)
380 {
381 if (borders[i]->width)
382 {
383 unsigned int j;
384 COLORREF crColor = borders[i]->colorRef;
385 sprintf(props + strlen(props), "\\trbrdr%c", sideChar[i]);
386 sprintf(props + strlen(props), "\\brdrs");
387 sprintf(props + strlen(props), "\\brdrw%d", borders[i]->width);
388 for (j = 1; j < pStream->nColorTblLen; j++) {
389 if (pStream->colortbl[j] == crColor) {
390 sprintf(props + strlen(props), "\\brdrcf%u", j);
391 break;
392 }
393 }
394 }
395 }
396 for (i = 0; i < pFmt->cTabCount; i++)
397 {
398 sprintf(props + strlen(props), "\\cellx%d", pFmt->rgxTabs[i] & 0x00FFFFFF);
399 }
400 }
401 if (!ME_StreamOutPrint(pStream, props))
402 return FALSE;
403 props[0] = '\0';
404 return TRUE;
405 }
406
407 static BOOL
408 ME_StreamOutRTFParaProps(ME_TextEditor *editor, ME_OutStream *pStream,
409 const ME_DisplayItem *para)
410 {
411 PARAFORMAT2 *fmt = para->member.para.pFmt;
412 char props[STREAMOUT_BUFFER_SIZE] = "";
413 int i;
414
415 /* TODO: Don't emit anything if the last PARAFORMAT2 is inherited */
416 if (!ME_StreamOutPrint(pStream, "\\pard"))
417 return FALSE;
418
419 if (!editor->bEmulateVersion10) { /* v4.1 */
420 if (pStream->nNestingLevel > 0)
421 strcat(props, "\\intbl");
422 if (pStream->nNestingLevel > 1)
423 sprintf(props + strlen(props), "\\itap%d", pStream->nNestingLevel);
424 } else { /* v1.0 - 3.0 */
425 if (fmt->dwMask & PFM_TABLE && fmt->wEffects & PFE_TABLE)
426 strcat(props, "\\intbl");
427 }
428
429 /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
430 * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
431 * set very different from the documentation.
432 * (Tested with RichEdit 5.50.25.0601) */
433
434 if (fmt->dwMask & PFM_ALIGNMENT) {
435 switch (fmt->wAlignment) {
436 case PFA_LEFT:
437 /* Default alignment: not emitted */
438 break;
439 case PFA_RIGHT:
440 strcat(props, "\\qr");
441 break;
442 case PFA_CENTER:
443 strcat(props, "\\qc");
444 break;
445 case PFA_JUSTIFY:
446 strcat(props, "\\qj");
447 break;
448 }
449 }
450
451 if (fmt->dwMask & PFM_LINESPACING) {
452 /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
453 * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
454 switch (fmt->bLineSpacingRule) {
455 case 0: /* Single spacing */
456 strcat(props, "\\sl-240\\slmult1");
457 break;
458 case 1: /* 1.5 spacing */
459 strcat(props, "\\sl-360\\slmult1");
460 break;
461 case 2: /* Double spacing */
462 strcat(props, "\\sl-480\\slmult1");
463 break;
464 case 3:
465 sprintf(props + strlen(props), "\\sl%d\\slmult0", fmt->dyLineSpacing);
466 break;
467 case 4:
468 sprintf(props + strlen(props), "\\sl-%d\\slmult0", fmt->dyLineSpacing);
469 break;
470 case 5:
471 sprintf(props + strlen(props), "\\sl-%d\\slmult1", fmt->dyLineSpacing * 240 / 20);
472 break;
473 }
474 }
475
476 if (fmt->dwMask & PFM_DONOTHYPHEN && fmt->wEffects & PFE_DONOTHYPHEN)
477 strcat(props, "\\hyph0");
478 if (fmt->dwMask & PFM_KEEP && fmt->wEffects & PFE_KEEP)
479 strcat(props, "\\keep");
480 if (fmt->dwMask & PFM_KEEPNEXT && fmt->wEffects & PFE_KEEPNEXT)
481 strcat(props, "\\keepn");
482 if (fmt->dwMask & PFM_NOLINENUMBER && fmt->wEffects & PFE_NOLINENUMBER)
483 strcat(props, "\\noline");
484 if (fmt->dwMask & PFM_NOWIDOWCONTROL && fmt->wEffects & PFE_NOWIDOWCONTROL)
485 strcat(props, "\\nowidctlpar");
486 if (fmt->dwMask & PFM_PAGEBREAKBEFORE && fmt->wEffects & PFE_PAGEBREAKBEFORE)
487 strcat(props, "\\pagebb");
488 if (fmt->dwMask & PFM_RTLPARA && fmt->wEffects & PFE_RTLPARA)
489 strcat(props, "\\rtlpar");
490 if (fmt->dwMask & PFM_SIDEBYSIDE && fmt->wEffects & PFE_SIDEBYSIDE)
491 strcat(props, "\\sbys");
492
493 if (!(editor->bEmulateVersion10 && /* v1.0 - 3.0 */
494 fmt->dwMask & PFM_TABLE && fmt->wEffects & PFE_TABLE))
495 {
496 if (fmt->dwMask & PFM_OFFSET)
497 sprintf(props + strlen(props), "\\li%d", fmt->dxOffset);
498 if (fmt->dwMask & PFM_OFFSETINDENT || fmt->dwMask & PFM_STARTINDENT)
499 sprintf(props + strlen(props), "\\fi%d", fmt->dxStartIndent);
500 if (fmt->dwMask & PFM_RIGHTINDENT)
501 sprintf(props + strlen(props), "\\ri%d", fmt->dxRightIndent);
502 if (fmt->dwMask & PFM_TABSTOPS) {
503 static const char * const leader[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
504
505 for (i = 0; i < fmt->cTabCount; i++) {
506 switch ((fmt->rgxTabs[i] >> 24) & 0xF) {
507 case 1:
508 strcat(props, "\\tqc");
509 break;
510 case 2:
511 strcat(props, "\\tqr");
512 break;
513 case 3:
514 strcat(props, "\\tqdec");
515 break;
516 case 4:
517 /* Word bar tab (vertical bar). Handled below */
518 break;
519 }
520 if (fmt->rgxTabs[i] >> 28 <= 5)
521 strcat(props, leader[fmt->rgxTabs[i] >> 28]);
522 sprintf(props+strlen(props), "\\tx%d", fmt->rgxTabs[i]&0x00FFFFFF);
523 }
524 }
525 }
526 if (fmt->dwMask & PFM_SPACEAFTER)
527 sprintf(props + strlen(props), "\\sa%d", fmt->dySpaceAfter);
528 if (fmt->dwMask & PFM_SPACEBEFORE)
529 sprintf(props + strlen(props), "\\sb%d", fmt->dySpaceBefore);
530 if (fmt->dwMask & PFM_STYLE)
531 sprintf(props + strlen(props), "\\s%d", fmt->sStyle);
532
533 if (fmt->dwMask & PFM_SHADING) {
534 static const char * const style[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
535 "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
536 "\\bghoriz", "\\bgvert", "\\bgfdiag",
537 "\\bgbdiag", "\\bgcross", "\\bgdcross",
538 "", "", "" };
539 if (fmt->wShadingWeight)
540 sprintf(props + strlen(props), "\\shading%d", fmt->wShadingWeight);
541 if (fmt->wShadingStyle & 0xF)
542 strcat(props, style[fmt->wShadingStyle & 0xF]);
543 sprintf(props + strlen(props), "\\cfpat%d\\cbpat%d",
544 (fmt->wShadingStyle >> 4) & 0xF, (fmt->wShadingStyle >> 8) & 0xF);
545 }
546
547 if (*props && !ME_StreamOutPrint(pStream, props))
548 return FALSE;
549
550 return TRUE;
551 }
552
553
554 static BOOL
555 ME_StreamOutRTFCharProps(ME_OutStream *pStream, CHARFORMAT2W *fmt)
556 {
557 char props[STREAMOUT_BUFFER_SIZE] = "";
558 unsigned int i;
559
560 if (fmt->dwMask & CFM_ALLCAPS && fmt->dwEffects & CFE_ALLCAPS)
561 strcat(props, "\\caps");
562 if (fmt->dwMask & CFM_ANIMATION)
563 sprintf(props + strlen(props), "\\animtext%u", fmt->bAnimation);
564 if (fmt->dwMask & CFM_BACKCOLOR) {
565 if (!(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
566 for (i = 1; i < pStream->nColorTblLen; i++)
567 if (pStream->colortbl[i] == fmt->crBackColor) {
568 sprintf(props + strlen(props), "\\cb%u", i);
569 break;
570 }
571 }
572 }
573 if (fmt->dwMask & CFM_BOLD && fmt->dwEffects & CFE_BOLD)
574 strcat(props, "\\b");
575 if (fmt->dwMask & CFM_COLOR) {
576 if (!(fmt->dwEffects & CFE_AUTOCOLOR)) {
577 for (i = 1; i < pStream->nColorTblLen; i++)
578 if (pStream->colortbl[i] == fmt->crTextColor) {
579 sprintf(props + strlen(props), "\\cf%u", i);
580 break;
581 }
582 }
583 }
584 /* TODO: CFM_DISABLED */
585 if (fmt->dwMask & CFM_EMBOSS && fmt->dwEffects & CFE_EMBOSS)
586 strcat(props, "\\embo");
587 if (fmt->dwMask & CFM_HIDDEN && fmt->dwEffects & CFE_HIDDEN)
588 strcat(props, "\\v");
589 if (fmt->dwMask & CFM_IMPRINT && fmt->dwEffects & CFE_IMPRINT)
590 strcat(props, "\\impr");
591 if (fmt->dwMask & CFM_ITALIC && fmt->dwEffects & CFE_ITALIC)
592 strcat(props, "\\i");
593 if (fmt->dwMask & CFM_KERNING)
594 sprintf(props + strlen(props), "\\kerning%u", fmt->wKerning);
595 if (fmt->dwMask & CFM_LCID) {
596 /* TODO: handle SFF_PLAINRTF */
597 if (LOWORD(fmt->lcid) == 1024)
598 strcat(props, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
599 else
600 sprintf(props + strlen(props), "\\lang%u", LOWORD(fmt->lcid));
601 }
602 /* CFM_LINK is not streamed out by M$ */
603 if (fmt->dwMask & CFM_OFFSET) {
604 if (fmt->yOffset >= 0)
605 sprintf(props + strlen(props), "\\up%d", fmt->yOffset);
606 else
607 sprintf(props + strlen(props), "\\dn%d", -fmt->yOffset);
608 }
609 if (fmt->dwMask & CFM_OUTLINE && fmt->dwEffects & CFE_OUTLINE)
610 strcat(props, "\\outl");
611 if (fmt->dwMask & CFM_PROTECTED && fmt->dwEffects & CFE_PROTECTED)
612 strcat(props, "\\protect");
613 /* TODO: CFM_REVISED CFM_REVAUTHOR - probably using rsidtbl? */
614 if (fmt->dwMask & CFM_SHADOW && fmt->dwEffects & CFE_SHADOW)
615 strcat(props, "\\shad");
616 if (fmt->dwMask & CFM_SIZE)
617 sprintf(props + strlen(props), "\\fs%d", fmt->yHeight / 10);
618 if (fmt->dwMask & CFM_SMALLCAPS && fmt->dwEffects & CFE_SMALLCAPS)
619 strcat(props, "\\scaps");
620 if (fmt->dwMask & CFM_SPACING)
621 sprintf(props + strlen(props), "\\expnd%u\\expndtw%u", fmt->sSpacing / 5, fmt->sSpacing);
622 if (fmt->dwMask & CFM_STRIKEOUT && fmt->dwEffects & CFE_STRIKEOUT)
623 strcat(props, "\\strike");
624 if (fmt->dwMask & CFM_STYLE) {
625 sprintf(props + strlen(props), "\\cs%u", fmt->sStyle);
626 /* TODO: emit style contents here */
627 }
628 if (fmt->dwMask & (CFM_SUBSCRIPT | CFM_SUPERSCRIPT)) {
629 if (fmt->dwEffects & CFE_SUBSCRIPT)
630 strcat(props, "\\sub");
631 else if (fmt->dwEffects & CFE_SUPERSCRIPT)
632 strcat(props, "\\super");
633 }
634 if (fmt->dwMask & CFM_UNDERLINE || fmt->dwMask & CFM_UNDERLINETYPE) {
635 if (fmt->dwMask & CFM_UNDERLINETYPE)
636 switch (fmt->bUnderlineType) {
637 case CFU_CF1UNDERLINE:
638 case CFU_UNDERLINE:
639 strcat(props, "\\ul");
640 break;
641 case CFU_UNDERLINEDOTTED:
642 strcat(props, "\\uld");
643 break;
644 case CFU_UNDERLINEDOUBLE:
645 strcat(props, "\\uldb");
646 break;
647 case CFU_UNDERLINEWORD:
648 strcat(props, "\\ulw");
649 break;
650 case CFU_UNDERLINENONE:
651 default:
652 strcat(props, "\\ul0");
653 break;
654 }
655 else if (fmt->dwEffects & CFE_UNDERLINE)
656 strcat(props, "\\ul");
657 }
658 /* FIXME: How to emit CFM_WEIGHT? */
659
660 if (fmt->dwMask & CFM_FACE || fmt->dwMask & CFM_CHARSET) {
661 WCHAR *szFaceName;
662
663 if (fmt->dwMask & CFM_FACE)
664 szFaceName = fmt->szFaceName;
665 else
666 szFaceName = pStream->fonttbl[0].szFaceName;
667 for (i = 0; i < pStream->nFontTblLen; i++) {
668 if (szFaceName == pStream->fonttbl[i].szFaceName
669 || !lstrcmpW(szFaceName, pStream->fonttbl[i].szFaceName))
670 if (!(fmt->dwMask & CFM_CHARSET)
671 || fmt->bCharSet == pStream->fonttbl[i].bCharSet)
672 break;
673 }
674 if (i < pStream->nFontTblLen)
675 {
676 if (i != pStream->nDefaultFont)
677 sprintf(props + strlen(props), "\\f%u", i);
678
679 /* In UTF-8 mode, charsets/codepages are not used */
680 if (pStream->nDefaultCodePage != CP_UTF8)
681 {
682 if (pStream->fonttbl[i].bCharSet == DEFAULT_CHARSET)
683 pStream->nCodePage = pStream->nDefaultCodePage;
684 else
685 pStream->nCodePage = RTFCharSetToCodePage(NULL, pStream->fonttbl[i].bCharSet);
686 }
687 }
688 }
689 if (*props)
690 strcat(props, " ");
691 if (!ME_StreamOutPrint(pStream, props))
692 return FALSE;
693 return TRUE;
694 }
695
696
697 static BOOL
698 ME_StreamOutRTFText(ME_OutStream *pStream, const WCHAR *text, LONG nChars)
699 {
700 char buffer[STREAMOUT_BUFFER_SIZE];
701 int pos = 0;
702 int fit, nBytes, i;
703
704 if (nChars == -1)
705 nChars = lstrlenW(text);
706
707 while (nChars) {
708 /* In UTF-8 mode, font charsets are not used. */
709 if (pStream->nDefaultCodePage == CP_UTF8) {
710 /* 6 is the maximum character length in UTF-8 */
711 fit = min(nChars, STREAMOUT_BUFFER_SIZE / 6);
712 nBytes = WideCharToMultiByte(CP_UTF8, 0, text, fit, buffer,
713 STREAMOUT_BUFFER_SIZE, NULL, NULL);
714 nChars -= fit;
715 text += fit;
716 for (i = 0; i < nBytes; i++)
717 if (buffer[i] == '{' || buffer[i] == '}' || buffer[i] == '\\') {
718 if (!ME_StreamOutPrint(pStream, "%.*s\\", i - pos, buffer + pos))
719 return FALSE;
720 pos = i;
721 }
722 if (pos < nBytes)
723 if (!ME_StreamOutMove(pStream, buffer + pos, nBytes - pos))
724 return FALSE;
725 pos = 0;
726 } else if (*text < 128) {
727 if (*text == '{' || *text == '}' || *text == '\\')
728 buffer[pos++] = '\\';
729 buffer[pos++] = (char)(*text++);
730 nChars--;
731 } else {
732 BOOL unknown = FALSE;
733 char letter[3];
734
735 /* FIXME: In the MS docs for WideCharToMultiByte there is a big list of
736 * codepages including CP_SYMBOL for which the last parameter must be set
737 * to NULL for the function to succeed. But in Wine we need to care only
738 * about CP_SYMBOL */
739 nBytes = WideCharToMultiByte(pStream->nCodePage, 0, text, 1,
740 letter, 3, NULL,
741 (pStream->nCodePage == CP_SYMBOL) ? NULL : &unknown);
742 if (unknown)
743 pos += sprintf(buffer + pos, "\\u%d?", (short)*text);
744 else if ((BYTE)*letter < 128) {
745 if (*letter == '{' || *letter == '}' || *letter == '\\')
746 buffer[pos++] = '\\';
747 buffer[pos++] = *letter;
748 } else {
749 for (i = 0; i < nBytes; i++)
750 pos += sprintf(buffer + pos, "\\'%02x", (BYTE)letter[i]);
751 }
752 text++;
753 nChars--;
754 }
755 if (pos >= STREAMOUT_BUFFER_SIZE - 11) {
756 if (!ME_StreamOutMove(pStream, buffer, pos))
757 return FALSE;
758 pos = 0;
759 }
760 }
761 return ME_StreamOutMove(pStream, buffer, pos);
762 }
763
764
765 static BOOL
766 ME_StreamOutRTF(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int nChars, int dwFormat)
767 {
768 ME_DisplayItem *p, *pEnd, *pPara;
769 int nOffset, nEndLen;
770
771 ME_RunOfsFromCharOfs(editor, nStart, &pPara, &p, &nOffset);
772 ME_RunOfsFromCharOfs(editor, nStart+nChars, NULL, &pEnd, &nEndLen);
773
774 if (!ME_StreamOutRTFHeader(pStream, dwFormat))
775 return FALSE;
776
777 if (!ME_StreamOutRTFFontAndColorTbl(pStream, p, pEnd))
778 return FALSE;
779
780 /* TODO: stylesheet table */
781
782 /* FIXME: maybe emit something smarter for the generator? */
783 if (!ME_StreamOutPrint(pStream, "{\\*\\generator Wine Riched20 2.0.????;}"))
784 return FALSE;
785
786 /* TODO: information group */
787
788 /* TODO: document formatting properties */
789
790 /* FIXME: We have only one document section */
791
792 /* TODO: section formatting properties */
793
794 if (!ME_StreamOutRTFParaProps(editor, pStream, ME_GetParagraph(p)))
795 return FALSE;
796
797 while(1)
798 {
799 switch(p->type)
800 {
801 case diParagraph:
802 if (!editor->bEmulateVersion10) { /* v4.1 */
803 if (p->member.para.nFlags & MEPF_ROWSTART) {
804 pStream->nNestingLevel++;
805 if (pStream->nNestingLevel == 1) {
806 if (!ME_StreamOutRTFTableProps(editor, pStream, p))
807 return FALSE;
808 }
809 } else if (p->member.para.nFlags & MEPF_ROWEND) {
810 pStream->nNestingLevel--;
811 if (pStream->nNestingLevel >= 1) {
812 if (!ME_StreamOutPrint(pStream, "{\\*\\nesttableprops"))
813 return FALSE;
814 if (!ME_StreamOutRTFTableProps(editor, pStream, p))
815 return FALSE;
816 if (!ME_StreamOutPrint(pStream, "\\nestrow}{\\nonesttables\\par}\r\n"))
817 return FALSE;
818 } else {
819 if (!ME_StreamOutPrint(pStream, "\\row \r\n"))
820 return FALSE;
821 }
822 } else if (!ME_StreamOutRTFParaProps(editor, pStream, p)) {
823 return FALSE;
824 }
825 } else { /* v1.0 - 3.0 */
826 if (p->member.para.pFmt->dwMask & PFM_TABLE &&
827 p->member.para.pFmt->wEffects & PFE_TABLE)
828 {
829 if (!ME_StreamOutRTFTableProps(editor, pStream, p))
830 return FALSE;
831 }
832 if (!ME_StreamOutRTFParaProps(editor, pStream, p))
833 return FALSE;
834 }
835 pPara = p;
836 break;
837 case diRun:
838 if (p == pEnd && !nEndLen)
839 break;
840 TRACE("flags %xh\n", p->member.run.nFlags);
841 /* TODO: emit embedded objects */
842 if (pPara->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
843 break;
844 if (p->member.run.nFlags & MERF_GRAPHICS) {
845 FIXME("embedded objects are not handled\n");
846 } else if (p->member.run.nFlags & MERF_TAB) {
847 if (editor->bEmulateVersion10 && /* v1.0 - 3.0 */
848 pPara->member.para.pFmt->dwMask & PFM_TABLE &&
849 pPara->member.para.pFmt->wEffects & PFE_TABLE)
850 {
851 if (!ME_StreamOutPrint(pStream, "\\cell "))
852 return FALSE;
853 } else {
854 if (!ME_StreamOutPrint(pStream, "\\tab "))
855 return FALSE;
856 }
857 } else if (p->member.run.nFlags & MERF_ENDCELL) {
858 if (pStream->nNestingLevel > 1) {
859 if (!ME_StreamOutPrint(pStream, "\\nestcell "))
860 return FALSE;
861 } else {
862 if (!ME_StreamOutPrint(pStream, "\\cell "))
863 return FALSE;
864 }
865 nChars--;
866 } else if (p->member.run.nFlags & MERF_ENDPARA) {
867 if (pPara->member.para.pFmt->dwMask & PFM_TABLE &&
868 pPara->member.para.pFmt->wEffects & PFE_TABLE &&
869 !(pPara->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND|MEPF_CELL)))
870 {
871 if (!ME_StreamOutPrint(pStream, "\\row \r\n"))
872 return FALSE;
873 } else {
874 if (!ME_StreamOutPrint(pStream, "\r\n\\par"))
875 return FALSE;
876 }
877 /* Skip as many characters as required by current line break */
878 nChars = max(0, nChars - p->member.run.strText->nLen);
879 } else if (p->member.run.nFlags & MERF_ENDROW) {
880 if (!ME_StreamOutPrint(pStream, "\\line \r\n"))
881 return FALSE;
882 nChars--;
883 } else {
884 int nEnd;
885
886 if (!ME_StreamOutPrint(pStream, "{"))
887 return FALSE;
888 TRACE("style %p\n", p->member.run.style);
889 if (!ME_StreamOutRTFCharProps(pStream, &p->member.run.style->fmt))
890 return FALSE;
891
892 nEnd = (p == pEnd) ? nEndLen : p->member.run.strText->nLen;
893 if (!ME_StreamOutRTFText(pStream, p->member.run.strText->szData + nOffset, nEnd - nOffset))
894 return FALSE;
895 nOffset = 0;
896 if (!ME_StreamOutPrint(pStream, "}"))
897 return FALSE;
898 }
899 break;
900 default: /* we missed the last item */
901 assert(0);
902 }
903 if (p == pEnd)
904 break;
905 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
906 }
907 if (!ME_StreamOutMove(pStream, "}\0", 2))
908 return FALSE;
909 return TRUE;
910 }
911
912
913 static BOOL
914 ME_StreamOutText(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int nChars, DWORD dwFormat)
915 {
916 ME_DisplayItem *item;
917 int nLen;
918 UINT nCodePage = CP_ACP;
919 char *buffer = NULL;
920 int nBufLen = 0;
921 BOOL success = TRUE;
922
923 ME_RunOfsFromCharOfs(editor, nStart, NULL, &item, &nStart);
924
925 if (!item)
926 return FALSE;
927
928 if (dwFormat & SF_USECODEPAGE)
929 nCodePage = HIWORD(dwFormat);
930
931 /* TODO: Handle SF_TEXTIZED */
932
933 while (success && nChars && item) {
934 nLen = min(nChars, item->member.run.strText->nLen - nStart);
935
936 if (!editor->bEmulateVersion10 && item->member.run.nFlags & MERF_ENDPARA)
937 {
938 static const WCHAR szEOL[2] = { '\r', '\n' };
939
940 /* richedit 2.0 - all line breaks are \r\n */
941 if (dwFormat & SF_UNICODE)
942 success = ME_StreamOutMove(pStream, (const char *)szEOL, sizeof(szEOL));
943 else
944 success = ME_StreamOutMove(pStream, "\r\n", 2);
945 } else {
946 if (dwFormat & SF_UNICODE)
947 success = ME_StreamOutMove(pStream, (const char *)(item->member.run.strText->szData + nStart),
948 sizeof(WCHAR) * nLen);
949 else {
950 int nSize;
951
952 nSize = WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
953 nLen, NULL, 0, NULL, NULL);
954 if (nSize > nBufLen) {
955 FREE_OBJ(buffer);
956 buffer = ALLOC_N_OBJ(char, nSize);
957 nBufLen = nSize;
958 }
959 WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
960 nLen, buffer, nSize, NULL, NULL);
961 success = ME_StreamOutMove(pStream, buffer, nSize);
962 }
963 }
964
965 nChars -= nLen;
966 nStart = 0;
967 item = ME_FindItemFwd(item, diRun);
968 }
969
970 FREE_OBJ(buffer);
971 return success;
972 }
973
974
975 LRESULT
976 ME_StreamOutRange(ME_TextEditor *editor, DWORD dwFormat, int nStart, int nTo, EDITSTREAM *stream)
977 {
978 ME_OutStream *pStream = ME_StreamOutInit(editor, stream);
979
980 if (nTo == -1)
981 {
982 nTo = ME_GetTextLength(editor);
983 /* Generate an end-of-paragraph at the end of SCF_ALL RTF output */
984 if (dwFormat & SF_RTF)
985 nTo++;
986 }
987 TRACE("from %d to %d\n", nStart, nTo);
988
989 if (dwFormat & SF_RTF)
990 ME_StreamOutRTF(editor, pStream, nStart, nTo - nStart, dwFormat);
991 else if (dwFormat & SF_TEXT || dwFormat & SF_TEXTIZED)
992 ME_StreamOutText(editor, pStream, nStart, nTo - nStart, dwFormat);
993 if (!pStream->stream->dwError)
994 ME_StreamOutFlush(pStream);
995 return ME_StreamOutFree(pStream);
996 }
997
998 LRESULT
999 ME_StreamOut(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream)
1000 {
1001 int nStart, nTo;
1002
1003 if (dwFormat & SFF_SELECTION)
1004 ME_GetSelection(editor, &nStart, &nTo);
1005 else {
1006 nStart = 0;
1007 nTo = -1;
1008 }
1009 return ME_StreamOutRange(editor, dwFormat, nStart, nTo, stream);
1010 }