merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / svrtf / rtfout.cxx
blobcda5b06b85500a9342b459ca3b9c182ea76a4126
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtfout.cxx,v $
10 * $Revision: 1.11 $
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>
39 #include <rtfout.hxx>
41 using namespace rtl;
43 #if defined(UNX)
44 const sal_Char RTFOutFuncs::sNewLine = '\012';
45 #else
46 const sal_Char __FAR_DATA RTFOutFuncs::sNewLine[] = "\015\012";
47 #endif
50 SvStream& RTFOutFuncs::Out_Char(SvStream& rStream, sal_Unicode c,
51 int *pUCMode, rtl_TextEncoding eDestEnc, BOOL bWriteHelpFile)
53 const sal_Char* pStr = 0;
54 switch (c)
56 case 0x1:
57 case 0x2:
58 // this are control character of our textattributes and will never be
59 // written
60 break;
61 case 0xA0:
62 rStream << "\\~";
63 break;
64 case 0xAD:
65 rStream << "\\-";
66 break;
67 case 0x2011:
68 rStream << "\\_";
69 break;
70 case '\n':
71 pStr = OOO_STRING_SVTOOLS_RTF_LINE;
72 break;
73 case '\t':
74 pStr = OOO_STRING_SVTOOLS_RTF_TAB;
75 break;
76 default:
77 if(!bWriteHelpFile)
79 switch(c)
81 case 149:
82 pStr = OOO_STRING_SVTOOLS_RTF_BULLET;
83 break;
84 case 150:
85 pStr = OOO_STRING_SVTOOLS_RTF_ENDASH;
86 break;
87 case 151:
88 pStr = OOO_STRING_SVTOOLS_RTF_EMDASH;
89 break;
90 case 145:
91 pStr = OOO_STRING_SVTOOLS_RTF_LQUOTE;
92 break;
93 case 146:
94 pStr = OOO_STRING_SVTOOLS_RTF_RQUOTE;
95 break;
96 case 147:
97 pStr = OOO_STRING_SVTOOLS_RTF_LDBLQUOTE;
98 break;
99 case 148:
100 pStr = OOO_STRING_SVTOOLS_RTF_RDBLQUOTE;
101 break;
104 if (pStr)
105 break;
108 switch (c)
110 case '\\':
111 case '}':
112 case '{':
113 rStream << '\\' << (sal_Char)c;
114 break;
115 default:
116 if (c >= ' ' && c <= '~')
117 rStream << (sal_Char)c;
118 else
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
124 //export as unicode
125 OUString sBuf(&c, 1);
126 OString sConverted;
127 sal_uInt32 nFlags =
128 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
129 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR;
130 bool bWriteAsUnicode = !(sBuf.convertToString(&sConverted,
131 eDestEnc, nFlags))
132 || (RTL_TEXTENCODING_UTF8==eDestEnc); // #i43933# do not export UTF-8 chars in RTF;
133 if (bWriteAsUnicode)
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.;
146 *pUCMode = nLen;
148 ByteString sNo(ByteString::CreateFromInt32(c));
149 rStream << "\\u" << sNo.GetBuffer();
152 for (sal_Int32 nI = 0; nI < nLen; ++nI)
154 rStream << "\\'";
155 Out_Hex(rStream, sConverted.getStr()[nI], 2);
158 break;
160 break;
163 if (pStr)
164 rStream << pStr << ' ';
166 return rStream;
169 SvStream& RTFOutFuncs::Out_String( SvStream& rStream, const String& rStr,
170 rtl_TextEncoding eDestEnc, BOOL bWriteHelpFile)
172 int nUCMode = 1;
173 for (xub_StrLen n = 0; n < rStr.Len(); ++n)
174 Out_Char(rStream, rStr.GetChar(n), &nUCMode, eDestEnc, bWriteHelpFile);
175 if (nUCMode != 1)
176 rStream << "\\uc1"<< " "; // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
177 return rStream;
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);
188 return rStream;
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;
204 if( *pStr > '9' )
205 *pStr += 39;
206 nHex >>= 4;
208 return rStream << pStr;