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
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
35 char buffer
[STREAMOUT_BUFFER_SIZE
];
39 ME_FontTableItem fonttbl
[STREAMOUT_FONTTBL_SIZE
];
41 COLORREF colortbl
[STREAMOUT_COLORTBL_SIZE
];
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. */
47 CHARFORMAT2W cur_fmt
; /* current character format */
51 ME_StreamOutRTFText(ME_OutStream
*pStream
, const WCHAR
*text
, LONG nChars
);
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
;
69 ME_StreamOutFlush(ME_OutStream
*pStream
)
72 EDITSTREAM
*stream
= pStream
->stream
;
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=%u written=%u\n", stream
->dwError
, nWritten
);
80 if (nWritten
== 0 || stream
->dwError
)
82 /* Don't resend partial chunks if nWritten < pStream->pos */
84 if (nWritten
== pStream
->pos
)
85 pStream
->written
+= nWritten
;
92 ME_StreamOutFree(ME_OutStream
*pStream
)
94 LONG written
= pStream
->written
;
95 TRACE("total length = %u\n", written
);
103 ME_StreamOutMove(ME_OutStream
*pStream
, const char *buffer
, int 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
);
114 if (pStream
->pos
== STREAMOUT_BUFFER_SIZE
) {
115 if (!ME_StreamOutFlush(pStream
))
124 ME_StreamOutPrint(ME_OutStream
*pStream
, const char *format
, ...)
126 char string
[STREAMOUT_BUFFER_SIZE
]; /* This is going to be enough */
130 va_start(valist
, format
);
131 len
= vsnprintf(string
, sizeof(string
), format
, valist
);
134 return ME_StreamOutMove(pStream
, string
, len
);
137 #define HEX_BYTES_PER_LINE 40
140 ME_StreamOutHexData(ME_OutStream
*stream
, const BYTE
*data
, UINT len
)
143 char line
[HEX_BYTES_PER_LINE
* 2 + 1];
145 static const char hex
[] = "0123456789abcdef";
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];
156 line
[size
* 2] = '\n';
157 if (!ME_StreamOutMove( stream
, line
, size
* 2 + 1 ))
165 ME_StreamOutRTFHeader(ME_OutStream
*pStream
, int dwFormat
)
167 const char *cCharSet
= NULL
;
172 if (dwFormat
& SF_USECODEPAGE
) {
175 switch (HIWORD(dwFormat
)) {
178 nCodePage
= GetACP();
181 nCodePage
= GetOEMCP();
182 if (nCodePage
== 437)
184 else if (nCodePage
== 850)
193 if (HIWORD(dwFormat
) == CP_MACCP
) {
195 nCodePage
= 10000; /* MacRoman */
198 nCodePage
= 1252; /* Latin-1 */
200 if (GetCPInfoExW(HIWORD(dwFormat
), 0, &info
))
201 nCodePage
= info
.CodePage
;
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
209 nCodePage
= GetACP();
211 if (nCodePage
== CP_UTF8
)
212 success
= ME_StreamOutPrint(pStream
, "{\\urtf");
214 success
= ME_StreamOutPrint(pStream
, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet
, nCodePage
);
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
))
227 /* FIXME: This should be a document property */
228 pStream
->nDefaultFont
= 0;
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
;
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
)))
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
)
263 if (fmt
->dwMask
& CFM_FACE
)
264 facename
= fmt
->szFaceName
;
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
)
279 return i
< stream
->nFontTblLen
;
282 static void add_color_to_colortbl( ME_OutStream
*stream
, COLORREF color
)
286 for (i
= 1; i
< stream
->nColorTblLen
; i
++)
287 if (stream
->colortbl
[i
] == color
)
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
)
302 for (i
= 1; i
< stream
->nColorTblLen
; i
++)
304 if (stream
->colortbl
[i
] == color
)
311 return i
< stream
->nFontTblLen
;
314 static BOOL
stream_out_font_and_colour_tbls( ME_OutStream
*pStream
, ME_Run
*first
, ME_Run
*last
)
317 ME_FontTableItem
*table
= pStream
->fonttbl
;
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
);
355 if (!ME_StreamOutPrint(pStream
, "{\\fonttbl"))
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
))
363 if (!ME_StreamOutPrint(pStream
, "{\\f%u ", i
))
366 if (!ME_StreamOutRTFText(pStream
, table
[i
].szFaceName
, -1))
368 if (!ME_StreamOutPrint(pStream
, ";}"))
371 if (!ME_StreamOutPrint(pStream
, "}\r\n"))
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))
382 if (!ME_StreamOutPrint(pStream
, "}\r\n")) return FALSE
;
387 static BOOL
stream_out_table_props( ME_TextEditor
*editor
, ME_OutStream
*pStream
,
391 char props
[STREAMOUT_BUFFER_SIZE
] = "";
393 const char sideChar
[4] = {'t','l','b','r'};
395 if (!ME_StreamOutPrint(pStream
, "\\trowd"))
397 if (!editor
->bEmulateVersion10
) /* v4.1 */
399 PARAFORMAT2
*pFmt
= &table_row_end( para
)->fmt
;
400 cell
= table_row_first_cell( para
);
403 sprintf(props
+ strlen(props
), "\\trgaph%d", pFmt
->dxOffset
);
404 if (pFmt
->dxStartIndent
)
405 sprintf(props
+ strlen(props
), "\\trleft%d", 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
)
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] = { ¶
->border
.top
,
431 ¶
->border
.bottom
,
432 ¶
->border
.right
};
433 PARAFORMAT2
*pFmt
= ¶
->fmt
;
435 assert( !(para
->nFlags
& (MEPF_ROWSTART
| MEPF_ROWEND
| MEPF_CELL
)) );
437 sprintf(props
+ strlen(props
), "\\trgaph%d", pFmt
->dxOffset
);
438 if (pFmt
->dxStartIndent
)
439 sprintf(props
+ strlen(props
), "\\trleft%d", pFmt
->dxStartIndent
);
440 for (i
= 0; i
< 4; i
++)
442 if (borders
[i
]->width
)
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%d", pFmt
->rgxTabs
[i
] & 0x00FFFFFF);
458 if (!ME_StreamOutPrint(pStream
, props
))
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
= "";
479 find_font_in_fonttbl( stream
, ¶
->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
))
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
))
495 switch (para
->fmt
.wNumbering
)
514 switch (para
->fmt
.wNumberingStyle
& 0xf00)
527 if (!ME_StreamOutPrint( stream
, fmt_label
, idx
, para
->fmt
.wNumberingTab
,
528 para
->fmt
.wNumberingStart
, type
, style
))
534 static BOOL
stream_out_para_props( ME_TextEditor
*editor
, ME_OutStream
*pStream
,
537 PARAFORMAT2
*fmt
= ¶
->fmt
;
538 char props
[STREAMOUT_BUFFER_SIZE
] = "";
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
;
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
;
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
) ))
573 return stream_out_para_num( pStream
, para
, FALSE
);
577 if (!ME_StreamOutPrint(pStream
, "\\pard"))
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
)
604 /* Default alignment: not emitted */
607 strcat(props
, "\\qr");
610 strcat(props
, "\\qc");
613 strcat(props
, "\\qj");
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");
627 case 1: /* 1.5 spacing */
628 strcat(props
, "\\sl-360\\slmult1");
630 case 2: /* Double spacing */
631 strcat(props
, "\\sl-480\\slmult1");
634 sprintf(props
+ strlen(props
), "\\sl%d\\slmult0", fmt
->dyLineSpacing
);
637 sprintf(props
+ strlen(props
), "\\sl-%d\\slmult0", fmt
->dyLineSpacing
);
640 sprintf(props
+ strlen(props
), "\\sl-%d\\slmult1", fmt
->dyLineSpacing
* 240 / 20);
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
))
666 sprintf(props
+ strlen(props
), "\\li%d", fmt
->dxOffset
);
667 if (fmt
->dxStartIndent
)
668 sprintf(props
+ strlen(props
), "\\fi%d", fmt
->dxStartIndent
);
669 if (fmt
->dxRightIndent
)
670 sprintf(props
+ strlen(props
), "\\ri%d", 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)
679 strcat(props
, "\\tqc");
682 strcat(props
, "\\tqr");
685 strcat(props
, "\\tqdec");
688 /* Word bar tab (vertical bar). Handled below */
691 if (fmt
->rgxTabs
[i
] >> 28 <= 5)
692 strcat(props
, leader
[fmt
->rgxTabs
[i
] >> 28]);
693 sprintf(props
+strlen(props
), "\\tx%d", fmt
->rgxTabs
[i
]&0x00FFFFFF);
697 if (fmt
->dySpaceAfter
)
698 sprintf(props
+ strlen(props
), "\\sa%d", fmt
->dySpaceAfter
);
699 if (fmt
->dySpaceBefore
)
700 sprintf(props
+ strlen(props
), "\\sb%d", 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",
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);
723 if (*props
&& !ME_StreamOutPrint(pStream
, props
))
731 ME_StreamOutRTFCharProps(ME_OutStream
*pStream
, CHARFORMAT2W
*fmt
)
733 char props
[STREAMOUT_BUFFER_SIZE
] = "";
735 CHARFORMAT2W
*old_fmt
= &pStream
->cur_fmt
;
739 const char *on
, *off
;
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");
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%d", fmt
->yOffset
);
796 sprintf(props
+ strlen(props
), "\\dn%d", -fmt
->yOffset
);
798 if (old_fmt
->yHeight
!= fmt
->yHeight
)
799 sprintf(props
+ strlen(props
), "\\fs%d", 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");
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
;
818 strcat(props
, "\\ul");
820 case CFU_UNDERLINEDOTTED
:
821 strcat(props
, "\\uld");
823 case CFU_UNDERLINEDOUBLE
:
824 strcat(props
, "\\uldb");
826 case CFU_UNDERLINEWORD
:
827 strcat(props
, "\\ulw");
829 case CFU_CF1UNDERLINE
:
830 case CFU_UNDERLINENONE
:
832 strcat(props
, "\\ulnone");
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
;
850 pStream
->nCodePage
= RTFCharSetToCodePage(NULL
, pStream
->fonttbl
[i
].bCharSet
);
856 if (!ME_StreamOutPrint(pStream
, props
))
864 ME_StreamOutRTFText(ME_OutStream
*pStream
, const WCHAR
*text
, LONG nChars
)
866 char buffer
[STREAMOUT_BUFFER_SIZE
];
871 nChars
= lstrlenW(text
);
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
);
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
))
889 if (!ME_StreamOutMove(pStream
, buffer
+ pos
, nBytes
- pos
))
892 } else if (*text
< 128) {
893 if (*text
== '{' || *text
== '}' || *text
== '\\')
894 buffer
[pos
++] = '\\';
895 buffer
[pos
++] = (char)(*text
++);
898 BOOL unknown
= FALSE
;
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
905 nBytes
= WideCharToMultiByte(pStream
->nCodePage
, 0, text
, 1,
907 (pStream
->nCodePage
== CP_SYMBOL
) ? NULL
: &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
;
915 for (i
= 0; i
< nBytes
; i
++)
916 pos
+= sprintf(buffer
+ pos
, "\\'%02x", (BYTE
)letter
[i
]);
921 if (pos
>= STREAMOUT_BUFFER_SIZE
- 11) {
922 if (!ME_StreamOutMove(pStream
, buffer
, pos
))
927 return ME_StreamOutMove(pStream
, buffer
, pos
);
930 static BOOL
stream_out_graphics( ME_TextEditor
*editor
, ME_OutStream
*stream
,
935 FORMATETC fmt
= { CF_ENHMETAFILE
, NULL
, DVASPECT_CONTENT
, -1, TYMED_ENHMF
};
936 STGMEDIUM med
= { TYMED_NULL
};
938 ENHMETAHEADER
*emf_bits
= NULL
;
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
))
980 if (!ME_StreamOutHexData( stream
, (BYTE
*)emf_bits
, size
))
983 if (!ME_StreamOutPrint( stream
, "}}\n" ))
989 ME_DestroyContext( &c
);
990 ITextHost_TxReleaseDC( editor
->texthost
, hdc
);
991 HeapFree( GetProcessHeap(), 0, emf_bits
);
992 ReleaseStgMedium( &med
);
993 IDataObject_Release( data
);
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
))
1009 if (!stream_out_font_and_colour_tbls( pStream
, cursor
.run
, endCur
.run
))
1012 /* TODO: stylesheet table */
1014 if (!ME_StreamOutPrint(pStream
, "{\\*\\generator Wine Riched20 2.0;}\r\n"))
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
))
1034 if (cursor
.run
== endCur
.run
&& !endCur
.nOffset
)
1037 TRACE("flags %xh\n", cursor
.run
->nFlags
);
1038 /* TODO: emit embedded objects */
1039 if (cursor
.para
->nFlags
& (MEPF_ROWSTART
| MEPF_ROWEND
))
1041 if (cursor
.run
->nFlags
& MERF_GRAPHICS
)
1043 if (!stream_out_graphics( editor
, pStream
, cursor
.run
))
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 "))
1056 if (!ME_StreamOutPrint(pStream
, "\\tab "))
1060 else if (cursor
.run
->nFlags
& MERF_ENDCELL
)
1062 if (pStream
->nNestingLevel
> 1)
1064 if (!ME_StreamOutPrint(pStream
, "\\nestcell "))
1069 if (!ME_StreamOutPrint(pStream
, "\\cell "))
1074 else if (cursor
.run
->nFlags
& MERF_ENDPARA
)
1076 if (!ME_StreamOutRTFCharProps( pStream
, &cursor
.run
->style
->fmt
))
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"))
1087 if (!ME_StreamOutPrint(pStream
, "\\par\r\n"))
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"))
1103 TRACE("style %p\n", cursor
.run
->style
);
1104 if (!ME_StreamOutRTFCharProps( pStream
, &cursor
.run
->style
->fmt
))
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
))
1113 } while (cursor
.run
!= endCur
.run
&& cursor_next_run( &cursor
, TRUE
));
1115 if (!ME_StreamOutMove(pStream
, "}\0", 2))
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
;
1126 UINT nCodePage
= CP_ACP
;
1127 char *buffer
= NULL
;
1129 BOOL success
= TRUE
;
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
));
1149 success
= ME_StreamOutMove(pStream
, "\r\n", 2);
1151 if (dwFormat
& SF_UNICODE
)
1152 success
= ME_StreamOutMove(pStream
, (const char *)(get_text( cursor
.run
, cursor
.nOffset
)),
1153 sizeof(WCHAR
) * nLen
);
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
);
1163 WideCharToMultiByte(nCodePage
, 0, get_text( cursor
.run
, cursor
.nOffset
),
1164 nLen
, buffer
, nSize
, NULL
, NULL
);
1165 success
= ME_StreamOutMove(pStream
, buffer
, nSize
);
1171 cursor
.run
= run_next_all_paras( cursor
.run
);
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
);
1195 ME_StreamOut(ME_TextEditor
*editor
, DWORD dwFormat
, EDITSTREAM
*stream
)
1200 if (dwFormat
& SFF_SELECTION
) {
1202 start
= editor
->pCursors
[ME_GetSelectionOfs(editor
, &nStart
, &nTo
)];
1203 nChars
= nTo
- nStart
;
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
)
1211 return ME_StreamOutRange(editor
, dwFormat
, &start
, nChars
, stream
);