LibreOffice Module svtools (master) 1
rtfout.cxx
Go to the documentation of this file.
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/solar.h>
23#include <rtl/string.hxx>
24#include <svtools/rtfkeywd.hxx>
25#include <svtools/rtfout.hxx>
26
27namespace {
28
29SvStream& Out_Hex( SvStream& rStream, sal_uLong nHex, sal_uInt8 nLen )
30{
31 char aNToABuf[] = "0000000000000000";
32
33 DBG_ASSERT( nLen < sizeof(aNToABuf), "too many places" );
34 if( nLen >= sizeof(aNToABuf) )
35 nLen = (sizeof(aNToABuf)-1);
36
37 // set pointer to end of buffer
38 char* pStr = aNToABuf + (sizeof(aNToABuf)-1);
39 for( sal_uInt8 n = 0; n < nLen; ++n )
40 {
41 *(--pStr) = static_cast<char>(nHex & 0xf ) + 48;
42 if( *pStr > '9' )
43 *pStr += 39;
44 nHex >>= 4;
45 }
46 return rStream.WriteOString( pStr );
47}
48
49// Ideally, this function should work on (sal_uInt32) Unicode scalar values
50// instead of (sal_Unicode) UTF-16 code units. However, at least "Rich Text
51// Format (RTF) Specification Version 1.9.1" available at
52// <https://www.microsoft.com/en-us/download/details.aspx?id=10725> does not
53// look like it allows non-BMP Unicode characters >= 0x10000 in the \uN notation
54// (it only talks about "Unicode character", but then explains how values of N
55// greater than 32767 will be expressed as negative signed 16-bit numbers, so
56// that smells like \uN is limited to BMP).
57// However the "Mathematics" section has an example that shows the code point
58// U+1D44E being encoded as UTF-16 surrogate pair "\u-10187?\u-9138?", so
59// sal_Unicode actually works fine here.
60SvStream& Out_Char(SvStream& rStream, sal_Unicode c,
61 int *pUCMode, rtl_TextEncoding eDestEnc)
62{
63 const char* pStr = nullptr;
64 switch (c)
65 {
66 case 0x1:
67 case 0x2:
68 // this are control character of our textattributes and will never be
69 // written
70 break;
71 case 0xA0:
72 rStream.WriteOString( "\\~" );
73 break;
74 case 0xAD:
75 rStream.WriteOString( "\\-" );
76 break;
77 case 0x2011:
78 rStream.WriteOString( "\\_" );
79 break;
80 case '\n':
82 break;
83 case '\t':
85 break;
86 default:
87 switch(c)
88 {
89 case 149:
91 break;
92 case 150:
94 break;
95 case 151:
97 break;
98 case 145:
100 break;
101 case 146:
103 break;
104 case 147:
106 break;
107 case 148:
109 break;
110 }
111
112 if (pStr)
113 break;
114
115 switch (c)
116 {
117 case '\\':
118 case '}':
119 case '{':
120 rStream.WriteChar( '\\' ).WriteChar( char(c) );
121 break;
122 default:
123 if (c >= ' ' && c <= '~')
124 rStream.WriteChar( char(c) );
125 else
126 {
127 //If we can't convert to the dest encoding, or if
128 //it's an uncommon multibyte sequence which most
129 //readers won't be able to handle correctly, then
130 //export as unicode
131 OUString sBuf(&c, 1);
132 OString sConverted;
133 sal_uInt32 const nFlags =
134 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
135 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR;
136 bool bWriteAsUnicode = !(sBuf.convertToString(&sConverted,
137 eDestEnc, nFlags))
138 || (RTL_TEXTENCODING_UTF8==eDestEnc); // #i43933# do not export UTF-8 chars in RTF;
139 if (bWriteAsUnicode)
140 {
141 (void)sBuf.convertToString(&sConverted,
142 eDestEnc, OUSTRING_TO_OSTRING_CVTFLAGS);
143 }
144 const sal_Int32 nLen = sConverted.getLength();
145
146 if (bWriteAsUnicode && pUCMode)
147 {
148 // then write as unicode - character
149 if (*pUCMode != nLen)
150 {
151 // #i47831# add an additional whitespace, so that
152 // "document whitespaces" are not ignored.;
153 rStream.WriteOString( "\\uc" )
154 .WriteNumberAsString( nLen ).WriteOString( " " );
155 *pUCMode = nLen;
156 }
157 rStream.WriteOString( "\\u" )
159 }
160
161 for (sal_Int32 nI = 0; nI < nLen; ++nI)
162 {
163 rStream.WriteOString( "\\'" );
164 Out_Hex(rStream, sConverted[nI], 2);
165 }
166 }
167 break;
168 }
169 break;
170 }
171
172 if (pStr)
173 rStream.WriteOString( pStr ).WriteChar( ' ' );
174
175 return rStream;
176}
177
178}
179
180SvStream& RTFOutFuncs::Out_String( SvStream& rStream, std::u16string_view rStr,
181 rtl_TextEncoding eDestEnc)
182{
183 int nUCMode = 1;
184 for (size_t n = 0; n < rStr.size(); ++n)
185 Out_Char(rStream, rStr[n], &nUCMode, eDestEnc);
186 if (nUCMode != 1)
187 rStream.WriteOString( "\\uc1" ).WriteOString( " " ); // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
188 return rStream;
189}
190
191/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvStream & WriteNumberAsString(N n)
SvStream & WriteOString(std::string_view rStr)
SvStream & WriteChar(char nChar)
#define DBG_ASSERT(sCon, aError)
sal_Int64 n
SVT_DLLPUBLIC SvStream & Out_String(SvStream &, std::u16string_view, rtl_TextEncoding eDestEnc=RTL_TEXTENCODING_MS_1252)
Definition: rtfout.cxx:180
#define OOO_STRING_SVTOOLS_RTF_RQUOTE
Definition: rtfkeywd.hxx:319
#define OOO_STRING_SVTOOLS_RTF_EMDASH
Definition: rtfkeywd.hxx:142
#define OOO_STRING_SVTOOLS_RTF_LDBLQUOTE
Definition: rtfkeywd.hxx:209
#define OOO_STRING_SVTOOLS_RTF_LINE
Definition: rtfkeywd.hxx:213
#define OOO_STRING_SVTOOLS_RTF_LQUOTE
Definition: rtfkeywd.hxx:223
#define OOO_STRING_SVTOOLS_RTF_ENDASH
Definition: rtfkeywd.hxx:143
#define OOO_STRING_SVTOOLS_RTF_RDBLQUOTE
Definition: rtfkeywd.hxx:309
#define OOO_STRING_SVTOOLS_RTF_BULLET
Definition: rtfkeywd.hxx:74
#define OOO_STRING_SVTOOLS_RTF_TAB
Definition: rtfkeywd.hxx:345
sal_uIntPtr sal_uLong
unsigned char sal_uInt8
sal_uInt16 sal_Unicode