bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / draw / ximp3dobject.cxx
blob97952fddb9b2481cd7a8d3007f29df3f8eb78b55
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/extract.hxx>
21 #include "ximp3dobject.hxx"
22 #include <xmloff/XMLShapeStyleContext.hxx>
23 #include <xmloff/xmluconv.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/drawing/PointSequenceSequence.hpp>
26 #include <com/sun/star/drawing/PointSequence.hpp>
27 #include "xexptran.hxx"
28 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
29 #include <xmloff/families.hxx>
30 #include "ximpstyl.hxx"
31 #include <xmloff/xmltoken.hxx>
32 #include <com/sun/star/drawing/PolyPolygonShape3D.hpp>
33 #include <com/sun/star/drawing/DoubleSequence.hpp>
34 #include <osl/diagnose.h>
35 #include <basegfx/polygon/b2dpolypolygon.hxx>
36 #include <basegfx/polygon/b2dpolypolygontools.hxx>
37 #include <basegfx/polygon/b3dpolypolygontools.hxx>
39 using namespace ::com::sun::star;
41 TYPEINIT1( SdXML3DObjectContext, SdXMLShapeContext );
43 SdXML3DObjectContext::SdXML3DObjectContext(
44 SvXMLImport& rImport,
45 sal_uInt16 nPrfx,
46 const OUString& rLocalName,
47 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList,
48 uno::Reference< drawing::XShapes >& rShapes,
49 bool bTemporaryShape)
50 : SdXMLShapeContext( rImport, nPrfx, rLocalName, xAttrList, rShapes, bTemporaryShape ),
51 mbSetTransform( false )
53 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
54 for(sal_Int16 i=0; i < nAttrCount; i++)
56 OUString sAttrName = xAttrList->getNameByIndex( i );
57 OUString aLocalName;
58 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
59 OUString sValue = xAttrList->getValueByIndex( i );
60 const SvXMLTokenMap& rAttrTokenMap = GetImport().GetShapeImport()->Get3DObjectAttrTokenMap();
62 switch(rAttrTokenMap.Get(nPrefix, aLocalName))
64 case XML_TOK_3DOBJECT_DRAWSTYLE_NAME:
66 maDrawStyleName = sValue;
67 break;
69 case XML_TOK_3DOBJECT_TRANSFORM:
71 SdXMLImExTransform3D aTransform(sValue, GetImport().GetMM100UnitConverter());
72 if(aTransform.NeedsAction())
73 mbSetTransform = aTransform.GetFullHomogenTransform(mxHomMat);
74 break;
80 SdXML3DObjectContext::~SdXML3DObjectContext()
84 void SdXML3DObjectContext::StartElement(const uno::Reference< xml::sax::XAttributeList>& xAttrList)
86 uno::Reference< beans::XPropertySet > xPropSet(mxShape, uno::UNO_QUERY);
87 if(xPropSet.is())
89 // set parameters
90 if(mbSetTransform)
92 uno::Any aAny;
93 aAny <<= mxHomMat;
94 xPropSet->setPropertyValue("D3DTransformMatrix", aAny);
97 // call parent
98 SdXMLShapeContext::StartElement(xAttrList);
102 void SdXML3DObjectContext::EndElement()
104 // call parent
105 SdXMLShapeContext::EndElement();
108 TYPEINIT1( SdXML3DCubeObjectShapeContext, SdXML3DObjectContext);
110 SdXML3DCubeObjectShapeContext::SdXML3DCubeObjectShapeContext(
111 SvXMLImport& rImport,
112 sal_uInt16 nPrfx,
113 const OUString& rLocalName,
114 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList,
115 uno::Reference< drawing::XShapes >& rShapes,
116 bool bTemporaryShape)
117 : SdXML3DObjectContext( rImport, nPrfx, rLocalName, xAttrList, rShapes, bTemporaryShape ),
118 maMinEdge(-2500.0, -2500.0, -2500.0),
119 maMaxEdge(2500.0, 2500.0, 2500.0),
120 mbMinEdgeUsed(false),
121 mbMaxEdgeUsed(false)
123 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
124 for(sal_Int16 i=0; i < nAttrCount; i++)
126 OUString sAttrName = xAttrList->getNameByIndex( i );
127 OUString aLocalName;
128 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
129 OUString sValue = xAttrList->getValueByIndex( i );
130 const SvXMLTokenMap& rAttrTokenMap = GetImport().GetShapeImport()->Get3DCubeObjectAttrTokenMap();
132 switch(rAttrTokenMap.Get(nPrefix, aLocalName))
134 case XML_TOK_3DCUBEOBJ_MINEDGE:
136 ::basegfx::B3DVector aNewVec;
137 SvXMLUnitConverter::convertB3DVector(aNewVec, sValue);
139 if(aNewVec != maMinEdge)
141 maMinEdge = aNewVec;
142 mbMinEdgeUsed = true;
144 break;
146 case XML_TOK_3DCUBEOBJ_MAXEDGE:
148 ::basegfx::B3DVector aNewVec;
149 SvXMLUnitConverter::convertB3DVector(aNewVec, sValue);
151 if(aNewVec != maMaxEdge)
153 maMaxEdge = aNewVec;
154 mbMaxEdgeUsed = true;
156 break;
162 SdXML3DCubeObjectShapeContext::~SdXML3DCubeObjectShapeContext()
166 void SdXML3DCubeObjectShapeContext::StartElement(const uno::Reference< xml::sax::XAttributeList>& xAttrList)
168 // create shape
169 AddShape( "com.sun.star.drawing.Shape3DCubeObject" );
170 if(mxShape.is())
172 // add, set style and properties from base shape
173 SetStyle();
174 SdXML3DObjectContext::StartElement(xAttrList);
176 // set local parameters on shape
177 uno::Reference< beans::XPropertySet > xPropSet(mxShape, uno::UNO_QUERY);
178 if(xPropSet.is())
180 // set parameters
181 drawing::Position3D aPosition3D;
182 drawing::Direction3D aDirection3D;
184 // convert from min, max to size to be set
185 maMaxEdge = maMaxEdge - maMinEdge;
187 aPosition3D.PositionX = maMinEdge.getX();
188 aPosition3D.PositionY = maMinEdge.getY();
189 aPosition3D.PositionZ = maMinEdge.getZ();
191 aDirection3D.DirectionX = maMaxEdge.getX();
192 aDirection3D.DirectionY = maMaxEdge.getY();
193 aDirection3D.DirectionZ = maMaxEdge.getZ();
195 uno::Any aAny;
196 aAny <<= aPosition3D;
197 xPropSet->setPropertyValue("D3DPosition", aAny);
198 aAny <<= aDirection3D;
199 xPropSet->setPropertyValue("D3DSize", aAny);
204 void SdXML3DCubeObjectShapeContext::EndElement()
206 // call parent
207 SdXML3DObjectContext::EndElement();
210 TYPEINIT1( SdXML3DSphereObjectShapeContext, SdXML3DObjectContext);
212 SdXML3DSphereObjectShapeContext::SdXML3DSphereObjectShapeContext(
213 SvXMLImport& rImport,
214 sal_uInt16 nPrfx,
215 const OUString& rLocalName,
216 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList,
217 uno::Reference< drawing::XShapes >& rShapes,
218 bool bTemporaryShape)
219 : SdXML3DObjectContext( rImport, nPrfx, rLocalName, xAttrList, rShapes, bTemporaryShape ),
220 maCenter(0.0, 0.0, 0.0),
221 maSize(5000.0, 5000.0, 5000.0),
222 mbCenterUsed(false),
223 mbSizeUsed(false)
225 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
226 for(sal_Int16 i=0; i < nAttrCount; i++)
228 OUString sAttrName = xAttrList->getNameByIndex( i );
229 OUString aLocalName;
230 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
231 OUString sValue = xAttrList->getValueByIndex( i );
232 const SvXMLTokenMap& rAttrTokenMap = GetImport().GetShapeImport()->Get3DSphereObjectAttrTokenMap();
234 switch(rAttrTokenMap.Get(nPrefix, aLocalName))
236 case XML_TOK_3DSPHEREOBJ_CENTER:
238 ::basegfx::B3DVector aNewVec;
239 SvXMLUnitConverter::convertB3DVector(aNewVec, sValue);
241 if(aNewVec != maCenter)
243 maCenter = aNewVec;
244 mbCenterUsed = true;
246 break;
248 case XML_TOK_3DSPHEREOBJ_SIZE:
250 ::basegfx::B3DVector aNewVec;
251 SvXMLUnitConverter::convertB3DVector(aNewVec, sValue);
253 if(aNewVec != maSize)
255 maSize = aNewVec;
256 mbSizeUsed = true;
258 break;
264 SdXML3DSphereObjectShapeContext::~SdXML3DSphereObjectShapeContext()
268 void SdXML3DSphereObjectShapeContext::StartElement(const uno::Reference< xml::sax::XAttributeList>& xAttrList)
270 // create shape
271 AddShape( "com.sun.star.drawing.Shape3DSphereObject" );
272 if(mxShape.is())
274 // add, set style and properties from base shape
275 SetStyle();
276 SdXML3DObjectContext::StartElement(xAttrList);
278 // set local parameters on shape
279 uno::Reference< beans::XPropertySet > xPropSet(mxShape, uno::UNO_QUERY);
280 if(xPropSet.is())
282 // set parameters
283 drawing::Position3D aPosition3D;
284 drawing::Direction3D aDirection3D;
286 aPosition3D.PositionX = maCenter.getX();
287 aPosition3D.PositionY = maCenter.getY();
288 aPosition3D.PositionZ = maCenter.getZ();
290 aDirection3D.DirectionX = maSize.getX();
291 aDirection3D.DirectionY = maSize.getY();
292 aDirection3D.DirectionZ = maSize.getZ();
294 uno::Any aAny;
295 aAny <<= aPosition3D;
296 xPropSet->setPropertyValue("D3DPosition", aAny);
297 aAny <<= aDirection3D;
298 xPropSet->setPropertyValue("D3DSize", aAny);
303 void SdXML3DSphereObjectShapeContext::EndElement()
305 // call parent
306 SdXML3DObjectContext::EndElement();
309 TYPEINIT1( SdXML3DPolygonBasedShapeContext, SdXML3DObjectContext );
311 SdXML3DPolygonBasedShapeContext::SdXML3DPolygonBasedShapeContext(
312 SvXMLImport& rImport,
313 sal_uInt16 nPrfx,
314 const OUString& rLocalName,
315 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList,
316 uno::Reference< drawing::XShapes >& rShapes,
317 bool bTemporaryShape)
318 : SdXML3DObjectContext( rImport, nPrfx, rLocalName, xAttrList, rShapes, bTemporaryShape )
320 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
321 for(sal_Int16 i=0; i < nAttrCount; i++)
323 OUString sAttrName = xAttrList->getNameByIndex( i );
324 OUString aLocalName;
325 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
326 OUString sValue = xAttrList->getValueByIndex( i );
327 const SvXMLTokenMap& rAttrTokenMap = GetImport().GetShapeImport()->Get3DPolygonBasedAttrTokenMap();
329 switch(rAttrTokenMap.Get(nPrefix, aLocalName))
331 case XML_TOK_3DPOLYGONBASED_VIEWBOX:
333 maViewBox = sValue;
334 break;
336 case XML_TOK_3DPOLYGONBASED_D:
338 maPoints = sValue;
339 break;
345 SdXML3DPolygonBasedShapeContext::~SdXML3DPolygonBasedShapeContext()
349 void SdXML3DPolygonBasedShapeContext::StartElement(const uno::Reference< xml::sax::XAttributeList>& xAttrList)
351 uno::Reference< beans::XPropertySet > xPropSet(mxShape, uno::UNO_QUERY);
353 if(xPropSet.is())
355 // set parameters
356 if(!maPoints.isEmpty() && !maViewBox.isEmpty())
358 // import 2d tools::PolyPolygon from svg:d
359 basegfx::B2DPolyPolygon aPolyPolygon;
361 if(basegfx::tools::importFromSvgD(aPolyPolygon, maPoints, GetImport().needFixPositionAfterZ(), 0))
363 // convert to 3D PolyPolygon
364 const basegfx::B3DPolyPolygon aB3DPolyPolygon(
365 basegfx::tools::createB3DPolyPolygonFromB2DPolyPolygon(
366 aPolyPolygon));
368 // convert to UNO API class PolyPolygonShape3D
369 drawing::PolyPolygonShape3D xPolyPolygon3D;
370 basegfx::tools::B3DPolyPolygonToUnoPolyPolygonShape3D(
371 aB3DPolyPolygon,
372 xPolyPolygon3D);
374 // set polygon data
375 uno::Any aAny;
376 aAny <<= xPolyPolygon3D;
377 xPropSet->setPropertyValue(OUString("D3DPolyPolygon3D"), aAny);
379 else
381 OSL_ENSURE(false, "Error on importing svg:d for 3D tools::PolyPolygon (!)");
385 // call parent
386 SdXML3DObjectContext::StartElement(xAttrList);
390 void SdXML3DPolygonBasedShapeContext::EndElement()
392 // call parent
393 SdXML3DObjectContext::EndElement();
396 TYPEINIT1( SdXML3DLatheObjectShapeContext, SdXML3DPolygonBasedShapeContext);
398 SdXML3DLatheObjectShapeContext::SdXML3DLatheObjectShapeContext(
399 SvXMLImport& rImport,
400 sal_uInt16 nPrfx,
401 const OUString& rLocalName,
402 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList,
403 uno::Reference< drawing::XShapes >& rShapes,
404 bool bTemporaryShape)
405 : SdXML3DPolygonBasedShapeContext( rImport, nPrfx, rLocalName, xAttrList, rShapes, bTemporaryShape )
409 SdXML3DLatheObjectShapeContext::~SdXML3DLatheObjectShapeContext()
413 void SdXML3DLatheObjectShapeContext::StartElement(const uno::Reference< xml::sax::XAttributeList>& xAttrList)
415 // create shape
416 AddShape( "com.sun.star.drawing.Shape3DLatheObject" );
417 if(mxShape.is())
419 // add, set style and properties from base shape
420 SetStyle();
421 SdXML3DPolygonBasedShapeContext::StartElement(xAttrList);
425 void SdXML3DLatheObjectShapeContext::EndElement()
427 // call parent
428 SdXML3DPolygonBasedShapeContext::EndElement();
431 TYPEINIT1( SdXML3DExtrudeObjectShapeContext, SdXML3DPolygonBasedShapeContext);
433 SdXML3DExtrudeObjectShapeContext::SdXML3DExtrudeObjectShapeContext(
434 SvXMLImport& rImport,
435 sal_uInt16 nPrfx,
436 const OUString& rLocalName,
437 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList,
438 uno::Reference< drawing::XShapes >& rShapes,
439 bool bTemporaryShape)
440 : SdXML3DPolygonBasedShapeContext( rImport, nPrfx, rLocalName, xAttrList, rShapes, bTemporaryShape )
444 SdXML3DExtrudeObjectShapeContext::~SdXML3DExtrudeObjectShapeContext()
448 void SdXML3DExtrudeObjectShapeContext::StartElement(const uno::Reference< xml::sax::XAttributeList>& xAttrList)
450 AddShape( "com.sun.star.drawing.Shape3DExtrudeObject" );
451 if(mxShape.is())
453 // add, set style and properties from base shape
454 SetStyle();
455 SdXML3DPolygonBasedShapeContext::StartElement(xAttrList);
459 void SdXML3DExtrudeObjectShapeContext::EndElement()
461 // call parent
462 SdXML3DPolygonBasedShapeContext::EndElement();
465 // EOF
467 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */