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 :
10 : #include <filter/msfilter/rtfutil.hxx>
11 : #include <rtl/strbuf.hxx>
12 : #include <svtools/rtfkeywd.hxx>
13 :
14 : namespace msfilter
15 : {
16 : namespace rtfutil
17 : {
18 :
19 964 : OString OutHex(sal_uLong nHex, sal_uInt8 nLen)
20 : {
21 964 : sal_Char aNToABuf[] = "0000000000000000";
22 :
23 : OSL_ENSURE(nLen < sizeof(aNToABuf), "nLen is too big");
24 964 : if (nLen >= sizeof(aNToABuf))
25 0 : nLen = (sizeof(aNToABuf)-1);
26 :
27 : // Set pointer to the buffer end
28 964 : sal_Char* pStr = aNToABuf + (sizeof(aNToABuf)-1);
29 2892 : for (sal_uInt8 n = 0; n < nLen; ++n)
30 : {
31 1928 : *(--pStr) = (sal_Char)(nHex & 0xf) + 48;
32 1928 : if (*pStr > '9')
33 350 : *pStr += 39;
34 1928 : nHex >>= 4;
35 : }
36 964 : return OString(pStr);
37 : }
38 :
39 45398 : OString OutChar(sal_Unicode c, int* pUCMode, rtl_TextEncoding eDestEnc, bool* pSuccess, bool bUnicode)
40 : {
41 45398 : if (pSuccess)
42 74 : *pSuccess = true;
43 45398 : OStringBuffer aBuf;
44 45398 : const sal_Char* pStr = 0;
45 : // 0x0b instead of \n, etc because of the replacements in SwWW8AttrIter::GetSnippet()
46 45398 : switch (c)
47 : {
48 : case 0x0b:
49 : // hard line break
50 0 : pStr = OOO_STRING_SVTOOLS_RTF_LINE;
51 0 : break;
52 : case '\t':
53 94 : pStr = OOO_STRING_SVTOOLS_RTF_TAB;
54 94 : break;
55 : case '\\':
56 : case '}':
57 : case '{':
58 12 : aBuf.append('\\');
59 12 : aBuf.append((sal_Char)c);
60 12 : break;
61 : case 0xa0:
62 : // non-breaking space
63 2 : pStr = "\\~";
64 2 : break;
65 : case 0x1e:
66 : // non-breaking hyphen
67 2 : pStr = "\\_";
68 2 : break;
69 : case 0x1f:
70 : // optional hyphen
71 2 : pStr = "\\-";
72 2 : break;
73 : default:
74 45286 : if (c >= ' ' && c <= '~')
75 44760 : aBuf.append((sal_Char)c);
76 : else
77 : {
78 526 : OUString sBuf(&c, 1);
79 1052 : OString sConverted;
80 526 : if (pSuccess)
81 8 : *pSuccess &= sBuf.convertToString(&sConverted, eDestEnc, RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR);
82 : else
83 518 : sBuf.convertToString(&sConverted, eDestEnc, OUSTRING_TO_OSTRING_CVTFLAGS);
84 526 : const sal_Int32 nLen = sConverted.getLength();
85 :
86 526 : if (pUCMode && bUnicode)
87 : {
88 322 : if (*pUCMode != nLen)
89 : {
90 4 : aBuf.append("\\uc");
91 4 : aBuf.append((sal_Int32)nLen);
92 : // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.
93 4 : aBuf.append(' ');
94 4 : *pUCMode = nLen;
95 : }
96 322 : aBuf.append("\\u");
97 322 : aBuf.append((sal_Int32)c);
98 : }
99 :
100 1068 : for (sal_Int32 nI = 0; nI < nLen; ++nI)
101 : {
102 542 : aBuf.append("\\'");
103 542 : aBuf.append(OutHex(sConverted[nI], 2));
104 526 : }
105 : }
106 : }
107 45398 : if (pStr)
108 : {
109 100 : aBuf.append(pStr);
110 100 : switch (c)
111 : {
112 : case 0xa0:
113 : case 0x1e:
114 : case 0x1f:
115 6 : break;
116 : default:
117 94 : aBuf.append(' ');
118 : }
119 : }
120 45398 : return aBuf.makeStringAndClear();
121 : }
122 :
123 5524 : OString OutString(const OUString& rStr, rtl_TextEncoding eDestEnc, bool bUnicode)
124 : {
125 : SAL_INFO("filter.ms", OSL_THIS_FUNC << ", rStr = '" << OUString(rStr) << "'");
126 5524 : OStringBuffer aBuf;
127 5524 : int nUCMode = 1;
128 50848 : for (sal_Int32 n = 0; n < rStr.getLength(); ++n)
129 45324 : aBuf.append(OutChar(rStr[n], &nUCMode, eDestEnc, 0, bUnicode));
130 5524 : if (nUCMode != 1)
131 : {
132 0 : aBuf.append(OOO_STRING_SVTOOLS_RTF_UC);
133 0 : aBuf.append((sal_Int32)1);
134 0 : aBuf.append(" "); // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
135 : }
136 5524 : return aBuf.makeStringAndClear();
137 : }
138 :
139 : /// Checks if lossless conversion of the string to eDestEnc is possible or not.
140 8 : static bool TryOutString(const OUString& rStr, rtl_TextEncoding eDestEnc)
141 : {
142 8 : int nUCMode = 1;
143 78 : for (sal_Int32 n = 0; n < rStr.getLength(); ++n)
144 : {
145 : bool bRet;
146 74 : OutChar(rStr[n], &nUCMode, eDestEnc, &bRet);
147 74 : if (!bRet)
148 4 : return false;
149 : }
150 4 : return true;
151 : }
152 :
153 8 : OString OutStringUpr(const sal_Char* pToken, const OUString& rStr, rtl_TextEncoding eDestEnc)
154 : {
155 8 : if (TryOutString(rStr, eDestEnc))
156 4 : return OString("{") + pToken + " " + OutString(rStr, eDestEnc) + "}";
157 :
158 4 : OStringBuffer aRet;
159 4 : aRet.append("{" OOO_STRING_SVTOOLS_RTF_UPR "{");
160 4 : aRet.append(pToken);
161 4 : aRet.append(" ");
162 4 : aRet.append(OutString(rStr, eDestEnc, /*bUnicode =*/ false));
163 4 : aRet.append("}{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_UD "{");
164 4 : aRet.append(pToken);
165 4 : aRet.append(" ");
166 4 : aRet.append(OutString(rStr, eDestEnc));
167 4 : aRet.append("}}}");
168 4 : return aRet.makeStringAndClear();
169 : }
170 : }
171 : }
172 :
173 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|