Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Initial Developer of the Original Code is
16 : * Miklos Vajna <vmiklos@suse.cz> (SUSE, Inc.)
17 : * Portions created by the Initial Developer are Copyright (C) 2012 the
18 : * Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s):
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : #include <filter/msfilter/rtfutil.hxx>
30 : #include <rtl/strbuf.hxx>
31 : #include <svtools/rtfkeywd.hxx>
32 :
33 : namespace msfilter {
34 : namespace rtfutil {
35 :
36 63 : OString OutHex(sal_uLong nHex, sal_uInt8 nLen)
37 : {
38 63 : sal_Char aNToABuf[] = "0000000000000000";
39 :
40 : OSL_ENSURE( nLen < sizeof(aNToABuf), "nLen is too big" );
41 63 : if( nLen >= sizeof(aNToABuf) )
42 0 : nLen = (sizeof(aNToABuf)-1);
43 :
44 : // Set pointer to the buffer end
45 63 : sal_Char* pStr = aNToABuf + (sizeof(aNToABuf)-1);
46 189 : for( sal_uInt8 n = 0; n < nLen; ++n )
47 : {
48 126 : *(--pStr) = (sal_Char)(nHex & 0xf ) + 48;
49 126 : if( *pStr > '9' )
50 38 : *pStr += 39;
51 126 : nHex >>= 4;
52 : }
53 63 : return OString(pStr);
54 : }
55 :
56 2593 : OString OutChar(sal_Unicode c, int *pUCMode, rtl_TextEncoding eDestEnc)
57 : {
58 2593 : OStringBuffer aBuf;
59 2593 : const sal_Char* pStr = 0;
60 : // 0x0b instead of \n, etc because of the replacements in SwWW8AttrIter::GetSnippet()
61 2593 : switch (c)
62 : {
63 : case 0x0b:
64 : // hard line break
65 0 : pStr = OOO_STRING_SVTOOLS_RTF_LINE;
66 0 : break;
67 : case '\t':
68 1 : pStr = OOO_STRING_SVTOOLS_RTF_TAB;
69 1 : break;
70 : case '\\':
71 : case '}':
72 : case '{':
73 4 : aBuf.append('\\');
74 4 : aBuf.append((sal_Char)c);
75 4 : break;
76 : case 0xa0:
77 : // non-breaking space
78 1 : pStr = "\\~";
79 1 : break;
80 : case 0x1e:
81 : // non-breaking hyphen
82 1 : pStr = "\\_";
83 1 : break;
84 : case 0x1f:
85 : // optional hyphen
86 1 : pStr = "\\-";
87 1 : break;
88 : default:
89 2585 : if (c >= ' ' && c <= '~')
90 2522 : aBuf.append((sal_Char)c);
91 : else {
92 63 : OUString sBuf(&c, 1);
93 63 : OString sConverted;
94 63 : sBuf.convertToString(&sConverted, eDestEnc, OUSTRING_TO_OSTRING_CVTFLAGS);
95 63 : const sal_Int32 nLen = sConverted.getLength();
96 :
97 63 : if (pUCMode)
98 : {
99 63 : if (*pUCMode != nLen)
100 : {
101 0 : aBuf.append("\\uc");
102 0 : aBuf.append((sal_Int32)nLen);
103 : // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.
104 0 : aBuf.append(' ');
105 0 : *pUCMode = nLen;
106 : }
107 63 : aBuf.append("\\u");
108 63 : aBuf.append((sal_Int32)c);
109 : }
110 :
111 126 : for (sal_Int32 nI = 0; nI < nLen; ++nI)
112 : {
113 63 : aBuf.append("\\'");
114 63 : aBuf.append(OutHex(sConverted.getStr()[nI], 2));
115 63 : }
116 : }
117 : }
118 2593 : if (pStr) {
119 4 : aBuf.append(pStr);
120 4 : switch (c)
121 : {
122 : case 0xa0:
123 : case 0x1e:
124 : case 0x1f:
125 3 : break;
126 : default:
127 1 : aBuf.append(' ');
128 : }
129 : }
130 2593 : return aBuf.makeStringAndClear();
131 : }
132 :
133 727 : OString OutString(const String &rStr, rtl_TextEncoding eDestEnc)
134 : {
135 : SAL_INFO("filter.ms", OSL_THIS_FUNC << ", rStr = '" << OUString(rStr) << "'");
136 727 : OStringBuffer aBuf;
137 727 : int nUCMode = 1;
138 3320 : for (xub_StrLen n = 0; n < rStr.Len(); ++n)
139 2593 : aBuf.append(OutChar(rStr.GetChar(n), &nUCMode, eDestEnc));
140 727 : if (nUCMode != 1) {
141 0 : aBuf.append(OOO_STRING_SVTOOLS_RTF_UC);
142 0 : aBuf.append((sal_Int32)1);
143 0 : aBuf.append(" "); // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
144 : }
145 727 : return aBuf.makeStringAndClear();
146 : }
147 :
148 : }
149 : }
150 :
151 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|