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