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 <sal/config.h>
22 #include <string_view>
24 #include <oox/drawingml/graphicshapecontext.hxx>
26 #include <osl/diagnose.h>
27 #include <sal/log.hxx>
29 #include <drawingml/embeddedwavaudiofile.hxx>
30 #include <drawingml/misccontexts.hxx>
31 #include <drawingml/graphicproperties.hxx>
32 #include <drawingml/customshapeproperties.hxx>
33 #include <oox/drawingml/diagram/diagram.hxx>
34 #include <drawingml/table/tablecontext.hxx>
35 #include <oox/core/xmlfilterbase.hxx>
36 #include <oox/helper/attributelist.hxx>
37 #include <oox/vml/vmldrawing.hxx>
38 #include <drawingml/transform2dcontext.hxx>
39 #include <oox/ppt/pptshapegroupcontext.hxx>
40 #include <oox/token/namespaces.hxx>
41 #include <oox/token/tokens.hxx>
43 using namespace ::com::sun::star
;
44 using namespace ::com::sun::star::io
;
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::lang
;
47 using namespace ::com::sun::star::beans
;
48 using namespace ::com::sun::star::xml::sax
;
49 using namespace ::oox::core
;
51 static uno::Reference
<io::XInputStream
>
52 lcl_GetMediaStream(const OUString
& rStream
, const oox::core::XmlFilterBase
& rFilter
)
54 if (rStream
.isEmpty())
57 Reference
< XInputStream
> xInStrm( rFilter
.openInputStream(rStream
), UNO_SET_THROW
);
61 static OUString
lcl_GetMediaReference(std::u16string_view rStream
)
63 return rStream
.empty() ? OUString() : OUString::Concat("vnd.sun.star.Package:") + rStream
;
66 namespace oox::drawingml
{
70 GraphicShapeContext::GraphicShapeContext( ContextHandler2Helper
const & rParent
, const ShapePtr
& pMasterShapePtr
, const ShapePtr
& pShapePtr
)
71 : ShapeContext( rParent
, pMasterShapePtr
, pShapePtr
)
75 ContextHandlerRef
GraphicShapeContext::onCreateContext( sal_Int32 aElementToken
, const AttributeList
& rAttribs
)
77 switch( getBaseToken( aElementToken
) )
81 return new Transform2DContext( *this, rAttribs
, *mpShapePtr
);
83 return new BlipFillContext(*this, rAttribs
, mpShapePtr
->getGraphicProperties().maBlipProps
, nullptr);
84 case XML_wavAudioFile
:
86 OUString
const path(getEmbeddedWAVAudioFile(getRelations(), rAttribs
));
87 Reference
<XInputStream
> xMediaStream
= lcl_GetMediaStream(path
, getFilter());
88 if (xMediaStream
.is())
90 mpShapePtr
->getGraphicProperties().m_xMediaStream
= xMediaStream
;
91 mpShapePtr
->getGraphicProperties().m_sMediaPackageURL
= lcl_GetMediaReference(path
);
98 OUString rPath
= getRelations().getFragmentPathFromRelId(
99 rAttribs
.getStringDefaulted(R_TOKEN(link
)) );
100 if (!rPath
.isEmpty())
102 Reference
<XInputStream
> xMediaStream
= lcl_GetMediaStream(rPath
, getFilter());
103 if (xMediaStream
.is()) // embedded media file
105 mpShapePtr
->getGraphicProperties().m_xMediaStream
= xMediaStream
;
106 mpShapePtr
->getGraphicProperties().m_sMediaPackageURL
107 = lcl_GetMediaReference(rPath
);
112 rPath
= getRelations().getExternalTargetFromRelId(
113 rAttribs
.getStringDefaulted(R_TOKEN(link
)));
114 if (!rPath
.isEmpty()) // linked media file
115 mpShapePtr
->getGraphicProperties().m_sMediaPackageURL
116 = getFilter().getAbsoluteUrl(rPath
);
122 if ((getNamespace( aElementToken
) == NMSP_vml
) && mpShapePtr
)
124 mpShapePtr
->setServiceName("com.sun.star.drawing.CustomShape");
125 CustomShapePropertiesPtr pCstmShpProps
126 (mpShapePtr
->getCustomShapeProperties());
128 pCstmShpProps
->setShapePresetType( getBaseToken( aElementToken
) );
131 return ShapeContext::onCreateContext( aElementToken
, rAttribs
);
134 // CT_GraphicalObjectFrameContext
136 GraphicalObjectFrameContext::GraphicalObjectFrameContext( ContextHandler2Helper
& rParent
, const ShapePtr
& pMasterShapePtr
, const ShapePtr
& pShapePtr
, bool bEmbedShapesInChart
) :
137 ShapeContext( rParent
, pMasterShapePtr
, pShapePtr
),
138 mbEmbedShapesInChart( bEmbedShapesInChart
),
143 ContextHandlerRef
GraphicalObjectFrameContext::onCreateContext( sal_Int32 aElementToken
, const AttributeList
& rAttribs
)
145 switch( getBaseToken( aElementToken
) )
147 // CT_ShapeProperties
148 case XML_nvGraphicFramePr
: // CT_GraphicalObjectFrameNonVisual
150 case XML_xfrm
: // CT_Transform2D
151 return new Transform2DContext( *this, rAttribs
, *mpShapePtr
);
152 case XML_graphic
: // CT_GraphicalObject
155 case XML_graphicData
: // CT_GraphicalObjectData
157 OUString
sUri( rAttribs
.getStringDefaulted( XML_uri
) );
158 if ( sUri
== "http://schemas.openxmlformats.org/presentationml/2006/ole" ||
159 sUri
== "http://purl.oclc.org/ooxml/presentationml/ole" )
160 return new OleObjectGraphicDataContext( *this, mpShapePtr
);
161 else if ( sUri
== "http://schemas.openxmlformats.org/drawingml/2006/diagram" ||
162 sUri
== "http://purl.oclc.org/ooxml/drawingml/diagram" )
163 return new DiagramGraphicDataContext( *this, mpShapePtr
);
164 else if ( sUri
== "http://schemas.openxmlformats.org/drawingml/2006/chart" ||
165 sUri
== "http://purl.oclc.org/ooxml/drawingml/chart" )
166 return new ChartGraphicDataContext( *this, mpShapePtr
, mbEmbedShapesInChart
);
167 else if ( sUri
== "http://schemas.openxmlformats.org/drawingml/2006/table" ||
168 sUri
== "http://purl.oclc.org/ooxml/drawingml/table" )
169 return new table::TableContext( *this, mpShapePtr
);
172 SAL_WARN("oox.drawingml", "OOX: Ignore graphicsData of :" << sUri
);
179 return ShapeContext::onCreateContext( aElementToken
, rAttribs
);
182 void GraphicalObjectFrameContext::onEndElement()
184 if( getCurrentElement() == PPT_TOKEN( graphicFrame
) && mpParent
)
186 oox::ppt::PPTShapeGroupContext
* pParent
= dynamic_cast<oox::ppt::PPTShapeGroupContext
*>(mpParent
);
188 pParent
->importExtDrawings();
192 OleObjectGraphicDataContext::OleObjectGraphicDataContext( ContextHandler2Helper
const & rParent
, const ShapePtr
& xShape
) :
193 ShapeContext( rParent
, ShapePtr(), xShape
),
194 mrOleObjectInfo( xShape
->setOleObjectType() )
198 OleObjectGraphicDataContext::~OleObjectGraphicDataContext()
200 /* Register the OLE shape at the VML drawing, this prevents that the
201 related VML shape converts the OLE object by itself. */
202 if( !mrOleObjectInfo
.maShapeId
.isEmpty() )
203 if( ::oox::vml::Drawing
* pVmlDrawing
= getFilter().getVmlDrawing() )
204 pVmlDrawing
->registerOleObject( mrOleObjectInfo
);
207 ContextHandlerRef
OleObjectGraphicDataContext::onCreateContext( sal_Int32 nElement
, const AttributeList
& rAttribs
)
211 case PPT_TOKEN( oleObj
):
213 mrOleObjectInfo
.maShapeId
= rAttribs
.getXString( XML_spid
, OUString() );
214 const Relation
* pRelation
= getRelations().getRelationFromRelId( rAttribs
.getStringDefaulted( R_TOKEN( id
)) );
215 OSL_ENSURE( pRelation
, "OleObjectGraphicDataContext::createFastChildContext - missing relation for OLE object" );
218 mrOleObjectInfo
.mbLinked
= pRelation
->mbExternal
;
219 if( pRelation
->mbExternal
)
221 mrOleObjectInfo
.maTargetLink
= getFilter().getAbsoluteUrl( pRelation
->maTarget
);
225 OUString aFragmentPath
= getFragmentPathFromRelation( *pRelation
);
226 if( !aFragmentPath
.isEmpty() )
227 getFilter().importBinaryData( mrOleObjectInfo
.maEmbeddedData
, aFragmentPath
);
230 mrOleObjectInfo
.maName
= rAttribs
.getXString( XML_name
, OUString() );
231 mrOleObjectInfo
.maProgId
= rAttribs
.getXString( XML_progId
, OUString() );
232 mrOleObjectInfo
.mbShowAsIcon
= rAttribs
.getBool( XML_showAsIcon
, false );
233 mrOleObjectInfo
.mbHasPicture
= false; // Initialize as false
238 case PPT_TOKEN( embed
):
239 OSL_ENSURE( !mrOleObjectInfo
.mbLinked
, "OleObjectGraphicDataContext::createFastChildContext - unexpected child element" );
242 case PPT_TOKEN( link
):
243 OSL_ENSURE( mrOleObjectInfo
.mbLinked
, "OleObjectGraphicDataContext::createFastChildContext - unexpected child element" );
244 mrOleObjectInfo
.mbAutoUpdate
= rAttribs
.getBool( XML_updateAutomatic
, false );
246 case PPT_TOKEN( pic
):
247 mrOleObjectInfo
.mbHasPicture
= true; // Set true if ole object has picture element.
248 return new GraphicShapeContext( *this, mpMasterShapePtr
, mpShapePtr
);
250 SAL_WARN("oox", "OleObjectGraphicDataContext::onCreateContext: unhandled element: "
251 << getBaseToken(nElement
));
255 void OleObjectGraphicDataContext::onEndElement()
257 if( getCurrentElement() == PPT_TOKEN( oleObj
) && !isMCEStateEmpty() )
259 if (getMCEState() == MCE_STATE::FoundChoice
&& !mrOleObjectInfo
.mbHasPicture
260 && mrOleObjectInfo
.maShapeId
.isEmpty())
261 setMCEState( MCE_STATE::Started
);
265 DiagramGraphicDataContext::DiagramGraphicDataContext( ContextHandler2Helper
const & rParent
, const ShapePtr
& pShapePtr
)
266 : ShapeContext( rParent
, ShapePtr(), pShapePtr
)
268 pShapePtr
->setDiagramType();
271 DiagramGraphicDataContext::~DiagramGraphicDataContext()
275 ContextHandlerRef
DiagramGraphicDataContext::onCreateContext( ::sal_Int32 aElementToken
, const AttributeList
& rAttribs
)
277 switch( aElementToken
)
279 case DGM_TOKEN( relIds
):
281 msDm
= rAttribs
.getStringDefaulted( R_TOKEN( dm
) );
282 msLo
= rAttribs
.getStringDefaulted( R_TOKEN( lo
) );
283 msQs
= rAttribs
.getStringDefaulted( R_TOKEN( qs
) );
284 msCs
= rAttribs
.getStringDefaulted( R_TOKEN( cs
) );
285 loadDiagram(mpShapePtr
,
287 getFragmentPathFromRelId( msDm
),
288 getFragmentPathFromRelId( msLo
),
289 getFragmentPathFromRelId( msQs
),
290 getFragmentPathFromRelId( msCs
),
292 SAL_INFO("oox.drawingml", "DiagramGraphicDataContext::onCreateContext: added shape " << mpShapePtr
->getName()
293 << " of type " << mpShapePtr
->getServiceName()
294 << ", position: " << mpShapePtr
->getPosition().X
295 << "," << mpShapePtr
->getPosition().Y
296 << ", size: " << mpShapePtr
->getSize().Width
297 << "x" << mpShapePtr
->getSize().Height
);
299 // No DrawingML fallback, need to warn the user at the end.
300 if (mpShapePtr
->getExtDrawings().empty())
301 getFilter().setMissingExtDrawing();
304 for (const auto& rRelId
: mpShapePtr
->getExtDrawings())
306 // An invalid fallback reference is as bad as a missing one.
307 if (getFragmentPathFromRelId(rRelId
).isEmpty())
309 getFilter().setMissingExtDrawing();
321 return ShapeContext::onCreateContext( aElementToken
, rAttribs
);
324 ChartGraphicDataContext::ChartGraphicDataContext( ContextHandler2Helper
const & rParent
, const ShapePtr
& rxShape
, bool bEmbedShapes
) :
325 ShapeContext( rParent
, ShapePtr(), rxShape
),
326 mrChartShapeInfo( rxShape
->setChartType( bEmbedShapes
) )
330 ContextHandlerRef
ChartGraphicDataContext::onCreateContext( ::sal_Int32 nElement
, const AttributeList
& rAttribs
)
332 if( nElement
== C_TOKEN( chart
) )
334 mrChartShapeInfo
.maFragmentPath
= getFragmentPathFromRelId( rAttribs
.getStringDefaulted( R_TOKEN( id
)) );
339 } // namespace oox::drawingml
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */