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 <tools/string.hxx>
23 : #include <rtl/string.hxx>
24 : #include <rtl/ustrbuf.hxx>
25 : #include <svtools/rtfkeywd.hxx>
26 : #include <svtools/rtfout.hxx>
27 :
28 :
29 : #if defined(UNX)
30 : const sal_Char RTFOutFuncs::sNewLine = '\012';
31 : #else
32 : const sal_Char RTFOutFuncs::sNewLine[] = "\015\012";
33 : #endif
34 :
35 :
36 0 : SvStream& RTFOutFuncs::Out_Char(SvStream& rStream, sal_Unicode c,
37 : int *pUCMode, rtl_TextEncoding eDestEnc, sal_Bool bWriteHelpFile)
38 : {
39 0 : const sal_Char* pStr = 0;
40 0 : switch (c)
41 : {
42 : case 0x1:
43 : case 0x2:
44 : // this are control character of our textattributes and will never be
45 : // written
46 0 : break;
47 : case 0xA0:
48 0 : rStream << "\\~";
49 0 : break;
50 : case 0xAD:
51 0 : rStream << "\\-";
52 0 : break;
53 : case 0x2011:
54 0 : rStream << "\\_";
55 0 : break;
56 : case '\n':
57 0 : pStr = OOO_STRING_SVTOOLS_RTF_LINE;
58 0 : break;
59 : case '\t':
60 0 : pStr = OOO_STRING_SVTOOLS_RTF_TAB;
61 0 : break;
62 : default:
63 0 : if(!bWriteHelpFile)
64 : {
65 0 : switch(c)
66 : {
67 : case 149:
68 0 : pStr = OOO_STRING_SVTOOLS_RTF_BULLET;
69 0 : break;
70 : case 150:
71 0 : pStr = OOO_STRING_SVTOOLS_RTF_ENDASH;
72 0 : break;
73 : case 151:
74 0 : pStr = OOO_STRING_SVTOOLS_RTF_EMDASH;
75 0 : break;
76 : case 145:
77 0 : pStr = OOO_STRING_SVTOOLS_RTF_LQUOTE;
78 0 : break;
79 : case 146:
80 0 : pStr = OOO_STRING_SVTOOLS_RTF_RQUOTE;
81 0 : break;
82 : case 147:
83 0 : pStr = OOO_STRING_SVTOOLS_RTF_LDBLQUOTE;
84 0 : break;
85 : case 148:
86 0 : pStr = OOO_STRING_SVTOOLS_RTF_RDBLQUOTE;
87 0 : break;
88 : }
89 :
90 0 : if (pStr)
91 0 : break;
92 : }
93 :
94 0 : switch (c)
95 : {
96 : case '\\':
97 : case '}':
98 : case '{':
99 0 : rStream << '\\' << (sal_Char)c;
100 0 : break;
101 : default:
102 0 : if (c >= ' ' && c <= '~')
103 0 : rStream << (sal_Char)c;
104 : else
105 : {
106 : //If we can't convert to the dest encoding, or if
107 : //its an uncommon multibyte sequence which most
108 : //readers won't be able to handle correctly, then
109 : //If we can't convert to the dest encoding, then
110 : //export as unicode
111 0 : OUString sBuf(&c, 1);
112 0 : OString sConverted;
113 : sal_uInt32 nFlags =
114 : RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
115 0 : RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR;
116 : bool bWriteAsUnicode = !(sBuf.convertToString(&sConverted,
117 0 : eDestEnc, nFlags))
118 0 : || (RTL_TEXTENCODING_UTF8==eDestEnc); // #i43933# do not export UTF-8 chars in RTF;
119 0 : if (bWriteAsUnicode)
120 : {
121 : sBuf.convertToString(&sConverted,
122 0 : eDestEnc, OUSTRING_TO_OSTRING_CVTFLAGS);
123 : }
124 0 : const sal_Int32 nLen = sConverted.getLength();
125 :
126 0 : if (bWriteAsUnicode && pUCMode)
127 : {
128 : // then write as unicode - character
129 0 : if (*pUCMode != nLen)
130 : {
131 : // #i47831# add an additional whitespace, so that
132 : // "document whitespaces" are not ignored.;
133 0 : rStream << "\\uc"
134 0 : << OString::valueOf(nLen).getStr() << " ";
135 0 : *pUCMode = nLen;
136 : }
137 0 : rStream << "\\u"
138 : << OString::valueOf(
139 0 : static_cast<sal_Int32>(c)).getStr();
140 : }
141 :
142 0 : for (sal_Int32 nI = 0; nI < nLen; ++nI)
143 : {
144 0 : rStream << "\\'";
145 0 : Out_Hex(rStream, sConverted.getStr()[nI], 2);
146 0 : }
147 : }
148 0 : break;
149 : }
150 0 : break;
151 : }
152 :
153 0 : if (pStr)
154 0 : rStream << pStr << ' ';
155 :
156 0 : return rStream;
157 : }
158 :
159 0 : SvStream& RTFOutFuncs::Out_String( SvStream& rStream, const String& rStr,
160 : rtl_TextEncoding eDestEnc, sal_Bool bWriteHelpFile)
161 : {
162 0 : int nUCMode = 1;
163 0 : for (xub_StrLen n = 0; n < rStr.Len(); ++n)
164 0 : Out_Char(rStream, rStr.GetChar(n), &nUCMode, eDestEnc, bWriteHelpFile);
165 0 : if (nUCMode != 1)
166 0 : rStream << "\\uc1"<< " "; // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
167 0 : return rStream;
168 : }
169 :
170 0 : SvStream& RTFOutFuncs::Out_Hex( SvStream& rStream, sal_uLong nHex, sal_uInt8 nLen )
171 : {
172 0 : sal_Char aNToABuf[] = "0000000000000000";
173 :
174 : DBG_ASSERT( nLen < sizeof(aNToABuf), "zu viele Stellen" );
175 0 : if( nLen >= sizeof(aNToABuf) )
176 0 : nLen = (sizeof(aNToABuf)-1);
177 :
178 : // Pointer an das Bufferende setzen
179 0 : sal_Char* pStr = aNToABuf + (sizeof(aNToABuf)-1);
180 0 : for( sal_uInt8 n = 0; n < nLen; ++n )
181 : {
182 0 : *(--pStr) = (sal_Char)(nHex & 0xf ) + 48;
183 0 : if( *pStr > '9' )
184 0 : *pStr += 39;
185 0 : nHex >>= 4;
186 : }
187 0 : return rStream << pStr;
188 : }
189 :
190 :
191 :
192 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|