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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
28 ME_StreamOutRTFText(ME_OutStream *pStream, WCHAR *text, LONG nChars);
32 ME_StreamOutInit(ME_TextEditor *editor, EDITSTREAM *stream)
34 ME_OutStream *pStream = ALLOC_OBJ(ME_OutStream);
35 pStream->stream = stream;
36 pStream->stream->dwError = 0;
39 pStream->nFontTblLen = 0;
40 pStream->nColorTblLen = 1;
46 ME_StreamOutFlush(ME_OutStream *pStream)
51 EDITSTREAM *stream = pStream->stream;
54 TRACE("sending %lu bytes\n", pStream->pos - nStart);
55 /* Some apps seem not to set *pcb unless a problem arises, relying
56 on initial random nWritten value, which is usually >STREAMOUT_BUFFER_SIZE */
57 nRemaining = pStream->pos - nStart;
58 nWritten = 0xDEADBEEF;
59 stream->dwError = stream->pfnCallback(stream->dwCookie, (LPBYTE)pStream->buffer + nStart,
60 pStream->pos - nStart, &nWritten);
61 TRACE("error=%lu written=%lu\n", stream->dwError, nWritten);
62 if (nWritten > (pStream->pos - nStart) || nWritten<0) {
63 FIXME("Invalid returned written size *pcb: 0x%x (%ld) instead of %ld\n",
64 (unsigned)nWritten, nWritten, nRemaining);
65 nWritten = nRemaining;
67 if (nWritten == 0 || stream->dwError)
69 pStream->written += nWritten;
71 } while (nStart < pStream->pos);
78 ME_StreamOutFree(ME_OutStream *pStream)
80 LONG written = pStream->written;
81 TRACE("total length = %lu\n", written);
89 ME_StreamOutMove(ME_OutStream *pStream, const char *buffer, int len)
92 int space = STREAMOUT_BUFFER_SIZE - pStream->pos;
93 int fit = min(space, len);
95 TRACE("%u:%u:%.*s\n", pStream->pos, fit, fit, buffer);
96 memmove(pStream->buffer + pStream->pos, buffer, fit);
100 if (pStream->pos == STREAMOUT_BUFFER_SIZE) {
101 if (!ME_StreamOutFlush(pStream))
110 ME_StreamOutPrint(ME_OutStream *pStream, const char *format, ...)
112 char string[STREAMOUT_BUFFER_SIZE]; /* This is going to be enough */
116 va_start(valist, format);
117 len = vsnprintf(string, sizeof(string), format, valist);
120 return ME_StreamOutMove(pStream, string, len);
125 ME_StreamOutRTFHeader(ME_OutStream *pStream, int dwFormat)
127 const char *cCharSet = NULL;
132 if (dwFormat & SF_USECODEPAGE) {
135 switch (HIWORD(dwFormat)) {
138 nCodePage = GetACP();
141 nCodePage = GetOEMCP();
142 if (nCodePage == 437)
144 else if (nCodePage == 850)
153 if (HIWORD(dwFormat) == CP_MACCP) {
155 nCodePage = 10000; /* MacRoman */
158 nCodePage = 1252; /* Latin-1 */
160 if (GetCPInfoExW(HIWORD(dwFormat), 0, &info))
161 nCodePage = info.CodePage;
165 /* TODO: If the original document contained an \ansicpg value, retain it.
166 * Otherwise, M$ richedit emits a codepage number determined from the
167 * charset of the default font here. Anyway, this value is not used by
169 nCodePage = GetACP();
171 if (nCodePage == CP_UTF8)
172 success = ME_StreamOutPrint(pStream, "{\\urtf");
174 success = ME_StreamOutPrint(pStream, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet, nCodePage);
179 pStream->nDefaultCodePage = nCodePage;
181 /* FIXME: This should be a document property */
182 /* TODO: handle SFF_PLAINRTF */
183 language = GetUserDefaultLangID();
184 if (!ME_StreamOutPrint(pStream, "\\deff0\\deflang%u\\deflangfe%u", language, language))
187 /* FIXME: This should be a document property */
188 pStream->nDefaultFont = 0;
195 ME_StreamOutRTFFontAndColorTbl(ME_OutStream *pStream, ME_DisplayItem *pFirstRun, ME_DisplayItem *pLastRun)
197 ME_DisplayItem *item = pFirstRun;
198 ME_FontTableItem *table = pStream->fonttbl;
202 CHARFORMAT2W *fmt = &item->member.run.style->fmt;
205 if (fmt->dwMask & CFM_FACE) {
206 WCHAR *face = fmt->szFaceName;
207 BYTE bCharSet = (fmt->dwMask & CFM_CHARSET) ? fmt->bCharSet : DEFAULT_CHARSET;
209 for (i = 0; i < pStream->nFontTblLen; i++)
210 if (table[i].bCharSet == bCharSet
211 && (table[i].szFaceName == face || !lstrcmpW(table[i].szFaceName, face)))
213 if (i == pStream->nFontTblLen) {
214 table[i].bCharSet = bCharSet;
215 table[i].szFaceName = face;
216 pStream->nFontTblLen++;
220 if (fmt->dwMask & CFM_COLOR && !(fmt->dwEffects & CFE_AUTOCOLOR)) {
221 crColor = fmt->crTextColor;
222 for (i = 1; i < pStream->nColorTblLen; i++)
223 if (pStream->colortbl[i] == crColor)
225 if (i == pStream->nColorTblLen) {
226 pStream->colortbl[i] = crColor;
227 pStream->nColorTblLen++;
230 if (fmt->dwMask & CFM_BACKCOLOR && !(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
231 crColor = fmt->crBackColor;
232 for (i = 1; i < pStream->nColorTblLen; i++)
233 if (pStream->colortbl[i] == crColor)
235 if (i == pStream->nColorTblLen) {
236 pStream->colortbl[i] = crColor;
237 pStream->nColorTblLen++;
241 if (item == pLastRun)
243 item = ME_FindItemFwd(item, diRun);
246 if (!ME_StreamOutPrint(pStream, "{\\fonttbl"))
249 for (i = 0; i < pStream->nFontTblLen; i++) {
250 if (table[i].bCharSet != DEFAULT_CHARSET) {
251 if (!ME_StreamOutPrint(pStream, "{\\f%u\\fcharset%u ", i, table[i].bCharSet))
254 if (!ME_StreamOutPrint(pStream, "{\\f%u ", i))
257 if (!ME_StreamOutRTFText(pStream, table[i].szFaceName, -1))
259 if (!ME_StreamOutPrint(pStream, ";}\r\n"))
262 if (!ME_StreamOutPrint(pStream, "}"))
265 /* Output colors table if not empty */
266 if (pStream->nColorTblLen > 1) {
267 if (!ME_StreamOutPrint(pStream, "{\\colortbl;"))
269 for (i = 1; i < pStream->nColorTblLen; i++) {
270 if (!ME_StreamOutPrint(pStream, "\\red%u\\green%u\\blue%u;",
271 pStream->colortbl[i] & 0xFF,
272 (pStream->colortbl[i] >> 8) & 0xFF,
273 (pStream->colortbl[i] >> 16) & 0xFF))
276 if (!ME_StreamOutPrint(pStream, "}"))
285 ME_StreamOutRTFParaProps(ME_OutStream *pStream, ME_DisplayItem *para)
287 PARAFORMAT2 *fmt = para->member.para.pFmt;
288 char props[STREAMOUT_BUFFER_SIZE] = "";
291 if (para->member.para.pCells)
293 ME_TableCell *cell = para->member.para.pCells;
295 if (!ME_StreamOutPrint(pStream, "\\trowd"))
298 sprintf(props, "\\cellx%d", cell->nRightBoundary);
299 if (!ME_StreamOutPrint(pStream, props))
306 /* TODO: Don't emit anything if the last PARAFORMAT2 is inherited */
307 if (!ME_StreamOutPrint(pStream, "\\pard"))
310 if (para->member.para.bTable)
311 strcat(props, "\\intbl");
313 /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
314 * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
315 * set very different from the documentation.
316 * (Tested with RichEdit 5.50.25.0601) */
318 if (fmt->dwMask & PFM_ALIGNMENT) {
319 switch (fmt->wAlignment) {
321 /* Default alignment: not emitted */
324 strcat(props, "\\qr");
327 strcat(props, "\\qc");
330 strcat(props, "\\qj");
335 if (fmt->dwMask & PFM_LINESPACING) {
336 /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
337 * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
338 switch (fmt->bLineSpacingRule) {
339 case 0: /* Single spacing */
340 strcat(props, "\\sl-240\\slmult1");
342 case 1: /* 1.5 spacing */
343 strcat(props, "\\sl-360\\slmult1");
345 case 2: /* Double spacing */
346 strcat(props, "\\sl-480\\slmult1");
349 sprintf(props + strlen(props), "\\sl%ld\\slmult0", fmt->dyLineSpacing);
352 sprintf(props + strlen(props), "\\sl-%ld\\slmult0", fmt->dyLineSpacing);
355 sprintf(props + strlen(props), "\\sl-%ld\\slmult1", fmt->dyLineSpacing * 240 / 20);
360 if (fmt->dwMask & PFM_DONOTHYPHEN && fmt->wEffects & PFE_DONOTHYPHEN)
361 strcat(props, "\\hyph0");
362 if (fmt->dwMask & PFM_KEEP && fmt->wEffects & PFE_KEEP)
363 strcat(props, "\\keep");
364 if (fmt->dwMask & PFM_KEEPNEXT && fmt->wEffects & PFE_KEEPNEXT)
365 strcat(props, "\\keepn");
366 if (fmt->dwMask & PFM_NOLINENUMBER && fmt->wEffects & PFE_NOLINENUMBER)
367 strcat(props, "\\noline");
368 if (fmt->dwMask & PFM_NOWIDOWCONTROL && fmt->wEffects & PFE_NOWIDOWCONTROL)
369 strcat(props, "\\nowidctlpar");
370 if (fmt->dwMask & PFM_PAGEBREAKBEFORE && fmt->wEffects & PFE_PAGEBREAKBEFORE)
371 strcat(props, "\\pagebb");
372 if (fmt->dwMask & PFM_RTLPARA && fmt->wEffects & PFE_RTLPARA)
373 strcat(props, "\\rtlpar");
374 if (fmt->dwMask & PFM_SIDEBYSIDE && fmt->wEffects & PFE_SIDEBYSIDE)
375 strcat(props, "\\sbys");
376 if (fmt->dwMask & PFM_TABLE && fmt->dwMask & PFE_TABLE)
377 strcat(props, "\\intbl");
379 if (fmt->dwMask & PFM_OFFSET)
380 sprintf(props + strlen(props), "\\li%ld", fmt->dxOffset);
381 if (fmt->dwMask & PFM_OFFSETINDENT || fmt->dwMask & PFM_STARTINDENT)
382 sprintf(props + strlen(props), "\\fi%ld", fmt->dxStartIndent);
383 if (fmt->dwMask & PFM_RIGHTINDENT)
384 sprintf(props + strlen(props), "\\ri%ld", fmt->dxRightIndent);
385 if (fmt->dwMask & PFM_SPACEAFTER)
386 sprintf(props + strlen(props), "\\sa%ld", fmt->dySpaceAfter);
387 if (fmt->dwMask & PFM_SPACEBEFORE)
388 sprintf(props + strlen(props), "\\sb%ld", fmt->dySpaceBefore);
389 if (fmt->dwMask & PFM_STYLE)
390 sprintf(props + strlen(props), "\\s%d", fmt->sStyle);
392 if (fmt->dwMask & PFM_TABSTOPS) {
393 static const char *leader[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
395 for (i = 0; i < fmt->cTabCount; i++) {
396 switch ((fmt->rgxTabs[i] >> 24) & 0xF) {
398 strcat(props, "\\tqc");
401 strcat(props, "\\tqr");
404 strcat(props, "\\tqdec");
407 /* Word bar tab (vertical bar). Handled below */
410 if (fmt->rgxTabs[i] >> 28 <= 5)
411 strcat(props, leader[fmt->rgxTabs[i] >> 28]);
412 sprintf(props+strlen(props), "\\tx%ld", fmt->rgxTabs[i]&0x00FFFFFF);
417 if (fmt->dwMask & PFM_SHADING) {
418 static const char *style[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
419 "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
420 "\\bghoriz", "\\bgvert", "\\bgfdiag",
421 "\\bgbdiag", "\\bgcross", "\\bgdcross",
423 if (fmt->wShadingWeight)
424 sprintf(props + strlen(props), "\\shading%d", fmt->wShadingWeight);
425 if (fmt->wShadingStyle & 0xF)
426 strcat(props, style[fmt->wShadingStyle & 0xF]);
427 sprintf(props + strlen(props), "\\cfpat%d\\cbpat%d",
428 (fmt->wShadingStyle >> 4) & 0xF, (fmt->wShadingStyle >> 8) & 0xF);
431 if (*props && !ME_StreamOutPrint(pStream, props))
439 ME_StreamOutRTFCharProps(ME_OutStream *pStream, CHARFORMAT2W *fmt)
441 char props[STREAMOUT_BUFFER_SIZE] = "";
444 if (fmt->dwMask & CFM_ALLCAPS && fmt->dwEffects & CFE_ALLCAPS)
445 strcat(props, "\\caps");
446 if (fmt->dwMask & CFM_ANIMATION)
447 sprintf(props + strlen(props), "\\animtext%u", fmt->bAnimation);
448 if (fmt->dwMask & CFM_BACKCOLOR) {
449 if (!(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
450 for (i = 1; i < pStream->nColorTblLen; i++)
451 if (pStream->colortbl[i] == fmt->crBackColor) {
452 sprintf(props + strlen(props), "\\cb%u", i);
457 if (fmt->dwMask & CFM_BOLD && fmt->dwEffects & CFE_BOLD)
458 strcat(props, "\\b");
459 if (fmt->dwMask & CFM_COLOR) {
460 if (!(fmt->dwEffects & CFE_AUTOCOLOR)) {
461 for (i = 1; i < pStream->nColorTblLen; i++)
462 if (pStream->colortbl[i] == fmt->crTextColor) {
463 sprintf(props + strlen(props), "\\cf%u", i);
468 /* TODO: CFM_DISABLED */
469 if (fmt->dwMask & CFM_EMBOSS && fmt->dwEffects & CFE_EMBOSS)
470 strcat(props, "\\embo");
471 if (fmt->dwMask & CFM_HIDDEN && fmt->dwEffects & CFE_HIDDEN)
472 strcat(props, "\\v");
473 if (fmt->dwMask & CFM_IMPRINT && fmt->dwEffects & CFE_IMPRINT)
474 strcat(props, "\\impr");
475 if (fmt->dwMask & CFM_ITALIC && fmt->dwEffects & CFE_ITALIC)
476 strcat(props, "\\i");
477 if (fmt->dwMask & CFM_KERNING)
478 sprintf(props + strlen(props), "\\kerning%u", fmt->wKerning);
479 if (fmt->dwMask & CFM_LCID) {
480 /* TODO: handle SFF_PLAINRTF */
481 if (LOWORD(fmt->lcid) == 1024)
482 strcat(props, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
484 sprintf(props + strlen(props), "\\lang%u", LOWORD(fmt->lcid));
486 /* CFM_LINK is not streamed out by M$ */
487 if (fmt->dwMask & CFM_OFFSET) {
488 if (fmt->yOffset >= 0)
489 sprintf(props + strlen(props), "\\up%ld", fmt->yOffset);
491 sprintf(props + strlen(props), "\\dn%ld", -fmt->yOffset);
493 if (fmt->dwMask & CFM_OUTLINE && fmt->dwEffects & CFE_OUTLINE)
494 strcat(props, "\\outl");
495 if (fmt->dwMask & CFM_PROTECTED && fmt->dwEffects & CFE_PROTECTED)
496 strcat(props, "\\protect");
497 /* TODO: CFM_REVISED CFM_REVAUTHOR - probably using rsidtbl? */
498 if (fmt->dwMask & CFM_SHADOW && fmt->dwEffects & CFE_SHADOW)
499 strcat(props, "\\shad");
500 if (fmt->dwMask & CFM_SIZE)
501 sprintf(props + strlen(props), "\\fs%ld", fmt->yHeight / 10);
502 if (fmt->dwMask & CFM_SMALLCAPS && fmt->dwEffects & CFE_SMALLCAPS)
503 strcat(props, "\\scaps");
504 if (fmt->dwMask & CFM_SPACING)
505 sprintf(props + strlen(props), "\\expnd%u\\expndtw%u", fmt->sSpacing / 5, fmt->sSpacing);
506 if (fmt->dwMask & CFM_STRIKEOUT && fmt->dwEffects & CFE_STRIKEOUT)
507 strcat(props, "\\strike");
508 if (fmt->dwMask & CFM_STYLE) {
509 sprintf(props + strlen(props), "\\cs%u", fmt->sStyle);
510 /* TODO: emit style contents here */
512 if (fmt->dwMask & (CFM_SUBSCRIPT | CFM_SUPERSCRIPT)) {
513 if (fmt->dwEffects & CFE_SUBSCRIPT)
514 strcat(props, "\\sub");
515 else if (fmt->dwEffects & CFE_SUPERSCRIPT)
516 strcat(props, "\\super");
518 if (fmt->dwMask & CFM_UNDERLINE || fmt->dwMask & CFM_UNDERLINETYPE) {
519 if (fmt->dwMask & CFM_UNDERLINETYPE)
520 switch (fmt->bUnderlineType) {
521 case CFU_CF1UNDERLINE:
523 strcat(props, "\\ul");
525 case CFU_UNDERLINEDOTTED:
526 strcat(props, "\\uld");
528 case CFU_UNDERLINEDOUBLE:
529 strcat(props, "\\uldb");
531 case CFU_UNDERLINEWORD:
532 strcat(props, "\\ulw");
534 case CFU_UNDERLINENONE:
536 strcat(props, "\\ul0");
539 else if (fmt->dwEffects & CFE_UNDERLINE)
540 strcat(props, "\\ul");
542 /* FIXME: How to emit CFM_WEIGHT? */
544 if (fmt->dwMask & CFM_FACE || fmt->dwMask & CFM_CHARSET) {
547 if (fmt->dwMask & CFM_FACE)
548 szFaceName = fmt->szFaceName;
550 szFaceName = pStream->fonttbl[0].szFaceName;
551 for (i = 0; i < pStream->nFontTblLen; i++) {
552 if (szFaceName == pStream->fonttbl[i].szFaceName
553 || !lstrcmpW(szFaceName, pStream->fonttbl[i].szFaceName))
554 if (!(fmt->dwMask & CFM_CHARSET)
555 || fmt->bCharSet == pStream->fonttbl[i].bCharSet)
558 if (i < pStream->nFontTblLen)
560 if (i != pStream->nDefaultFont)
561 sprintf(props + strlen(props), "\\f%u", i);
563 /* In UTF-8 mode, charsets/codepages are not used */
564 if (pStream->nDefaultCodePage != CP_UTF8)
566 if (pStream->fonttbl[i].bCharSet == DEFAULT_CHARSET)
567 pStream->nCodePage = pStream->nDefaultCodePage;
569 pStream->nCodePage = RTFCharSetToCodePage(NULL, pStream->fonttbl[i].bCharSet);
575 if (!ME_StreamOutPrint(pStream, props))
582 ME_StreamOutRTFText(ME_OutStream *pStream, WCHAR *text, LONG nChars)
584 char buffer[STREAMOUT_BUFFER_SIZE];
589 nChars = lstrlenW(text);
592 /* In UTF-8 mode, font charsets are not used. */
593 if (pStream->nDefaultCodePage == CP_UTF8) {
594 /* 6 is the maximum character length in UTF-8 */
595 fit = min(nChars, STREAMOUT_BUFFER_SIZE / 6);
596 nBytes = WideCharToMultiByte(CP_UTF8, 0, text, fit, buffer,
597 STREAMOUT_BUFFER_SIZE, NULL, NULL);
600 for (i = 0; i < nBytes; i++)
601 if (buffer[i] == '{' || buffer[i] == '}' || buffer[i] == '\\') {
602 if (!ME_StreamOutPrint(pStream, "%.*s\\", i - pos, buffer + pos))
607 if (!ME_StreamOutMove(pStream, buffer + pos, nBytes - pos))
610 } else if (*text < 128) {
611 if (*text == '{' || *text == '}' || *text == '\\')
612 buffer[pos++] = '\\';
613 buffer[pos++] = (char)(*text++);
616 BOOL unknown = FALSE;
619 /* FIXME: In the MS docs for WideCharToMultiByte there is a big list of
620 * codepages including CP_SYMBOL for which the last parameter must be set
621 * to NULL for the function to succeed. But in Wine we need to care only
623 nBytes = WideCharToMultiByte(pStream->nCodePage, 0, text, 1,
625 (pStream->nCodePage == CP_SYMBOL) ? NULL : &unknown);
627 pos += sprintf(buffer + pos, "\\u%d?", (short)*text);
628 else if ((BYTE)*letter < 128) {
629 if (*letter == '{' || *letter == '}' || *letter == '\\')
630 buffer[pos++] = '\\';
631 buffer[pos++] = *letter;
633 for (i = 0; i < nBytes; i++)
634 pos += sprintf(buffer + pos, "\\'%02x", (BYTE)letter[i]);
639 if (pos >= STREAMOUT_BUFFER_SIZE - 11) {
640 if (!ME_StreamOutMove(pStream, buffer, pos))
645 return ME_StreamOutMove(pStream, buffer, pos);
650 ME_StreamOutRTF(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int nChars, int dwFormat)
652 ME_DisplayItem *p, *pEnd, *pPara;
653 int nOffset, nEndLen;
655 ME_RunOfsFromCharOfs(editor, nStart, &p, &nOffset);
656 ME_RunOfsFromCharOfs(editor, nStart+nChars, &pEnd, &nEndLen);
658 pPara = ME_GetParagraph(p);
660 if (!ME_StreamOutRTFHeader(pStream, dwFormat))
663 if (!ME_StreamOutRTFFontAndColorTbl(pStream, p, pEnd))
666 /* TODO: stylesheet table */
668 /* FIXME: maybe emit something smarter for the generator? */
669 if (!ME_StreamOutPrint(pStream, "{\\*\\generator Wine Riched20 2.0.????;}"))
672 /* TODO: information group */
674 /* TODO: document formatting properties */
676 /* FIXME: We have only one document section */
678 /* TODO: section formatting properties */
680 if (!ME_StreamOutRTFParaProps(pStream, ME_GetParagraph(p)))
688 if (!ME_StreamOutRTFParaProps(pStream, p))
693 if (p == pEnd && !nEndLen)
695 TRACE("flags %xh\n", p->member.run.nFlags);
696 /* TODO: emit embedded objects */
697 if (p->member.run.nFlags & MERF_GRAPHICS)
698 FIXME("embedded objects are not handled\n");
699 if (p->member.run.nFlags & MERF_CELL) {
700 if (!ME_StreamOutPrint(pStream, "\\cell "))
703 } else if (p->member.run.nFlags & MERF_ENDPARA) {
704 if (pPara->member.para.bTable) {
705 if (!ME_StreamOutPrint(pStream, "\\row \r\n"))
708 if (!ME_StreamOutPrint(pStream, "\r\n\\par"))
712 if (editor->bEmulateVersion10 && nChars)
717 if (!ME_StreamOutPrint(pStream, "{"))
719 TRACE("style %p\n", p->member.run.style);
720 if (!ME_StreamOutRTFCharProps(pStream, &p->member.run.style->fmt))
723 nEnd = (p == pEnd) ? nEndLen : ME_StrLen(p->member.run.strText);
724 if (!ME_StreamOutRTFText(pStream, p->member.run.strText->szData + nOffset, nEnd - nOffset))
727 if (!ME_StreamOutPrint(pStream, "}"))
731 default: /* we missed the last item */
736 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
738 if (!ME_StreamOutPrint(pStream, "}"))
745 ME_StreamOutText(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int nChars, DWORD dwFormat)
747 /* FIXME: use ME_RunOfsFromCharOfs */
748 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
750 UINT nCodePage = CP_ACP;
758 if (dwFormat & SF_USECODEPAGE)
759 nCodePage = HIWORD(dwFormat);
761 /* TODO: Handle SF_TEXTIZED */
763 while (success && nChars && item) {
764 nLen = ME_StrLen(item->member.run.strText) - nStart;
768 if (item->member.run.nFlags & MERF_ENDPARA) {
769 static const WCHAR szEOL[2] = { '\r', '\n' };
771 if (dwFormat & SF_UNICODE)
772 success = ME_StreamOutMove(pStream, (const char *)szEOL, sizeof(szEOL));
774 success = ME_StreamOutMove(pStream, "\r\n", 2);
776 if (dwFormat & SF_UNICODE)
777 success = ME_StreamOutMove(pStream, (const char *)(item->member.run.strText->szData + nStart),
778 sizeof(WCHAR) * nLen);
782 nSize = WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
783 nLen, NULL, 0, NULL, NULL);
784 if (nSize > nBufLen) {
787 buffer = ALLOC_N_OBJ(char, nSize);
790 WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
791 nLen, buffer, nSize, NULL, NULL);
792 success = ME_StreamOutMove(pStream, buffer, nSize);
797 if (editor->bEmulateVersion10 && nChars && item->member.run.nFlags & MERF_ENDPARA)
800 item = ME_FindItemFwd(item, diRun);
810 ME_StreamOutRange(ME_TextEditor *editor, DWORD dwFormat, int nStart, int nTo, EDITSTREAM *stream)
812 ME_OutStream *pStream = ME_StreamOutInit(editor, stream);
816 nTo = ME_GetTextLength(editor);
817 /* Generate an end-of-paragraph at the end of SCF_ALL RTF output */
818 if (dwFormat & SF_RTF)
821 TRACE("from %d to %d\n", nStart, nTo);
823 if (dwFormat & SF_RTF)
824 ME_StreamOutRTF(editor, pStream, nStart, nTo - nStart, dwFormat);
825 else if (dwFormat & SF_TEXT || dwFormat & SF_TEXTIZED)
826 ME_StreamOutText(editor, pStream, nStart, nTo - nStart, dwFormat);
827 if (!pStream->stream->dwError)
828 ME_StreamOutFlush(pStream);
829 return ME_StreamOutFree(pStream);
833 ME_StreamOut(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream)
837 if (dwFormat & SFF_SELECTION)
838 ME_GetSelection(editor, &nStart, &nTo);
843 return ME_StreamOutRange(editor, dwFormat, nStart, nTo, stream);