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 #ifndef INCLUDED_XMLOFF_SHAPEIMPORT_HXX
21 #define INCLUDED_XMLOFF_SHAPEIMPORT_HXX
22 
23 #include <sal/config.h>
24 #include <xmloff/dllapi.h>
25 #include <com/sun/star/drawing/HomogenMatrix.hpp>
26 #include <com/sun/star/drawing/ProjectionMode.hpp>
27 #include <com/sun/star/drawing/ShadeMode.hpp>
28 #include <salhelper/simplereferenceobject.hxx>
29 #include <xmloff/xmlictxt.hxx>
30 
31 #include <basegfx/vector/b3dvector.hxx>
32 #include <vector>
33 #include <memory>
34 
35 namespace com { namespace sun { namespace star { namespace beans { class XPropertySet; } } } }
36 namespace com { namespace sun { namespace star { namespace drawing { class XShape; } } } }
37 namespace com { namespace sun { namespace star { namespace drawing { class XShapes; } } } }
38 namespace com { namespace sun { namespace star { namespace frame { class XModel; } } } }
39 namespace com { namespace sun { namespace star { namespace xml { namespace sax { class XAttributeList; } } } } }
40 
41 class SvXMLImport;
42 class SvXMLTokenMap;
43 class SvXMLStylesContext;
44 class XMLSdPropHdlFactory;
45 class SvXMLImportPropertyMapper;
46 class XMLTableImport;
47 
48 
49 enum SdXMLGroupShapeElemTokenMap
50 {
51     XML_TOK_GROUP_GROUP,
52     XML_TOK_GROUP_RECT,
53     XML_TOK_GROUP_LINE,
54     XML_TOK_GROUP_CIRCLE,
55     XML_TOK_GROUP_ELLIPSE,
56     XML_TOK_GROUP_POLYGON,
57     XML_TOK_GROUP_POLYLINE,
58     XML_TOK_GROUP_PATH,
59 
60     XML_TOK_GROUP_CONTROL,
61     XML_TOK_GROUP_CONNECTOR,
62     XML_TOK_GROUP_MEASURE,
63     XML_TOK_GROUP_PAGE,
64     XML_TOK_GROUP_CAPTION,
65 
66     XML_TOK_GROUP_CHART,
67     XML_TOK_GROUP_3DSCENE,
68 
69     XML_TOK_GROUP_FRAME,
70 
71     XML_TOK_GROUP_CUSTOM_SHAPE,
72 
73     XML_TOK_GROUP_ANNOTATION,
74 
75     XML_TOK_GROUP_A
76 };
77 
78 enum SdXMLFrameShapeElemTokenMap
79 {
80     XML_TOK_FRAME_TEXT_BOX,
81     XML_TOK_FRAME_IMAGE,
82     XML_TOK_FRAME_OBJECT,
83     XML_TOK_FRAME_OBJECT_OLE,
84     XML_TOK_FRAME_PLUGIN,
85     XML_TOK_FRAME_FLOATING_FRAME,
86     XML_TOK_FRAME_APPLET,
87     XML_TOK_FRAME_TABLE
88 };
89 
90 enum SdXML3DSceneShapeElemTokenMap
91 {
92     XML_TOK_3DSCENE_3DSCENE,
93     XML_TOK_3DSCENE_3DCUBE,
94     XML_TOK_3DSCENE_3DSPHERE,
95     XML_TOK_3DSCENE_3DLATHE,
96     XML_TOK_3DSCENE_3DEXTRUDE
97 };
98 
99 enum SdXML3DObjectAttrTokenMap
100 {
101     XML_TOK_3DOBJECT_DRAWSTYLE_NAME,
102     XML_TOK_3DOBJECT_TRANSFORM
103 };
104 
105 enum SdXML3DPolygonBasedAttrTokenMap
106 {
107     XML_TOK_3DPOLYGONBASED_VIEWBOX,
108     XML_TOK_3DPOLYGONBASED_D
109 };
110 
111 enum SdXML3DCubeObjectAttrTokenMap
112 {
113     XML_TOK_3DCUBEOBJ_MINEDGE,
114     XML_TOK_3DCUBEOBJ_MAXEDGE
115 };
116 
117 enum SdXML3DSphereObjectAttrTokenMap
118 {
119     XML_TOK_3DSPHEREOBJ_CENTER,
120     XML_TOK_3DSPHEREOBJ_SIZE
121 };
122 
123 enum SdXML3DLightAttrTokenMap
124 {
125     XML_TOK_3DLIGHT_DIFFUSE_COLOR,
126     XML_TOK_3DLIGHT_DIRECTION,
127     XML_TOK_3DLIGHT_ENABLED,
128     XML_TOK_3DLIGHT_SPECULAR
129 };
130 
131 
132 // dr3d:3dlight context
133 
134 class SdXML3DLightContext final : public SvXMLImportContext
135 {
136     // local parameters which need to be read
137     sal_Int32                   maDiffuseColor;
138     ::basegfx::B3DVector        maDirection;
139     bool                        mbEnabled;
140     bool                        mbSpecular;
141 
142 public:
143     SdXML3DLightContext(
144         SvXMLImport& rImport,
145         sal_uInt16 nPrfx,
146         const OUString& rLName,
147         const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList);
148     virtual ~SdXML3DLightContext() override;
149 
GetDiffuseColor() const150     sal_Int32 GetDiffuseColor() const { return maDiffuseColor; }
GetDirection() const151     const ::basegfx::B3DVector& GetDirection() const { return maDirection; }
GetEnabled() const152     bool GetEnabled() const { return mbEnabled; }
153 };
154 
155 
156 class SdXML3DSceneAttributesHelper
157 {
158 protected:
159     SvXMLImport& mrImport;
160 
161     // list for local light contexts
162     ::std::vector< rtl::Reference< SdXML3DLightContext > >
163                                 maList;
164 
165     // local parameters which need to be read
166     css::drawing::HomogenMatrix mxHomMat;
167     bool                        mbSetTransform;
168 
169     css::drawing::ProjectionMode mxPrjMode;
170     sal_Int32                   mnDistance;
171     sal_Int32                   mnFocalLength;
172     sal_Int32                   mnShadowSlant;
173     css::drawing::ShadeMode     mxShadeMode;
174     sal_Int32                   maAmbientColor;
175     bool                        mbLightingMode;
176 
177     ::basegfx::B3DVector        maVRP;
178     ::basegfx::B3DVector        maVPN;
179     ::basegfx::B3DVector        maVUP;
180     bool                        mbVRPUsed;
181     bool                        mbVPNUsed;
182     bool                        mbVUPUsed;
183 
184 public:
185     SdXML3DSceneAttributesHelper( SvXMLImport& rImporter );
186 
187     /** creates a 3d light context and adds it to the internal list for later processing */
188     SvXMLImportContext * create3DLightContext( sal_uInt16 nPrfx, const OUString& rLName, const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList);
189 
190     /** this should be called for each scene attribute */
191     void processSceneAttribute( sal_uInt16 nPrefix, const OUString& rLocalName, const OUString& rValue );
192 
193     /** this sets the scene attributes at this propertyset */
194     void setSceneAttributes( const css::uno::Reference< css::beans::XPropertySet >& xPropSet );
195 };
196 
197 
198 class SvXMLShapeContext : public SvXMLImportContext
199 {
200 protected:
201     css::uno::Reference< css::drawing::XShape >   mxShape;
202     bool const                                    mbTemporaryShape;
203     OUString                                      msHyperlink;
204 
205 public:
SvXMLShapeContext(SvXMLImport & rImp,sal_uInt16 nPrfx,const OUString & rLName,bool bTemporaryShape)206     SvXMLShapeContext( SvXMLImport& rImp, sal_uInt16 nPrfx,
207         const OUString& rLName, bool bTemporaryShape ) : SvXMLImportContext( rImp, nPrfx, rLName ), mbTemporaryShape(bTemporaryShape) {}
208 
209 
getShape() const210     const css::uno::Reference< css::drawing::XShape >& getShape() const { return mxShape; }
211 
212     void setHyperlink( const OUString& rHyperlink );
213 };
214 
215 
216 struct XMLShapeImportHelperImpl;
217 struct XMLShapeImportPageContextImpl;
218 struct SdXMLEventContextData;
219 
220 class XMLOFF_DLLPUBLIC XMLShapeImportHelper : public salhelper::SimpleReferenceObject
221 {
222     std::unique_ptr<XMLShapeImportHelperImpl> mpImpl;
223 
224     std::shared_ptr<XMLShapeImportPageContextImpl> mpPageContext;
225 
226     // PropertySetMappers and factory
227     rtl::Reference<XMLSdPropHdlFactory>       mpSdPropHdlFactory;
228     rtl::Reference<SvXMLImportPropertyMapper> mpPropertySetMapper;
229     rtl::Reference<SvXMLImportPropertyMapper> mpPresPagePropsMapper;
230 
231     // contexts for Style and AutoStyle import
232     rtl::Reference<SvXMLStylesContext> mxStylesContext;
233     rtl::Reference<SvXMLStylesContext> mxAutoStylesContext;
234 
235     // contexts for xShape contents TokenMaps
236     std::unique_ptr<SvXMLTokenMap>              mpGroupShapeElemTokenMap;
237     std::unique_ptr<SvXMLTokenMap>              mpFrameShapeElemTokenMap;
238     std::unique_ptr<SvXMLTokenMap>              mp3DSceneShapeElemTokenMap;
239     std::unique_ptr<SvXMLTokenMap>              mp3DObjectAttrTokenMap;
240     std::unique_ptr<SvXMLTokenMap>              mp3DPolygonBasedAttrTokenMap;
241     std::unique_ptr<SvXMLTokenMap>              mp3DCubeObjectAttrTokenMap;
242     std::unique_ptr<SvXMLTokenMap>              mp3DSphereObjectAttrTokenMap;
243     std::unique_ptr<SvXMLTokenMap>              mp3DLightAttrTokenMap;
244 
245     rtl::Reference< XMLTableImport > mxShapeTableImport;
246 
247 protected:
248     SvXMLImport& mrImporter;
249 
250 public:
251     XMLShapeImportHelper( SvXMLImport& rImporter,
252         const css::uno::Reference< css::frame::XModel>& rModel,
253     SvXMLImportPropertyMapper *pExtMapper=nullptr );
254 
255     virtual ~XMLShapeImportHelper() override;
256 
257     SvXMLShapeContext* CreateGroupChildContext(
258         SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName,
259         const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList,
260         css::uno::Reference< css::drawing::XShapes > const & rShapes,
261         bool bTemporaryShape = false);
262 
263     SvXMLShapeContext* CreateFrameChildContext(
264         SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName,
265         const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList,
266         css::uno::Reference< css::drawing::XShapes > const & rShapes,
267         const css::uno::Reference< css::xml::sax::XAttributeList >& xFrameAttrList);
268     static SvXMLImportContextRef CreateFrameChildContext(
269         SvXMLImportContext *pThisContext, sal_uInt16 nPrefix, const OUString& rLocalName,
270         const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList );
271 
272     SvXMLShapeContext* Create3DSceneChildContext(
273         SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName,
274         const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList,
275         css::uno::Reference< css::drawing::XShapes > const & rShapes);
276 
277     const SvXMLTokenMap& GetGroupShapeElemTokenMap();
278     const SvXMLTokenMap& GetFrameShapeElemTokenMap();
279     const SvXMLTokenMap& Get3DSceneShapeElemTokenMap();
280     const SvXMLTokenMap& Get3DObjectAttrTokenMap();
281     const SvXMLTokenMap& Get3DPolygonBasedAttrTokenMap();
282     const SvXMLTokenMap& Get3DCubeObjectAttrTokenMap();
283     const SvXMLTokenMap& Get3DSphereObjectAttrTokenMap();
284     const SvXMLTokenMap& Get3DLightAttrTokenMap();
285 
286     // Styles and AutoStyles contexts
GetStylesContext() const287     SvXMLStylesContext* GetStylesContext() const { return mxStylesContext.get(); }
288     void SetStylesContext(SvXMLStylesContext* pNew);
GetAutoStylesContext() const289     SvXMLStylesContext* GetAutoStylesContext() const { return mxAutoStylesContext.get(); }
290     void SetAutoStylesContext(SvXMLStylesContext* pNew);
291 
292     // get factories and mappers
GetPropertySetMapper() const293     SvXMLImportPropertyMapper* GetPropertySetMapper() const { return mpPropertySetMapper.get(); }
GetPresPagePropsMapper() const294     SvXMLImportPropertyMapper* GetPresPagePropsMapper() const { return mpPresPagePropsMapper.get(); }
295 
296     // this function is called whenever the implementation classes like to add this new
297     // shape to the given XShapes.
298     virtual void addShape(
299         css::uno::Reference< css::drawing::XShape >& rShape,
300         const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList,
301         css::uno::Reference< css::drawing::XShapes >& rShapes);
302 
303     // this function is called whenever the implementation classes have finished importing
304     // a shape to the given XShapes. The shape is already inserted into its XShapes and
305     // all properties and styles are set.
306     virtual void finishShape(
307         css::uno::Reference< css::drawing::XShape >& rShape,
308         const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList,
309         css::uno::Reference< css::drawing::XShapes >& rShapes);
310 
311     // tdf#127791 help function for group shape events
312     void addShapeEvents(SdXMLEventContextData& rData);
313 
314     // helper functions processing groups after their component shapes are collected
315     // e.g. for z-order sorting or adding events to the group
316     void pushGroupForPostProcessing( css::uno::Reference< css::drawing::XShapes >& rShapes );
317     void popGroupAndPostProcess();
318 
319     void shapeWithZIndexAdded( css::uno::Reference< css::drawing::XShape > const & rShape,
320                                sal_Int32 nZIndex );
321     /// Updates the z-order of other shapes to be consistent again, needed due
322     /// to the removal of rShape.
323     void shapeRemoved(const css::uno::Reference<css::drawing::XShape>& rShape);
324 
325     void addShapeConnection( css::uno::Reference< css::drawing::XShape > const & rConnectorShape,
326                              bool bStart,
327                              const OUString& rDestShapeId,
328                              sal_Int32 nDestGlueId );
329 
330     /** adds a mapping for a glue point identifier from an xml file to the identifier created after inserting
331         the new glue point into the core. The saved mappings can be retrieved by getGluePointId() */
332     void addGluePointMapping( css::uno::Reference< css::drawing::XShape > const & xShape,
333                               sal_Int32 nSourceId, sal_Int32 nDestinnationId );
334 
335     /** moves all current DestinationId's for rXShape by n */
336     void moveGluePointMapping( const css::uno::Reference< css::drawing::XShape >& xShape, const sal_Int32 n );
337 
338     /** retrieves a mapping for a glue point identifier from the current xml file to the identifier created after
339         inserting the new glue point into the core. The mapping must be initialized first with addGluePointMapping() */
340     sal_Int32 getGluePointId( const css::uno::Reference< css::drawing::XShape >& xShape, sal_Int32 nSourceId );
341 
342     /** this method must be calling before the first shape is imported for the given page.
343         Calls to this method can be nested */
344     void startPage( css::uno::Reference< css::drawing::XShapes > const & rShapes );
345 
346     /** this method must be calling after the last shape is imported for the given page
347         Calls to this method can be nested */
348     void endPage( css::uno::Reference< css::drawing::XShapes > const & rShapes );
349 
350     void restoreConnections();
351 
352     /** creates a property mapper for external chaining */
353     static SvXMLImportPropertyMapper* CreateShapePropMapper(
354         const css::uno::Reference< css::frame::XModel>& rModel, SvXMLImport& rImport );
355 
356     /** defines if the import should increment the progress bar or not */
357     void enableHandleProgressBar();
358     bool IsHandleProgressBarEnabled() const;
359 
360     /** queries the capability of the current model to create presentation shapes */
361     bool IsPresentationShapesSupported() const;
362 
GetSdPropHdlFactory() const363     XMLSdPropHdlFactory* GetSdPropHdlFactory() const { return mpSdPropHdlFactory.get(); }
364 
365     const rtl::Reference< XMLTableImport >&     GetShapeTableImport();
366 };
367 
368 #endif // INCLUDED_XMLOFF_SHAPEIMPORT_HXX
369 
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
371