1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xistring.cxx,v $
10 * $Revision: 1.5.90.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_sc.hxx"
32 #include "xistring.hxx"
33 #include "xlstyle.hxx"
34 #include "xistream.hxx"
37 // Byte/Unicode strings =======================================================
39 /** All allowed flags for import. */
40 const XclStrFlags nAllowedFlags
= EXC_STR_8BITLENGTH
| EXC_STR_SMARTFLAGS
| EXC_STR_SEPARATEFORMATS
;
42 // ----------------------------------------------------------------------------
44 XclImpString::XclImpString()
48 XclImpString::XclImpString( const String
& rString
) :
53 XclImpString::~XclImpString()
57 void XclImpString::Read( XclImpStream
& rStrm
, XclStrFlags nFlags
)
59 if( !::get_flag( nFlags
, EXC_STR_SEPARATEFORMATS
) )
62 DBG_ASSERT( (nFlags
& ~nAllowedFlags
) == 0, "XclImpString::Read - unknown flag" );
63 bool b16BitLen
= !::get_flag( nFlags
, EXC_STR_8BITLENGTH
);
65 switch( rStrm
.GetRoot().GetBiff() )
71 // no integrated formatting in BIFF2-BIFF7
72 maString
= rStrm
.ReadByteString( b16BitLen
);
77 // --- string header ---
78 sal_uInt16 nChars
= b16BitLen
? rStrm
.ReaduInt16() : rStrm
.ReaduInt8();
79 sal_uInt8 nFlagField
= 0;
80 if( nChars
|| !::get_flag( nFlags
, EXC_STR_SMARTFLAGS
) )
83 bool b16Bit
, bRich
, bFarEast
;
86 rStrm
.ReadUniStringExtHeader( b16Bit
, bRich
, bFarEast
, nRunCount
, nExtInf
, nFlagField
);
87 // #122185# ignore the flags, they may be wrong
89 // --- character array ---
90 maString
= rStrm
.ReadRawUniString( nChars
, b16Bit
);
94 ReadFormats( rStrm
, nRunCount
);
96 // --- extended (FarEast) information ---
97 rStrm
.Ignore( nExtInf
);
106 void XclImpString::AppendFormat( XclFormatRunVec
& rFormats
, sal_uInt16 nChar
, sal_uInt16 nFontIdx
)
108 // #i33341# real life -- same character index may occur several times
109 DBG_ASSERT( rFormats
.empty() || (rFormats
.back().mnChar
<= nChar
), "XclImpString::AppendFormat - wrong char order" );
110 if( rFormats
.empty() || (rFormats
.back().mnChar
< nChar
) )
111 rFormats
.push_back( XclFormatRun( nChar
, nFontIdx
) );
113 rFormats
.back().mnFontIdx
= nFontIdx
;
116 void XclImpString::ReadFormats( XclImpStream
& rStrm
, XclFormatRunVec
& rFormats
)
118 bool bBiff8
= rStrm
.GetRoot().GetBiff() == EXC_BIFF8
;
119 sal_uInt16 nRunCount
= bBiff8
? rStrm
.ReaduInt16() : rStrm
.ReaduInt8();
120 ReadFormats( rStrm
, rFormats
, nRunCount
);
123 void XclImpString::ReadFormats( XclImpStream
& rStrm
, XclFormatRunVec
& rFormats
, sal_uInt16 nRunCount
)
126 rFormats
.reserve( nRunCount
);
127 /* #i33341# real life -- same character index may occur several times
128 -> use AppendFormat() to validate formats */
129 if( rStrm
.GetRoot().GetBiff() == EXC_BIFF8
)
131 for( sal_uInt16 nIdx
= 0; nIdx
< nRunCount
; ++nIdx
)
133 sal_uInt16 nChar
, nFontIdx
;
134 rStrm
>> nChar
>> nFontIdx
;
135 AppendFormat( rFormats
, nChar
, nFontIdx
);
140 for( sal_uInt16 nIdx
= 0; nIdx
< nRunCount
; ++nIdx
)
142 sal_uInt8 nChar
, nFontIdx
;
143 rStrm
>> nChar
>> nFontIdx
;
144 AppendFormat( rFormats
, nChar
, nFontIdx
);
149 void XclImpString::ReadObjFormats( XclImpStream
& rStrm
, XclFormatRunVec
& rFormats
, sal_uInt16 nFormatSize
)
151 // number of formatting runs, each takes 8 bytes
152 sal_uInt16 nRunCount
= nFormatSize
/ 8;
154 rFormats
.reserve( nRunCount
);
155 for( sal_uInt16 nIdx
= 0; nIdx
< nRunCount
; ++nIdx
)
157 sal_uInt16 nChar
, nFontIdx
;
158 rStrm
>> nChar
>> nFontIdx
;
160 AppendFormat( rFormats
, nChar
, nFontIdx
);
164 // String iterator ============================================================
166 XclImpStringIterator::XclImpStringIterator( const XclImpString
& rString
) :
167 mrText( rString
.GetText() ),
168 mrFormats( rString
.GetFormats() ),
175 // first portion is formatted, adjust vector index to next portion
176 if( !mrFormats
.empty() && (mrFormats
.front().mnChar
== 0) )
178 // find end position of the first portion
179 mnTextEnd
= static_cast< xub_StrLen
>( (mnFormatsEnd
< mrFormats
.size()) ?
180 mrFormats
[ mnFormatsEnd
].mnChar
: mrText
.Len() );
183 String
XclImpStringIterator::GetPortionText() const
185 return String( mrText
, mnTextBeg
, mnTextEnd
- mnTextBeg
);
188 sal_uInt16
XclImpStringIterator::GetPortionFont() const
190 return (mnFormatsBeg
< mnFormatsEnd
) ? mrFormats
[ mnFormatsBeg
].mnFontIdx
: EXC_FONT_NOTFOUND
;
193 XclImpStringIterator
& XclImpStringIterator::operator++()
200 // indexes into vector of formatting runs
201 if( mnFormatsBeg
< mnFormatsEnd
)
203 if( mnFormatsEnd
< mrFormats
.size() )
205 // character positions of next portion
206 mnTextBeg
= mnTextEnd
;
207 mnTextEnd
= static_cast< xub_StrLen
>( (mnFormatsEnd
< mrFormats
.size()) ?
208 mrFormats
[ mnFormatsEnd
].mnChar
: mrText
.Len() );
210 while( Is() && (mnTextBeg
== mnTextEnd
) );
215 // ============================================================================