Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svtools / source / svrtf / rtfout.cxx
blob843256dc2d476da6b461ede7d83dc9eba0522173
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
27 namespace {
29 SvStream& Out_Hex( SvStream& rStream, sal_uLong nHex, sal_uInt8 nLen )
31 char aNToABuf[] = "0000000000000000";
33 DBG_ASSERT( nLen < sizeof(aNToABuf), "too many places" );
34 if( nLen >= sizeof(aNToABuf) )
35 nLen = (sizeof(aNToABuf)-1);
37 // set pointer to end of buffer
38 char* pStr = aNToABuf + (sizeof(aNToABuf)-1);
39 for( sal_uInt8 n = 0; n < nLen; ++n )
41 *(--pStr) = static_cast<char>(nHex & 0xf ) + 48;
42 if( *pStr > '9' )
43 *pStr += 39;
44 nHex >>= 4;
46 return rStream.WriteOString( pStr );
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.
60 SvStream& Out_Char(SvStream& rStream, sal_Unicode c,
61 int *pUCMode, rtl_TextEncoding eDestEnc)
63 const char* pStr = nullptr;
64 switch (c)
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':
81 pStr = OOO_STRING_SVTOOLS_RTF_LINE;
82 break;
83 case '\t':
84 pStr = OOO_STRING_SVTOOLS_RTF_TAB;
85 break;
86 default:
87 switch(c)
89 case 149:
90 pStr = OOO_STRING_SVTOOLS_RTF_BULLET;
91 break;
92 case 150:
93 pStr = OOO_STRING_SVTOOLS_RTF_ENDASH;
94 break;
95 case 151:
96 pStr = OOO_STRING_SVTOOLS_RTF_EMDASH;
97 break;
98 case 145:
99 pStr = OOO_STRING_SVTOOLS_RTF_LQUOTE;
100 break;
101 case 146:
102 pStr = OOO_STRING_SVTOOLS_RTF_RQUOTE;
103 break;
104 case 147:
105 pStr = OOO_STRING_SVTOOLS_RTF_LDBLQUOTE;
106 break;
107 case 148:
108 pStr = OOO_STRING_SVTOOLS_RTF_RDBLQUOTE;
109 break;
112 if (pStr)
113 break;
115 switch (c)
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
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)
141 (void)sBuf.convertToString(&sConverted,
142 eDestEnc, OUSTRING_TO_OSTRING_CVTFLAGS);
144 const sal_Int32 nLen = sConverted.getLength();
146 if (bWriteAsUnicode && pUCMode)
148 // then write as unicode - character
149 if (*pUCMode != nLen)
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;
157 rStream.WriteOString( "\\u" )
158 .WriteNumberAsString(c);
161 for (sal_Int32 nI = 0; nI < nLen; ++nI)
163 rStream.WriteOString( "\\'" );
164 Out_Hex(rStream, sConverted[nI], 2);
167 break;
169 break;
172 if (pStr)
173 rStream.WriteOString( pStr ).WriteChar( ' ' );
175 return rStream;
180 SvStream& RTFOutFuncs::Out_String( SvStream& rStream, std::u16string_view rStr,
181 rtl_TextEncoding eDestEnc)
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;
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */