Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
26 :
27 0 : SvStream& RTFOutFuncs::Out_Char(SvStream& rStream, sal_Unicode c,
28 : int *pUCMode, rtl_TextEncoding eDestEnc, bool bWriteHelpFile)
29 : {
30 0 : const sal_Char* pStr = 0;
31 0 : switch (c)
32 : {
33 : case 0x1:
34 : case 0x2:
35 : // this are control character of our textattributes and will never be
36 : // written
37 0 : break;
38 : case 0xA0:
39 0 : rStream.WriteCharPtr( "\\~" );
40 0 : break;
41 : case 0xAD:
42 0 : rStream.WriteCharPtr( "\\-" );
43 0 : break;
44 : case 0x2011:
45 0 : rStream.WriteCharPtr( "\\_" );
46 0 : break;
47 : case '\n':
48 0 : pStr = OOO_STRING_SVTOOLS_RTF_LINE;
49 0 : break;
50 : case '\t':
51 0 : pStr = OOO_STRING_SVTOOLS_RTF_TAB;
52 0 : break;
53 : default:
54 0 : if(!bWriteHelpFile)
55 : {
56 0 : switch(c)
57 : {
58 : case 149:
59 0 : pStr = OOO_STRING_SVTOOLS_RTF_BULLET;
60 0 : break;
61 : case 150:
62 0 : pStr = OOO_STRING_SVTOOLS_RTF_ENDASH;
63 0 : break;
64 : case 151:
65 0 : pStr = OOO_STRING_SVTOOLS_RTF_EMDASH;
66 0 : break;
67 : case 145:
68 0 : pStr = OOO_STRING_SVTOOLS_RTF_LQUOTE;
69 0 : break;
70 : case 146:
71 0 : pStr = OOO_STRING_SVTOOLS_RTF_RQUOTE;
72 0 : break;
73 : case 147:
74 0 : pStr = OOO_STRING_SVTOOLS_RTF_LDBLQUOTE;
75 0 : break;
76 : case 148:
77 0 : pStr = OOO_STRING_SVTOOLS_RTF_RDBLQUOTE;
78 0 : break;
79 : }
80 :
81 0 : if (pStr)
82 0 : break;
83 : }
84 :
85 0 : switch (c)
86 : {
87 : case '\\':
88 : case '}':
89 : case '{':
90 0 : rStream.WriteChar( '\\' ).WriteChar( (sal_Char)c );
91 0 : break;
92 : default:
93 0 : if (c >= ' ' && c <= '~')
94 0 : rStream.WriteChar( (sal_Char)c );
95 : else
96 : {
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 0 : OUString sBuf(&c, 1);
103 0 : OString sConverted;
104 : sal_uInt32 nFlags =
105 : RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
106 0 : RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR;
107 : bool bWriteAsUnicode = !(sBuf.convertToString(&sConverted,
108 0 : eDestEnc, nFlags))
109 0 : || (RTL_TEXTENCODING_UTF8==eDestEnc); // #i43933# do not export UTF-8 chars in RTF;
110 0 : if (bWriteAsUnicode)
111 : {
112 : sBuf.convertToString(&sConverted,
113 0 : eDestEnc, OUSTRING_TO_OSTRING_CVTFLAGS);
114 : }
115 0 : const sal_Int32 nLen = sConverted.getLength();
116 :
117 0 : if (bWriteAsUnicode && pUCMode)
118 : {
119 : // then write as unicode - character
120 0 : if (*pUCMode != nLen)
121 : {
122 : // #i47831# add an additional whitespace, so that
123 : // "document whitespaces" are not ignored.;
124 0 : rStream.WriteCharPtr( "\\uc" )
125 0 : .WriteCharPtr( OString::number(nLen).getStr() ).WriteCharPtr( " " );
126 0 : *pUCMode = nLen;
127 : }
128 0 : rStream.WriteCharPtr( "\\u" )
129 : .WriteCharPtr( OString::number(
130 0 : static_cast<sal_Int32>(c)).getStr() );
131 : }
132 :
133 0 : for (sal_Int32 nI = 0; nI < nLen; ++nI)
134 : {
135 0 : rStream.WriteCharPtr( "\\'" );
136 0 : Out_Hex(rStream, sConverted[nI], 2);
137 0 : }
138 : }
139 0 : break;
140 : }
141 0 : break;
142 : }
143 :
144 0 : if (pStr)
145 0 : rStream.WriteCharPtr( pStr ).WriteChar( ' ' );
146 :
147 0 : return rStream;
148 : }
149 :
150 0 : SvStream& RTFOutFuncs::Out_String( SvStream& rStream, const OUString& rStr,
151 : rtl_TextEncoding eDestEnc, bool bWriteHelpFile)
152 : {
153 0 : int nUCMode = 1;
154 0 : for (sal_Int32 n = 0; n < rStr.getLength(); ++n)
155 0 : Out_Char(rStream, rStr[n], &nUCMode, eDestEnc, bWriteHelpFile);
156 0 : if (nUCMode != 1)
157 0 : rStream.WriteCharPtr( "\\uc1" ).WriteCharPtr( " " ); // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
158 0 : return rStream;
159 : }
160 :
161 0 : SvStream& RTFOutFuncs::Out_Hex( SvStream& rStream, sal_uLong nHex, sal_uInt8 nLen )
162 : {
163 0 : sal_Char aNToABuf[] = "0000000000000000";
164 :
165 : DBG_ASSERT( nLen < sizeof(aNToABuf), "zu viele Stellen" );
166 0 : if( nLen >= sizeof(aNToABuf) )
167 0 : nLen = (sizeof(aNToABuf)-1);
168 :
169 : // set pointer to end of buffer
170 0 : sal_Char* pStr = aNToABuf + (sizeof(aNToABuf)-1);
171 0 : for( sal_uInt8 n = 0; n < nLen; ++n )
172 : {
173 0 : *(--pStr) = (sal_Char)(nHex & 0xf ) + 48;
174 0 : if( *pStr > '9' )
175 0 : *pStr += 39;
176 0 : nHex >>= 4;
177 : }
178 0 : return rStream.WriteCharPtr( pStr );
179 : }
180 :
181 :
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|