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->pos = 0;
37 editor->pStream->written = 0;
38 editor->pStream->nFontTblLen = 0;
39 editor->pStream->nColorTblLen = 1;
44 ME_StreamOutFlush(ME_TextEditor *editor)
49 EDITSTREAM *stream = editor->pStream->stream;
52 TRACE("sending %lu bytes\n", editor->pStream->pos - nStart);
53 /* Some apps seem not to set *pcb unless a problem arises, relying
54 on initial random nWritten value, which is usually >STREAMOUT_BUFFER_SIZE */
55 nRemaining = editor->pStream->pos - nStart;
56 nWritten = 0xDEADBEEF;
57 stream->dwError = stream->pfnCallback(stream->dwCookie, editor->pStream->buffer + nStart,
58 editor->pStream->pos - nStart, &nWritten);
59 TRACE("error=%lu written=%lu\n", stream->dwError, nWritten);
60 if (nWritten > (editor->pStream->pos - nStart) || nWritten<0) {
61 FIXME("Invalid returned written size *pcb: 0x%x (%ld) instead of %ld\n",
62 (unsigned)nWritten, nWritten, nRemaining);
63 nWritten = nRemaining;
65 if (nWritten == 0 || stream->dwError)
67 editor->pStream->written += nWritten;
69 } while (nStart < editor->pStream->pos);
70 editor->pStream->pos = 0;
76 ME_StreamOutFree(ME_TextEditor *editor)
78 LONG written = editor->pStream->written;
79 TRACE("total length = %lu\n", written);
81 FREE_OBJ(editor->pStream);
82 editor->pStream = NULL;
88 ME_StreamOutMove(ME_TextEditor *editor, BYTE *buffer, int len)
90 ME_OutStream *pStream = editor->pStream;
93 int space = STREAMOUT_BUFFER_SIZE - pStream->pos;
94 int fit = min(space, len);
96 TRACE("%u:%u:%.*s\n", pStream->pos, fit, fit, buffer);
97 memmove(pStream->buffer + pStream->pos, buffer, fit);
101 if (pStream->pos == STREAMOUT_BUFFER_SIZE) {
102 if (!ME_StreamOutFlush(editor))
111 ME_StreamOutPrint(ME_TextEditor *editor, char *format, ...)
113 char string[STREAMOUT_BUFFER_SIZE]; /* This is going to be enough */
117 va_start(valist, format);
118 len = vsnprintf(string, sizeof(string), format, valist);
121 return ME_StreamOutMove(editor, string, len);
126 ME_StreamOutRTFHeader(ME_TextEditor *editor, int dwFormat)
128 char *cCharSet = NULL;
133 if (dwFormat & SF_USECODEPAGE) {
136 switch (HIWORD(dwFormat)) {
139 nCodePage = GetACP();
142 nCodePage = GetOEMCP();
143 if (nCodePage == 437)
145 else if (nCodePage == 850)
154 if (HIWORD(dwFormat) == CP_MACCP) {
156 nCodePage = 10000; /* MacRoman */
159 nCodePage = 1252; /* Latin-1 */
161 if (GetCPInfoExW(HIWORD(dwFormat), 0, &info))
162 nCodePage = info.CodePage;
166 /* TODO: If the original document contained an \ansicpg value, retain it.
167 * Otherwise, M$ richedit emits a codepage number determined from the
168 * charset of the default font here. Anyway, this value is not used by
170 nCodePage = GetACP();
172 if (nCodePage == CP_UTF8)
173 success = ME_StreamOutPrint(editor, "{\\urtf");
175 success = ME_StreamOutPrint(editor, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet, nCodePage);
180 editor->pStream->nDefaultCodePage = nCodePage;
182 /* FIXME: This should be a document property */
183 /* TODO: handle SFF_PLAINRTF */
184 language = GetUserDefaultLangID();
185 if (!ME_StreamOutPrint(editor, "\\deff0\\deflang%u\\deflangfe%u", language, language))
188 /* FIXME: This should be a document property */
189 editor->pStream->nDefaultFont = 0;
196 ME_StreamOutRTFFontAndColorTbl(ME_TextEditor *editor, ME_DisplayItem *pFirstRun, ME_DisplayItem *pLastRun)
198 ME_DisplayItem *item = pFirstRun;
199 ME_FontTableItem *table = editor->pStream->fonttbl;
203 CHARFORMAT2W *fmt = &item->member.run.style->fmt;
206 if (fmt->dwMask & CFM_FACE) {
207 WCHAR *face = fmt->szFaceName;
208 BYTE bCharSet = (fmt->dwMask & CFM_CHARSET) ? fmt->bCharSet : DEFAULT_CHARSET;
210 for (i = 0; i < editor->pStream->nFontTblLen; i++)
211 if (table[i].bCharSet == bCharSet
212 && (table[i].szFaceName == face || !lstrcmpW(table[i].szFaceName, face)))
214 if (i == editor->pStream->nFontTblLen) {
215 table[i].bCharSet = bCharSet;
216 table[i].szFaceName = face;
217 editor->pStream->nFontTblLen++;
221 if (fmt->dwMask & CFM_COLOR && !(fmt->dwEffects & CFE_AUTOCOLOR)) {
222 crColor = fmt->crTextColor;
223 for (i = 1; i < editor->pStream->nColorTblLen; i++)
224 if (editor->pStream->colortbl[i] == crColor)
226 if (i == editor->pStream->nColorTblLen) {
227 editor->pStream->colortbl[i] = crColor;
228 editor->pStream->nColorTblLen++;
231 if (fmt->dwMask & CFM_BACKCOLOR && !(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
232 crColor = fmt->crBackColor;
233 for (i = 1; i < editor->pStream->nColorTblLen; i++)
234 if (editor->pStream->colortbl[i] == crColor)
236 if (i == editor->pStream->nColorTblLen) {
237 editor->pStream->colortbl[i] = crColor;
238 editor->pStream->nColorTblLen++;
242 if (item == pLastRun)
244 item = ME_FindItemFwd(item, diRun);
247 if (!ME_StreamOutPrint(editor, "{\\fonttbl"))
250 for (i = 0; i < editor->pStream->nFontTblLen; i++) {
251 if (table[i].bCharSet != DEFAULT_CHARSET) {
252 if (!ME_StreamOutPrint(editor, "{\\f%u\\fcharset%u ", i, table[i].bCharSet))
255 if (!ME_StreamOutPrint(editor, "{\\f%u ", i))
258 if (!ME_StreamOutRTFText(editor, table[i].szFaceName, -1))
260 if (!ME_StreamOutPrint(editor, ";}\r\n"))
263 if (!ME_StreamOutPrint(editor, "}"))
266 /* Output colors table if not empty */
267 if (editor->pStream->nColorTblLen > 1) {
268 if (!ME_StreamOutPrint(editor, "{\\colortbl;"))
270 for (i = 1; i < editor->pStream->nColorTblLen; i++) {
271 if (!ME_StreamOutPrint(editor, "\\red%u\\green%u\\blue%u;",
272 editor->pStream->colortbl[i] & 0xFF,
273 (editor->pStream->colortbl[i] >> 8) & 0xFF,
274 (editor->pStream->colortbl[i] >> 16) & 0xFF))
277 if (!ME_StreamOutPrint(editor, "}"))
286 ME_StreamOutRTFParaProps(ME_TextEditor *editor, ME_DisplayItem *para)
288 PARAFORMAT2 *fmt = para->member.para.pFmt;
289 char props[STREAMOUT_BUFFER_SIZE] = "";
292 /* TODO: Don't emit anything if the last PARAFORMAT2 is inherited */
293 if (!ME_StreamOutPrint(editor, "\\pard"))
296 /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
297 * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
298 * set very different from the documentation.
299 * (Tested with RichEdit 5.50.25.0601) */
301 if (fmt->dwMask & PFM_ALIGNMENT) {
302 switch (fmt->wAlignment) {
304 /* Default alignment: not emitted */
307 strcat(props, "\\qr");
310 strcat(props, "\\qc");
313 strcat(props, "\\qj");
318 if (fmt->dwMask & PFM_LINESPACING) {
319 /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
320 * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
321 switch (fmt->bLineSpacingRule) {
322 case 0: /* Single spacing */
323 strcat(props, "\\sl-240\\slmult1");
325 case 1: /* 1.5 spacing */
326 strcat(props, "\\sl-360\\slmult1");
328 case 2: /* Double spacing */
329 strcat(props, "\\sl-480\\slmult1");
332 sprintf(props + strlen(props), "\\sl%ld\\slmult0", fmt->dyLineSpacing);
335 sprintf(props + strlen(props), "\\sl-%ld\\slmult0", fmt->dyLineSpacing);
338 sprintf(props + strlen(props), "\\sl-%ld\\slmult1", fmt->dyLineSpacing * 240 / 20);
343 if (fmt->dwMask & PFM_DONOTHYPHEN && fmt->wEffects & PFE_DONOTHYPHEN)
344 strcat(props, "\\hyph0");
345 if (fmt->dwMask & PFM_KEEP && fmt->wEffects & PFE_KEEP)
346 strcat(props, "\\keep");
347 if (fmt->dwMask & PFM_KEEPNEXT && fmt->wEffects & PFE_KEEPNEXT)
348 strcat(props, "\\keepn");
349 if (fmt->dwMask & PFM_NOLINENUMBER && fmt->wEffects & PFE_NOLINENUMBER)
350 strcat(props, "\\noline");
351 if (fmt->dwMask & PFM_NOWIDOWCONTROL && fmt->wEffects & PFE_NOWIDOWCONTROL)
352 strcat(props, "\\nowidctlpar");
353 if (fmt->dwMask & PFM_PAGEBREAKBEFORE && fmt->wEffects & PFE_PAGEBREAKBEFORE)
354 strcat(props, "\\pagebb");
355 if (fmt->dwMask & PFM_RTLPARA && fmt->wEffects & PFE_RTLPARA)
356 strcat(props, "\\rtlpar");
357 if (fmt->dwMask & PFM_SIDEBYSIDE && fmt->wEffects & PFE_SIDEBYSIDE)
358 strcat(props, "\\sbys");
359 if (fmt->dwMask & PFM_TABLE && fmt->dwMask & PFE_TABLE)
360 strcat(props, "\\intbl");
362 if (fmt->dwMask & PFM_OFFSET)
363 sprintf(props + strlen(props), "\\li%ld", fmt->dxOffset);
364 if (fmt->dwMask & PFM_OFFSETINDENT || fmt->dwMask & PFM_STARTINDENT)
365 sprintf(props + strlen(props), "\\fi%ld", fmt->dxStartIndent);
366 if (fmt->dwMask & PFM_RIGHTINDENT)
367 sprintf(props + strlen(props), "\\ri%ld", fmt->dxRightIndent);
368 if (fmt->dwMask & PFM_SPACEAFTER)
369 sprintf(props + strlen(props), "\\sa%ld", fmt->dySpaceAfter);
370 if (fmt->dwMask & PFM_SPACEBEFORE)
371 sprintf(props + strlen(props), "\\sb%ld", fmt->dySpaceBefore);
372 if (fmt->dwMask & PFM_STYLE)
373 sprintf(props + strlen(props), "\\s%d", fmt->sStyle);
375 if (fmt->dwMask & PFM_TABSTOPS) {
376 static const char *leader[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
378 for (i = 0; i < fmt->cTabCount; i++) {
379 switch ((fmt->rgxTabs[i] >> 24) & 0xF) {
381 strcat(props, "\\tqc");
384 strcat(props, "\\tqr");
387 strcat(props, "\\tqdec");
390 /* Word bar tab (vertical bar). Handled below */
393 if (fmt->rgxTabs[i] >> 28 <= 5)
394 strcat(props, leader[fmt->rgxTabs[i] >> 28]);
395 sprintf(props+strlen(props), "\\tx%ld", fmt->rgxTabs[i]&0x00FFFFFF);
400 if (fmt->dwMask & PFM_SHADING) {
401 static const char *style[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
402 "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
403 "\\bghoriz", "\\bgvert", "\\bgfdiag",
404 "\\bgbdiag", "\\bgcross", "\\bgdcross",
406 if (fmt->wShadingWeight)
407 sprintf(props + strlen(props), "\\shading%d", fmt->wShadingWeight);
408 if (fmt->wShadingStyle & 0xF)
409 strcat(props, style[fmt->wShadingStyle & 0xF]);
410 sprintf(props + strlen(props), "\\cfpat%d\\cbpat%d",
411 (fmt->wShadingStyle >> 4) & 0xF, (fmt->wShadingStyle >> 8) & 0xF);
414 if (*props && !ME_StreamOutPrint(editor, props))
422 ME_StreamOutRTFCharProps(ME_TextEditor *editor, CHARFORMAT2W *fmt)
424 char props[STREAMOUT_BUFFER_SIZE] = "";
427 if (fmt->dwMask & CFM_ALLCAPS && fmt->dwEffects & CFE_ALLCAPS)
428 strcat(props, "\\caps");
429 if (fmt->dwMask & CFM_ANIMATION)
430 sprintf(props + strlen(props), "\\animtext%u", fmt->bAnimation);
431 if (fmt->dwMask & CFM_BACKCOLOR) {
432 if (!(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
433 for (i = 1; i < editor->pStream->nColorTblLen; i++)
434 if (editor->pStream->colortbl[i] == fmt->crBackColor) {
435 sprintf(props + strlen(props), "\\cb%u", i);
440 if (fmt->dwMask & CFM_BOLD && fmt->dwEffects & CFE_BOLD)
441 strcat(props, "\\b");
442 if (fmt->dwMask & CFM_COLOR) {
443 if (!(fmt->dwEffects & CFE_AUTOCOLOR)) {
444 for (i = 1; i < editor->pStream->nColorTblLen; i++)
445 if (editor->pStream->colortbl[i] == fmt->crTextColor) {
446 sprintf(props + strlen(props), "\\cf%u", i);
451 /* TODO: CFM_DISABLED */
452 if (fmt->dwMask & CFM_EMBOSS && fmt->dwEffects & CFE_EMBOSS)
453 strcat(props, "\\embo");
454 if (fmt->dwMask & CFM_HIDDEN && fmt->dwEffects & CFE_HIDDEN)
455 strcat(props, "\\v");
456 if (fmt->dwMask & CFM_IMPRINT && fmt->dwEffects & CFE_IMPRINT)
457 strcat(props, "\\impr");
458 if (fmt->dwMask & CFM_ITALIC && fmt->dwEffects & CFE_ITALIC)
459 strcat(props, "\\i");
460 if (fmt->dwMask & CFM_KERNING)
461 sprintf(props + strlen(props), "\\kerning%u", fmt->wKerning);
462 if (fmt->dwMask & CFM_LCID) {
463 /* TODO: handle SFF_PLAINRTF */
464 if (LOWORD(fmt->lcid) == 1024)
465 strcat(props, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
467 sprintf(props + strlen(props), "\\lang%u", LOWORD(fmt->lcid));
469 /* CFM_LINK is not streamed out by M$ */
470 if (fmt->dwMask & CFM_OFFSET) {
471 if (fmt->yOffset >= 0)
472 sprintf(props + strlen(props), "\\up%ld", fmt->yOffset);
474 sprintf(props + strlen(props), "\\dn%ld", -fmt->yOffset);
476 if (fmt->dwMask & CFM_OUTLINE && fmt->dwEffects & CFE_OUTLINE)
477 strcat(props, "\\outl");
478 if (fmt->dwMask & CFM_PROTECTED && fmt->dwEffects & CFE_PROTECTED)
479 strcat(props, "\\protect");
480 /* TODO: CFM_REVISED CFM_REVAUTHOR - probably using rsidtbl? */
481 if (fmt->dwMask & CFM_SHADOW && fmt->dwEffects & CFE_SHADOW)
482 strcat(props, "\\shad");
483 if (fmt->dwMask & CFM_SIZE)
484 sprintf(props + strlen(props), "\\fs%ld", fmt->yHeight / 10);
485 if (fmt->dwMask & CFM_SMALLCAPS && fmt->dwEffects & CFE_SMALLCAPS)
486 strcat(props, "\\scaps");
487 if (fmt->dwMask & CFM_SPACING)
488 sprintf(props + strlen(props), "\\expnd%u\\expndtw%u", fmt->sSpacing / 5, fmt->sSpacing);
489 if (fmt->dwMask & CFM_STRIKEOUT && fmt->dwEffects & CFE_STRIKEOUT)
490 strcat(props, "\\strike");
491 if (fmt->dwMask & CFM_STYLE) {
492 sprintf(props + strlen(props), "\\cs%u", fmt->sStyle);
493 /* TODO: emit style contents here */
495 if (fmt->dwMask & (CFM_SUBSCRIPT | CFM_SUPERSCRIPT)) {
496 if (fmt->dwEffects & CFE_SUBSCRIPT)
497 strcat(props, "\\sub");
498 else if (fmt->dwEffects & CFE_SUPERSCRIPT)
499 strcat(props, "\\super");
501 if (fmt->dwMask & CFM_UNDERLINE || fmt->dwMask & CFM_UNDERLINETYPE) {
502 if (fmt->dwMask & CFM_UNDERLINETYPE)
503 switch (fmt->bUnderlineType) {
504 case CFU_CF1UNDERLINE:
506 strcat(props, "\\ul");
508 case CFU_UNDERLINEDOTTED:
509 strcat(props, "\\uld");
511 case CFU_UNDERLINEDOUBLE:
512 strcat(props, "\\uldb");
514 case CFU_UNDERLINEWORD:
515 strcat(props, "\\ulw");
517 case CFU_UNDERLINENONE:
519 strcat(props, "\\ul0");
522 else if (fmt->dwEffects & CFE_UNDERLINE)
523 strcat(props, "\\ul");
525 /* FIXME: How to emit CFM_WEIGHT? */
527 if (fmt->dwMask & CFM_FACE || fmt->dwMask & CFM_CHARSET) {
530 if (fmt->dwMask & CFM_FACE)
531 szFaceName = fmt->szFaceName;
533 szFaceName = editor->pStream->fonttbl[0].szFaceName;
534 for (i = 0; i < editor->pStream->nFontTblLen; i++) {
535 if (szFaceName == editor->pStream->fonttbl[i].szFaceName
536 || !lstrcmpW(szFaceName, editor->pStream->fonttbl[i].szFaceName))
537 if (!(fmt->dwMask & CFM_CHARSET)
538 || fmt->bCharSet == editor->pStream->fonttbl[i].bCharSet)
541 if (i < editor->pStream->nFontTblLen)
543 if (i != editor->pStream->nDefaultFont)
544 sprintf(props + strlen(props), "\\f%u", i);
546 /* In UTF-8 mode, charsets/codepages are not used */
547 if (editor->pStream->nDefaultCodePage != CP_UTF8)
549 if (editor->pStream->fonttbl[i].bCharSet == DEFAULT_CHARSET)
550 editor->pStream->nCodePage = editor->pStream->nDefaultCodePage;
552 editor->pStream->nCodePage = RTFCharSetToCodePage(NULL,
553 editor->pStream->fonttbl[i].bCharSet);
559 if (!ME_StreamOutPrint(editor, props))
566 ME_StreamOutRTFText(ME_TextEditor *editor, WCHAR *text, LONG nChars)
568 char buffer[STREAMOUT_BUFFER_SIZE];
573 nChars = lstrlenW(text);
576 /* In UTF-8 mode, font charsets are not used. */
577 if (editor->pStream->nDefaultCodePage == CP_UTF8) {
578 /* 6 is the maximum character length in UTF-8 */
579 fit = min(nChars, STREAMOUT_BUFFER_SIZE / 6);
580 WideCharToMultiByte(CP_UTF8, 0, text, fit, buffer, STREAMOUT_BUFFER_SIZE,
584 for (i = 0; buffer[i]; i++)
585 if (buffer[i] == '{' || buffer[i] == '}' || buffer[i] == '\\') {
586 if (!ME_StreamOutPrint(editor, "%.*s\\", i - pos, buffer + pos))
591 if (!ME_StreamOutPrint(editor, "%s", buffer + pos))
594 } else if (*text < 128) {
595 if (*text == '{' || *text == '}' || *text == '\\')
596 buffer[pos++] = '\\';
597 buffer[pos++] = (char)(*text++);
600 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 || dwFormat & SF_RTFNOOBJS)
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);