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_TextEditor *editor, WCHAR *text, LONG nChars);
32 ME_StreamOutInit(ME_TextEditor *editor, EDITSTREAM *stream)
34 editor->pStream = ALLOC_OBJ(ME_OutStream);
35 editor->pStream->stream = stream;
36 editor->pStream->stream->dwError = 0;
37 editor->pStream->pos = 0;
38 editor->pStream->written = 0;
39 editor->pStream->nFontTblLen = 0;
40 editor->pStream->nColorTblLen = 1;
45 ME_StreamOutFlush(ME_TextEditor *editor)
50 EDITSTREAM *stream = editor->pStream->stream;
53 TRACE("sending %lu bytes\n", editor->pStream->pos - nStart);
54 /* Some apps seem not to set *pcb unless a problem arises, relying
55 on initial random nWritten value, which is usually >STREAMOUT_BUFFER_SIZE */
56 nRemaining = editor->pStream->pos - nStart;
57 nWritten = 0xDEADBEEF;
58 stream->dwError = stream->pfnCallback(stream->dwCookie, editor->pStream->buffer + nStart,
59 editor->pStream->pos - nStart, &nWritten);
60 TRACE("error=%lu written=%lu\n", stream->dwError, nWritten);
61 if (nWritten > (editor->pStream->pos - nStart) || nWritten<0) {
62 FIXME("Invalid returned written size *pcb: 0x%x (%ld) instead of %ld\n",
63 (unsigned)nWritten, nWritten, nRemaining);
64 nWritten = nRemaining;
66 if (nWritten == 0 || stream->dwError)
68 editor->pStream->written += nWritten;
70 } while (nStart < editor->pStream->pos);
71 editor->pStream->pos = 0;
77 ME_StreamOutFree(ME_TextEditor *editor)
79 LONG written = editor->pStream->written;
80 TRACE("total length = %lu\n", written);
82 FREE_OBJ(editor->pStream);
83 editor->pStream = NULL;
89 ME_StreamOutMove(ME_TextEditor *editor, BYTE *buffer, int len)
91 ME_OutStream *pStream = editor->pStream;
94 int space = STREAMOUT_BUFFER_SIZE - pStream->pos;
95 int fit = min(space, len);
97 TRACE("%u:%u:%.*s\n", pStream->pos, fit, fit, buffer);
98 memmove(pStream->buffer + pStream->pos, buffer, fit);
102 if (pStream->pos == STREAMOUT_BUFFER_SIZE) {
103 if (!ME_StreamOutFlush(editor))
112 ME_StreamOutPrint(ME_TextEditor *editor, char *format, ...)
114 char string[STREAMOUT_BUFFER_SIZE]; /* This is going to be enough */
118 va_start(valist, format);
119 len = vsnprintf(string, sizeof(string), format, valist);
122 return ME_StreamOutMove(editor, string, len);
127 ME_StreamOutRTFHeader(ME_TextEditor *editor, int dwFormat)
129 char *cCharSet = NULL;
134 if (dwFormat & SF_USECODEPAGE) {
137 switch (HIWORD(dwFormat)) {
140 nCodePage = GetACP();
143 nCodePage = GetOEMCP();
144 if (nCodePage == 437)
146 else if (nCodePage == 850)
155 if (HIWORD(dwFormat) == CP_MACCP) {
157 nCodePage = 10000; /* MacRoman */
160 nCodePage = 1252; /* Latin-1 */
162 if (GetCPInfoExW(HIWORD(dwFormat), 0, &info))
163 nCodePage = info.CodePage;
167 /* TODO: If the original document contained an \ansicpg value, retain it.
168 * Otherwise, M$ richedit emits a codepage number determined from the
169 * charset of the default font here. Anyway, this value is not used by
171 nCodePage = GetACP();
173 if (nCodePage == CP_UTF8)
174 success = ME_StreamOutPrint(editor, "{\\urtf");
176 success = ME_StreamOutPrint(editor, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet, nCodePage);
181 editor->pStream->nDefaultCodePage = nCodePage;
183 /* FIXME: This should be a document property */
184 /* TODO: handle SFF_PLAINRTF */
185 language = GetUserDefaultLangID();
186 if (!ME_StreamOutPrint(editor, "\\deff0\\deflang%u\\deflangfe%u", language, language))
189 /* FIXME: This should be a document property */
190 editor->pStream->nDefaultFont = 0;
197 ME_StreamOutRTFFontAndColorTbl(ME_TextEditor *editor, ME_DisplayItem *pFirstRun, ME_DisplayItem *pLastRun)
199 ME_DisplayItem *item = pFirstRun;
200 ME_FontTableItem *table = editor->pStream->fonttbl;
204 CHARFORMAT2W *fmt = &item->member.run.style->fmt;
207 if (fmt->dwMask & CFM_FACE) {
208 WCHAR *face = fmt->szFaceName;
209 BYTE bCharSet = (fmt->dwMask & CFM_CHARSET) ? fmt->bCharSet : DEFAULT_CHARSET;
211 for (i = 0; i < editor->pStream->nFontTblLen; i++)
212 if (table[i].bCharSet == bCharSet
213 && (table[i].szFaceName == face || !lstrcmpW(table[i].szFaceName, face)))
215 if (i == editor->pStream->nFontTblLen) {
216 table[i].bCharSet = bCharSet;
217 table[i].szFaceName = face;
218 editor->pStream->nFontTblLen++;
222 if (fmt->dwMask & CFM_COLOR && !(fmt->dwEffects & CFE_AUTOCOLOR)) {
223 crColor = fmt->crTextColor;
224 for (i = 1; i < editor->pStream->nColorTblLen; i++)
225 if (editor->pStream->colortbl[i] == crColor)
227 if (i == editor->pStream->nColorTblLen) {
228 editor->pStream->colortbl[i] = crColor;
229 editor->pStream->nColorTblLen++;
232 if (fmt->dwMask & CFM_BACKCOLOR && !(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
233 crColor = fmt->crBackColor;
234 for (i = 1; i < editor->pStream->nColorTblLen; i++)
235 if (editor->pStream->colortbl[i] == crColor)
237 if (i == editor->pStream->nColorTblLen) {
238 editor->pStream->colortbl[i] = crColor;
239 editor->pStream->nColorTblLen++;
243 if (item == pLastRun)
245 item = ME_FindItemFwd(item, diRun);
248 if (!ME_StreamOutPrint(editor, "{\\fonttbl"))
251 for (i = 0; i < editor->pStream->nFontTblLen; i++) {
252 if (table[i].bCharSet != DEFAULT_CHARSET) {
253 if (!ME_StreamOutPrint(editor, "{\\f%u\\fcharset%u ", i, table[i].bCharSet))
256 if (!ME_StreamOutPrint(editor, "{\\f%u ", i))
259 if (!ME_StreamOutRTFText(editor, table[i].szFaceName, -1))
261 if (!ME_StreamOutPrint(editor, ";}\r\n"))
264 if (!ME_StreamOutPrint(editor, "}"))
267 /* Output colors table if not empty */
268 if (editor->pStream->nColorTblLen > 1) {
269 if (!ME_StreamOutPrint(editor, "{\\colortbl;"))
271 for (i = 1; i < editor->pStream->nColorTblLen; i++) {
272 if (!ME_StreamOutPrint(editor, "\\red%u\\green%u\\blue%u;",
273 editor->pStream->colortbl[i] & 0xFF,
274 (editor->pStream->colortbl[i] >> 8) & 0xFF,
275 (editor->pStream->colortbl[i] >> 16) & 0xFF))
278 if (!ME_StreamOutPrint(editor, "}"))
287 ME_StreamOutRTFParaProps(ME_TextEditor *editor, ME_DisplayItem *para)
289 PARAFORMAT2 *fmt = para->member.para.pFmt;
290 char props[STREAMOUT_BUFFER_SIZE] = "";
293 /* TODO: Don't emit anything if the last PARAFORMAT2 is inherited */
294 if (!ME_StreamOutPrint(editor, "\\pard"))
297 /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
298 * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
299 * set very different from the documentation.
300 * (Tested with RichEdit 5.50.25.0601) */
302 if (fmt->dwMask & PFM_ALIGNMENT) {
303 switch (fmt->wAlignment) {
305 /* Default alignment: not emitted */
308 strcat(props, "\\qr");
311 strcat(props, "\\qc");
314 strcat(props, "\\qj");
319 if (fmt->dwMask & PFM_LINESPACING) {
320 /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
321 * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
322 switch (fmt->bLineSpacingRule) {
323 case 0: /* Single spacing */
324 strcat(props, "\\sl-240\\slmult1");
326 case 1: /* 1.5 spacing */
327 strcat(props, "\\sl-360\\slmult1");
329 case 2: /* Double spacing */
330 strcat(props, "\\sl-480\\slmult1");
333 sprintf(props + strlen(props), "\\sl%ld\\slmult0", fmt->dyLineSpacing);
336 sprintf(props + strlen(props), "\\sl-%ld\\slmult0", fmt->dyLineSpacing);
339 sprintf(props + strlen(props), "\\sl-%ld\\slmult1", fmt->dyLineSpacing * 240 / 20);
344 if (fmt->dwMask & PFM_DONOTHYPHEN && fmt->wEffects & PFE_DONOTHYPHEN)
345 strcat(props, "\\hyph0");
346 if (fmt->dwMask & PFM_KEEP && fmt->wEffects & PFE_KEEP)
347 strcat(props, "\\keep");
348 if (fmt->dwMask & PFM_KEEPNEXT && fmt->wEffects & PFE_KEEPNEXT)
349 strcat(props, "\\keepn");
350 if (fmt->dwMask & PFM_NOLINENUMBER && fmt->wEffects & PFE_NOLINENUMBER)
351 strcat(props, "\\noline");
352 if (fmt->dwMask & PFM_NOWIDOWCONTROL && fmt->wEffects & PFE_NOWIDOWCONTROL)
353 strcat(props, "\\nowidctlpar");
354 if (fmt->dwMask & PFM_PAGEBREAKBEFORE && fmt->wEffects & PFE_PAGEBREAKBEFORE)
355 strcat(props, "\\pagebb");
356 if (fmt->dwMask & PFM_RTLPARA && fmt->wEffects & PFE_RTLPARA)
357 strcat(props, "\\rtlpar");
358 if (fmt->dwMask & PFM_SIDEBYSIDE && fmt->wEffects & PFE_SIDEBYSIDE)
359 strcat(props, "\\sbys");
360 if (fmt->dwMask & PFM_TABLE && fmt->dwMask & PFE_TABLE)
361 strcat(props, "\\intbl");
363 if (fmt->dwMask & PFM_OFFSET)
364 sprintf(props + strlen(props), "\\li%ld", fmt->dxOffset);
365 if (fmt->dwMask & PFM_OFFSETINDENT || fmt->dwMask & PFM_STARTINDENT)
366 sprintf(props + strlen(props), "\\fi%ld", fmt->dxStartIndent);
367 if (fmt->dwMask & PFM_RIGHTINDENT)
368 sprintf(props + strlen(props), "\\ri%ld", fmt->dxRightIndent);
369 if (fmt->dwMask & PFM_SPACEAFTER)
370 sprintf(props + strlen(props), "\\sa%ld", fmt->dySpaceAfter);
371 if (fmt->dwMask & PFM_SPACEBEFORE)
372 sprintf(props + strlen(props), "\\sb%ld", fmt->dySpaceBefore);
373 if (fmt->dwMask & PFM_STYLE)
374 sprintf(props + strlen(props), "\\s%d", fmt->sStyle);
376 if (fmt->dwMask & PFM_TABSTOPS) {
377 static const char *leader[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
379 for (i = 0; i < fmt->cTabCount; i++) {
380 switch ((fmt->rgxTabs[i] >> 24) & 0xF) {
382 strcat(props, "\\tqc");
385 strcat(props, "\\tqr");
388 strcat(props, "\\tqdec");
391 /* Word bar tab (vertical bar). Handled below */
394 if (fmt->rgxTabs[i] >> 28 <= 5)
395 strcat(props, leader[fmt->rgxTabs[i] >> 28]);
396 sprintf(props+strlen(props), "\\tx%ld", fmt->rgxTabs[i]&0x00FFFFFF);
401 if (fmt->dwMask & PFM_SHADING) {
402 static const char *style[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
403 "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
404 "\\bghoriz", "\\bgvert", "\\bgfdiag",
405 "\\bgbdiag", "\\bgcross", "\\bgdcross",
407 if (fmt->wShadingWeight)
408 sprintf(props + strlen(props), "\\shading%d", fmt->wShadingWeight);
409 if (fmt->wShadingStyle & 0xF)
410 strcat(props, style[fmt->wShadingStyle & 0xF]);
411 sprintf(props + strlen(props), "\\cfpat%d\\cbpat%d",
412 (fmt->wShadingStyle >> 4) & 0xF, (fmt->wShadingStyle >> 8) & 0xF);
415 if (*props && !ME_StreamOutPrint(editor, props))
423 ME_StreamOutRTFCharProps(ME_TextEditor *editor, CHARFORMAT2W *fmt)
425 char props[STREAMOUT_BUFFER_SIZE] = "";
428 if (fmt->dwMask & CFM_ALLCAPS && fmt->dwEffects & CFE_ALLCAPS)
429 strcat(props, "\\caps");
430 if (fmt->dwMask & CFM_ANIMATION)
431 sprintf(props + strlen(props), "\\animtext%u", fmt->bAnimation);
432 if (fmt->dwMask & CFM_BACKCOLOR) {
433 if (!(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
434 for (i = 1; i < editor->pStream->nColorTblLen; i++)
435 if (editor->pStream->colortbl[i] == fmt->crBackColor) {
436 sprintf(props + strlen(props), "\\cb%u", i);
441 if (fmt->dwMask & CFM_BOLD && fmt->dwEffects & CFE_BOLD)
442 strcat(props, "\\b");
443 if (fmt->dwMask & CFM_COLOR) {
444 if (!(fmt->dwEffects & CFE_AUTOCOLOR)) {
445 for (i = 1; i < editor->pStream->nColorTblLen; i++)
446 if (editor->pStream->colortbl[i] == fmt->crTextColor) {
447 sprintf(props + strlen(props), "\\cf%u", i);
452 /* TODO: CFM_DISABLED */
453 if (fmt->dwMask & CFM_EMBOSS && fmt->dwEffects & CFE_EMBOSS)
454 strcat(props, "\\embo");
455 if (fmt->dwMask & CFM_HIDDEN && fmt->dwEffects & CFE_HIDDEN)
456 strcat(props, "\\v");
457 if (fmt->dwMask & CFM_IMPRINT && fmt->dwEffects & CFE_IMPRINT)
458 strcat(props, "\\impr");
459 if (fmt->dwMask & CFM_ITALIC && fmt->dwEffects & CFE_ITALIC)
460 strcat(props, "\\i");
461 if (fmt->dwMask & CFM_KERNING)
462 sprintf(props + strlen(props), "\\kerning%u", fmt->wKerning);
463 if (fmt->dwMask & CFM_LCID) {
464 /* TODO: handle SFF_PLAINRTF */
465 if (LOWORD(fmt->lcid) == 1024)
466 strcat(props, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
468 sprintf(props + strlen(props), "\\lang%u", LOWORD(fmt->lcid));
470 /* CFM_LINK is not streamed out by M$ */
471 if (fmt->dwMask & CFM_OFFSET) {
472 if (fmt->yOffset >= 0)
473 sprintf(props + strlen(props), "\\up%ld", fmt->yOffset);
475 sprintf(props + strlen(props), "\\dn%ld", -fmt->yOffset);
477 if (fmt->dwMask & CFM_OUTLINE && fmt->dwEffects & CFE_OUTLINE)
478 strcat(props, "\\outl");
479 if (fmt->dwMask & CFM_PROTECTED && fmt->dwEffects & CFE_PROTECTED)
480 strcat(props, "\\protect");
481 /* TODO: CFM_REVISED CFM_REVAUTHOR - probably using rsidtbl? */
482 if (fmt->dwMask & CFM_SHADOW && fmt->dwEffects & CFE_SHADOW)
483 strcat(props, "\\shad");
484 if (fmt->dwMask & CFM_SIZE)
485 sprintf(props + strlen(props), "\\fs%ld", fmt->yHeight / 10);
486 if (fmt->dwMask & CFM_SMALLCAPS && fmt->dwEffects & CFE_SMALLCAPS)
487 strcat(props, "\\scaps");
488 if (fmt->dwMask & CFM_SPACING)
489 sprintf(props + strlen(props), "\\expnd%u\\expndtw%u", fmt->sSpacing / 5, fmt->sSpacing);
490 if (fmt->dwMask & CFM_STRIKEOUT && fmt->dwEffects & CFE_STRIKEOUT)
491 strcat(props, "\\strike");
492 if (fmt->dwMask & CFM_STYLE) {
493 sprintf(props + strlen(props), "\\cs%u", fmt->sStyle);
494 /* TODO: emit style contents here */
496 if (fmt->dwMask & (CFM_SUBSCRIPT | CFM_SUPERSCRIPT)) {
497 if (fmt->dwEffects & CFE_SUBSCRIPT)
498 strcat(props, "\\sub");
499 else if (fmt->dwEffects & CFE_SUPERSCRIPT)
500 strcat(props, "\\super");
502 if (fmt->dwMask & CFM_UNDERLINE || fmt->dwMask & CFM_UNDERLINETYPE) {
503 if (fmt->dwMask & CFM_UNDERLINETYPE)
504 switch (fmt->bUnderlineType) {
505 case CFU_CF1UNDERLINE:
507 strcat(props, "\\ul");
509 case CFU_UNDERLINEDOTTED:
510 strcat(props, "\\uld");
512 case CFU_UNDERLINEDOUBLE:
513 strcat(props, "\\uldb");
515 case CFU_UNDERLINEWORD:
516 strcat(props, "\\ulw");
518 case CFU_UNDERLINENONE:
520 strcat(props, "\\ul0");
523 else if (fmt->dwEffects & CFE_UNDERLINE)
524 strcat(props, "\\ul");
526 /* FIXME: How to emit CFM_WEIGHT? */
528 if (fmt->dwMask & CFM_FACE || fmt->dwMask & CFM_CHARSET) {
531 if (fmt->dwMask & CFM_FACE)
532 szFaceName = fmt->szFaceName;
534 szFaceName = editor->pStream->fonttbl[0].szFaceName;
535 for (i = 0; i < editor->pStream->nFontTblLen; i++) {
536 if (szFaceName == editor->pStream->fonttbl[i].szFaceName
537 || !lstrcmpW(szFaceName, editor->pStream->fonttbl[i].szFaceName))
538 if (!(fmt->dwMask & CFM_CHARSET)
539 || fmt->bCharSet == editor->pStream->fonttbl[i].bCharSet)
542 if (i < editor->pStream->nFontTblLen)
544 if (i != editor->pStream->nDefaultFont)
545 sprintf(props + strlen(props), "\\f%u", i);
547 /* In UTF-8 mode, charsets/codepages are not used */
548 if (editor->pStream->nDefaultCodePage != CP_UTF8)
550 if (editor->pStream->fonttbl[i].bCharSet == DEFAULT_CHARSET)
551 editor->pStream->nCodePage = editor->pStream->nDefaultCodePage;
553 editor->pStream->nCodePage = RTFCharSetToCodePage(NULL,
554 editor->pStream->fonttbl[i].bCharSet);
560 if (!ME_StreamOutPrint(editor, props))
567 ME_StreamOutRTFText(ME_TextEditor *editor, WCHAR *text, LONG nChars)
569 char buffer[STREAMOUT_BUFFER_SIZE];
574 nChars = lstrlenW(text);
577 /* In UTF-8 mode, font charsets are not used. */
578 if (editor->pStream->nDefaultCodePage == CP_UTF8) {
579 /* 6 is the maximum character length in UTF-8 */
580 fit = min(nChars, STREAMOUT_BUFFER_SIZE / 6);
581 nBytes = WideCharToMultiByte(CP_UTF8, 0, text, fit, buffer,
582 STREAMOUT_BUFFER_SIZE, NULL, NULL);
585 for (i = 0; i < nBytes; i++)
586 if (buffer[i] == '{' || buffer[i] == '}' || buffer[i] == '\\') {
587 if (!ME_StreamOutPrint(editor, "%.*s\\", i - pos, buffer + pos))
592 if (!ME_StreamOutMove(editor, buffer + pos, nBytes - pos))
595 } else if (*text < 128) {
596 if (*text == '{' || *text == '}' || *text == '\\')
597 buffer[pos++] = '\\';
598 buffer[pos++] = (char)(*text++);
601 BOOL unknown = FALSE;
604 /* FIXME: In the MS docs for WideCharToMultiByte there is a big list of
605 * codepages including CP_SYMBOL for which the last parameter must be set
606 * to NULL for the function to succeed. But in Wine we need to care only
608 nBytes = WideCharToMultiByte(editor->pStream->nCodePage, 0, text, 1,
610 (editor->pStream->nCodePage == CP_SYMBOL) ? NULL : &unknown);
612 pos += sprintf(buffer + pos, "\\u%d?", (short)*text);
613 else if (*letter < 128) {
614 if (*letter == '{' || *letter == '}' || *letter == '\\')
615 buffer[pos++] = '\\';
616 buffer[pos++] = *letter;
618 for (i = 0; i < nBytes; i++)
619 pos += sprintf(buffer + pos, "\\'%02x", letter[i]);
624 if (pos >= STREAMOUT_BUFFER_SIZE - 11) {
625 if (!ME_StreamOutMove(editor, buffer, pos))
630 return ME_StreamOutMove(editor, buffer, pos);
635 ME_StreamOutRTF(ME_TextEditor *editor, int nStart, int nChars, int dwFormat)
637 ME_DisplayItem *p, *pEnd;
638 int nOffset, nEndLen;
639 ME_RunOfsFromCharOfs(editor, nStart, &p, &nOffset);
640 ME_RunOfsFromCharOfs(editor, nStart+nChars, &pEnd, &nEndLen);
642 if (!ME_StreamOutRTFHeader(editor, dwFormat))
645 if (!ME_StreamOutRTFFontAndColorTbl(editor, p, pEnd))
648 /* TODO: stylesheet table */
650 /* FIXME: maybe emit something smarter for the generator? */
651 if (!ME_StreamOutPrint(editor, "{\\*\\generator Wine Riched20 2.0.????;}"))
654 /* TODO: information group */
656 /* TODO: document formatting properties */
658 /* FIXME: We have only one document section */
660 /* TODO: section formatting properties */
662 if (!ME_StreamOutRTFParaProps(editor, ME_GetParagraph(p)))
670 if (!ME_StreamOutRTFParaProps(editor, p))
674 if (p == pEnd && !nEndLen)
676 TRACE("flags %xh\n", p->member.run.nFlags);
677 /* TODO: emit embedded objects */
678 if (p->member.run.nFlags & MERF_GRAPHICS)
679 FIXME("embedded objects are not handled\n");
680 if (p->member.run.nFlags & MERF_ENDPARA) {
681 if (!ME_StreamOutPrint(editor, "\r\n\\par"))
687 if (!ME_StreamOutPrint(editor, "{"))
689 TRACE("style %p\n", p->member.run.style);
690 if (!ME_StreamOutRTFCharProps(editor, &p->member.run.style->fmt))
693 nEnd = (p == pEnd) ? nEndLen : ME_StrLen(p->member.run.strText);
694 if (!ME_StreamOutRTFText(editor, p->member.run.strText->szData + nOffset, nEnd - nOffset))
697 if (!ME_StreamOutPrint(editor, "}"))
701 default: /* we missed the last item */
706 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
708 if (!ME_StreamOutPrint(editor, "}"))
715 ME_StreamOutText(ME_TextEditor *editor, int nStart, int nChars, DWORD dwFormat)
717 /* FIXME: use ME_RunOfsFromCharOfs */
718 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
720 UINT nCodePage = CP_ACP;
728 if (dwFormat & SF_USECODEPAGE)
729 nCodePage = HIWORD(dwFormat);
731 /* TODO: Handle SF_TEXTIZED */
733 while (success && nChars && item) {
734 nLen = ME_StrLen(item->member.run.strText) - nStart;
738 if (item->member.run.nFlags & MERF_ENDPARA) {
739 WCHAR szEOL[] = { '\r', '\n' };
741 if (dwFormat & SF_UNICODE)
742 success = ME_StreamOutMove(editor, (BYTE *)szEOL, 4);
744 success = ME_StreamOutMove(editor, "\r\n", 2);
746 if (dwFormat & SF_UNICODE)
747 success = ME_StreamOutMove(editor, (BYTE *)(item->member.run.strText->szData + nStart),
748 sizeof(WCHAR) * nLen);
752 nSize = WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
753 nLen, NULL, 0, NULL, NULL);
754 if (nSize > nBufLen) {
757 buffer = ALLOC_N_OBJ(BYTE, nSize);
760 WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
761 nLen, buffer, nSize, NULL, NULL);
762 success = ME_StreamOutMove(editor, buffer, nSize - 1);
768 item = ME_FindItemFwd(item, diRun);
778 ME_StreamOut(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream)
782 ME_StreamOutInit(editor, stream);
784 if (dwFormat & SFF_SELECTION)
785 ME_GetSelection(editor, &nStart, &nTo);
791 nTo = ME_GetTextLength(editor);
792 TRACE("from %d to %d\n", nStart, nTo);
794 if (dwFormat & SF_RTF)
795 ME_StreamOutRTF(editor, nStart, nTo - nStart, dwFormat);
796 else if (dwFormat & SF_TEXT || dwFormat & SF_TEXTIZED)
797 ME_StreamOutText(editor, nStart, nTo - nStart, dwFormat);
798 if (!editor->pStream->stream->dwError)
799 ME_StreamOutFlush(editor);
800 return ME_StreamOutFree(editor);