merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / tools / CommonConverters.cxx
blob307fe2251d1e26a38ac0377ab8251735ba900fb1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CommonConverters.cxx,v $
10 * $Revision: 1.12.44.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "CommonConverters.hxx"
34 #include <com/sun/star/drawing/DoubleSequence.hpp>
35 #include <com/sun/star/text/WritingMode2.hpp>
36 #include <com/sun/star/chart2/data/XNumericalDataSequence.hpp>
37 #include <com/sun/star/chart2/data/XTextualDataSequence.hpp>
38 #include <rtl/math.hxx>
39 #include <basegfx/matrix/b3dhommatrix.hxx>
41 #include <cstdarg>
44 //.............................................................................
45 namespace chart
47 //.............................................................................
49 using namespace ::com::sun::star;
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 // diverse methods for class conversions; e.g. ::basegfx::B3DHomMatrix to HomogenMatrix
54 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 drawing::HomogenMatrix B3DHomMatrixToHomogenMatrix( const ::basegfx::B3DHomMatrix& rM )
59 drawing::HomogenMatrix aHM;
60 aHM.Line1.Column1 = rM.get(0, 0);
61 aHM.Line1.Column2 = rM.get(0, 1);
62 aHM.Line1.Column3 = rM.get(0, 2);
63 aHM.Line1.Column4 = rM.get(0, 3);
64 aHM.Line2.Column1 = rM.get(1, 0);
65 aHM.Line2.Column2 = rM.get(1, 1);
66 aHM.Line2.Column3 = rM.get(1, 2);
67 aHM.Line2.Column4 = rM.get(1, 3);
68 aHM.Line3.Column1 = rM.get(2, 0);
69 aHM.Line3.Column2 = rM.get(2, 1);
70 aHM.Line3.Column3 = rM.get(2, 2);
71 aHM.Line3.Column4 = rM.get(2, 3);
72 aHM.Line4.Column1 = rM.get(3, 0);
73 aHM.Line4.Column2 = rM.get(3, 1);
74 aHM.Line4.Column3 = rM.get(3, 2);
75 aHM.Line4.Column4 = rM.get(3, 3);
76 return aHM;
79 ::basegfx::B3DHomMatrix HomogenMatrixToB3DHomMatrix( const drawing::HomogenMatrix& rHM )
81 ::basegfx::B3DHomMatrix aM;
82 aM.set(0, 0, rHM.Line1.Column1);
83 aM.set(0, 1, rHM.Line1.Column2);
84 aM.set(0, 2, rHM.Line1.Column3);
85 aM.set(0, 3, rHM.Line1.Column4);
86 aM.set(1, 0, rHM.Line2.Column1);
87 aM.set(1, 1, rHM.Line2.Column2);
88 aM.set(1, 2, rHM.Line2.Column3);
89 aM.set(1, 3, rHM.Line2.Column4);
90 aM.set(2, 0, rHM.Line3.Column1);
91 aM.set(2, 1, rHM.Line3.Column2);
92 aM.set(2, 2, rHM.Line3.Column3);
93 aM.set(2, 3, rHM.Line3.Column4);
94 aM.set(3, 0, rHM.Line4.Column1);
95 aM.set(3, 1, rHM.Line4.Column2);
96 aM.set(3, 2, rHM.Line4.Column3);
97 aM.set(3, 3, rHM.Line4.Column4);
98 return aM;
101 ::basegfx::B2DHomMatrix IgnoreZ( const ::basegfx::B3DHomMatrix& rM )
103 ::basegfx::B2DHomMatrix aM;
104 aM.set(0, 0, rM.get(0, 0));
105 aM.set(0, 1, rM.get(0, 1));
106 aM.set(0, 2, rM.get(0, 3));
107 aM.set(1, 0, rM.get(1, 0));
108 aM.set(1, 1, rM.get(1, 1));
109 aM.set(1, 2, rM.get(1, 3));
110 aM.set(2, 0, rM.get(3, 0));
111 aM.set(2, 1, rM.get(3, 1));
112 aM.set(2, 2, rM.get(3, 3));
113 return aM;
117 drawing::HomogenMatrix3 B2DHomMatrixToHomogenMatrix3( const ::basegfx::B2DHomMatrix& rM )
119 drawing::HomogenMatrix3 aHM;
120 aHM.Line1.Column1 = rM.get(0, 0);
121 aHM.Line1.Column2 = rM.get(0, 1);
122 aHM.Line1.Column3 = rM.get(0, 2);
123 aHM.Line2.Column1 = rM.get(1, 0);
124 aHM.Line2.Column2 = rM.get(1, 1);
125 aHM.Line2.Column3 = rM.get(1, 2);
126 aHM.Line3.Column1 = rM.get(2, 0);
127 aHM.Line3.Column2 = rM.get(2, 1);
128 aHM.Line3.Column3 = rM.get(2, 2);
129 return aHM;
132 ::basegfx::B3DPoint Position3DToB3DPoint( const drawing::Position3D& rPosition )
134 return ::basegfx::B3DPoint(
135 rPosition.PositionX ,
136 rPosition.PositionY ,
137 rPosition.PositionZ );
140 drawing::Direction3D B3DVectorToDirection3D( const ::basegfx::B3DVector& rVector)
142 return drawing::Direction3D(
143 rVector.getX()
144 , rVector.getY()
145 , rVector.getZ()
149 drawing::Position3D B3DPointToPosition3D( const ::basegfx::B3DPoint& rPoint)
151 return drawing::Position3D(
152 rPoint.getX()
153 , rPoint.getY()
154 , rPoint.getZ()
158 ::basegfx::B3DVector Direction3DToB3DVector( const drawing::Direction3D& rDirection)
160 return ::basegfx::B3DVector(
161 rDirection.DirectionX
162 , rDirection.DirectionY
163 , rDirection.DirectionZ
167 void AddPointToPoly( drawing::PolyPolygonShape3D& rPoly, const drawing::Position3D& rPos, sal_Int32 nPolygonIndex )
169 if(nPolygonIndex<0)
171 OSL_ENSURE( false, "The polygon index needs to be > 0");
172 nPolygonIndex=0;
175 //make sure that we have enough polygons
176 if(nPolygonIndex >= rPoly.SequenceX.getLength() )
178 rPoly.SequenceX.realloc(nPolygonIndex+1);
179 rPoly.SequenceY.realloc(nPolygonIndex+1);
180 rPoly.SequenceZ.realloc(nPolygonIndex+1);
183 drawing::DoubleSequence* pOuterSequenceX = &rPoly.SequenceX.getArray()[nPolygonIndex];
184 drawing::DoubleSequence* pOuterSequenceY = &rPoly.SequenceY.getArray()[nPolygonIndex];
185 drawing::DoubleSequence* pOuterSequenceZ = &rPoly.SequenceZ.getArray()[nPolygonIndex];
187 sal_Int32 nOldPointCount = pOuterSequenceX->getLength();
189 pOuterSequenceX->realloc(nOldPointCount+1);
190 pOuterSequenceY->realloc(nOldPointCount+1);
191 pOuterSequenceZ->realloc(nOldPointCount+1);
193 double* pInnerSequenceX = pOuterSequenceX->getArray();
194 double* pInnerSequenceY = pOuterSequenceY->getArray();
195 double* pInnerSequenceZ = pOuterSequenceZ->getArray();
197 pInnerSequenceX[nOldPointCount] = rPos.PositionX;
198 pInnerSequenceY[nOldPointCount] = rPos.PositionY;
199 pInnerSequenceZ[nOldPointCount] = rPos.PositionZ;
202 drawing::Position3D getPointFromPoly( const drawing::PolyPolygonShape3D& rPolygon, sal_Int32 nPointIndex, sal_Int32 nPolyIndex )
204 drawing::Position3D aRet(0.0,0.0,0.0);
206 if( nPolyIndex>=0 && nPolyIndex<rPolygon.SequenceX.getLength())
208 if(nPointIndex<rPolygon.SequenceX[nPolyIndex].getLength())
210 aRet.PositionX = rPolygon.SequenceX[nPolyIndex][nPointIndex];
211 aRet.PositionY = rPolygon.SequenceY[nPolyIndex][nPointIndex];
212 aRet.PositionZ = rPolygon.SequenceZ[nPolyIndex][nPointIndex];
214 else
216 ;DBG_ERROR("polygon was accessed with a wrong index");
219 else
221 ;DBG_ERROR("polygon was accessed with a wrong index");
223 return aRet;
226 void addPolygon( drawing::PolyPolygonShape3D& rRet, const drawing::PolyPolygonShape3D& rAdd )
228 sal_Int32 nAddOuterCount = rAdd.SequenceX.getLength();
229 sal_Int32 nOuterCount = rRet.SequenceX.getLength() + nAddOuterCount;
230 rRet.SequenceX.realloc( nOuterCount );
231 rRet.SequenceY.realloc( nOuterCount );
232 rRet.SequenceZ.realloc( nOuterCount );
234 sal_Int32 nIndex = 0;
235 sal_Int32 nOuter = nOuterCount - nAddOuterCount;
236 for( ; nOuter < nOuterCount; nOuter++ )
238 if( nIndex >= nAddOuterCount )
239 break;
241 rRet.SequenceX[nOuter] = rAdd.SequenceX[nIndex];
242 rRet.SequenceY[nOuter] = rAdd.SequenceY[nIndex];
243 rRet.SequenceZ[nOuter] = rAdd.SequenceZ[nIndex];
245 nIndex++;
249 void appendPoly( drawing::PolyPolygonShape3D& rRet, const drawing::PolyPolygonShape3D& rAdd )
251 sal_Int32 nOuterCount = Max( rRet.SequenceX.getLength(), rAdd.SequenceX.getLength() );
252 rRet.SequenceX.realloc(nOuterCount);
253 rRet.SequenceY.realloc(nOuterCount);
254 rRet.SequenceZ.realloc(nOuterCount);
256 for( sal_Int32 nOuter=0;nOuter<nOuterCount;nOuter++ )
258 sal_Int32 nOldPointCount = rRet.SequenceX[nOuter].getLength();
259 sal_Int32 nAddPointCount = 0;
260 if(nOuter<rAdd.SequenceX.getLength())
261 nAddPointCount = rAdd.SequenceX[nOuter].getLength();
262 if(!nAddPointCount)
263 continue;
265 sal_Int32 nNewPointCount = nOldPointCount + nAddPointCount;
267 rRet.SequenceX[nOuter].realloc(nNewPointCount);
268 rRet.SequenceY[nOuter].realloc(nNewPointCount);
269 rRet.SequenceZ[nOuter].realloc(nNewPointCount);
271 sal_Int32 nPointTarget=nOldPointCount;
272 sal_Int32 nPointSource=nAddPointCount;
273 for( ; nPointSource-- ; nPointTarget++ )
275 rRet.SequenceX[nOuter][nPointTarget] = rAdd.SequenceX[nOuter][nPointSource];
276 rRet.SequenceY[nOuter][nPointTarget] = rAdd.SequenceY[nOuter][nPointSource];
277 rRet.SequenceZ[nOuter][nPointTarget] = rAdd.SequenceZ[nOuter][nPointSource];
282 drawing::PolyPolygonShape3D BezierToPoly(
283 const drawing::PolyPolygonBezierCoords& rBezier )
285 const drawing::PointSequenceSequence& rPointSequence = rBezier.Coordinates;
286 // const drawing::FlagSequenceSequence& rFlags = rBezier.Flags;
288 drawing::PolyPolygonShape3D aRet;
289 aRet.SequenceX.realloc( rPointSequence.getLength() );
290 aRet.SequenceY.realloc( rPointSequence.getLength() );
291 aRet.SequenceZ.realloc( rPointSequence.getLength() );
293 sal_Int32 nRealOuter = 0;
294 for(sal_Int32 nN = 0; nN < rPointSequence.getLength(); nN++)
296 sal_Int32 nInnerLength = rPointSequence[nN].getLength();
297 aRet.SequenceX[nN].realloc( nInnerLength );
298 aRet.SequenceY[nN].realloc( nInnerLength );
299 aRet.SequenceZ[nN].realloc( nInnerLength );
301 bool bHasOuterFlags = nN < rBezier.Flags.getLength();
303 sal_Int32 nRealInner = 0;
304 for( sal_Int32 nM = 0; nM < nInnerLength; nM++)
306 bool bHasInnerFlags = bHasOuterFlags && (nM < rBezier.Flags[nN].getLength());
308 if( !bHasInnerFlags || (rBezier.Flags[nN][nM] == drawing::PolygonFlags_NORMAL) )
310 aRet.SequenceX[nRealOuter][nRealInner] = rPointSequence[nN][nM].X;
311 aRet.SequenceY[nRealOuter][nRealInner] = rPointSequence[nN][nM].Y;
312 aRet.SequenceZ[nRealOuter][nRealInner] = 0.0;
313 nRealInner++;
317 aRet.SequenceX[nRealOuter].realloc( nRealInner );
318 aRet.SequenceY[nRealOuter].realloc( nRealInner );
319 aRet.SequenceZ[nRealOuter].realloc( nRealInner );
321 if( nRealInner>0 )
322 nRealOuter++;
325 aRet.SequenceX.realloc( nRealOuter );
326 aRet.SequenceY.realloc( nRealOuter );
327 aRet.SequenceZ.realloc( nRealOuter );
329 return aRet;
332 drawing::PointSequenceSequence PolyToPointSequence(
333 const drawing::PolyPolygonShape3D& rPolyPolygon )
335 drawing::PointSequenceSequence aRet;
336 aRet.realloc( rPolyPolygon.SequenceX.getLength() );
338 for(sal_Int32 nN = 0; nN < rPolyPolygon.SequenceX.getLength(); nN++)
340 sal_Int32 nInnerLength = rPolyPolygon.SequenceX[nN].getLength();
341 aRet[nN].realloc( nInnerLength );
342 for( sal_Int32 nM = 0; nM < nInnerLength; nM++)
344 aRet[nN][nM].X = static_cast<sal_Int32>(rPolyPolygon.SequenceX[nN][nM]);
345 aRet[nN][nM].Y = static_cast<sal_Int32>(rPolyPolygon.SequenceY[nN][nM]);
348 return aRet;
351 void appendPointSequence( drawing::PointSequenceSequence& rTarget
352 , drawing::PointSequenceSequence& rAdd )
354 sal_Int32 nAddCount = rAdd.getLength();
355 if(!nAddCount)
356 return;
357 sal_Int32 nOldCount = rTarget.getLength();
359 rTarget.realloc(nOldCount+nAddCount);
360 for(sal_Int32 nS=0; nS<nAddCount; nS++ )
361 rTarget[nOldCount+nS]=rAdd[nS];
364 drawing::Position3D operator+( const drawing::Position3D& rPos
365 , const drawing::Direction3D& rDirection)
367 return drawing::Position3D(
368 rPos.PositionX + rDirection.DirectionX
369 , rPos.PositionY + rDirection.DirectionY
370 , rPos.PositionZ + rDirection.DirectionZ
374 drawing::Direction3D operator-( const drawing::Position3D& rPos1
375 , const drawing::Position3D& rPos2)
377 return drawing::Direction3D(
378 rPos1.PositionX - rPos2.PositionX
379 , rPos1.PositionY - rPos2.PositionY
380 , rPos1.PositionZ - rPos2.PositionZ
384 bool operator==( const drawing::Position3D& rPos1
385 , const drawing::Position3D& rPos2)
387 return rPos1.PositionX == rPos2.PositionX
388 && rPos1.PositionY == rPos2.PositionY
389 && rPos1.PositionZ == rPos2.PositionZ;
392 awt::Point Position3DToAWTPoint( const drawing::Position3D& rPos )
394 awt::Point aRet;
395 aRet.X = static_cast<sal_Int32>(rPos.PositionX);
396 aRet.Y = static_cast<sal_Int32>(rPos.PositionY);
397 return aRet;
400 awt::Point ToPoint( const awt::Rectangle& rRectangle )
402 return awt::Point( rRectangle.X, rRectangle.Y );
405 awt::Size ToSize( const awt::Rectangle& rRectangle )
407 return awt::Size( rRectangle.Width, rRectangle.Height );
410 awt::Size Direction3DToAWTSize( const drawing::Direction3D& rDirection )
412 awt::Size aRet;
413 aRet.Width = static_cast<sal_Int32>(rDirection.DirectionX);
414 aRet.Height = static_cast<sal_Int32>(rDirection.DirectionY);
415 return aRet;
418 uno::Sequence< double > B3DPointToSequence( const ::basegfx::B3DPoint& rPoint )
420 uno::Sequence< double > aRet(3);
421 aRet[0] = rPoint.getX();
422 aRet[1] = rPoint.getY();
423 aRet[2] = rPoint.getZ();
424 return aRet;
427 drawing::Position3D SequenceToPosition3D( const uno::Sequence< double >& rSeq )
429 OSL_ENSURE(rSeq.getLength()==3,"The sequence needs to have length 3 for conversion into vector");
431 drawing::Position3D aRet;
432 aRet.PositionX = rSeq.getLength()>0?rSeq[0]:0.0;
433 aRet.PositionY = rSeq.getLength()>1?rSeq[1]:0.0;
434 aRet.PositionZ = rSeq.getLength()>2?rSeq[2]:0.0;
435 return aRet;
438 uno::Sequence< double > Position3DToSequence( const drawing::Position3D& rPosition )
440 uno::Sequence< double > aRet(3);
441 aRet[0] = rPosition.PositionX;
442 aRet[1] = rPosition.PositionY;
443 aRet[2] = rPosition.PositionZ;
444 return aRet;
447 using namespace ::com::sun::star::chart2;
449 uno::Sequence< double > DataSequenceToDoubleSequence(
450 const uno::Reference< data::XDataSequence >& xDataSequence )
452 uno::Sequence< double > aResult;
453 OSL_ASSERT( xDataSequence.is());
454 if(!xDataSequence.is())
455 return aResult;
457 uno::Reference< data::XNumericalDataSequence > xNumericalDataSequence( xDataSequence, uno::UNO_QUERY );
458 if( xNumericalDataSequence.is() )
460 aResult = xNumericalDataSequence->getNumericalData();
462 else
464 uno::Sequence< uno::Any > aValues = xDataSequence->getData();
465 aResult.realloc(aValues.getLength());
466 for(sal_Int32 nN=aValues.getLength();nN--;)
468 if( !(aValues[nN] >>= aResult[nN]) )
469 ::rtl::math::setNan( &aResult[nN] );
473 return aResult;
476 uno::Sequence< rtl::OUString > DataSequenceToStringSequence(
477 const uno::Reference< data::XDataSequence >& xDataSequence )
479 uno::Sequence< rtl::OUString > aResult;
480 OSL_ASSERT( xDataSequence.is());
481 if(!xDataSequence.is())
482 return aResult;
484 uno::Reference< data::XTextualDataSequence > xTextualDataSequence( xDataSequence, uno::UNO_QUERY );
485 if( xTextualDataSequence.is() )
487 aResult = xTextualDataSequence->getTextualData();
489 else
491 uno::Sequence< uno::Any > aValues = xDataSequence->getData();
492 aResult.realloc(aValues.getLength());
494 for(sal_Int32 nN=aValues.getLength();nN--;)
495 aValues[nN] >>= aResult[nN];
498 return aResult;
501 sal_Bool hasDoubleValue( const uno::Any& rAny )
503 sal_Bool bRet = sal_False;
504 double fValue = 0.0;
505 if( rAny >>= fValue )
506 bRet = sal_True;
507 return bRet;
510 sal_Bool hasLongOrShortValue( const uno::Any& rAny )
512 sal_Bool bRet = sal_False;
513 sal_Int32 n32 = 0;
514 if( rAny >>= n32 )
515 bRet = sal_True;
516 else
518 sal_Int16 n16 = 0;
519 if( rAny >>= n16 )
520 bRet = sal_True;
522 return bRet;
524 sal_Int16 getShortForLongAlso( const uno::Any& rAny )
526 sal_Int16 nRet = 0;
528 if( !(rAny >>= nRet) )
530 sal_Int32 n32 = 0;
531 if( rAny >>= n32 )
532 nRet = static_cast<sal_Int16>(n32);
534 return nRet;
537 bool replaceParamterInString( rtl::OUString & rInOutResourceString,
538 const rtl::OUString & rParamToReplace,
539 const rtl::OUString & rReplaceWith )
541 sal_Int32 nPos = rInOutResourceString.indexOf( rParamToReplace );
542 if( nPos == -1 )
543 return false;
545 rInOutResourceString = rInOutResourceString.replaceAt( nPos
546 , rParamToReplace.getLength(), rReplaceWith );
547 return true;
550 //.............................................................................
551 } //namespace chart
552 //.............................................................................