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

Generated by: LCOV version 1.10