push 63c1876572cbb61a874995ad42ef27c14590d232
[wine/hacks.git] / dlls / riched20 / writer.c
blob13afb3cc4fdd9d67b99def1b1ab5f50e0300bdd0
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 return pStream;
48 static BOOL
49 ME_StreamOutFlush(ME_OutStream *pStream)
51 LONG nStart = 0;
52 LONG nWritten = 0;
53 LONG nRemaining = 0;
54 EDITSTREAM *stream = pStream->stream;
56 do {
57 TRACE("sending %u bytes\n", pStream->pos - nStart);
58 /* Some apps seem not to set *pcb unless a problem arises, relying
59 on initial random nWritten value, which is usually >STREAMOUT_BUFFER_SIZE */
60 nRemaining = pStream->pos - nStart;
61 nWritten = 0xDEADBEEF;
62 stream->dwError = stream->pfnCallback(stream->dwCookie, (LPBYTE)pStream->buffer + nStart,
63 pStream->pos - nStart, &nWritten);
64 TRACE("error=%u written=%u\n", stream->dwError, nWritten);
65 if (nWritten > (pStream->pos - nStart) || nWritten<0) {
66 FIXME("Invalid returned written size *pcb: 0x%x (%d) instead of %d\n",
67 (unsigned)nWritten, nWritten, nRemaining);
68 nWritten = nRemaining;
70 if (nWritten == 0 || stream->dwError)
71 return FALSE;
72 pStream->written += nWritten;
73 nStart += nWritten;
74 } while (nStart < pStream->pos);
75 pStream->pos = 0;
76 return TRUE;
80 static LONG
81 ME_StreamOutFree(ME_OutStream *pStream)
83 LONG written = pStream->written;
84 TRACE("total length = %u\n", written);
86 FREE_OBJ(pStream);
87 return written;
91 static BOOL
92 ME_StreamOutMove(ME_OutStream *pStream, const char *buffer, int len)
94 while (len) {
95 int space = STREAMOUT_BUFFER_SIZE - pStream->pos;
96 int fit = min(space, len);
98 TRACE("%u:%u:%s\n", pStream->pos, fit, debugstr_an(buffer,fit));
99 memmove(pStream->buffer + pStream->pos, buffer, fit);
100 len -= fit;
101 buffer += fit;
102 pStream->pos += fit;
103 if (pStream->pos == STREAMOUT_BUFFER_SIZE) {
104 if (!ME_StreamOutFlush(pStream))
105 return FALSE;
108 return TRUE;
112 static BOOL
113 ME_StreamOutPrint(ME_OutStream *pStream, const char *format, ...)
115 char string[STREAMOUT_BUFFER_SIZE]; /* This is going to be enough */
116 int len;
117 va_list valist;
119 va_start(valist, format);
120 len = vsnprintf(string, sizeof(string), format, valist);
121 va_end(valist);
123 return ME_StreamOutMove(pStream, string, len);
127 static BOOL
128 ME_StreamOutRTFHeader(ME_OutStream *pStream, int dwFormat)
130 const char *cCharSet = NULL;
131 UINT nCodePage;
132 LANGID language;
133 BOOL success;
135 if (dwFormat & SF_USECODEPAGE) {
136 CPINFOEXW info;
138 switch (HIWORD(dwFormat)) {
139 case CP_ACP:
140 cCharSet = "ansi";
141 nCodePage = GetACP();
142 break;
143 case CP_OEMCP:
144 nCodePage = GetOEMCP();
145 if (nCodePage == 437)
146 cCharSet = "pc";
147 else if (nCodePage == 850)
148 cCharSet = "pca";
149 else
150 cCharSet = "ansi";
151 break;
152 case CP_UTF8:
153 nCodePage = CP_UTF8;
154 break;
155 default:
156 if (HIWORD(dwFormat) == CP_MACCP) {
157 cCharSet = "mac";
158 nCodePage = 10000; /* MacRoman */
159 } else {
160 cCharSet = "ansi";
161 nCodePage = 1252; /* Latin-1 */
163 if (GetCPInfoExW(HIWORD(dwFormat), 0, &info))
164 nCodePage = info.CodePage;
166 } else {
167 cCharSet = "ansi";
168 /* TODO: If the original document contained an \ansicpg value, retain it.
169 * Otherwise, M$ richedit emits a codepage number determined from the
170 * charset of the default font here. Anyway, this value is not used by
171 * the reader... */
172 nCodePage = GetACP();
174 if (nCodePage == CP_UTF8)
175 success = ME_StreamOutPrint(pStream, "{\\urtf");
176 else
177 success = ME_StreamOutPrint(pStream, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet, nCodePage);
179 if (!success)
180 return FALSE;
182 pStream->nDefaultCodePage = nCodePage;
184 /* FIXME: This should be a document property */
185 /* TODO: handle SFF_PLAINRTF */
186 language = GetUserDefaultLangID();
187 if (!ME_StreamOutPrint(pStream, "\\deff0\\deflang%u\\deflangfe%u", language, language))
188 return FALSE;
190 /* FIXME: This should be a document property */
191 pStream->nDefaultFont = 0;
193 return TRUE;
197 static BOOL
198 ME_StreamOutRTFFontAndColorTbl(ME_OutStream *pStream, ME_DisplayItem *pFirstRun, const ME_DisplayItem *pLastRun)
200 ME_DisplayItem *item = pFirstRun;
201 ME_FontTableItem *table = pStream->fonttbl;
202 int i;
204 do {
205 CHARFORMAT2W *fmt = &item->member.run.style->fmt;
206 COLORREF crColor;
208 if (fmt->dwMask & CFM_FACE) {
209 WCHAR *face = fmt->szFaceName;
210 BYTE bCharSet = (fmt->dwMask & CFM_CHARSET) ? fmt->bCharSet : DEFAULT_CHARSET;
212 for (i = 0; i < pStream->nFontTblLen; i++)
213 if (table[i].bCharSet == bCharSet
214 && (table[i].szFaceName == face || !lstrcmpW(table[i].szFaceName, face)))
215 break;
216 if (i == pStream->nFontTblLen) {
217 table[i].bCharSet = bCharSet;
218 table[i].szFaceName = face;
219 pStream->nFontTblLen++;
223 if (fmt->dwMask & CFM_COLOR && !(fmt->dwEffects & CFE_AUTOCOLOR)) {
224 crColor = fmt->crTextColor;
225 for (i = 1; i < pStream->nColorTblLen; i++)
226 if (pStream->colortbl[i] == crColor)
227 break;
228 if (i == pStream->nColorTblLen) {
229 pStream->colortbl[i] = crColor;
230 pStream->nColorTblLen++;
233 if (fmt->dwMask & CFM_BACKCOLOR && !(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
234 crColor = fmt->crBackColor;
235 for (i = 1; i < pStream->nColorTblLen; i++)
236 if (pStream->colortbl[i] == crColor)
237 break;
238 if (i == pStream->nColorTblLen) {
239 pStream->colortbl[i] = crColor;
240 pStream->nColorTblLen++;
244 if (item == pLastRun)
245 break;
246 item = ME_FindItemFwd(item, diRun);
247 } while (item);
249 if (!ME_StreamOutPrint(pStream, "{\\fonttbl"))
250 return FALSE;
252 for (i = 0; i < pStream->nFontTblLen; i++) {
253 if (table[i].bCharSet != DEFAULT_CHARSET) {
254 if (!ME_StreamOutPrint(pStream, "{\\f%u\\fcharset%u ", i, table[i].bCharSet))
255 return FALSE;
256 } else {
257 if (!ME_StreamOutPrint(pStream, "{\\f%u ", i))
258 return FALSE;
260 if (!ME_StreamOutRTFText(pStream, table[i].szFaceName, -1))
261 return FALSE;
262 if (!ME_StreamOutPrint(pStream, ";}\r\n"))
263 return FALSE;
265 if (!ME_StreamOutPrint(pStream, "}"))
266 return FALSE;
268 /* Output colors table if not empty */
269 if (pStream->nColorTblLen > 1) {
270 if (!ME_StreamOutPrint(pStream, "{\\colortbl;"))
271 return FALSE;
272 for (i = 1; i < pStream->nColorTblLen; i++) {
273 if (!ME_StreamOutPrint(pStream, "\\red%u\\green%u\\blue%u;",
274 pStream->colortbl[i] & 0xFF,
275 (pStream->colortbl[i] >> 8) & 0xFF,
276 (pStream->colortbl[i] >> 16) & 0xFF))
277 return FALSE;
279 if (!ME_StreamOutPrint(pStream, "}"))
280 return FALSE;
283 return TRUE;
286 static BOOL
287 ME_StreamOutRTFTableProps(ME_OutStream *pStream, const ME_DisplayItem *para)
289 PARAFORMAT2 *pFmt;
290 char props[STREAMOUT_BUFFER_SIZE] = "";
291 int i;
293 if (!ME_StreamOutPrint(pStream, "\\trowd"))
294 return FALSE;
295 pFmt = para->member.para.pFmt;
297 for (i = 0; i < pFmt->cTabCount; i++)
299 sprintf(props, "\\cellx%d", pFmt->rgxTabs[i] & 0x00FFFFFF);
300 if (!ME_StreamOutPrint(pStream, props))
301 return FALSE;
303 props[0] = '\0';
304 return TRUE;
307 static BOOL
308 ME_StreamOutRTFParaProps(ME_OutStream *pStream, const ME_DisplayItem *para)
310 PARAFORMAT2 *fmt = para->member.para.pFmt;
311 char props[STREAMOUT_BUFFER_SIZE] = "";
312 int i;
314 /* TODO: Don't emit anything if the last PARAFORMAT2 is inherited */
315 if (!ME_StreamOutPrint(pStream, "\\pard"))
316 return FALSE;
318 if (fmt->dwMask & PFM_TABLE && fmt->wEffects & PFE_TABLE)
319 strcat(props, "\\intbl");
321 /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
322 * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
323 * set very different from the documentation.
324 * (Tested with RichEdit 5.50.25.0601) */
326 if (fmt->dwMask & PFM_ALIGNMENT) {
327 switch (fmt->wAlignment) {
328 case PFA_LEFT:
329 /* Default alignment: not emitted */
330 break;
331 case PFA_RIGHT:
332 strcat(props, "\\qr");
333 break;
334 case PFA_CENTER:
335 strcat(props, "\\qc");
336 break;
337 case PFA_JUSTIFY:
338 strcat(props, "\\qj");
339 break;
343 if (fmt->dwMask & PFM_LINESPACING) {
344 /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
345 * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
346 switch (fmt->bLineSpacingRule) {
347 case 0: /* Single spacing */
348 strcat(props, "\\sl-240\\slmult1");
349 break;
350 case 1: /* 1.5 spacing */
351 strcat(props, "\\sl-360\\slmult1");
352 break;
353 case 2: /* Double spacing */
354 strcat(props, "\\sl-480\\slmult1");
355 break;
356 case 3:
357 sprintf(props + strlen(props), "\\sl%d\\slmult0", fmt->dyLineSpacing);
358 break;
359 case 4:
360 sprintf(props + strlen(props), "\\sl-%d\\slmult0", fmt->dyLineSpacing);
361 break;
362 case 5:
363 sprintf(props + strlen(props), "\\sl-%d\\slmult1", fmt->dyLineSpacing * 240 / 20);
364 break;
368 if (fmt->dwMask & PFM_DONOTHYPHEN && fmt->wEffects & PFE_DONOTHYPHEN)
369 strcat(props, "\\hyph0");
370 if (fmt->dwMask & PFM_KEEP && fmt->wEffects & PFE_KEEP)
371 strcat(props, "\\keep");
372 if (fmt->dwMask & PFM_KEEPNEXT && fmt->wEffects & PFE_KEEPNEXT)
373 strcat(props, "\\keepn");
374 if (fmt->dwMask & PFM_NOLINENUMBER && fmt->wEffects & PFE_NOLINENUMBER)
375 strcat(props, "\\noline");
376 if (fmt->dwMask & PFM_NOWIDOWCONTROL && fmt->wEffects & PFE_NOWIDOWCONTROL)
377 strcat(props, "\\nowidctlpar");
378 if (fmt->dwMask & PFM_PAGEBREAKBEFORE && fmt->wEffects & PFE_PAGEBREAKBEFORE)
379 strcat(props, "\\pagebb");
380 if (fmt->dwMask & PFM_RTLPARA && fmt->wEffects & PFE_RTLPARA)
381 strcat(props, "\\rtlpar");
382 if (fmt->dwMask & PFM_SIDEBYSIDE && fmt->wEffects & PFE_SIDEBYSIDE)
383 strcat(props, "\\sbys");
385 if (fmt->dwMask & PFM_OFFSET)
386 sprintf(props + strlen(props), "\\li%d", fmt->dxOffset);
387 if (fmt->dwMask & PFM_OFFSETINDENT || fmt->dwMask & PFM_STARTINDENT)
388 sprintf(props + strlen(props), "\\fi%d", fmt->dxStartIndent);
389 if (fmt->dwMask & PFM_RIGHTINDENT)
390 sprintf(props + strlen(props), "\\ri%d", fmt->dxRightIndent);
391 if (fmt->dwMask & PFM_SPACEAFTER)
392 sprintf(props + strlen(props), "\\sa%d", fmt->dySpaceAfter);
393 if (fmt->dwMask & PFM_SPACEBEFORE)
394 sprintf(props + strlen(props), "\\sb%d", fmt->dySpaceBefore);
395 if (fmt->dwMask & PFM_STYLE)
396 sprintf(props + strlen(props), "\\s%d", fmt->sStyle);
398 if (fmt->dwMask & PFM_TABSTOPS) {
399 static const char * const leader[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
401 for (i = 0; i < fmt->cTabCount; i++) {
402 switch ((fmt->rgxTabs[i] >> 24) & 0xF) {
403 case 1:
404 strcat(props, "\\tqc");
405 break;
406 case 2:
407 strcat(props, "\\tqr");
408 break;
409 case 3:
410 strcat(props, "\\tqdec");
411 break;
412 case 4:
413 /* Word bar tab (vertical bar). Handled below */
414 break;
416 if (fmt->rgxTabs[i] >> 28 <= 5)
417 strcat(props, leader[fmt->rgxTabs[i] >> 28]);
418 sprintf(props+strlen(props), "\\tx%d", fmt->rgxTabs[i]&0x00FFFFFF);
423 if (fmt->dwMask & PFM_SHADING) {
424 static const char * const style[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
425 "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
426 "\\bghoriz", "\\bgvert", "\\bgfdiag",
427 "\\bgbdiag", "\\bgcross", "\\bgdcross",
428 "", "", "" };
429 if (fmt->wShadingWeight)
430 sprintf(props + strlen(props), "\\shading%d", fmt->wShadingWeight);
431 if (fmt->wShadingStyle & 0xF)
432 strcat(props, style[fmt->wShadingStyle & 0xF]);
433 sprintf(props + strlen(props), "\\cfpat%d\\cbpat%d",
434 (fmt->wShadingStyle >> 4) & 0xF, (fmt->wShadingStyle >> 8) & 0xF);
437 if (*props && !ME_StreamOutPrint(pStream, props))
438 return FALSE;
440 return TRUE;
444 static BOOL
445 ME_StreamOutRTFCharProps(ME_OutStream *pStream, CHARFORMAT2W *fmt)
447 char props[STREAMOUT_BUFFER_SIZE] = "";
448 int i;
450 if (fmt->dwMask & CFM_ALLCAPS && fmt->dwEffects & CFE_ALLCAPS)
451 strcat(props, "\\caps");
452 if (fmt->dwMask & CFM_ANIMATION)
453 sprintf(props + strlen(props), "\\animtext%u", fmt->bAnimation);
454 if (fmt->dwMask & CFM_BACKCOLOR) {
455 if (!(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
456 for (i = 1; i < pStream->nColorTblLen; i++)
457 if (pStream->colortbl[i] == fmt->crBackColor) {
458 sprintf(props + strlen(props), "\\cb%u", i);
459 break;
463 if (fmt->dwMask & CFM_BOLD && fmt->dwEffects & CFE_BOLD)
464 strcat(props, "\\b");
465 if (fmt->dwMask & CFM_COLOR) {
466 if (!(fmt->dwEffects & CFE_AUTOCOLOR)) {
467 for (i = 1; i < pStream->nColorTblLen; i++)
468 if (pStream->colortbl[i] == fmt->crTextColor) {
469 sprintf(props + strlen(props), "\\cf%u", i);
470 break;
474 /* TODO: CFM_DISABLED */
475 if (fmt->dwMask & CFM_EMBOSS && fmt->dwEffects & CFE_EMBOSS)
476 strcat(props, "\\embo");
477 if (fmt->dwMask & CFM_HIDDEN && fmt->dwEffects & CFE_HIDDEN)
478 strcat(props, "\\v");
479 if (fmt->dwMask & CFM_IMPRINT && fmt->dwEffects & CFE_IMPRINT)
480 strcat(props, "\\impr");
481 if (fmt->dwMask & CFM_ITALIC && fmt->dwEffects & CFE_ITALIC)
482 strcat(props, "\\i");
483 if (fmt->dwMask & CFM_KERNING)
484 sprintf(props + strlen(props), "\\kerning%u", fmt->wKerning);
485 if (fmt->dwMask & CFM_LCID) {
486 /* TODO: handle SFF_PLAINRTF */
487 if (LOWORD(fmt->lcid) == 1024)
488 strcat(props, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
489 else
490 sprintf(props + strlen(props), "\\lang%u", LOWORD(fmt->lcid));
492 /* CFM_LINK is not streamed out by M$ */
493 if (fmt->dwMask & CFM_OFFSET) {
494 if (fmt->yOffset >= 0)
495 sprintf(props + strlen(props), "\\up%d", fmt->yOffset);
496 else
497 sprintf(props + strlen(props), "\\dn%d", -fmt->yOffset);
499 if (fmt->dwMask & CFM_OUTLINE && fmt->dwEffects & CFE_OUTLINE)
500 strcat(props, "\\outl");
501 if (fmt->dwMask & CFM_PROTECTED && fmt->dwEffects & CFE_PROTECTED)
502 strcat(props, "\\protect");
503 /* TODO: CFM_REVISED CFM_REVAUTHOR - probably using rsidtbl? */
504 if (fmt->dwMask & CFM_SHADOW && fmt->dwEffects & CFE_SHADOW)
505 strcat(props, "\\shad");
506 if (fmt->dwMask & CFM_SIZE)
507 sprintf(props + strlen(props), "\\fs%d", fmt->yHeight / 10);
508 if (fmt->dwMask & CFM_SMALLCAPS && fmt->dwEffects & CFE_SMALLCAPS)
509 strcat(props, "\\scaps");
510 if (fmt->dwMask & CFM_SPACING)
511 sprintf(props + strlen(props), "\\expnd%u\\expndtw%u", fmt->sSpacing / 5, fmt->sSpacing);
512 if (fmt->dwMask & CFM_STRIKEOUT && fmt->dwEffects & CFE_STRIKEOUT)
513 strcat(props, "\\strike");
514 if (fmt->dwMask & CFM_STYLE) {
515 sprintf(props + strlen(props), "\\cs%u", fmt->sStyle);
516 /* TODO: emit style contents here */
518 if (fmt->dwMask & (CFM_SUBSCRIPT | CFM_SUPERSCRIPT)) {
519 if (fmt->dwEffects & CFE_SUBSCRIPT)
520 strcat(props, "\\sub");
521 else if (fmt->dwEffects & CFE_SUPERSCRIPT)
522 strcat(props, "\\super");
524 if (fmt->dwMask & CFM_UNDERLINE || fmt->dwMask & CFM_UNDERLINETYPE) {
525 if (fmt->dwMask & CFM_UNDERLINETYPE)
526 switch (fmt->bUnderlineType) {
527 case CFU_CF1UNDERLINE:
528 case CFU_UNDERLINE:
529 strcat(props, "\\ul");
530 break;
531 case CFU_UNDERLINEDOTTED:
532 strcat(props, "\\uld");
533 break;
534 case CFU_UNDERLINEDOUBLE:
535 strcat(props, "\\uldb");
536 break;
537 case CFU_UNDERLINEWORD:
538 strcat(props, "\\ulw");
539 break;
540 case CFU_UNDERLINENONE:
541 default:
542 strcat(props, "\\ul0");
543 break;
545 else if (fmt->dwEffects & CFE_UNDERLINE)
546 strcat(props, "\\ul");
548 /* FIXME: How to emit CFM_WEIGHT? */
550 if (fmt->dwMask & CFM_FACE || fmt->dwMask & CFM_CHARSET) {
551 WCHAR *szFaceName;
553 if (fmt->dwMask & CFM_FACE)
554 szFaceName = fmt->szFaceName;
555 else
556 szFaceName = pStream->fonttbl[0].szFaceName;
557 for (i = 0; i < pStream->nFontTblLen; i++) {
558 if (szFaceName == pStream->fonttbl[i].szFaceName
559 || !lstrcmpW(szFaceName, pStream->fonttbl[i].szFaceName))
560 if (!(fmt->dwMask & CFM_CHARSET)
561 || fmt->bCharSet == pStream->fonttbl[i].bCharSet)
562 break;
564 if (i < pStream->nFontTblLen)
566 if (i != pStream->nDefaultFont)
567 sprintf(props + strlen(props), "\\f%u", i);
569 /* In UTF-8 mode, charsets/codepages are not used */
570 if (pStream->nDefaultCodePage != CP_UTF8)
572 if (pStream->fonttbl[i].bCharSet == DEFAULT_CHARSET)
573 pStream->nCodePage = pStream->nDefaultCodePage;
574 else
575 pStream->nCodePage = RTFCharSetToCodePage(NULL, pStream->fonttbl[i].bCharSet);
579 if (*props)
580 strcat(props, " ");
581 if (!ME_StreamOutPrint(pStream, props))
582 return FALSE;
583 return TRUE;
587 static BOOL
588 ME_StreamOutRTFText(ME_OutStream *pStream, const WCHAR *text, LONG nChars)
590 char buffer[STREAMOUT_BUFFER_SIZE];
591 int pos = 0;
592 int fit, nBytes, i;
594 if (nChars == -1)
595 nChars = lstrlenW(text);
597 while (nChars) {
598 /* In UTF-8 mode, font charsets are not used. */
599 if (pStream->nDefaultCodePage == CP_UTF8) {
600 /* 6 is the maximum character length in UTF-8 */
601 fit = min(nChars, STREAMOUT_BUFFER_SIZE / 6);
602 nBytes = WideCharToMultiByte(CP_UTF8, 0, text, fit, buffer,
603 STREAMOUT_BUFFER_SIZE, NULL, NULL);
604 nChars -= fit;
605 text += fit;
606 for (i = 0; i < nBytes; i++)
607 if (buffer[i] == '{' || buffer[i] == '}' || buffer[i] == '\\') {
608 if (!ME_StreamOutPrint(pStream, "%.*s\\", i - pos, buffer + pos))
609 return FALSE;
610 pos = i;
612 if (pos < nBytes)
613 if (!ME_StreamOutMove(pStream, buffer + pos, nBytes - pos))
614 return FALSE;
615 pos = 0;
616 } else if (*text < 128) {
617 if (*text == '{' || *text == '}' || *text == '\\')
618 buffer[pos++] = '\\';
619 buffer[pos++] = (char)(*text++);
620 nChars--;
621 } else {
622 BOOL unknown = FALSE;
623 char letter[3];
625 /* FIXME: In the MS docs for WideCharToMultiByte there is a big list of
626 * codepages including CP_SYMBOL for which the last parameter must be set
627 * to NULL for the function to succeed. But in Wine we need to care only
628 * about CP_SYMBOL */
629 nBytes = WideCharToMultiByte(pStream->nCodePage, 0, text, 1,
630 letter, 3, NULL,
631 (pStream->nCodePage == CP_SYMBOL) ? NULL : &unknown);
632 if (unknown)
633 pos += sprintf(buffer + pos, "\\u%d?", (short)*text);
634 else if ((BYTE)*letter < 128) {
635 if (*letter == '{' || *letter == '}' || *letter == '\\')
636 buffer[pos++] = '\\';
637 buffer[pos++] = *letter;
638 } else {
639 for (i = 0; i < nBytes; i++)
640 pos += sprintf(buffer + pos, "\\'%02x", (BYTE)letter[i]);
642 text++;
643 nChars--;
645 if (pos >= STREAMOUT_BUFFER_SIZE - 11) {
646 if (!ME_StreamOutMove(pStream, buffer, pos))
647 return FALSE;
648 pos = 0;
651 return ME_StreamOutMove(pStream, buffer, pos);
655 static BOOL
656 ME_StreamOutRTF(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int nChars, int dwFormat)
658 ME_DisplayItem *p, *pEnd, *pPara;
659 int nOffset, nEndLen;
661 ME_RunOfsFromCharOfs(editor, nStart, &p, &nOffset);
662 ME_RunOfsFromCharOfs(editor, nStart+nChars, &pEnd, &nEndLen);
664 pPara = ME_GetParagraph(p);
666 if (!ME_StreamOutRTFHeader(pStream, dwFormat))
667 return FALSE;
669 if (!ME_StreamOutRTFFontAndColorTbl(pStream, p, pEnd))
670 return FALSE;
672 /* TODO: stylesheet table */
674 /* FIXME: maybe emit something smarter for the generator? */
675 if (!ME_StreamOutPrint(pStream, "{\\*\\generator Wine Riched20 2.0.????;}"))
676 return FALSE;
678 /* TODO: information group */
680 /* TODO: document formatting properties */
682 /* FIXME: We have only one document section */
684 /* TODO: section formatting properties */
686 if (!ME_StreamOutRTFParaProps(pStream, ME_GetParagraph(p)))
687 return FALSE;
689 while(1)
691 switch(p->type)
693 case diParagraph:
694 if (p->member.para.pFmt->dwMask & PFM_TABLE &&
695 p->member.para.pFmt->wEffects & PFE_TABLE)
697 if (!ME_StreamOutRTFTableProps(pStream, p))
698 return FALSE;
700 if (!ME_StreamOutRTFParaProps(pStream, p))
701 return FALSE;
702 pPara = p;
703 break;
704 case diRun:
705 if (p == pEnd && !nEndLen)
706 break;
707 TRACE("flags %xh\n", p->member.run.nFlags);
708 /* TODO: emit embedded objects */
709 if (p->member.run.nFlags & MERF_GRAPHICS) {
710 FIXME("embedded objects are not handled\n");
711 } else if (p->member.run.nFlags & MERF_TAB) {
712 if (pPara->member.para.pFmt->dwMask & PFM_TABLE &&
713 pPara->member.para.pFmt->wEffects & PFE_TABLE)
715 if (!ME_StreamOutPrint(pStream, "\\cell "))
716 return FALSE;
717 } else {
718 if (!ME_StreamOutPrint(pStream, "\\tab "))
719 return FALSE;
721 } else if (p->member.run.nFlags & MERF_ENDPARA) {
722 if (pPara->member.para.pFmt->dwMask & PFM_TABLE
723 && pPara->member.para.pFmt->wEffects & PFE_TABLE)
725 if (!ME_StreamOutPrint(pStream, "\\row \r\n"))
726 return FALSE;
727 } else {
728 if (!ME_StreamOutPrint(pStream, "\r\n\\par"))
729 return FALSE;
731 /* Skip as many characters as required by current line break */
732 nChars -= (p->member.run.nCR <= nChars) ? p->member.run.nCR : nChars;
733 nChars -= (p->member.run.nLF <= nChars) ? p->member.run.nLF : nChars;
734 } else if (p->member.run.nFlags & MERF_ENDROW) {
735 if (!ME_StreamOutPrint(pStream, "\\line \r\n"))
736 return FALSE;
737 nChars--;
738 } else {
739 int nEnd;
741 if (!ME_StreamOutPrint(pStream, "{"))
742 return FALSE;
743 TRACE("style %p\n", p->member.run.style);
744 if (!ME_StreamOutRTFCharProps(pStream, &p->member.run.style->fmt))
745 return FALSE;
747 nEnd = (p == pEnd) ? nEndLen : ME_StrLen(p->member.run.strText);
748 if (!ME_StreamOutRTFText(pStream, p->member.run.strText->szData + nOffset, nEnd - nOffset))
749 return FALSE;
750 nOffset = 0;
751 if (!ME_StreamOutPrint(pStream, "}"))
752 return FALSE;
754 break;
755 default: /* we missed the last item */
756 assert(0);
758 if (p == pEnd)
759 break;
760 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
762 if (!ME_StreamOutPrint(pStream, "}"))
763 return FALSE;
764 return TRUE;
768 static BOOL
769 ME_StreamOutText(ME_TextEditor *editor, ME_OutStream *pStream, int nStart, int nChars, DWORD dwFormat)
771 /* FIXME: use ME_RunOfsFromCharOfs */
772 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
773 int nLen;
774 UINT nCodePage = CP_ACP;
775 char *buffer = NULL;
776 int nBufLen = 0;
777 BOOL success = TRUE;
779 if (!item)
780 return FALSE;
782 if (dwFormat & SF_USECODEPAGE)
783 nCodePage = HIWORD(dwFormat);
785 /* TODO: Handle SF_TEXTIZED */
787 while (success && nChars && item) {
788 nLen = ME_StrLen(item->member.run.strText) - nStart;
789 if (nLen > nChars)
790 nLen = nChars;
792 if (item->member.run.nFlags & MERF_ENDPARA) {
793 static const WCHAR szEOL[2] = { '\r', '\n' };
795 if (!editor->bEmulateVersion10) {
796 /* richedit 2.0 - all line breaks are \r\n */
797 if (dwFormat & SF_UNICODE)
798 success = ME_StreamOutMove(pStream, (const char *)szEOL, sizeof(szEOL));
799 else
800 success = ME_StreamOutMove(pStream, "\r\n", 2);
801 assert(nLen == 1);
802 } else {
803 int i; int tnLen;
805 /* richedit 1.0 - need to honor actual \r and \n amounts */
806 nLen = item->member.run.nCR + item->member.run.nLF;
807 if (nLen > nChars)
808 nLen = nChars;
809 tnLen = nLen;
811 i = 0;
812 while (tnLen > 0 && i < item->member.run.nCR) {
813 if (dwFormat & SF_UNICODE)
814 success = ME_StreamOutMove(pStream, (const char *)(&szEOL[0]), sizeof(WCHAR));
815 else
816 success = ME_StreamOutMove(pStream, "\r", 1);
817 tnLen--; i++;
819 i = 0;
820 while (tnLen > 0 && i < item->member.run.nLF) {
821 if (dwFormat & SF_UNICODE)
822 success = ME_StreamOutMove(pStream, (const char *)(&szEOL[1]), sizeof(WCHAR));
823 else
824 success = ME_StreamOutMove(pStream, "\n", 1);
825 tnLen--; i++;
828 } else {
829 if (dwFormat & SF_UNICODE)
830 success = ME_StreamOutMove(pStream, (const char *)(item->member.run.strText->szData + nStart),
831 sizeof(WCHAR) * nLen);
832 else {
833 int nSize;
835 nSize = WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
836 nLen, NULL, 0, NULL, NULL);
837 if (nSize > nBufLen) {
838 FREE_OBJ(buffer);
839 buffer = ALLOC_N_OBJ(char, nSize);
840 nBufLen = nSize;
842 WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
843 nLen, buffer, nSize, NULL, NULL);
844 success = ME_StreamOutMove(pStream, buffer, nSize);
848 nChars -= nLen;
849 nStart = 0;
850 item = ME_FindItemFwd(item, diRun);
853 FREE_OBJ(buffer);
854 return success;
858 LRESULT
859 ME_StreamOutRange(ME_TextEditor *editor, DWORD dwFormat, int nStart, int nTo, EDITSTREAM *stream)
861 ME_OutStream *pStream = ME_StreamOutInit(editor, stream);
863 if (nTo == -1)
865 nTo = ME_GetTextLength(editor);
866 /* Generate an end-of-paragraph at the end of SCF_ALL RTF output */
867 if (dwFormat & SF_RTF)
868 nTo++;
870 TRACE("from %d to %d\n", nStart, nTo);
872 if (dwFormat & SF_RTF)
873 ME_StreamOutRTF(editor, pStream, nStart, nTo - nStart, dwFormat);
874 else if (dwFormat & SF_TEXT || dwFormat & SF_TEXTIZED)
875 ME_StreamOutText(editor, pStream, nStart, nTo - nStart, dwFormat);
876 if (!pStream->stream->dwError)
877 ME_StreamOutFlush(pStream);
878 return ME_StreamOutFree(pStream);
881 LRESULT
882 ME_StreamOut(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream)
884 int nStart, nTo;
886 if (dwFormat & SFF_SELECTION)
887 ME_GetSelection(editor, &nStart, &nTo);
888 else {
889 nStart = 0;
890 nTo = -1;
892 return ME_StreamOutRange(editor, dwFormat, nStart, nTo, stream);