1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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;
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;
68 // this are control character of our textattributes and will never be
72 rStream
.WriteOString( "\\~" );
75 rStream
.WriteOString( "\\-" );
78 rStream
.WriteOString( "\\_" );
81 pStr
= OOO_STRING_SVTOOLS_RTF_LINE
;
84 pStr
= OOO_STRING_SVTOOLS_RTF_TAB
;
90 pStr
= OOO_STRING_SVTOOLS_RTF_BULLET
;
93 pStr
= OOO_STRING_SVTOOLS_RTF_ENDASH
;
96 pStr
= OOO_STRING_SVTOOLS_RTF_EMDASH
;
99 pStr
= OOO_STRING_SVTOOLS_RTF_LQUOTE
;
102 pStr
= OOO_STRING_SVTOOLS_RTF_RQUOTE
;
105 pStr
= OOO_STRING_SVTOOLS_RTF_LDBLQUOTE
;
108 pStr
= OOO_STRING_SVTOOLS_RTF_RDBLQUOTE
;
120 rStream
.WriteChar( '\\' ).WriteChar( char(c
) );
123 if (c
>= ' ' && c
<= '~')
124 rStream
.WriteChar( char(c
) );
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
131 OUString
sBuf(&c
, 1);
133 sal_uInt32
const nFlags
=
134 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
|
135 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
;
136 bool bWriteAsUnicode
= !(sBuf
.convertToString(&sConverted
,
138 || (RTL_TEXTENCODING_UTF8
==eDestEnc
); // #i43933# do not export UTF-8 chars in RTF;
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( " " );
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);
173 rStream
.WriteOString( pStr
).WriteChar( ' ' );
180 SvStream
& RTFOutFuncs::Out_String( SvStream
& rStream
, std::u16string_view rStr
,
181 rtl_TextEncoding eDestEnc
)
184 for (size_t n
= 0; n
< rStr
.size(); ++n
)
185 Out_Char(rStream
, rStr
[n
], &nUCMode
, eDestEnc
);
187 rStream
.WriteOString( "\\uc1" ).WriteOString( " " ); // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */