fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / filter / ftools / ftools.cxx
blob342715e93d01032541d447d11489cd4d5a313bef
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 "ftools.hxx"
21 #include <osl/diagnose.h>
22 #include <osl/thread.h>
23 #include <rtl/strbuf.hxx>
24 #include <tools/color.hxx>
25 #include <unotools/charclass.hxx>
26 #include <svl/itempool.hxx>
27 #include <svl/itemset.hxx>
28 #include <svl/poolitem.hxx>
29 #include <sot/storage.hxx>
31 #include <math.h>
32 #include "global.hxx"
33 #include "document.hxx"
34 #include "stlpool.hxx"
35 #include "stlsheet.hxx"
36 #include "compiler.hxx"
38 #include <config_orcus.h>
40 #if ENABLE_ORCUS
41 #include "orcusfiltersimpl.hxx"
42 #endif
44 #include <stdio.h>
46 // ScFilterTools::ReadLongDouble()
48 double ScfTools::ReadLongDouble( SvStream& rStrm )
50 #ifdef __SIMPLE_FUNC // for <=VC 1.5
52 long double fRet;
53 rStrm.Read( &fRet, 10 );
54 return static_cast< double >( fRet );
56 #undef __SIMPLE_FUNC
58 #else // detailed for all others
62 " M a p p i n g - G u i d e " 10-Byte Intel
64 77777777 77666666 66665555 55555544 44444444 33333333 33222222 22221111 11111100 00000000 x10
65 98765432 10987654 32109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210 Bit-# total
66 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0 Byte-#
67 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 Bit-# in Byte
68 SEEEEEEE EEEEEEEE IMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM Group
69 01111110 00000000 06665555 55555544 44444444 33333333 33222222 22221111 11111100 00000000 x10
70 14321098 76543210 02109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210 Bit in Group
73 long double lfDouble = 0.0;
74 long double lfFakt = 256.0;
75 sal_uInt8 pDouble10[ 10 ];
77 rStrm.Read( pDouble10, 10 ); // Intel-10 in pDouble10
79 lfDouble = static_cast< long double >( pDouble10[ 7 ] ); // Byte 7
80 lfDouble *= lfFakt;
81 lfDouble += static_cast< long double >( pDouble10[ 6 ] ); // Byte 6
82 lfDouble *= lfFakt;
83 lfDouble += static_cast< long double >( pDouble10[ 5 ] ); // Byte 5
84 lfDouble *= lfFakt;
85 lfDouble += static_cast< long double >( pDouble10[ 4 ] ); // Byte 4
86 lfDouble *= lfFakt;
87 lfDouble += static_cast< long double >( pDouble10[ 3 ] ); // Byte 3
88 lfDouble *= lfFakt;
89 lfDouble += static_cast< long double >( pDouble10[ 2 ] ); // Byte 2
90 lfDouble *= lfFakt;
91 lfDouble += static_cast< long double >( pDouble10[ 1 ] ); // Byte 1
92 lfDouble *= lfFakt;
93 lfDouble += static_cast< long double >( pDouble10[ 0 ] ); // Byte 0
95 // For value 0.0 all bits are zero; pow(2.0,-16446) does not work with CSet compilers
96 if( lfDouble != 0.0 )
98 // exponent
99 sal_Int32 nExp;
100 nExp = pDouble10[ 9 ] & 0x7F;
101 nExp <<= 8;
102 nExp += pDouble10[ 8 ];
103 nExp -= 16446;
105 lfDouble *= pow( 2.0, static_cast< double >( nExp ) );
108 // sign
109 if( pDouble10[ 9 ] & 0x80 )
110 lfDouble *= static_cast< long double >( -1.0 );
112 return static_cast< double >( lfDouble );
114 #endif
116 // *** common methods *** -----------------------------------------------------
118 rtl_TextEncoding ScfTools::GetSystemTextEncoding()
120 return osl_getThreadTextEncoding();
123 OUString ScfTools::GetHexStr( sal_uInt16 nValue )
125 const sal_Char pHex[] = "0123456789ABCDEF";
126 OUString aStr;
128 aStr += OUString( pHex[ nValue >> 12 ] );
129 aStr += OUString( pHex[ (nValue >> 8) & 0x000F ] );
130 aStr += OUString( pHex[ (nValue >> 4) & 0x000F ] );
131 aStr += OUString( pHex[ nValue & 0x000F ] );
132 return aStr;
135 sal_uInt8 ScfTools::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans )
137 sal_Int32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
138 return static_cast< sal_uInt8 >( nTemp );
141 Color ScfTools::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
143 return Color(
144 GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
145 GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
146 GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ) );
149 // *** conversion of names *** ------------------------------------------------
151 /* XXX As in sc/source/core/tool/rangenam.cxx ScRangeData::IsValidName() */
153 OUString ScfTools::ConvertToScDefinedName(const OUString& rName )
155 //fdo#37872: we don't allow points in range names any more
156 OUString sName = rName.replace(static_cast<sal_Unicode>('.'),
157 static_cast<sal_Unicode>('_'));
158 sal_Int32 nLen = sName.getLength();
159 if( nLen && !ScCompiler::IsCharFlagAllConventions( sName, 0, SC_COMPILER_C_CHAR_NAME ) )
160 sName = sName.replaceAt( 0, 1, "_" );
161 for( sal_Int32 nPos = 1; nPos < nLen; ++nPos )
162 if( !ScCompiler::IsCharFlagAllConventions( sName, nPos, SC_COMPILER_C_NAME ) )
163 sName = sName.replaceAt( nPos, 1, "_" );
164 return sName;
167 // *** streams and storages *** -----------------------------------------------
169 tools::SvRef<SotStorage> ScfTools::OpenStorageRead( tools::SvRef<SotStorage> xStrg, const OUString& rStrgName )
171 tools::SvRef<SotStorage> xSubStrg;
172 if( xStrg.Is() && xStrg->IsContained( rStrgName ) )
173 xSubStrg = xStrg->OpenSotStorage( rStrgName, STREAM_STD_READ );
174 return xSubStrg;
177 tools::SvRef<SotStorage> ScfTools::OpenStorageWrite( tools::SvRef<SotStorage> xStrg, const OUString& rStrgName )
179 tools::SvRef<SotStorage> xSubStrg;
180 if( xStrg.Is() )
181 xSubStrg = xStrg->OpenSotStorage( rStrgName, STREAM_STD_WRITE );
182 return xSubStrg;
185 tools::SvRef<SotStorageStream> ScfTools::OpenStorageStreamRead( tools::SvRef<SotStorage> xStrg, const OUString& rStrmName )
187 tools::SvRef<SotStorageStream> xStrm;
188 if( xStrg.Is() && xStrg->IsContained( rStrmName ) && xStrg->IsStream( rStrmName ) )
189 xStrm = xStrg->OpenSotStream( rStrmName, STREAM_STD_READ );
190 return xStrm;
193 tools::SvRef<SotStorageStream> ScfTools::OpenStorageStreamWrite( tools::SvRef<SotStorage> xStrg, const OUString& rStrmName )
195 OSL_ENSURE( !xStrg || !xStrg->IsContained( rStrmName ), "ScfTools::OpenStorageStreamWrite - stream exists already" );
196 tools::SvRef<SotStorageStream> xStrm;
197 if( xStrg.Is() )
198 xStrm = xStrg->OpenSotStream( rStrmName, STREAM_STD_WRITE | StreamMode::TRUNC );
199 return xStrm;
202 // *** item handling *** ------------------------------------------------------
204 bool ScfTools::CheckItem( const SfxItemSet& rItemSet, sal_uInt16 nWhichId, bool bDeep )
206 return rItemSet.GetItemState( nWhichId, bDeep ) == SfxItemState::SET;
209 bool ScfTools::CheckItems( const SfxItemSet& rItemSet, const sal_uInt16* pnWhichIds, bool bDeep )
211 OSL_ENSURE( pnWhichIds, "ScfTools::CheckItems - no which id list" );
212 for( const sal_uInt16* pnWhichId = pnWhichIds; *pnWhichId != 0; ++pnWhichId )
213 if( CheckItem( rItemSet, *pnWhichId, bDeep ) )
214 return true;
215 return false;
218 void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, sal_uInt16 nWhichId, bool bSkipPoolDef )
220 if( !bSkipPoolDef || (rItem != rItemSet.GetPool()->GetDefaultItem( nWhichId )) )
221 rItemSet.Put( rItem, nWhichId );
224 void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, bool bSkipPoolDef )
226 PutItem( rItemSet, rItem, rItem.Which(), bSkipPoolDef );
229 // *** style sheet handling *** -----------------------------------------------
231 namespace {
233 ScStyleSheet& lclMakeStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, SfxStyleFamily eFamily, bool bForceName )
235 // find an unused name
236 OUString aNewName( rStyleName );
237 sal_Int32 nIndex = 0;
238 SfxStyleSheetBase* pOldStyleSheet = 0;
239 while( SfxStyleSheetBase* pStyleSheet = rPool.Find( aNewName, eFamily ) )
241 if( !pOldStyleSheet )
242 pOldStyleSheet = pStyleSheet;
243 aNewName = rStyleName + " " + OUString::number( ++nIndex );
246 // rename existing style
247 if( pOldStyleSheet && bForceName )
249 pOldStyleSheet->SetName( aNewName );
250 aNewName = rStyleName;
253 // create new style sheet
254 return static_cast< ScStyleSheet& >( rPool.Make( aNewName, eFamily, SFXSTYLEBIT_USERDEF ) );
257 } // namespace
259 ScStyleSheet& ScfTools::MakeCellStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, bool bForceName )
261 return lclMakeStyleSheet( rPool, rStyleName, SFX_STYLE_FAMILY_PARA, bForceName );
264 ScStyleSheet& ScfTools::MakePageStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, bool bForceName )
266 return lclMakeStyleSheet( rPool, rStyleName, SFX_STYLE_FAMILY_PAGE, bForceName );
269 // *** byte string import operations *** --------------------------------------
271 OString ScfTools::read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm, sal_Int32& rnBytesLeft)
273 OString aRet(::read_zeroTerminated_uInt8s_ToOString(rStrm));
274 rnBytesLeft -= aRet.getLength(); //we read this number of bytes anyway
275 if (rStrm.good()) //if the stream is happy we read the null terminator as well
276 --rnBytesLeft;
277 return aRet;
280 void ScfTools::AppendCString( SvStream& rStrm, OUString& rString, rtl_TextEncoding eTextEnc )
282 rString += ::read_zeroTerminated_uInt8s_ToOUString(rStrm, eTextEnc);
285 // *** HTML table names <-> named range names *** -----------------------------
287 const OUString& ScfTools::GetHTMLDocName()
289 static const OUString saHTMLDoc( "HTML_all" );
290 return saHTMLDoc;
293 const OUString& ScfTools::GetHTMLTablesName()
295 static const OUString saHTMLTables( "HTML_tables" );
296 return saHTMLTables;
299 const OUString& ScfTools::GetHTMLIndexPrefix()
301 static const OUString saHTMLIndexPrefix( "HTML_" );
302 return saHTMLIndexPrefix;
306 const OUString& ScfTools::GetHTMLNamePrefix()
308 static const OUString saHTMLNamePrefix( "HTML__" );
309 return saHTMLNamePrefix;
312 OUString ScfTools::GetNameFromHTMLIndex( sal_uInt32 nIndex )
314 OUString aName = GetHTMLIndexPrefix() +
315 OUString::number( static_cast< sal_Int32 >( nIndex ) );
316 return aName;
319 OUString ScfTools::GetNameFromHTMLName( const OUString& rTabName )
321 OUString aName( GetHTMLNamePrefix() );
322 aName += rTabName;
323 return aName;
326 bool ScfTools::IsHTMLDocName( const OUString& rSource )
328 return rSource.equalsIgnoreAsciiCase( GetHTMLDocName() );
331 bool ScfTools::IsHTMLTablesName( const OUString& rSource )
333 return rSource.equalsIgnoreAsciiCase( GetHTMLTablesName() );
336 bool ScfTools::GetHTMLNameFromName( const OUString& rSource, OUString& rName )
338 rName.clear();
339 if( rSource.startsWithIgnoreAsciiCase( GetHTMLNamePrefix() ) )
341 rName = rSource.copy( GetHTMLNamePrefix().getLength() );
342 ScGlobal::AddQuotes( rName, '"', false );
344 else if( rSource.startsWithIgnoreAsciiCase( GetHTMLIndexPrefix() ) )
346 OUString aIndex( rSource.copy( GetHTMLIndexPrefix().getLength() ) );
347 if( CharClass::isAsciiNumeric( aIndex ) && (aIndex.toInt32() > 0) )
348 rName = aIndex;
350 return !rName.isEmpty();
353 ScFormatFilterPluginImpl::ScFormatFilterPluginImpl() {}
354 ScFormatFilterPluginImpl::~ScFormatFilterPluginImpl() {}
356 ScOrcusFilters* ScFormatFilterPluginImpl::GetOrcusFilters()
358 #if ENABLE_ORCUS
359 static ScOrcusFiltersImpl aImpl;
360 return &aImpl;
361 #else
362 return NULL;
363 #endif
366 ScFormatFilterPlugin * SAL_CALL ScFilterCreate()
368 return new ScFormatFilterPluginImpl();
371 // implementation class inside the filters
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */