configure: Make the font warning more explicit about what package is missing.
[wine/multimedia.git] / dlls / riched20 / writer.c
blob7ec1eaac48e610efff93eb27c7c3f9a1ac8165c6
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 "editor.h"
22 #include "rtf.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
27 static BOOL
28 ME_StreamOutRTFText(ME_OutStream *pStream, WCHAR *text, LONG nChars);
31 static ME_OutStream*
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;
37 pStream->pos = 0;
38 pStream->written = 0;
39 pStream->nFontTblLen = 0;
40 pStream->nColorTblLen = 1;
41 return pStream;
45 static BOOL
46 ME_StreamOutFlush(ME_OutStream *pStream)
48 LONG nStart = 0;
49 LONG nWritten = 0;
50 LONG nRemaining = 0;
51 EDITSTREAM *stream = pStream->stream;
53 do {
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)
68 return FALSE;
69 pStream->written += nWritten;
70 nStart += nWritten;
71 } while (nStart < pStream->pos);
72 pStream->pos = 0;
73 return TRUE;
77 static LONG
78 ME_StreamOutFree(ME_OutStream *pStream)
80 LONG written = pStream->written;
81 TRACE("total length = %lu\n", written);
83 FREE_OBJ(pStream);
84 return written;
88 static BOOL
89 ME_StreamOutMove(ME_OutStream *pStream, const char *buffer, int len)
91 while (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);
97 len -= fit;
98 buffer += fit;
99 pStream->pos += fit;
100 if (pStream->pos == STREAMOUT_BUFFER_SIZE) {
101 if (!ME_StreamOutFlush(pStream))
102 return FALSE;
105 return TRUE;
109 static BOOL
110 ME_StreamOutPrint(ME_OutStream *pStream, const char *format, ...)
112 char string[STREAMOUT_BUFFER_SIZE]; /* This is going to be enough */
113 int len;
114 va_list valist;
116 va_start(valist, format);
117 len = vsnprintf(string, sizeof(string), format, valist);
118 va_end(valist);
120 return ME_StreamOutMove(pStream, string, len);
124 static BOOL
125 ME_StreamOutRTFHeader(ME_OutStream *pStream, int dwFormat)
127 const char *cCharSet = NULL;
128 UINT nCodePage;
129 LANGID language;
130 BOOL success;
132 if (dwFormat & SF_USECODEPAGE) {
133 CPINFOEXW info;
135 switch (HIWORD(dwFormat)) {
136 case CP_ACP:
137 cCharSet = "ansi";
138 nCodePage = GetACP();
139 break;
140 case CP_OEMCP:
141 nCodePage = GetOEMCP();
142 if (nCodePage == 437)
143 cCharSet = "pc";
144 else if (nCodePage == 850)
145 cCharSet = "pca";
146 else
147 cCharSet = "ansi";
148 break;
149 case CP_UTF8:
150 nCodePage = CP_UTF8;
151 break;
152 default:
153 if (HIWORD(dwFormat) == CP_MACCP) {
154 cCharSet = "mac";
155 nCodePage = 10000; /* MacRoman */
156 } else {
157 cCharSet = "ansi";
158 nCodePage = 1252; /* Latin-1 */
160 if (GetCPInfoExW(HIWORD(dwFormat), 0, &info))
161 nCodePage = info.CodePage;
163 } else {
164 cCharSet = "ansi";
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
168 * the reader... */
169 nCodePage = GetACP();
171 if (nCodePage == CP_UTF8)
172 success = ME_StreamOutPrint(pStream, "{\\urtf");
173 else
174 success = ME_StreamOutPrint(pStream, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet, nCodePage);
176 if (!success)
177 return FALSE;
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))
185 return FALSE;
187 /* FIXME: This should be a document property */
188 pStream->nDefaultFont = 0;
190 return TRUE;
194 static BOOL
195 ME_StreamOutRTFFontAndColorTbl(ME_OutStream *pStream, ME_DisplayItem *pFirstRun, ME_DisplayItem *pLastRun)
197 ME_DisplayItem *item = pFirstRun;
198 ME_FontTableItem *table = pStream->fonttbl;
199 int i;
201 do {
202 CHARFORMAT2W *fmt = &item->member.run.style->fmt;
203 COLORREF crColor;
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)))
212 break;
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)
224 break;
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)
234 break;
235 if (i == pStream->nColorTblLen) {
236 pStream->colortbl[i] = crColor;
237 pStream->nColorTblLen++;
241 if (item == pLastRun)
242 break;
243 item = ME_FindItemFwd(item, diRun);
244 } while (item);
246 if (!ME_StreamOutPrint(pStream, "{\\fonttbl"))
247 return FALSE;
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))
252 return FALSE;
253 } else {
254 if (!ME_StreamOutPrint(pStream, "{\\f%u ", i))
255 return FALSE;
257 if (!ME_StreamOutRTFText(pStream, table[i].szFaceName, -1))
258 return FALSE;
259 if (!ME_StreamOutPrint(pStream, ";}\r\n"))
260 return FALSE;
262 if (!ME_StreamOutPrint(pStream, "}"))
263 return FALSE;
265 /* Output colors table if not empty */
266 if (pStream->nColorTblLen > 1) {
267 if (!ME_StreamOutPrint(pStream, "{\\colortbl;"))
268 return FALSE;
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))
274 return FALSE;
276 if (!ME_StreamOutPrint(pStream, "}"))
277 return FALSE;
280 return TRUE;
284 static BOOL
285 ME_StreamOutRTFParaProps(ME_OutStream *pStream, ME_DisplayItem *para)
287 PARAFORMAT2 *fmt = para->member.para.pFmt;
288 char props[STREAMOUT_BUFFER_SIZE] = "";
289 int i;
291 if (para->member.para.pCells)
293 ME_TableCell *cell = para->member.para.pCells;
295 if (!ME_StreamOutPrint(pStream, "\\trowd"))
296 return FALSE;
297 do {
298 sprintf(props, "\\cellx%d", cell->nRightBoundary);
299 if (!ME_StreamOutPrint(pStream, props))
300 return FALSE;
301 cell = cell->next;
302 } while (cell);
303 props[0] = '\0';
306 /* TODO: Don't emit anything if the last PARAFORMAT2 is inherited */
307 if (!ME_StreamOutPrint(pStream, "\\pard"))
308 return FALSE;
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) {
320 case PFA_LEFT:
321 /* Default alignment: not emitted */
322 break;
323 case PFA_RIGHT:
324 strcat(props, "\\qr");
325 break;
326 case PFA_CENTER:
327 strcat(props, "\\qc");
328 break;
329 case PFA_JUSTIFY:
330 strcat(props, "\\qj");
331 break;
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");
341 break;
342 case 1: /* 1.5 spacing */
343 strcat(props, "\\sl-360\\slmult1");
344 break;
345 case 2: /* Double spacing */
346 strcat(props, "\\sl-480\\slmult1");
347 break;
348 case 3:
349 sprintf(props + strlen(props), "\\sl%ld\\slmult0", fmt->dyLineSpacing);
350 break;
351 case 4:
352 sprintf(props + strlen(props), "\\sl-%ld\\slmult0", fmt->dyLineSpacing);
353 break;
354 case 5:
355 sprintf(props + strlen(props), "\\sl-%ld\\slmult1", fmt->dyLineSpacing * 240 / 20);
356 break;
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) {
397 case 1:
398 strcat(props, "\\tqc");
399 break;
400 case 2:
401 strcat(props, "\\tqr");
402 break;
403 case 3:
404 strcat(props, "\\tqdec");
405 break;
406 case 4:
407 /* Word bar tab (vertical bar). Handled below */
408 break;
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",
422 "", "", "" };
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))
432 return FALSE;
434 return TRUE;
438 static BOOL
439 ME_StreamOutRTFCharProps(ME_OutStream *pStream, CHARFORMAT2W *fmt)
441 char props[STREAMOUT_BUFFER_SIZE] = "";
442 int i;
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);
453 break;
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);
464 break;
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");
483 else
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);
490 else
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:
522 case CFU_UNDERLINE:
523 strcat(props, "\\ul");
524 break;
525 case CFU_UNDERLINEDOTTED:
526 strcat(props, "\\uld");
527 break;
528 case CFU_UNDERLINEDOUBLE:
529 strcat(props, "\\uldb");
530 break;
531 case CFU_UNDERLINEWORD:
532 strcat(props, "\\ulw");
533 break;
534 case CFU_UNDERLINENONE:
535 default:
536 strcat(props, "\\ul0");
537 break;
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) {
545 WCHAR *szFaceName;
547 if (fmt->dwMask & CFM_FACE)
548 szFaceName = fmt->szFaceName;
549 else
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)
556 break;
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;
568 else
569 pStream->nCodePage = RTFCharSetToCodePage(NULL, pStream->fonttbl[i].bCharSet);
573 if (*props)
574 strcat(props, " ");
575 if (!ME_StreamOutPrint(pStream, props))
576 return FALSE;
577 return TRUE;
581 static BOOL
582 ME_StreamOutRTFText(ME_OutStream *pStream, WCHAR *text, LONG nChars)
584 char buffer[STREAMOUT_BUFFER_SIZE];
585 int pos = 0;
586 int fit, nBytes, i;
588 if (nChars == -1)
589 nChars = lstrlenW(text);
591 while (nChars) {
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);
598 nChars -= fit;
599 text += fit;
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))
603 return FALSE;
604 pos = i;
606 if (pos < nBytes)
607 if (!ME_StreamOutMove(pStream, buffer + pos, nBytes - pos))
608 return FALSE;
609 pos = 0;
610 } else if (*text < 128) {
611 if (*text == '{' || *text == '}' || *text == '\\')
612 buffer[pos++] = '\\';
613 buffer[pos++] = (char)(*text++);
614 nChars--;
615 } else {
616 BOOL unknown = FALSE;
617 char letter[3];
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
622 * about CP_SYMBOL */
623 nBytes = WideCharToMultiByte(pStream->nCodePage, 0, text, 1,
624 letter, 3, NULL,
625 (pStream->nCodePage == CP_SYMBOL) ? NULL : &unknown);
626 if (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;
632 } else {
633 for (i = 0; i < nBytes; i++)
634 pos += sprintf(buffer + pos, "\\'%02x", (BYTE)letter[i]);
636 text++;
637 nChars--;
639 if (pos >= STREAMOUT_BUFFER_SIZE - 11) {
640 if (!ME_StreamOutMove(pStream, buffer, pos))
641 return FALSE;
642 pos = 0;
645 return ME_StreamOutMove(pStream, buffer, pos);
649 static BOOL
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))
661 return FALSE;
663 if (!ME_StreamOutRTFFontAndColorTbl(pStream, p, pEnd))
664 return FALSE;
666 /* TODO: stylesheet table */
668 /* FIXME: maybe emit something smarter for the generator? */
669 if (!ME_StreamOutPrint(pStream, "{\\*\\generator Wine Riched20 2.0.????;}"))
670 return FALSE;
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)))
681 return FALSE;
683 while(1)
685 switch(p->type)
687 case diParagraph:
688 if (!ME_StreamOutRTFParaProps(pStream, p))
689 return FALSE;
690 pPara = p;
691 break;
692 case diRun:
693 if (p == pEnd && !nEndLen)
694 break;
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 "))
701 return FALSE;
702 nChars--;
703 } else if (p->member.run.nFlags & MERF_ENDPARA) {
704 if (pPara->member.para.bTable) {
705 if (!ME_StreamOutPrint(pStream, "\\row \r\n"))
706 return FALSE;
707 } else {
708 if (!ME_StreamOutPrint(pStream, "\r\n\\par"))
709 return FALSE;
711 nChars--;
712 if (editor->bEmulateVersion10 && nChars)
713 nChars--;
714 } else {
715 int nEnd;
717 if (!ME_StreamOutPrint(pStream, "{"))
718 return FALSE;
719 TRACE("style %p\n", p->member.run.style);
720 if (!ME_StreamOutRTFCharProps(pStream, &p->member.run.style->fmt))
721 return FALSE;
723 nEnd = (p == pEnd) ? nEndLen : ME_StrLen(p->member.run.strText);
724 if (!ME_StreamOutRTFText(pStream, p->member.run.strText->szData + nOffset, nEnd - nOffset))
725 return FALSE;
726 nOffset = 0;
727 if (!ME_StreamOutPrint(pStream, "}"))
728 return FALSE;
730 break;
731 default: /* we missed the last item */
732 assert(0);
734 if (p == pEnd)
735 break;
736 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
738 if (!ME_StreamOutPrint(pStream, "}"))
739 return FALSE;
740 return TRUE;
744 static BOOL
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);
749 int nLen;
750 UINT nCodePage = CP_ACP;
751 char *buffer = NULL;
752 int nBufLen = 0;
753 BOOL success = TRUE;
755 if (!item)
756 return FALSE;
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;
765 if (nLen > nChars)
766 nLen = nChars;
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));
773 else
774 success = ME_StreamOutMove(pStream, "\r\n", 2);
775 } else {
776 if (dwFormat & SF_UNICODE)
777 success = ME_StreamOutMove(pStream, (const char *)(item->member.run.strText->szData + nStart),
778 sizeof(WCHAR) * nLen);
779 else {
780 int nSize;
782 nSize = WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
783 nLen, NULL, 0, NULL, NULL);
784 if (nSize > nBufLen) {
785 FREE_OBJ(buffer);
786 buffer = ALLOC_N_OBJ(char, nSize);
787 nBufLen = nSize;
789 WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
790 nLen, buffer, nSize, NULL, NULL);
791 success = ME_StreamOutMove(pStream, buffer, nSize);
795 nChars -= nLen;
796 if (editor->bEmulateVersion10 && nChars && item->member.run.nFlags & MERF_ENDPARA)
797 nChars--;
798 nStart = 0;
799 item = ME_FindItemFwd(item, diRun);
802 FREE_OBJ(buffer);
803 return success;
807 LRESULT
808 ME_StreamOutRange(ME_TextEditor *editor, DWORD dwFormat, int nStart, int nTo, EDITSTREAM *stream)
810 ME_OutStream *pStream = ME_StreamOutInit(editor, stream);
812 if (nTo == -1)
814 nTo = ME_GetTextLength(editor);
815 /* Generate an end-of-paragraph at the end of SCF_ALL RTF output */
816 if (dwFormat & SF_RTF)
817 nTo++;
819 TRACE("from %d to %d\n", nStart, nTo);
821 if (dwFormat & SF_RTF)
822 ME_StreamOutRTF(editor, pStream, nStart, nTo - nStart, dwFormat);
823 else if (dwFormat & SF_TEXT || dwFormat & SF_TEXTIZED)
824 ME_StreamOutText(editor, pStream, nStart, nTo - nStart, dwFormat);
825 if (!pStream->stream->dwError)
826 ME_StreamOutFlush(pStream);
827 return ME_StreamOutFree(pStream);
830 LRESULT
831 ME_StreamOut(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream)
833 int nStart, nTo;
835 if (dwFormat & SFF_SELECTION)
836 ME_GetSelection(editor, &nStart, &nTo);
837 else {
838 nStart = 0;
839 nTo = -1;
841 return ME_StreamOutRange(editor, dwFormat, nStart, nTo, stream);