1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtfout.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #include <tools/debug.hxx>
34 #include <tools/stream.hxx>
35 #include <tools/string.hxx>
36 #include <rtl/string.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <rtfkeywd.hxx>
44 const sal_Char
RTFOutFuncs::sNewLine
= '\012';
46 const sal_Char __FAR_DATA
RTFOutFuncs::sNewLine
[] = "\015\012";
50 SvStream
& RTFOutFuncs::Out_Char(SvStream
& rStream
, sal_Unicode c
,
51 int *pUCMode
, rtl_TextEncoding eDestEnc
, BOOL bWriteHelpFile
)
53 const sal_Char
* pStr
= 0;
58 // this are control character of our textattributes and will never be
71 pStr
= OOO_STRING_SVTOOLS_RTF_LINE
;
74 pStr
= OOO_STRING_SVTOOLS_RTF_TAB
;
82 pStr
= OOO_STRING_SVTOOLS_RTF_BULLET
;
85 pStr
= OOO_STRING_SVTOOLS_RTF_ENDASH
;
88 pStr
= OOO_STRING_SVTOOLS_RTF_EMDASH
;
91 pStr
= OOO_STRING_SVTOOLS_RTF_LQUOTE
;
94 pStr
= OOO_STRING_SVTOOLS_RTF_RQUOTE
;
97 pStr
= OOO_STRING_SVTOOLS_RTF_LDBLQUOTE
;
100 pStr
= OOO_STRING_SVTOOLS_RTF_RDBLQUOTE
;
113 rStream
<< '\\' << (sal_Char
)c
;
116 if (c
>= ' ' && c
<= '~')
117 rStream
<< (sal_Char
)c
;
120 //If we can't convert to the dest encoding, or if
121 //its an uncommon multibyte sequence which most
122 //readers won't be able to handle correctly, then
123 //If we can't convert to the dest encoding, then
125 OUString
sBuf(&c
, 1);
128 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
|
129 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
;
130 bool bWriteAsUnicode
= !(sBuf
.convertToString(&sConverted
,
132 || (RTL_TEXTENCODING_UTF8
==eDestEnc
); // #i43933# do not export UTF-8 chars in RTF;
135 sBuf
.convertToString(&sConverted
,
136 eDestEnc
, OUSTRING_TO_OSTRING_CVTFLAGS
);
138 const sal_Int32 nLen
= sConverted
.getLength();
140 if (bWriteAsUnicode
&& pUCMode
)
142 // then write as unicode - character
143 if (*pUCMode
!= nLen
)
145 rStream
<< "\\uc" << ByteString::CreateFromInt32(nLen
).GetBuffer() << " "; // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
148 ByteString
sNo(ByteString::CreateFromInt32(c
));
149 rStream
<< "\\u" << sNo
.GetBuffer();
152 for (sal_Int32 nI
= 0; nI
< nLen
; ++nI
)
155 Out_Hex(rStream
, sConverted
.getStr()[nI
], 2);
164 rStream
<< pStr
<< ' ';
169 SvStream
& RTFOutFuncs::Out_String( SvStream
& rStream
, const String
& rStr
,
170 rtl_TextEncoding eDestEnc
, BOOL bWriteHelpFile
)
173 for (xub_StrLen n
= 0; n
< rStr
.Len(); ++n
)
174 Out_Char(rStream
, rStr
.GetChar(n
), &nUCMode
, eDestEnc
, bWriteHelpFile
);
176 rStream
<< "\\uc1"<< " "; // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
180 SvStream
& RTFOutFuncs::Out_Fontname(SvStream
& rStream
, const String
& rStr
,
181 rtl_TextEncoding eDestEnc
, BOOL bWriteHelpFile
)
183 //Fontnames in word have a quirk in that \uc and usage of ansi replacement
184 //chars after a \u don't work and in wordpad \u doesn't work, so we are
185 //left with forcing ansi characters only for fontnames
186 for (xub_StrLen n
= 0; n
< rStr
.Len(); ++n
)
187 Out_Char(rStream
, rStr
.GetChar(n
), 0, eDestEnc
, bWriteHelpFile
);
191 SvStream
& RTFOutFuncs::Out_Hex( SvStream
& rStream
, ULONG nHex
, BYTE nLen
)
193 sal_Char aNToABuf
[] = "0000000000000000";
195 DBG_ASSERT( nLen
< sizeof(aNToABuf
), "zu viele Stellen" );
196 if( nLen
>= sizeof(aNToABuf
) )
197 nLen
= (sizeof(aNToABuf
)-1);
199 // Pointer an das Bufferende setzen
200 sal_Char
* pStr
= aNToABuf
+ (sizeof(aNToABuf
)-1);
201 for( BYTE n
= 0; n
< nLen
; ++n
)
203 *(--pStr
) = (sal_Char
)(nHex
& 0xf ) + 48;
208 return rStream
<< pStr
;