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 1618 : inline bool lclNeedsRichTextFormat( const oox::xls::Font* pFont )
44 : {
45 1618 : return pFont && pFont->needsRichTextFormat();
46 : }
47 :
48 : } // namespace
49 :
50 1372 : RichStringPortion::RichStringPortion( const WorkbookHelper& rHelper ) :
51 : WorkbookHelper( rHelper ),
52 : mnFontId( -1 ),
53 1372 : mbConverted( false )
54 : {
55 1372 : }
56 :
57 1370 : void RichStringPortion::setText( const OUString& rText )
58 : {
59 1370 : maText = rText;
60 1370 : }
61 :
62 6 : FontRef RichStringPortion::createFont()
63 : {
64 6 : mxFont.reset( new Font( *this, false ) );
65 6 : return mxFont;
66 : }
67 :
68 0 : void RichStringPortion::setFontId( sal_Int32 nFontId )
69 : {
70 0 : mnFontId = nFontId;
71 0 : }
72 :
73 1368 : void RichStringPortion::finalizeImport()
74 : {
75 1368 : if( mxFont.get() )
76 4 : mxFont->finalizeImport();
77 1364 : else if( mnFontId >= 0 )
78 0 : mxFont = getStyles().getFont( mnFontId );
79 1368 : }
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 6 : void RichStringPortion::convert( ScEditEngineDefaulter& rEE, ESelection& rSelection, const oox::xls::Font* pFont )
116 : {
117 6 : rSelection.nStartPos = rSelection.nEndPos;
118 6 : rSelection.nStartPara = rSelection.nEndPara;
119 6 : SfxItemSet aItemSet( rEE.GetEmptyItemSet() );
120 :
121 6 : const Font* pFontToUse = mxFont.get() ? mxFont.get() : lclNeedsRichTextFormat( pFont ) ? pFont : NULL;
122 :
123 6 : if ( pFontToUse )
124 2 : pFontToUse->fillToItemSet( aItemSet, FONT_PROPTYPE_TEXT );
125 :
126 : // #TODO need to manually adjust nEndPos ( and nEndPara ) to cater for any paragraphs
127 6 : sal_Int32 nLastParaLoc = -1;
128 6 : sal_Int32 nSearchIndex = maText.indexOf( '\n' );
129 6 : sal_Int32 nParaOccurence = 0;
130 16 : while ( nSearchIndex != -1 )
131 : {
132 4 : nLastParaLoc = nSearchIndex;
133 4 : ++nParaOccurence;
134 4 : rSelection.nEndPos = 0;
135 4 : nSearchIndex = maText.indexOf( '\n', nSearchIndex + 1);
136 : }
137 :
138 6 : rSelection.nEndPara += nParaOccurence;
139 6 : if ( nLastParaLoc != -1 )
140 : {
141 2 : rSelection.nEndPos = maText.getLength() - 1 - nLastParaLoc;
142 : }
143 : else
144 : {
145 4 : rSelection.nEndPos = rSelection.nStartPos + maText.getLength();
146 : }
147 6 : rEE.QuickSetAttribs( aItemSet, rSelection );
148 6 : }
149 :
150 4 : void RichStringPortion::writeFontProperties( const Reference<XText>& rxText, const oox::xls::Font* pFont ) const
151 : {
152 4 : PropertySet aPropSet(rxText);
153 :
154 4 : if (mxFont.get())
155 4 : mxFont->writeToPropertySet(aPropSet, FONT_PROPTYPE_TEXT);
156 :
157 4 : if (lclNeedsRichTextFormat(pFont))
158 0 : pFont->writeToPropertySet(aPropSet, FONT_PROPTYPE_TEXT);
159 4 : }
160 :
161 0 : void FontPortionModel::read( SequenceInputStream& rStrm )
162 : {
163 0 : mnPos = rStrm.readuInt16();
164 0 : mnFontId = rStrm.readuInt16();
165 0 : }
166 :
167 0 : 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 0 : if( mvModels.empty() || (mvModels.back().mnPos < rPortion.mnPos) )
172 0 : mvModels.push_back( rPortion );
173 : else
174 0 : mvModels.back().mnFontId = rPortion.mnFontId;
175 0 : }
176 :
177 0 : void FontPortionModelList::importPortions( SequenceInputStream& rStrm )
178 : {
179 0 : sal_Int32 nCount = rStrm.readInt32();
180 0 : mvModels.clear();
181 0 : if( nCount > 0 )
182 : {
183 0 : 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 0 : FontPortionModel aPortion;
187 0 : for( sal_Int32 nIndex = 0; !rStrm.isEof() && (nIndex < nCount); ++nIndex )
188 : {
189 0 : aPortion.read( rStrm );
190 0 : appendPortion( aPortion );
191 : }
192 : }
193 0 : }
194 :
195 1632 : PhoneticDataModel::PhoneticDataModel() :
196 : mnFontId( -1 ),
197 : mnType( XML_fullwidthKatakana ),
198 1632 : mnAlignment( XML_left )
199 : {
200 1632 : }
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 1632 : PhoneticSettings::PhoneticSettings( const WorkbookHelper& rHelper ) :
212 1632 : WorkbookHelper( rHelper )
213 : {
214 1632 : }
215 :
216 24 : void PhoneticSettings::importPhoneticPr( const AttributeList& rAttribs )
217 : {
218 24 : maModel.mnFontId = rAttribs.getInteger( XML_fontId, -1 );
219 24 : maModel.mnType = rAttribs.getToken( XML_type, XML_fullwidthKatakana );
220 24 : maModel.mnAlignment = rAttribs.getToken( XML_alignment, XML_left );
221 24 : }
222 :
223 0 : void PhoneticSettings::importPhoneticPr( SequenceInputStream& rStrm )
224 : {
225 : sal_uInt16 nFontId;
226 : sal_Int32 nType, nAlignment;
227 0 : rStrm >> nFontId >> nType >> nAlignment;
228 0 : maModel.mnFontId = nFontId;
229 0 : maModel.setBiffData( nType, nAlignment );
230 0 : }
231 :
232 0 : void PhoneticSettings::importStringData( SequenceInputStream& rStrm )
233 : {
234 : sal_uInt16 nFontId, nFlags;
235 0 : rStrm >> nFontId >> nFlags;
236 0 : maModel.mnFontId = nFontId;
237 0 : maModel.setBiffData( extractValue< sal_Int32 >( nFlags, 0, 2 ), extractValue< sal_Int32 >( nFlags, 2, 2 ) );
238 0 : }
239 :
240 0 : RichStringPhonetic::RichStringPhonetic( const WorkbookHelper& rHelper ) :
241 : WorkbookHelper( rHelper ),
242 : mnBasePos( -1 ),
243 0 : mnBaseEnd( -1 )
244 : {
245 0 : }
246 :
247 0 : void RichStringPhonetic::setText( const OUString& rText )
248 : {
249 0 : maText = rText;
250 0 : }
251 :
252 0 : void RichStringPhonetic::importPhoneticRun( const AttributeList& rAttribs )
253 : {
254 0 : mnBasePos = rAttribs.getInteger( XML_sb, -1 );
255 0 : mnBaseEnd = rAttribs.getInteger( XML_eb, -1 );
256 0 : }
257 :
258 0 : void RichStringPhonetic::setBaseRange( sal_Int32 nBasePos, sal_Int32 nBaseEnd )
259 : {
260 0 : mnBasePos = nBasePos;
261 0 : mnBaseEnd = nBaseEnd;
262 0 : }
263 :
264 0 : void PhoneticPortionModel::read( SequenceInputStream& rStrm )
265 : {
266 0 : mnPos = rStrm.readuInt16();
267 0 : mnBasePos = rStrm.readuInt16();
268 0 : mnBaseLen = rStrm.readuInt16();
269 0 : }
270 :
271 0 : void PhoneticPortionModelList::appendPortion( const PhoneticPortionModel& rPortion )
272 : {
273 : // same character index may occur several times
274 : OSL_ENSURE( mvModels.empty() || ((mvModels.back().mnPos <= rPortion.mnPos) &&
275 : (mvModels.back().mnBasePos + mvModels.back().mnBaseLen <= rPortion.mnBasePos)),
276 : "PhoneticPortionModelList::appendPortion - wrong char order" );
277 0 : if( mvModels.empty() || (mvModels.back().mnPos < rPortion.mnPos) )
278 : {
279 0 : mvModels.push_back( rPortion );
280 : }
281 0 : else if( mvModels.back().mnPos == rPortion.mnPos )
282 : {
283 0 : mvModels.back().mnBasePos = rPortion.mnBasePos;
284 0 : mvModels.back().mnBaseLen = rPortion.mnBaseLen;
285 : }
286 0 : }
287 :
288 0 : void PhoneticPortionModelList::importPortions( SequenceInputStream& rStrm )
289 : {
290 0 : sal_Int32 nCount = rStrm.readInt32();
291 0 : mvModels.clear();
292 0 : if( nCount > 0 )
293 : {
294 0 : mvModels.reserve( getLimitedValue< size_t, sal_Int64 >( nCount, 0, rStrm.getRemaining() / 6 ) );
295 0 : PhoneticPortionModel aPortion;
296 0 : for( sal_Int32 nIndex = 0; !rStrm.isEof() && (nIndex < nCount); ++nIndex )
297 : {
298 0 : aPortion.read( rStrm );
299 0 : appendPortion( aPortion );
300 : }
301 : }
302 0 : }
303 :
304 1372 : RichString::RichString( const WorkbookHelper& rHelper ) :
305 : WorkbookHelper( rHelper ),
306 1372 : maPhonSettings( rHelper )
307 : {
308 1372 : }
309 :
310 1366 : RichStringPortionRef RichString::importText( const AttributeList& )
311 : {
312 1366 : return createPortion();
313 : }
314 :
315 6 : RichStringPortionRef RichString::importRun( const AttributeList& )
316 : {
317 6 : return createPortion();
318 : }
319 :
320 0 : RichStringPhoneticRef RichString::importPhoneticRun( const AttributeList& rAttribs )
321 : {
322 0 : RichStringPhoneticRef xPhonetic = createPhonetic();
323 0 : xPhonetic->importPhoneticRun( rAttribs );
324 0 : return xPhonetic;
325 : }
326 :
327 0 : void RichString::importPhoneticPr( const AttributeList& rAttribs )
328 : {
329 0 : maPhonSettings.importPhoneticPr( rAttribs );
330 0 : }
331 :
332 0 : void RichString::importString( SequenceInputStream& rStrm, bool bRich )
333 : {
334 0 : sal_uInt8 nFlags = bRich ? rStrm.readuInt8() : 0;
335 0 : OUString aBaseText = BiffHelper::readString( rStrm );
336 :
337 0 : if( !rStrm.isEof() && getFlag( nFlags, BIFF12_STRINGFLAG_FONTS ) )
338 : {
339 0 : FontPortionModelList aPortions;
340 0 : aPortions.importPortions( rStrm );
341 0 : createTextPortions( aBaseText, aPortions );
342 : }
343 : else
344 : {
345 0 : createPortion()->setText( aBaseText );
346 : }
347 :
348 0 : if( !rStrm.isEof() && getFlag( nFlags, BIFF12_STRINGFLAG_PHONETICS ) )
349 : {
350 0 : OUString aPhoneticText = BiffHelper::readString( rStrm );
351 0 : PhoneticPortionModelList aPortions;
352 0 : aPortions.importPortions( rStrm );
353 0 : maPhonSettings.importStringData( rStrm );
354 0 : createPhoneticPortions( aPhoneticText, aPortions, aBaseText.getLength() );
355 0 : }
356 0 : }
357 :
358 1368 : void RichString::finalizeImport()
359 : {
360 1368 : maTextPortions.forEachMem( &RichStringPortion::finalizeImport );
361 1368 : }
362 :
363 1610 : bool RichString::extractPlainString( OUString& orString, const oox::xls::Font* pFirstPortionFont ) const
364 : {
365 1610 : if( !maPhonPortions.empty() )
366 0 : return false;
367 1610 : if( maTextPortions.empty() )
368 : {
369 0 : orString = OUString();
370 0 : return true;
371 : }
372 1610 : if( (maTextPortions.size() == 1) && !maTextPortions.front()->hasFont() && !lclNeedsRichTextFormat( pFirstPortionFont ) )
373 : {
374 1610 : orString = maTextPortions.front()->getText();
375 1610 : return orString.indexOf( '\x0A' ) < 0;
376 : }
377 0 : return false;
378 : }
379 :
380 4 : void RichString::convert( const Reference< XText >& rxText, bool bReplaceOld, const oox::xls::Font* pFirstPortionFont ) const
381 : {
382 4 : if (maTextPortions.size() == 1)
383 : {
384 : // Set text directly to the cell when the string has only one portion.
385 : // It's much faster this way.
386 4 : RichStringPortion& rPtn = *maTextPortions.front();
387 4 : rxText->setString(rPtn.getText());
388 4 : rPtn.writeFontProperties(rxText, pFirstPortionFont);
389 8 : return;
390 : }
391 :
392 0 : for( PortionVector::const_iterator aIt = maTextPortions.begin(), aEnd = maTextPortions.end(); aIt != aEnd; ++aIt )
393 : {
394 0 : (*aIt)->convert( rxText, pFirstPortionFont, bReplaceOld );
395 0 : pFirstPortionFont = 0; // use passed font for first portion only
396 0 : bReplaceOld = false; // do not replace first portion text with following portions
397 : }
398 : }
399 :
400 6 : ::EditTextObject* RichString::convert( ScEditEngineDefaulter& rEE, const oox::xls::Font* pFirstPortionFont ) const
401 : {
402 6 : ESelection aSelection;
403 :
404 6 : OUString sString;
405 12 : for( PortionVector::const_iterator aIt = maTextPortions.begin(), aEnd = maTextPortions.end(); aIt != aEnd; ++aIt )
406 6 : sString += (*aIt)->getText();
407 :
408 : // fdo#84370 - diving into editeng is not thread safe.
409 12 : SolarMutexGuard aGuard;
410 :
411 6 : rEE.SetText( sString );
412 :
413 12 : for( PortionVector::const_iterator aIt = maTextPortions.begin(), aEnd = maTextPortions.end(); aIt != aEnd; ++aIt )
414 : {
415 6 : (*aIt)->convert( rEE, aSelection, pFirstPortionFont );
416 6 : pFirstPortionFont = 0;
417 : }
418 :
419 12 : return rEE.CreateTextObject();
420 : }
421 :
422 : // private --------------------------------------------------------------------
423 :
424 1372 : RichStringPortionRef RichString::createPortion()
425 : {
426 1372 : RichStringPortionRef xPortion( new RichStringPortion( *this ) );
427 1372 : maTextPortions.push_back( xPortion );
428 1372 : return xPortion;
429 : }
430 :
431 0 : RichStringPhoneticRef RichString::createPhonetic()
432 : {
433 0 : RichStringPhoneticRef xPhonetic( new RichStringPhonetic( *this ) );
434 0 : maPhonPortions.push_back( xPhonetic );
435 0 : return xPhonetic;
436 : }
437 :
438 0 : void RichString::createTextPortions( const OUString& rText, FontPortionModelList& rPortions )
439 : {
440 0 : maTextPortions.clear();
441 0 : if( !rText.isEmpty() )
442 : {
443 0 : sal_Int32 nStrLen = rText.getLength();
444 : // add leading and trailing string position to ease the following loop
445 0 : if( rPortions.empty() || (rPortions.front().mnPos > 0) )
446 0 : rPortions.insert( rPortions.begin(), FontPortionModel( 0, -1 ) );
447 0 : if( rPortions.back().mnPos < nStrLen )
448 0 : rPortions.push_back( FontPortionModel( nStrLen, -1 ) );
449 :
450 : // create all string portions according to the font id vector
451 0 : for( ::std::vector< FontPortionModel >::const_iterator aIt = rPortions.begin(); aIt->mnPos < nStrLen; ++aIt )
452 : {
453 0 : sal_Int32 nPortionLen = (aIt + 1)->mnPos - aIt->mnPos;
454 0 : if( (0 < nPortionLen) && (aIt->mnPos + nPortionLen <= nStrLen) )
455 : {
456 0 : RichStringPortionRef xPortion = createPortion();
457 0 : xPortion->setText( rText.copy( aIt->mnPos, nPortionLen ) );
458 0 : xPortion->setFontId( aIt->mnFontId );
459 : }
460 : }
461 : }
462 0 : }
463 :
464 0 : void RichString::createPhoneticPortions( const OUString& rText, PhoneticPortionModelList& rPortions, sal_Int32 nBaseLen )
465 : {
466 0 : maPhonPortions.clear();
467 0 : if( !rText.isEmpty())
468 : {
469 0 : sal_Int32 nStrLen = rText.getLength();
470 : // no portions - assign phonetic text to entire base text
471 0 : if( rPortions.empty() )
472 0 : rPortions.push_back( PhoneticPortionModel( 0, 0, nBaseLen ) );
473 : // add trailing string position to ease the following loop
474 0 : if( rPortions.back().mnPos < nStrLen )
475 0 : rPortions.push_back( PhoneticPortionModel( nStrLen, nBaseLen, 0 ) );
476 :
477 : // create all phonetic portions according to the portions vector
478 0 : for( ::std::vector< PhoneticPortionModel >::const_iterator aIt = rPortions.begin(); aIt->mnPos < nStrLen; ++aIt )
479 : {
480 0 : sal_Int32 nPortionLen = (aIt + 1)->mnPos - aIt->mnPos;
481 0 : if( (0 < nPortionLen) && (aIt->mnPos + nPortionLen <= nStrLen) )
482 : {
483 0 : RichStringPhoneticRef xPhonetic = createPhonetic();
484 0 : xPhonetic->setText( rText.copy( aIt->mnPos, nPortionLen ) );
485 0 : xPhonetic->setBaseRange( aIt->mnBasePos, aIt->mnBasePos + aIt->mnBaseLen );
486 : }
487 : }
488 : }
489 0 : }
490 :
491 : } // namespace xls
492 48 : } // namespace oox
493 :
494 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|