1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/io/TempFile.hpp>
21 #include "oox/drawingml/graphicshapecontext.hxx"
23 #include <osl/diagnose.h>
25 #include <drawingml/embeddedwavaudiofile.hxx>
26 #include "drawingml/misccontexts.hxx"
27 #include "drawingml/graphicproperties.hxx"
28 #include "drawingml/customshapeproperties.hxx"
29 #include "drawingml/diagram/diagram.hxx"
30 #include "drawingml/table/tablecontext.hxx"
31 #include "oox/core/xmlfilterbase.hxx"
32 #include "oox/helper/attributelist.hxx"
33 #include "oox/helper/graphichelper.hxx"
34 #include "oox/helper/propertyset.hxx"
35 #include "oox/vml/vmldrawing.hxx"
36 #include "oox/vml/vmlshape.hxx"
37 #include "oox/vml/vmlshapecontainer.hxx"
38 #include "drawingml/fillproperties.hxx"
39 #include "drawingml/transform2dcontext.hxx"
40 #include "oox/helper/binaryinputstream.hxx"
41 #include "oox/helper/binaryoutputstream.hxx"
42 #include "oox/ppt/pptshapegroupcontext.hxx"
43 #include <oox/token/namespaces.hxx>
44 #include <oox/token/tokens.hxx>
46 using namespace ::com::sun::star
;
47 using namespace ::com::sun::star::io
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::lang
;
50 using namespace ::com::sun::star::beans
;
51 using namespace ::com::sun::star::xml::sax
;
52 using namespace ::oox::core
;
54 static uno::Reference
<io::XInputStream
>
55 lcl_GetMediaStream(const OUString
& rStream
, const oox::core::XmlFilterBase
& rFilter
)
57 if (rStream
.isEmpty())
60 Reference
< XInputStream
> xInStrm( rFilter
.openInputStream(rStream
), UNO_SET_THROW
);
64 static OUString
lcl_GetMediaReference(const OUString
& rStream
)
66 return rStream
.isEmpty() ? OUString() : "vnd.sun.star.Package:" + rStream
;
74 GraphicShapeContext::GraphicShapeContext( ContextHandler2Helper
& rParent
, ShapePtr pMasterShapePtr
, ShapePtr pShapePtr
)
75 : ShapeContext( rParent
, pMasterShapePtr
, pShapePtr
)
79 ContextHandlerRef
GraphicShapeContext::onCreateContext( sal_Int32 aElementToken
, const AttributeList
& rAttribs
)
81 switch( getBaseToken( aElementToken
) )
85 return new Transform2DContext( *this, rAttribs
, *mpShapePtr
);
87 return new BlipFillContext( *this, rAttribs
, mpShapePtr
->getGraphicProperties().maBlipProps
);
88 case XML_wavAudioFile
:
90 OUString
const path(getEmbeddedWAVAudioFile(getRelations(), rAttribs
));
91 mpShapePtr
->getGraphicProperties().m_xMediaStream
=
92 lcl_GetMediaStream(path
, getFilter());
93 mpShapePtr
->getGraphicProperties().m_sMediaPackageURL
=
94 lcl_GetMediaReference(path
);
100 OUString rPath
= getRelations().getFragmentPathFromRelId(
101 rAttribs
.getString(R_TOKEN(link
)).get() );
102 mpShapePtr
->getGraphicProperties().m_xMediaStream
=
103 lcl_GetMediaStream(rPath
, getFilter());
104 mpShapePtr
->getGraphicProperties().m_sMediaPackageURL
=
105 lcl_GetMediaReference(rPath
);
110 if ((getNamespace( aElementToken
) == NMSP_vml
) && mpShapePtr
)
112 mpShapePtr
->setServiceName("com.sun.star.drawing.CustomShape");
113 CustomShapePropertiesPtr pCstmShpProps
114 (mpShapePtr
->getCustomShapeProperties());
116 pCstmShpProps
->setShapePresetType( getBaseToken( aElementToken
) );
119 return ShapeContext::onCreateContext( aElementToken
, rAttribs
);
122 // CT_GraphicalObjectFrameContext
124 GraphicalObjectFrameContext::GraphicalObjectFrameContext( ContextHandler2Helper
& rParent
, ShapePtr pMasterShapePtr
, ShapePtr pShapePtr
, bool bEmbedShapesInChart
) :
125 ShapeContext( rParent
, pMasterShapePtr
, pShapePtr
),
126 mbEmbedShapesInChart( bEmbedShapesInChart
),
131 ContextHandlerRef
GraphicalObjectFrameContext::onCreateContext( sal_Int32 aElementToken
, const AttributeList
& rAttribs
)
133 switch( getBaseToken( aElementToken
) )
135 // CT_ShapeProperties
136 case XML_nvGraphicFramePr
: // CT_GraphicalObjectFrameNonVisual
138 case XML_xfrm
: // CT_Transform2D
139 return new Transform2DContext( *this, rAttribs
, *mpShapePtr
);
140 case XML_graphic
: // CT_GraphicalObject
143 case XML_graphicData
: // CT_GraphicalObjectData
145 OUString
sUri( rAttribs
.getString( XML_uri
).get() );
146 if ( sUri
== "http://schemas.openxmlformats.org/presentationml/2006/ole" ||
147 sUri
== "http://purl.oclc.org/ooxml/presentationml/ole" )
148 return new OleObjectGraphicDataContext( *this, mpShapePtr
);
149 else if ( sUri
== "http://schemas.openxmlformats.org/drawingml/2006/diagram" ||
150 sUri
== "http://purl.oclc.org/ooxml/drawingml/diagram" )
151 return new DiagramGraphicDataContext( *this, mpShapePtr
);
152 else if ( sUri
== "http://schemas.openxmlformats.org/drawingml/2006/chart" ||
153 sUri
== "http://purl.oclc.org/ooxml/drawingml/chart" )
154 return new ChartGraphicDataContext( *this, mpShapePtr
, mbEmbedShapesInChart
);
155 else if ( sUri
== "http://schemas.openxmlformats.org/drawingml/2006/table" ||
156 sUri
== "http://purl.oclc.org/ooxml/drawingml/table" )
157 return new table::TableContext( *this, mpShapePtr
);
160 SAL_WARN("oox.drawingml", "OOX: Ignore graphicsData of :" << sUri
);
167 return ShapeContext::onCreateContext( aElementToken
, rAttribs
);
170 void GraphicalObjectFrameContext::onEndElement()
172 if( getCurrentElement() == PPT_TOKEN( graphicFrame
) && mpParent
)
174 oox::ppt::PPTShapeGroupContext
* pParent
= dynamic_cast<oox::ppt::PPTShapeGroupContext
*>(mpParent
);
176 pParent
->importExtDrawings();
180 OleObjectGraphicDataContext::OleObjectGraphicDataContext( ContextHandler2Helper
& rParent
, ShapePtr xShape
) :
181 ShapeContext( rParent
, ShapePtr(), xShape
),
182 mrOleObjectInfo( xShape
->setOleObjectType() )
186 OleObjectGraphicDataContext::~OleObjectGraphicDataContext()
188 /* Register the OLE shape at the VML drawing, this prevents that the
189 related VML shape converts the OLE object by itself. */
190 if( !mrOleObjectInfo
.maShapeId
.isEmpty() )
191 if( ::oox::vml::Drawing
* pVmlDrawing
= getFilter().getVmlDrawing() )
192 pVmlDrawing
->registerOleObject( mrOleObjectInfo
);
195 ContextHandlerRef
OleObjectGraphicDataContext::onCreateContext( sal_Int32 nElement
, const AttributeList
& rAttribs
)
199 case PPT_TOKEN( oleObj
):
201 mrOleObjectInfo
.maShapeId
= rAttribs
.getXString( XML_spid
, OUString() );
202 const Relation
* pRelation
= getRelations().getRelationFromRelId( rAttribs
.getString( R_TOKEN( id
), OUString() ) );
203 OSL_ENSURE( pRelation
, "OleObjectGraphicDataContext::createFastChildContext - missing relation for OLE object" );
206 mrOleObjectInfo
.mbLinked
= pRelation
->mbExternal
;
207 if( pRelation
->mbExternal
)
209 mrOleObjectInfo
.maTargetLink
= getFilter().getAbsoluteUrl( pRelation
->maTarget
);
213 OUString aFragmentPath
= getFragmentPathFromRelation( *pRelation
);
214 if( !aFragmentPath
.isEmpty() )
215 getFilter().importBinaryData( mrOleObjectInfo
.maEmbeddedData
, aFragmentPath
);
218 mrOleObjectInfo
.maName
= rAttribs
.getXString( XML_name
, OUString() );
219 mrOleObjectInfo
.maProgId
= rAttribs
.getXString( XML_progId
, OUString() );
220 mrOleObjectInfo
.mbShowAsIcon
= rAttribs
.getBool( XML_showAsIcon
, false );
225 case PPT_TOKEN( embed
):
226 OSL_ENSURE( !mrOleObjectInfo
.mbLinked
, "OleObjectGraphicDataContext::createFastChildContext - unexpected child element" );
229 case PPT_TOKEN( link
):
230 OSL_ENSURE( mrOleObjectInfo
.mbLinked
, "OleObjectGraphicDataContext::createFastChildContext - unexpected child element" );
231 mrOleObjectInfo
.mbAutoUpdate
= rAttribs
.getBool( XML_updateAutomatic
, false );
233 case PPT_TOKEN( pic
):
234 return new GraphicShapeContext( *this, mpMasterShapePtr
, mpShapePtr
);
240 DiagramGraphicDataContext::DiagramGraphicDataContext( ContextHandler2Helper
& rParent
, ShapePtr pShapePtr
)
241 : ShapeContext( rParent
, ShapePtr(), pShapePtr
)
243 pShapePtr
->setDiagramType();
246 DiagramGraphicDataContext::~DiagramGraphicDataContext()
250 ContextHandlerRef
DiagramGraphicDataContext::onCreateContext( ::sal_Int32 aElementToken
, const AttributeList
& rAttribs
)
252 switch( aElementToken
)
254 case DGM_TOKEN( relIds
):
256 msDm
= rAttribs
.getString( R_TOKEN( dm
) ).get();
257 msLo
= rAttribs
.getString( R_TOKEN( lo
) ).get();
258 msQs
= rAttribs
.getString( R_TOKEN( qs
) ).get();
259 msCs
= rAttribs
.getString( R_TOKEN( cs
) ).get();
260 loadDiagram(mpShapePtr
,
262 getFragmentPathFromRelId( msDm
),
263 getFragmentPathFromRelId( msLo
),
264 getFragmentPathFromRelId( msQs
),
265 getFragmentPathFromRelId( msCs
));
266 SAL_INFO("oox.drawingml", "DiagramGraphicDataContext::onCreateContext: added shape " << mpShapePtr
->getName()
267 << " of type " << mpShapePtr
->getServiceName()
268 << ", position: " << mpShapePtr
->getPosition().X
269 << "," << mpShapePtr
->getPosition().Y
270 << ", size: " << mpShapePtr
->getSize().Width
271 << "x" << mpShapePtr
->getSize().Height
);
273 // No DrawingML fallback, need to warn the user at the end.
274 if (mpShapePtr
->getExtDrawings().empty())
275 getFilter().setMissingExtDrawing();
278 for (const auto& rRelId
: mpShapePtr
->getExtDrawings())
280 // An invalid fallback reference is as bad as a missing one.
281 if (getFragmentPathFromRelId(rRelId
).isEmpty())
283 getFilter().setMissingExtDrawing();
295 return ShapeContext::onCreateContext( aElementToken
, rAttribs
);
298 ChartGraphicDataContext::ChartGraphicDataContext( ContextHandler2Helper
& rParent
, const ShapePtr
& rxShape
, bool bEmbedShapes
) :
299 ShapeContext( rParent
, ShapePtr(), rxShape
),
300 mrChartShapeInfo( rxShape
->setChartType( bEmbedShapes
) )
304 ContextHandlerRef
ChartGraphicDataContext::onCreateContext( ::sal_Int32 nElement
, const AttributeList
& rAttribs
)
306 if( nElement
== C_TOKEN( chart
) )
308 mrChartShapeInfo
.maFragmentPath
= getFragmentPathFromRelId( rAttribs
.getString( R_TOKEN( id
), OUString() ) );
313 } // namespace drawingml
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */