d3drm: Improve IDirect3DRMViewportX_Render stub.
[wine/multimedia.git] / dlls / riched20 / writer.c
bloba5e5d509cb3cc12914b234d59116d6065418ac15
1 /*
2 * RichEdit - RTF writer module
4 * Copyright 2005 by Phil Krylov
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include "editor.h"
25 #include "rtf.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
30 static BOOL
31 ME_StreamOutRTFText(ME_OutStream *pStream, const WCHAR *text, LONG nChars);
34 static ME_OutStream*
35 ME_StreamOutInit(ME_TextEditor *editor, EDITSTREAM *stream)
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;
49 static BOOL
50 ME_StreamOutFlush(ME_OutStream *pStream)
52 LONG nStart = 0;
53 LONG nWritten = 0;
54 LONG nRemaining = 0;
55 EDITSTREAM *stream = pStream->stream;
57 while (nStart < pStream->pos) {
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;
71 if (nWritten == 0 || stream->dwError)
72 return FALSE;
73 pStream->written += nWritten;
74 nStart += nWritten;
76 pStream->pos = 0;
77 return TRUE;
81 static LONG
82 ME_StreamOutFree(ME_OutStream *pStream)
84 LONG written = pStream->written;
85 TRACE("total length = %u\n", written);
87 FREE_OBJ(pStream);
88 return written;
92 static BOOL
93 ME_StreamOutMove(ME_OutStream *pStream, const char *buffer, int len)
95 while (len) {
96 int space = STREAMOUT_BUFFER_SIZE - pStream->pos;
97 int fit = min(space, len);
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;
109 return TRUE;
113 static BOOL
114 ME_StreamOutPrint(ME_OutStream *pStream, const char *format, ...)
116 char string[STREAMOUT_BUFFER_SIZE]; /* This is going to be enough */
117 int len;
118 va_list valist;
120 va_start(valist, format);
121 len = vsnprintf(string, sizeof(string), format, valist);
122 va_end(valist);
124 return ME_StreamOutMove(pStream, string, len);
128 static BOOL
129 ME_StreamOutRTFHeader(ME_OutStream *pStream, int dwFormat)
131 const char *cCharSet = NULL;
132 UINT nCodePage;
133 LANGID language;
134 BOOL success;
136 if (dwFormat & SF_USECODEPAGE) {
137 CPINFOEXW info;
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 */
164 if (GetCPInfoExW(HIWORD(dwFormat), 0, &info))
165 nCodePage = info.CodePage;
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();
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);
180 if (!success)
181 return FALSE;
183 pStream->nDefaultCodePage = nCodePage;
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;
191 /* FIXME: This should be a document property */
192 pStream->nDefaultFont = 0;
194 return TRUE;
198 static BOOL
199 ME_StreamOutRTFFontAndColorTbl(ME_OutStream *pStream, ME_DisplayItem *pFirstRun,
200 ME_DisplayItem *pLastRun)
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;
208 do {
209 CHARFORMAT2W *fmt = &item->member.run.style->fmt;
210 COLORREF crColor;
212 if (fmt->dwMask & CFM_FACE) {
213 WCHAR *face = fmt->szFaceName;
214 BYTE bCharSet = (fmt->dwMask & CFM_CHARSET) ? fmt->bCharSet : DEFAULT_CHARSET;
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++;
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++;
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++;
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)
256 pCell = item->member.para.pCell;
257 if (pCell)
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++)
265 if (borders[i]->width > 0)
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++;
280 if (item == pLastPara)
281 break;
282 item = item->member.para.next_para;
283 } while (item);
285 if (!ME_StreamOutPrint(pStream, "{\\fonttbl"))
286 return FALSE;
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;
296 if (!ME_StreamOutRTFText(pStream, table[i].szFaceName, -1))
297 return FALSE;
298 if (!ME_StreamOutPrint(pStream, ";}"))
299 return FALSE;
301 if (!ME_StreamOutPrint(pStream, "}\r\n"))
302 return FALSE;
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;
315 if (!ME_StreamOutPrint(pStream, "}"))
316 return FALSE;
319 return TRUE;
322 static BOOL
323 ME_StreamOutRTFTableProps(ME_TextEditor *editor, ME_OutStream *pStream,
324 ME_DisplayItem *para)
326 ME_DisplayItem *cell;
327 char props[STREAMOUT_BUFFER_SIZE] = "";
328 int i;
329 const char sideChar[4] = {'t','l','b','r'};
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++)
349 if (borders[i]->width)
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;
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;
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++)
381 if (borders[i]->width)
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;
396 for (i = 0; i < pFmt->cTabCount; i++)
398 sprintf(props + strlen(props), "\\cellx%d", pFmt->rgxTabs[i] & 0x00FFFFFF);
401 if (!ME_StreamOutPrint(pStream, props))
402 return FALSE;
403 props[0] = '\0';
404 return TRUE;
407 static BOOL
408 ME_StreamOutRTFParaProps(ME_TextEditor *editor, ME_OutStream *pStream,
409 ME_DisplayItem *para)
411 PARAFORMAT2 *fmt = para->member.para.pFmt;
412 char props[STREAMOUT_BUFFER_SIZE] = "";
413 int i;
415 if (!editor->bEmulateVersion10) { /* v4.1 */
416 if (para->member.para.nFlags & MEPF_ROWSTART) {
417 pStream->nNestingLevel++;
418 if (pStream->nNestingLevel == 1) {
419 if (!ME_StreamOutRTFTableProps(editor, pStream, para))
420 return FALSE;
422 return TRUE;
423 } else if (para->member.para.nFlags & MEPF_ROWEND) {
424 pStream->nNestingLevel--;
425 if (pStream->nNestingLevel >= 1) {
426 if (!ME_StreamOutPrint(pStream, "{\\*\\nesttableprops"))
427 return FALSE;
428 if (!ME_StreamOutRTFTableProps(editor, pStream, para))
429 return FALSE;
430 if (!ME_StreamOutPrint(pStream, "\\nestrow}{\\nonesttables\\par}\r\n"))
431 return FALSE;
432 } else {
433 if (!ME_StreamOutPrint(pStream, "\\row \r\n"))
434 return FALSE;
436 return TRUE;
438 } else { /* v1.0 - 3.0 */
439 if (para->member.para.pFmt->dwMask & PFM_TABLE &&
440 para->member.para.pFmt->wEffects & PFE_TABLE)
442 if (!ME_StreamOutRTFTableProps(editor, pStream, para))
443 return FALSE;
447 /* TODO: Don't emit anything if the last PARAFORMAT2 is inherited */
448 if (!ME_StreamOutPrint(pStream, "\\pard"))
449 return FALSE;
451 if (!editor->bEmulateVersion10) { /* v4.1 */
452 if (pStream->nNestingLevel > 0)
453 strcat(props, "\\intbl");
454 if (pStream->nNestingLevel > 1)
455 sprintf(props + strlen(props), "\\itap%d", pStream->nNestingLevel);
456 } else { /* v1.0 - 3.0 */
457 if (fmt->dwMask & PFM_TABLE && fmt->wEffects & PFE_TABLE)
458 strcat(props, "\\intbl");
461 /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
462 * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
463 * set very different from the documentation.
464 * (Tested with RichEdit 5.50.25.0601) */
466 if (fmt->dwMask & PFM_ALIGNMENT) {
467 switch (fmt->wAlignment) {
468 case PFA_LEFT:
469 /* Default alignment: not emitted */
470 break;
471 case PFA_RIGHT:
472 strcat(props, "\\qr");
473 break;
474 case PFA_CENTER:
475 strcat(props, "\\qc");
476 break;
477 case PFA_JUSTIFY:
478 strcat(props, "\\qj");
479 break;
483 if (fmt->dwMask & PFM_LINESPACING) {
484 /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
485 * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
486 switch (fmt->bLineSpacingRule) {
487 case 0: /* Single spacing */
488 strcat(props, "\\sl-240\\slmult1");
489 break;
490 case 1: /* 1.5 spacing */
491 strcat(props, "\\sl-360\\slmult1");
492 break;
493 case 2: /* Double spacing */
494 strcat(props, "\\sl-480\\slmult1");
495 break;
496 case 3:
497 sprintf(props + strlen(props), "\\sl%d\\slmult0", fmt->dyLineSpacing);
498 break;
499 case 4:
500 sprintf(props + strlen(props), "\\sl-%d\\slmult0", fmt->dyLineSpacing);
501 break;
502 case 5:
503 sprintf(props + strlen(props), "\\sl-%d\\slmult1", fmt->dyLineSpacing * 240 / 20);
504 break;
508 if (fmt->dwMask & PFM_DONOTHYPHEN && fmt->wEffects & PFE_DONOTHYPHEN)
509 strcat(props, "\\hyph0");
510 if (fmt->dwMask & PFM_KEEP && fmt->wEffects & PFE_KEEP)
511 strcat(props, "\\keep");
512 if (fmt->dwMask & PFM_KEEPNEXT && fmt->wEffects & PFE_KEEPNEXT)
513 strcat(props, "\\keepn");
514 if (fmt->dwMask & PFM_NOLINENUMBER && fmt->wEffects & PFE_NOLINENUMBER)
515 strcat(props, "\\noline");
516 if (fmt->dwMask & PFM_NOWIDOWCONTROL && fmt->wEffects & PFE_NOWIDOWCONTROL)
517 strcat(props, "\\nowidctlpar");
518 if (fmt->dwMask & PFM_PAGEBREAKBEFORE && fmt->wEffects & PFE_PAGEBREAKBEFORE)
519 strcat(props, "\\pagebb");
520 if (fmt->dwMask & PFM_RTLPARA && fmt->wEffects & PFE_RTLPARA)
521 strcat(props, "\\rtlpar");
522 if (fmt->dwMask & PFM_SIDEBYSIDE && fmt->wEffects & PFE_SIDEBYSIDE)
523 strcat(props, "\\sbys");
525 if (!(editor->bEmulateVersion10 && /* v1.0 - 3.0 */
526 fmt->dwMask & PFM_TABLE && fmt->wEffects & PFE_TABLE))
528 if (fmt->dwMask & PFM_OFFSET)
529 sprintf(props + strlen(props), "\\li%d", fmt->dxOffset);
530 if (fmt->dwMask & PFM_OFFSETINDENT || fmt->dwMask & PFM_STARTINDENT)
531 sprintf(props + strlen(props), "\\fi%d", fmt->dxStartIndent);
532 if (fmt->dwMask & PFM_RIGHTINDENT)
533 sprintf(props + strlen(props), "\\ri%d", fmt->dxRightIndent);
534 if (fmt->dwMask & PFM_TABSTOPS) {
535 static const char * const leader[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
537 for (i = 0; i < fmt->cTabCount; i++) {
538 switch ((fmt->rgxTabs[i] >> 24) & 0xF) {
539 case 1:
540 strcat(props, "\\tqc");
541 break;
542 case 2:
543 strcat(props, "\\tqr");
544 break;
545 case 3:
546 strcat(props, "\\tqdec");
547 break;
548 case 4:
549 /* Word bar tab (vertical bar). Handled below */
550 break;
552 if (fmt->rgxTabs[i] >> 28 <= 5)
553 strcat(props, leader[fmt->rgxTabs[i] >> 28]);
554 sprintf(props+strlen(props), "\\tx%d", fmt->rgxTabs[i]&0x00FFFFFF);
558 if (fmt->dwMask & PFM_SPACEAFTER)
559 sprintf(props + strlen(props), "\\sa%d", fmt->dySpaceAfter);
560 if (fmt->dwMask & PFM_SPACEBEFORE)
561 sprintf(props + strlen(props), "\\sb%d", fmt->dySpaceBefore);
562 if (fmt->dwMask & PFM_STYLE)
563 sprintf(props + strlen(props), "\\s%d", fmt->sStyle);
565 if (fmt->dwMask & PFM_SHADING) {
566 static const char * const style[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
567 "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
568 "\\bghoriz", "\\bgvert", "\\bgfdiag",
569 "\\bgbdiag", "\\bgcross", "\\bgdcross",
570 "", "", "" };
571 if (fmt->wShadingWeight)
572 sprintf(props + strlen(props), "\\shading%d", fmt->wShadingWeight);
573 if (fmt->wShadingStyle & 0xF)
574 strcat(props, style[fmt->wShadingStyle & 0xF]);
575 sprintf(props + strlen(props), "\\cfpat%d\\cbpat%d",
576 (fmt->wShadingStyle >> 4) & 0xF, (fmt->wShadingStyle >> 8) & 0xF);
579 if (*props && !ME_StreamOutPrint(pStream, props))
580 return FALSE;
582 return TRUE;
586 static BOOL
587 ME_StreamOutRTFCharProps(ME_OutStream *pStream, CHARFORMAT2W *fmt)
589 char props[STREAMOUT_BUFFER_SIZE] = "";
590 unsigned int i;
592 if (fmt->dwMask & CFM_ALLCAPS && fmt->dwEffects & CFE_ALLCAPS)
593 strcat(props, "\\caps");
594 if (fmt->dwMask & CFM_ANIMATION)
595 sprintf(props + strlen(props), "\\animtext%u", fmt->bAnimation);
596 if (fmt->dwMask & CFM_BACKCOLOR) {
597 if (!(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
598 for (i = 1; i < pStream->nColorTblLen; i++)
599 if (pStream->colortbl[i] == fmt->crBackColor) {
600 sprintf(props + strlen(props), "\\cb%u", i);
601 break;
605 if (fmt->dwMask & CFM_BOLD && fmt->dwEffects & CFE_BOLD)
606 strcat(props, "\\b");
607 if (fmt->dwMask & CFM_COLOR) {
608 if (!(fmt->dwEffects & CFE_AUTOCOLOR)) {
609 for (i = 1; i < pStream->nColorTblLen; i++)
610 if (pStream->colortbl[i] == fmt->crTextColor) {
611 sprintf(props + strlen(props), "\\cf%u", i);
612 break;
616 /* TODO: CFM_DISABLED */
617 if (fmt->dwMask & CFM_EMBOSS && fmt->dwEffects & CFE_EMBOSS)
618 strcat(props, "\\embo");
619 if (fmt->dwMask & CFM_HIDDEN && fmt->dwEffects & CFE_HIDDEN)
620 strcat(props, "\\v");
621 if (fmt->dwMask & CFM_IMPRINT && fmt->dwEffects & CFE_IMPRINT)
622 strcat(props, "\\impr");
623 if (fmt->dwMask & CFM_ITALIC && fmt->dwEffects & CFE_ITALIC)
624 strcat(props, "\\i");
625 if (fmt->dwMask & CFM_KERNING)
626 sprintf(props + strlen(props), "\\kerning%u", fmt->wKerning);
627 if (fmt->dwMask & CFM_LCID) {
628 /* TODO: handle SFF_PLAINRTF */
629 if (LOWORD(fmt->lcid) == 1024)
630 strcat(props, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
631 else
632 sprintf(props + strlen(props), "\\lang%u", LOWORD(fmt->lcid));
634 /* CFM_LINK is not streamed out by M$ */
635 if (fmt->dwMask & CFM_OFFSET) {
636 if (fmt->yOffset >= 0)
637 sprintf(props + strlen(props), "\\up%d", fmt->yOffset);
638 else
639 sprintf(props + strlen(props), "\\dn%d", -fmt->yOffset);
641 if (fmt->dwMask & CFM_OUTLINE && fmt->dwEffects & CFE_OUTLINE)
642 strcat(props, "\\outl");
643 if (fmt->dwMask & CFM_PROTECTED && fmt->dwEffects & CFE_PROTECTED)
644 strcat(props, "\\protect");
645 /* TODO: CFM_REVISED CFM_REVAUTHOR - probably using rsidtbl? */
646 if (fmt->dwMask & CFM_SHADOW && fmt->dwEffects & CFE_SHADOW)
647 strcat(props, "\\shad");
648 if (fmt->dwMask & CFM_SIZE)
649 sprintf(props + strlen(props), "\\fs%d", fmt->yHeight / 10);
650 if (fmt->dwMask & CFM_SMALLCAPS && fmt->dwEffects & CFE_SMALLCAPS)
651 strcat(props, "\\scaps");
652 if (fmt->dwMask & CFM_SPACING)
653 sprintf(props + strlen(props), "\\expnd%u\\expndtw%u", fmt->sSpacing / 5, fmt->sSpacing);
654 if (fmt->dwMask & CFM_STRIKEOUT && fmt->dwEffects & CFE_STRIKEOUT)
655 strcat(props, "\\strike");
656 if (fmt->dwMask & CFM_STYLE) {
657 sprintf(props + strlen(props), "\\cs%u", fmt->sStyle);
658 /* TODO: emit style contents here */
660 if (fmt->dwMask & (CFM_SUBSCRIPT | CFM_SUPERSCRIPT)) {
661 if (fmt->dwEffects & CFE_SUBSCRIPT)
662 strcat(props, "\\sub");
663 else if (fmt->dwEffects & CFE_SUPERSCRIPT)
664 strcat(props, "\\super");
666 if (fmt->dwMask & CFM_UNDERLINE || fmt->dwMask & CFM_UNDERLINETYPE) {
667 if (fmt->dwMask & CFM_UNDERLINETYPE)
668 switch (fmt->bUnderlineType) {
669 case CFU_CF1UNDERLINE:
670 case CFU_UNDERLINE:
671 strcat(props, "\\ul");
672 break;
673 case CFU_UNDERLINEDOTTED:
674 strcat(props, "\\uld");
675 break;
676 case CFU_UNDERLINEDOUBLE:
677 strcat(props, "\\uldb");
678 break;
679 case CFU_UNDERLINEWORD:
680 strcat(props, "\\ulw");
681 break;
682 case CFU_UNDERLINENONE:
683 default:
684 strcat(props, "\\ul0");
685 break;
687 else if (fmt->dwEffects & CFE_UNDERLINE)
688 strcat(props, "\\ul");
690 /* FIXME: How to emit CFM_WEIGHT? */
692 if (fmt->dwMask & CFM_FACE || fmt->dwMask & CFM_CHARSET) {
693 WCHAR *szFaceName;
695 if (fmt->dwMask & CFM_FACE)
696 szFaceName = fmt->szFaceName;
697 else
698 szFaceName = pStream->fonttbl[0].szFaceName;
699 for (i = 0; i < pStream->nFontTblLen; i++) {
700 if (szFaceName == pStream->fonttbl[i].szFaceName
701 || !lstrcmpW(szFaceName, pStream->fonttbl[i].szFaceName))
702 if (!(fmt->dwMask & CFM_CHARSET)
703 || fmt->bCharSet == pStream->fonttbl[i].bCharSet)
704 break;
706 if (i < pStream->nFontTblLen)
708 if (i != pStream->nDefaultFont)
709 sprintf(props + strlen(props), "\\f%u", i);
711 /* In UTF-8 mode, charsets/codepages are not used */
712 if (pStream->nDefaultCodePage != CP_UTF8)
714 if (pStream->fonttbl[i].bCharSet == DEFAULT_CHARSET)
715 pStream->nCodePage = pStream->nDefaultCodePage;
716 else
717 pStream->nCodePage = RTFCharSetToCodePage(NULL, pStream->fonttbl[i].bCharSet);
721 if (*props)
722 strcat(props, " ");
723 if (!ME_StreamOutPrint(pStream, props))
724 return FALSE;
725 return TRUE;
729 static BOOL
730 ME_StreamOutRTFText(ME_OutStream *pStream, const WCHAR *text, LONG nChars)
732 char buffer[STREAMOUT_BUFFER_SIZE];
733 int pos = 0;
734 int fit, nBytes, i;
736 if (nChars == -1)
737 nChars = lstrlenW(text);
739 while (nChars) {
740 /* In UTF-8 mode, font charsets are not used. */
741 if (pStream->nDefaultCodePage == CP_UTF8) {
742 /* 6 is the maximum character length in UTF-8 */
743 fit = min(nChars, STREAMOUT_BUFFER_SIZE / 6);
744 nBytes = WideCharToMultiByte(CP_UTF8, 0, text, fit, buffer,
745 STREAMOUT_BUFFER_SIZE, NULL, NULL);
746 nChars -= fit;
747 text += fit;
748 for (i = 0; i < nBytes; i++)
749 if (buffer[i] == '{' || buffer[i] == '}' || buffer[i] == '\\') {
750 if (!ME_StreamOutPrint(pStream, "%.*s\\", i - pos, buffer + pos))
751 return FALSE;
752 pos = i;
754 if (pos < nBytes)
755 if (!ME_StreamOutMove(pStream, buffer + pos, nBytes - pos))
756 return FALSE;
757 pos = 0;
758 } else if (*text < 128) {
759 if (*text == '{' || *text == '}' || *text == '\\')
760 buffer[pos++] = '\\';
761 buffer[pos++] = (char)(*text++);
762 nChars--;
763 } else {
764 BOOL unknown = FALSE;
765 char letter[3];
767 /* FIXME: In the MS docs for WideCharToMultiByte there is a big list of
768 * codepages including CP_SYMBOL for which the last parameter must be set
769 * to NULL for the function to succeed. But in Wine we need to care only
770 * about CP_SYMBOL */
771 nBytes = WideCharToMultiByte(pStream->nCodePage, 0, text, 1,
772 letter, 3, NULL,
773 (pStream->nCodePage == CP_SYMBOL) ? NULL : &unknown);
774 if (unknown)
775 pos += sprintf(buffer + pos, "\\u%d?", (short)*text);
776 else if ((BYTE)*letter < 128) {
777 if (*letter == '{' || *letter == '}' || *letter == '\\')
778 buffer[pos++] = '\\';
779 buffer[pos++] = *letter;
780 } else {
781 for (i = 0; i < nBytes; i++)
782 pos += sprintf(buffer + pos, "\\'%02x", (BYTE)letter[i]);
784 text++;
785 nChars--;
787 if (pos >= STREAMOUT_BUFFER_SIZE - 11) {
788 if (!ME_StreamOutMove(pStream, buffer, pos))
789 return FALSE;
790 pos = 0;
793 return ME_StreamOutMove(pStream, buffer, pos);
797 static BOOL ME_StreamOutRTF(ME_TextEditor *editor, ME_OutStream *pStream,
798 const ME_Cursor *start, int nChars, int dwFormat)
800 ME_Cursor cursor = *start;
801 ME_DisplayItem *prev_para = cursor.pPara;
802 ME_Cursor endCur = cursor;
804 ME_MoveCursorChars(editor, &endCur, nChars);
806 if (!ME_StreamOutRTFHeader(pStream, dwFormat))
807 return FALSE;
809 if (!ME_StreamOutRTFFontAndColorTbl(pStream, cursor.pRun, endCur.pRun))
810 return FALSE;
812 /* TODO: stylesheet table */
814 /* FIXME: maybe emit something smarter for the generator? */
815 if (!ME_StreamOutPrint(pStream, "{\\*\\generator Wine Riched20 2.0.????;}"))
816 return FALSE;
818 /* TODO: information group */
820 /* TODO: document formatting properties */
822 /* FIXME: We have only one document section */
824 /* TODO: section formatting properties */
826 if (!ME_StreamOutRTFParaProps(editor, pStream, cursor.pPara))
827 return FALSE;
829 do {
830 if (cursor.pPara != prev_para)
832 prev_para = cursor.pPara;
833 if (!ME_StreamOutRTFParaProps(editor, pStream, cursor.pPara))
834 return FALSE;
837 if (cursor.pRun == endCur.pRun && !endCur.nOffset)
838 break;
839 TRACE("flags %xh\n", cursor.pRun->member.run.nFlags);
840 /* TODO: emit embedded objects */
841 if (cursor.pPara->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
842 continue;
843 if (cursor.pRun->member.run.nFlags & MERF_GRAPHICS) {
844 FIXME("embedded objects are not handled\n");
845 } else if (cursor.pRun->member.run.nFlags & MERF_TAB) {
846 if (editor->bEmulateVersion10 && /* v1.0 - 3.0 */
847 cursor.pPara->member.para.pFmt->dwMask & PFM_TABLE &&
848 cursor.pPara->member.para.pFmt->wEffects & PFE_TABLE)
850 if (!ME_StreamOutPrint(pStream, "\\cell "))
851 return FALSE;
852 } else {
853 if (!ME_StreamOutPrint(pStream, "\\tab "))
854 return FALSE;
856 } else if (cursor.pRun->member.run.nFlags & MERF_ENDCELL) {
857 if (pStream->nNestingLevel > 1) {
858 if (!ME_StreamOutPrint(pStream, "\\nestcell "))
859 return FALSE;
860 } else {
861 if (!ME_StreamOutPrint(pStream, "\\cell "))
862 return FALSE;
864 nChars--;
865 } else if (cursor.pRun->member.run.nFlags & MERF_ENDPARA) {
866 if (cursor.pPara->member.para.pFmt->dwMask & PFM_TABLE &&
867 cursor.pPara->member.para.pFmt->wEffects & PFE_TABLE &&
868 !(cursor.pPara->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND|MEPF_CELL)))
870 if (!ME_StreamOutPrint(pStream, "\\row \r\n"))
871 return FALSE;
872 } else {
873 if (!ME_StreamOutPrint(pStream, "\r\n\\par"))
874 return FALSE;
876 /* Skip as many characters as required by current line break */
877 nChars = max(0, nChars - cursor.pRun->member.run.strText->nLen);
878 } else if (cursor.pRun->member.run.nFlags & MERF_ENDROW) {
879 if (!ME_StreamOutPrint(pStream, "\\line \r\n"))
880 return FALSE;
881 nChars--;
882 } else {
883 int nEnd;
885 if (!ME_StreamOutPrint(pStream, "{"))
886 return FALSE;
887 TRACE("style %p\n", cursor.pRun->member.run.style);
888 if (!ME_StreamOutRTFCharProps(pStream, &cursor.pRun->member.run.style->fmt))
889 return FALSE;
891 nEnd = (cursor.pRun == endCur.pRun) ? endCur.nOffset : cursor.pRun->member.run.strText->nLen;
892 if (!ME_StreamOutRTFText(pStream, cursor.pRun->member.run.strText->szData + cursor.nOffset,
893 nEnd - cursor.nOffset))
894 return FALSE;
895 cursor.nOffset = 0;
896 if (!ME_StreamOutPrint(pStream, "}"))
897 return FALSE;
899 } while (cursor.pRun != endCur.pRun && ME_NextRun(&cursor.pPara, &cursor.pRun));
901 if (!ME_StreamOutMove(pStream, "}\0", 2))
902 return FALSE;
903 return TRUE;
907 static BOOL ME_StreamOutText(ME_TextEditor *editor, ME_OutStream *pStream,
908 const ME_Cursor *start, int nChars, DWORD dwFormat)
910 ME_Cursor cursor = *start;
911 int nLen;
912 UINT nCodePage = CP_ACP;
913 char *buffer = NULL;
914 int nBufLen = 0;
915 BOOL success = TRUE;
917 if (!cursor.pRun)
918 return FALSE;
920 if (dwFormat & SF_USECODEPAGE)
921 nCodePage = HIWORD(dwFormat);
923 /* TODO: Handle SF_TEXTIZED */
925 while (success && nChars && cursor.pRun) {
926 nLen = min(nChars, cursor.pRun->member.run.strText->nLen - cursor.nOffset);
928 if (!editor->bEmulateVersion10 && cursor.pRun->member.run.nFlags & MERF_ENDPARA)
930 static const WCHAR szEOL[] = { '\r', '\n' };
932 /* richedit 2.0 - all line breaks are \r\n */
933 if (dwFormat & SF_UNICODE)
934 success = ME_StreamOutMove(pStream, (const char *)szEOL, sizeof(szEOL));
935 else
936 success = ME_StreamOutMove(pStream, "\r\n", 2);
937 } else {
938 if (dwFormat & SF_UNICODE)
939 success = ME_StreamOutMove(pStream, (const char *)(cursor.pRun->member.run.strText->szData + cursor.nOffset),
940 sizeof(WCHAR) * nLen);
941 else {
942 int nSize;
944 nSize = WideCharToMultiByte(nCodePage, 0, cursor.pRun->member.run.strText->szData + cursor.nOffset,
945 nLen, NULL, 0, NULL, NULL);
946 if (nSize > nBufLen) {
947 FREE_OBJ(buffer);
948 buffer = ALLOC_N_OBJ(char, nSize);
949 nBufLen = nSize;
951 WideCharToMultiByte(nCodePage, 0, cursor.pRun->member.run.strText->szData + cursor.nOffset,
952 nLen, buffer, nSize, NULL, NULL);
953 success = ME_StreamOutMove(pStream, buffer, nSize);
957 nChars -= nLen;
958 cursor.nOffset = 0;
959 cursor.pRun = ME_FindItemFwd(cursor.pRun, diRun);
962 FREE_OBJ(buffer);
963 return success;
967 LRESULT ME_StreamOutRange(ME_TextEditor *editor, DWORD dwFormat,
968 const ME_Cursor *start,
969 int nChars, EDITSTREAM *stream)
971 ME_OutStream *pStream = ME_StreamOutInit(editor, stream);
973 if (dwFormat & SF_RTF)
974 ME_StreamOutRTF(editor, pStream, start, nChars, dwFormat);
975 else if (dwFormat & SF_TEXT || dwFormat & SF_TEXTIZED)
976 ME_StreamOutText(editor, pStream, start, nChars, dwFormat);
977 if (!pStream->stream->dwError)
978 ME_StreamOutFlush(pStream);
979 return ME_StreamOutFree(pStream);
982 LRESULT
983 ME_StreamOut(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream)
985 ME_Cursor start;
986 int nChars;
988 if (dwFormat & SFF_SELECTION) {
989 int nStart, nTo;
990 start = editor->pCursors[ME_GetSelectionOfs(editor, &nStart, &nTo)];
991 nChars = nTo - nStart;
992 } else {
993 ME_SetCursorToStart(editor, &start);
994 nChars = ME_GetTextLength(editor);
995 /* Generate an end-of-paragraph at the end of SCF_ALL RTF output */
996 if (dwFormat & SF_RTF)
997 nChars++;
999 return ME_StreamOutRange(editor, dwFormat, &start, nChars, stream);