Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "richstring.hxx"
21 :
22 : #include <com/sun/star/text/XText.hpp>
23 : #include <rtl/ustrbuf.hxx>
24 : #include <editeng/editobj.hxx>
25 : #include <oox/helper/attributelist.hxx>
26 : #include <oox/helper/propertyset.hxx>
27 : #include "biffinputstream.hxx"
28 : #include "editutil.hxx"
29 :
30 : #include <vcl/svapp.hxx>
31 :
32 : namespace oox {
33 : namespace xls {
34 :
35 : using namespace ::com::sun::star::text;
36 : using namespace ::com::sun::star::uno;
37 :
38 : namespace {
39 :
40 : const sal_uInt8 BIFF12_STRINGFLAG_FONTS = 0x01;
41 : const sal_uInt8 BIFF12_STRINGFLAG_PHONETICS = 0x02;
42 :
43 931 : inline bool lclNeedsRichTextFormat( const oox::xls::Font* pFont )
44 : {
45 931 : return pFont && pFont->needsRichTextFormat();
46 : }
47 :
48 : } // namespace
49 :
50 816 : RichStringPortion::RichStringPortion( const WorkbookHelper& rHelper ) :
51 : WorkbookHelper( rHelper ),
52 : mnFontId( -1 ),
53 816 : mbConverted( false )
54 : {
55 816 : }
56 :
57 815 : void RichStringPortion::setText( const OUString& rText )
58 : {
59 815 : maText = rText;
60 815 : }
61 :
62 11 : FontRef RichStringPortion::createFont()
63 : {
64 11 : mxFont.reset( new Font( *this, false ) );
65 11 : return mxFont;
66 : }
67 :
68 1 : void RichStringPortion::setFontId( sal_Int32 nFontId )
69 : {
70 1 : mnFontId = nFontId;
71 1 : }
72 :
73 814 : void RichStringPortion::finalizeImport()
74 : {
75 814 : if( mxFont.get() )
76 10 : mxFont->finalizeImport();
77 804 : else if( mnFontId >= 0 )
78 1 : mxFont = getStyles().getFont( mnFontId );
79 814 : }
80 :
81 0 : void RichStringPortion::convert( const Reference< XText >& rxText, const oox::xls::Font* pFont, bool bReplace )
82 : {
83 0 : if ( mbConverted )
84 0 : return;
85 :
86 0 : Reference< XTextRange > xRange;
87 0 : if( bReplace )
88 0 : xRange.set( rxText, UNO_QUERY );
89 : else
90 0 : xRange = rxText->getEnd();
91 : OSL_ENSURE( xRange.is(), "RichStringPortion::convert - cannot get text range interface" );
92 :
93 0 : if( xRange.is() )
94 : {
95 0 : xRange->setString( maText );
96 0 : if( mxFont.get() )
97 : {
98 0 : PropertySet aPropSet( xRange );
99 0 : mxFont->writeToPropertySet( aPropSet, FONT_PROPTYPE_TEXT );
100 : }
101 :
102 : /* Some font attributes cannot be set to cell formatting in Calc but
103 : require to use rich formatting, e.g. font escapement. But do not
104 : use the passed font if this portion has its own font. */
105 0 : else if( lclNeedsRichTextFormat( pFont ) )
106 : {
107 0 : PropertySet aPropSet( xRange );
108 0 : pFont->writeToPropertySet( aPropSet, FONT_PROPTYPE_TEXT );
109 : }
110 : }
111 :
112 0 : mbConverted = true;
113 : }
114 :
115 11 : void RichStringPortion::convert( ScEditEngineDefaulter& rEE, ESelection& rSelection, const oox::xls::Font* pFont )
116 : {
117 11 : rSelection.nStartPos = rSelection.nEndPos;
118 11 : rSelection.nStartPara = rSelection.nEndPara;
119 11 : SfxItemSet aItemSet( rEE.GetEmptyItemSet() );
120 :
121 11 : const Font* pFontToUse = mxFont.get() ? mxFont.get() : lclNeedsRichTextFormat( pFont ) ? pFont : NULL;
122 :
123 11 : if ( pFontToUse )
124 9 : pFontToUse->fillToItemSet( aItemSet, true );
125 :
126 : // #TODO need to manually adjust nEndPos ( and nEndPara ) to cater for any paragraphs
127 11 : sal_Int32 nLastParaLoc = -1;
128 11 : sal_Int32 nSearchIndex = maText.indexOf( '\n' );
129 11 : sal_Int32 nParaOccurence = 0;
130 24 : while ( nSearchIndex != -1 )
131 : {
132 2 : nLastParaLoc = nSearchIndex;
133 2 : ++nParaOccurence;
134 2 : rSelection.nEndPos = 0;
135 2 : nSearchIndex = maText.indexOf( '\n', nSearchIndex + 1);
136 : }
137 :
138 11 : rSelection.nEndPara += nParaOccurence;
139 11 : if ( nLastParaLoc != -1 )
140 : {
141 1 : rSelection.nEndPos = maText.getLength() - 1 - nLastParaLoc;
142 : }
143 : else
144 : {
145 10 : rSelection.nEndPos = rSelection.nStartPos + maText.getLength();
146 : }
147 11 : rEE.QuickSetAttribs( aItemSet, rSelection );
148 11 : }
149 :
150 3 : void RichStringPortion::writeFontProperties( const Reference<XText>& rxText, const oox::xls::Font* pFont ) const
151 : {
152 3 : PropertySet aPropSet(rxText);
153 :
154 3 : if (mxFont.get())
155 3 : mxFont->writeToPropertySet(aPropSet, FONT_PROPTYPE_TEXT);
156 :
157 3 : if (lclNeedsRichTextFormat(pFont))
158 0 : pFont->writeToPropertySet(aPropSet, FONT_PROPTYPE_TEXT);
159 3 : }
160 :
161 1 : void FontPortionModel::read( SequenceInputStream& rStrm )
162 : {
163 1 : mnPos = rStrm.readuInt16();
164 1 : mnFontId = rStrm.readuInt16();
165 1 : }
166 :
167 1 : void FontPortionModelList::appendPortion( const FontPortionModel& rPortion )
168 : {
169 : // #i33341# real life -- same character index may occur several times
170 : OSL_ENSURE( mvModels.empty() || (mvModels.back().mnPos <= rPortion.mnPos), "FontPortionModelList::appendPortion - wrong char order" );
171 1 : if( mvModels.empty() || (mvModels.back().mnPos < rPortion.mnPos) )
172 1 : mvModels.push_back( rPortion );
173 : else
174 0 : mvModels.back().mnFontId = rPortion.mnFontId;
175 1 : }
176 :
177 1 : void FontPortionModelList::importPortions( SequenceInputStream& rStrm )
178 : {
179 1 : sal_Int32 nCount = rStrm.readInt32();
180 1 : mvModels.clear();
181 1 : if( nCount > 0 )
182 : {
183 1 : mvModels.reserve( getLimitedValue< size_t, sal_Int64 >( nCount, 0, rStrm.getRemaining() / 4 ) );
184 : /* #i33341# real life -- same character index may occur several times
185 : -> use appendPortion() to validate string position. */
186 1 : FontPortionModel aPortion;
187 2 : for( sal_Int32 nIndex = 0; !rStrm.isEof() && (nIndex < nCount); ++nIndex )
188 : {
189 1 : aPortion.read( rStrm );
190 1 : appendPortion( aPortion );
191 : }
192 : }
193 1 : }
194 :
195 1080 : PhoneticDataModel::PhoneticDataModel() :
196 : mnFontId( -1 ),
197 : mnType( XML_fullwidthKatakana ),
198 1080 : mnAlignment( XML_left )
199 : {
200 1080 : }
201 :
202 0 : void PhoneticDataModel::setBiffData( sal_Int32 nType, sal_Int32 nAlignment )
203 : {
204 : static const sal_Int32 spnTypeIds[] = { XML_halfwidthKatakana, XML_fullwidthKatakana, XML_hiragana, XML_noConversion };
205 0 : mnType = STATIC_ARRAY_SELECT( spnTypeIds, nType, XML_fullwidthKatakana );
206 :
207 : static const sal_Int32 spnAlignments[] = { XML_noControl, XML_left, XML_center, XML_distributed };
208 0 : mnAlignment = STATIC_ARRAY_SELECT( spnAlignments, nAlignment, XML_left );
209 0 : }
210 :
211 1080 : PhoneticSettings::PhoneticSettings( const WorkbookHelper& rHelper ) :
212 1080 : WorkbookHelper( rHelper )
213 : {
214 1080 : }
215 :
216 12 : void PhoneticSettings::importPhoneticPr( const AttributeList& rAttribs )
217 : {
218 12 : maModel.mnFontId = rAttribs.getInteger( XML_fontId, -1 );
219 12 : maModel.mnType = rAttribs.getToken( XML_type, XML_fullwidthKatakana );
220 12 : maModel.mnAlignment = rAttribs.getToken( XML_alignment, XML_left );
221 12 : }
222 :
223 0 : void PhoneticSettings::importPhoneticPr( SequenceInputStream& rStrm )
224 : {
225 : sal_uInt16 nFontId;
226 : sal_Int32 nType, nAlignment;
227 0 : nFontId = rStrm.readuInt16();
228 0 : nType = rStrm.readInt32();
229 0 : nAlignment = rStrm.readInt32();
230 0 : maModel.mnFontId = nFontId;
231 0 : maModel.setBiffData( nType, nAlignment );
232 0 : }
233 :
234 0 : void PhoneticSettings::importStringData( SequenceInputStream& rStrm )
235 : {
236 : sal_uInt16 nFontId, nFlags;
237 0 : nFontId = rStrm.readuInt16();
238 0 : nFlags = rStrm.readuInt16();
239 0 : maModel.mnFontId = nFontId;
240 0 : maModel.setBiffData( extractValue< sal_Int32 >( nFlags, 0, 2 ), extractValue< sal_Int32 >( nFlags, 2, 2 ) );
241 0 : }
242 :
243 0 : RichStringPhonetic::RichStringPhonetic( const WorkbookHelper& rHelper ) :
244 : WorkbookHelper( rHelper ),
245 : mnBasePos( -1 ),
246 0 : mnBaseEnd( -1 )
247 : {
248 0 : }
249 :
250 0 : void RichStringPhonetic::setText( const OUString& rText )
251 : {
252 0 : maText = rText;
253 0 : }
254 :
255 0 : void RichStringPhonetic::importPhoneticRun( const AttributeList& rAttribs )
256 : {
257 0 : mnBasePos = rAttribs.getInteger( XML_sb, -1 );
258 0 : mnBaseEnd = rAttribs.getInteger( XML_eb, -1 );
259 0 : }
260 :
261 0 : void RichStringPhonetic::setBaseRange( sal_Int32 nBasePos, sal_Int32 nBaseEnd )
262 : {
263 0 : mnBasePos = nBasePos;
264 0 : mnBaseEnd = nBaseEnd;
265 0 : }
266 :
267 0 : void PhoneticPortionModel::read( SequenceInputStream& rStrm )
268 : {
269 0 : mnPos = rStrm.readuInt16();
270 0 : mnBasePos = rStrm.readuInt16();
271 0 : mnBaseLen = rStrm.readuInt16();
272 0 : }
273 :
274 0 : void PhoneticPortionModelList::appendPortion( const PhoneticPortionModel& rPortion )
275 : {
276 : // same character index may occur several times
277 : OSL_ENSURE( mvModels.empty() || ((mvModels.back().mnPos <= rPortion.mnPos) &&
278 : (mvModels.back().mnBasePos + mvModels.back().mnBaseLen <= rPortion.mnBasePos)),
279 : "PhoneticPortionModelList::appendPortion - wrong char order" );
280 0 : if( mvModels.empty() || (mvModels.back().mnPos < rPortion.mnPos) )
281 : {
282 0 : mvModels.push_back( rPortion );
283 : }
284 0 : else if( mvModels.back().mnPos == rPortion.mnPos )
285 : {
286 0 : mvModels.back().mnBasePos = rPortion.mnBasePos;
287 0 : mvModels.back().mnBaseLen = rPortion.mnBaseLen;
288 : }
289 0 : }
290 :
291 0 : void PhoneticPortionModelList::importPortions( SequenceInputStream& rStrm )
292 : {
293 0 : sal_Int32 nCount = rStrm.readInt32();
294 0 : mvModels.clear();
295 0 : if( nCount > 0 )
296 : {
297 0 : mvModels.reserve( getLimitedValue< size_t, sal_Int64 >( nCount, 0, rStrm.getRemaining() / 6 ) );
298 0 : PhoneticPortionModel aPortion;
299 0 : for( sal_Int32 nIndex = 0; !rStrm.isEof() && (nIndex < nCount); ++nIndex )
300 : {
301 0 : aPortion.read( rStrm );
302 0 : appendPortion( aPortion );
303 : }
304 : }
305 0 : }
306 :
307 811 : RichString::RichString( const WorkbookHelper& rHelper ) :
308 : WorkbookHelper( rHelper ),
309 811 : maPhonSettings( rHelper )
310 : {
311 811 : }
312 :
313 802 : RichStringPortionRef RichString::importText( const AttributeList& )
314 : {
315 802 : return createPortion();
316 : }
317 :
318 11 : RichStringPortionRef RichString::importRun( const AttributeList& )
319 : {
320 11 : return createPortion();
321 : }
322 :
323 0 : RichStringPhoneticRef RichString::importPhoneticRun( const AttributeList& rAttribs )
324 : {
325 0 : RichStringPhoneticRef xPhonetic = createPhonetic();
326 0 : xPhonetic->importPhoneticRun( rAttribs );
327 0 : return xPhonetic;
328 : }
329 :
330 0 : void RichString::importPhoneticPr( const AttributeList& rAttribs )
331 : {
332 0 : maPhonSettings.importPhoneticPr( rAttribs );
333 0 : }
334 :
335 3 : void RichString::importString( SequenceInputStream& rStrm, bool bRich )
336 : {
337 3 : sal_uInt8 nFlags = bRich ? rStrm.readuInt8() : 0;
338 3 : OUString aBaseText = BiffHelper::readString( rStrm );
339 :
340 3 : if( !rStrm.isEof() && getFlag( nFlags, BIFF12_STRINGFLAG_FONTS ) )
341 : {
342 1 : FontPortionModelList aPortions;
343 1 : aPortions.importPortions( rStrm );
344 1 : createTextPortions( aBaseText, aPortions );
345 : }
346 : else
347 : {
348 2 : createPortion()->setText( aBaseText );
349 : }
350 :
351 3 : if( !rStrm.isEof() && getFlag( nFlags, BIFF12_STRINGFLAG_PHONETICS ) )
352 : {
353 0 : OUString aPhoneticText = BiffHelper::readString( rStrm );
354 0 : PhoneticPortionModelList aPortions;
355 0 : aPortions.importPortions( rStrm );
356 0 : maPhonSettings.importStringData( rStrm );
357 0 : createPhoneticPortions( aPhoneticText, aPortions, aBaseText.getLength() );
358 3 : }
359 3 : }
360 :
361 809 : void RichString::finalizeImport()
362 : {
363 809 : maTextPortions.forEachMem( &RichStringPortion::finalizeImport );
364 809 : }
365 :
366 929 : bool RichString::extractPlainString( OUString& orString, const oox::xls::Font* pFirstPortionFont ) const
367 : {
368 929 : if( !maPhonPortions.empty() )
369 0 : return false;
370 929 : if( maTextPortions.empty() )
371 : {
372 0 : orString.clear();
373 0 : return true;
374 : }
375 929 : if( (maTextPortions.size() == 1) && !maTextPortions.front()->hasFont() && !lclNeedsRichTextFormat( pFirstPortionFont ) )
376 : {
377 926 : orString = maTextPortions.front()->getText();
378 926 : return orString.indexOf( '\x0A' ) < 0;
379 : }
380 3 : return false;
381 : }
382 :
383 3 : void RichString::convert( const Reference< XText >& rxText, bool bReplaceOld, const oox::xls::Font* pFirstPortionFont ) const
384 : {
385 3 : if (maTextPortions.size() == 1)
386 : {
387 : // Set text directly to the cell when the string has only one portion.
388 : // It's much faster this way.
389 3 : RichStringPortion& rPtn = *maTextPortions.front();
390 3 : rxText->setString(rPtn.getText());
391 3 : rPtn.writeFontProperties(rxText, pFirstPortionFont);
392 6 : return;
393 : }
394 :
395 0 : for( PortionVector::const_iterator aIt = maTextPortions.begin(), aEnd = maTextPortions.end(); aIt != aEnd; ++aIt )
396 : {
397 0 : (*aIt)->convert( rxText, pFirstPortionFont, bReplaceOld );
398 0 : pFirstPortionFont = 0; // use passed font for first portion only
399 0 : bReplaceOld = false; // do not replace first portion text with following portions
400 : }
401 : }
402 :
403 6 : ::EditTextObject* RichString::convert( ScEditEngineDefaulter& rEE, const oox::xls::Font* pFirstPortionFont ) const
404 : {
405 6 : ESelection aSelection;
406 :
407 6 : OUString sString;
408 17 : for( PortionVector::const_iterator aIt = maTextPortions.begin(), aEnd = maTextPortions.end(); aIt != aEnd; ++aIt )
409 11 : sString += (*aIt)->getText();
410 :
411 : // fdo#84370 - diving into editeng is not thread safe.
412 12 : SolarMutexGuard aGuard;
413 :
414 6 : rEE.SetText( sString );
415 :
416 17 : for( PortionVector::const_iterator aIt = maTextPortions.begin(), aEnd = maTextPortions.end(); aIt != aEnd; ++aIt )
417 : {
418 11 : (*aIt)->convert( rEE, aSelection, pFirstPortionFont );
419 11 : pFirstPortionFont = 0;
420 : }
421 :
422 12 : return rEE.CreateTextObject();
423 : }
424 :
425 : // private --------------------------------------------------------------------
426 :
427 816 : RichStringPortionRef RichString::createPortion()
428 : {
429 816 : RichStringPortionRef xPortion( new RichStringPortion( *this ) );
430 816 : maTextPortions.push_back( xPortion );
431 816 : return xPortion;
432 : }
433 :
434 0 : RichStringPhoneticRef RichString::createPhonetic()
435 : {
436 0 : RichStringPhoneticRef xPhonetic( new RichStringPhonetic( *this ) );
437 0 : maPhonPortions.push_back( xPhonetic );
438 0 : return xPhonetic;
439 : }
440 :
441 1 : void RichString::createTextPortions( const OUString& rText, FontPortionModelList& rPortions )
442 : {
443 1 : maTextPortions.clear();
444 1 : if( !rText.isEmpty() )
445 : {
446 1 : sal_Int32 nStrLen = rText.getLength();
447 : // add leading and trailing string position to ease the following loop
448 1 : if( rPortions.empty() || (rPortions.front().mnPos > 0) )
449 0 : rPortions.insert( rPortions.begin(), FontPortionModel( 0, -1 ) );
450 1 : if( rPortions.back().mnPos < nStrLen )
451 1 : rPortions.push_back( FontPortionModel( nStrLen, -1 ) );
452 :
453 : // create all string portions according to the font id vector
454 2 : for( ::std::vector< FontPortionModel >::const_iterator aIt = rPortions.begin(); aIt->mnPos < nStrLen; ++aIt )
455 : {
456 1 : sal_Int32 nPortionLen = (aIt + 1)->mnPos - aIt->mnPos;
457 1 : if( (0 < nPortionLen) && (aIt->mnPos + nPortionLen <= nStrLen) )
458 : {
459 1 : RichStringPortionRef xPortion = createPortion();
460 1 : xPortion->setText( rText.copy( aIt->mnPos, nPortionLen ) );
461 1 : xPortion->setFontId( aIt->mnFontId );
462 : }
463 : }
464 : }
465 1 : }
466 :
467 0 : void RichString::createPhoneticPortions( const OUString& rText, PhoneticPortionModelList& rPortions, sal_Int32 nBaseLen )
468 : {
469 0 : maPhonPortions.clear();
470 0 : if( !rText.isEmpty())
471 : {
472 0 : sal_Int32 nStrLen = rText.getLength();
473 : // no portions - assign phonetic text to entire base text
474 0 : if( rPortions.empty() )
475 0 : rPortions.push_back( PhoneticPortionModel( 0, 0, nBaseLen ) );
476 : // add trailing string position to ease the following loop
477 0 : if( rPortions.back().mnPos < nStrLen )
478 0 : rPortions.push_back( PhoneticPortionModel( nStrLen, nBaseLen, 0 ) );
479 :
480 : // create all phonetic portions according to the portions vector
481 0 : for( ::std::vector< PhoneticPortionModel >::const_iterator aIt = rPortions.begin(); aIt->mnPos < nStrLen; ++aIt )
482 : {
483 0 : sal_Int32 nPortionLen = (aIt + 1)->mnPos - aIt->mnPos;
484 0 : if( (0 < nPortionLen) && (aIt->mnPos + nPortionLen <= nStrLen) )
485 : {
486 0 : RichStringPhoneticRef xPhonetic = createPhonetic();
487 0 : xPhonetic->setText( rText.copy( aIt->mnPos, nPortionLen ) );
488 0 : xPhonetic->setBaseRange( aIt->mnBasePos, aIt->mnBasePos + aIt->mnBaseLen );
489 : }
490 : }
491 : }
492 0 : }
493 :
494 : } // namespace xls
495 30 : } // namespace oox
496 :
497 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|