LCOV - code coverage report
Current view: top level - oox/source/drawingml - customshapeproperties.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 183 188 97.3 %
Date: 2015-06-13 12:38:46 Functions: 9 10 90.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 "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             : #include <osl/diagnose.h>
      34             : 
      35             : using namespace ::oox::core;
      36             : using namespace ::com::sun::star;
      37             : using namespace ::com::sun::star::uno;
      38             : using namespace ::com::sun::star::beans;
      39             : using namespace ::com::sun::star::graphic;
      40             : using namespace ::com::sun::star::drawing;
      41             : 
      42             : namespace oox { namespace drawingml {
      43             : 
      44       13004 : CustomShapeProperties::CustomShapeProperties()
      45             : : mnShapePresetType ( -1 )
      46             : , mbShapeTypeOverride(false)
      47             : , mbMirroredX   ( false )
      48             : , mbMirroredY   ( false )
      49             : , mnTextRotateAngle ( 0 )
      50       13004 : , mnArcNum ( 0 )
      51             : {
      52       13004 : }
      53       26686 : CustomShapeProperties::~CustomShapeProperties()
      54             : {
      55       26686 : }
      56             : 
      57           0 : uno::Sequence< sal_Int8 > CustomShapeProperties::getShapePresetTypeName() const
      58             : {
      59           0 :     return StaticTokenMap::get().getUtf8TokenName( mnShapePresetType );
      60             : }
      61             : 
      62        4220 : sal_Int32 CustomShapeProperties::SetCustomShapeGuideValue( std::vector< CustomShapeGuide >& rGuideList, const CustomShapeGuide& rGuide )
      63             : {
      64        4220 :     sal_uInt32 nIndex = 0;
      65       75669 :     for( ; nIndex < rGuideList.size(); nIndex++ )
      66             :     {
      67       73630 :         if ( rGuideList[ nIndex ].maName == rGuide.maName )
      68        2181 :             break;
      69             :     }
      70        4220 :     if ( nIndex == rGuideList.size() )
      71        2039 :         rGuideList.push_back( rGuide );
      72        4220 :     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       19627 : 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       19627 :     sal_Int32 nIndex = static_cast< sal_Int32 >( rGuideList.size() ) - 1;
      83      208938 :     for( ; nIndex >= 0; nIndex-- )
      84             :     {
      85      199406 :         if ( rGuideList[ nIndex ].maName == rFormulaName )
      86       10095 :             break;
      87             :     }
      88             : 
      89       19627 :     return nIndex;
      90             : }
      91             : 
      92          82 : CustomShapeProperties::PresetDataMap CustomShapeProperties::maPresetDataMap;
      93             : 
      94        1517 : static OUString GetConnectorShapeType( sal_Int32 nType )
      95             : {
      96             :     SAL_INFO(
      97             :         "oox.drawingml", "preset: " << nType << " " << XML_straightConnector1);
      98             : 
      99        1517 :     OUString sType;
     100        1517 :     switch( nType )
     101             :     {
     102             :         case XML_straightConnector1:
     103         109 :             sType = "mso-spt32";
     104         109 :             break;
     105             :         default:
     106        1408 :             break;
     107             :     }
     108        1517 :     return sType;
     109             : }
     110             : 
     111        1976 : void CustomShapeProperties::pushToPropSet( const ::oox::core::FilterBase& /* rFilterBase */,
     112             :     const Reference < XPropertySet >& xPropSet, const Reference < XShape > & xShape, const awt::Size &aSize )
     113             : {
     114        1976 :     if ( mnShapePresetType >= 0 )
     115             :     {
     116             :         SAL_INFO("oox.drawingml", "preset: " << mnShapePresetType);
     117             : 
     118        1517 :         if (maPresetDataMap.empty())
     119          17 :             initializePresetDataMap();
     120             : 
     121        1517 :         PropertyMap aPropertyMap;
     122        3034 :         PropertySet aPropSet( xPropSet );
     123             : 
     124        3034 :         OUString sConnectorShapeType = GetConnectorShapeType( mnShapePresetType );
     125             : 
     126        1517 :         if (sConnectorShapeType.getLength() > 0)
     127             :         {
     128             :             SAL_INFO(
     129             :                 "oox.drawingml",
     130             :                 "connector shape: " << sConnectorShapeType << " ("
     131             :                     << mnShapePresetType << ")");
     132             :             //const uno::Reference < drawing::XShape > xShape( xPropSet, UNO_QUERY );
     133         109 :             Reference< drawing::XEnhancedCustomShapeDefaulter > xDefaulter( xShape, UNO_QUERY );
     134         109 :             if( xDefaulter.is() ) {
     135         109 :                 xDefaulter->createCustomShapeDefaults( sConnectorShapeType );
     136         109 :                 aPropertyMap.setProperty( PROP_Type, sConnectorShapeType );
     137         109 :             }
     138             :         }
     139        1408 :         else if (maPresetDataMap.find(mnShapePresetType) != maPresetDataMap.end())
     140             :         {
     141             :             SAL_INFO(
     142             :                 "oox.drawingml",
     143             :                 "found property map for preset: " << mnShapePresetType);
     144             : 
     145        1408 :             aPropertyMap = maPresetDataMap[mnShapePresetType];
     146             : #ifdef DEBUG
     147             :             aPropertyMap.dumpCode();
     148             : #endif
     149             :         }
     150             : 
     151        1517 :         aPropertyMap.setProperty( PROP_MirroredX, mbMirroredX );
     152        1517 :         aPropertyMap.setProperty( PROP_MirroredY, mbMirroredY );
     153        1517 :         aPropertyMap.setProperty( PROP_TextPreRotateAngle, mnTextRotateAngle );
     154        1517 :         aPropertyMap.setProperty( PROP_IsPostRotateAngle, true); // For OpenXML Imports
     155        3034 :         Sequence< PropertyValue > aSeq = aPropertyMap.makePropertyValueSequence();
     156        1517 :         aPropSet.setProperty( PROP_CustomShapeGeometry, aSeq );
     157             : 
     158        3034 :         const OUString sCustomShapeGeometry("CustomShapeGeometry");
     159        3034 :         uno::Any aGeoPropSet = xPropSet->getPropertyValue( sCustomShapeGeometry );
     160        3034 :         uno::Sequence< beans::PropertyValue > aGeoPropSeq;
     161             : 
     162        1517 :         sal_Int32 i, nCount = 0;
     163        1517 :         if (aGeoPropSet >>= aGeoPropSeq)
     164             :         {
     165        1517 :             nCount = aGeoPropSeq.getLength();
     166       16469 :             for ( i = 0; i < nCount; i++ )
     167             :             {
     168       14952 :                 const OUString sAdjustmentValues("AdjustmentValues");
     169       14952 :                 if ( aGeoPropSeq[ i ].Name.equals( sAdjustmentValues ) )
     170             :                 {
     171        1517 :                     OUString presetTextWarp;
     172        1517 :                     if ( aGeoPropSeq[ i ].Value >>= presetTextWarp )
     173             :                     {
     174           0 :                         aPropertyMap.setProperty( PROP_PresetTextWarp, Any( presetTextWarp ) );
     175        1517 :                     }
     176             :                 }
     177       14952 :             }
     178             :         }
     179             : 
     180        1517 :         if ( maAdjustmentGuideList.size() )
     181             :         {
     182         226 :             const OUString sType = "Type";
     183         226 :             if ( aGeoPropSet >>= aGeoPropSeq )
     184             :             {
     185         226 :                 nCount = aGeoPropSeq.getLength();
     186        2486 :                 for ( i = 0; i < nCount; i++ )
     187             :                 {
     188        2260 :                     const OUString sAdjustmentValues("AdjustmentValues");
     189        2260 :                     if ( aGeoPropSeq[ i ].Name.equals( sAdjustmentValues ) )
     190             :                     {
     191         226 :                         uno::Sequence< com::sun::star::drawing::EnhancedCustomShapeAdjustmentValue > aAdjustmentSeq;
     192         226 :                         if ( aGeoPropSeq[ i ].Value >>= aAdjustmentSeq )
     193             :                         {
     194         226 :                             int nIndex=0;
     195         526 :                             for (std::vector< CustomShapeGuide >::const_iterator aIter( maAdjustmentGuideList.begin() ), aEnd(maAdjustmentGuideList.end());
     196             :                              aIter != aEnd; ++aIter)
     197             :                             {
     198         300 :                                 if ( (*aIter).maName.getLength() > 3 )
     199             :                                 {
     200         110 :                                     sal_Int32 nAdjustmentIndex = (*aIter).maName.copy( 3 ).toInt32() - 1;
     201         110 :                                     if ( ( nAdjustmentIndex >= 0 ) && ( nAdjustmentIndex < aAdjustmentSeq.getLength() ) )
     202             :                                     {
     203         110 :                                         EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
     204         110 :                                         aAdjustmentVal.Value <<= (*aIter).maFormula.toInt32();
     205         110 :                                         aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
     206         110 :                                         aAdjustmentVal.Name = (*aIter).maName;
     207         110 :                                         aAdjustmentSeq[ nAdjustmentIndex ] = aAdjustmentVal;
     208             :                                     }
     209         190 :                                 } else if ( aAdjustmentSeq.getLength() > 0 ) {
     210         190 :                                     EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
     211         190 :                                     aAdjustmentVal.Value <<= (*aIter).maFormula.toInt32();
     212         190 :                                     aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
     213         190 :                                     aAdjustmentVal.Name = (*aIter).maName;
     214         190 :                                     aAdjustmentSeq[ nIndex++ ] = aAdjustmentVal;
     215             :                                 }
     216             :                             }
     217         226 :                             aGeoPropSeq[ i ].Value <<= aAdjustmentSeq;
     218         226 :                             xPropSet->setPropertyValue( sCustomShapeGeometry, Any( aGeoPropSeq ) );
     219         226 :                         }
     220             :                     }
     221        2034 :                     else if ( aGeoPropSeq[ i ].Name.equals( sType ) )
     222             :                     {
     223         226 :                         if ( sConnectorShapeType.getLength() > 0 )
     224           0 :                             aGeoPropSeq[ i ].Value <<= sConnectorShapeType;
     225             :                         else
     226         226 :                             aGeoPropSeq[ i ].Value <<= OUString( "ooxml-CustomShape" );
     227             :                     }
     228        2260 :                 }
     229         226 :             }
     230        1517 :         }
     231             :     }
     232             :     else
     233             :     {
     234             :         sal_uInt32 i;
     235         459 :         PropertyMap aPropertyMap;
     236         459 :         aPropertyMap.setProperty( PROP_Type, OUString( "ooxml-non-primitive" ));
     237         459 :         aPropertyMap.setProperty( PROP_MirroredX, mbMirroredX );
     238         459 :         aPropertyMap.setProperty( PROP_MirroredY, mbMirroredY );
     239             :         // Note 1: If Equations are defined - they are processed using internal div by 360 coordinates
     240             :         // while if they are not, standard ooxml coordinates are used.
     241             :         // This size specifically affects scaling.
     242             :         // Note 2: Width and Height are set to 0 to force scaling to 1.
     243         459 :         awt::Rectangle aViewBox( 0, 0, aSize.Width, aSize.Height );
     244         459 :         if( maGuideList.size() )
     245         390 :             aViewBox = awt::Rectangle( 0, 0, 0, 0 );
     246         459 :         aPropertyMap.setProperty( PROP_ViewBox, aViewBox);
     247             : 
     248         918 :         Sequence< EnhancedCustomShapeAdjustmentValue > aAdjustmentValues( maAdjustmentGuideList.size() );
     249         757 :         for ( i = 0; i < maAdjustmentGuideList.size(); i++ )
     250             :         {
     251         298 :             EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
     252         298 :             aAdjustmentVal.Value <<= maAdjustmentGuideList[ i ].maFormula.toInt32();
     253         298 :             aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
     254         298 :             aAdjustmentVal.Name = maAdjustmentGuideList[ i ].maName;
     255         298 :             aAdjustmentValues[ i ] = aAdjustmentVal;
     256         298 :         }
     257         459 :         aPropertyMap.setProperty( PROP_AdjustmentValues, aAdjustmentValues);
     258             : 
     259         918 :         PropertyMap aPath;
     260             : 
     261         918 :         Sequence< EnhancedCustomShapeSegment > aSegments( maSegments.size() );
     262        4093 :         for ( i = 0; i < maSegments.size(); i++ )
     263        3634 :             aSegments[ i ] = maSegments[ i ];
     264         459 :         aPath.setProperty( PROP_Segments, aSegments);
     265             : 
     266         459 :         if ( maTextRect.has() ) {
     267         448 :             Sequence< EnhancedCustomShapeTextFrame > aTextFrames(1);
     268         448 :             aTextFrames[0].TopLeft.First = maTextRect.get().l;
     269         448 :             aTextFrames[0].TopLeft.Second = maTextRect.get().t;
     270         448 :             aTextFrames[0].BottomRight.First = maTextRect.get().r;
     271         448 :             aTextFrames[0].BottomRight.Second = maTextRect.get().b;
     272         448 :             aPath.setProperty( PROP_TextFrames, aTextFrames);
     273             :         }
     274             : 
     275         459 :         sal_uInt32 j, k, nParameterPairs = 0;
     276        1078 :         for ( i = 0; i < maPath2DList.size(); i++ )
     277         619 :             nParameterPairs += maPath2DList[ i ].parameter.size();
     278             : 
     279         918 :         Sequence< EnhancedCustomShapeParameterPair > aParameterPairs( nParameterPairs );
     280        1078 :         for ( i = 0, k = 0; i < maPath2DList.size(); i++ )
     281        8222 :             for ( j = 0; j < maPath2DList[ i ].parameter.size(); j++ )
     282        7603 :                 aParameterPairs[ k++ ] = maPath2DList[ i ].parameter[ j ];
     283         459 :         aPath.setProperty( PROP_Coordinates, aParameterPairs);
     284             : 
     285         459 :         if ( maPath2DList.size() )
     286             :         {
     287         448 :             bool bAllZero = true;
     288         786 :             for ( i=0; i < maPath2DList.size(); i++ )
     289             :             {
     290         559 :                 if ( maPath2DList[i].w || maPath2DList[i].h ) {
     291         221 :                     bAllZero = false;
     292         221 :                     break;
     293             :                 }
     294             :             }
     295             : 
     296         448 :             if ( !bAllZero ) {
     297         221 :                 Sequence< awt::Size > aSubViewSize( maPath2DList.size() );
     298         502 :                 for ( i=0; i < maPath2DList.size(); i++ )
     299             :                 {
     300         281 :                     aSubViewSize[i].Width = static_cast< sal_Int32 >( maPath2DList[i].w );
     301         281 :                     aSubViewSize[i].Height = static_cast< sal_Int32 >( maPath2DList[i].h );
     302             :                     SAL_INFO(
     303             :                         "oox.cscode",
     304             :                         "set subpath " << i << " size: " << maPath2DList[i].w
     305             :                             << " x " << maPath2DList[i].h);
     306             :                 }
     307         221 :                 aPath.setProperty( PROP_SubViewSize, aSubViewSize);
     308             :             }
     309             :         }
     310             : 
     311         918 :         Sequence< PropertyValue > aPathSequence = aPath.makePropertyValueSequence();
     312         459 :         aPropertyMap.setProperty( PROP_Path, aPathSequence);
     313             : 
     314         918 :         Sequence< OUString > aEquations( maGuideList.size() );
     315        6406 :         for ( i = 0; i < maGuideList.size(); i++ )
     316        5947 :             aEquations[ i ] = maGuideList[ i ].maFormula;
     317         459 :         aPropertyMap.setProperty( PROP_Equations, aEquations);
     318             : 
     319         918 :         Sequence< PropertyValues > aHandles( maAdjustHandleList.size() );
     320         700 :         for ( i = 0; i < maAdjustHandleList.size(); i++ )
     321             :         {
     322         241 :             PropertyMap aHandle;
     323             :             // maAdjustmentHandle[ i ].gdRef1 ... maAdjustmentHandle[ i ].gdRef2 ... :(
     324             :             // gdRef1 && gdRef2 -> we do not offer such reference, so it is difficult
     325             :             // to determine the correct adjustment handle that should be updated with the adjustment
     326             :             // position. here is the solution: the adjustment value that is used within the position
     327             :             // has to be updated, in case the position is a formula the first usage of a
     328             :             // adjustment value is decisive
     329         241 :             if ( maAdjustHandleList[ i ].polar )
     330             :             {
     331          23 :                 aHandle.setProperty( PROP_Position, maAdjustHandleList[ i ].pos);
     332          23 :                 if ( maAdjustHandleList[ i ].min1.has() )
     333           0 :                     aHandle.setProperty( PROP_RadiusRangeMinimum, maAdjustHandleList[ i ].min1.get());
     334          23 :                 if ( maAdjustHandleList[ i ].max1.has() )
     335           9 :                     aHandle.setProperty( PROP_RadiusRangeMaximum, maAdjustHandleList[ i ].max1.get());
     336             : 
     337             :                 /* TODO: AngleMin & AngleMax
     338             :                 if ( maAdjustHandleList[ i ].min2.has() )
     339             :                     aHandle.setProperty( PROP_ ] = maAdjustHandleList[ i ].min2.get());
     340             :                 if ( maAdjustHandleList[ i ].max2.has() )
     341             :                     aHandle.setProperty( PROP_ ] = maAdjustHandleList[ i ].max2.get());
     342             :                 */
     343             :             }
     344             :             else
     345             :             {
     346         218 :                 aHandle.setProperty( PROP_Position, maAdjustHandleList[ i ].pos);
     347         218 :                 if ( maAdjustHandleList[ i ].gdRef1.has() )
     348             :                 {
     349             :                     // TODO: PROP_RefX and PROP_RefY are not yet part of our file format,
     350             :                     // so the handles will not work after save/reload
     351         131 :                     sal_Int32 nIndex = GetCustomShapeGuideValue( maAdjustmentGuideList, maAdjustHandleList[ i ].gdRef1.get() );
     352         131 :                     if ( nIndex >= 0 )
     353         131 :                         aHandle.setProperty( PROP_RefX, nIndex);
     354             :                 }
     355         218 :                 if ( maAdjustHandleList[ i ].gdRef2.has() )
     356             :                 {
     357         127 :                     sal_Int32 nIndex = GetCustomShapeGuideValue( maAdjustmentGuideList, maAdjustHandleList[ i ].gdRef2.get() );
     358         127 :                     if ( nIndex >= 0 )
     359         127 :                         aHandle.setProperty( PROP_RefY, nIndex);
     360             :                 }
     361         218 :                 if ( maAdjustHandleList[ i ].min1.has() )
     362          56 :                     aHandle.setProperty( PROP_RangeXMinimum, maAdjustHandleList[ i ].min1.get());
     363         218 :                 if ( maAdjustHandleList[ i ].max1.has() )
     364         131 :                     aHandle.setProperty( PROP_RangeXMaximum, maAdjustHandleList[ i ].max1.get());
     365         218 :                 if ( maAdjustHandleList[ i ].min2.has() )
     366          51 :                     aHandle.setProperty( PROP_RangeYMinimum, maAdjustHandleList[ i ].min2.get());
     367         218 :                 if ( maAdjustHandleList[ i ].max2.has() )
     368         127 :                     aHandle.setProperty( PROP_RangeYMaximum, maAdjustHandleList[ i ].max2.get());
     369             :             }
     370         241 :             aHandles[ i ] = aHandle.makePropertyValueSequence();
     371         241 :         }
     372         459 :         aPropertyMap.setProperty( PROP_Handles, aHandles);
     373             : 
     374             : #ifdef DEBUG
     375             :         SAL_INFO("oox.cscode", "==cscode== begin");
     376             :         aPropertyMap.dumpCode();
     377             :         SAL_INFO("oox.cscode", "==cscode== end");
     378             :         SAL_INFO("oox.csdata", "==csdata== begin");
     379             :         aPropertyMap.dumpData();
     380             :         SAL_INFO("oox.csdata", "==csdata== end");
     381             : #endif
     382             :         // converting the vector to a sequence
     383         918 :         Sequence< PropertyValue > aSeq = aPropertyMap.makePropertyValueSequence();
     384         918 :         PropertySet aPropSet( xPropSet );
     385         918 :         aPropSet.setProperty( PROP_CustomShapeGeometry, aSeq );
     386             :     }
     387        1976 : }
     388             : 
     389         246 : } }
     390             : 
     391             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11