1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_xmloff.hxx"
24 
25 #include <com/sun/star/drawing/HomogenMatrix.hpp>
26 #include <com/sun/star/drawing/PolyPolygonShape3D.hpp>
27 #include <com/sun/star/drawing/ProjectionMode.hpp>
28 #include <com/sun/star/drawing/ShadeMode.hpp>
29 #include <com/sun/star/drawing/Direction3D.hpp>
30 #include <com/sun/star/drawing/Position3D.hpp>
31 #include <com/sun/star/drawing/CameraGeometry.hpp>
32 #include <com/sun/star/drawing/DoubleSequence.hpp>
33 #include <tools/gen.hxx>
34 #include <xmloff/shapeexport.hxx>
35 #include "sdpropls.hxx"
36 #include <tools/debug.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <xmloff/xmlexp.hxx>
39 #include <xmloff/xmluconv.hxx>
40 #include "xexptran.hxx"
41 #include <xmloff/xmltoken.hxx>
42 #include <basegfx/vector/b3dvector.hxx>
43 #include <xmloff/xmlnmspe.hxx>
44 #include <basegfx/polygon/b3dpolypolygon.hxx>
45 #include <basegfx/polygon/b3dpolypolygontools.hxx>
46 #include <basegfx/matrix/b3dhommatrix.hxx>
47 #include <basegfx/polygon/b2dpolypolygon.hxx>
48 #include <basegfx/polygon/b2dpolypolygontools.hxx>
49 
50 using ::rtl::OUString;
51 using ::rtl::OUStringBuffer;
52 
53 using namespace ::com::sun::star;
54 using namespace ::xmloff::token;
55 
56 
57 //////////////////////////////////////////////////////////////////////////////
58 
59 void XMLShapeExport::ImpExport3DSceneShape( const uno::Reference< drawing::XShape >& xShape, XmlShapeType, sal_Int32 nFeatures, awt::Point* pRefPoint)
60 {
61 	uno::Reference< drawing::XShapes > xShapes(xShape, uno::UNO_QUERY);
62 	if(xShapes.is() && xShapes->getCount())
63 	{
64 		uno::Reference< beans::XPropertySet > xPropSet( xShape, uno::UNO_QUERY );
65 		DBG_ASSERT( xPropSet.is(), "XMLShapeExport::ImpExport3DSceneShape can't export a scene without a propertyset" );
66 		if( xPropSet.is() )
67 		{
68 			// Transformation
69 			ImpExportNewTrans(xPropSet, nFeatures, pRefPoint);
70 
71 			// 3d attributes
72 			export3DSceneAttributes( xPropSet );
73 
74 			// write 3DScene shape
75 			sal_Bool bCreateNewline( (nFeatures & SEF_EXPORT_NO_WS) == 0 ); // #86116#/#92210#
76 			SvXMLElementExport aOBJ( mrExport, XML_NAMESPACE_DR3D, XML_SCENE, bCreateNewline, sal_True);
77 
78 			ImpExportDescription( xShape ); // #i68101#
79 			ImpExportEvents( xShape );
80 
81 			// write 3DSceneLights
82 			export3DLamps( xPropSet );
83 
84 			// #89764# if export of position is supressed for group shape,
85 			// positions of contained objects should be written relative to
86 			// the upper left edge of the group.
87 			awt::Point aUpperLeft;
88 
89 			if(!(nFeatures & SEF_EXPORT_POSITION))
90 			{
91 				nFeatures |= SEF_EXPORT_POSITION;
92 				aUpperLeft = xShape->getPosition();
93 				pRefPoint = &aUpperLeft;
94 			}
95 
96 			// write members
97 			exportShapes( xShapes, nFeatures, pRefPoint );
98 		}
99 	}
100 }
101 
102 //////////////////////////////////////////////////////////////////////////////
103 
104 void XMLShapeExport::ImpExport3DShape(
105 	const uno::Reference< drawing::XShape >& xShape,
106 	XmlShapeType eShapeType, sal_Int32 /* nFeatures = SEF_DEFAULT */, awt::Point* /*pRefPoint = NULL */)
107 {
108 	const uno::Reference< beans::XPropertySet > xPropSet(xShape, uno::UNO_QUERY);
109 	if(xPropSet.is())
110 	{
111 		OUString aStr;
112 		OUStringBuffer sStringBuffer;
113 
114 		// transformation (UNO_NAME_3D_TRANSFORM_MATRIX == "D3DTransformMatrix")
115 		uno::Any aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DTransformMatrix")));
116 		drawing::HomogenMatrix xHomMat;
117 		aAny >>= xHomMat;
118 		SdXMLImExTransform3D aTransform;
119 		aTransform.AddHomogenMatrix(xHomMat);
120 		if(aTransform.NeedsAction())
121 			mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_TRANSFORM, aTransform.GetExportString(mrExport.GetMM100UnitConverter()));
122 
123 		switch(eShapeType)
124 		{
125 			case XmlShapeTypeDraw3DCubeObject:
126 			{
127 				// write 3DCube shape
128 				SvXMLElementExport aOBJ(mrExport, XML_NAMESPACE_DR3D, XML_CUBE, sal_True, sal_True);
129 
130 				// minEdge
131 				aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DPosition")));
132 				drawing::Position3D aPosition3D;
133 				aAny >>= aPosition3D;
134 				::basegfx::B3DVector aPos3D(aPosition3D.PositionX, aPosition3D.PositionY, aPosition3D.PositionZ);
135 
136 				// maxEdge
137 				aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSize")));
138 				drawing::Direction3D aDirection3D;
139 				aAny >>= aDirection3D;
140 				::basegfx::B3DVector aDir3D(aDirection3D.DirectionX, aDirection3D.DirectionY, aDirection3D.DirectionZ);
141 
142 				// transform maxEdge from distance to pos
143 				aDir3D = aPos3D + aDir3D;
144 
145 				// write minEdge
146 				if(aPos3D != ::basegfx::B3DVector(-2500.0, -2500.0, -2500.0)) // write only when not default
147 				{
148 					mrExport.GetMM100UnitConverter().convertB3DVector(sStringBuffer, aPos3D);
149 					aStr = sStringBuffer.makeStringAndClear();
150 					mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_MIN_EDGE, aStr);
151 				}
152 
153 				// write maxEdge
154 				if(aDir3D != ::basegfx::B3DVector(2500.0, 2500.0, 2500.0)) // write only when not default
155 				{
156 					mrExport.GetMM100UnitConverter().convertB3DVector(sStringBuffer, aDir3D);
157 					aStr = sStringBuffer.makeStringAndClear();
158 					mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_MAX_EDGE, aStr);
159 				}
160 
161 				break;
162 			}
163 			case XmlShapeTypeDraw3DSphereObject:
164 			{
165 				// write 3DSphere shape
166 				SvXMLElementExport aOBJ(mrExport, XML_NAMESPACE_DR3D, XML_SPHERE, sal_True, sal_True);
167 
168 				// Center
169 				aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DPosition")));
170 				drawing::Position3D aPosition3D;
171 				aAny >>= aPosition3D;
172 				::basegfx::B3DVector aPos3D(aPosition3D.PositionX, aPosition3D.PositionY, aPosition3D.PositionZ);
173 
174 				// Size
175 				aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSize")));
176 				drawing::Direction3D aDirection3D;
177 				aAny >>= aDirection3D;
178 				::basegfx::B3DVector aDir3D(aDirection3D.DirectionX, aDirection3D.DirectionY, aDirection3D.DirectionZ);
179 
180 				// write Center
181 				if(aPos3D != ::basegfx::B3DVector(0.0, 0.0, 0.0)) // write only when not default
182 				{
183 					mrExport.GetMM100UnitConverter().convertB3DVector(sStringBuffer, aPos3D);
184 					aStr = sStringBuffer.makeStringAndClear();
185 					mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_CENTER, aStr);
186 				}
187 
188 				// write Size
189 				if(aDir3D != ::basegfx::B3DVector(5000.0, 5000.0, 5000.0)) // write only when not default
190 				{
191 					mrExport.GetMM100UnitConverter().convertB3DVector(sStringBuffer, aDir3D);
192 					aStr = sStringBuffer.makeStringAndClear();
193 					mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_SIZE, aStr);
194 				}
195 
196 				break;
197 			}
198 			case XmlShapeTypeDraw3DLatheObject:
199 			case XmlShapeTypeDraw3DExtrudeObject:
200 			{
201                 // write special 3DLathe/3DExtrude attributes, get 3D PolyPolygon as drawing::PolyPolygonShape3D
202                 aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DPolyPolygon3D")));
203                 drawing::PolyPolygonShape3D xPolyPolygon3D;
204                 aAny >>= xPolyPolygon3D;
205 
206                 // convert to 3D PolyPolygon
207                 const basegfx::B3DPolyPolygon aPolyPolygon3D(
208                     basegfx::tools::UnoPolyPolygonShape3DToB3DPolyPolygon(
209                         xPolyPolygon3D));
210 
211                 // convert to 2D PolyPolygon using identity 3D transformation (just grep X and Y)
212                 const basegfx::B3DHomMatrix aB3DHomMatrixFor2DConversion;
213                 const basegfx::B2DPolyPolygon aPolyPolygon(
214                     basegfx::tools::createB2DPolyPolygonFromB3DPolyPolygon(
215                         aPolyPolygon3D,
216                         aB3DHomMatrixFor2DConversion));
217 
218                 // get 2D range of it
219                 const basegfx::B2DRange aPolyPolygonRange(aPolyPolygon.getB2DRange());
220 
221                 // export ViewBox
222                 SdXMLImExViewBox aViewBox(
223                     aPolyPolygonRange.getMinX(),
224                     aPolyPolygonRange.getMinY(),
225                     aPolyPolygonRange.getWidth(),
226                     aPolyPolygonRange.getHeight());
227 
228                 mrExport.AddAttribute(XML_NAMESPACE_SVG, XML_VIEWBOX, aViewBox.GetExportString());
229 
230                 // prepare svg:d string
231                 const ::rtl::OUString aPolygonString(
232                     basegfx::tools::exportToSvgD(
233                         aPolyPolygon,
234                         true,           // bUseRelativeCoordinates
235                         false,          // bDetectQuadraticBeziers TTTT: not used in old, but maybe activated now
236                         true));         // bHandleRelativeNextPointCompatible
237 
238                 // write point array
239                 mrExport.AddAttribute(XML_NAMESPACE_SVG, XML_D, aPolygonString);
240 
241 				if(eShapeType == XmlShapeTypeDraw3DLatheObject)
242 				{
243 					// write 3DLathe shape
244 					SvXMLElementExport aOBJ(mrExport, XML_NAMESPACE_DR3D, XML_ROTATE, sal_True, sal_True);
245 				}
246 				else
247 				{
248 					// write 3DExtrude shape
249 					SvXMLElementExport aOBJ(mrExport, XML_NAMESPACE_DR3D, XML_EXTRUDE, sal_True, sal_True);
250 				}
251 				break;
252 			}
253 			default:
254 				break;
255 		}
256 	}
257 }
258 
259 //////////////////////////////////////////////////////////////////////////////
260 
261 /** helper for chart that adds all attributes of a 3d scene element to the export */
262 void XMLShapeExport::export3DSceneAttributes( const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet >& xPropSet )
263 {
264 	OUString aStr;
265 	OUStringBuffer sStringBuffer;
266 
267 	// world transformation (UNO_NAME_3D_TRANSFORM_MATRIX == "D3DTransformMatrix")
268 	uno::Any aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DTransformMatrix")));
269 	drawing::HomogenMatrix xHomMat;
270 	aAny >>= xHomMat;
271 	SdXMLImExTransform3D aTransform;
272 	aTransform.AddHomogenMatrix(xHomMat);
273 	if(aTransform.NeedsAction())
274 		mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_TRANSFORM, aTransform.GetExportString(mrExport.GetMM100UnitConverter()));
275 
276 	// VRP, VPN, VUP
277 	aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DCameraGeometry")));
278 	drawing::CameraGeometry aCamGeo;
279 	aAny >>= aCamGeo;
280 
281 	::basegfx::B3DVector aVRP(aCamGeo.vrp.PositionX, aCamGeo.vrp.PositionY, aCamGeo.vrp.PositionZ);
282 	if(aVRP != ::basegfx::B3DVector(0.0, 0.0, 1.0)) // write only when not default
283 	{
284 		mrExport.GetMM100UnitConverter().convertB3DVector(sStringBuffer, aVRP);
285 		aStr = sStringBuffer.makeStringAndClear();
286 		mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_VRP, aStr);
287 	}
288 
289 	::basegfx::B3DVector aVPN(aCamGeo.vpn.DirectionX, aCamGeo.vpn.DirectionY, aCamGeo.vpn.DirectionZ);
290 	if(aVPN != ::basegfx::B3DVector(0.0, 0.0, 1.0)) // write only when not default
291 	{
292 		mrExport.GetMM100UnitConverter().convertB3DVector(sStringBuffer, aVPN);
293 		aStr = sStringBuffer.makeStringAndClear();
294 		mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_VPN, aStr);
295 	}
296 
297 	::basegfx::B3DVector aVUP(aCamGeo.vup.DirectionX, aCamGeo.vup.DirectionY, aCamGeo.vup.DirectionZ);
298 	if(aVUP != ::basegfx::B3DVector(0.0, 1.0, 0.0)) // write only when not default
299 	{
300 		mrExport.GetMM100UnitConverter().convertB3DVector(sStringBuffer, aVUP);
301 		aStr = sStringBuffer.makeStringAndClear();
302 		mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_VUP, aStr);
303 	}
304 
305 	// projection "D3DScenePerspective" drawing::ProjectionMode
306 	aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DScenePerspective")));
307 	drawing::ProjectionMode xPrjMode;
308 	aAny >>= xPrjMode;
309 	if(xPrjMode == drawing::ProjectionMode_PARALLEL)
310 		aStr = GetXMLToken(XML_PARALLEL);
311 	else
312 		aStr = GetXMLToken(XML_PERSPECTIVE);
313 	mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_PROJECTION, aStr);
314 
315 	// distance
316 	aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneDistance")));
317 	sal_Int32 nDistance = 0;
318 	aAny >>= nDistance;
319 	mrExport.GetMM100UnitConverter().convertMeasure(sStringBuffer, nDistance);
320 	aStr = sStringBuffer.makeStringAndClear();
321 	mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_DISTANCE, aStr);
322 
323 	// focalLength
324 	aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneFocalLength")));
325 	sal_Int32 nFocalLength = 0;
326 	aAny >>= nFocalLength;
327 	mrExport.GetMM100UnitConverter().convertMeasure(sStringBuffer, nFocalLength);
328 	aStr = sStringBuffer.makeStringAndClear();
329 	mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_FOCAL_LENGTH, aStr);
330 
331 	// shadowSlant
332 	aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneShadowSlant")));
333 	sal_Int16 nShadowSlant = 0;
334 	aAny >>= nShadowSlant;
335 	mrExport.GetMM100UnitConverter().convertNumber(sStringBuffer, (sal_Int32)nShadowSlant);
336 	aStr = sStringBuffer.makeStringAndClear();
337 	mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_SHADOW_SLANT, aStr);
338 
339 	// shadeMode
340 	aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneShadeMode")));
341 	drawing::ShadeMode xShadeMode;
342 	if(aAny >>= xShadeMode)
343 	{
344 		if(xShadeMode == drawing::ShadeMode_FLAT)
345 			aStr = GetXMLToken(XML_FLAT);
346 		else if(xShadeMode == drawing::ShadeMode_PHONG)
347 			aStr = GetXMLToken(XML_PHONG);
348 		else if(xShadeMode == drawing::ShadeMode_SMOOTH)
349 			aStr = GetXMLToken(XML_GOURAUD);
350 		else
351 			aStr = GetXMLToken(XML_DRAFT);
352 	}
353 	else
354 	{
355 		// ShadeMode enum not there, write default
356 		aStr = GetXMLToken(XML_GOURAUD);
357 	}
358 	mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_SHADE_MODE, aStr);
359 
360 	// ambientColor
361 	aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneAmbientColor")));
362 	sal_Int32 aColTemp = 0;
363 	Color aAmbientColor;
364 	aAny >>= aColTemp; aAmbientColor.SetColor(aColTemp);
365 	mrExport.GetMM100UnitConverter().convertColor(sStringBuffer, aAmbientColor);
366 	aStr = sStringBuffer.makeStringAndClear();
367 	mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_AMBIENT_COLOR, aStr);
368 
369 	// lightingMode
370 	aAny = xPropSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneTwoSidedLighting")));
371 	sal_Bool bTwoSidedLighting = false;
372 	aAny >>= bTwoSidedLighting;
373 	mrExport.GetMM100UnitConverter().convertBool(sStringBuffer, bTwoSidedLighting);
374 	aStr = sStringBuffer.makeStringAndClear();
375 	mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_LIGHTING_MODE, aStr);
376 }
377 
378 /** helper for chart that exports all lamps from the propertyset */
379 void XMLShapeExport::export3DLamps( const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet >& xPropSet )
380 {
381 	// write lamps 1..8 as content
382 	OUString aStr;
383 	OUStringBuffer sStringBuffer;
384 
385 	const OUString aColorPropName(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightColor") );
386 	const OUString aDirectionPropName(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightDirection") );
387 	const OUString aLightOnPropName(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightOn") );
388 
389 	OUString aPropName;
390 	OUString aIndexStr;
391 	sal_Int32 aColTemp = 0;
392 	Color aLightColor;
393 	::basegfx::B3DVector aLightDirection;
394 	drawing::Direction3D xLightDir;
395 	sal_Bool bLightOnOff = false;
396 	for(sal_Int32 nLamp = 1; nLamp <= 8; nLamp++)
397 	{
398 		aIndexStr = OUString::valueOf( nLamp );
399 
400 		// lightcolor
401 		aPropName = aColorPropName;
402 		aPropName += aIndexStr;
403 		xPropSet->getPropertyValue( aPropName ) >>= aColTemp;
404 		aLightColor.SetColor(aColTemp);
405 		mrExport.GetMM100UnitConverter().convertColor(sStringBuffer, aLightColor);
406 		aStr = sStringBuffer.makeStringAndClear();
407 		mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_DIFFUSE_COLOR, aStr);
408 
409 		// lightdirection
410 		aPropName = aDirectionPropName;
411 		aPropName += aIndexStr;
412 		xPropSet->getPropertyValue(aPropName) >>= xLightDir;
413 		aLightDirection = ::basegfx::B3DVector(xLightDir.DirectionX, xLightDir.DirectionY, xLightDir.DirectionZ);
414 		mrExport.GetMM100UnitConverter().convertB3DVector(sStringBuffer, aLightDirection);
415 		aStr = sStringBuffer.makeStringAndClear();
416 		mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_DIRECTION, aStr);
417 
418 		// lighton
419 		aPropName = aLightOnPropName;
420 		aPropName += aIndexStr;
421 		xPropSet->getPropertyValue(aPropName) >>= bLightOnOff;
422 		mrExport.GetMM100UnitConverter().convertBool(sStringBuffer, bLightOnOff);
423 		aStr = sStringBuffer.makeStringAndClear();
424 		mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_ENABLED, aStr);
425 
426 		// specular
427 		mrExport.AddAttribute(XML_NAMESPACE_DR3D, XML_SPECULAR,
428 			nLamp == 1 ? XML_TRUE : XML_FALSE);
429 
430 		// write light entry
431 		SvXMLElementExport aOBJ(mrExport, XML_NAMESPACE_DR3D, XML_LIGHT, sal_True, sal_True);
432 	}
433 }
434 
435 //////////////////////////////////////////////////////////////////////////////
436