fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / filter / excel / xestring.cxx
blob0e1a1279752be881f53a6f760c38e17370a45795
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 <algorithm>
21 #include <stdio.h>
23 #include <osl/diagnose.h>
24 #include "xlstyle.hxx"
25 #include "xestyle.hxx"
26 #include "xestream.hxx"
27 #include "xestring.hxx"
29 using namespace ::oox;
31 namespace {
33 // compare vectors
35 /** Compares two vectors.
36 @return A negative value, if rLeft<rRight; or a positive value, if rLeft>rRight;
37 or 0, if rLeft==rRight. */
38 template< typename Type >
39 int lclCompareVectors( const ::std::vector< Type >& rLeft, const ::std::vector< Type >& rRight )
41 int nResult = 0;
43 // 1st: compare all elements of the vectors
44 typedef typename ::std::vector< Type >::const_iterator CIT;
45 CIT aEndL = rLeft.end(), aEndR = rRight.end();
46 for( CIT aItL = rLeft.begin(), aItR = rRight.begin(); !nResult && (aItL != aEndL) && (aItR != aEndR); ++aItL, ++aItR )
47 nResult = static_cast< int >( *aItL ) - static_cast< int >( *aItR );
49 // 2nd: no differences found so far -> compare the vector sizes. Shorter vector is less
50 if( !nResult )
51 nResult = static_cast< int >( rLeft.size() ) - static_cast< int >( rRight.size() );
53 return nResult;
56 // hashing helpers
58 /** Base class for value hashers.
59 @descr These function objects are used to hash any value to a sal_uInt32 value. */
60 template< typename Type >
61 struct XclHasher : public ::std::unary_function< Type, sal_uInt32 > {};
63 template< typename Type >
64 struct XclDirectHasher : public XclHasher< Type >
66 inline sal_uInt32 operator()( Type nVal ) const { return nVal; }
69 struct XclFormatRunHasher : public XclHasher< const XclFormatRun& >
71 inline sal_uInt32 operator()( const XclFormatRun& rRun ) const
72 { return (rRun.mnChar << 8) ^ rRun.mnFontIdx; }
75 /** Calculates a hash value from a vector.
76 @descr Uses the passed hasher function object to calculate hash values from
77 all vector elements. */
78 template< typename Type, typename ValueHasher >
79 sal_uInt16 lclHashVector( const ::std::vector< Type >& rVec, const ValueHasher& rHasher )
81 sal_uInt32 nHash = rVec.size();
82 typedef typename ::std::vector< Type >::const_iterator CIT;
83 for( CIT aIt = rVec.begin(), aEnd = rVec.end(); aIt != aEnd; ++aIt )
84 (nHash *= 31) += rHasher( *aIt );
85 return static_cast< sal_uInt16 >( nHash ^ (nHash >> 16) );
88 /** Calculates a hash value from a vector. Uses XclDirectHasher to hash the vector elements. */
89 template< typename Type >
90 inline sal_uInt16 lclHashVector( const ::std::vector< Type >& rVec )
92 return lclHashVector( rVec, XclDirectHasher< Type >() );
95 } // namespace
97 // constructors ---------------------------------------------------------------
99 XclExpString::XclExpString( XclStrFlags nFlags, sal_uInt16 nMaxLen )
101 Init( 0, nFlags, nMaxLen, true );
104 XclExpString::XclExpString( const OUString& rString, XclStrFlags nFlags, sal_uInt16 nMaxLen )
106 Assign( rString, nFlags, nMaxLen );
109 // assign ---------------------------------------------------------------------
111 void XclExpString::Assign( const OUString& rString, XclStrFlags nFlags, sal_uInt16 nMaxLen )
113 Build( rString.getStr(), rString.getLength(), nFlags, nMaxLen );
116 void XclExpString::Assign( sal_Unicode cChar, XclStrFlags nFlags, sal_uInt16 nMaxLen )
118 Build( &cChar, 1, nFlags, nMaxLen );
121 void XclExpString::AssignByte(
122 const OUString& rString, rtl_TextEncoding eTextEnc, XclStrFlags nFlags, sal_uInt16 nMaxLen )
124 // length may differ from length of rString
125 OString aByteStr(OUStringToOString(rString, eTextEnc));
126 Build(aByteStr.getStr(), aByteStr.getLength(), nFlags, nMaxLen);
129 // append ---------------------------------------------------------------------
131 void XclExpString::Append( const OUString& rString )
133 BuildAppend( rString.getStr(), rString.getLength() );
136 void XclExpString::AppendByte( const OUString& rString, rtl_TextEncoding eTextEnc )
138 if (!rString.isEmpty())
140 // length may differ from length of rString
141 OString aByteStr(OUStringToOString(rString, eTextEnc));
142 BuildAppend(aByteStr.getStr(), aByteStr.getLength());
146 void XclExpString::AppendByte( sal_Unicode cChar, rtl_TextEncoding eTextEnc )
148 if( !cChar )
150 sal_Char cByteChar = 0;
151 BuildAppend( &cByteChar, 1 );
153 else
155 OString aByteStr( &cChar, 1, eTextEnc ); // length may be >1
156 BuildAppend( aByteStr.getStr(), aByteStr.getLength() );
160 // formatting runs ------------------------------------------------------------
162 void XclExpString::AppendFormat( sal_uInt16 nChar, sal_uInt16 nFontIdx, bool bDropDuplicate )
164 OSL_ENSURE( maFormats.empty() || (maFormats.back().mnChar < nChar), "XclExpString::AppendFormat - invalid char index" );
165 size_t nMaxSize = static_cast< size_t >( mbIsBiff8 ? EXC_STR_MAXLEN : EXC_STR_MAXLEN_8BIT );
166 if( maFormats.empty() || ((maFormats.size() < nMaxSize) && (!bDropDuplicate || (maFormats.back().mnFontIdx != nFontIdx))) )
167 maFormats.push_back( XclFormatRun( nChar, nFontIdx ) );
170 void XclExpString::AppendTrailingFormat( sal_uInt16 nFontIdx )
172 AppendFormat( mnLen, nFontIdx, false );
175 void XclExpString::LimitFormatCount( sal_uInt16 nMaxCount )
177 if( maFormats.size() > nMaxCount )
178 maFormats.erase( maFormats.begin() + nMaxCount, maFormats.end() );
181 sal_uInt16 XclExpString::RemoveLeadingFont()
183 sal_uInt16 nFontIdx = EXC_FONT_NOTFOUND;
184 if( !maFormats.empty() && (maFormats.front().mnChar == 0) )
186 nFontIdx = maFormats.front().mnFontIdx;
187 maFormats.erase( maFormats.begin() );
189 return nFontIdx;
192 bool XclExpString::IsEqual( const XclExpString& rCmp ) const
194 return
195 (mnLen == rCmp.mnLen) &&
196 (mbIsBiff8 == rCmp.mbIsBiff8) &&
197 (mbIsUnicode == rCmp.mbIsUnicode) &&
198 (mbWrapped == rCmp.mbWrapped) &&
200 ( mbIsBiff8 && (maUniBuffer == rCmp.maUniBuffer)) ||
201 (!mbIsBiff8 && (maCharBuffer == rCmp.maCharBuffer))
202 ) &&
203 (maFormats == rCmp.maFormats);
206 bool XclExpString::IsLessThan( const XclExpString& rCmp ) const
208 int nResult = mbIsBiff8 ?
209 lclCompareVectors( maUniBuffer, rCmp.maUniBuffer ) :
210 lclCompareVectors( maCharBuffer, rCmp.maCharBuffer );
211 return (nResult != 0) ? (nResult < 0) : (maFormats < rCmp.maFormats);
214 // get data -------------------------------------------------------------------
216 sal_uInt16 XclExpString::GetFormatsCount() const
218 return static_cast< sal_uInt16 >( maFormats.size() );
221 sal_uInt8 XclExpString::GetFlagField() const
223 return (mbIsUnicode ? EXC_STRF_16BIT : 0) | (IsWriteFormats() ? EXC_STRF_RICH : 0);
226 sal_uInt16 XclExpString::GetHeaderSize() const
228 return
229 (mb8BitLen ? 1 : 2) + // length field
230 (IsWriteFlags() ? 1 : 0) + // flag field
231 (IsWriteFormats() ? 2 : 0); // richtext formattting count
234 sal_Size XclExpString::GetBufferSize() const
236 return static_cast<sal_Size>(mnLen) * (mbIsUnicode ? 2 : 1);
239 sal_Size XclExpString::GetSize() const
241 return
242 GetHeaderSize() + // header
243 GetBufferSize() + // character buffer
244 (IsWriteFormats() ? (4 * GetFormatsCount()) : 0); // richtext formattting
247 sal_uInt16 XclExpString::GetChar( sal_uInt16 nCharIdx ) const
249 OSL_ENSURE( nCharIdx < Len(), "XclExpString::GetChar - invalid character index" );
250 return static_cast< sal_uInt16 >( mbIsBiff8 ? maUniBuffer[ nCharIdx ] : maCharBuffer[ nCharIdx ] );
253 sal_uInt16 XclExpString::GetHash() const
255 return
256 (mbIsBiff8 ? lclHashVector( maUniBuffer ) : lclHashVector( maCharBuffer )) ^
257 lclHashVector( maFormats, XclFormatRunHasher() );
260 // streaming ------------------------------------------------------------------
262 void XclExpString::WriteLenField( XclExpStream& rStrm ) const
264 if( mb8BitLen )
265 rStrm << static_cast< sal_uInt8 >( mnLen );
266 else
267 rStrm << mnLen;
270 void XclExpString::WriteFlagField( XclExpStream& rStrm ) const
272 if( mbIsBiff8 )
274 PrepareWrite( rStrm, 1 );
275 rStrm << GetFlagField();
276 rStrm.SetSliceSize( 0 );
280 void XclExpString::WriteHeader( XclExpStream& rStrm ) const
282 OSL_ENSURE( !mb8BitLen || (mnLen < 256), "XclExpString::WriteHeader - string too long" );
283 PrepareWrite( rStrm, GetHeaderSize() );
284 // length
285 WriteLenField( rStrm );
286 // flag field
287 if( IsWriteFlags() )
288 rStrm << GetFlagField();
289 // format run count
290 if( IsWriteFormats() )
291 rStrm << GetFormatsCount();
292 rStrm.SetSliceSize( 0 );
295 void XclExpString::WriteBuffer( XclExpStream& rStrm ) const
297 if( mbIsBiff8 )
298 rStrm.WriteUnicodeBuffer( maUniBuffer, GetFlagField() );
299 else
300 rStrm.WriteCharBuffer( maCharBuffer );
303 void XclExpString::WriteFormats( XclExpStream& rStrm, bool bWriteSize ) const
305 if( IsRich() )
307 XclFormatRunVec::const_iterator aIt = maFormats.begin(), aEnd = maFormats.end();
308 if( mbIsBiff8 )
310 if( bWriteSize )
311 rStrm << GetFormatsCount();
312 rStrm.SetSliceSize( 4 );
313 for( ; aIt != aEnd; ++aIt )
314 rStrm << aIt->mnChar << aIt->mnFontIdx;
316 else
318 if( bWriteSize )
319 rStrm << static_cast< sal_uInt8 >( GetFormatsCount() );
320 rStrm.SetSliceSize( 2 );
321 for( ; aIt != aEnd; ++aIt )
322 rStrm << static_cast< sal_uInt8 >( aIt->mnChar ) << static_cast< sal_uInt8 >( aIt->mnFontIdx );
324 rStrm.SetSliceSize( 0 );
328 void XclExpString::Write( XclExpStream& rStrm ) const
330 if (!mbSkipHeader)
331 WriteHeader( rStrm );
332 WriteBuffer( rStrm );
333 if( IsWriteFormats() ) // only in BIFF8 included in string
334 WriteFormats( rStrm );
337 void XclExpString::WriteHeaderToMem( sal_uInt8* pnMem ) const
339 OSL_ENSURE( pnMem, "XclExpString::WriteHeaderToMem - no memory to write to" );
340 OSL_ENSURE( !mb8BitLen || (mnLen < 256), "XclExpString::WriteHeaderToMem - string too long" );
341 OSL_ENSURE( !IsWriteFormats(), "XclExpString::WriteHeaderToMem - formatted strings not supported" );
342 // length
343 if( mb8BitLen )
345 *pnMem = static_cast< sal_uInt8 >( mnLen );
346 ++pnMem;
348 else
350 ShortToSVBT16( mnLen, pnMem );
351 pnMem += 2;
353 // flag field
354 if( IsWriteFlags() )
355 *pnMem = GetFlagField();
358 void XclExpString::WriteBufferToMem( sal_uInt8* pnMem ) const
360 OSL_ENSURE( pnMem, "XclExpString::WriteBufferToMem - no memory to write to" );
361 if( !IsEmpty() )
363 if( mbIsBiff8 )
365 for( ScfUInt16Vec::const_iterator aIt = maUniBuffer.begin(), aEnd = maUniBuffer.end(); aIt != aEnd; ++aIt )
367 sal_uInt16 nChar = *aIt;
368 *pnMem = static_cast< sal_uInt8 >( nChar );
369 ++pnMem;
370 if( mbIsUnicode )
372 *pnMem = static_cast< sal_uInt8 >( nChar >> 8 );
373 ++pnMem;
377 else
378 memcpy( pnMem, &maCharBuffer[ 0 ], mnLen );
382 void XclExpString::WriteToMem( sal_uInt8* pnMem ) const
384 WriteHeaderToMem( pnMem );
385 WriteBufferToMem( pnMem + GetHeaderSize() );
388 static sal_uInt16 lcl_WriteRun( XclExpXmlStream& rStrm, const ScfUInt16Vec& rBuffer, sal_uInt16 nStart, sal_Int32 nLength, const XclExpFont* pFont )
390 if( nLength == 0 )
391 return nStart;
393 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
395 rWorksheet->startElement( XML_r, FSEND );
396 if( pFont )
398 const XclFontData& rFontData = pFont->GetFontData();
399 rWorksheet->startElement( XML_rPr, FSEND );
400 XclXmlUtils::WriteFontData( rWorksheet, rFontData, XML_rFont );
401 rWorksheet->endElement( XML_rPr );
403 rWorksheet->startElement( XML_t,
404 FSEND );
405 rWorksheet->writeEscaped( XclXmlUtils::ToOUString( rBuffer, nStart, nLength ) );
406 rWorksheet->endElement( XML_t );
407 rWorksheet->endElement( XML_r );
408 return nStart + nLength;
411 void XclExpString::WriteXml( XclExpXmlStream& rStrm ) const
413 sax_fastparser::FSHelperPtr rWorksheet = rStrm.GetCurrentStream();
415 if( !IsWriteFormats() )
417 rWorksheet->startElement( XML_t, FSEND );
418 rWorksheet->writeEscaped( XclXmlUtils::ToOUString( *this ) );
419 rWorksheet->endElement( XML_t );
421 else
423 XclExpFontBuffer& rFonts = rStrm.GetRoot().GetFontBuffer();
424 XclFormatRunVec::const_iterator aIt = maFormats.begin(), aEnd = maFormats.end();
426 sal_uInt16 nStart = 0;
427 const XclExpFont* pFont = NULL;
428 for ( ; aIt != aEnd; ++aIt )
430 // pFont getting first then pass it to run otherwise pFont is NULL.
431 pFont = rFonts.GetFont( aIt->mnFontIdx );
432 nStart = lcl_WriteRun( rStrm, GetUnicodeBuffer(),
433 nStart, aIt->mnChar-nStart, pFont );
435 lcl_WriteRun( rStrm, GetUnicodeBuffer(),
436 nStart, GetUnicodeBuffer().size() - nStart, pFont );
440 bool XclExpString::IsWriteFlags() const
442 return mbIsBiff8 && (!IsEmpty() || !mbSmartFlags);
445 bool XclExpString::IsWriteFormats() const
447 return mbIsBiff8 && !mbSkipFormats && IsRich();
450 void XclExpString::SetStrLen( sal_Int32 nNewLen )
452 sal_uInt16 nAllowedLen = (mb8BitLen && (mnMaxLen > 255)) ? 255 : mnMaxLen;
453 mnLen = limit_cast< sal_uInt16 >( nNewLen, 0, nAllowedLen );
456 void XclExpString::CharsToBuffer( const sal_Unicode* pcSource, sal_Int32 nBegin, sal_Int32 nLen )
458 OSL_ENSURE( maUniBuffer.size() >= static_cast< size_t >( nBegin + nLen ),
459 "XclExpString::CharsToBuffer - char buffer invalid" );
460 ScfUInt16Vec::iterator aBeg = maUniBuffer.begin() + nBegin;
461 ScfUInt16Vec::iterator aEnd = aBeg + nLen;
462 const sal_Unicode* pcSrcChar = pcSource;
463 for( ScfUInt16Vec::iterator aIt = aBeg; aIt != aEnd; ++aIt, ++pcSrcChar )
465 *aIt = static_cast< sal_uInt16 >( *pcSrcChar );
466 if( *aIt & 0xFF00 )
467 mbIsUnicode = true;
469 if( !mbWrapped )
470 mbWrapped = ::std::find( aBeg, aEnd, EXC_LF ) != aEnd;
473 void XclExpString::CharsToBuffer( const sal_Char* pcSource, sal_Int32 nBegin, sal_Int32 nLen )
475 OSL_ENSURE( maCharBuffer.size() >= static_cast< size_t >( nBegin + nLen ),
476 "XclExpString::CharsToBuffer - char buffer invalid" );
477 ScfUInt8Vec::iterator aBeg = maCharBuffer.begin() + nBegin;
478 ScfUInt8Vec::iterator aEnd = aBeg + nLen;
479 const sal_Char* pcSrcChar = pcSource;
480 for( ScfUInt8Vec::iterator aIt = aBeg; aIt != aEnd; ++aIt, ++pcSrcChar )
481 *aIt = static_cast< sal_uInt8 >( *pcSrcChar );
482 mbIsUnicode = false;
483 if( !mbWrapped )
484 mbWrapped = ::std::find( aBeg, aEnd, EXC_LF_C ) != aEnd;
487 void XclExpString::Init( sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen, bool bBiff8 )
489 mbIsBiff8 = bBiff8;
490 mbIsUnicode = bBiff8 && ::get_flag( nFlags, EXC_STR_FORCEUNICODE );
491 mb8BitLen = ::get_flag( nFlags, EXC_STR_8BITLENGTH );
492 mbSmartFlags = bBiff8 && ::get_flag( nFlags, EXC_STR_SMARTFLAGS );
493 mbSkipFormats = ::get_flag( nFlags, EXC_STR_SEPARATEFORMATS );
494 mbWrapped = false;
495 mbSkipHeader = ::get_flag( nFlags, EXC_STR_NOHEADER );
496 mnMaxLen = nMaxLen;
497 SetStrLen( nCurrLen );
499 maFormats.clear();
500 if( mbIsBiff8 )
502 maCharBuffer.clear();
503 maUniBuffer.resize( mnLen );
505 else
507 maUniBuffer.clear();
508 maCharBuffer.resize( mnLen );
512 void XclExpString::Build( const sal_Unicode* pcSource, sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen )
514 Init( nCurrLen, nFlags, nMaxLen, true );
515 CharsToBuffer( pcSource, 0, mnLen );
518 void XclExpString::Build( const sal_Char* pcSource, sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen )
520 Init( nCurrLen, nFlags, nMaxLen, false );
521 CharsToBuffer( pcSource, 0, mnLen );
524 void XclExpString::InitAppend( sal_Int32 nAddLen )
526 SetStrLen( static_cast< sal_Int32 >( mnLen ) + nAddLen );
527 if( mbIsBiff8 )
528 maUniBuffer.resize( mnLen );
529 else
530 maCharBuffer.resize( mnLen );
533 void XclExpString::BuildAppend( const sal_Unicode* pcSource, sal_Int32 nAddLen )
535 OSL_ENSURE( mbIsBiff8, "XclExpString::BuildAppend - must not be called at byte strings" );
536 if( mbIsBiff8 )
538 sal_uInt16 nOldLen = mnLen;
539 InitAppend( nAddLen );
540 CharsToBuffer( pcSource, nOldLen, mnLen - nOldLen );
544 void XclExpString::BuildAppend( const sal_Char* pcSource, sal_Int32 nAddLen )
546 OSL_ENSURE( !mbIsBiff8, "XclExpString::BuildAppend - must not be called at unicode strings" );
547 if( !mbIsBiff8 )
549 sal_uInt16 nOldLen = mnLen;
550 InitAppend( nAddLen );
551 CharsToBuffer( pcSource, nOldLen, mnLen - nOldLen );
555 void XclExpString::PrepareWrite( XclExpStream& rStrm, sal_uInt16 nBytes ) const
557 rStrm.SetSliceSize( nBytes + (mbIsUnicode ? 2 : 1) );
560 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */