xref: /aoo42x/main/oox/source/xls/richstring.cxx (revision ca5ec200)
1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/xls/richstring.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/text/XText.hpp>
27cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
28cdf0e10cSrcweir #include "oox/helper/attributelist.hxx"
29cdf0e10cSrcweir #include "oox/helper/propertyset.hxx"
30cdf0e10cSrcweir #include "oox/xls/biffinputstream.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace oox {
33cdf0e10cSrcweir namespace xls {
34cdf0e10cSrcweir 
35cdf0e10cSrcweir // ============================================================================
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace ::com::sun::star::text;
38cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using ::rtl::OString;
41cdf0e10cSrcweir using ::rtl::OUString;
42cdf0e10cSrcweir using ::rtl::OUStringBuffer;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // ============================================================================
45cdf0e10cSrcweir 
46cdf0e10cSrcweir namespace {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir const sal_uInt8 BIFF12_STRINGFLAG_FONTS         = 0x01;
49cdf0e10cSrcweir const sal_uInt8 BIFF12_STRINGFLAG_PHONETICS     = 0x02;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir inline bool lclNeedsRichTextFormat( const Font* pFont )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     return pFont && pFont->needsRichTextFormat();
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir } // namespace
57cdf0e10cSrcweir 
58cdf0e10cSrcweir // ============================================================================
59cdf0e10cSrcweir 
60cdf0e10cSrcweir RichStringPortion::RichStringPortion( const WorkbookHelper& rHelper ) :
61cdf0e10cSrcweir     WorkbookHelper( rHelper ),
62cdf0e10cSrcweir     mnFontId( -1 )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir void RichStringPortion::setText( const OUString& rText )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     maText = rText;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir FontRef RichStringPortion::createFont()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     mxFont.reset( new Font( *this, false ) );
74cdf0e10cSrcweir     return mxFont;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir void RichStringPortion::setFontId( sal_Int32 nFontId )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     mnFontId = nFontId;
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir void RichStringPortion::finalizeImport()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir     if( mxFont.get() )
85cdf0e10cSrcweir         mxFont->finalizeImport();
86cdf0e10cSrcweir     else if( mnFontId >= 0 )
87cdf0e10cSrcweir         mxFont = getStyles().getFont( mnFontId );
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir void RichStringPortion::convert( const Reference< XText >& rxText, const Font* pFont, bool bReplace )
91cdf0e10cSrcweir {
92cdf0e10cSrcweir     Reference< XTextRange > xRange;
93cdf0e10cSrcweir     if( bReplace )
94cdf0e10cSrcweir         xRange.set( rxText, UNO_QUERY );
95cdf0e10cSrcweir     else
96cdf0e10cSrcweir         xRange = rxText->getEnd();
97cdf0e10cSrcweir     OSL_ENSURE( xRange.is(), "RichStringPortion::convert - cannot get text range interface" );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     if( xRange.is() )
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         xRange->setString( maText );
102cdf0e10cSrcweir         if( mxFont.get() )
103cdf0e10cSrcweir         {
104cdf0e10cSrcweir             PropertySet aPropSet( xRange );
105cdf0e10cSrcweir             mxFont->writeToPropertySet( aPropSet, FONT_PROPTYPE_TEXT );
106cdf0e10cSrcweir         }
1070dac23a0SMichael Stahl         /*  Some font attributes cannot be set to cell formatting in Calc but
1080dac23a0SMichael Stahl             require to use rich formatting, e.g. font escapement. But do not
1090dac23a0SMichael Stahl             use the passed font if this portion has its own font. */
1100dac23a0SMichael Stahl         else if( lclNeedsRichTextFormat( pFont ) )
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             PropertySet aPropSet( xRange );
113cdf0e10cSrcweir             pFont->writeToPropertySet( aPropSet, FONT_PROPTYPE_TEXT );
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir // ----------------------------------------------------------------------------
119cdf0e10cSrcweir 
120cdf0e10cSrcweir void FontPortionModel::read( SequenceInputStream& rStrm )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     mnPos = rStrm.readuInt16();
123cdf0e10cSrcweir     mnFontId = rStrm.readuInt16();
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir void FontPortionModel::read( BiffInputStream& rStrm, BiffFontPortionMode eMode )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     switch( eMode )
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         case BIFF_FONTPORTION_8BIT:
131cdf0e10cSrcweir             mnPos = rStrm.readuInt8();
132cdf0e10cSrcweir             mnFontId = rStrm.readuInt8();
133cdf0e10cSrcweir         break;
134cdf0e10cSrcweir         case BIFF_FONTPORTION_16BIT:
135cdf0e10cSrcweir             mnPos = rStrm.readuInt16();
136cdf0e10cSrcweir             mnFontId = rStrm.readuInt16();
137cdf0e10cSrcweir         break;
138cdf0e10cSrcweir         case BIFF_FONTPORTION_OBJ:
139cdf0e10cSrcweir             mnPos = rStrm.readuInt16();
140cdf0e10cSrcweir             mnFontId = rStrm.readuInt16();
141cdf0e10cSrcweir             rStrm.skip( 4 );
142cdf0e10cSrcweir         break;
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir // ----------------------------------------------------------------------------
147cdf0e10cSrcweir 
148cdf0e10cSrcweir void FontPortionModelList::appendPortion( const FontPortionModel& rPortion )
149cdf0e10cSrcweir {
150cdf0e10cSrcweir     // #i33341# real life -- same character index may occur several times
151cdf0e10cSrcweir     OSL_ENSURE( empty() || (back().mnPos <= rPortion.mnPos), "FontPortionModelList::appendPortion - wrong char order" );
152cdf0e10cSrcweir     if( empty() || (back().mnPos < rPortion.mnPos) )
153cdf0e10cSrcweir         push_back( rPortion );
154cdf0e10cSrcweir     else
155cdf0e10cSrcweir         back().mnFontId = rPortion.mnFontId;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir void FontPortionModelList::importPortions( SequenceInputStream& rStrm )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     sal_Int32 nCount = rStrm.readInt32();
161cdf0e10cSrcweir     clear();
162cdf0e10cSrcweir     if( nCount > 0 )
163cdf0e10cSrcweir     {
164cdf0e10cSrcweir         reserve( getLimitedValue< size_t, sal_Int64 >( nCount, 0, rStrm.getRemaining() / 4 ) );
165cdf0e10cSrcweir         /*  #i33341# real life -- same character index may occur several times
166cdf0e10cSrcweir             -> use appendPortion() to validate string position. */
167cdf0e10cSrcweir         FontPortionModel aPortion;
168cdf0e10cSrcweir         for( sal_Int32 nIndex = 0; !rStrm.isEof() && (nIndex < nCount); ++nIndex )
169cdf0e10cSrcweir         {
170cdf0e10cSrcweir             aPortion.read( rStrm );
171cdf0e10cSrcweir             appendPortion( aPortion );
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir void FontPortionModelList::importPortions( BiffInputStream& rStrm, sal_uInt16 nCount, BiffFontPortionMode eMode )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir     clear();
179cdf0e10cSrcweir     reserve( nCount );
180cdf0e10cSrcweir     /*  #i33341# real life -- same character index may occur several times
181cdf0e10cSrcweir         -> use appendPortion() to validate string position. */
182cdf0e10cSrcweir     FontPortionModel aPortion;
183cdf0e10cSrcweir     for( sal_uInt16 nIndex = 0; !rStrm.isEof() && (nIndex < nCount); ++nIndex )
184cdf0e10cSrcweir     {
185cdf0e10cSrcweir         aPortion.read( rStrm, eMode );
186cdf0e10cSrcweir         appendPortion( aPortion );
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir void FontPortionModelList::importPortions( BiffInputStream& rStrm, bool b16Bit )
191cdf0e10cSrcweir {
192cdf0e10cSrcweir     sal_uInt16 nCount = b16Bit ? rStrm.readuInt16() : rStrm.readuInt8();
193cdf0e10cSrcweir     importPortions( rStrm, nCount, b16Bit ? BIFF_FONTPORTION_16BIT : BIFF_FONTPORTION_8BIT );
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir // ============================================================================
197cdf0e10cSrcweir 
198cdf0e10cSrcweir PhoneticDataModel::PhoneticDataModel() :
199cdf0e10cSrcweir     mnFontId( -1 ),
200cdf0e10cSrcweir     mnType( XML_fullwidthKatakana ),
201cdf0e10cSrcweir     mnAlignment( XML_left )
202cdf0e10cSrcweir {
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir void PhoneticDataModel::setBiffData( sal_Int32 nType, sal_Int32 nAlignment )
206cdf0e10cSrcweir {
207cdf0e10cSrcweir     static const sal_Int32 spnTypeIds[] = { XML_halfwidthKatakana, XML_fullwidthKatakana, XML_hiragana, XML_noConversion };
208cdf0e10cSrcweir     mnType = STATIC_ARRAY_SELECT( spnTypeIds, nType, XML_fullwidthKatakana );
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     static const sal_Int32 spnAlignments[] = { XML_noControl, XML_left, XML_center, XML_distributed };
211cdf0e10cSrcweir     mnAlignment = STATIC_ARRAY_SELECT( spnAlignments, nAlignment, XML_left );
212cdf0e10cSrcweir }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir // ----------------------------------------------------------------------------
215cdf0e10cSrcweir 
216cdf0e10cSrcweir PhoneticSettings::PhoneticSettings( const WorkbookHelper& rHelper ) :
217cdf0e10cSrcweir     WorkbookHelper( rHelper )
218cdf0e10cSrcweir {
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir void PhoneticSettings::importPhoneticPr( const AttributeList& rAttribs )
222cdf0e10cSrcweir {
223cdf0e10cSrcweir     maModel.mnFontId    = rAttribs.getInteger( XML_fontId, -1 );
224cdf0e10cSrcweir     maModel.mnType      = rAttribs.getToken( XML_type, XML_fullwidthKatakana );
225cdf0e10cSrcweir     maModel.mnAlignment = rAttribs.getToken( XML_alignment, XML_left );
226cdf0e10cSrcweir }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir void PhoneticSettings::importPhoneticPr( SequenceInputStream& rStrm )
229cdf0e10cSrcweir {
230cdf0e10cSrcweir     sal_uInt16 nFontId;
231cdf0e10cSrcweir     sal_Int32 nType, nAlignment;
232cdf0e10cSrcweir     rStrm >> nFontId >> nType >> nAlignment;
233cdf0e10cSrcweir     maModel.mnFontId = nFontId;
234cdf0e10cSrcweir     maModel.setBiffData( nType, nAlignment );
235cdf0e10cSrcweir }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir void PhoneticSettings::importPhoneticPr( BiffInputStream& rStrm )
238cdf0e10cSrcweir {
239cdf0e10cSrcweir     sal_uInt16 nFontId, nFlags;
240cdf0e10cSrcweir     rStrm >> nFontId >> nFlags;
241cdf0e10cSrcweir     maModel.mnFontId = nFontId;
242cdf0e10cSrcweir     maModel.setBiffData( extractValue< sal_Int32 >( nFlags, 0, 2 ), extractValue< sal_Int32 >( nFlags, 2, 2 ) );
243cdf0e10cSrcweir     // following: range list with cells showing phonetic text
244cdf0e10cSrcweir }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir void PhoneticSettings::importStringData( SequenceInputStream& rStrm )
247cdf0e10cSrcweir {
248cdf0e10cSrcweir     sal_uInt16 nFontId, nFlags;
249cdf0e10cSrcweir     rStrm >> nFontId >> nFlags;
250cdf0e10cSrcweir     maModel.mnFontId = nFontId;
251cdf0e10cSrcweir     maModel.setBiffData( extractValue< sal_Int32 >( nFlags, 0, 2 ), extractValue< sal_Int32 >( nFlags, 2, 2 ) );
252cdf0e10cSrcweir }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir void PhoneticSettings::importStringData( BiffInputStream& rStrm )
255cdf0e10cSrcweir {
256cdf0e10cSrcweir     sal_uInt16 nFontId, nFlags;
257cdf0e10cSrcweir     rStrm >> nFontId >> nFlags;
258cdf0e10cSrcweir     maModel.mnFontId = nFontId;
259cdf0e10cSrcweir     maModel.setBiffData( extractValue< sal_Int32 >( nFlags, 0, 2 ), extractValue< sal_Int32 >( nFlags, 2, 2 ) );
260cdf0e10cSrcweir }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir // ============================================================================
263cdf0e10cSrcweir 
264cdf0e10cSrcweir RichStringPhonetic::RichStringPhonetic( const WorkbookHelper& rHelper ) :
265cdf0e10cSrcweir     WorkbookHelper( rHelper ),
266cdf0e10cSrcweir     mnBasePos( -1 ),
267cdf0e10cSrcweir     mnBaseEnd( -1 )
268cdf0e10cSrcweir {
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir void RichStringPhonetic::setText( const OUString& rText )
272cdf0e10cSrcweir {
273cdf0e10cSrcweir     maText = rText;
274cdf0e10cSrcweir }
275cdf0e10cSrcweir 
276cdf0e10cSrcweir void RichStringPhonetic::importPhoneticRun( const AttributeList& rAttribs )
277cdf0e10cSrcweir {
278cdf0e10cSrcweir     mnBasePos = rAttribs.getInteger( XML_sb, -1 );
279cdf0e10cSrcweir     mnBaseEnd = rAttribs.getInteger( XML_eb, -1 );
280cdf0e10cSrcweir }
281cdf0e10cSrcweir 
282cdf0e10cSrcweir void RichStringPhonetic::setBaseRange( sal_Int32 nBasePos, sal_Int32 nBaseEnd )
283cdf0e10cSrcweir {
284cdf0e10cSrcweir     mnBasePos = nBasePos;
285cdf0e10cSrcweir     mnBaseEnd = nBaseEnd;
286cdf0e10cSrcweir }
287cdf0e10cSrcweir 
288cdf0e10cSrcweir // ----------------------------------------------------------------------------
289cdf0e10cSrcweir 
290cdf0e10cSrcweir void PhoneticPortionModel::read( SequenceInputStream& rStrm )
291cdf0e10cSrcweir {
292cdf0e10cSrcweir     mnPos = rStrm.readuInt16();
293cdf0e10cSrcweir     mnBasePos = rStrm.readuInt16();
294cdf0e10cSrcweir     mnBaseLen = rStrm.readuInt16();
295cdf0e10cSrcweir }
296cdf0e10cSrcweir 
297cdf0e10cSrcweir void PhoneticPortionModel::read( BiffInputStream& rStrm )
298cdf0e10cSrcweir {
299cdf0e10cSrcweir     mnPos = rStrm.readuInt16();
300cdf0e10cSrcweir     mnBasePos = rStrm.readuInt16();
301cdf0e10cSrcweir     mnBaseLen = rStrm.readuInt16();
302cdf0e10cSrcweir }
303cdf0e10cSrcweir 
304cdf0e10cSrcweir // ----------------------------------------------------------------------------
305cdf0e10cSrcweir 
306cdf0e10cSrcweir void PhoneticPortionModelList::appendPortion( const PhoneticPortionModel& rPortion )
307cdf0e10cSrcweir {
308cdf0e10cSrcweir     // same character index may occur several times
309cdf0e10cSrcweir     OSL_ENSURE( empty() || ((back().mnPos <= rPortion.mnPos) &&
310cdf0e10cSrcweir         (back().mnBasePos + back().mnBaseLen <= rPortion.mnBasePos)),
311cdf0e10cSrcweir         "PhoneticPortionModelList::appendPortion - wrong char order" );
312cdf0e10cSrcweir     if( empty() || (back().mnPos < rPortion.mnPos) )
313cdf0e10cSrcweir     {
314cdf0e10cSrcweir         push_back( rPortion );
315cdf0e10cSrcweir     }
316cdf0e10cSrcweir     else if( back().mnPos == rPortion.mnPos )
317cdf0e10cSrcweir     {
318cdf0e10cSrcweir         back().mnBasePos = rPortion.mnBasePos;
319cdf0e10cSrcweir         back().mnBaseLen = rPortion.mnBaseLen;
320cdf0e10cSrcweir     }
321cdf0e10cSrcweir }
322cdf0e10cSrcweir 
323cdf0e10cSrcweir void PhoneticPortionModelList::importPortions( SequenceInputStream& rStrm )
324cdf0e10cSrcweir {
325cdf0e10cSrcweir     sal_Int32 nCount = rStrm.readInt32();
326cdf0e10cSrcweir     clear();
327cdf0e10cSrcweir     if( nCount > 0 )
328cdf0e10cSrcweir     {
329cdf0e10cSrcweir         reserve( getLimitedValue< size_t, sal_Int64 >( nCount, 0, rStrm.getRemaining() / 6 ) );
330cdf0e10cSrcweir         PhoneticPortionModel aPortion;
331cdf0e10cSrcweir         for( sal_Int32 nIndex = 0; !rStrm.isEof() && (nIndex < nCount); ++nIndex )
332cdf0e10cSrcweir         {
333cdf0e10cSrcweir             aPortion.read( rStrm );
334cdf0e10cSrcweir             appendPortion( aPortion );
335cdf0e10cSrcweir         }
336cdf0e10cSrcweir     }
337cdf0e10cSrcweir }
338cdf0e10cSrcweir 
339cdf0e10cSrcweir OUString PhoneticPortionModelList::importPortions( BiffInputStream& rStrm, sal_Int32 nPhoneticSize )
340cdf0e10cSrcweir {
341cdf0e10cSrcweir     OUString aPhoneticText;
342cdf0e10cSrcweir     sal_uInt16 nPortionCount, nTextLen1, nTextLen2;
343cdf0e10cSrcweir     rStrm >> nPortionCount >> nTextLen1 >> nTextLen2;
344cdf0e10cSrcweir     OSL_ENSURE( nTextLen1 == nTextLen2, "PhoneticPortionModelList::importPortions - wrong phonetic text length" );
345cdf0e10cSrcweir     if( (nTextLen1 == nTextLen2) && (nTextLen1 > 0) )
346cdf0e10cSrcweir     {
347cdf0e10cSrcweir         sal_Int32 nMinSize = 2 * nTextLen1 + 6 * nPortionCount + 14;
348cdf0e10cSrcweir         OSL_ENSURE( nMinSize <= nPhoneticSize, "PhoneticPortionModelList::importPortions - wrong size of phonetic data" );
349cdf0e10cSrcweir         if( nMinSize <= nPhoneticSize )
350cdf0e10cSrcweir         {
351cdf0e10cSrcweir             aPhoneticText = rStrm.readUnicodeArray( nTextLen1 );
352cdf0e10cSrcweir             clear();
353cdf0e10cSrcweir             reserve( nPortionCount );
354cdf0e10cSrcweir             PhoneticPortionModel aPortion;
355cdf0e10cSrcweir             for( sal_uInt16 nPortion = 0; nPortion < nPortionCount; ++nPortion )
356cdf0e10cSrcweir             {
357cdf0e10cSrcweir                 aPortion.read( rStrm );
358cdf0e10cSrcweir                 appendPortion( aPortion );
359cdf0e10cSrcweir             }
360cdf0e10cSrcweir         }
361cdf0e10cSrcweir     }
362cdf0e10cSrcweir     return aPhoneticText;
363cdf0e10cSrcweir }
364cdf0e10cSrcweir 
365cdf0e10cSrcweir // ============================================================================
366cdf0e10cSrcweir 
367cdf0e10cSrcweir RichString::RichString( const WorkbookHelper& rHelper ) :
368cdf0e10cSrcweir     WorkbookHelper( rHelper ),
369cdf0e10cSrcweir     maPhonSettings( rHelper )
370cdf0e10cSrcweir {
371cdf0e10cSrcweir }
372cdf0e10cSrcweir 
373cdf0e10cSrcweir RichStringPortionRef RichString::importText( const AttributeList& )
374cdf0e10cSrcweir {
375cdf0e10cSrcweir     return createPortion();
376cdf0e10cSrcweir }
377cdf0e10cSrcweir 
378cdf0e10cSrcweir RichStringPortionRef RichString::importRun( const AttributeList& )
379cdf0e10cSrcweir {
380cdf0e10cSrcweir     return createPortion();
381cdf0e10cSrcweir }
382cdf0e10cSrcweir 
383cdf0e10cSrcweir RichStringPhoneticRef RichString::importPhoneticRun( const AttributeList& rAttribs )
384cdf0e10cSrcweir {
385cdf0e10cSrcweir     RichStringPhoneticRef xPhonetic = createPhonetic();
386cdf0e10cSrcweir     xPhonetic->importPhoneticRun( rAttribs );
387cdf0e10cSrcweir     return xPhonetic;
388cdf0e10cSrcweir }
389cdf0e10cSrcweir 
390cdf0e10cSrcweir void RichString::importPhoneticPr( const AttributeList& rAttribs )
391cdf0e10cSrcweir {
392cdf0e10cSrcweir     maPhonSettings.importPhoneticPr( rAttribs );
393cdf0e10cSrcweir }
394cdf0e10cSrcweir 
395cdf0e10cSrcweir void RichString::importString( SequenceInputStream& rStrm, bool bRich )
396cdf0e10cSrcweir {
397cdf0e10cSrcweir     sal_uInt8 nFlags = bRich ? rStrm.readuInt8() : 0;
398cdf0e10cSrcweir     OUString aBaseText = BiffHelper::readString( rStrm );
399cdf0e10cSrcweir 
400cdf0e10cSrcweir     if( !rStrm.isEof() && getFlag( nFlags, BIFF12_STRINGFLAG_FONTS ) )
401cdf0e10cSrcweir     {
402cdf0e10cSrcweir         FontPortionModelList aPortions;
403cdf0e10cSrcweir         aPortions.importPortions( rStrm );
404cdf0e10cSrcweir         createTextPortions( aBaseText, aPortions );
405cdf0e10cSrcweir     }
406cdf0e10cSrcweir     else
407cdf0e10cSrcweir     {
408cdf0e10cSrcweir         createPortion()->setText( aBaseText );
409cdf0e10cSrcweir     }
410cdf0e10cSrcweir 
411cdf0e10cSrcweir     if( !rStrm.isEof() && getFlag( nFlags, BIFF12_STRINGFLAG_PHONETICS ) )
412cdf0e10cSrcweir     {
413cdf0e10cSrcweir         OUString aPhoneticText = BiffHelper::readString( rStrm );
414cdf0e10cSrcweir         PhoneticPortionModelList aPortions;
415cdf0e10cSrcweir         aPortions.importPortions( rStrm );
416cdf0e10cSrcweir         maPhonSettings.importStringData( rStrm );
417cdf0e10cSrcweir         createPhoneticPortions( aPhoneticText, aPortions, aBaseText.getLength() );
418cdf0e10cSrcweir     }
419cdf0e10cSrcweir }
420cdf0e10cSrcweir 
421cdf0e10cSrcweir void RichString::importCharArray( BiffInputStream& rStrm, sal_uInt16 nChars, rtl_TextEncoding eTextEnc )
422cdf0e10cSrcweir {
423cdf0e10cSrcweir     createPortion()->setText( rStrm.readCharArrayUC( nChars, eTextEnc ) );
424cdf0e10cSrcweir }
425cdf0e10cSrcweir 
426cdf0e10cSrcweir void RichString::importByteString( BiffInputStream& rStrm, rtl_TextEncoding eTextEnc, BiffStringFlags nFlags )
427cdf0e10cSrcweir {
428cdf0e10cSrcweir     OSL_ENSURE( !getFlag( nFlags, BIFF_STR_KEEPFONTS ), "RichString::importString - keep fonts not implemented" );
429cdf0e10cSrcweir     OSL_ENSURE( !getFlag( nFlags, static_cast< BiffStringFlags >( ~(BIFF_STR_8BITLENGTH | BIFF_STR_EXTRAFONTS) ) ), "RichString::importByteString - unknown flag" );
430cdf0e10cSrcweir     bool b8BitLength = getFlag( nFlags, BIFF_STR_8BITLENGTH );
431cdf0e10cSrcweir 
432cdf0e10cSrcweir     OString aBaseText = rStrm.readByteString( !b8BitLength );
433cdf0e10cSrcweir 
434cdf0e10cSrcweir     if( !rStrm.isEof() && getFlag( nFlags, BIFF_STR_EXTRAFONTS ) )
435cdf0e10cSrcweir     {
436cdf0e10cSrcweir         FontPortionModelList aPortions;
437cdf0e10cSrcweir         aPortions.importPortions( rStrm, false );
438cdf0e10cSrcweir         createTextPortions( aBaseText, eTextEnc, aPortions );
439cdf0e10cSrcweir     }
440cdf0e10cSrcweir     else
441cdf0e10cSrcweir     {
442cdf0e10cSrcweir         createPortion()->setText( OStringToOUString( aBaseText, eTextEnc ) );
443cdf0e10cSrcweir     }
444cdf0e10cSrcweir }
445cdf0e10cSrcweir 
446cdf0e10cSrcweir void RichString::importUniString( BiffInputStream& rStrm, BiffStringFlags nFlags )
447cdf0e10cSrcweir {
448cdf0e10cSrcweir     OSL_ENSURE( !getFlag( nFlags, BIFF_STR_KEEPFONTS ), "RichString::importUniString - keep fonts not implemented" );
449cdf0e10cSrcweir     OSL_ENSURE( !getFlag( nFlags, static_cast< BiffStringFlags >( ~(BIFF_STR_8BITLENGTH | BIFF_STR_SMARTFLAGS) ) ), "RichString::importUniString - unknown flag" );
450cdf0e10cSrcweir     bool b8BitLength = getFlag( nFlags, BIFF_STR_8BITLENGTH );
451cdf0e10cSrcweir 
452cdf0e10cSrcweir     // --- string header ---
453cdf0e10cSrcweir     sal_uInt16 nChars = b8BitLength ? rStrm.readuInt8() : rStrm.readuInt16();
454cdf0e10cSrcweir     sal_uInt8 nFlagField = 0;
455cdf0e10cSrcweir     if( (nChars > 0) || !getFlag( nFlags, BIFF_STR_SMARTFLAGS ) )
456cdf0e10cSrcweir         rStrm >> nFlagField;
457cdf0e10cSrcweir     bool b16Bit    = getFlag( nFlagField, BIFF_STRF_16BIT );
458cdf0e10cSrcweir     bool bFonts    = getFlag( nFlagField, BIFF_STRF_RICH );
459cdf0e10cSrcweir     bool bPhonetic = getFlag( nFlagField, BIFF_STRF_PHONETIC );
460cdf0e10cSrcweir     sal_uInt16 nFontCount = bFonts ? rStrm.readuInt16() : 0;
461cdf0e10cSrcweir     sal_Int32 nPhoneticSize = bPhonetic ? rStrm.readInt32() : 0;
462cdf0e10cSrcweir 
463cdf0e10cSrcweir     // --- character array ---
464cdf0e10cSrcweir     OUString aBaseText = rStrm.readUniStringChars( nChars, b16Bit );
465cdf0e10cSrcweir 
466cdf0e10cSrcweir     // --- formatting ---
467cdf0e10cSrcweir     // #122185# bRich flag may be set, but format runs may be missing
468cdf0e10cSrcweir     if( !rStrm.isEof() && (nFontCount > 0) )
469cdf0e10cSrcweir     {
470cdf0e10cSrcweir         FontPortionModelList aPortions;
471cdf0e10cSrcweir         aPortions.importPortions( rStrm, nFontCount, BIFF_FONTPORTION_16BIT );
472cdf0e10cSrcweir         createTextPortions( aBaseText, aPortions );
473cdf0e10cSrcweir     }
474cdf0e10cSrcweir     else
475cdf0e10cSrcweir     {
476cdf0e10cSrcweir         createPortion()->setText( aBaseText );
477cdf0e10cSrcweir     }
478cdf0e10cSrcweir 
479cdf0e10cSrcweir     // --- Asian phonetic information ---
480cdf0e10cSrcweir     // #122185# bPhonetic flag may be set, but phonetic info may be missing
481cdf0e10cSrcweir     if( !rStrm.isEof() && (nPhoneticSize > 0) )
482cdf0e10cSrcweir     {
483cdf0e10cSrcweir         sal_Int64 nPhoneticEnd = rStrm.tell() + nPhoneticSize;
484cdf0e10cSrcweir         OSL_ENSURE( nPhoneticSize > 14, "RichString::importUniString - wrong size of phonetic data" );
485cdf0e10cSrcweir         if( nPhoneticSize > 14 )
486cdf0e10cSrcweir         {
487cdf0e10cSrcweir             sal_uInt16 nId, nSize;
488cdf0e10cSrcweir             rStrm >> nId >> nSize;
489cdf0e10cSrcweir             OSL_ENSURE( nId == 1, "RichString::importUniString - unknown phonetic data identifier" );
490cdf0e10cSrcweir             sal_Int32 nMinSize = nSize + 4;
491cdf0e10cSrcweir             OSL_ENSURE( nMinSize <= nPhoneticSize, "RichString::importUniString - wrong size of phonetic data" );
492cdf0e10cSrcweir             if( (nId == 1) && (nMinSize <= nPhoneticSize) )
493cdf0e10cSrcweir             {
494cdf0e10cSrcweir                 maPhonSettings.importStringData( rStrm );
495cdf0e10cSrcweir                 PhoneticPortionModelList aPortions;
496cdf0e10cSrcweir                 OUString aPhoneticText = aPortions.importPortions( rStrm, nPhoneticSize );
497cdf0e10cSrcweir                 createPhoneticPortions( aPhoneticText, aPortions, aBaseText.getLength() );
498cdf0e10cSrcweir             }
499cdf0e10cSrcweir         }
500cdf0e10cSrcweir         rStrm.seek( nPhoneticEnd );
501cdf0e10cSrcweir     }
502cdf0e10cSrcweir }
503cdf0e10cSrcweir 
504cdf0e10cSrcweir void RichString::finalizeImport()
505cdf0e10cSrcweir {
506cdf0e10cSrcweir     maTextPortions.forEachMem( &RichStringPortion::finalizeImport );
507cdf0e10cSrcweir }
508cdf0e10cSrcweir 
509cdf0e10cSrcweir bool RichString::extractPlainString( OUString& orString, const Font* pFirstPortionFont ) const
510cdf0e10cSrcweir {
511cdf0e10cSrcweir     if( !maPhonPortions.empty() )
512cdf0e10cSrcweir         return false;
513cdf0e10cSrcweir     if( maTextPortions.empty() )
514cdf0e10cSrcweir     {
515cdf0e10cSrcweir         orString = OUString();
516cdf0e10cSrcweir         return true;
517cdf0e10cSrcweir     }
518cdf0e10cSrcweir     if( (maTextPortions.size() == 1) && !maTextPortions.front()->hasFont() && !lclNeedsRichTextFormat( pFirstPortionFont ) )
519cdf0e10cSrcweir     {
520cdf0e10cSrcweir         orString = maTextPortions.front()->getText();
5210dac23a0SMichael Stahl         return orString.indexOf( '\x0A' ) < 0;
522cdf0e10cSrcweir     }
523cdf0e10cSrcweir     return false;
524cdf0e10cSrcweir }
525cdf0e10cSrcweir 
5260dac23a0SMichael Stahl void RichString::convert( const Reference< XText >& rxText, bool bReplaceOld, const Font* pFirstPortionFont ) const
527cdf0e10cSrcweir {
528cdf0e10cSrcweir     for( PortionVector::const_iterator aIt = maTextPortions.begin(), aEnd = maTextPortions.end(); aIt != aEnd; ++aIt )
529cdf0e10cSrcweir     {
5300dac23a0SMichael Stahl         (*aIt)->convert( rxText, pFirstPortionFont, bReplaceOld );
531cdf0e10cSrcweir         pFirstPortionFont = 0;  // use passed font for first portion only
5320dac23a0SMichael Stahl         bReplaceOld = false;    // do not replace first portion text with following portions
533cdf0e10cSrcweir     }
534cdf0e10cSrcweir }
535cdf0e10cSrcweir 
536cdf0e10cSrcweir // private --------------------------------------------------------------------
537cdf0e10cSrcweir 
538cdf0e10cSrcweir RichStringPortionRef RichString::createPortion()
539cdf0e10cSrcweir {
540cdf0e10cSrcweir     RichStringPortionRef xPortion( new RichStringPortion( *this ) );
541cdf0e10cSrcweir     maTextPortions.push_back( xPortion );
542cdf0e10cSrcweir     return xPortion;
543cdf0e10cSrcweir }
544cdf0e10cSrcweir 
545cdf0e10cSrcweir RichStringPhoneticRef RichString::createPhonetic()
546cdf0e10cSrcweir {
547cdf0e10cSrcweir     RichStringPhoneticRef xPhonetic( new RichStringPhonetic( *this ) );
548cdf0e10cSrcweir     maPhonPortions.push_back( xPhonetic );
549cdf0e10cSrcweir     return xPhonetic;
550cdf0e10cSrcweir }
551cdf0e10cSrcweir 
552cdf0e10cSrcweir void RichString::createTextPortions( const OString& rText, rtl_TextEncoding eTextEnc, FontPortionModelList& rPortions )
553cdf0e10cSrcweir {
554cdf0e10cSrcweir     maTextPortions.clear();
555cdf0e10cSrcweir     sal_Int32 nStrLen = rText.getLength();
556cdf0e10cSrcweir     if( nStrLen > 0 )
557cdf0e10cSrcweir     {
558cdf0e10cSrcweir         // add leading and trailing string position to ease the following loop
559cdf0e10cSrcweir         if( rPortions.empty() || (rPortions.front().mnPos > 0) )
560cdf0e10cSrcweir             rPortions.insert( rPortions.begin(), FontPortionModel( 0, -1 ) );
561cdf0e10cSrcweir         if( rPortions.back().mnPos < nStrLen )
562cdf0e10cSrcweir             rPortions.push_back( FontPortionModel( nStrLen, -1 ) );
563cdf0e10cSrcweir 
564cdf0e10cSrcweir         // create all string portions according to the font id vector
565cdf0e10cSrcweir         for( FontPortionModelList::const_iterator aIt = rPortions.begin(); aIt->mnPos < nStrLen; ++aIt )
566cdf0e10cSrcweir         {
567cdf0e10cSrcweir             sal_Int32 nPortionLen = (aIt + 1)->mnPos - aIt->mnPos;
568cdf0e10cSrcweir             if( (0 < nPortionLen) && (aIt->mnPos + nPortionLen <= nStrLen) )
569cdf0e10cSrcweir             {
570cdf0e10cSrcweir                 // convert byte string to unicode string, using current font encoding
571cdf0e10cSrcweir                 FontRef xFont = getStyles().getFont( aIt->mnFontId );
572cdf0e10cSrcweir                 rtl_TextEncoding eFontEnc = xFont.get() ? xFont->getFontEncoding() : eTextEnc;
573cdf0e10cSrcweir                 OUString aUniStr = OStringToOUString( rText.copy( aIt->mnPos, nPortionLen ), eFontEnc );
574cdf0e10cSrcweir                 // create string portion
575cdf0e10cSrcweir                 RichStringPortionRef xPortion = createPortion();
576cdf0e10cSrcweir                 xPortion->setText( aUniStr );
577cdf0e10cSrcweir                 xPortion->setFontId( aIt->mnFontId );
578cdf0e10cSrcweir             }
579cdf0e10cSrcweir         }
580cdf0e10cSrcweir     }
581cdf0e10cSrcweir }
582cdf0e10cSrcweir 
583cdf0e10cSrcweir void RichString::createTextPortions( const OUString& rText, FontPortionModelList& rPortions )
584cdf0e10cSrcweir {
585cdf0e10cSrcweir     maTextPortions.clear();
586cdf0e10cSrcweir     sal_Int32 nStrLen = rText.getLength();
587cdf0e10cSrcweir     if( nStrLen > 0 )
588cdf0e10cSrcweir     {
589cdf0e10cSrcweir         // add leading and trailing string position to ease the following loop
590cdf0e10cSrcweir         if( rPortions.empty() || (rPortions.front().mnPos > 0) )
591cdf0e10cSrcweir             rPortions.insert( rPortions.begin(), FontPortionModel( 0, -1 ) );
592cdf0e10cSrcweir         if( rPortions.back().mnPos < nStrLen )
593cdf0e10cSrcweir             rPortions.push_back( FontPortionModel( nStrLen, -1 ) );
594cdf0e10cSrcweir 
595cdf0e10cSrcweir         // create all string portions according to the font id vector
596cdf0e10cSrcweir         for( FontPortionModelList::const_iterator aIt = rPortions.begin(); aIt->mnPos < nStrLen; ++aIt )
597cdf0e10cSrcweir         {
598cdf0e10cSrcweir             sal_Int32 nPortionLen = (aIt + 1)->mnPos - aIt->mnPos;
599cdf0e10cSrcweir             if( (0 < nPortionLen) && (aIt->mnPos + nPortionLen <= nStrLen) )
600cdf0e10cSrcweir             {
601cdf0e10cSrcweir                 RichStringPortionRef xPortion = createPortion();
602cdf0e10cSrcweir                 xPortion->setText( rText.copy( aIt->mnPos, nPortionLen ) );
603cdf0e10cSrcweir                 xPortion->setFontId( aIt->mnFontId );
604cdf0e10cSrcweir             }
605cdf0e10cSrcweir         }
606cdf0e10cSrcweir     }
607cdf0e10cSrcweir }
608cdf0e10cSrcweir 
609cdf0e10cSrcweir void RichString::createPhoneticPortions( const ::rtl::OUString& rText, PhoneticPortionModelList& rPortions, sal_Int32 nBaseLen )
610cdf0e10cSrcweir {
611cdf0e10cSrcweir     maPhonPortions.clear();
612cdf0e10cSrcweir     sal_Int32 nStrLen = rText.getLength();
613cdf0e10cSrcweir     if( nStrLen > 0 )
614cdf0e10cSrcweir     {
615cdf0e10cSrcweir         // no portions - assign phonetic text to entire base text
616cdf0e10cSrcweir         if( rPortions.empty() )
617cdf0e10cSrcweir             rPortions.push_back( PhoneticPortionModel( 0, 0, nBaseLen ) );
618cdf0e10cSrcweir         // add trailing string position to ease the following loop
619cdf0e10cSrcweir         if( rPortions.back().mnPos < nStrLen )
620cdf0e10cSrcweir             rPortions.push_back( PhoneticPortionModel( nStrLen, nBaseLen, 0 ) );
621cdf0e10cSrcweir 
622cdf0e10cSrcweir         // create all phonetic portions according to the portions vector
623cdf0e10cSrcweir         for( PhoneticPortionModelList::const_iterator aIt = rPortions.begin(); aIt->mnPos < nStrLen; ++aIt )
624cdf0e10cSrcweir         {
625cdf0e10cSrcweir             sal_Int32 nPortionLen = (aIt + 1)->mnPos - aIt->mnPos;
626cdf0e10cSrcweir             if( (0 < nPortionLen) && (aIt->mnPos + nPortionLen <= nStrLen) )
627cdf0e10cSrcweir             {
628cdf0e10cSrcweir                 RichStringPhoneticRef xPhonetic = createPhonetic();
629cdf0e10cSrcweir                 xPhonetic->setText( rText.copy( aIt->mnPos, nPortionLen ) );
630cdf0e10cSrcweir                 xPhonetic->setBaseRange( aIt->mnBasePos, aIt->mnBasePos + aIt->mnBaseLen );
631cdf0e10cSrcweir             }
632cdf0e10cSrcweir         }
633cdf0e10cSrcweir     }
634cdf0e10cSrcweir }
635cdf0e10cSrcweir 
636cdf0e10cSrcweir // ============================================================================
637cdf0e10cSrcweir 
638cdf0e10cSrcweir } // namespace xls
639cdf0e10cSrcweir } // namespace oox
640