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"
24 #define NONAMELESSUNION
29 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
31 #define STREAMOUT_BUFFER_SIZE 4096
32 #define STREAMOUT_FONTTBL_SIZE 8192
33 #define STREAMOUT_COLORTBL_SIZE 1024
35 typedef struct tagME_OutStream
38 char buffer
[STREAMOUT_BUFFER_SIZE
];
42 ME_FontTableItem fonttbl
[STREAMOUT_FONTTBL_SIZE
];
44 COLORREF colortbl
[STREAMOUT_COLORTBL_SIZE
];
46 UINT nDefaultCodePage
;
47 /* nNestingLevel = 0 means we aren't in a cell, 1 means we are in a cell,
48 * an greater numbers mean we are in a cell nested within a cell. */
50 CHARFORMAT2W cur_fmt
; /* current character format */
54 ME_StreamOutRTFText(ME_OutStream
*pStream
, const WCHAR
*text
, LONG nChars
);
58 ME_StreamOutInit(ME_TextEditor
*editor
, EDITSTREAM
*stream
)
60 ME_OutStream
*pStream
= heap_alloc_zero(sizeof(*pStream
));
62 pStream
->stream
= stream
;
63 pStream
->stream
->dwError
= 0;
64 pStream
->nColorTblLen
= 1;
65 pStream
->cur_fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
66 pStream
->cur_fmt
.bUnderlineType
= CFU_UNDERLINE
;
72 ME_StreamOutFlush(ME_OutStream
*pStream
)
75 EDITSTREAM
*stream
= pStream
->stream
;
78 TRACE("sending %u bytes\n", pStream
->pos
);
79 nWritten
= pStream
->pos
;
80 stream
->dwError
= stream
->pfnCallback(stream
->dwCookie
, (LPBYTE
)pStream
->buffer
,
81 pStream
->pos
, &nWritten
);
82 TRACE("error=%u written=%u\n", stream
->dwError
, nWritten
);
83 if (nWritten
== 0 || stream
->dwError
)
85 /* Don't resend partial chunks if nWritten < pStream->pos */
87 if (nWritten
== pStream
->pos
)
88 pStream
->written
+= nWritten
;
95 ME_StreamOutFree(ME_OutStream
*pStream
)
97 LONG written
= pStream
->written
;
98 TRACE("total length = %u\n", written
);
106 ME_StreamOutMove(ME_OutStream
*pStream
, const char *buffer
, int len
)
109 int space
= STREAMOUT_BUFFER_SIZE
- pStream
->pos
;
110 int fit
= min(space
, len
);
112 TRACE("%u:%u:%s\n", pStream
->pos
, fit
, debugstr_an(buffer
,fit
));
113 memmove(pStream
->buffer
+ pStream
->pos
, buffer
, fit
);
117 if (pStream
->pos
== STREAMOUT_BUFFER_SIZE
) {
118 if (!ME_StreamOutFlush(pStream
))
127 ME_StreamOutPrint(ME_OutStream
*pStream
, const char *format
, ...)
129 char string
[STREAMOUT_BUFFER_SIZE
]; /* This is going to be enough */
133 va_start(valist
, format
);
134 len
= vsnprintf(string
, sizeof(string
), format
, valist
);
137 return ME_StreamOutMove(pStream
, string
, len
);
140 #define HEX_BYTES_PER_LINE 40
143 ME_StreamOutHexData(ME_OutStream
*stream
, const BYTE
*data
, UINT len
)
146 char line
[HEX_BYTES_PER_LINE
* 2 + 1];
148 static const char hex
[] = "0123456789abcdef";
152 size
= min( len
, HEX_BYTES_PER_LINE
);
153 for (i
= 0; i
< size
; i
++)
155 line
[i
* 2] = hex
[(*data
>> 4) & 0xf];
156 line
[i
* 2 + 1] = hex
[*data
& 0xf];
159 line
[size
* 2] = '\n';
160 if (!ME_StreamOutMove( stream
, line
, size
* 2 + 1 ))
168 ME_StreamOutRTFHeader(ME_OutStream
*pStream
, int dwFormat
)
170 const char *cCharSet
= NULL
;
175 if (dwFormat
& SF_USECODEPAGE
) {
178 switch (HIWORD(dwFormat
)) {
181 nCodePage
= GetACP();
184 nCodePage
= GetOEMCP();
185 if (nCodePage
== 437)
187 else if (nCodePage
== 850)
196 if (HIWORD(dwFormat
) == CP_MACCP
) {
198 nCodePage
= 10000; /* MacRoman */
201 nCodePage
= 1252; /* Latin-1 */
203 if (GetCPInfoExW(HIWORD(dwFormat
), 0, &info
))
204 nCodePage
= info
.CodePage
;
208 /* TODO: If the original document contained an \ansicpg value, retain it.
209 * Otherwise, M$ richedit emits a codepage number determined from the
210 * charset of the default font here. Anyway, this value is not used by
212 nCodePage
= GetACP();
214 if (nCodePage
== CP_UTF8
)
215 success
= ME_StreamOutPrint(pStream
, "{\\urtf");
217 success
= ME_StreamOutPrint(pStream
, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet
, nCodePage
);
222 pStream
->nDefaultCodePage
= nCodePage
;
224 /* FIXME: This should be a document property */
225 /* TODO: handle SFF_PLAINRTF */
226 language
= GetUserDefaultLangID();
227 if (!ME_StreamOutPrint(pStream
, "\\deff0\\deflang%u\\deflangfe%u", language
, language
))
230 /* FIXME: This should be a document property */
231 pStream
->nDefaultFont
= 0;
236 static void add_font_to_fonttbl( ME_OutStream
*stream
, ME_Style
*style
)
238 ME_FontTableItem
*table
= stream
->fonttbl
;
239 CHARFORMAT2W
*fmt
= &style
->fmt
;
240 WCHAR
*face
= fmt
->szFaceName
;
241 BYTE charset
= (fmt
->dwMask
& CFM_CHARSET
) ? fmt
->bCharSet
: DEFAULT_CHARSET
;
244 if (fmt
->dwMask
& CFM_FACE
)
246 for (i
= 0; i
< stream
->nFontTblLen
; i
++)
247 if (table
[i
].bCharSet
== charset
248 && (table
[i
].szFaceName
== face
|| !lstrcmpW(table
[i
].szFaceName
, face
)))
251 if (i
== stream
->nFontTblLen
&& i
< STREAMOUT_FONTTBL_SIZE
)
253 table
[i
].bCharSet
= charset
;
254 table
[i
].szFaceName
= face
;
255 stream
->nFontTblLen
++;
260 static BOOL
find_font_in_fonttbl( ME_OutStream
*stream
, CHARFORMAT2W
*fmt
, unsigned int *idx
)
266 if (fmt
->dwMask
& CFM_FACE
)
267 facename
= fmt
->szFaceName
;
269 facename
= stream
->fonttbl
[0].szFaceName
;
270 for (i
= 0; i
< stream
->nFontTblLen
; i
++)
272 if (facename
== stream
->fonttbl
[i
].szFaceName
273 || !lstrcmpW(facename
, stream
->fonttbl
[i
].szFaceName
))
274 if (!(fmt
->dwMask
& CFM_CHARSET
)
275 || fmt
->bCharSet
== stream
->fonttbl
[i
].bCharSet
)
282 return i
< stream
->nFontTblLen
;
285 static void add_color_to_colortbl( ME_OutStream
*stream
, COLORREF color
)
289 for (i
= 1; i
< stream
->nColorTblLen
; i
++)
290 if (stream
->colortbl
[i
] == color
)
293 if (i
== stream
->nColorTblLen
&& i
< STREAMOUT_COLORTBL_SIZE
)
295 stream
->colortbl
[i
] = color
;
296 stream
->nColorTblLen
++;
300 static BOOL
find_color_in_colortbl( ME_OutStream
*stream
, COLORREF color
, unsigned int *idx
)
305 for (i
= 1; i
< stream
->nColorTblLen
; i
++)
307 if (stream
->colortbl
[i
] == color
)
314 return i
< stream
->nFontTblLen
;
318 ME_StreamOutRTFFontAndColorTbl(ME_OutStream
*pStream
, ME_DisplayItem
*pFirstRun
,
319 ME_DisplayItem
*pLastRun
)
321 ME_DisplayItem
*item
= pFirstRun
;
322 ME_FontTableItem
*table
= pStream
->fonttbl
;
324 ME_DisplayItem
*pCell
= NULL
;
325 ME_Paragraph
*prev_para
= NULL
;
328 CHARFORMAT2W
*fmt
= &item
->member
.run
.style
->fmt
;
330 add_font_to_fonttbl( pStream
, item
->member
.run
.style
);
332 if (fmt
->dwMask
& CFM_COLOR
&& !(fmt
->dwEffects
& CFE_AUTOCOLOR
))
333 add_color_to_colortbl( pStream
, fmt
->crTextColor
);
334 if (fmt
->dwMask
& CFM_BACKCOLOR
&& !(fmt
->dwEffects
& CFE_AUTOBACKCOLOR
))
335 add_color_to_colortbl( pStream
, fmt
->crBackColor
);
337 if (item
->member
.run
.para
!= prev_para
)
339 /* check for any para numbering text */
340 if (item
->member
.run
.para
->fmt
.wNumbering
)
341 add_font_to_fonttbl( pStream
, item
->member
.run
.para
->para_num
.style
);
343 if ((pCell
= item
->member
.para
.pCell
))
345 ME_Border
* borders
[4] = { &pCell
->member
.cell
.border
.top
,
346 &pCell
->member
.cell
.border
.left
,
347 &pCell
->member
.cell
.border
.bottom
,
348 &pCell
->member
.cell
.border
.right
};
349 for (i
= 0; i
< 4; i
++)
350 if (borders
[i
]->width
> 0)
351 add_color_to_colortbl( pStream
, borders
[i
]->colorRef
);
354 prev_para
= item
->member
.run
.para
;
357 if (item
== pLastRun
)
359 item
= ME_FindItemFwd(item
, diRun
);
362 if (!ME_StreamOutPrint(pStream
, "{\\fonttbl"))
365 for (i
= 0; i
< pStream
->nFontTblLen
; i
++) {
366 if (table
[i
].bCharSet
!= DEFAULT_CHARSET
) {
367 if (!ME_StreamOutPrint(pStream
, "{\\f%u\\fcharset%u ", i
, table
[i
].bCharSet
))
370 if (!ME_StreamOutPrint(pStream
, "{\\f%u ", i
))
373 if (!ME_StreamOutRTFText(pStream
, table
[i
].szFaceName
, -1))
375 if (!ME_StreamOutPrint(pStream
, ";}"))
378 if (!ME_StreamOutPrint(pStream
, "}\r\n"))
381 /* Output the color table */
382 if (!ME_StreamOutPrint(pStream
, "{\\colortbl;")) return FALSE
; /* first entry is auto-color */
383 for (i
= 1; i
< pStream
->nColorTblLen
; i
++)
385 if (!ME_StreamOutPrint(pStream
, "\\red%u\\green%u\\blue%u;", pStream
->colortbl
[i
] & 0xFF,
386 (pStream
->colortbl
[i
] >> 8) & 0xFF, (pStream
->colortbl
[i
] >> 16) & 0xFF))
389 if (!ME_StreamOutPrint(pStream
, "}\r\n")) return FALSE
;
395 ME_StreamOutRTFTableProps(ME_TextEditor
*editor
, ME_OutStream
*pStream
,
396 ME_DisplayItem
*para
)
398 ME_DisplayItem
*cell
;
399 char props
[STREAMOUT_BUFFER_SIZE
] = "";
401 const char sideChar
[4] = {'t','l','b','r'};
403 if (!ME_StreamOutPrint(pStream
, "\\trowd"))
405 if (!editor
->bEmulateVersion10
) { /* v4.1 */
406 PARAFORMAT2
*pFmt
= &ME_GetTableRowEnd(para
)->member
.para
.fmt
;
407 para
= ME_GetTableRowStart(para
);
408 cell
= para
->member
.para
.next_para
->member
.para
.pCell
;
411 sprintf(props
+ strlen(props
), "\\trgaph%d", pFmt
->dxOffset
);
412 if (pFmt
->dxStartIndent
)
413 sprintf(props
+ strlen(props
), "\\trleft%d", pFmt
->dxStartIndent
);
415 ME_Border
* borders
[4] = { &cell
->member
.cell
.border
.top
,
416 &cell
->member
.cell
.border
.left
,
417 &cell
->member
.cell
.border
.bottom
,
418 &cell
->member
.cell
.border
.right
};
419 for (i
= 0; i
< 4; i
++)
421 if (borders
[i
]->width
)
424 COLORREF crColor
= borders
[i
]->colorRef
;
425 sprintf(props
+ strlen(props
), "\\clbrdr%c", sideChar
[i
]);
426 sprintf(props
+ strlen(props
), "\\brdrs");
427 sprintf(props
+ strlen(props
), "\\brdrw%d", borders
[i
]->width
);
428 if (find_color_in_colortbl( pStream
, crColor
, &idx
))
429 sprintf(props
+ strlen(props
), "\\brdrcf%u", idx
);
432 sprintf(props
+ strlen(props
), "\\cellx%d", cell
->member
.cell
.nRightBoundary
);
433 cell
= cell
->member
.cell
.next_cell
;
434 } while (cell
->member
.cell
.next_cell
);
435 } else { /* v1.0 - 3.0 */
436 const ME_Border
* borders
[4] = { ¶
->member
.para
.border
.top
,
437 ¶
->member
.para
.border
.left
,
438 ¶
->member
.para
.border
.bottom
,
439 ¶
->member
.para
.border
.right
};
440 PARAFORMAT2
*pFmt
= ¶
->member
.para
.fmt
;
442 assert(!(para
->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
|MEPF_CELL
)));
444 sprintf(props
+ strlen(props
), "\\trgaph%d", pFmt
->dxOffset
);
445 if (pFmt
->dxStartIndent
)
446 sprintf(props
+ strlen(props
), "\\trleft%d", pFmt
->dxStartIndent
);
447 for (i
= 0; i
< 4; i
++)
449 if (borders
[i
]->width
)
452 COLORREF crColor
= borders
[i
]->colorRef
;
453 sprintf(props
+ strlen(props
), "\\trbrdr%c", sideChar
[i
]);
454 sprintf(props
+ strlen(props
), "\\brdrs");
455 sprintf(props
+ strlen(props
), "\\brdrw%d", borders
[i
]->width
);
456 if (find_color_in_colortbl( pStream
, crColor
, &idx
))
457 sprintf(props
+ strlen(props
), "\\brdrcf%u", idx
);
460 for (i
= 0; i
< pFmt
->cTabCount
; i
++)
462 sprintf(props
+ strlen(props
), "\\cellx%d", pFmt
->rgxTabs
[i
] & 0x00FFFFFF);
465 if (!ME_StreamOutPrint(pStream
, props
))
471 static BOOL
stream_out_para_num( ME_OutStream
*stream
, ME_Paragraph
*para
, BOOL pn_dest
)
473 static const char fmt_label
[] = "{\\*\\pn\\pnlvlbody\\pnf%u\\pnindent%d\\pnstart%d%s%s}";
474 static const char fmt_bullet
[] = "{\\*\\pn\\pnlvlblt\\pnf%u\\pnindent%d{\\pntxtb\\'b7}}";
475 static const char dec
[] = "\\pndec";
476 static const char lcltr
[] = "\\pnlcltr";
477 static const char ucltr
[] = "\\pnucltr";
478 static const char lcrm
[] = "\\pnlcrm";
479 static const char ucrm
[] = "\\pnucrm";
480 static const char period
[] = "{\\pntxta.}";
481 static const char paren
[] = "{\\pntxta)}";
482 static const char parens
[] = "{\\pntxtb(}{\\pntxta)}";
483 const char *type
, *style
= "";
486 find_font_in_fonttbl( stream
, ¶
->para_num
.style
->fmt
, &idx
);
488 if (!ME_StreamOutPrint( stream
, "{\\pntext\\f%u ", idx
)) return FALSE
;
489 if (!ME_StreamOutRTFText( stream
, para
->para_num
.text
->szData
, para
->para_num
.text
->nLen
))
491 if (!ME_StreamOutPrint( stream
, "\\tab}" )) return FALSE
;
493 if (!pn_dest
) return TRUE
;
495 if (para
->fmt
.wNumbering
== PFN_BULLET
)
497 if (!ME_StreamOutPrint( stream
, fmt_bullet
, idx
, para
->fmt
.wNumberingTab
))
502 switch (para
->fmt
.wNumbering
)
521 switch (para
->fmt
.wNumberingStyle
& 0xf00)
534 if (!ME_StreamOutPrint( stream
, fmt_label
, idx
, para
->fmt
.wNumberingTab
,
535 para
->fmt
.wNumberingStart
, type
, style
))
542 ME_StreamOutRTFParaProps(ME_TextEditor
*editor
, ME_OutStream
*pStream
,
543 ME_DisplayItem
*para
)
545 PARAFORMAT2
*fmt
= ¶
->member
.para
.fmt
;
546 char props
[STREAMOUT_BUFFER_SIZE
] = "";
548 ME_Paragraph
*prev_para
= NULL
;
550 if (para
->member
.para
.prev_para
->type
== diParagraph
)
551 prev_para
= ¶
->member
.para
.prev_para
->member
.para
;
553 if (!editor
->bEmulateVersion10
) { /* v4.1 */
554 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
) {
555 pStream
->nNestingLevel
++;
556 if (pStream
->nNestingLevel
== 1) {
557 if (!ME_StreamOutRTFTableProps(editor
, pStream
, para
))
561 } else if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
562 pStream
->nNestingLevel
--;
563 if (pStream
->nNestingLevel
>= 1) {
564 if (!ME_StreamOutPrint(pStream
, "{\\*\\nesttableprops"))
566 if (!ME_StreamOutRTFTableProps(editor
, pStream
, para
))
568 if (!ME_StreamOutPrint(pStream
, "\\nestrow}{\\nonesttables\\par}\r\n"))
571 if (!ME_StreamOutPrint(pStream
, "\\row\r\n"))
576 } else { /* v1.0 - 3.0 */
577 if (para
->member
.para
.fmt
.dwMask
& PFM_TABLE
&&
578 para
->member
.para
.fmt
.wEffects
& PFE_TABLE
)
580 if (!ME_StreamOutRTFTableProps(editor
, pStream
, para
))
585 if (prev_para
&& !memcmp( fmt
, &prev_para
->fmt
, sizeof(*fmt
) ))
588 return stream_out_para_num( pStream
, ¶
->member
.para
, FALSE
);
592 if (!ME_StreamOutPrint(pStream
, "\\pard"))
596 if (!stream_out_para_num( pStream
, ¶
->member
.para
, TRUE
)) return FALSE
;
598 if (!editor
->bEmulateVersion10
) { /* v4.1 */
599 if (pStream
->nNestingLevel
> 0)
600 strcat(props
, "\\intbl");
601 if (pStream
->nNestingLevel
> 1)
602 sprintf(props
+ strlen(props
), "\\itap%d", pStream
->nNestingLevel
);
603 } else { /* v1.0 - 3.0 */
604 if (fmt
->dwMask
& PFM_TABLE
&& fmt
->wEffects
& PFE_TABLE
)
605 strcat(props
, "\\intbl");
608 /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
609 * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
610 * set very different from the documentation.
611 * (Tested with RichEdit 5.50.25.0601) */
613 if (fmt
->dwMask
& PFM_ALIGNMENT
) {
614 switch (fmt
->wAlignment
) {
616 /* Default alignment: not emitted */
619 strcat(props
, "\\qr");
622 strcat(props
, "\\qc");
625 strcat(props
, "\\qj");
630 if (fmt
->dwMask
& PFM_LINESPACING
) {
631 /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
632 * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
633 switch (fmt
->bLineSpacingRule
) {
634 case 0: /* Single spacing */
635 strcat(props
, "\\sl-240\\slmult1");
637 case 1: /* 1.5 spacing */
638 strcat(props
, "\\sl-360\\slmult1");
640 case 2: /* Double spacing */
641 strcat(props
, "\\sl-480\\slmult1");
644 sprintf(props
+ strlen(props
), "\\sl%d\\slmult0", fmt
->dyLineSpacing
);
647 sprintf(props
+ strlen(props
), "\\sl-%d\\slmult0", fmt
->dyLineSpacing
);
650 sprintf(props
+ strlen(props
), "\\sl-%d\\slmult1", fmt
->dyLineSpacing
* 240 / 20);
655 if (fmt
->dwMask
& PFM_DONOTHYPHEN
&& fmt
->wEffects
& PFE_DONOTHYPHEN
)
656 strcat(props
, "\\hyph0");
657 if (fmt
->dwMask
& PFM_KEEP
&& fmt
->wEffects
& PFE_KEEP
)
658 strcat(props
, "\\keep");
659 if (fmt
->dwMask
& PFM_KEEPNEXT
&& fmt
->wEffects
& PFE_KEEPNEXT
)
660 strcat(props
, "\\keepn");
661 if (fmt
->dwMask
& PFM_NOLINENUMBER
&& fmt
->wEffects
& PFE_NOLINENUMBER
)
662 strcat(props
, "\\noline");
663 if (fmt
->dwMask
& PFM_NOWIDOWCONTROL
&& fmt
->wEffects
& PFE_NOWIDOWCONTROL
)
664 strcat(props
, "\\nowidctlpar");
665 if (fmt
->dwMask
& PFM_PAGEBREAKBEFORE
&& fmt
->wEffects
& PFE_PAGEBREAKBEFORE
)
666 strcat(props
, "\\pagebb");
667 if (fmt
->dwMask
& PFM_RTLPARA
&& fmt
->wEffects
& PFE_RTLPARA
)
668 strcat(props
, "\\rtlpar");
669 if (fmt
->dwMask
& PFM_SIDEBYSIDE
&& fmt
->wEffects
& PFE_SIDEBYSIDE
)
670 strcat(props
, "\\sbys");
672 if (!(editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
673 fmt
->dwMask
& PFM_TABLE
&& fmt
->wEffects
& PFE_TABLE
))
676 sprintf(props
+ strlen(props
), "\\li%d", fmt
->dxOffset
);
677 if (fmt
->dxStartIndent
)
678 sprintf(props
+ strlen(props
), "\\fi%d", fmt
->dxStartIndent
);
679 if (fmt
->dxRightIndent
)
680 sprintf(props
+ strlen(props
), "\\ri%d", fmt
->dxRightIndent
);
681 if (fmt
->dwMask
& PFM_TABSTOPS
) {
682 static const char * const leader
[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
684 for (i
= 0; i
< fmt
->cTabCount
; i
++) {
685 switch ((fmt
->rgxTabs
[i
] >> 24) & 0xF) {
687 strcat(props
, "\\tqc");
690 strcat(props
, "\\tqr");
693 strcat(props
, "\\tqdec");
696 /* Word bar tab (vertical bar). Handled below */
699 if (fmt
->rgxTabs
[i
] >> 28 <= 5)
700 strcat(props
, leader
[fmt
->rgxTabs
[i
] >> 28]);
701 sprintf(props
+strlen(props
), "\\tx%d", fmt
->rgxTabs
[i
]&0x00FFFFFF);
705 if (fmt
->dySpaceAfter
)
706 sprintf(props
+ strlen(props
), "\\sa%d", fmt
->dySpaceAfter
);
707 if (fmt
->dySpaceBefore
)
708 sprintf(props
+ strlen(props
), "\\sb%d", fmt
->dySpaceBefore
);
709 if (fmt
->sStyle
!= -1)
710 sprintf(props
+ strlen(props
), "\\s%d", fmt
->sStyle
);
712 if (fmt
->dwMask
& PFM_SHADING
) {
713 static const char * const style
[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
714 "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
715 "\\bghoriz", "\\bgvert", "\\bgfdiag",
716 "\\bgbdiag", "\\bgcross", "\\bgdcross",
718 if (fmt
->wShadingWeight
)
719 sprintf(props
+ strlen(props
), "\\shading%d", fmt
->wShadingWeight
);
720 if (fmt
->wShadingStyle
& 0xF)
721 strcat(props
, style
[fmt
->wShadingStyle
& 0xF]);
722 if ((fmt
->wShadingStyle
>> 4) & 0xf)
723 sprintf(props
+ strlen(props
), "\\cfpat%d", (fmt
->wShadingStyle
>> 4) & 0xf);
724 if ((fmt
->wShadingStyle
>> 8) & 0xf)
725 sprintf(props
+ strlen(props
), "\\cbpat%d", (fmt
->wShadingStyle
>> 8) & 0xf);
730 if (*props
&& !ME_StreamOutPrint(pStream
, props
))
738 ME_StreamOutRTFCharProps(ME_OutStream
*pStream
, CHARFORMAT2W
*fmt
)
740 char props
[STREAMOUT_BUFFER_SIZE
] = "";
742 CHARFORMAT2W
*old_fmt
= &pStream
->cur_fmt
;
746 const char *on
, *off
;
749 { CFE_ALLCAPS
, "\\caps", "\\caps0" },
750 { CFE_BOLD
, "\\b", "\\b0" },
751 { CFE_DISABLED
, "\\disabled", "\\disabled0" },
752 { CFE_EMBOSS
, "\\embo", "\\embo0" },
753 { CFE_HIDDEN
, "\\v", "\\v0" },
754 { CFE_IMPRINT
, "\\impr", "\\impr0" },
755 { CFE_ITALIC
, "\\i", "\\i0" },
756 { CFE_OUTLINE
, "\\outl", "\\outl0" },
757 { CFE_PROTECTED
, "\\protect", "\\protect0" },
758 { CFE_SHADOW
, "\\shad", "\\shad0" },
759 { CFE_SMALLCAPS
, "\\scaps", "\\scaps0" },
760 { CFE_STRIKEOUT
, "\\strike", "\\strike0" },
763 for (i
= 0; i
< sizeof(effects
) / sizeof(effects
[0]); i
++)
765 if ((old_fmt
->dwEffects
^ fmt
->dwEffects
) & effects
[i
].effect
)
766 strcat( props
, fmt
->dwEffects
& effects
[i
].effect
? effects
[i
].on
: effects
[i
].off
);
769 if ((old_fmt
->dwEffects
^ fmt
->dwEffects
) & CFE_AUTOBACKCOLOR
||
770 (!(fmt
->dwEffects
& CFE_AUTOBACKCOLOR
) && old_fmt
->crBackColor
!= fmt
->crBackColor
))
772 if (fmt
->dwEffects
& CFE_AUTOBACKCOLOR
) i
= 0;
773 else find_color_in_colortbl( pStream
, fmt
->crBackColor
, &i
);
774 sprintf(props
+ strlen(props
), "\\highlight%u", i
);
776 if ((old_fmt
->dwEffects
^ fmt
->dwEffects
) & CFE_AUTOCOLOR
||
777 (!(fmt
->dwEffects
& CFE_AUTOCOLOR
) && old_fmt
->crTextColor
!= fmt
->crTextColor
))
779 if (fmt
->dwEffects
& CFE_AUTOCOLOR
) i
= 0;
780 else find_color_in_colortbl( pStream
, fmt
->crTextColor
, &i
);
781 sprintf(props
+ strlen(props
), "\\cf%u", i
);
784 if (old_fmt
->bAnimation
!= fmt
->bAnimation
)
785 sprintf(props
+ strlen(props
), "\\animtext%u", fmt
->bAnimation
);
786 if (old_fmt
->wKerning
!= fmt
->wKerning
)
787 sprintf(props
+ strlen(props
), "\\kerning%u", fmt
->wKerning
);
789 if (old_fmt
->lcid
!= fmt
->lcid
)
791 /* TODO: handle SFF_PLAINRTF */
792 if (LOWORD(fmt
->lcid
) == 1024)
793 strcat(props
, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
795 sprintf(props
+ strlen(props
), "\\lang%u", LOWORD(fmt
->lcid
));
798 if (old_fmt
->yOffset
!= fmt
->yOffset
)
800 if (fmt
->yOffset
>= 0)
801 sprintf(props
+ strlen(props
), "\\up%d", fmt
->yOffset
);
803 sprintf(props
+ strlen(props
), "\\dn%d", -fmt
->yOffset
);
805 if (old_fmt
->yHeight
!= fmt
->yHeight
)
806 sprintf(props
+ strlen(props
), "\\fs%d", fmt
->yHeight
/ 10);
807 if (old_fmt
->sSpacing
!= fmt
->sSpacing
)
808 sprintf(props
+ strlen(props
), "\\expnd%u\\expndtw%u", fmt
->sSpacing
/ 5, fmt
->sSpacing
);
809 if ((old_fmt
->dwEffects
^ fmt
->dwEffects
) & (CFM_SUBSCRIPT
| CFM_SUPERSCRIPT
))
811 if (fmt
->dwEffects
& CFE_SUBSCRIPT
)
812 strcat(props
, "\\sub");
813 else if (fmt
->dwEffects
& CFE_SUPERSCRIPT
)
814 strcat(props
, "\\super");
816 strcat(props
, "\\nosupersub");
818 if ((old_fmt
->dwEffects
^ fmt
->dwEffects
) & CFE_UNDERLINE
||
819 old_fmt
->bUnderlineType
!= fmt
->bUnderlineType
)
821 BYTE type
= (fmt
->dwEffects
& CFE_UNDERLINE
) ? fmt
->bUnderlineType
: CFU_UNDERLINENONE
;
825 strcat(props
, "\\ul");
827 case CFU_UNDERLINEDOTTED
:
828 strcat(props
, "\\uld");
830 case CFU_UNDERLINEDOUBLE
:
831 strcat(props
, "\\uldb");
833 case CFU_UNDERLINEWORD
:
834 strcat(props
, "\\ulw");
836 case CFU_CF1UNDERLINE
:
837 case CFU_UNDERLINENONE
:
839 strcat(props
, "\\ulnone");
844 if (strcmpW(old_fmt
->szFaceName
, fmt
->szFaceName
) ||
845 old_fmt
->bCharSet
!= fmt
->bCharSet
)
847 if (find_font_in_fonttbl( pStream
, fmt
, &i
))
849 sprintf(props
+ strlen(props
), "\\f%u", i
);
851 /* In UTF-8 mode, charsets/codepages are not used */
852 if (pStream
->nDefaultCodePage
!= CP_UTF8
)
854 if (pStream
->fonttbl
[i
].bCharSet
== DEFAULT_CHARSET
)
855 pStream
->nCodePage
= pStream
->nDefaultCodePage
;
857 pStream
->nCodePage
= RTFCharSetToCodePage(NULL
, pStream
->fonttbl
[i
].bCharSet
);
863 if (!ME_StreamOutPrint(pStream
, props
))
871 ME_StreamOutRTFText(ME_OutStream
*pStream
, const WCHAR
*text
, LONG nChars
)
873 char buffer
[STREAMOUT_BUFFER_SIZE
];
878 nChars
= lstrlenW(text
);
881 /* In UTF-8 mode, font charsets are not used. */
882 if (pStream
->nDefaultCodePage
== CP_UTF8
) {
883 /* 6 is the maximum character length in UTF-8 */
884 fit
= min(nChars
, STREAMOUT_BUFFER_SIZE
/ 6);
885 nBytes
= WideCharToMultiByte(CP_UTF8
, 0, text
, fit
, buffer
,
886 STREAMOUT_BUFFER_SIZE
, NULL
, NULL
);
889 for (i
= 0; i
< nBytes
; i
++)
890 if (buffer
[i
] == '{' || buffer
[i
] == '}' || buffer
[i
] == '\\') {
891 if (!ME_StreamOutPrint(pStream
, "%.*s\\", i
- pos
, buffer
+ pos
))
896 if (!ME_StreamOutMove(pStream
, buffer
+ pos
, nBytes
- pos
))
899 } else if (*text
< 128) {
900 if (*text
== '{' || *text
== '}' || *text
== '\\')
901 buffer
[pos
++] = '\\';
902 buffer
[pos
++] = (char)(*text
++);
905 BOOL unknown
= FALSE
;
908 /* FIXME: In the MS docs for WideCharToMultiByte there is a big list of
909 * codepages including CP_SYMBOL for which the last parameter must be set
910 * to NULL for the function to succeed. But in Wine we need to care only
912 nBytes
= WideCharToMultiByte(pStream
->nCodePage
, 0, text
, 1,
914 (pStream
->nCodePage
== CP_SYMBOL
) ? NULL
: &unknown
);
916 pos
+= sprintf(buffer
+ pos
, "\\u%d?", (short)*text
);
917 else if ((BYTE
)*letter
< 128) {
918 if (*letter
== '{' || *letter
== '}' || *letter
== '\\')
919 buffer
[pos
++] = '\\';
920 buffer
[pos
++] = *letter
;
922 for (i
= 0; i
< nBytes
; i
++)
923 pos
+= sprintf(buffer
+ pos
, "\\'%02x", (BYTE
)letter
[i
]);
928 if (pos
>= STREAMOUT_BUFFER_SIZE
- 11) {
929 if (!ME_StreamOutMove(pStream
, buffer
, pos
))
934 return ME_StreamOutMove(pStream
, buffer
, pos
);
937 static BOOL
stream_out_graphics( ME_TextEditor
*editor
, ME_OutStream
*stream
,
942 FORMATETC fmt
= { CF_ENHMETAFILE
, NULL
, DVASPECT_CONTENT
, -1, TYMED_ENHMF
};
943 STGMEDIUM med
= { TYMED_NULL
};
945 ENHMETAHEADER
*emf_bits
= NULL
;
950 hr
= IOleObject_QueryInterface( run
->ole_obj
->poleobj
, &IID_IDataObject
, (void **)&data
);
951 if (FAILED(hr
)) return FALSE
;
953 ME_InitContext( &c
, editor
, ITextHost_TxGetDC( editor
->texthost
) );
954 hr
= IDataObject_QueryGetData( data
, &fmt
);
955 if (hr
!= S_OK
) goto done
;
957 hr
= IDataObject_GetData( data
, &fmt
, &med
);
958 if (FAILED(hr
)) goto done
;
959 if (med
.tymed
!= TYMED_ENHMF
) goto done
;
961 size
= GetEnhMetaFileBits( med
.u
.hEnhMetaFile
, 0, NULL
);
962 if (size
< FIELD_OFFSET(ENHMETAHEADER
, cbPixelFormat
)) goto done
;
964 emf_bits
= HeapAlloc( GetProcessHeap(), 0, size
);
965 if (!emf_bits
) goto done
;
967 size
= GetEnhMetaFileBits( med
.u
.hEnhMetaFile
, size
, (BYTE
*)emf_bits
);
968 if (size
< FIELD_OFFSET(ENHMETAHEADER
, cbPixelFormat
)) goto done
;
970 /* size_in_pixels = (frame_size / 100) * szlDevice / szlMillimeters
971 pic = size_in_pixels * 2540 / dpi */
972 pic
.cx
= MulDiv( emf_bits
->rclFrame
.right
- emf_bits
->rclFrame
.left
, emf_bits
->szlDevice
.cx
* 254,
973 emf_bits
->szlMillimeters
.cx
* c
.dpi
.cx
* 10 );
974 pic
.cy
= MulDiv( emf_bits
->rclFrame
.bottom
- emf_bits
->rclFrame
.top
, emf_bits
->szlDevice
.cy
* 254,
975 emf_bits
->szlMillimeters
.cy
* c
.dpi
.cy
* 10 );
977 /* convert goal size to twips */
978 goal
.cx
= MulDiv( run
->ole_obj
->sizel
.cx
, 144, 254 );
979 goal
.cy
= MulDiv( run
->ole_obj
->sizel
.cy
, 144, 254 );
981 if (!ME_StreamOutPrint( stream
, "{\\*\\shppict{\\pict\\emfblip\\picw%d\\pich%d\\picwgoal%d\\pichgoal%d\n",
982 pic
.cx
, pic
.cy
, goal
.cx
, goal
.cy
))
985 if (!ME_StreamOutHexData( stream
, (BYTE
*)emf_bits
, size
))
988 if (!ME_StreamOutPrint( stream
, "}}\n" ))
994 ME_DestroyContext( &c
);
995 HeapFree( GetProcessHeap(), 0, emf_bits
);
996 ReleaseStgMedium( &med
);
997 IDataObject_Release( data
);
1001 static BOOL
ME_StreamOutRTF(ME_TextEditor
*editor
, ME_OutStream
*pStream
,
1002 const ME_Cursor
*start
, int nChars
, int dwFormat
)
1004 ME_Cursor cursor
= *start
;
1005 ME_DisplayItem
*prev_para
= NULL
;
1006 ME_Cursor endCur
= cursor
;
1008 ME_MoveCursorChars(editor
, &endCur
, nChars
, TRUE
);
1010 if (!ME_StreamOutRTFHeader(pStream
, dwFormat
))
1013 if (!ME_StreamOutRTFFontAndColorTbl(pStream
, cursor
.pRun
, endCur
.pRun
))
1016 /* TODO: stylesheet table */
1018 if (!ME_StreamOutPrint(pStream
, "{\\*\\generator Wine Riched20 2.0;}\r\n"))
1021 /* TODO: information group */
1023 /* TODO: document formatting properties */
1025 /* FIXME: We have only one document section */
1027 /* TODO: section formatting properties */
1030 if (cursor
.pPara
!= prev_para
)
1032 prev_para
= cursor
.pPara
;
1033 if (!ME_StreamOutRTFParaProps(editor
, pStream
, cursor
.pPara
))
1037 if (cursor
.pRun
== endCur
.pRun
&& !endCur
.nOffset
)
1039 TRACE("flags %xh\n", cursor
.pRun
->member
.run
.nFlags
);
1040 /* TODO: emit embedded objects */
1041 if (cursor
.pPara
->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
))
1043 if (cursor
.pRun
->member
.run
.nFlags
& MERF_GRAPHICS
) {
1044 if (!stream_out_graphics(editor
, pStream
, &cursor
.pRun
->member
.run
))
1046 } else if (cursor
.pRun
->member
.run
.nFlags
& MERF_TAB
) {
1047 if (editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
1048 cursor
.pPara
->member
.para
.fmt
.dwMask
& PFM_TABLE
&&
1049 cursor
.pPara
->member
.para
.fmt
.wEffects
& PFE_TABLE
)
1051 if (!ME_StreamOutPrint(pStream
, "\\cell "))
1054 if (!ME_StreamOutPrint(pStream
, "\\tab "))
1057 } else if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDCELL
) {
1058 if (pStream
->nNestingLevel
> 1) {
1059 if (!ME_StreamOutPrint(pStream
, "\\nestcell "))
1062 if (!ME_StreamOutPrint(pStream
, "\\cell "))
1066 } else if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
) {
1067 if (!ME_StreamOutRTFCharProps(pStream
, &cursor
.pRun
->member
.run
.style
->fmt
))
1070 if (cursor
.pPara
->member
.para
.fmt
.dwMask
& PFM_TABLE
&&
1071 cursor
.pPara
->member
.para
.fmt
.wEffects
& PFE_TABLE
&&
1072 !(cursor
.pPara
->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
|MEPF_CELL
)))
1074 if (!ME_StreamOutPrint(pStream
, "\\row\r\n"))
1077 if (!ME_StreamOutPrint(pStream
, "\\par\r\n"))
1080 /* Skip as many characters as required by current line break */
1081 nChars
= max(0, nChars
- cursor
.pRun
->member
.run
.len
);
1082 } else if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDROW
) {
1083 if (!ME_StreamOutPrint(pStream
, "\\line\r\n"))
1089 TRACE("style %p\n", cursor
.pRun
->member
.run
.style
);
1090 if (!ME_StreamOutRTFCharProps(pStream
, &cursor
.pRun
->member
.run
.style
->fmt
))
1093 nEnd
= (cursor
.pRun
== endCur
.pRun
) ? endCur
.nOffset
: cursor
.pRun
->member
.run
.len
;
1094 if (!ME_StreamOutRTFText(pStream
, get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
),
1095 nEnd
- cursor
.nOffset
))
1099 } while (cursor
.pRun
!= endCur
.pRun
&& ME_NextRun(&cursor
.pPara
, &cursor
.pRun
, TRUE
));
1101 if (!ME_StreamOutMove(pStream
, "}\0", 2))
1107 static BOOL
ME_StreamOutText(ME_TextEditor
*editor
, ME_OutStream
*pStream
,
1108 const ME_Cursor
*start
, int nChars
, DWORD dwFormat
)
1110 ME_Cursor cursor
= *start
;
1112 UINT nCodePage
= CP_ACP
;
1113 char *buffer
= NULL
;
1115 BOOL success
= TRUE
;
1120 if (dwFormat
& SF_USECODEPAGE
)
1121 nCodePage
= HIWORD(dwFormat
);
1123 /* TODO: Handle SF_TEXTIZED */
1125 while (success
&& nChars
&& cursor
.pRun
) {
1126 nLen
= min(nChars
, cursor
.pRun
->member
.run
.len
- cursor
.nOffset
);
1128 if (!editor
->bEmulateVersion10
&& cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
1130 static const WCHAR szEOL
[] = { '\r', '\n' };
1132 /* richedit 2.0 - all line breaks are \r\n */
1133 if (dwFormat
& SF_UNICODE
)
1134 success
= ME_StreamOutMove(pStream
, (const char *)szEOL
, sizeof(szEOL
));
1136 success
= ME_StreamOutMove(pStream
, "\r\n", 2);
1138 if (dwFormat
& SF_UNICODE
)
1139 success
= ME_StreamOutMove(pStream
, (const char *)(get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
)),
1140 sizeof(WCHAR
) * nLen
);
1144 nSize
= WideCharToMultiByte(nCodePage
, 0, get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
),
1145 nLen
, NULL
, 0, NULL
, NULL
);
1146 if (nSize
> nBufLen
) {
1147 buffer
= heap_realloc(buffer
, nSize
);
1150 WideCharToMultiByte(nCodePage
, 0, get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
),
1151 nLen
, buffer
, nSize
, NULL
, NULL
);
1152 success
= ME_StreamOutMove(pStream
, buffer
, nSize
);
1158 cursor
.pRun
= ME_FindItemFwd(cursor
.pRun
, diRun
);
1166 LRESULT
ME_StreamOutRange(ME_TextEditor
*editor
, DWORD dwFormat
,
1167 const ME_Cursor
*start
,
1168 int nChars
, EDITSTREAM
*stream
)
1170 ME_OutStream
*pStream
= ME_StreamOutInit(editor
, stream
);
1172 if (dwFormat
& SF_RTF
)
1173 ME_StreamOutRTF(editor
, pStream
, start
, nChars
, dwFormat
);
1174 else if (dwFormat
& SF_TEXT
|| dwFormat
& SF_TEXTIZED
)
1175 ME_StreamOutText(editor
, pStream
, start
, nChars
, dwFormat
);
1176 if (!pStream
->stream
->dwError
)
1177 ME_StreamOutFlush(pStream
);
1178 return ME_StreamOutFree(pStream
);
1182 ME_StreamOut(ME_TextEditor
*editor
, DWORD dwFormat
, EDITSTREAM
*stream
)
1187 if (dwFormat
& SFF_SELECTION
) {
1189 start
= editor
->pCursors
[ME_GetSelectionOfs(editor
, &nStart
, &nTo
)];
1190 nChars
= nTo
- nStart
;
1192 ME_SetCursorToStart(editor
, &start
);
1193 nChars
= ME_GetTextLength(editor
);
1194 /* Generate an end-of-paragraph at the end of SCF_ALL RTF output */
1195 if (dwFormat
& SF_RTF
)
1198 return ME_StreamOutRange(editor
, dwFormat
, &start
, nChars
, stream
);