LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/oox/source/drawingml - customshapeproperties.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 160 202 79.2 %
Date: 2013-07-09 Functions: 13 14 92.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 "oox/drawingml/customshapeproperties.hxx"
      21             : #include "oox/helper/helper.hxx"
      22             : #include "oox/helper/propertymap.hxx"
      23             : #include "oox/helper/propertyset.hxx"
      24             : #include "oox/token/tokenmap.hxx"
      25             : #include <com/sun/star/awt/Rectangle.hpp>
      26             : #include <com/sun/star/awt/Size.hpp>
      27             : #include <com/sun/star/beans/XMultiPropertySet.hpp>
      28             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      29             : #include <com/sun/star/graphic/XGraphicTransformer.hpp>
      30             : #include <com/sun/star/drawing/XShape.hpp>
      31             : #include <com/sun/star/drawing/XEnhancedCustomShapeDefaulter.hpp>
      32             : #include <com/sun/star/drawing/EnhancedCustomShapeTextFrame.hpp>
      33             : 
      34             : using namespace ::oox::core;
      35             : using namespace ::com::sun::star;
      36             : using namespace ::com::sun::star::uno;
      37             : using namespace ::com::sun::star::beans;
      38             : using namespace ::com::sun::star::graphic;
      39             : using namespace ::com::sun::star::drawing;
      40             : 
      41             : # define USS(x) OUStringToOString( x, RTL_TEXTENCODING_UTF8 ).getStr()
      42             : 
      43             : namespace oox { namespace drawingml {
      44             : 
      45         549 : CustomShapeProperties::CustomShapeProperties()
      46             : : mnShapePresetType ( -1 )
      47             : , mbMirroredX   ( sal_False )
      48             : , mbMirroredY   ( sal_False )
      49             : , mnTextRotateAngle ( 0 )
      50         549 : , mnArcNum ( 0 )
      51             : {
      52         549 : }
      53        1164 : CustomShapeProperties::~CustomShapeProperties()
      54             : {
      55        1164 : }
      56             : 
      57           0 : OUString CustomShapeProperties::getShapePresetTypeName() const
      58             : {
      59           0 :     return StaticTokenMap::get().getUnicodeTokenName( mnShapePresetType );
      60             : }
      61             : 
      62           6 : sal_Int32 CustomShapeProperties::SetCustomShapeGuideValue( std::vector< CustomShapeGuide >& rGuideList, const CustomShapeGuide& rGuide )
      63             : {
      64           6 :     sal_uInt32 nIndex = 0;
      65         317 :     for( ; nIndex < rGuideList.size(); nIndex++ )
      66             :     {
      67         311 :         if ( rGuideList[ nIndex ].maName == rGuide.maName )
      68           0 :             break;
      69             :     }
      70           6 :     if ( nIndex == rGuideList.size() )
      71           6 :         rGuideList.push_back( rGuide );
      72           6 :     return static_cast< sal_Int32 >( nIndex );
      73             : }
      74             : 
      75             : // returns the index into the guidelist for a given formula name,
      76             : // if the return value is < 0 then the guide value could not be found
      77          68 : sal_Int32 CustomShapeProperties::GetCustomShapeGuideValue( const std::vector< CustomShapeGuide >& rGuideList, const OUString& rFormulaName )
      78             : {
      79             :     // traverse the list from the end, because guide names can be reused
      80             :     // and current is the last one
      81             :     // see a1 guide in gear6 custom shape preset as example
      82          68 :     sal_Int32 nIndex = static_cast< sal_Int32 >( rGuideList.size() ) - 1;
      83         245 :     for( ; nIndex >= 0; nIndex-- )
      84             :     {
      85         211 :         if ( rGuideList[ nIndex ].maName == rFormulaName )
      86          34 :             break;
      87             :     }
      88             : 
      89          68 :     return nIndex;
      90             : }
      91             : 
      92          47 : CustomShapeProperties::PresetsMap CustomShapeProperties::maPresetsMap;
      93             : 
      94          68 : static OUString GetConnectorShapeType( sal_Int32 nType )
      95             : {
      96             :     OSL_TRACE("GetConnectorShapeType preset: %d %d", nType, XML_straightConnector1);
      97             : 
      98          68 :     OUString sType;
      99          68 :     switch( nType )
     100             :     {
     101             :         case XML_straightConnector1:
     102           4 :             sType = "mso-spt32";
     103           4 :             break;
     104             :         default:
     105          64 :             break;
     106             :     }
     107          68 :     return sType;
     108             : }
     109             : 
     110             : 
     111          71 : void CustomShapeProperties::pushToPropSet( const ::oox::core::FilterBase& /* rFilterBase */,
     112             :     const Reference < XPropertySet >& xPropSet, const Reference < XShape > & xShape )
     113             : {
     114          71 :     if ( mnShapePresetType >= 0 )
     115             :     {
     116             :         OSL_TRACE("preset: %d", mnShapePresetType);
     117             : 
     118          68 :         if (maPresetsMap.empty())
     119           2 :             initializePresetsMap();
     120             : 
     121          68 :         PropertyMap aPropertyMap;
     122         136 :         PropertySet aPropSet( xPropSet );
     123             : 
     124         136 :         OUString sConnectorShapeType = GetConnectorShapeType( mnShapePresetType );
     125             : 
     126          68 :         if (sConnectorShapeType.getLength() > 0)
     127             :         {
     128             :             OSL_TRACE("connector shape: %s (%d)", USS(sConnectorShapeType), mnShapePresetType);
     129             :             //const uno::Reference < drawing::XShape > xShape( xPropSet, UNO_QUERY );
     130           4 :             Reference< drawing::XEnhancedCustomShapeDefaulter > xDefaulter( xShape, UNO_QUERY );
     131           4 :             if( xDefaulter.is() ) {
     132           4 :                 xDefaulter->createCustomShapeDefaults( sConnectorShapeType );
     133           4 :                 aPropertyMap[ PROP_Type ] <<= Any( sConnectorShapeType );
     134           4 :             }
     135             :         }
     136          64 :         else if (maPresetsMap.find(mnShapePresetType) != maPresetsMap.end())
     137             :         {
     138             :             OSL_TRACE("found property map for preset: %s (%d)", USS(getShapePresetTypeName()), mnShapePresetType);
     139             : 
     140          64 :             CustomShapeProvider *pProvider = maPresetsMap[ mnShapePresetType ];
     141          64 :             if (pProvider)
     142          64 :                 aPropertyMap = pProvider->getProperties();
     143             : #ifdef DEBUG
     144             :             aPropertyMap.dumpCode();
     145             : #endif
     146             :         }
     147             : 
     148          68 :         aPropertyMap[ PROP_MirroredX ] <<= Any( mbMirroredX );
     149          68 :         aPropertyMap[ PROP_MirroredY ] <<= Any( mbMirroredY );
     150          68 :         aPropertyMap[ PROP_TextPreRotateAngle ] <<= Any( mnTextRotateAngle );
     151          68 :         aPropertyMap[ PROP_IsPostRotateAngle ] <<= true; // For OpenXML Imports
     152         136 :         Sequence< PropertyValue > aSeq = aPropertyMap.makePropertyValueSequence();
     153          68 :         aPropSet.setProperty( PROP_CustomShapeGeometry, aSeq );
     154             : 
     155          68 :         if ( maAdjustmentGuideList.size() )
     156             :         {
     157           5 :             const OUString sType = "Type";
     158          10 :             const OUString sCustomShapeGeometry("CustomShapeGeometry");
     159          10 :             uno::Any aGeoPropSet = xPropSet->getPropertyValue( sCustomShapeGeometry );
     160          10 :             uno::Sequence< beans::PropertyValue > aGeoPropSeq;
     161           5 :             if ( aGeoPropSet >>= aGeoPropSeq )
     162             :             {
     163           5 :                 sal_Int32 i, nCount = aGeoPropSeq.getLength();
     164          55 :                 for ( i = 0; i < nCount; i++ )
     165             :                 {
     166          50 :                     const OUString sAdjustmentValues("AdjustmentValues");
     167          50 :                     if ( aGeoPropSeq[ i ].Name.equals( sAdjustmentValues ) )
     168             :                     {
     169           5 :                         uno::Sequence< com::sun::star::drawing::EnhancedCustomShapeAdjustmentValue > aAdjustmentSeq;
     170           5 :                         if ( aGeoPropSeq[ i ].Value >>= aAdjustmentSeq )
     171             :                         {
     172          18 :                             for (std::vector< CustomShapeGuide >::const_iterator aIter( maAdjustmentGuideList.begin() ), aEnd(maAdjustmentGuideList.end());
     173             :                              aIter != aEnd; ++aIter)
     174             :                             {
     175          13 :                                 if ( (*aIter).maName.getLength() > 3 )
     176             :                                 {
     177          13 :                                     sal_Int32 nAdjustmentIndex = (*aIter).maName.copy( 3 ).toInt32() - 1;
     178          13 :                                     if ( ( nAdjustmentIndex >= 0 ) && ( nAdjustmentIndex < aAdjustmentSeq.getLength() ) )
     179             :                                     {
     180          13 :                                         EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
     181          13 :                                         aAdjustmentVal.Value <<= (*aIter).maFormula.toInt32();
     182          13 :                                         aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
     183          13 :                                         aAdjustmentVal.Name = (*aIter).maName;
     184          13 :                                         aAdjustmentSeq[ nAdjustmentIndex ] = aAdjustmentVal;
     185             :                                     }
     186           0 :                                 } else if ( aAdjustmentSeq.getLength() > 0 ) {
     187           0 :                                     EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
     188           0 :                                     aAdjustmentVal.Value <<= (*aIter).maFormula.toInt32();
     189           0 :                                     aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
     190           0 :                                     aAdjustmentVal.Name = (*aIter).maName;
     191           0 :                                     aAdjustmentSeq[ 0 ] = aAdjustmentVal;
     192             :                                 }
     193             :                             }
     194           5 :                             aGeoPropSeq[ i ].Value <<= aAdjustmentSeq;
     195           5 :                             xPropSet->setPropertyValue( sCustomShapeGeometry, Any( aGeoPropSeq ) );
     196           5 :                         }
     197             :                     }
     198          45 :                     else if ( aGeoPropSeq[ i ].Name.equals( sType ) )
     199             :                     {
     200           5 :                         if ( sConnectorShapeType.getLength() > 0 )
     201           0 :                             aGeoPropSeq[ i ].Value <<= sConnectorShapeType;
     202             :                         else
     203           5 :                             aGeoPropSeq[ i ].Value <<= OUString( "ooxml-CustomShape" );
     204             :                     }
     205          50 :                 }
     206           5 :             }
     207          68 :         }
     208             :     }
     209             :     else
     210             :     {
     211             :         sal_uInt32 i;
     212           3 :         PropertyMap aPropertyMap;
     213           3 :         aPropertyMap[ PROP_Type ] <<= OUString( "ooxml-non-primitive" );
     214           3 :         aPropertyMap[ PROP_MirroredX ] <<= Any( mbMirroredX );
     215           3 :         aPropertyMap[ PROP_MirroredY ] <<= Any( mbMirroredY );
     216           3 :         awt::Size aSize;
     217           3 :         awt::Rectangle aViewBox( 0, 0, aSize.Width * 360, aSize.Height * 360 );
     218           3 :         aPropertyMap[ PROP_ViewBox ] <<= aViewBox;
     219             : 
     220           6 :         Sequence< EnhancedCustomShapeAdjustmentValue > aAdjustmentValues( maAdjustmentGuideList.size() );
     221           3 :         for ( i = 0; i < maAdjustmentGuideList.size(); i++ )
     222             :         {
     223           0 :             EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
     224           0 :             aAdjustmentVal.Value <<= maAdjustmentGuideList[ i ].maFormula.toInt32();
     225           0 :             aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
     226           0 :             aAdjustmentVal.Name = maAdjustmentGuideList[ i ].maName;
     227           0 :             aAdjustmentValues[ i ] = aAdjustmentVal;
     228           0 :         }
     229           3 :         aPropertyMap[ PROP_AdjustmentValues ] <<= aAdjustmentValues;
     230             : 
     231           6 :         PropertyMap aPath;
     232             : 
     233           6 :         Sequence< EnhancedCustomShapeSegment > aSegments( maSegments.size() );
     234          16 :         for ( i = 0; i < maSegments.size(); i++ )
     235          13 :             aSegments[ i ] = maSegments[ i ];
     236           3 :         aPath[ PROP_Segments ] <<= aSegments;
     237             : 
     238           3 :         if ( maTextRect.has() ) {
     239           3 :             Sequence< EnhancedCustomShapeTextFrame > aTextFrames(1);
     240           3 :             aTextFrames[0].TopLeft.First = maTextRect.get().l;
     241           3 :             aTextFrames[0].TopLeft.Second = maTextRect.get().t;
     242           3 :             aTextFrames[0].BottomRight.First = maTextRect.get().r;
     243           3 :             aTextFrames[0].BottomRight.Second = maTextRect.get().b;
     244           3 :             aPath[ PROP_TextFrames ] <<= aTextFrames;
     245             :         }
     246             : 
     247           3 :         sal_uInt32 j, k, nParameterPairs = 0;
     248           6 :         for ( i = 0; i < maPath2DList.size(); i++ )
     249           3 :             nParameterPairs += maPath2DList[ i ].parameter.size();
     250             : 
     251           6 :         Sequence< EnhancedCustomShapeParameterPair > aParameterPairs( nParameterPairs );
     252           6 :         for ( i = 0, k = 0; i < maPath2DList.size(); i++ )
     253          32 :             for ( j = 0; j < maPath2DList[ i ].parameter.size(); j++ )
     254          29 :                 aParameterPairs[ k++ ] = maPath2DList[ i ].parameter[ j ];
     255           3 :         aPath[ PROP_Coordinates ] <<= aParameterPairs;
     256             : 
     257           3 :         if ( maPath2DList.size() )
     258             :         {
     259           3 :             sal_Bool bAllZero = sal_True;
     260           3 :             for ( i=0; i < maPath2DList.size(); i++ )
     261             :             {
     262           3 :                 if ( maPath2DList[i].w || maPath2DList[i].h ) {
     263           3 :                     bAllZero = sal_False;
     264           3 :                     break;
     265             :                 }
     266             :             }
     267             : 
     268           3 :             if ( !bAllZero ) {
     269           3 :                 Sequence< awt::Size > aSubViewSize( maPath2DList.size() );
     270           6 :                 for ( i=0; i < maPath2DList.size(); i++ )
     271             :                 {
     272           3 :                     aSubViewSize[i].Width = static_cast< sal_Int32 >( maPath2DList[i].w );
     273           3 :                     aSubViewSize[i].Height = static_cast< sal_Int32 >( maPath2DList[i].h );
     274             :                     OSL_TRACE("set subpath %d size: %d x %d", i, maPath2DList[i].w, maPath2DList[i].h);
     275             :                 }
     276           3 :                 aPath[ PROP_SubViewSize ] <<= aSubViewSize;
     277             :             }
     278             :         }
     279             : 
     280           6 :         Sequence< PropertyValue > aPathSequence = aPath.makePropertyValueSequence();
     281           3 :         aPropertyMap[ PROP_Path ] <<= aPathSequence;
     282             : 
     283           6 :         Sequence< OUString > aEquations( maGuideList.size() );
     284         163 :         for ( i = 0; i < maGuideList.size(); i++ )
     285         160 :             aEquations[ i ] = maGuideList[ i ].maFormula;
     286           3 :         aPropertyMap[ PROP_Equations ] <<= aEquations;
     287             : 
     288           6 :         Sequence< PropertyValues > aHandles( maAdjustHandleList.size() );
     289           3 :         for ( i = 0; i < maAdjustHandleList.size(); i++ )
     290             :         {
     291           0 :             PropertyMap aHandle;
     292             :             // maAdjustmentHandle[ i ].gdRef1 ... maAdjustmentHandle[ i ].gdRef2 ... :(
     293             :             // gdRef1 && gdRef2 -> we do not offer such reference, so it is difficult
     294             :             // to determine the correct adjustment handle that should be updated with the adjustment
     295             :             // position. here is the solution: the adjustment value that is used within the position
     296             :             // has to be updated, in case the position is a formula the first usage of a
     297             :             // adjustment value is decisive
     298           0 :             if ( maAdjustHandleList[ i ].polar )
     299             :             {
     300           0 :                 aHandle[ PROP_Position ] <<= maAdjustHandleList[ i ].pos;
     301           0 :                 if ( maAdjustHandleList[ i ].min1.has() )
     302           0 :                     aHandle[ PROP_RadiusRangeMinimum ] <<= maAdjustHandleList[ i ].min1.get();
     303           0 :                 if ( maAdjustHandleList[ i ].max1.has() )
     304           0 :                     aHandle[ PROP_RadiusRangeMaximum ] <<= maAdjustHandleList[ i ].max1.get();
     305             : 
     306             :                 /* TODO: AngleMin & AngleMax
     307             :                 if ( maAdjustHandleList[ i ].min2.has() )
     308             :                     aHandle[ PROP_ ] = maAdjustHandleList[ i ].min2.get();
     309             :                 if ( maAdjustHandleList[ i ].max2.has() )
     310             :                     aHandle[ PROP_ ] = maAdjustHandleList[ i ].max2.get();
     311             :                 */
     312             :             }
     313             :             else
     314             :             {
     315           0 :                 aHandle[ PROP_Position ] <<= maAdjustHandleList[ i ].pos;
     316           0 :                 if ( maAdjustHandleList[ i ].gdRef1.has() )
     317             :                 {
     318             :                     // TODO: PROP_RefX and PROP_RefY are not yet part of our file format,
     319             :                     // so the handles will not work after save/reload
     320           0 :                     sal_Int32 nIndex = GetCustomShapeGuideValue( maAdjustmentGuideList, maAdjustHandleList[ i ].gdRef1.get() );
     321           0 :                     if ( nIndex >= 0 )
     322           0 :                         aHandle[ PROP_RefX ] <<= nIndex;
     323             :                 }
     324           0 :                 if ( maAdjustHandleList[ i ].gdRef2.has() )
     325             :                 {
     326           0 :                     sal_Int32 nIndex = GetCustomShapeGuideValue( maAdjustmentGuideList, maAdjustHandleList[ i ].gdRef2.get() );
     327           0 :                     if ( nIndex >= 0 )
     328           0 :                         aHandle[ PROP_RefY ] <<= nIndex;
     329             :                 }
     330           0 :                 if ( maAdjustHandleList[ i ].min1.has() )
     331           0 :                     aHandle[ PROP_RangeXMinimum ] <<= maAdjustHandleList[ i ].min1.get();
     332           0 :                 if ( maAdjustHandleList[ i ].max1.has() )
     333           0 :                     aHandle[ PROP_RangeXMaximum ] <<= maAdjustHandleList[ i ].max1.get();
     334           0 :                 if ( maAdjustHandleList[ i ].min2.has() )
     335           0 :                     aHandle[ PROP_RangeYMinimum ] <<= maAdjustHandleList[ i ].min2.get();
     336           0 :                 if ( maAdjustHandleList[ i ].max2.has() )
     337           0 :                     aHandle[ PROP_RangeYMaximum ] <<= maAdjustHandleList[ i ].max2.get();
     338             :             }
     339           0 :             aHandles[ i ] = aHandle.makePropertyValueSequence();
     340           0 :         }
     341           3 :         aPropertyMap[ PROP_Handles ] <<= aHandles;
     342             : 
     343             : #ifdef DEBUG
     344             :         OSL_TRACE("==cscode== begin");
     345             :         aPropertyMap.dumpCode();
     346             :         OSL_TRACE("==cscode== end");
     347             : #endif
     348             :         // converting the vector to a sequence
     349           6 :         Sequence< PropertyValue > aSeq = aPropertyMap.makePropertyValueSequence();
     350           6 :         PropertySet aPropSet( xPropSet );
     351           6 :         aPropSet.setProperty( PROP_CustomShapeGeometry, aSeq );
     352             :     }
     353          71 : }
     354             : 
     355          64 : Any CustomShapeProvider::createStringSequence( size_t nStrings, const char **pStrings )
     356             : {
     357          64 :     Sequence< OUString > aStringSequence( nStrings );
     358         574 :     for (size_t i = 0; i < nStrings; i++)
     359        1530 :         aStringSequence[i] = OUString::intern(
     360        1020 :                                 pStrings[i], strlen( pStrings[i] ),
     361         510 :                                 RTL_TEXTENCODING_ASCII_US );
     362          64 :     return makeAny( aStringSequence );
     363             : }
     364             : 
     365             : com::sun::star::uno::Sequence< com::sun::star::drawing::EnhancedCustomShapeSegment >
     366          64 : CustomShapeProvider::createSegmentSequence( size_t nElems, const sal_uInt16 *pValues )
     367             : {
     368          64 :     Sequence< EnhancedCustomShapeSegment > aSequence( (nElems + 1) / 2 );
     369         359 :     for (size_t i = 0, j = 0; i < nElems / 2; i++)
     370             :     {
     371         295 :         aSequence[i].Command = pValues[j++];
     372         295 :         aSequence[i].Count = pValues[j++];
     373             :     }
     374          64 :     return aSequence;
     375             : }
     376             : 
     377             : com::sun::star::drawing::EnhancedCustomShapeParameterPair
     378         499 : CustomShapeProvider::createParameterPair( const ParameterPairData *pData )
     379             : {
     380         499 :     EnhancedCustomShapeParameterPair aParameterPair;
     381         499 :     aParameterPair.First.Type = pData->nFirstType;
     382         499 :     aParameterPair.First.Value = makeAny(pData->nFirstValue);
     383         499 :     aParameterPair.Second.Type = pData->nSecondType;
     384         499 :     aParameterPair.Second.Value = makeAny(pData->nSecondValue);
     385         499 :     return aParameterPair;
     386             : }
     387             : 
     388             : com::sun::star::uno::Sequence< com::sun::star::drawing::EnhancedCustomShapeParameterPair >
     389          64 : CustomShapeProvider::createParameterPairSequence( size_t nElems, const ParameterPairData *pData )
     390             : {
     391          64 :     Sequence< EnhancedCustomShapeParameterPair > aSequence( nElems );
     392         423 :     for (size_t i = 0; i < nElems; i++)
     393         359 :         aSequence[i] = createParameterPair( pData + i );
     394          64 :     return aSequence;
     395             : }
     396             : 
     397         141 : } }
     398             : 
     399             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10