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 :
21 : #include <sax/tools/converter.hxx>
22 :
23 : #include "ximp3dscene.hxx"
24 : #include <xmloff/xmluconv.hxx>
25 : #include "xexptran.hxx"
26 : #include <xmloff/xmltoken.hxx>
27 : #include "xmloff/xmlnmspe.hxx"
28 : #include <com/sun/star/drawing/Direction3D.hpp>
29 : #include <com/sun/star/drawing/CameraGeometry.hpp>
30 : #include "eventimp.hxx"
31 : #include "descriptionimp.hxx"
32 :
33 : using ::rtl::OUString;
34 : using ::rtl::OUStringBuffer;
35 :
36 : using namespace ::com::sun::star;
37 : using namespace ::xmloff::token;
38 :
39 : //////////////////////////////////////////////////////////////////////////////
40 : // dr3d:3dlight context
41 :
42 0 : SdXML3DLightContext::SdXML3DLightContext(
43 : SvXMLImport& rImport,
44 : sal_uInt16 nPrfx,
45 : const rtl::OUString& rLName,
46 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >& xAttrList)
47 : : SvXMLImportContext( rImport, nPrfx, rLName),
48 : maDiffuseColor(0x00000000),
49 : maDirection(0.0, 0.0, 1.0),
50 : mbEnabled(false),
51 0 : mbSpecular(false)
52 : {
53 : // read attributes for the 3DScene
54 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
55 0 : for(sal_Int16 i=0; i < nAttrCount; i++)
56 : {
57 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
58 0 : OUString aLocalName;
59 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
60 0 : OUString sValue = xAttrList->getValueByIndex( i );
61 0 : const SvXMLTokenMap& rAttrTokenMap = GetImport().GetShapeImport()->Get3DLightAttrTokenMap();
62 :
63 0 : switch(rAttrTokenMap.Get(nPrefix, aLocalName))
64 : {
65 : case XML_TOK_3DLIGHT_DIFFUSE_COLOR:
66 : {
67 0 : ::sax::Converter::convertColor(maDiffuseColor, sValue);
68 0 : break;
69 : }
70 : case XML_TOK_3DLIGHT_DIRECTION:
71 : {
72 0 : GetImport().GetMM100UnitConverter().convertB3DVector(maDirection, sValue);
73 0 : break;
74 : }
75 : case XML_TOK_3DLIGHT_ENABLED:
76 : {
77 0 : ::sax::Converter::convertBool(mbEnabled, sValue);
78 0 : break;
79 : }
80 : case XML_TOK_3DLIGHT_SPECULAR:
81 : {
82 0 : ::sax::Converter::convertBool(mbSpecular, sValue);
83 0 : break;
84 : }
85 : }
86 0 : }
87 0 : }
88 :
89 0 : SdXML3DLightContext::~SdXML3DLightContext()
90 : {
91 0 : }
92 :
93 : //////////////////////////////////////////////////////////////////////////////
94 :
95 0 : TYPEINIT1( SdXML3DSceneShapeContext, SdXMLShapeContext );
96 :
97 0 : SdXML3DSceneShapeContext::SdXML3DSceneShapeContext(
98 : SvXMLImport& rImport,
99 : sal_uInt16 nPrfx,
100 : const OUString& rLocalName,
101 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList,
102 : uno::Reference< drawing::XShapes >& rShapes,
103 : sal_Bool bTemporaryShapes)
104 0 : : SdXMLShapeContext( rImport, nPrfx, rLocalName, xAttrList, rShapes, bTemporaryShapes ), SdXML3DSceneAttributesHelper( rImport )
105 : {
106 0 : }
107 :
108 : //////////////////////////////////////////////////////////////////////////////
109 :
110 0 : SdXML3DSceneShapeContext::~SdXML3DSceneShapeContext()
111 : {
112 0 : }
113 :
114 : //////////////////////////////////////////////////////////////////////////////
115 :
116 0 : void SdXML3DSceneShapeContext::StartElement(const uno::Reference< xml::sax::XAttributeList>& xAttrList)
117 : {
118 : // create new 3DScene shape and add it to rShapes, use it
119 : // as base for the new 3DScene import
120 0 : AddShape( "com.sun.star.drawing.Shape3DSceneObject" );
121 0 : if( mxShape.is() )
122 : {
123 0 : SetStyle();
124 :
125 0 : mxChildren = uno::Reference< drawing::XShapes >::query( mxShape );
126 0 : if( mxChildren.is() )
127 0 : GetImport().GetShapeImport()->pushGroupForSorting( mxChildren );
128 :
129 0 : SetLayer();
130 :
131 : // set pos, size, shear and rotate
132 0 : SetTransformation();
133 : }
134 :
135 : // read attributes for the 3DScene
136 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
137 0 : for(sal_Int16 i=0; i < nAttrCount; i++)
138 : {
139 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
140 0 : OUString aLocalName;
141 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
142 0 : OUString sValue = xAttrList->getValueByIndex( i );
143 0 : processSceneAttribute( nPrefix, aLocalName, sValue );
144 0 : }
145 :
146 : // #91047# call parent function is missing here, added it
147 0 : if(mxShape.is())
148 : {
149 : // call parent
150 0 : SdXMLShapeContext::StartElement(xAttrList);
151 : }
152 0 : }
153 :
154 : //////////////////////////////////////////////////////////////////////////////
155 :
156 0 : void SdXML3DSceneShapeContext::EndElement()
157 : {
158 0 : if(mxShape.is())
159 : {
160 0 : uno::Reference< beans::XPropertySet > xPropSet(mxShape, uno::UNO_QUERY);
161 0 : if(xPropSet.is())
162 : {
163 0 : setSceneAttributes( xPropSet );
164 : }
165 :
166 0 : if( mxChildren.is() )
167 0 : GetImport().GetShapeImport()->popGroupAndSort();
168 :
169 : // call parent
170 0 : SdXMLShapeContext::EndElement();
171 : }
172 0 : }
173 :
174 : //////////////////////////////////////////////////////////////////////////////
175 :
176 0 : SvXMLImportContext* SdXML3DSceneShapeContext::CreateChildContext( sal_uInt16 nPrefix,
177 : const OUString& rLocalName,
178 : const uno::Reference< xml::sax::XAttributeList>& xAttrList )
179 : {
180 0 : SvXMLImportContext* pContext = 0L;
181 :
182 : // #i68101#
183 0 : if( nPrefix == XML_NAMESPACE_SVG &&
184 0 : (IsXMLToken( rLocalName, XML_TITLE ) || IsXMLToken( rLocalName, XML_DESC ) ) )
185 : {
186 0 : pContext = new SdXMLDescriptionContext( GetImport(), nPrefix, rLocalName, xAttrList, mxShape );
187 : }
188 0 : else if( nPrefix == XML_NAMESPACE_OFFICE && IsXMLToken( rLocalName, XML_EVENT_LISTENERS ) )
189 : {
190 0 : pContext = new SdXMLEventsContext( GetImport(), nPrefix, rLocalName, xAttrList, mxShape );
191 : }
192 : // look for local light context first
193 0 : else if(nPrefix == XML_NAMESPACE_DR3D && IsXMLToken( rLocalName, XML_LIGHT ) )
194 : {
195 : // dr3d:light inside dr3d:scene context
196 0 : pContext = create3DLightContext( nPrefix, rLocalName, xAttrList );
197 : }
198 :
199 : // call GroupChildContext function at common ShapeImport
200 0 : if(!pContext)
201 : {
202 0 : pContext = GetImport().GetShapeImport()->Create3DSceneChildContext(
203 0 : GetImport(), nPrefix, rLocalName, xAttrList, mxChildren);
204 : }
205 :
206 : // call parent when no own context was created
207 0 : if(!pContext)
208 : {
209 : pContext = SvXMLImportContext::CreateChildContext(
210 0 : nPrefix, rLocalName, xAttrList);
211 : }
212 :
213 0 : return pContext;
214 : }
215 :
216 : //////////////////////////////////////////////////////////////////////////////
217 :
218 0 : SdXML3DSceneAttributesHelper::SdXML3DSceneAttributesHelper( SvXMLImport& rImporter )
219 : : mrImport( rImporter ),
220 : mbSetTransform( sal_False ),
221 : mxPrjMode(drawing::ProjectionMode_PERSPECTIVE),
222 : mnDistance(1000),
223 : mnFocalLength(1000),
224 : mnShadowSlant(0),
225 : mxShadeMode(drawing::ShadeMode_SMOOTH),
226 : maAmbientColor(0x00666666),
227 : mbLightingMode(false),
228 : maVRP(0.0, 0.0, 1.0),
229 : maVPN(0.0, 0.0, 1.0),
230 : maVUP(0.0, 1.0, 0.0),
231 : mbVRPUsed(sal_False),
232 : mbVPNUsed(sal_False),
233 0 : mbVUPUsed(sal_False)
234 : {
235 0 : }
236 :
237 0 : SdXML3DSceneAttributesHelper::~SdXML3DSceneAttributesHelper()
238 : {
239 : // release remembered light contexts, they are no longer needed
240 0 : for ( size_t i = maList.size(); i > 0; )
241 0 : maList[ --i ]->ReleaseRef();
242 0 : maList.clear();
243 0 : }
244 :
245 : /** creates a 3d ligth context and adds it to the internal list for later processing */
246 0 : SvXMLImportContext * SdXML3DSceneAttributesHelper::create3DLightContext( sal_uInt16 nPrfx, const rtl::OUString& rLName, const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >& xAttrList)
247 : {
248 0 : SvXMLImportContext* pContext = new SdXML3DLightContext(mrImport, nPrfx, rLName, xAttrList);
249 :
250 : // remember SdXML3DLightContext for later evaluation
251 0 : if(pContext)
252 : {
253 0 : pContext->AddRef();
254 0 : maList.push_back( (SdXML3DLightContext*)pContext );
255 : }
256 :
257 0 : return pContext;
258 : }
259 :
260 : /** this should be called for each scene attribute */
261 0 : void SdXML3DSceneAttributesHelper::processSceneAttribute( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName, const ::rtl::OUString& rValue )
262 : {
263 0 : if( XML_NAMESPACE_DR3D == nPrefix )
264 : {
265 0 : if( IsXMLToken( rLocalName, XML_TRANSFORM ) )
266 : {
267 0 : SdXMLImExTransform3D aTransform(rValue, mrImport.GetMM100UnitConverter());
268 0 : if(aTransform.NeedsAction())
269 0 : mbSetTransform = aTransform.GetFullHomogenTransform(mxHomMat);
270 0 : return;
271 : }
272 0 : else if( IsXMLToken( rLocalName, XML_VRP ) )
273 : {
274 0 : ::basegfx::B3DVector aNewVec;
275 0 : mrImport.GetMM100UnitConverter().convertB3DVector(aNewVec, rValue);
276 :
277 0 : if(aNewVec != maVRP)
278 : {
279 0 : maVRP = aNewVec;
280 0 : mbVRPUsed = sal_True;
281 : }
282 0 : return;
283 : }
284 0 : else if( IsXMLToken( rLocalName, XML_VPN ) )
285 : {
286 0 : ::basegfx::B3DVector aNewVec;
287 0 : mrImport.GetMM100UnitConverter().convertB3DVector(aNewVec, rValue);
288 :
289 0 : if(aNewVec != maVPN)
290 : {
291 0 : maVPN = aNewVec;
292 0 : mbVPNUsed = sal_True;
293 : }
294 0 : return;
295 : }
296 0 : else if( IsXMLToken( rLocalName, XML_VUP ) )
297 : {
298 0 : ::basegfx::B3DVector aNewVec;
299 0 : mrImport.GetMM100UnitConverter().convertB3DVector(aNewVec, rValue);
300 :
301 0 : if(aNewVec != maVUP)
302 : {
303 0 : maVUP = aNewVec;
304 0 : mbVUPUsed = sal_True;
305 : }
306 0 : return;
307 : }
308 0 : else if( IsXMLToken( rLocalName, XML_PROJECTION ) )
309 : {
310 0 : if( IsXMLToken( rValue, XML_PARALLEL ) )
311 0 : mxPrjMode = drawing::ProjectionMode_PARALLEL;
312 : else
313 0 : mxPrjMode = drawing::ProjectionMode_PERSPECTIVE;
314 0 : return;
315 : }
316 0 : else if( IsXMLToken( rLocalName, XML_DISTANCE ) )
317 : {
318 0 : mrImport.GetMM100UnitConverter().convertMeasureToCore(mnDistance,
319 0 : rValue);
320 0 : return;
321 : }
322 0 : else if( IsXMLToken( rLocalName, XML_FOCAL_LENGTH ) )
323 : {
324 0 : mrImport.GetMM100UnitConverter().convertMeasureToCore(mnFocalLength,
325 0 : rValue);
326 0 : return;
327 : }
328 0 : else if( IsXMLToken( rLocalName, XML_SHADOW_SLANT ) )
329 : {
330 0 : ::sax::Converter::convertNumber(mnShadowSlant, rValue);
331 0 : return;
332 : }
333 0 : else if( IsXMLToken( rLocalName, XML_SHADE_MODE ) )
334 : {
335 0 : if( IsXMLToken( rValue, XML_FLAT ) )
336 0 : mxShadeMode = drawing::ShadeMode_FLAT;
337 0 : else if( IsXMLToken( rValue, XML_PHONG ) )
338 0 : mxShadeMode = drawing::ShadeMode_PHONG;
339 0 : else if( IsXMLToken( rValue, XML_GOURAUD ) )
340 0 : mxShadeMode = drawing::ShadeMode_SMOOTH;
341 : else
342 0 : mxShadeMode = drawing::ShadeMode_DRAFT;
343 0 : return;
344 : }
345 0 : else if( IsXMLToken( rLocalName, XML_AMBIENT_COLOR ) )
346 : {
347 0 : ::sax::Converter::convertColor(maAmbientColor, rValue);
348 0 : return;
349 : }
350 0 : else if( IsXMLToken( rLocalName, XML_LIGHTING_MODE ) )
351 : {
352 0 : ::sax::Converter::convertBool(mbLightingMode, rValue);
353 0 : return;
354 : }
355 : }
356 : }
357 :
358 : /** this sets the scene attributes at this propertyset */
359 0 : void SdXML3DSceneAttributesHelper::setSceneAttributes( const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet >& xPropSet )
360 : {
361 0 : uno::Any aAny;
362 :
363 : // world transformation
364 0 : if(mbSetTransform)
365 : {
366 0 : aAny <<= mxHomMat;
367 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DTransformMatrix")), aAny);
368 : }
369 :
370 : // distance
371 0 : aAny <<= mnDistance;
372 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneDistance")), aAny);
373 :
374 : // focalLength
375 0 : aAny <<= mnFocalLength;
376 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneFocalLength")), aAny);
377 :
378 : // shadowSlant
379 0 : aAny <<= (sal_Int16)mnShadowSlant;
380 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneShadowSlant")), aAny);
381 :
382 : // shadeMode
383 0 : aAny <<= mxShadeMode;
384 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneShadeMode")), aAny);
385 :
386 : // ambientColor
387 0 : aAny <<= maAmbientColor;
388 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneAmbientColor")), aAny);
389 :
390 : // lightingMode
391 0 : aAny <<= mbLightingMode;
392 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneTwoSidedLighting")), aAny);
393 :
394 0 : if( !maList.empty() )
395 : {
396 0 : uno::Any aAny2;
397 0 : uno::Any aAny3;
398 :
399 : // set lights
400 0 : for( size_t a = 0; a < maList.size(); a++)
401 : {
402 0 : SdXML3DLightContext* pCtx = (SdXML3DLightContext*)maList[ a ];
403 :
404 : // set anys
405 0 : aAny <<= pCtx->GetDiffuseColor();
406 0 : drawing::Direction3D xLightDir;
407 0 : xLightDir.DirectionX = pCtx->GetDirection().getX();
408 0 : xLightDir.DirectionY = pCtx->GetDirection().getY();
409 0 : xLightDir.DirectionZ = pCtx->GetDirection().getZ();
410 0 : aAny2 <<= xLightDir;
411 0 : aAny3 <<= pCtx->GetEnabled();
412 :
413 0 : switch(a)
414 : {
415 : case 0:
416 : {
417 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightColor1")), aAny);
418 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightDirection1")), aAny2);
419 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightOn1")), aAny3);
420 0 : break;
421 : }
422 : case 1:
423 : {
424 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightColor2")), aAny);
425 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightDirection2")), aAny2);
426 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightOn2")), aAny3);
427 0 : break;
428 : }
429 : case 2:
430 : {
431 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightColor3")), aAny);
432 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightDirection3")), aAny2);
433 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightOn3")), aAny3);
434 0 : break;
435 : }
436 : case 3:
437 : {
438 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightColor4")), aAny);
439 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightDirection4")), aAny2);
440 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightOn4")), aAny3);
441 0 : break;
442 : }
443 : case 4:
444 : {
445 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightColor5")), aAny);
446 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightDirection5")), aAny2);
447 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightOn5")), aAny3);
448 0 : break;
449 : }
450 : case 5:
451 : {
452 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightColor6")), aAny);
453 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightDirection6")), aAny2);
454 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightOn6")), aAny3);
455 0 : break;
456 : }
457 : case 6:
458 : {
459 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightColor7")), aAny);
460 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightDirection7")), aAny2);
461 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightOn7")), aAny3);
462 0 : break;
463 : }
464 : case 7:
465 : {
466 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightColor8")), aAny);
467 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightDirection8")), aAny2);
468 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DSceneLightOn8")), aAny3);
469 0 : break;
470 : }
471 : }
472 0 : }
473 : }
474 :
475 : // CameraGeometry and camera settings
476 0 : drawing::CameraGeometry aCamGeo;
477 0 : aCamGeo.vrp.PositionX = maVRP.getX();
478 0 : aCamGeo.vrp.PositionY = maVRP.getY();
479 0 : aCamGeo.vrp.PositionZ = maVRP.getZ();
480 0 : aCamGeo.vpn.DirectionX = maVPN.getX();
481 0 : aCamGeo.vpn.DirectionY = maVPN.getY();
482 0 : aCamGeo.vpn.DirectionZ = maVPN.getZ();
483 0 : aCamGeo.vup.DirectionX = maVUP.getX();
484 0 : aCamGeo.vup.DirectionY = maVUP.getY();
485 0 : aCamGeo.vup.DirectionZ = maVUP.getZ();
486 0 : aAny <<= aCamGeo;
487 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DCameraGeometry")), aAny);
488 :
489 : // #91047# set drawing::ProjectionMode AFTER camera geometry is set
490 : // projection "D3DScenePerspective" drawing::ProjectionMode
491 0 : aAny <<= mxPrjMode;
492 0 : xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("D3DScenePerspective")), aAny);
493 0 : }
494 :
495 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|