xref: /aoo41x/main/svtools/source/svrtf/rtfout.cxx (revision 5900e8ec)
1*5900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5900e8ecSAndrew Rist  * distributed with this work for additional information
6*5900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
9*5900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5900e8ecSAndrew Rist  *
11*5900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5900e8ecSAndrew Rist  *
13*5900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5900e8ecSAndrew Rist  * software distributed under the License is distributed on an
15*5900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
17*5900e8ecSAndrew Rist  * specific language governing permissions and limitations
18*5900e8ecSAndrew Rist  * under the License.
19*5900e8ecSAndrew Rist  *
20*5900e8ecSAndrew Rist  *************************************************************/
21*5900e8ecSAndrew Rist 
22*5900e8ecSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir #include <tools/debug.hxx>
27cdf0e10cSrcweir #include <tools/stream.hxx>
28cdf0e10cSrcweir #include <tools/string.hxx>
29cdf0e10cSrcweir #include <rtl/string.hxx>
30cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
31cdf0e10cSrcweir #include <svtools/rtfkeywd.hxx>
32cdf0e10cSrcweir #include <svtools/rtfout.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace rtl;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #if defined(UNX)
37cdf0e10cSrcweir const sal_Char RTFOutFuncs::sNewLine = '\012';
38cdf0e10cSrcweir #else
39cdf0e10cSrcweir const sal_Char __FAR_DATA RTFOutFuncs::sNewLine[] = "\015\012";
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 
Out_Char(SvStream & rStream,sal_Unicode c,int * pUCMode,rtl_TextEncoding eDestEnc,sal_Bool bWriteHelpFile)43cdf0e10cSrcweir SvStream& RTFOutFuncs::Out_Char(SvStream& rStream, sal_Unicode c,
44cdf0e10cSrcweir 	int *pUCMode, rtl_TextEncoding eDestEnc, sal_Bool bWriteHelpFile)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 	const sal_Char* pStr = 0;
47cdf0e10cSrcweir 	switch (c)
48cdf0e10cSrcweir 	{
49cdf0e10cSrcweir 	case 0x1:
50cdf0e10cSrcweir 	case 0x2:
51cdf0e10cSrcweir 		// this are control character of our textattributes and will never be
52cdf0e10cSrcweir 		// written
53cdf0e10cSrcweir 		break;
54cdf0e10cSrcweir 	case 0xA0:
55cdf0e10cSrcweir 		rStream << "\\~";
56cdf0e10cSrcweir 		break;
57cdf0e10cSrcweir 	case 0xAD:
58cdf0e10cSrcweir 		rStream << "\\-";
59cdf0e10cSrcweir 		break;
60cdf0e10cSrcweir 	case 0x2011:
61cdf0e10cSrcweir 		rStream << "\\_";
62cdf0e10cSrcweir 		break;
63cdf0e10cSrcweir 	case '\n':
64cdf0e10cSrcweir 		pStr = OOO_STRING_SVTOOLS_RTF_LINE;
65cdf0e10cSrcweir 		break;
66cdf0e10cSrcweir 	case '\t':
67cdf0e10cSrcweir 		pStr = OOO_STRING_SVTOOLS_RTF_TAB;
68cdf0e10cSrcweir 		break;
69cdf0e10cSrcweir 	default:
70cdf0e10cSrcweir 		if(!bWriteHelpFile)
71cdf0e10cSrcweir         {
72cdf0e10cSrcweir 			switch(c)
73cdf0e10cSrcweir 			{
74cdf0e10cSrcweir 				case 149:
75cdf0e10cSrcweir                     pStr = OOO_STRING_SVTOOLS_RTF_BULLET;
76cdf0e10cSrcweir                     break;
77cdf0e10cSrcweir 				case 150:
78cdf0e10cSrcweir                     pStr = OOO_STRING_SVTOOLS_RTF_ENDASH;
79cdf0e10cSrcweir                     break;
80cdf0e10cSrcweir 				case 151:
81cdf0e10cSrcweir                     pStr = OOO_STRING_SVTOOLS_RTF_EMDASH;
82cdf0e10cSrcweir                     break;
83cdf0e10cSrcweir 				case 145:
84cdf0e10cSrcweir                     pStr = OOO_STRING_SVTOOLS_RTF_LQUOTE;
85cdf0e10cSrcweir                     break;
86cdf0e10cSrcweir 				case 146:
87cdf0e10cSrcweir                     pStr = OOO_STRING_SVTOOLS_RTF_RQUOTE;
88cdf0e10cSrcweir                     break;
89cdf0e10cSrcweir 				case 147:
90cdf0e10cSrcweir                     pStr = OOO_STRING_SVTOOLS_RTF_LDBLQUOTE;
91cdf0e10cSrcweir                     break;
92cdf0e10cSrcweir 				case 148:
93cdf0e10cSrcweir                     pStr = OOO_STRING_SVTOOLS_RTF_RDBLQUOTE;
94cdf0e10cSrcweir                     break;
95cdf0e10cSrcweir 			}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             if (pStr)
98cdf0e10cSrcweir                 break;
99cdf0e10cSrcweir         }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 		switch (c)
102cdf0e10cSrcweir 		{
103cdf0e10cSrcweir 			case '\\':
104cdf0e10cSrcweir 			case '}':
105cdf0e10cSrcweir 			case '{':
106cdf0e10cSrcweir                 rStream << '\\' << (sal_Char)c;
107cdf0e10cSrcweir                 break;
108cdf0e10cSrcweir 			default:
109cdf0e10cSrcweir 				if (c >= ' ' && c <= '~')
110cdf0e10cSrcweir 					rStream << (sal_Char)c;
111cdf0e10cSrcweir                 else
112cdf0e10cSrcweir                 {
113cdf0e10cSrcweir                     //If we can't convert to the dest encoding, or if
114cdf0e10cSrcweir                     //its an uncommon multibyte sequence which most
115cdf0e10cSrcweir                     //readers won't be able to handle correctly, then
116cdf0e10cSrcweir                     //If we can't convert to the dest encoding, then
117cdf0e10cSrcweir                     //export as unicode
118cdf0e10cSrcweir                     OUString sBuf(&c, 1);
119cdf0e10cSrcweir                     OString sConverted;
120cdf0e10cSrcweir                     sal_uInt32 nFlags =
121cdf0e10cSrcweir                         RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
122cdf0e10cSrcweir                         RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR;
123cdf0e10cSrcweir                     bool bWriteAsUnicode = !(sBuf.convertToString(&sConverted,
124cdf0e10cSrcweir 	                                     eDestEnc, nFlags))
125cdf0e10cSrcweir                                             || (RTL_TEXTENCODING_UTF8==eDestEnc); // #i43933# do not export UTF-8 chars in RTF;
126cdf0e10cSrcweir                     if (bWriteAsUnicode)
127cdf0e10cSrcweir                     {
128cdf0e10cSrcweir                         sBuf.convertToString(&sConverted,
129cdf0e10cSrcweir                             eDestEnc, OUSTRING_TO_OSTRING_CVTFLAGS);
130cdf0e10cSrcweir                     }
131cdf0e10cSrcweir                     const sal_Int32 nLen = sConverted.getLength();
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 			        if (bWriteAsUnicode && pUCMode)
134cdf0e10cSrcweir 			        {
135cdf0e10cSrcweir 				        // then write as unicode - character
136cdf0e10cSrcweir 						if (*pUCMode != nLen)
137cdf0e10cSrcweir 						{
138cdf0e10cSrcweir 						  rStream << "\\uc" << ByteString::CreateFromInt32(nLen).GetBuffer() << " "; // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
139cdf0e10cSrcweir 							*pUCMode = nLen;
140cdf0e10cSrcweir 						}
141cdf0e10cSrcweir 					    ByteString sNo(ByteString::CreateFromInt32(c));
142cdf0e10cSrcweir 			 		    rStream << "\\u" << sNo.GetBuffer();
143cdf0e10cSrcweir                     }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir                     for (sal_Int32 nI = 0; nI < nLen; ++nI)
146cdf0e10cSrcweir                     {
147cdf0e10cSrcweir                         rStream << "\\'";
148cdf0e10cSrcweir                         Out_Hex(rStream, sConverted.getStr()[nI], 2);
149cdf0e10cSrcweir                     }
150cdf0e10cSrcweir                 }
151cdf0e10cSrcweir 				break;
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir 		break;
154cdf0e10cSrcweir 	}
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	if (pStr)
157cdf0e10cSrcweir 		rStream << pStr << ' ';
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	return rStream;
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
Out_String(SvStream & rStream,const String & rStr,rtl_TextEncoding eDestEnc,sal_Bool bWriteHelpFile)162cdf0e10cSrcweir SvStream& RTFOutFuncs::Out_String( SvStream& rStream, const String& rStr,
163cdf0e10cSrcweir     rtl_TextEncoding eDestEnc, sal_Bool bWriteHelpFile)
164cdf0e10cSrcweir {
165cdf0e10cSrcweir 	int nUCMode = 1;
166cdf0e10cSrcweir 	for (xub_StrLen n = 0; n < rStr.Len(); ++n)
167cdf0e10cSrcweir 		Out_Char(rStream, rStr.GetChar(n), &nUCMode, eDestEnc, bWriteHelpFile);
168cdf0e10cSrcweir 	if (nUCMode != 1)
169cdf0e10cSrcweir 	  rStream << "\\uc1"<< " "; // #i47831# add an additional whitespace, so that "document whitespaces" are not ignored.;
170cdf0e10cSrcweir 	return rStream;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
Out_Fontname(SvStream & rStream,const String & rStr,rtl_TextEncoding eDestEnc,sal_Bool bWriteHelpFile)173cdf0e10cSrcweir SvStream& RTFOutFuncs::Out_Fontname(SvStream& rStream, const String& rStr,
174cdf0e10cSrcweir     rtl_TextEncoding eDestEnc, sal_Bool bWriteHelpFile)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     //Fontnames in word have a quirk in that \uc and usage of ansi replacement
177cdf0e10cSrcweir     //chars after a \u don't work and in wordpad \u doesn't work, so we are
178cdf0e10cSrcweir     //left with forcing ansi characters only for fontnames
179cdf0e10cSrcweir     for (xub_StrLen n = 0; n < rStr.Len(); ++n)
180cdf0e10cSrcweir         Out_Char(rStream, rStr.GetChar(n), 0, eDestEnc, bWriteHelpFile);
181cdf0e10cSrcweir     return rStream;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
Out_Hex(SvStream & rStream,sal_uLong nHex,sal_uInt8 nLen)184cdf0e10cSrcweir SvStream& RTFOutFuncs::Out_Hex( SvStream& rStream, sal_uLong nHex, sal_uInt8 nLen )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir 	sal_Char aNToABuf[] = "0000000000000000";
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	DBG_ASSERT( nLen < sizeof(aNToABuf), "zu viele Stellen" );
189cdf0e10cSrcweir 	if( nLen >= sizeof(aNToABuf) )
190cdf0e10cSrcweir 		nLen = (sizeof(aNToABuf)-1);
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 	// Pointer an das Bufferende setzen
193cdf0e10cSrcweir 	sal_Char* pStr = aNToABuf + (sizeof(aNToABuf)-1);
194cdf0e10cSrcweir 	for( sal_uInt8 n = 0; n < nLen; ++n )
195cdf0e10cSrcweir 	{
196cdf0e10cSrcweir 		*(--pStr) = (sal_Char)(nHex & 0xf ) + 48;
197cdf0e10cSrcweir 		if( *pStr > '9' )
198cdf0e10cSrcweir 			*pStr += 39;
199cdf0e10cSrcweir 		nHex >>= 4;
200cdf0e10cSrcweir 	}
201cdf0e10cSrcweir 	return rStream << pStr;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 
206