Bump version to 5.0-14
[LibreOffice.git] / svtools / source / svrtf / rtfout.cxx
blob517e831424355debf6d1d4a5acae9e052828dbfc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/debug.hxx>
21 #include <tools/stream.hxx>
22 #include <rtl/string.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include <svtools/rtfkeywd.hxx>
25 #include <svtools/rtfout.hxx>
27 SvStream& RTFOutFuncs::Out_Char(SvStream& rStream, sal_Unicode c,
28 int *pUCMode, rtl_TextEncoding eDestEnc, bool bWriteHelpFile)
30 const sal_Char* pStr = 0;
31 switch (c)
33 case 0x1:
34 case 0x2:
35 // this are control character of our textattributes and will never be
36 // written
37 break;
38 case 0xA0:
39 rStream.WriteCharPtr( "\\~" );
40 break;
41 case 0xAD:
42 rStream.WriteCharPtr( "\\-" );
43 break;
44 case 0x2011:
45 rStream.WriteCharPtr( "\\_" );
46 break;
47 case '\n':
48 pStr = OOO_STRING_SVTOOLS_RTF_LINE;
49 break;
50 case '\t':
51 pStr = OOO_STRING_SVTOOLS_RTF_TAB;
52 break;
53 default:
54 if(!bWriteHelpFile)
56 switch(c)
58 case 149:
59 pStr = OOO_STRING_SVTOOLS_RTF_BULLET;
60 break;
61 case 150:
62 pStr = OOO_STRING_SVTOOLS_RTF_ENDASH;
63 break;
64 case 151:
65 pStr = OOO_STRING_SVTOOLS_RTF_EMDASH;
66 break;
67 case 145:
68 pStr = OOO_STRING_SVTOOLS_RTF_LQUOTE;
69 break;
70 case 146:
71 pStr = OOO_STRING_SVTOOLS_RTF_RQUOTE;
72 break;
73 case 147:
74 pStr = OOO_STRING_SVTOOLS_RTF_LDBLQUOTE;
75 break;
76 case 148:
77 pStr = OOO_STRING_SVTOOLS_RTF_RDBLQUOTE;
78 break;
81 if (pStr)
82 break;
85 switch (c)
87 case '\\':
88 case '}':
89 case '{':
90 rStream.WriteChar( '\\' ).WriteChar( c );
91 break;
92 default:
93 if (c >= ' ' && c <= '~')
94 rStream.WriteChar( c );
95 else
97 //If we can't convert to the dest encoding, or if
98 //its an uncommon multibyte sequence which most
99 //readers won't be able to handle correctly, then
100 //If we can't convert to the dest encoding, then
101 //export as unicode
102 OUString sBuf(&c, 1);
103 OString sConverted;
104 sal_uInt32 nFlags =
105 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
106 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR;
107 bool bWriteAsUnicode = !(sBuf.convertToString(&sConverted,
108 eDestEnc, nFlags))
109 || (RTL_TEXTENCODING_UTF8==eDestEnc); // #i43933# do not export UTF-8 chars in RTF;
110 if (bWriteAsUnicode)
112 sBuf.convertToString(&sConverted,
113 eDestEnc, OUSTRING_TO_OSTRING_CVTFLAGS);
115 const sal_Int32 nLen = sConverted.getLength();
117 if (bWriteAsUnicode && pUCMode)
119 // then write as unicode - character
120 if (*pUCMode != nLen)
122 // #i47831# add an additional whitespace, so that
123 // "document whitespaces" are not ignored.;
124 rStream.WriteCharPtr( "\\uc" )
125 .WriteCharPtr( OString::number(nLen).getStr() ).WriteCharPtr( " " );
126 *pUCMode = nLen;
128 rStream.WriteCharPtr( "\\u" )
129 .WriteCharPtr( OString::number(
130 static_cast<sal_Int32>(c)).getStr() );
133 for (sal_Int32 nI = 0; nI < nLen; ++nI)
135 rStream.WriteCharPtr( "\\'" );
136 Out_Hex(rStream, sConverted[nI], 2);
139 break;
141 break;
144 if (pStr)
145 rStream.WriteCharPtr( pStr ).WriteChar( ' ' );
147 return rStream;
150 SvStream& RTFOutFuncs::Out_String( SvStream& rStream, const OUString& rStr,
151 rtl_TextEncoding eDestEnc, bool bWriteHelpFile)
153 int nUCMode = 1;
154 for (sal_Int32 n = 0; n < rStr.getLength(); ++n)
155 Out_Char(rStream, rStr[n], &nUCMode, eDestEnc, bWriteHelpFile);
156 if (nUCMode != 1)
157 rStream.WriteCharPtr( "\\uc1" ).WriteCharPtr( " " ); // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
158 return rStream;
161 SvStream& RTFOutFuncs::Out_Hex( SvStream& rStream, sal_uLong nHex, sal_uInt8 nLen )
163 sal_Char aNToABuf[] = "0000000000000000";
165 DBG_ASSERT( nLen < sizeof(aNToABuf), "zu viele Stellen" );
166 if( nLen >= sizeof(aNToABuf) )
167 nLen = (sizeof(aNToABuf)-1);
169 // set pointer to end of buffer
170 sal_Char* pStr = aNToABuf + (sizeof(aNToABuf)-1);
171 for( sal_uInt8 n = 0; n < nLen; ++n )
173 *(--pStr) = (sal_Char)(nHex & 0xf ) + 48;
174 if( *pStr > '9' )
175 *pStr += 39;
176 nHex >>= 4;
178 return rStream.WriteCharPtr( pStr );
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */