1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <richstring.hxx>
21 #include <biffhelper.hxx>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/text/XText.hpp>
25 #include <rtl/ustrbuf.hxx>
26 #include <editeng/editobj.hxx>
27 #include <osl/diagnose.h>
28 #include <oox/helper/binaryinputstream.hxx>
29 #include <oox/helper/attributelist.hxx>
30 #include <oox/helper/propertyset.hxx>
31 #include <oox/token/tokens.hxx>
32 #include <editutil.hxx>
34 #include <vcl/svapp.hxx>
39 using namespace ::com::sun::star::text
;
40 using namespace ::com::sun::star::uno
;
44 const sal_uInt8 BIFF12_STRINGFLAG_FONTS
= 0x01;
45 const sal_uInt8 BIFF12_STRINGFLAG_PHONETICS
= 0x02;
47 bool lclNeedsRichTextFormat( const oox::xls::Font
* pFont
)
49 return pFont
&& pFont
->needsRichTextFormat();
54 RichStringPortion::RichStringPortion( const WorkbookHelper
& rHelper
) :
55 WorkbookHelper( rHelper
),
61 void RichStringPortion::setText( const OUString
& rText
)
66 FontRef
const & RichStringPortion::createFont()
68 mxFont
.reset( new Font( *this, false ) );
72 void RichStringPortion::setFontId( sal_Int32 nFontId
)
77 void RichStringPortion::finalizeImport()
80 mxFont
->finalizeImport();
81 else if( mnFontId
>= 0 )
82 mxFont
= getStyles().getFont( mnFontId
);
85 void RichStringPortion::convert( const Reference
< XText
>& rxText
, bool bReplace
)
90 Reference
< XTextRange
> xRange
;
94 xRange
= rxText
->getEnd();
95 OSL_ENSURE( xRange
.is(), "RichStringPortion::convert - cannot get text range interface" );
99 xRange
->setString( maText
);
102 PropertySet
aPropSet( xRange
);
103 mxFont
->writeToPropertySet( aPropSet
);
110 void RichStringPortion::convert( ScEditEngineDefaulter
& rEE
, ESelection
& rSelection
, const oox::xls::Font
* pFont
)
112 rSelection
.nStartPos
= rSelection
.nEndPos
;
113 rSelection
.nStartPara
= rSelection
.nEndPara
;
114 SfxItemSet
aItemSet( rEE
.GetEmptyItemSet() );
116 const Font
* pFontToUse
= mxFont
.get() ? mxFont
.get() : lclNeedsRichTextFormat( pFont
) ? pFont
: nullptr;
119 pFontToUse
->fillToItemSet( aItemSet
, true );
121 // #TODO need to manually adjust nEndPos ( and nEndPara ) to cater for any paragraphs
122 sal_Int32 nLastParaLoc
= -1;
123 sal_Int32 nSearchIndex
= maText
.indexOf( '\n' );
124 sal_Int32 nParaOccurence
= 0;
125 while ( nSearchIndex
!= -1 )
127 nLastParaLoc
= nSearchIndex
;
129 rSelection
.nEndPos
= 0;
130 nSearchIndex
= maText
.indexOf( '\n', nSearchIndex
+ 1);
133 rSelection
.nEndPara
+= nParaOccurence
;
134 if ( nLastParaLoc
!= -1 )
136 rSelection
.nEndPos
= maText
.getLength() - 1 - nLastParaLoc
;
140 rSelection
.nEndPos
= rSelection
.nStartPos
+ maText
.getLength();
142 rEE
.QuickSetAttribs( aItemSet
, rSelection
);
145 void RichStringPortion::writeFontProperties( const Reference
<XText
>& rxText
) const
147 PropertySet
aPropSet(rxText
);
150 mxFont
->writeToPropertySet(aPropSet
);
153 void FontPortionModel::read( SequenceInputStream
& rStrm
)
155 mnPos
= rStrm
.readuInt16();
156 mnFontId
= rStrm
.readuInt16();
159 void FontPortionModelList::appendPortion( const FontPortionModel
& rPortion
)
161 // #i33341# real life -- same character index may occur several times
162 OSL_ENSURE( mvModels
.empty() || (mvModels
.back().mnPos
<= rPortion
.mnPos
), "FontPortionModelList::appendPortion - wrong char order" );
163 if( mvModels
.empty() || (mvModels
.back().mnPos
< rPortion
.mnPos
) )
164 mvModels
.push_back( rPortion
);
166 mvModels
.back().mnFontId
= rPortion
.mnFontId
;
169 void FontPortionModelList::importPortions( SequenceInputStream
& rStrm
)
171 sal_Int32 nCount
= rStrm
.readInt32();
175 mvModels
.reserve( getLimitedValue
< size_t, sal_Int64
>( nCount
, 0, rStrm
.getRemaining() / 4 ) );
176 /* #i33341# real life -- same character index may occur several times
177 -> use appendPortion() to validate string position. */
178 FontPortionModel aPortion
;
179 for( sal_Int32 nIndex
= 0; !rStrm
.isEof() && (nIndex
< nCount
); ++nIndex
)
181 aPortion
.read( rStrm
);
182 appendPortion( aPortion
);
187 PhoneticDataModel::PhoneticDataModel() :
189 mnType( XML_fullwidthKatakana
),
190 mnAlignment( XML_left
)
194 void PhoneticDataModel::setBiffData( sal_Int32 nType
, sal_Int32 nAlignment
)
196 static const sal_Int32 spnTypeIds
[] = { XML_halfwidthKatakana
, XML_fullwidthKatakana
, XML_hiragana
, XML_noConversion
};
197 mnType
= STATIC_ARRAY_SELECT( spnTypeIds
, nType
, XML_fullwidthKatakana
);
199 static const sal_Int32 spnAlignments
[] = { XML_noControl
, XML_left
, XML_center
, XML_distributed
};
200 mnAlignment
= STATIC_ARRAY_SELECT( spnAlignments
, nAlignment
, XML_left
);
203 PhoneticSettings::PhoneticSettings( const WorkbookHelper
& rHelper
) :
204 WorkbookHelper( rHelper
)
208 void PhoneticSettings::importPhoneticPr( const AttributeList
& rAttribs
)
210 maModel
.mnFontId
= rAttribs
.getInteger( XML_fontId
, -1 );
211 maModel
.mnType
= rAttribs
.getToken( XML_type
, XML_fullwidthKatakana
);
212 maModel
.mnAlignment
= rAttribs
.getToken( XML_alignment
, XML_left
);
215 void PhoneticSettings::importPhoneticPr( SequenceInputStream
& rStrm
)
218 sal_Int32 nType
, nAlignment
;
219 nFontId
= rStrm
.readuInt16();
220 nType
= rStrm
.readInt32();
221 nAlignment
= rStrm
.readInt32();
222 maModel
.mnFontId
= nFontId
;
223 maModel
.setBiffData( nType
, nAlignment
);
226 void PhoneticSettings::importStringData( SequenceInputStream
& rStrm
)
228 sal_uInt16 nFontId
, nFlags
;
229 nFontId
= rStrm
.readuInt16();
230 nFlags
= rStrm
.readuInt16();
231 maModel
.mnFontId
= nFontId
;
232 maModel
.setBiffData( extractValue
< sal_Int32
>( nFlags
, 0, 2 ), extractValue
< sal_Int32
>( nFlags
, 2, 2 ) );
235 RichStringPhonetic::RichStringPhonetic( const WorkbookHelper
& rHelper
) :
236 WorkbookHelper( rHelper
),
242 void RichStringPhonetic::setText( const OUString
& rText
)
247 void RichStringPhonetic::importPhoneticRun( const AttributeList
& rAttribs
)
249 mnBasePos
= rAttribs
.getInteger( XML_sb
, -1 );
250 mnBaseEnd
= rAttribs
.getInteger( XML_eb
, -1 );
253 void RichStringPhonetic::setBaseRange( sal_Int32 nBasePos
, sal_Int32 nBaseEnd
)
255 mnBasePos
= nBasePos
;
256 mnBaseEnd
= nBaseEnd
;
259 void PhoneticPortionModel::read( SequenceInputStream
& rStrm
)
261 mnPos
= rStrm
.readuInt16();
262 mnBasePos
= rStrm
.readuInt16();
263 mnBaseLen
= rStrm
.readuInt16();
266 void PhoneticPortionModelList::appendPortion( const PhoneticPortionModel
& rPortion
)
268 // same character index may occur several times
269 OSL_ENSURE( mvModels
.empty() || ((mvModels
.back().mnPos
<= rPortion
.mnPos
) &&
270 (mvModels
.back().mnBasePos
+ mvModels
.back().mnBaseLen
<= rPortion
.mnBasePos
)),
271 "PhoneticPortionModelList::appendPortion - wrong char order" );
272 if( mvModels
.empty() || (mvModels
.back().mnPos
< rPortion
.mnPos
) )
274 mvModels
.push_back( rPortion
);
276 else if( mvModels
.back().mnPos
== rPortion
.mnPos
)
278 mvModels
.back().mnBasePos
= rPortion
.mnBasePos
;
279 mvModels
.back().mnBaseLen
= rPortion
.mnBaseLen
;
283 void PhoneticPortionModelList::importPortions( SequenceInputStream
& rStrm
)
285 sal_Int32 nCount
= rStrm
.readInt32();
289 mvModels
.reserve( getLimitedValue
< size_t, sal_Int64
>( nCount
, 0, rStrm
.getRemaining() / 6 ) );
290 PhoneticPortionModel aPortion
;
291 for( sal_Int32 nIndex
= 0; !rStrm
.isEof() && (nIndex
< nCount
); ++nIndex
)
293 aPortion
.read( rStrm
);
294 appendPortion( aPortion
);
299 RichString::RichString( const WorkbookHelper
& rHelper
) :
300 WorkbookHelper( rHelper
),
301 maPhonSettings( rHelper
)
305 RichStringPortionRef
RichString::importText()
307 return createPortion();
310 RichStringPortionRef
RichString::importRun()
312 return createPortion();
315 RichStringPhoneticRef
RichString::importPhoneticRun( const AttributeList
& rAttribs
)
317 RichStringPhoneticRef xPhonetic
= createPhonetic();
318 xPhonetic
->importPhoneticRun( rAttribs
);
322 void RichString::importPhoneticPr( const AttributeList
& rAttribs
)
324 maPhonSettings
.importPhoneticPr( rAttribs
);
327 void RichString::importString( SequenceInputStream
& rStrm
, bool bRich
)
329 sal_uInt8 nFlags
= bRich
? rStrm
.readuInt8() : 0;
330 OUString aBaseText
= BiffHelper::readString( rStrm
);
332 if( !rStrm
.isEof() && getFlag( nFlags
, BIFF12_STRINGFLAG_FONTS
) )
334 FontPortionModelList aPortions
;
335 aPortions
.importPortions( rStrm
);
336 createTextPortions( aBaseText
, aPortions
);
340 createPortion()->setText( aBaseText
);
343 if( !rStrm
.isEof() && getFlag( nFlags
, BIFF12_STRINGFLAG_PHONETICS
) )
345 OUString aPhoneticText
= BiffHelper::readString( rStrm
);
346 PhoneticPortionModelList aPortions
;
347 aPortions
.importPortions( rStrm
);
348 maPhonSettings
.importStringData( rStrm
);
349 createPhoneticPortions( aPhoneticText
, aPortions
, aBaseText
.getLength() );
353 void RichString::finalizeImport()
355 maTextPortions
.forEachMem( &RichStringPortion::finalizeImport
);
358 bool RichString::extractPlainString( OUString
& orString
, const oox::xls::Font
* pFirstPortionFont
) const
360 if( !maPhonPortions
.empty() )
362 if( maTextPortions
.empty() )
367 if( (maTextPortions
.size() == 1) && !maTextPortions
.front()->hasFont() && !lclNeedsRichTextFormat( pFirstPortionFont
) )
369 orString
= maTextPortions
.front()->getText();
370 return orString
.indexOf( '\x0A' ) < 0;
375 void RichString::convert( const Reference
< XText
>& rxText
) const
377 if (maTextPortions
.size() == 1)
379 // Set text directly to the cell when the string has only one portion.
380 // It's much faster this way.
381 RichStringPortion
& rPtn
= *maTextPortions
.front();
382 rxText
->setString(rPtn
.getText());
383 rPtn
.writeFontProperties(rxText
);
387 bool bReplaceOld
= true;
388 for( const auto& rxTextPortion
: maTextPortions
)
390 rxTextPortion
->convert( rxText
, bReplaceOld
);
391 bReplaceOld
= false; // do not replace first portion text with following portions
395 std::unique_ptr
<EditTextObject
> RichString::convert( ScEditEngineDefaulter
& rEE
, const oox::xls::Font
* pFirstPortionFont
) const
397 ESelection aSelection
;
399 OUStringBuffer sString
;
400 for( const auto& rxTextPortion
: maTextPortions
)
401 sString
.append(rxTextPortion
->getText());
403 // fdo#84370 - diving into editeng is not thread safe.
404 SolarMutexGuard aGuard
;
406 rEE
.SetText( sString
.makeStringAndClear() );
408 for( const auto& rxTextPortion
: maTextPortions
)
410 rxTextPortion
->convert( rEE
, aSelection
, pFirstPortionFont
);
411 pFirstPortionFont
= nullptr;
414 return rEE
.CreateTextObject();
417 // private --------------------------------------------------------------------
419 RichStringPortionRef
RichString::createPortion()
421 RichStringPortionRef
xPortion( new RichStringPortion( *this ) );
422 maTextPortions
.push_back( xPortion
);
426 RichStringPhoneticRef
RichString::createPhonetic()
428 RichStringPhoneticRef
xPhonetic( new RichStringPhonetic( *this ) );
429 maPhonPortions
.push_back( xPhonetic
);
433 void RichString::createTextPortions( const OUString
& rText
, FontPortionModelList
& rPortions
)
435 maTextPortions
.clear();
436 if( !rText
.isEmpty() )
438 sal_Int32 nStrLen
= rText
.getLength();
439 // add leading and trailing string position to ease the following loop
440 if( rPortions
.empty() || (rPortions
.front().mnPos
> 0) )
441 rPortions
.insert( rPortions
.begin(), FontPortionModel( 0 ) );
442 if( rPortions
.back().mnPos
< nStrLen
)
443 rPortions
.push_back( FontPortionModel( nStrLen
) );
445 // create all string portions according to the font id vector
446 for( ::std::vector
< FontPortionModel
>::const_iterator aIt
= rPortions
.begin(); aIt
->mnPos
< nStrLen
; ++aIt
)
448 sal_Int32 nPortionLen
= (aIt
+ 1)->mnPos
- aIt
->mnPos
;
449 if( (0 < nPortionLen
) && (aIt
->mnPos
+ nPortionLen
<= nStrLen
) )
451 RichStringPortionRef xPortion
= createPortion();
452 xPortion
->setText( rText
.copy( aIt
->mnPos
, nPortionLen
) );
453 xPortion
->setFontId( aIt
->mnFontId
);
459 void RichString::createPhoneticPortions( const OUString
& rText
, PhoneticPortionModelList
& rPortions
, sal_Int32 nBaseLen
)
461 maPhonPortions
.clear();
462 if( !rText
.isEmpty())
464 sal_Int32 nStrLen
= rText
.getLength();
465 // no portions - assign phonetic text to entire base text
466 if( rPortions
.empty() )
467 rPortions
.push_back( PhoneticPortionModel( 0, 0, nBaseLen
) );
468 // add trailing string position to ease the following loop
469 if( rPortions
.back().mnPos
< nStrLen
)
470 rPortions
.push_back( PhoneticPortionModel( nStrLen
, nBaseLen
, 0 ) );
472 // create all phonetic portions according to the portions vector
473 for( ::std::vector
< PhoneticPortionModel
>::const_iterator aIt
= rPortions
.begin(); aIt
->mnPos
< nStrLen
; ++aIt
)
475 sal_Int32 nPortionLen
= (aIt
+ 1)->mnPos
- aIt
->mnPos
;
476 if( (0 < nPortionLen
) && (aIt
->mnPos
+ nPortionLen
<= nStrLen
) )
478 RichStringPhoneticRef xPhonetic
= createPhonetic();
479 xPhonetic
->setText( rText
.copy( aIt
->mnPos
, nPortionLen
) );
480 xPhonetic
->setBaseRange( aIt
->mnBasePos
, aIt
->mnBasePos
+ aIt
->mnBaseLen
);
489 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */