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
22 #include "wine/port.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
31 ME_StreamOutRTFText(ME_OutStream
*pStream
, const WCHAR
*text
, LONG nChars
);
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;
42 pStream
->nFontTblLen
= 0;
43 pStream
->nColorTblLen
= 1;
44 pStream
->nNestingLevel
= 0;
50 ME_StreamOutFlush(ME_OutStream
*pStream
)
55 EDITSTREAM
*stream
= pStream
->stream
;
58 TRACE("sending %u bytes\n", pStream
->pos
- nStart
);
59 /* Some apps seem not to set *pcb unless a problem arises, relying
60 on initial random nWritten value, which is usually >STREAMOUT_BUFFER_SIZE */
61 nRemaining
= pStream
->pos
- nStart
;
62 nWritten
= 0xDEADBEEF;
63 stream
->dwError
= stream
->pfnCallback(stream
->dwCookie
, (LPBYTE
)pStream
->buffer
+ nStart
,
64 pStream
->pos
- nStart
, &nWritten
);
65 TRACE("error=%u written=%u\n", stream
->dwError
, nWritten
);
66 if (nWritten
> (pStream
->pos
- nStart
) || nWritten
<0) {
67 FIXME("Invalid returned written size *pcb: 0x%x (%d) instead of %d\n",
68 (unsigned)nWritten
, nWritten
, nRemaining
);
69 nWritten
= nRemaining
;
71 if (nWritten
== 0 || stream
->dwError
)
73 pStream
->written
+= nWritten
;
75 } while (nStart
< pStream
->pos
);
82 ME_StreamOutFree(ME_OutStream
*pStream
)
84 LONG written
= pStream
->written
;
85 TRACE("total length = %u\n", written
);
93 ME_StreamOutMove(ME_OutStream
*pStream
, const char *buffer
, int len
)
96 int space
= STREAMOUT_BUFFER_SIZE
- pStream
->pos
;
97 int fit
= min(space
, len
);
99 TRACE("%u:%u:%s\n", pStream
->pos
, fit
, debugstr_an(buffer
,fit
));
100 memmove(pStream
->buffer
+ pStream
->pos
, buffer
, fit
);
104 if (pStream
->pos
== STREAMOUT_BUFFER_SIZE
) {
105 if (!ME_StreamOutFlush(pStream
))
114 ME_StreamOutPrint(ME_OutStream
*pStream
, const char *format
, ...)
116 char string
[STREAMOUT_BUFFER_SIZE
]; /* This is going to be enough */
120 va_start(valist
, format
);
121 len
= vsnprintf(string
, sizeof(string
), format
, valist
);
124 return ME_StreamOutMove(pStream
, string
, len
);
129 ME_StreamOutRTFHeader(ME_OutStream
*pStream
, int dwFormat
)
131 const char *cCharSet
= NULL
;
136 if (dwFormat
& SF_USECODEPAGE
) {
139 switch (HIWORD(dwFormat
)) {
142 nCodePage
= GetACP();
145 nCodePage
= GetOEMCP();
146 if (nCodePage
== 437)
148 else if (nCodePage
== 850)
157 if (HIWORD(dwFormat
) == CP_MACCP
) {
159 nCodePage
= 10000; /* MacRoman */
162 nCodePage
= 1252; /* Latin-1 */
164 if (GetCPInfoExW(HIWORD(dwFormat
), 0, &info
))
165 nCodePage
= info
.CodePage
;
169 /* TODO: If the original document contained an \ansicpg value, retain it.
170 * Otherwise, M$ richedit emits a codepage number determined from the
171 * charset of the default font here. Anyway, this value is not used by
173 nCodePage
= GetACP();
175 if (nCodePage
== CP_UTF8
)
176 success
= ME_StreamOutPrint(pStream
, "{\\urtf");
178 success
= ME_StreamOutPrint(pStream
, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet
, nCodePage
);
183 pStream
->nDefaultCodePage
= nCodePage
;
185 /* FIXME: This should be a document property */
186 /* TODO: handle SFF_PLAINRTF */
187 language
= GetUserDefaultLangID();
188 if (!ME_StreamOutPrint(pStream
, "\\deff0\\deflang%u\\deflangfe%u", language
, language
))
191 /* FIXME: This should be a document property */
192 pStream
->nDefaultFont
= 0;
199 ME_StreamOutRTFFontAndColorTbl(ME_OutStream
*pStream
, ME_DisplayItem
*pFirstRun
, const ME_DisplayItem
*pLastRun
)
201 ME_DisplayItem
*item
= pFirstRun
;
202 ME_FontTableItem
*table
= pStream
->fonttbl
;
206 CHARFORMAT2W
*fmt
= &item
->member
.run
.style
->fmt
;
209 if (fmt
->dwMask
& CFM_FACE
) {
210 WCHAR
*face
= fmt
->szFaceName
;
211 BYTE bCharSet
= (fmt
->dwMask
& CFM_CHARSET
) ? fmt
->bCharSet
: DEFAULT_CHARSET
;
213 for (i
= 0; i
< pStream
->nFontTblLen
; i
++)
214 if (table
[i
].bCharSet
== bCharSet
215 && (table
[i
].szFaceName
== face
|| !lstrcmpW(table
[i
].szFaceName
, face
)))
217 if (i
== pStream
->nFontTblLen
) {
218 table
[i
].bCharSet
= bCharSet
;
219 table
[i
].szFaceName
= face
;
220 pStream
->nFontTblLen
++;
224 if (fmt
->dwMask
& CFM_COLOR
&& !(fmt
->dwEffects
& CFE_AUTOCOLOR
)) {
225 crColor
= fmt
->crTextColor
;
226 for (i
= 1; i
< pStream
->nColorTblLen
; i
++)
227 if (pStream
->colortbl
[i
] == crColor
)
229 if (i
== pStream
->nColorTblLen
) {
230 pStream
->colortbl
[i
] = crColor
;
231 pStream
->nColorTblLen
++;
234 if (fmt
->dwMask
& CFM_BACKCOLOR
&& !(fmt
->dwEffects
& CFE_AUTOBACKCOLOR
)) {
235 crColor
= fmt
->crBackColor
;
236 for (i
= 1; i
< pStream
->nColorTblLen
; i
++)
237 if (pStream
->colortbl
[i
] == crColor
)
239 if (i
== pStream
->nColorTblLen
) {
240 pStream
->colortbl
[i
] = crColor
;
241 pStream
->nColorTblLen
++;
245 if (item
== pLastRun
)
247 item
= ME_FindItemFwd(item
, diRun
);
250 if (!ME_StreamOutPrint(pStream
, "{\\fonttbl"))
253 for (i
= 0; i
< pStream
->nFontTblLen
; i
++) {
254 if (table
[i
].bCharSet
!= DEFAULT_CHARSET
) {
255 if (!ME_StreamOutPrint(pStream
, "{\\f%u\\fcharset%u ", i
, table
[i
].bCharSet
))
258 if (!ME_StreamOutPrint(pStream
, "{\\f%u ", i
))
261 if (!ME_StreamOutRTFText(pStream
, table
[i
].szFaceName
, -1))
263 if (!ME_StreamOutPrint(pStream
, ";}\r\n"))
266 if (!ME_StreamOutPrint(pStream
, "}"))
269 /* Output colors table if not empty */
270 if (pStream
->nColorTblLen
> 1) {
271 if (!ME_StreamOutPrint(pStream
, "{\\colortbl;"))
273 for (i
= 1; i
< pStream
->nColorTblLen
; i
++) {
274 if (!ME_StreamOutPrint(pStream
, "\\red%u\\green%u\\blue%u;",
275 pStream
->colortbl
[i
] & 0xFF,
276 (pStream
->colortbl
[i
] >> 8) & 0xFF,
277 (pStream
->colortbl
[i
] >> 16) & 0xFF))
280 if (!ME_StreamOutPrint(pStream
, "}"))
288 ME_StreamOutRTFTableProps(ME_TextEditor
*editor
, ME_OutStream
*pStream
,
289 const ME_DisplayItem
*para
)
291 ME_DisplayItem
*cell
;
292 char props
[STREAMOUT_BUFFER_SIZE
] = "";
294 if (!ME_StreamOutPrint(pStream
, "\\trowd"))
296 if (!editor
->bEmulateVersion10
) { /* v4.1 */
297 assert(para
->member
.para
.nFlags
& MEPF_ROWSTART
);
298 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
301 sprintf(props
+ strlen(props
), "\\cellx%d", cell
->member
.cell
.nRightBoundary
);
302 cell
= cell
->member
.cell
.next_cell
;
303 } while (cell
->member
.cell
.next_cell
);
304 } else { /* v1.0 - 3.0 */
305 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
308 assert(!(para
->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
|MEPF_CELL
)));
310 sprintf(props
+ strlen(props
), "\\trgaph%d", pFmt
->dxOffset
);
311 if (pFmt
->dxStartIndent
)
312 sprintf(props
+ strlen(props
), "\\trleft%d", pFmt
->dxStartIndent
);
313 for (i
= 0; i
< pFmt
->cTabCount
; i
++)
315 sprintf(props
+ strlen(props
), "\\cellx%d", pFmt
->rgxTabs
[i
] & 0x00FFFFFF);
318 if (!ME_StreamOutPrint(pStream
, props
))
325 ME_StreamOutRTFParaProps(ME_TextEditor
*editor
, ME_OutStream
*pStream
,
326 const ME_DisplayItem
*para
)
328 PARAFORMAT2
*fmt
= para
->member
.para
.pFmt
;
329 char props
[STREAMOUT_BUFFER_SIZE
] = "";
332 /* TODO: Don't emit anything if the last PARAFORMAT2 is inherited */
333 if (!ME_StreamOutPrint(pStream
, "\\pard"))
336 if (!editor
->bEmulateVersion10
) { /* v4.1 */
337 if (pStream
->nNestingLevel
> 0)
338 strcat(props
, "\\intbl");
339 if (pStream
->nNestingLevel
> 1)
340 sprintf(props
+ strlen(props
), "\\itap%d", pStream
->nNestingLevel
);
341 } else { /* v1.0 - 3.0 */
342 if (fmt
->dwMask
& PFM_TABLE
&& fmt
->wEffects
& PFE_TABLE
)
343 strcat(props
, "\\intbl");
346 /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
347 * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
348 * set very different from the documentation.
349 * (Tested with RichEdit 5.50.25.0601) */
351 if (fmt
->dwMask
& PFM_ALIGNMENT
) {
352 switch (fmt
->wAlignment
) {
354 /* Default alignment: not emitted */
357 strcat(props
, "\\qr");
360 strcat(props
, "\\qc");
363 strcat(props
, "\\qj");
368 if (fmt
->dwMask
& PFM_LINESPACING
) {
369 /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
370 * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
371 switch (fmt
->bLineSpacingRule
) {
372 case 0: /* Single spacing */
373 strcat(props
, "\\sl-240\\slmult1");
375 case 1: /* 1.5 spacing */
376 strcat(props
, "\\sl-360\\slmult1");
378 case 2: /* Double spacing */
379 strcat(props
, "\\sl-480\\slmult1");
382 sprintf(props
+ strlen(props
), "\\sl%d\\slmult0", fmt
->dyLineSpacing
);
385 sprintf(props
+ strlen(props
), "\\sl-%d\\slmult0", fmt
->dyLineSpacing
);
388 sprintf(props
+ strlen(props
), "\\sl-%d\\slmult1", fmt
->dyLineSpacing
* 240 / 20);
393 if (fmt
->dwMask
& PFM_DONOTHYPHEN
&& fmt
->wEffects
& PFE_DONOTHYPHEN
)
394 strcat(props
, "\\hyph0");
395 if (fmt
->dwMask
& PFM_KEEP
&& fmt
->wEffects
& PFE_KEEP
)
396 strcat(props
, "\\keep");
397 if (fmt
->dwMask
& PFM_KEEPNEXT
&& fmt
->wEffects
& PFE_KEEPNEXT
)
398 strcat(props
, "\\keepn");
399 if (fmt
->dwMask
& PFM_NOLINENUMBER
&& fmt
->wEffects
& PFE_NOLINENUMBER
)
400 strcat(props
, "\\noline");
401 if (fmt
->dwMask
& PFM_NOWIDOWCONTROL
&& fmt
->wEffects
& PFE_NOWIDOWCONTROL
)
402 strcat(props
, "\\nowidctlpar");
403 if (fmt
->dwMask
& PFM_PAGEBREAKBEFORE
&& fmt
->wEffects
& PFE_PAGEBREAKBEFORE
)
404 strcat(props
, "\\pagebb");
405 if (fmt
->dwMask
& PFM_RTLPARA
&& fmt
->wEffects
& PFE_RTLPARA
)
406 strcat(props
, "\\rtlpar");
407 if (fmt
->dwMask
& PFM_SIDEBYSIDE
&& fmt
->wEffects
& PFE_SIDEBYSIDE
)
408 strcat(props
, "\\sbys");
410 if (!(editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
411 fmt
->dwMask
& PFM_TABLE
&& fmt
->wEffects
& PFE_TABLE
))
413 if (fmt
->dwMask
& PFM_OFFSET
)
414 sprintf(props
+ strlen(props
), "\\li%d", fmt
->dxOffset
);
415 if (fmt
->dwMask
& PFM_OFFSETINDENT
|| fmt
->dwMask
& PFM_STARTINDENT
)
416 sprintf(props
+ strlen(props
), "\\fi%d", fmt
->dxStartIndent
);
417 if (fmt
->dwMask
& PFM_RIGHTINDENT
)
418 sprintf(props
+ strlen(props
), "\\ri%d", fmt
->dxRightIndent
);
419 if (fmt
->dwMask
& PFM_TABSTOPS
) {
420 static const char * const leader
[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
422 for (i
= 0; i
< fmt
->cTabCount
; i
++) {
423 switch ((fmt
->rgxTabs
[i
] >> 24) & 0xF) {
425 strcat(props
, "\\tqc");
428 strcat(props
, "\\tqr");
431 strcat(props
, "\\tqdec");
434 /* Word bar tab (vertical bar). Handled below */
437 if (fmt
->rgxTabs
[i
] >> 28 <= 5)
438 strcat(props
, leader
[fmt
->rgxTabs
[i
] >> 28]);
439 sprintf(props
+strlen(props
), "\\tx%d", fmt
->rgxTabs
[i
]&0x00FFFFFF);
443 if (fmt
->dwMask
& PFM_SPACEAFTER
)
444 sprintf(props
+ strlen(props
), "\\sa%d", fmt
->dySpaceAfter
);
445 if (fmt
->dwMask
& PFM_SPACEBEFORE
)
446 sprintf(props
+ strlen(props
), "\\sb%d", fmt
->dySpaceBefore
);
447 if (fmt
->dwMask
& PFM_STYLE
)
448 sprintf(props
+ strlen(props
), "\\s%d", fmt
->sStyle
);
450 if (fmt
->dwMask
& PFM_SHADING
) {
451 static const char * const style
[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
452 "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
453 "\\bghoriz", "\\bgvert", "\\bgfdiag",
454 "\\bgbdiag", "\\bgcross", "\\bgdcross",
456 if (fmt
->wShadingWeight
)
457 sprintf(props
+ strlen(props
), "\\shading%d", fmt
->wShadingWeight
);
458 if (fmt
->wShadingStyle
& 0xF)
459 strcat(props
, style
[fmt
->wShadingStyle
& 0xF]);
460 sprintf(props
+ strlen(props
), "\\cfpat%d\\cbpat%d",
461 (fmt
->wShadingStyle
>> 4) & 0xF, (fmt
->wShadingStyle
>> 8) & 0xF);
464 if (*props
&& !ME_StreamOutPrint(pStream
, props
))
472 ME_StreamOutRTFCharProps(ME_OutStream
*pStream
, CHARFORMAT2W
*fmt
)
474 char props
[STREAMOUT_BUFFER_SIZE
] = "";
477 if (fmt
->dwMask
& CFM_ALLCAPS
&& fmt
->dwEffects
& CFE_ALLCAPS
)
478 strcat(props
, "\\caps");
479 if (fmt
->dwMask
& CFM_ANIMATION
)
480 sprintf(props
+ strlen(props
), "\\animtext%u", fmt
->bAnimation
);
481 if (fmt
->dwMask
& CFM_BACKCOLOR
) {
482 if (!(fmt
->dwEffects
& CFE_AUTOBACKCOLOR
)) {
483 for (i
= 1; i
< pStream
->nColorTblLen
; i
++)
484 if (pStream
->colortbl
[i
] == fmt
->crBackColor
) {
485 sprintf(props
+ strlen(props
), "\\cb%u", i
);
490 if (fmt
->dwMask
& CFM_BOLD
&& fmt
->dwEffects
& CFE_BOLD
)
491 strcat(props
, "\\b");
492 if (fmt
->dwMask
& CFM_COLOR
) {
493 if (!(fmt
->dwEffects
& CFE_AUTOCOLOR
)) {
494 for (i
= 1; i
< pStream
->nColorTblLen
; i
++)
495 if (pStream
->colortbl
[i
] == fmt
->crTextColor
) {
496 sprintf(props
+ strlen(props
), "\\cf%u", i
);
501 /* TODO: CFM_DISABLED */
502 if (fmt
->dwMask
& CFM_EMBOSS
&& fmt
->dwEffects
& CFE_EMBOSS
)
503 strcat(props
, "\\embo");
504 if (fmt
->dwMask
& CFM_HIDDEN
&& fmt
->dwEffects
& CFE_HIDDEN
)
505 strcat(props
, "\\v");
506 if (fmt
->dwMask
& CFM_IMPRINT
&& fmt
->dwEffects
& CFE_IMPRINT
)
507 strcat(props
, "\\impr");
508 if (fmt
->dwMask
& CFM_ITALIC
&& fmt
->dwEffects
& CFE_ITALIC
)
509 strcat(props
, "\\i");
510 if (fmt
->dwMask
& CFM_KERNING
)
511 sprintf(props
+ strlen(props
), "\\kerning%u", fmt
->wKerning
);
512 if (fmt
->dwMask
& CFM_LCID
) {
513 /* TODO: handle SFF_PLAINRTF */
514 if (LOWORD(fmt
->lcid
) == 1024)
515 strcat(props
, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
517 sprintf(props
+ strlen(props
), "\\lang%u", LOWORD(fmt
->lcid
));
519 /* CFM_LINK is not streamed out by M$ */
520 if (fmt
->dwMask
& CFM_OFFSET
) {
521 if (fmt
->yOffset
>= 0)
522 sprintf(props
+ strlen(props
), "\\up%d", fmt
->yOffset
);
524 sprintf(props
+ strlen(props
), "\\dn%d", -fmt
->yOffset
);
526 if (fmt
->dwMask
& CFM_OUTLINE
&& fmt
->dwEffects
& CFE_OUTLINE
)
527 strcat(props
, "\\outl");
528 if (fmt
->dwMask
& CFM_PROTECTED
&& fmt
->dwEffects
& CFE_PROTECTED
)
529 strcat(props
, "\\protect");
530 /* TODO: CFM_REVISED CFM_REVAUTHOR - probably using rsidtbl? */
531 if (fmt
->dwMask
& CFM_SHADOW
&& fmt
->dwEffects
& CFE_SHADOW
)
532 strcat(props
, "\\shad");
533 if (fmt
->dwMask
& CFM_SIZE
)
534 sprintf(props
+ strlen(props
), "\\fs%d", fmt
->yHeight
/ 10);
535 if (fmt
->dwMask
& CFM_SMALLCAPS
&& fmt
->dwEffects
& CFE_SMALLCAPS
)
536 strcat(props
, "\\scaps");
537 if (fmt
->dwMask
& CFM_SPACING
)
538 sprintf(props
+ strlen(props
), "\\expnd%u\\expndtw%u", fmt
->sSpacing
/ 5, fmt
->sSpacing
);
539 if (fmt
->dwMask
& CFM_STRIKEOUT
&& fmt
->dwEffects
& CFE_STRIKEOUT
)
540 strcat(props
, "\\strike");
541 if (fmt
->dwMask
& CFM_STYLE
) {
542 sprintf(props
+ strlen(props
), "\\cs%u", fmt
->sStyle
);
543 /* TODO: emit style contents here */
545 if (fmt
->dwMask
& (CFM_SUBSCRIPT
| CFM_SUPERSCRIPT
)) {
546 if (fmt
->dwEffects
& CFE_SUBSCRIPT
)
547 strcat(props
, "\\sub");
548 else if (fmt
->dwEffects
& CFE_SUPERSCRIPT
)
549 strcat(props
, "\\super");
551 if (fmt
->dwMask
& CFM_UNDERLINE
|| fmt
->dwMask
& CFM_UNDERLINETYPE
) {
552 if (fmt
->dwMask
& CFM_UNDERLINETYPE
)
553 switch (fmt
->bUnderlineType
) {
554 case CFU_CF1UNDERLINE
:
556 strcat(props
, "\\ul");
558 case CFU_UNDERLINEDOTTED
:
559 strcat(props
, "\\uld");
561 case CFU_UNDERLINEDOUBLE
:
562 strcat(props
, "\\uldb");
564 case CFU_UNDERLINEWORD
:
565 strcat(props
, "\\ulw");
567 case CFU_UNDERLINENONE
:
569 strcat(props
, "\\ul0");
572 else if (fmt
->dwEffects
& CFE_UNDERLINE
)
573 strcat(props
, "\\ul");
575 /* FIXME: How to emit CFM_WEIGHT? */
577 if (fmt
->dwMask
& CFM_FACE
|| fmt
->dwMask
& CFM_CHARSET
) {
580 if (fmt
->dwMask
& CFM_FACE
)
581 szFaceName
= fmt
->szFaceName
;
583 szFaceName
= pStream
->fonttbl
[0].szFaceName
;
584 for (i
= 0; i
< pStream
->nFontTblLen
; i
++) {
585 if (szFaceName
== pStream
->fonttbl
[i
].szFaceName
586 || !lstrcmpW(szFaceName
, pStream
->fonttbl
[i
].szFaceName
))
587 if (!(fmt
->dwMask
& CFM_CHARSET
)
588 || fmt
->bCharSet
== pStream
->fonttbl
[i
].bCharSet
)
591 if (i
< pStream
->nFontTblLen
)
593 if (i
!= pStream
->nDefaultFont
)
594 sprintf(props
+ strlen(props
), "\\f%u", i
);
596 /* In UTF-8 mode, charsets/codepages are not used */
597 if (pStream
->nDefaultCodePage
!= CP_UTF8
)
599 if (pStream
->fonttbl
[i
].bCharSet
== DEFAULT_CHARSET
)
600 pStream
->nCodePage
= pStream
->nDefaultCodePage
;
602 pStream
->nCodePage
= RTFCharSetToCodePage(NULL
, pStream
->fonttbl
[i
].bCharSet
);
608 if (!ME_StreamOutPrint(pStream
, props
))
615 ME_StreamOutRTFText(ME_OutStream
*pStream
, const WCHAR
*text
, LONG nChars
)
617 char buffer
[STREAMOUT_BUFFER_SIZE
];
622 nChars
= lstrlenW(text
);
625 /* In UTF-8 mode, font charsets are not used. */
626 if (pStream
->nDefaultCodePage
== CP_UTF8
) {
627 /* 6 is the maximum character length in UTF-8 */
628 fit
= min(nChars
, STREAMOUT_BUFFER_SIZE
/ 6);
629 nBytes
= WideCharToMultiByte(CP_UTF8
, 0, text
, fit
, buffer
,
630 STREAMOUT_BUFFER_SIZE
, NULL
, NULL
);
633 for (i
= 0; i
< nBytes
; i
++)
634 if (buffer
[i
] == '{' || buffer
[i
] == '}' || buffer
[i
] == '\\') {
635 if (!ME_StreamOutPrint(pStream
, "%.*s\\", i
- pos
, buffer
+ pos
))
640 if (!ME_StreamOutMove(pStream
, buffer
+ pos
, nBytes
- pos
))
643 } else if (*text
< 128) {
644 if (*text
== '{' || *text
== '}' || *text
== '\\')
645 buffer
[pos
++] = '\\';
646 buffer
[pos
++] = (char)(*text
++);
649 BOOL unknown
= FALSE
;
652 /* FIXME: In the MS docs for WideCharToMultiByte there is a big list of
653 * codepages including CP_SYMBOL for which the last parameter must be set
654 * to NULL for the function to succeed. But in Wine we need to care only
656 nBytes
= WideCharToMultiByte(pStream
->nCodePage
, 0, text
, 1,
658 (pStream
->nCodePage
== CP_SYMBOL
) ? NULL
: &unknown
);
660 pos
+= sprintf(buffer
+ pos
, "\\u%d?", (short)*text
);
661 else if ((BYTE
)*letter
< 128) {
662 if (*letter
== '{' || *letter
== '}' || *letter
== '\\')
663 buffer
[pos
++] = '\\';
664 buffer
[pos
++] = *letter
;
666 for (i
= 0; i
< nBytes
; i
++)
667 pos
+= sprintf(buffer
+ pos
, "\\'%02x", (BYTE
)letter
[i
]);
672 if (pos
>= STREAMOUT_BUFFER_SIZE
- 11) {
673 if (!ME_StreamOutMove(pStream
, buffer
, pos
))
678 return ME_StreamOutMove(pStream
, buffer
, pos
);
683 ME_StreamOutRTF(ME_TextEditor
*editor
, ME_OutStream
*pStream
, int nStart
, int nChars
, int dwFormat
)
685 ME_DisplayItem
*p
, *pEnd
, *pPara
;
686 int nOffset
, nEndLen
;
688 ME_RunOfsFromCharOfs(editor
, nStart
, &p
, &nOffset
);
689 ME_RunOfsFromCharOfs(editor
, nStart
+nChars
, &pEnd
, &nEndLen
);
691 pPara
= ME_GetParagraph(p
);
693 if (!ME_StreamOutRTFHeader(pStream
, dwFormat
))
696 if (!ME_StreamOutRTFFontAndColorTbl(pStream
, p
, pEnd
))
699 /* TODO: stylesheet table */
701 /* FIXME: maybe emit something smarter for the generator? */
702 if (!ME_StreamOutPrint(pStream
, "{\\*\\generator Wine Riched20 2.0.????;}"))
705 /* TODO: information group */
707 /* TODO: document formatting properties */
709 /* FIXME: We have only one document section */
711 /* TODO: section formatting properties */
713 if (!ME_StreamOutRTFParaProps(editor
, pStream
, ME_GetParagraph(p
)))
721 if (!editor
->bEmulateVersion10
) { /* v4.1 */
722 if (p
->member
.para
.nFlags
& MEPF_ROWSTART
) {
723 pStream
->nNestingLevel
++;
724 if (pStream
->nNestingLevel
== 1) {
725 if (!ME_StreamOutRTFTableProps(editor
, pStream
, p
))
728 } else if (p
->member
.para
.nFlags
& MEPF_ROWEND
) {
729 pStream
->nNestingLevel
--;
730 if (pStream
->nNestingLevel
> 1) {
731 if (!ME_StreamOutPrint(pStream
, "{\\*\\nesttableprops"))
733 if (!ME_StreamOutRTFTableProps(editor
, pStream
, p
))
735 if (!ME_StreamOutPrint(pStream
, "\\nestrow}{\\nonesttables\\par}\r\n"))
738 if (!ME_StreamOutPrint(pStream
, "\\row \r\n"))
741 } else if (!ME_StreamOutRTFParaProps(editor
, pStream
, p
)) {
744 } else { /* v1.0 - 3.0 */
745 if (p
->member
.para
.pFmt
->dwMask
& PFM_TABLE
&&
746 p
->member
.para
.pFmt
->wEffects
& PFE_TABLE
)
748 if (!ME_StreamOutRTFTableProps(editor
, pStream
, p
))
751 if (!ME_StreamOutRTFParaProps(editor
, pStream
, p
))
757 if (p
== pEnd
&& !nEndLen
)
759 TRACE("flags %xh\n", p
->member
.run
.nFlags
);
760 /* TODO: emit embedded objects */
761 if (pPara
->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
))
763 if (p
->member
.run
.nFlags
& MERF_GRAPHICS
) {
764 FIXME("embedded objects are not handled\n");
765 } else if (p
->member
.run
.nFlags
& MERF_TAB
) {
766 if (editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
767 pPara
->member
.para
.pFmt
->dwMask
& PFM_TABLE
&&
768 pPara
->member
.para
.pFmt
->wEffects
& PFE_TABLE
)
770 if (!ME_StreamOutPrint(pStream
, "\\cell "))
773 if (!ME_StreamOutPrint(pStream
, "\\tab "))
776 } else if (p
->member
.run
.nFlags
& MERF_ENDCELL
) {
777 if (pStream
->nNestingLevel
> 1) {
778 if (!ME_StreamOutPrint(pStream
, "\\nestcell "))
781 if (!ME_StreamOutPrint(pStream
, "\\cell "))
785 } else if (p
->member
.run
.nFlags
& MERF_ENDPARA
) {
786 if (pPara
->member
.para
.pFmt
->dwMask
& PFM_TABLE
&&
787 pPara
->member
.para
.pFmt
->wEffects
& PFE_TABLE
&&
788 !(pPara
->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
|MEPF_CELL
)))
790 if (!ME_StreamOutPrint(pStream
, "\\row \r\n"))
793 if (!ME_StreamOutPrint(pStream
, "\r\n\\par"))
796 /* Skip as many characters as required by current line break */
797 nChars
-= (p
->member
.run
.nCR
<= nChars
) ? p
->member
.run
.nCR
: nChars
;
798 nChars
-= (p
->member
.run
.nLF
<= nChars
) ? p
->member
.run
.nLF
: nChars
;
799 } else if (p
->member
.run
.nFlags
& MERF_ENDROW
) {
800 if (!ME_StreamOutPrint(pStream
, "\\line \r\n"))
806 if (!ME_StreamOutPrint(pStream
, "{"))
808 TRACE("style %p\n", p
->member
.run
.style
);
809 if (!ME_StreamOutRTFCharProps(pStream
, &p
->member
.run
.style
->fmt
))
812 nEnd
= (p
== pEnd
) ? nEndLen
: ME_StrLen(p
->member
.run
.strText
);
813 if (!ME_StreamOutRTFText(pStream
, p
->member
.run
.strText
->szData
+ nOffset
, nEnd
- nOffset
))
816 if (!ME_StreamOutPrint(pStream
, "}"))
820 default: /* we missed the last item */
825 p
= ME_FindItemFwd(p
, diRunOrParagraphOrEnd
);
827 if (!ME_StreamOutPrint(pStream
, "}"))
834 ME_StreamOutText(ME_TextEditor
*editor
, ME_OutStream
*pStream
, int nStart
, int nChars
, DWORD dwFormat
)
836 /* FIXME: use ME_RunOfsFromCharOfs */
837 ME_DisplayItem
*item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
839 UINT nCodePage
= CP_ACP
;
847 if (dwFormat
& SF_USECODEPAGE
)
848 nCodePage
= HIWORD(dwFormat
);
850 /* TODO: Handle SF_TEXTIZED */
852 while (success
&& nChars
&& item
) {
853 nLen
= ME_StrLen(item
->member
.run
.strText
) - nStart
;
857 if (item
->member
.run
.nFlags
& MERF_ENDPARA
) {
858 static const WCHAR szEOL
[2] = { '\r', '\n' };
860 if (!editor
->bEmulateVersion10
) {
861 /* richedit 2.0 - all line breaks are \r\n */
862 if (dwFormat
& SF_UNICODE
)
863 success
= ME_StreamOutMove(pStream
, (const char *)szEOL
, sizeof(szEOL
));
865 success
= ME_StreamOutMove(pStream
, "\r\n", 2);
870 /* richedit 1.0 - need to honor actual \r and \n amounts */
871 nLen
= item
->member
.run
.nCR
+ item
->member
.run
.nLF
;
877 while (tnLen
> 0 && i
< item
->member
.run
.nCR
) {
878 if (dwFormat
& SF_UNICODE
)
879 success
= ME_StreamOutMove(pStream
, (const char *)(&szEOL
[0]), sizeof(WCHAR
));
881 success
= ME_StreamOutMove(pStream
, "\r", 1);
885 while (tnLen
> 0 && i
< item
->member
.run
.nLF
) {
886 if (dwFormat
& SF_UNICODE
)
887 success
= ME_StreamOutMove(pStream
, (const char *)(&szEOL
[1]), sizeof(WCHAR
));
889 success
= ME_StreamOutMove(pStream
, "\n", 1);
894 if (dwFormat
& SF_UNICODE
)
895 success
= ME_StreamOutMove(pStream
, (const char *)(item
->member
.run
.strText
->szData
+ nStart
),
896 sizeof(WCHAR
) * nLen
);
900 nSize
= WideCharToMultiByte(nCodePage
, 0, item
->member
.run
.strText
->szData
+ nStart
,
901 nLen
, NULL
, 0, NULL
, NULL
);
902 if (nSize
> nBufLen
) {
904 buffer
= ALLOC_N_OBJ(char, nSize
);
907 WideCharToMultiByte(nCodePage
, 0, item
->member
.run
.strText
->szData
+ nStart
,
908 nLen
, buffer
, nSize
, NULL
, NULL
);
909 success
= ME_StreamOutMove(pStream
, buffer
, nSize
);
915 item
= ME_FindItemFwd(item
, diRun
);
924 ME_StreamOutRange(ME_TextEditor
*editor
, DWORD dwFormat
, int nStart
, int nTo
, EDITSTREAM
*stream
)
926 ME_OutStream
*pStream
= ME_StreamOutInit(editor
, stream
);
930 nTo
= ME_GetTextLength(editor
);
931 /* Generate an end-of-paragraph at the end of SCF_ALL RTF output */
932 if (dwFormat
& SF_RTF
)
935 TRACE("from %d to %d\n", nStart
, nTo
);
937 if (dwFormat
& SF_RTF
)
938 ME_StreamOutRTF(editor
, pStream
, nStart
, nTo
- nStart
, dwFormat
);
939 else if (dwFormat
& SF_TEXT
|| dwFormat
& SF_TEXTIZED
)
940 ME_StreamOutText(editor
, pStream
, nStart
, nTo
- nStart
, dwFormat
);
941 if (!pStream
->stream
->dwError
)
942 ME_StreamOutFlush(pStream
);
943 return ME_StreamOutFree(pStream
);
947 ME_StreamOut(ME_TextEditor
*editor
, DWORD dwFormat
, EDITSTREAM
*stream
)
951 if (dwFormat
& SFF_SELECTION
)
952 ME_GetSelection(editor
, &nStart
, &nTo
);
957 return ME_StreamOutRange(editor
, dwFormat
, nStart
, nTo
, stream
);