dxgi: Create d3d11 swapchain textures directly from d3d11_swapchain_init().
[wine.git] / dlls / riched20 / writer.c
blobdef16a4925a2339268c5188a0b1169bb67a33ae4
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 #define NONAMELESSUNION
23 #include "editor.h"
24 #include "rtf.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
28 #define STREAMOUT_BUFFER_SIZE 4096
29 #define STREAMOUT_FONTTBL_SIZE 8192
30 #define STREAMOUT_COLORTBL_SIZE 1024
32 typedef struct tagME_OutStream
34 EDITSTREAM *stream;
35 char buffer[STREAMOUT_BUFFER_SIZE];
36 UINT pos, written;
37 UINT nCodePage;
38 UINT nFontTblLen;
39 ME_FontTableItem fonttbl[STREAMOUT_FONTTBL_SIZE];
40 UINT nColorTblLen;
41 COLORREF colortbl[STREAMOUT_COLORTBL_SIZE];
42 UINT nDefaultFont;
43 UINT nDefaultCodePage;
44 /* nNestingLevel = 0 means we aren't in a cell, 1 means we are in a cell,
45 * an greater numbers mean we are in a cell nested within a cell. */
46 UINT nNestingLevel;
47 CHARFORMAT2W cur_fmt; /* current character format */
48 } ME_OutStream;
50 static BOOL
51 ME_StreamOutRTFText(ME_OutStream *pStream, const WCHAR *text, LONG nChars);
54 static ME_OutStream*
55 ME_StreamOutInit(ME_TextEditor *editor, EDITSTREAM *stream)
57 ME_OutStream *pStream = heap_alloc_zero(sizeof(*pStream));
59 pStream->stream = stream;
60 pStream->stream->dwError = 0;
61 pStream->nColorTblLen = 1;
62 pStream->cur_fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
63 pStream->cur_fmt.bUnderlineType = CFU_UNDERLINE;
64 return pStream;
68 static BOOL
69 ME_StreamOutFlush(ME_OutStream *pStream)
71 LONG nWritten = 0;
72 EDITSTREAM *stream = pStream->stream;
74 if (pStream->pos) {
75 TRACE("sending %u bytes\n", pStream->pos);
76 nWritten = pStream->pos;
77 stream->dwError = stream->pfnCallback(stream->dwCookie, (LPBYTE)pStream->buffer,
78 pStream->pos, &nWritten);
79 TRACE("error=%lu written=%lu\n", stream->dwError, nWritten);
80 if (nWritten == 0 || stream->dwError)
81 return FALSE;
82 /* Don't resend partial chunks if nWritten < pStream->pos */
84 if (nWritten == pStream->pos)
85 pStream->written += nWritten;
86 pStream->pos = 0;
87 return TRUE;
91 static LONG
92 ME_StreamOutFree(ME_OutStream *pStream)
94 LONG written = pStream->written;
95 TRACE("total length = %lu\n", written);
97 heap_free(pStream);
98 return written;
102 static BOOL
103 ME_StreamOutMove(ME_OutStream *pStream, const char *buffer, int len)
105 while (len) {
106 int space = STREAMOUT_BUFFER_SIZE - pStream->pos;
107 int fit = min(space, len);
109 TRACE("%u:%u:%s\n", pStream->pos, fit, debugstr_an(buffer,fit));
110 memmove(pStream->buffer + pStream->pos, buffer, fit);
111 len -= fit;
112 buffer += fit;
113 pStream->pos += fit;
114 if (pStream->pos == STREAMOUT_BUFFER_SIZE) {
115 if (!ME_StreamOutFlush(pStream))
116 return FALSE;
119 return TRUE;
123 static BOOL WINAPIV
124 ME_StreamOutPrint(ME_OutStream *pStream, const char *format, ...)
126 char string[STREAMOUT_BUFFER_SIZE]; /* This is going to be enough */
127 int len;
128 va_list valist;
130 va_start(valist, format);
131 len = vsnprintf(string, sizeof(string), format, valist);
132 va_end(valist);
134 return ME_StreamOutMove(pStream, string, len);
137 #define HEX_BYTES_PER_LINE 40
139 static BOOL
140 ME_StreamOutHexData(ME_OutStream *stream, const BYTE *data, UINT len)
143 char line[HEX_BYTES_PER_LINE * 2 + 1];
144 UINT size, i;
145 static const char hex[] = "0123456789abcdef";
147 while (len)
149 size = min( len, HEX_BYTES_PER_LINE );
150 for (i = 0; i < size; i++)
152 line[i * 2] = hex[(*data >> 4) & 0xf];
153 line[i * 2 + 1] = hex[*data & 0xf];
154 data++;
156 line[size * 2] = '\n';
157 if (!ME_StreamOutMove( stream, line, size * 2 + 1 ))
158 return FALSE;
159 len -= size;
161 return TRUE;
164 static BOOL
165 ME_StreamOutRTFHeader(ME_OutStream *pStream, int dwFormat)
167 const char *cCharSet = NULL;
168 UINT nCodePage;
169 LANGID language;
170 BOOL success;
172 if (dwFormat & SF_USECODEPAGE) {
173 CPINFOEXW info;
175 switch (HIWORD(dwFormat)) {
176 case CP_ACP:
177 cCharSet = "ansi";
178 nCodePage = GetACP();
179 break;
180 case CP_OEMCP:
181 nCodePage = GetOEMCP();
182 if (nCodePage == 437)
183 cCharSet = "pc";
184 else if (nCodePage == 850)
185 cCharSet = "pca";
186 else
187 cCharSet = "ansi";
188 break;
189 case CP_UTF8:
190 nCodePage = CP_UTF8;
191 break;
192 default:
193 if (HIWORD(dwFormat) == CP_MACCP) {
194 cCharSet = "mac";
195 nCodePage = 10000; /* MacRoman */
196 } else {
197 cCharSet = "ansi";
198 nCodePage = 1252; /* Latin-1 */
200 if (GetCPInfoExW(HIWORD(dwFormat), 0, &info))
201 nCodePage = info.CodePage;
203 } else {
204 cCharSet = "ansi";
205 /* TODO: If the original document contained an \ansicpg value, retain it.
206 * Otherwise, M$ richedit emits a codepage number determined from the
207 * charset of the default font here. Anyway, this value is not used by
208 * the reader... */
209 nCodePage = GetACP();
211 if (nCodePage == CP_UTF8)
212 success = ME_StreamOutPrint(pStream, "{\\urtf");
213 else
214 success = ME_StreamOutPrint(pStream, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet, nCodePage);
216 if (!success)
217 return FALSE;
219 pStream->nDefaultCodePage = nCodePage;
221 /* FIXME: This should be a document property */
222 /* TODO: handle SFF_PLAINRTF */
223 language = GetUserDefaultLangID();
224 if (!ME_StreamOutPrint(pStream, "\\deff0\\deflang%u\\deflangfe%u", language, language))
225 return FALSE;
227 /* FIXME: This should be a document property */
228 pStream->nDefaultFont = 0;
230 return TRUE;
233 static void add_font_to_fonttbl( ME_OutStream *stream, ME_Style *style )
235 ME_FontTableItem *table = stream->fonttbl;
236 CHARFORMAT2W *fmt = &style->fmt;
237 WCHAR *face = fmt->szFaceName;
238 BYTE charset = (fmt->dwMask & CFM_CHARSET) ? fmt->bCharSet : DEFAULT_CHARSET;
239 int i;
241 if (fmt->dwMask & CFM_FACE)
243 for (i = 0; i < stream->nFontTblLen; i++)
244 if (table[i].bCharSet == charset
245 && (table[i].szFaceName == face || !wcscmp(table[i].szFaceName, face)))
246 break;
248 if (i == stream->nFontTblLen && i < STREAMOUT_FONTTBL_SIZE)
250 table[i].bCharSet = charset;
251 table[i].szFaceName = face;
252 stream->nFontTblLen++;
257 static BOOL find_font_in_fonttbl( ME_OutStream *stream, CHARFORMAT2W *fmt, unsigned int *idx )
259 WCHAR *facename;
260 int i;
262 *idx = 0;
263 if (fmt->dwMask & CFM_FACE)
264 facename = fmt->szFaceName;
265 else
266 facename = stream->fonttbl[0].szFaceName;
267 for (i = 0; i < stream->nFontTblLen; i++)
269 if (facename == stream->fonttbl[i].szFaceName
270 || !wcscmp(facename, stream->fonttbl[i].szFaceName))
271 if (!(fmt->dwMask & CFM_CHARSET)
272 || fmt->bCharSet == stream->fonttbl[i].bCharSet)
274 *idx = i;
275 break;
279 return i < stream->nFontTblLen;
282 static void add_color_to_colortbl( ME_OutStream *stream, COLORREF color )
284 int i;
286 for (i = 1; i < stream->nColorTblLen; i++)
287 if (stream->colortbl[i] == color)
288 break;
290 if (i == stream->nColorTblLen && i < STREAMOUT_COLORTBL_SIZE)
292 stream->colortbl[i] = color;
293 stream->nColorTblLen++;
297 static BOOL find_color_in_colortbl( ME_OutStream *stream, COLORREF color, unsigned int *idx )
299 int i;
301 *idx = 0;
302 for (i = 1; i < stream->nColorTblLen; i++)
304 if (stream->colortbl[i] == color)
306 *idx = i;
307 break;
311 return i < stream->nFontTblLen;
314 static BOOL stream_out_font_and_colour_tbls( ME_OutStream *pStream, ME_Run *first, ME_Run *last )
316 ME_Run *run = first;
317 ME_FontTableItem *table = pStream->fonttbl;
318 unsigned int i;
319 ME_Cell *cell = NULL;
320 ME_Paragraph *prev_para = NULL;
324 CHARFORMAT2W *fmt = &run->style->fmt;
326 add_font_to_fonttbl( pStream, run->style );
328 if (fmt->dwMask & CFM_COLOR && !(fmt->dwEffects & CFE_AUTOCOLOR))
329 add_color_to_colortbl( pStream, fmt->crTextColor );
330 if (fmt->dwMask & CFM_BACKCOLOR && !(fmt->dwEffects & CFE_AUTOBACKCOLOR))
331 add_color_to_colortbl( pStream, fmt->crBackColor );
333 if (run->para != prev_para)
335 /* check for any para numbering text */
336 if (run->para->fmt.wNumbering)
337 add_font_to_fonttbl( pStream, run->para->para_num.style );
339 if ((cell = para_cell( run->para )))
341 ME_Border* borders[4] = { &cell->border.top, &cell->border.left,
342 &cell->border.bottom, &cell->border.right };
343 for (i = 0; i < 4; i++)
344 if (borders[i]->width > 0)
345 add_color_to_colortbl( pStream, borders[i]->colorRef );
348 prev_para = run->para;
351 if (run == last) break;
352 run = run_next_all_paras( run );
353 } while (run);
355 if (!ME_StreamOutPrint(pStream, "{\\fonttbl"))
356 return FALSE;
358 for (i = 0; i < pStream->nFontTblLen; i++) {
359 if (table[i].bCharSet != DEFAULT_CHARSET) {
360 if (!ME_StreamOutPrint(pStream, "{\\f%u\\fcharset%u ", i, table[i].bCharSet))
361 return FALSE;
362 } else {
363 if (!ME_StreamOutPrint(pStream, "{\\f%u ", i))
364 return FALSE;
366 if (!ME_StreamOutRTFText(pStream, table[i].szFaceName, -1))
367 return FALSE;
368 if (!ME_StreamOutPrint(pStream, ";}"))
369 return FALSE;
371 if (!ME_StreamOutPrint(pStream, "}\r\n"))
372 return FALSE;
374 /* Output the color table */
375 if (!ME_StreamOutPrint(pStream, "{\\colortbl;")) return FALSE; /* first entry is auto-color */
376 for (i = 1; i < pStream->nColorTblLen; i++)
378 if (!ME_StreamOutPrint(pStream, "\\red%u\\green%u\\blue%u;", pStream->colortbl[i] & 0xFF,
379 (pStream->colortbl[i] >> 8) & 0xFF, (pStream->colortbl[i] >> 16) & 0xFF))
380 return FALSE;
382 if (!ME_StreamOutPrint(pStream, "}\r\n")) return FALSE;
384 return TRUE;
387 static BOOL stream_out_table_props( ME_TextEditor *editor, ME_OutStream *pStream,
388 ME_Paragraph *para )
390 ME_Cell *cell;
391 char props[STREAMOUT_BUFFER_SIZE] = "";
392 int i;
393 const char sideChar[4] = {'t','l','b','r'};
395 if (!ME_StreamOutPrint(pStream, "\\trowd"))
396 return FALSE;
397 if (!editor->bEmulateVersion10) /* v4.1 */
399 PARAFORMAT2 *pFmt = &table_row_end( para )->fmt;
400 cell = table_row_first_cell( para );
401 assert( cell );
402 if (pFmt->dxOffset)
403 sprintf(props + strlen(props), "\\trgaph%ld", pFmt->dxOffset);
404 if (pFmt->dxStartIndent)
405 sprintf(props + strlen(props), "\\trleft%ld", pFmt->dxStartIndent);
408 ME_Border* borders[4] = { &cell->border.top, &cell->border.left,
409 &cell->border.bottom, &cell->border.right };
410 for (i = 0; i < 4; i++)
412 if (borders[i]->width)
414 unsigned int idx;
415 COLORREF crColor = borders[i]->colorRef;
416 sprintf(props + strlen(props), "\\clbrdr%c", sideChar[i]);
417 sprintf(props + strlen(props), "\\brdrs");
418 sprintf(props + strlen(props), "\\brdrw%d", borders[i]->width);
419 if (find_color_in_colortbl( pStream, crColor, &idx ))
420 sprintf(props + strlen(props), "\\brdrcf%u", idx);
423 sprintf( props + strlen(props), "\\cellx%d", cell->nRightBoundary );
424 cell = cell_next( cell );
425 } while (cell_next( cell ));
427 else /* v1.0 - 3.0 */
429 const ME_Border* borders[4] = { &para->border.top,
430 &para->border.left,
431 &para->border.bottom,
432 &para->border.right };
433 PARAFORMAT2 *pFmt = &para->fmt;
435 assert( !(para->nFlags & (MEPF_ROWSTART | MEPF_ROWEND | MEPF_CELL)) );
436 if (pFmt->dxOffset)
437 sprintf(props + strlen(props), "\\trgaph%ld", pFmt->dxOffset);
438 if (pFmt->dxStartIndent)
439 sprintf(props + strlen(props), "\\trleft%ld", pFmt->dxStartIndent);
440 for (i = 0; i < 4; i++)
442 if (borders[i]->width)
444 unsigned int idx;
445 COLORREF crColor = borders[i]->colorRef;
446 sprintf(props + strlen(props), "\\trbrdr%c", sideChar[i]);
447 sprintf(props + strlen(props), "\\brdrs");
448 sprintf(props + strlen(props), "\\brdrw%d", borders[i]->width);
449 if (find_color_in_colortbl( pStream, crColor, &idx ))
450 sprintf(props + strlen(props), "\\brdrcf%u", idx);
453 for (i = 0; i < pFmt->cTabCount; i++)
455 sprintf(props + strlen(props), "\\cellx%ld", pFmt->rgxTabs[i] & 0x00FFFFFF);
458 if (!ME_StreamOutPrint(pStream, props))
459 return FALSE;
460 props[0] = '\0';
461 return TRUE;
464 static BOOL stream_out_para_num( ME_OutStream *stream, ME_Paragraph *para, BOOL pn_dest )
466 static const char fmt_label[] = "{\\*\\pn\\pnlvlbody\\pnf%u\\pnindent%d\\pnstart%d%s%s}";
467 static const char fmt_bullet[] = "{\\*\\pn\\pnlvlblt\\pnf%u\\pnindent%d{\\pntxtb\\'b7}}";
468 static const char dec[] = "\\pndec";
469 static const char lcltr[] = "\\pnlcltr";
470 static const char ucltr[] = "\\pnucltr";
471 static const char lcrm[] = "\\pnlcrm";
472 static const char ucrm[] = "\\pnucrm";
473 static const char period[] = "{\\pntxta.}";
474 static const char paren[] = "{\\pntxta)}";
475 static const char parens[] = "{\\pntxtb(}{\\pntxta)}";
476 const char *type, *style = "";
477 unsigned int idx;
479 find_font_in_fonttbl( stream, &para->para_num.style->fmt, &idx );
481 if (!ME_StreamOutPrint( stream, "{\\pntext\\f%u ", idx )) return FALSE;
482 if (!ME_StreamOutRTFText( stream, para->para_num.text->szData, para->para_num.text->nLen ))
483 return FALSE;
484 if (!ME_StreamOutPrint( stream, "\\tab}" )) return FALSE;
486 if (!pn_dest) return TRUE;
488 if (para->fmt.wNumbering == PFN_BULLET)
490 if (!ME_StreamOutPrint( stream, fmt_bullet, idx, para->fmt.wNumberingTab ))
491 return FALSE;
493 else
495 switch (para->fmt.wNumbering)
497 case PFN_ARABIC:
498 default:
499 type = dec;
500 break;
501 case PFN_LCLETTER:
502 type = lcltr;
503 break;
504 case PFN_UCLETTER:
505 type = ucltr;
506 break;
507 case PFN_LCROMAN:
508 type = lcrm;
509 break;
510 case PFN_UCROMAN:
511 type = ucrm;
512 break;
514 switch (para->fmt.wNumberingStyle & 0xf00)
516 case PFNS_PERIOD:
517 style = period;
518 break;
519 case PFNS_PAREN:
520 style = paren;
521 break;
522 case PFNS_PARENS:
523 style = parens;
524 break;
527 if (!ME_StreamOutPrint( stream, fmt_label, idx, para->fmt.wNumberingTab,
528 para->fmt.wNumberingStart, type, style ))
529 return FALSE;
531 return TRUE;
534 static BOOL stream_out_para_props( ME_TextEditor *editor, ME_OutStream *pStream,
535 ME_Paragraph *para )
537 PARAFORMAT2 *fmt = &para->fmt;
538 char props[STREAMOUT_BUFFER_SIZE] = "";
539 int i;
540 ME_Paragraph *prev_para = para_prev( para );
542 if (!editor->bEmulateVersion10) /* v4.1 */
544 if (para->nFlags & MEPF_ROWSTART)
546 pStream->nNestingLevel++;
547 if (pStream->nNestingLevel == 1)
548 if (!stream_out_table_props( editor, pStream, para )) return FALSE;
549 return TRUE;
551 else if (para->nFlags & MEPF_ROWEND)
553 pStream->nNestingLevel--;
554 if (pStream->nNestingLevel >= 1)
556 if (!ME_StreamOutPrint(pStream, "{\\*\\nesttableprops")) return FALSE;
557 if (!stream_out_table_props( editor, pStream, para )) return FALSE;
558 if (!ME_StreamOutPrint(pStream, "\\nestrow}{\\nonesttables\\par}\r\n")) return FALSE;
560 else if (!ME_StreamOutPrint(pStream, "\\row\r\n")) return FALSE;
561 return TRUE;
564 else /* v1.0 - 3.0 */
566 if (para->fmt.dwMask & PFM_TABLE && para->fmt.wEffects & PFE_TABLE)
567 if (!stream_out_table_props( editor, pStream, para )) return FALSE;
570 if (prev_para && !memcmp( fmt, &prev_para->fmt, sizeof(*fmt) ))
572 if (fmt->wNumbering)
573 return stream_out_para_num( pStream, para, FALSE );
574 return TRUE;
577 if (!ME_StreamOutPrint(pStream, "\\pard"))
578 return FALSE;
580 if (fmt->wNumbering)
581 if (!stream_out_para_num( pStream, para, TRUE )) return FALSE;
583 if (!editor->bEmulateVersion10) /* v4.1 */
585 if (pStream->nNestingLevel > 0) strcat(props, "\\intbl");
586 if (pStream->nNestingLevel > 1) sprintf(props + strlen(props), "\\itap%d", pStream->nNestingLevel);
588 else /* v1.0 - 3.0 */
590 if (fmt->dwMask & PFM_TABLE && fmt->wEffects & PFE_TABLE)
591 strcat(props, "\\intbl");
594 /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
595 * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
596 * set very different from the documentation.
597 * (Tested with RichEdit 5.50.25.0601) */
599 if (fmt->dwMask & PFM_ALIGNMENT)
601 switch (fmt->wAlignment)
603 case PFA_LEFT:
604 /* Default alignment: not emitted */
605 break;
606 case PFA_RIGHT:
607 strcat(props, "\\qr");
608 break;
609 case PFA_CENTER:
610 strcat(props, "\\qc");
611 break;
612 case PFA_JUSTIFY:
613 strcat(props, "\\qj");
614 break;
618 if (fmt->dwMask & PFM_LINESPACING)
620 /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
621 * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
622 switch (fmt->bLineSpacingRule)
624 case 0: /* Single spacing */
625 strcat(props, "\\sl-240\\slmult1");
626 break;
627 case 1: /* 1.5 spacing */
628 strcat(props, "\\sl-360\\slmult1");
629 break;
630 case 2: /* Double spacing */
631 strcat(props, "\\sl-480\\slmult1");
632 break;
633 case 3:
634 sprintf(props + strlen(props), "\\sl%ld\\slmult0", fmt->dyLineSpacing);
635 break;
636 case 4:
637 sprintf(props + strlen(props), "\\sl-%ld\\slmult0", fmt->dyLineSpacing);
638 break;
639 case 5:
640 sprintf(props + strlen(props), "\\sl-%ld\\slmult1", fmt->dyLineSpacing * 240 / 20);
641 break;
645 if (fmt->dwMask & PFM_DONOTHYPHEN && fmt->wEffects & PFE_DONOTHYPHEN)
646 strcat(props, "\\hyph0");
647 if (fmt->dwMask & PFM_KEEP && fmt->wEffects & PFE_KEEP)
648 strcat(props, "\\keep");
649 if (fmt->dwMask & PFM_KEEPNEXT && fmt->wEffects & PFE_KEEPNEXT)
650 strcat(props, "\\keepn");
651 if (fmt->dwMask & PFM_NOLINENUMBER && fmt->wEffects & PFE_NOLINENUMBER)
652 strcat(props, "\\noline");
653 if (fmt->dwMask & PFM_NOWIDOWCONTROL && fmt->wEffects & PFE_NOWIDOWCONTROL)
654 strcat(props, "\\nowidctlpar");
655 if (fmt->dwMask & PFM_PAGEBREAKBEFORE && fmt->wEffects & PFE_PAGEBREAKBEFORE)
656 strcat(props, "\\pagebb");
657 if (fmt->dwMask & PFM_RTLPARA && fmt->wEffects & PFE_RTLPARA)
658 strcat(props, "\\rtlpar");
659 if (fmt->dwMask & PFM_SIDEBYSIDE && fmt->wEffects & PFE_SIDEBYSIDE)
660 strcat(props, "\\sbys");
662 if (!(editor->bEmulateVersion10 && /* v1.0 - 3.0 */
663 fmt->dwMask & PFM_TABLE && fmt->wEffects & PFE_TABLE))
665 if (fmt->dxOffset)
666 sprintf(props + strlen(props), "\\li%ld", fmt->dxOffset);
667 if (fmt->dxStartIndent)
668 sprintf(props + strlen(props), "\\fi%ld", fmt->dxStartIndent);
669 if (fmt->dxRightIndent)
670 sprintf(props + strlen(props), "\\ri%ld", fmt->dxRightIndent);
671 if (fmt->dwMask & PFM_TABSTOPS) {
672 static const char * const leader[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
674 for (i = 0; i < fmt->cTabCount; i++)
676 switch ((fmt->rgxTabs[i] >> 24) & 0xf)
678 case 1:
679 strcat(props, "\\tqc");
680 break;
681 case 2:
682 strcat(props, "\\tqr");
683 break;
684 case 3:
685 strcat(props, "\\tqdec");
686 break;
687 case 4:
688 /* Word bar tab (vertical bar). Handled below */
689 break;
691 if (fmt->rgxTabs[i] >> 28 <= 5)
692 strcat(props, leader[fmt->rgxTabs[i] >> 28]);
693 sprintf(props+strlen(props), "\\tx%ld", fmt->rgxTabs[i]&0x00FFFFFF);
697 if (fmt->dySpaceAfter)
698 sprintf(props + strlen(props), "\\sa%ld", fmt->dySpaceAfter);
699 if (fmt->dySpaceBefore)
700 sprintf(props + strlen(props), "\\sb%ld", fmt->dySpaceBefore);
701 if (fmt->sStyle != -1)
702 sprintf(props + strlen(props), "\\s%d", fmt->sStyle);
704 if (fmt->dwMask & PFM_SHADING)
706 static const char * const style[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
707 "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
708 "\\bghoriz", "\\bgvert", "\\bgfdiag",
709 "\\bgbdiag", "\\bgcross", "\\bgdcross",
710 "", "", "" };
711 if (fmt->wShadingWeight)
712 sprintf(props + strlen(props), "\\shading%d", fmt->wShadingWeight);
713 if (fmt->wShadingStyle & 0xF)
714 strcat(props, style[fmt->wShadingStyle & 0xF]);
715 if ((fmt->wShadingStyle >> 4) & 0xf)
716 sprintf(props + strlen(props), "\\cfpat%d", (fmt->wShadingStyle >> 4) & 0xf);
717 if ((fmt->wShadingStyle >> 8) & 0xf)
718 sprintf(props + strlen(props), "\\cbpat%d", (fmt->wShadingStyle >> 8) & 0xf);
720 if (*props)
721 strcat(props, " ");
723 if (*props && !ME_StreamOutPrint(pStream, props))
724 return FALSE;
726 return TRUE;
730 static BOOL
731 ME_StreamOutRTFCharProps(ME_OutStream *pStream, CHARFORMAT2W *fmt)
733 char props[STREAMOUT_BUFFER_SIZE] = "";
734 unsigned int i;
735 CHARFORMAT2W *old_fmt = &pStream->cur_fmt;
736 static const struct
738 DWORD effect;
739 const char *on, *off;
740 } effects[] =
742 { CFE_ALLCAPS, "\\caps", "\\caps0" },
743 { CFE_BOLD, "\\b", "\\b0" },
744 { CFE_DISABLED, "\\disabled", "\\disabled0" },
745 { CFE_EMBOSS, "\\embo", "\\embo0" },
746 { CFE_HIDDEN, "\\v", "\\v0" },
747 { CFE_IMPRINT, "\\impr", "\\impr0" },
748 { CFE_ITALIC, "\\i", "\\i0" },
749 { CFE_OUTLINE, "\\outl", "\\outl0" },
750 { CFE_PROTECTED, "\\protect", "\\protect0" },
751 { CFE_SHADOW, "\\shad", "\\shad0" },
752 { CFE_SMALLCAPS, "\\scaps", "\\scaps0" },
753 { CFE_STRIKEOUT, "\\strike", "\\strike0" },
756 for (i = 0; i < ARRAY_SIZE( effects ); i++)
758 if ((old_fmt->dwEffects ^ fmt->dwEffects) & effects[i].effect)
759 strcat( props, fmt->dwEffects & effects[i].effect ? effects[i].on : effects[i].off );
762 if ((old_fmt->dwEffects ^ fmt->dwEffects) & CFE_AUTOBACKCOLOR ||
763 (!(fmt->dwEffects & CFE_AUTOBACKCOLOR) && old_fmt->crBackColor != fmt->crBackColor))
765 if (fmt->dwEffects & CFE_AUTOBACKCOLOR) i = 0;
766 else find_color_in_colortbl( pStream, fmt->crBackColor, &i );
767 sprintf(props + strlen(props), "\\highlight%u", i);
769 if ((old_fmt->dwEffects ^ fmt->dwEffects) & CFE_AUTOCOLOR ||
770 (!(fmt->dwEffects & CFE_AUTOCOLOR) && old_fmt->crTextColor != fmt->crTextColor))
772 if (fmt->dwEffects & CFE_AUTOCOLOR) i = 0;
773 else find_color_in_colortbl( pStream, fmt->crTextColor, &i );
774 sprintf(props + strlen(props), "\\cf%u", i);
777 if (old_fmt->bAnimation != fmt->bAnimation)
778 sprintf(props + strlen(props), "\\animtext%u", fmt->bAnimation);
779 if (old_fmt->wKerning != fmt->wKerning)
780 sprintf(props + strlen(props), "\\kerning%u", fmt->wKerning);
782 if (old_fmt->lcid != fmt->lcid)
784 /* TODO: handle SFF_PLAINRTF */
785 if (LOWORD(fmt->lcid) == 1024)
786 strcat(props, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
787 else
788 sprintf(props + strlen(props), "\\lang%u", LOWORD(fmt->lcid));
791 if (old_fmt->yOffset != fmt->yOffset)
793 if (fmt->yOffset >= 0)
794 sprintf(props + strlen(props), "\\up%ld", fmt->yOffset);
795 else
796 sprintf(props + strlen(props), "\\dn%ld", -fmt->yOffset);
798 if (old_fmt->yHeight != fmt->yHeight)
799 sprintf(props + strlen(props), "\\fs%ld", fmt->yHeight / 10);
800 if (old_fmt->sSpacing != fmt->sSpacing)
801 sprintf(props + strlen(props), "\\expnd%u\\expndtw%u", fmt->sSpacing / 5, fmt->sSpacing);
802 if ((old_fmt->dwEffects ^ fmt->dwEffects) & (CFM_SUBSCRIPT | CFM_SUPERSCRIPT))
804 if (fmt->dwEffects & CFE_SUBSCRIPT)
805 strcat(props, "\\sub");
806 else if (fmt->dwEffects & CFE_SUPERSCRIPT)
807 strcat(props, "\\super");
808 else
809 strcat(props, "\\nosupersub");
811 if ((old_fmt->dwEffects ^ fmt->dwEffects) & CFE_UNDERLINE ||
812 old_fmt->bUnderlineType != fmt->bUnderlineType)
814 BYTE type = (fmt->dwEffects & CFE_UNDERLINE) ? fmt->bUnderlineType : CFU_UNDERLINENONE;
815 switch (type)
817 case CFU_UNDERLINE:
818 strcat(props, "\\ul");
819 break;
820 case CFU_UNDERLINEDOTTED:
821 strcat(props, "\\uld");
822 break;
823 case CFU_UNDERLINEDOUBLE:
824 strcat(props, "\\uldb");
825 break;
826 case CFU_UNDERLINEWORD:
827 strcat(props, "\\ulw");
828 break;
829 case CFU_CF1UNDERLINE:
830 case CFU_UNDERLINENONE:
831 default:
832 strcat(props, "\\ulnone");
833 break;
837 if (wcscmp(old_fmt->szFaceName, fmt->szFaceName) ||
838 old_fmt->bCharSet != fmt->bCharSet)
840 if (find_font_in_fonttbl( pStream, fmt, &i ))
842 sprintf(props + strlen(props), "\\f%u", i);
844 /* In UTF-8 mode, charsets/codepages are not used */
845 if (pStream->nDefaultCodePage != CP_UTF8)
847 if (pStream->fonttbl[i].bCharSet == DEFAULT_CHARSET)
848 pStream->nCodePage = pStream->nDefaultCodePage;
849 else
850 pStream->nCodePage = RTFCharSetToCodePage(NULL, pStream->fonttbl[i].bCharSet);
854 if (*props)
855 strcat(props, " ");
856 if (!ME_StreamOutPrint(pStream, props))
857 return FALSE;
858 *old_fmt = *fmt;
859 return TRUE;
863 static BOOL
864 ME_StreamOutRTFText(ME_OutStream *pStream, const WCHAR *text, LONG nChars)
866 char buffer[STREAMOUT_BUFFER_SIZE];
867 int pos = 0;
868 int fit, nBytes, i;
870 if (nChars == -1)
871 nChars = lstrlenW(text);
873 while (nChars) {
874 /* In UTF-8 mode, font charsets are not used. */
875 if (pStream->nDefaultCodePage == CP_UTF8) {
876 /* 6 is the maximum character length in UTF-8 */
877 fit = min(nChars, STREAMOUT_BUFFER_SIZE / 6);
878 nBytes = WideCharToMultiByte(CP_UTF8, 0, text, fit, buffer,
879 STREAMOUT_BUFFER_SIZE, NULL, NULL);
880 nChars -= fit;
881 text += fit;
882 for (i = 0; i < nBytes; i++)
883 if (buffer[i] == '{' || buffer[i] == '}' || buffer[i] == '\\') {
884 if (!ME_StreamOutPrint(pStream, "%.*s\\", i - pos, buffer + pos))
885 return FALSE;
886 pos = i;
888 if (pos < nBytes)
889 if (!ME_StreamOutMove(pStream, buffer + pos, nBytes - pos))
890 return FALSE;
891 pos = 0;
892 } else if (*text < 128) {
893 if (*text == '{' || *text == '}' || *text == '\\')
894 buffer[pos++] = '\\';
895 buffer[pos++] = (char)(*text++);
896 nChars--;
897 } else {
898 BOOL unknown = FALSE;
899 char letter[3];
901 /* FIXME: In the MS docs for WideCharToMultiByte there is a big list of
902 * codepages including CP_SYMBOL for which the last parameter must be set
903 * to NULL for the function to succeed. But in Wine we need to care only
904 * about CP_SYMBOL */
905 nBytes = WideCharToMultiByte(pStream->nCodePage, 0, text, 1,
906 letter, 3, NULL,
907 (pStream->nCodePage == CP_SYMBOL) ? NULL : &unknown);
908 if (unknown)
909 pos += sprintf(buffer + pos, "\\u%d?", (short)*text);
910 else if ((BYTE)*letter < 128) {
911 if (*letter == '{' || *letter == '}' || *letter == '\\')
912 buffer[pos++] = '\\';
913 buffer[pos++] = *letter;
914 } else {
915 for (i = 0; i < nBytes; i++)
916 pos += sprintf(buffer + pos, "\\'%02x", (BYTE)letter[i]);
918 text++;
919 nChars--;
921 if (pos >= STREAMOUT_BUFFER_SIZE - 11) {
922 if (!ME_StreamOutMove(pStream, buffer, pos))
923 return FALSE;
924 pos = 0;
927 return ME_StreamOutMove(pStream, buffer, pos);
930 static BOOL stream_out_graphics( ME_TextEditor *editor, ME_OutStream *stream,
931 ME_Run *run )
933 IDataObject *data;
934 HRESULT hr;
935 FORMATETC fmt = { CF_ENHMETAFILE, NULL, DVASPECT_CONTENT, -1, TYMED_ENHMF };
936 STGMEDIUM med = { TYMED_NULL };
937 BOOL ret = FALSE;
938 ENHMETAHEADER *emf_bits = NULL;
939 UINT size;
940 SIZE goal, pic;
941 ME_Context c;
942 HDC hdc;
944 hr = IOleObject_QueryInterface( run->reobj->obj.poleobj, &IID_IDataObject, (void **)&data );
945 if (FAILED(hr)) return FALSE;
947 hdc = ITextHost_TxGetDC( editor->texthost );
948 ME_InitContext( &c, editor, hdc );
949 hr = IDataObject_QueryGetData( data, &fmt );
950 if (hr != S_OK) goto done;
952 hr = IDataObject_GetData( data, &fmt, &med );
953 if (FAILED(hr)) goto done;
954 if (med.tymed != TYMED_ENHMF) goto done;
956 size = GetEnhMetaFileBits( med.u.hEnhMetaFile, 0, NULL );
957 if (size < FIELD_OFFSET(ENHMETAHEADER, cbPixelFormat)) goto done;
959 emf_bits = HeapAlloc( GetProcessHeap(), 0, size );
960 if (!emf_bits) goto done;
962 size = GetEnhMetaFileBits( med.u.hEnhMetaFile, size, (BYTE *)emf_bits );
963 if (size < FIELD_OFFSET(ENHMETAHEADER, cbPixelFormat)) goto done;
965 /* size_in_pixels = (frame_size / 100) * szlDevice / szlMillimeters
966 pic = size_in_pixels * 2540 / dpi */
967 pic.cx = MulDiv( emf_bits->rclFrame.right - emf_bits->rclFrame.left, emf_bits->szlDevice.cx * 254,
968 emf_bits->szlMillimeters.cx * c.dpi.cx * 10 );
969 pic.cy = MulDiv( emf_bits->rclFrame.bottom - emf_bits->rclFrame.top, emf_bits->szlDevice.cy * 254,
970 emf_bits->szlMillimeters.cy * c.dpi.cy * 10 );
972 /* convert goal size to twips */
973 goal.cx = MulDiv( run->reobj->obj.sizel.cx, 144, 254 );
974 goal.cy = MulDiv( run->reobj->obj.sizel.cy, 144, 254 );
976 if (!ME_StreamOutPrint( stream, "{\\*\\shppict{\\pict\\emfblip\\picw%d\\pich%d\\picwgoal%d\\pichgoal%d\n",
977 pic.cx, pic.cy, goal.cx, goal.cy ))
978 goto done;
980 if (!ME_StreamOutHexData( stream, (BYTE *)emf_bits, size ))
981 goto done;
983 if (!ME_StreamOutPrint( stream, "}}\n" ))
984 goto done;
986 ret = TRUE;
988 done:
989 ME_DestroyContext( &c );
990 ITextHost_TxReleaseDC( editor->texthost, hdc );
991 HeapFree( GetProcessHeap(), 0, emf_bits );
992 ReleaseStgMedium( &med );
993 IDataObject_Release( data );
994 return ret;
997 static BOOL ME_StreamOutRTF(ME_TextEditor *editor, ME_OutStream *pStream,
998 const ME_Cursor *start, int nChars, int dwFormat)
1000 ME_Cursor cursor = *start;
1001 ME_Paragraph *prev_para = NULL;
1002 ME_Cursor endCur = cursor;
1004 ME_MoveCursorChars(editor, &endCur, nChars, TRUE);
1006 if (!ME_StreamOutRTFHeader(pStream, dwFormat))
1007 return FALSE;
1009 if (!stream_out_font_and_colour_tbls( pStream, cursor.run, endCur.run ))
1010 return FALSE;
1012 /* TODO: stylesheet table */
1014 if (!ME_StreamOutPrint(pStream, "{\\*\\generator Wine Riched20 2.0;}\r\n"))
1015 return FALSE;
1017 /* TODO: information group */
1019 /* TODO: document formatting properties */
1021 /* FIXME: We have only one document section */
1023 /* TODO: section formatting properties */
1027 if (cursor.para != prev_para)
1029 prev_para = cursor.para;
1030 if (!stream_out_para_props( editor, pStream, cursor.para ))
1031 return FALSE;
1034 if (cursor.run == endCur.run && !endCur.nOffset)
1035 break;
1037 TRACE("flags %xh\n", cursor.run->nFlags);
1038 /* TODO: emit embedded objects */
1039 if (cursor.para->nFlags & (MEPF_ROWSTART | MEPF_ROWEND))
1040 continue;
1041 if (cursor.run->nFlags & MERF_GRAPHICS)
1043 if (!stream_out_graphics( editor, pStream, cursor.run ))
1044 return FALSE;
1046 else if (cursor.run->nFlags & MERF_TAB)
1048 if (editor->bEmulateVersion10 && /* v1.0 - 3.0 */
1049 para_in_table( cursor.para ))
1051 if (!ME_StreamOutPrint(pStream, "\\cell "))
1052 return FALSE;
1054 else
1056 if (!ME_StreamOutPrint(pStream, "\\tab "))
1057 return FALSE;
1060 else if (cursor.run->nFlags & MERF_ENDCELL)
1062 if (pStream->nNestingLevel > 1)
1064 if (!ME_StreamOutPrint(pStream, "\\nestcell "))
1065 return FALSE;
1067 else
1069 if (!ME_StreamOutPrint(pStream, "\\cell "))
1070 return FALSE;
1072 nChars--;
1074 else if (cursor.run->nFlags & MERF_ENDPARA)
1076 if (!ME_StreamOutRTFCharProps( pStream, &cursor.run->style->fmt ))
1077 return FALSE;
1079 if (para_in_table( cursor.para ) &&
1080 !(cursor.para->nFlags & (MEPF_ROWSTART | MEPF_ROWEND | MEPF_CELL)))
1082 if (!ME_StreamOutPrint(pStream, "\\row\r\n"))
1083 return FALSE;
1085 else
1087 if (!ME_StreamOutPrint(pStream, "\\par\r\n"))
1088 return FALSE;
1090 /* Skip as many characters as required by current line break */
1091 nChars = max(0, nChars - cursor.run->len);
1093 else if (cursor.run->nFlags & MERF_ENDROW)
1095 if (!ME_StreamOutPrint(pStream, "\\line\r\n"))
1096 return FALSE;
1097 nChars--;
1099 else
1101 int nEnd;
1103 TRACE("style %p\n", cursor.run->style);
1104 if (!ME_StreamOutRTFCharProps( pStream, &cursor.run->style->fmt ))
1105 return FALSE;
1107 nEnd = (cursor.run == endCur.run) ? endCur.nOffset : cursor.run->len;
1108 if (!ME_StreamOutRTFText(pStream, get_text( cursor.run, cursor.nOffset ),
1109 nEnd - cursor.nOffset))
1110 return FALSE;
1111 cursor.nOffset = 0;
1113 } while (cursor.run != endCur.run && cursor_next_run( &cursor, TRUE ));
1115 if (!ME_StreamOutMove(pStream, "}\0", 2))
1116 return FALSE;
1117 return TRUE;
1121 static BOOL ME_StreamOutText(ME_TextEditor *editor, ME_OutStream *pStream,
1122 const ME_Cursor *start, int nChars, DWORD dwFormat)
1124 ME_Cursor cursor = *start;
1125 int nLen;
1126 UINT nCodePage = CP_ACP;
1127 char *buffer = NULL;
1128 int nBufLen = 0;
1129 BOOL success = TRUE;
1131 if (!cursor.run)
1132 return FALSE;
1134 if (dwFormat & SF_USECODEPAGE)
1135 nCodePage = HIWORD(dwFormat);
1137 /* TODO: Handle SF_TEXTIZED */
1139 while (success && nChars && cursor.run)
1141 nLen = min(nChars, cursor.run->len - cursor.nOffset);
1143 if (!editor->bEmulateVersion10 && cursor.run->nFlags & MERF_ENDPARA)
1145 /* richedit 2.0 - all line breaks are \r\n */
1146 if (dwFormat & SF_UNICODE)
1147 success = ME_StreamOutMove(pStream, (const char *)L"\r\n", 2 * sizeof(WCHAR));
1148 else
1149 success = ME_StreamOutMove(pStream, "\r\n", 2);
1150 } else {
1151 if (dwFormat & SF_UNICODE)
1152 success = ME_StreamOutMove(pStream, (const char *)(get_text( cursor.run, cursor.nOffset )),
1153 sizeof(WCHAR) * nLen);
1154 else {
1155 int nSize;
1157 nSize = WideCharToMultiByte(nCodePage, 0, get_text( cursor.run, cursor.nOffset ),
1158 nLen, NULL, 0, NULL, NULL);
1159 if (nSize > nBufLen) {
1160 buffer = heap_realloc(buffer, nSize);
1161 nBufLen = nSize;
1163 WideCharToMultiByte(nCodePage, 0, get_text( cursor.run, cursor.nOffset ),
1164 nLen, buffer, nSize, NULL, NULL);
1165 success = ME_StreamOutMove(pStream, buffer, nSize);
1169 nChars -= nLen;
1170 cursor.nOffset = 0;
1171 cursor.run = run_next_all_paras( cursor.run );
1174 heap_free(buffer);
1175 return success;
1179 LRESULT ME_StreamOutRange(ME_TextEditor *editor, DWORD dwFormat,
1180 const ME_Cursor *start,
1181 int nChars, EDITSTREAM *stream)
1183 ME_OutStream *pStream = ME_StreamOutInit(editor, stream);
1185 if (dwFormat & SF_RTF)
1186 ME_StreamOutRTF(editor, pStream, start, nChars, dwFormat);
1187 else if (dwFormat & SF_TEXT || dwFormat & SF_TEXTIZED)
1188 ME_StreamOutText(editor, pStream, start, nChars, dwFormat);
1189 if (!pStream->stream->dwError)
1190 ME_StreamOutFlush(pStream);
1191 return ME_StreamOutFree(pStream);
1194 LRESULT
1195 ME_StreamOut(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream)
1197 ME_Cursor start;
1198 int nChars;
1200 if (dwFormat & SFF_SELECTION) {
1201 LONG nStart, nTo;
1202 start = editor->pCursors[ME_GetSelectionOfs(editor, &nStart, &nTo)];
1203 nChars = nTo - nStart;
1204 } else {
1205 ME_SetCursorToStart(editor, &start);
1206 nChars = ME_GetTextLength(editor);
1207 /* Generate an end-of-paragraph at the end of SCF_ALL RTF output */
1208 if (dwFormat & SF_RTF)
1209 nChars++;
1211 return ME_StreamOutRange(editor, dwFormat, &start, nChars, stream);