LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sc/source/filter/oox - richstring.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 60 245 24.5 %
Date: 2013-07-09 Functions: 18 41 43.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10