cid#1640468 Dereference after null check
[LibreOffice.git] / oox / source / drawingml / shapepropertymap.cxx
blob253b9259f7aa8c60a5f7b24038e4a03185092f5b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <oox/drawingml/shapepropertymap.hxx>
22 #include <com/sun/star/awt/Gradient2.hpp>
23 #include <com/sun/star/beans/NamedValue.hpp>
24 #include <com/sun/star/drawing/LineDash.hpp>
25 #include <com/sun/star/drawing/Hatch.hpp>
26 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
27 #include <com/sun/star/graphic/XGraphic.hpp>
29 #include <oox/helper/modelobjecthelper.hxx>
30 #include <oox/token/properties.hxx>
32 namespace oox::drawingml {
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::drawing;
37 using namespace ::com::sun::star::uno;
39 namespace {
41 const ShapePropertyIds spnDefaultShapeIds =
43 PROP_LineStyle, PROP_LineWidth, PROP_LineColor, PROP_LineTransparence, PROP_LineDash, PROP_LineCap, PROP_LineJoint,
44 PROP_LineStartName, PROP_LineStartWidth, PROP_LineStartCenter, PROP_LineEndName, PROP_LineEndWidth, PROP_LineEndCenter,
45 PROP_FillStyle, PROP_FillColor, PROP_FillTransparence, PROP_FillTransparenceGradientName, PROP_FillGradient,
46 PROP_FillBitmap, PROP_FillBitmapMode, PROP_FillBitmapSizeX, PROP_FillBitmapSizeY,
47 PROP_FillBitmapPositionOffsetX, PROP_FillBitmapPositionOffsetY, PROP_FillBitmapRectanglePoint,
48 PROP_FillHatch,
49 PROP_FillBackground,
50 PROP_FillUseSlideBackground,
51 PROP_FillBitmapName,
52 PROP_ShadowXDistance,
53 PROP_ShadowSizeX,
54 PROP_ShadowSizeY
57 } // namespace
59 ShapePropertyInfo ShapePropertyInfo::DEFAULT( spnDefaultShapeIds, true, false, false, false, false );
61 ShapePropertyInfo::ShapePropertyInfo( const ShapePropertyIds& rnPropertyIds,
62 bool bNamedLineMarker, bool bNamedLineDash, bool bNamedFillGradient, bool bNamedFillBitmap, bool bNamedFillHatch ) :
63 mrPropertyIds(rnPropertyIds),
64 mbNamedLineMarker( bNamedLineMarker ),
65 mbNamedLineDash( bNamedLineDash ),
66 mbNamedFillGradient( bNamedFillGradient ),
67 mbNamedFillBitmap( bNamedFillBitmap ),
68 mbNamedFillHatch( bNamedFillHatch )
72 ShapePropertyMap::ShapePropertyMap( ModelObjectHelper& rModelObjHelper, const ShapePropertyInfo& rShapePropInfo ) :
73 mrModelObjHelper( rModelObjHelper ),
74 maShapePropInfo( rShapePropInfo )
78 bool ShapePropertyMap::supportsProperty( ShapeProperty ePropId ) const
80 return maShapePropInfo.has( ePropId );
83 bool ShapePropertyMap::hasNamedLineMarkerInTable( const OUString& rMarkerName ) const
85 return maShapePropInfo.mbNamedLineMarker && mrModelObjHelper.hasLineMarker( rMarkerName );
88 bool ShapePropertyMap::setAnyProperty( ShapeProperty ePropId, const Any& rValue )
90 // get current property identifier for the specified property
91 sal_Int32 nPropId = maShapePropInfo[ ePropId ];
92 if( nPropId < 0 ) return false;
94 // special handling for properties supporting named objects in tables
95 switch( ePropId )
97 case ShapeProperty::LineStart:
98 case ShapeProperty::LineEnd:
99 return setLineMarker( nPropId, rValue );
101 case ShapeProperty::LineDash:
102 return setLineDash( nPropId, rValue );
104 case ShapeProperty::FillGradient:
105 return setFillGradient( nPropId, rValue );
107 case ShapeProperty::GradientTransparency:
108 return setGradientTrans( nPropId, rValue );
110 case ShapeProperty::FillBitmap:
111 return setFillBitmap(nPropId, rValue);
113 case ShapeProperty::FillBitmapName:
114 return setFillBitmapName(rValue);
116 case ShapeProperty::FillHatch:
117 return setFillHatch( nPropId, rValue );
119 default:; // suppress compiler warnings
122 // set plain property value
123 setAnyProperty( nPropId, rValue );
124 return true;
127 // private --------------------------------------------------------------------
129 bool ShapePropertyMap::setLineMarker( sal_Int32 nPropId, const Any& rValue )
131 NamedValue aNamedMarker;
132 if( (rValue >>= aNamedMarker) && !aNamedMarker.Name.isEmpty() )
134 // push line marker explicitly
135 if( !maShapePropInfo.mbNamedLineMarker )
136 return setAnyProperty( nPropId, aNamedMarker.Value );
138 // create named line marker (if coordinates have been passed) and push its name
139 bool bInserted = !aNamedMarker.Value.has< PolyPolygonBezierCoords >() ||
140 mrModelObjHelper.insertLineMarker( aNamedMarker.Name, aNamedMarker.Value.get< PolyPolygonBezierCoords >() );
141 return bInserted && setProperty( nPropId, aNamedMarker.Name );
143 return false;
146 bool ShapePropertyMap::setLineDash( sal_Int32 nPropId, const Any& rValue )
148 // push line dash explicitly
149 if( !maShapePropInfo.mbNamedLineDash )
150 return setAnyProperty( nPropId, rValue );
152 // create named line dash and push its name
153 if( rValue.has< LineDash >() )
155 OUString aDashName = mrModelObjHelper.insertLineDash( rValue.get< LineDash >() );
156 return !aDashName.isEmpty() && setProperty( nPropId, aDashName );
159 return false;
162 bool ShapePropertyMap::setFillGradient( sal_Int32 nPropId, const Any& rValue )
164 // push gradient explicitly
165 if( !maShapePropInfo.mbNamedFillGradient )
166 return setAnyProperty( nPropId, rValue );
168 // create named gradient and push its name
169 if( rValue.has< awt::Gradient2 >() )
171 OUString aGradientName = mrModelObjHelper.insertFillGradient( rValue.get< awt::Gradient2 >() );
172 return !aGradientName.isEmpty() && setProperty( nPropId, aGradientName );
174 else if( rValue.has< awt::Gradient >() )
176 OUString aGradientName = mrModelObjHelper.insertFillGradient( rValue.get< awt::Gradient >() );
177 return !aGradientName.isEmpty() && setProperty( nPropId, aGradientName );
180 return false;
183 bool ShapePropertyMap::setFillHatch( sal_Int32 nPropId, const Any& rValue )
185 // push hatch explicitly
186 if( !maShapePropInfo.mbNamedFillHatch )
187 return setAnyProperty( nPropId, rValue );
189 // create named hatch and push its name
190 if (rValue.has<drawing::Hatch>())
192 OUString aHatchName = mrModelObjHelper.insertFillHatch(rValue.get<drawing::Hatch>());
193 return !aHatchName.isEmpty() && setProperty( nPropId, aHatchName );
196 return false;
199 bool ShapePropertyMap::setGradientTrans( sal_Int32 nPropId, const Any& rValue )
201 // create named gradient and push its name
202 if( rValue.has< awt::Gradient2 >() )
204 OUString aGradientName = mrModelObjHelper.insertTransGrandient( rValue.get< awt::Gradient2 >() );
205 return !aGradientName.isEmpty() && setProperty( nPropId, aGradientName );
207 else if( rValue.has< awt::Gradient >() )
209 OUString aGradientName = mrModelObjHelper.insertTransGrandient( rValue.get< awt::Gradient >() );
210 return !aGradientName.isEmpty() && setProperty( nPropId, aGradientName );
213 return false;
216 bool ShapePropertyMap::setFillBitmap(sal_Int32 nPropId, const Any& rValue)
218 // push bitmap explicitly
219 if (!maShapePropInfo.mbNamedFillBitmap)
221 return setAnyProperty(nPropId, rValue);
224 // create named bitmap URL and push its name
225 if (rValue.has<uno::Reference<graphic::XGraphic>>())
227 auto xGraphic = rValue.get<uno::Reference<graphic::XGraphic>>();
228 OUString aBitmapName = mrModelObjHelper.insertFillBitmapXGraphic(xGraphic);
229 return !aBitmapName.isEmpty() && setProperty(nPropId, aBitmapName);
232 return false;
235 bool ShapePropertyMap::setFillBitmapName(const Any& rValue)
237 if (rValue.has<uno::Reference<graphic::XGraphic>>())
239 auto xGraphic = rValue.get<uno::Reference<graphic::XGraphic>>();
240 OUString aBitmapUrlName = mrModelObjHelper.insertFillBitmapXGraphic(xGraphic);
241 return !aBitmapUrlName.isEmpty() && setProperty(PROP_FillBitmapName, aBitmapUrlName);
243 return false;
246 } // namespace oox::drawingml
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */