1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/drawingml/fillpropertiesgroupcontext.hxx"
25cdf0e10cSrcweir #include "oox/helper/attributelist.hxx"
26cdf0e10cSrcweir #include "oox/helper/graphichelper.hxx"
27cdf0e10cSrcweir #include "oox/core/xmlfilterbase.hxx"
28cdf0e10cSrcweir #include "oox/drawingml/drawingmltypes.hxx"
29cdf0e10cSrcweir #include "oox/drawingml/fillproperties.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using ::rtl::OUString;
32cdf0e10cSrcweir using namespace ::com::sun::star;
33cdf0e10cSrcweir using namespace ::com::sun::star::uno;
34cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
35cdf0e10cSrcweir using ::oox::core::ContextHandler;
36cdf0e10cSrcweir using ::oox::core::XmlFilterBase;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace oox {
39cdf0e10cSrcweir namespace drawingml {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // ============================================================================
42cdf0e10cSrcweir 
SolidFillContext(ContextHandler & rParent,const Reference<XFastAttributeList> &,FillProperties & rFillProps)43cdf0e10cSrcweir SolidFillContext::SolidFillContext( ContextHandler& rParent,
44cdf0e10cSrcweir         const Reference< XFastAttributeList >&, FillProperties& rFillProps ) :
45cdf0e10cSrcweir     ColorContext( rParent, rFillProps.maFillColor )
46cdf0e10cSrcweir {
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
49cdf0e10cSrcweir // ============================================================================
50cdf0e10cSrcweir 
GradientFillContext(ContextHandler & rParent,const Reference<XFastAttributeList> & rxAttribs,GradientFillProperties & rGradientProps)51cdf0e10cSrcweir GradientFillContext::GradientFillContext( ContextHandler& rParent,
52cdf0e10cSrcweir         const Reference< XFastAttributeList >& rxAttribs, GradientFillProperties& rGradientProps ) :
53cdf0e10cSrcweir     ContextHandler( rParent ),
54cdf0e10cSrcweir     mrGradientProps( rGradientProps )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     AttributeList aAttribs( rxAttribs );
57cdf0e10cSrcweir     mrGradientProps.moShadeFlip = aAttribs.getToken( XML_flip );
58cdf0e10cSrcweir     mrGradientProps.moRotateWithShape = aAttribs.getBool( XML_rotWithShape );
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
createFastChildContext(sal_Int32 nElement,const Reference<XFastAttributeList> & rxAttribs)61cdf0e10cSrcweir Reference< XFastContextHandler > GradientFillContext::createFastChildContext(
62cdf0e10cSrcweir         sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw (SAXException, RuntimeException)
63cdf0e10cSrcweir {
64cdf0e10cSrcweir     AttributeList aAttribs( rxAttribs );
65cdf0e10cSrcweir     switch( nElement )
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir         case A_TOKEN( gsLst ):
68cdf0e10cSrcweir             return this;    // for gs elements
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         case A_TOKEN( gs ):
71cdf0e10cSrcweir             if( aAttribs.hasAttribute( XML_pos ) )
72cdf0e10cSrcweir             {
73cdf0e10cSrcweir                 double fPosition = getLimitedValue< double >( aAttribs.getDouble( XML_pos, 0.0 ) / 100000.0, 0.0, 1.0 );
74cdf0e10cSrcweir                 return new ColorContext( *this, mrGradientProps.maGradientStops[ fPosition ] );
75cdf0e10cSrcweir             }
76cdf0e10cSrcweir         break;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir         case A_TOKEN( lin ):
79cdf0e10cSrcweir             mrGradientProps.moShadeAngle = aAttribs.getInteger( XML_ang );
80cdf0e10cSrcweir             mrGradientProps.moShadeScaled = aAttribs.getBool( XML_scaled );
81cdf0e10cSrcweir         break;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         case A_TOKEN( path ):
84cdf0e10cSrcweir             // always set a path type, this disables linear gradient in conversion
85cdf0e10cSrcweir             mrGradientProps.moGradientPath = aAttribs.getToken( XML_path, XML_rect );
86cdf0e10cSrcweir             return this;    // for fillToRect element
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         case A_TOKEN( fillToRect ):
89cdf0e10cSrcweir             mrGradientProps.moFillToRect = GetRelativeRect( rxAttribs );
90cdf0e10cSrcweir         break;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         case A_TOKEN( tileRect ):
93cdf0e10cSrcweir             mrGradientProps.moTileRect = GetRelativeRect( rxAttribs );
94cdf0e10cSrcweir         break;
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir     return 0;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir // ============================================================================
100cdf0e10cSrcweir 
PatternFillContext(ContextHandler & rParent,const Reference<XFastAttributeList> & rxAttribs,PatternFillProperties & rPatternProps)101cdf0e10cSrcweir PatternFillContext::PatternFillContext( ContextHandler& rParent,
102cdf0e10cSrcweir         const Reference< XFastAttributeList >& rxAttribs, PatternFillProperties& rPatternProps ) :
103cdf0e10cSrcweir     ContextHandler( rParent ),
104cdf0e10cSrcweir     mrPatternProps( rPatternProps )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     AttributeList aAttribs( rxAttribs );
107cdf0e10cSrcweir     mrPatternProps.moPattPreset = aAttribs.getToken( XML_prst );
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
createFastChildContext(sal_Int32 nElement,const Reference<XFastAttributeList> &)110cdf0e10cSrcweir Reference< XFastContextHandler > PatternFillContext::createFastChildContext(
111cdf0e10cSrcweir         sal_Int32 nElement, const Reference< XFastAttributeList >& ) throw (SAXException, RuntimeException)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     switch( nElement )
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         case A_TOKEN( bgClr ):
116cdf0e10cSrcweir             return new ColorContext( *this, mrPatternProps.maPattBgColor );
117cdf0e10cSrcweir         case A_TOKEN( fgClr ):
118cdf0e10cSrcweir             return new ColorContext( *this, mrPatternProps.maPattFgColor );
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir     return 0;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir // ============================================================================
124cdf0e10cSrcweir // ============================================================================
125cdf0e10cSrcweir 
ColorChangeContext(ContextHandler & rParent,const Reference<XFastAttributeList> & rxAttribs,BlipFillProperties & rBlipProps)126cdf0e10cSrcweir ColorChangeContext::ColorChangeContext( ContextHandler& rParent,
127cdf0e10cSrcweir         const Reference< XFastAttributeList >& rxAttribs, BlipFillProperties& rBlipProps ) :
128cdf0e10cSrcweir     ContextHandler( rParent ),
129cdf0e10cSrcweir     mrBlipProps( rBlipProps )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     mrBlipProps.maColorChangeFrom.setUnused();
132cdf0e10cSrcweir     mrBlipProps.maColorChangeTo.setUnused();
133cdf0e10cSrcweir     AttributeList aAttribs( rxAttribs );
134cdf0e10cSrcweir     mbUseAlpha = aAttribs.getBool( XML_useA, true );
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
~ColorChangeContext()137cdf0e10cSrcweir ColorChangeContext::~ColorChangeContext()
138cdf0e10cSrcweir {
139cdf0e10cSrcweir     if( !mbUseAlpha )
140cdf0e10cSrcweir         mrBlipProps.maColorChangeTo.clearTransparence();
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
createFastChildContext(sal_Int32 nElement,const Reference<XFastAttributeList> &)143cdf0e10cSrcweir Reference< XFastContextHandler > ColorChangeContext::createFastChildContext(
144cdf0e10cSrcweir         sal_Int32 nElement, const Reference< XFastAttributeList >& ) throw (SAXException, RuntimeException)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     switch( nElement )
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         case A_TOKEN( clrFrom ):
149cdf0e10cSrcweir             return new ColorContext( *this, mrBlipProps.maColorChangeFrom );
150cdf0e10cSrcweir         case A_TOKEN( clrTo ):
151cdf0e10cSrcweir             return new ColorContext( *this, mrBlipProps.maColorChangeTo );
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir     return 0;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir // ============================================================================
157cdf0e10cSrcweir 
BlipContext(ContextHandler & rParent,const Reference<XFastAttributeList> & rxAttribs,BlipFillProperties & rBlipProps)158cdf0e10cSrcweir BlipContext::BlipContext( ContextHandler& rParent,
159cdf0e10cSrcweir         const Reference< XFastAttributeList >& rxAttribs, BlipFillProperties& rBlipProps ) :
160cdf0e10cSrcweir     ContextHandler( rParent ),
161cdf0e10cSrcweir     mrBlipProps( rBlipProps )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir     AttributeList aAttribs( rxAttribs );
164cdf0e10cSrcweir     if( aAttribs.hasAttribute( R_TOKEN( embed ) ) )
165cdf0e10cSrcweir     {
166cdf0e10cSrcweir         // internal picture URL
167cdf0e10cSrcweir         OUString aFragmentPath = getFragmentPathFromRelId( aAttribs.getString( R_TOKEN( embed ), OUString() ) );
168cdf0e10cSrcweir         if( aFragmentPath.getLength() > 0 )
169cdf0e10cSrcweir             mrBlipProps.mxGraphic = getFilter().getGraphicHelper().importEmbeddedGraphic( aFragmentPath );
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir     else if( aAttribs.hasAttribute( R_TOKEN( link ) ) )
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir         // external URL
174cdf0e10cSrcweir         OUString aRelId = aAttribs.getString( R_TOKEN( link ), OUString() );
175cdf0e10cSrcweir         OUString aTargetLink = getFilter().getAbsoluteUrl( getRelations().getExternalTargetFromRelId( aRelId ) );
176cdf0e10cSrcweir         // TODO: load external picture
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir }
179cdf0e10cSrcweir 
createFastChildContext(sal_Int32 nElement,const Reference<XFastAttributeList> & rxAttribs)180cdf0e10cSrcweir Reference< XFastContextHandler > BlipContext::createFastChildContext(
181cdf0e10cSrcweir         sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw (SAXException, RuntimeException)
182cdf0e10cSrcweir {
183cdf0e10cSrcweir     AttributeList aAttribs( rxAttribs );
184cdf0e10cSrcweir     switch( nElement )
185cdf0e10cSrcweir     {
186cdf0e10cSrcweir         case A_TOKEN( biLevel ):
187cdf0e10cSrcweir         case A_TOKEN( grayscl ):
188cdf0e10cSrcweir             mrBlipProps.moColorEffect = getBaseToken( nElement );
189cdf0e10cSrcweir         break;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         case A_TOKEN( clrChange ):
192cdf0e10cSrcweir             return new ColorChangeContext( *this, rxAttribs, mrBlipProps );
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         case A_TOKEN( lum ):
195cdf0e10cSrcweir             mrBlipProps.moBrightness = aAttribs.getInteger( XML_bright );
196cdf0e10cSrcweir             mrBlipProps.moContrast = aAttribs.getInteger( XML_contrast );
197cdf0e10cSrcweir         break;
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir     return 0;
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir // ============================================================================
203cdf0e10cSrcweir 
BlipFillContext(ContextHandler & rParent,const Reference<XFastAttributeList> & rxAttribs,BlipFillProperties & rBlipProps)204cdf0e10cSrcweir BlipFillContext::BlipFillContext( ContextHandler& rParent,
205cdf0e10cSrcweir         const Reference< XFastAttributeList >& rxAttribs, BlipFillProperties& rBlipProps ) :
206cdf0e10cSrcweir     ContextHandler( rParent ),
207cdf0e10cSrcweir     mrBlipProps( rBlipProps )
208cdf0e10cSrcweir {
209cdf0e10cSrcweir     AttributeList aAttribs( rxAttribs );
210cdf0e10cSrcweir     mrBlipProps.moRotateWithShape = aAttribs.getBool( XML_rotWithShape );
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
createFastChildContext(sal_Int32 nElement,const Reference<XFastAttributeList> & rxAttribs)213cdf0e10cSrcweir Reference< XFastContextHandler > BlipFillContext::createFastChildContext(
214cdf0e10cSrcweir         sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw (SAXException, RuntimeException)
215cdf0e10cSrcweir {
216cdf0e10cSrcweir     AttributeList aAttribs( rxAttribs );
217cdf0e10cSrcweir     switch( nElement )
218cdf0e10cSrcweir     {
219cdf0e10cSrcweir         case A_TOKEN( blip ):
220cdf0e10cSrcweir             return new BlipContext( *this, rxAttribs, mrBlipProps );
221cdf0e10cSrcweir 
222cdf0e10cSrcweir         case A_TOKEN( srcRect ):
223cdf0e10cSrcweir 			{
224cdf0e10cSrcweir 				rtl::OUString aDefault( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "0" ) ) );
225cdf0e10cSrcweir 				::com::sun::star::geometry::IntegerRectangle2D aClipRect;
226cdf0e10cSrcweir 				aClipRect.X1 = GetPercent( aAttribs.getString( XML_l, aDefault ) );
227cdf0e10cSrcweir 				aClipRect.Y1 = GetPercent( aAttribs.getString( XML_t, aDefault ) );
228cdf0e10cSrcweir 				aClipRect.X2 = GetPercent( aAttribs.getString( XML_r, aDefault ) );
229cdf0e10cSrcweir 				aClipRect.Y2 = GetPercent( aAttribs.getString( XML_b, aDefault ) );
230cdf0e10cSrcweir 				mrBlipProps.moClipRect = aClipRect;
231cdf0e10cSrcweir 			}
232cdf0e10cSrcweir         break;
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         case A_TOKEN( tile ):
235cdf0e10cSrcweir             mrBlipProps.moBitmapMode = getBaseToken( nElement );
236cdf0e10cSrcweir             mrBlipProps.moTileOffsetX = aAttribs.getInteger( XML_tx );
237cdf0e10cSrcweir             mrBlipProps.moTileOffsetY = aAttribs.getInteger( XML_ty );
238cdf0e10cSrcweir             mrBlipProps.moTileScaleX = aAttribs.getInteger( XML_sx );
239cdf0e10cSrcweir             mrBlipProps.moTileScaleY = aAttribs.getInteger( XML_sy );
240cdf0e10cSrcweir             mrBlipProps.moTileAlign = aAttribs.getToken( XML_algn );
241cdf0e10cSrcweir             mrBlipProps.moTileFlip = aAttribs.getToken( XML_flip );
242cdf0e10cSrcweir         break;
243cdf0e10cSrcweir 
244cdf0e10cSrcweir         case A_TOKEN( stretch ):
245cdf0e10cSrcweir             mrBlipProps.moBitmapMode = getBaseToken( nElement );
246cdf0e10cSrcweir             return this;    // for fillRect element
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         case A_TOKEN( fillRect ):
249cdf0e10cSrcweir             mrBlipProps.moFillRect = GetRelativeRect( rxAttribs );
250cdf0e10cSrcweir         break;
251cdf0e10cSrcweir     }
252cdf0e10cSrcweir     return 0;
253cdf0e10cSrcweir }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir // ============================================================================
256cdf0e10cSrcweir // ============================================================================
257cdf0e10cSrcweir 
FillPropertiesContext(ContextHandler & rParent,FillProperties & rFillProps)258cdf0e10cSrcweir FillPropertiesContext::FillPropertiesContext( ContextHandler& rParent, FillProperties& rFillProps ) :
259cdf0e10cSrcweir     ContextHandler( rParent ),
260cdf0e10cSrcweir     mrFillProps( rFillProps )
261cdf0e10cSrcweir {
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
createFastChildContext(sal_Int32 nElement,const Reference<XFastAttributeList> & rxAttribs)264cdf0e10cSrcweir Reference< XFastContextHandler > FillPropertiesContext::createFastChildContext(
265cdf0e10cSrcweir         sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
266cdf0e10cSrcweir     throw ( SAXException, RuntimeException )
267cdf0e10cSrcweir {
268cdf0e10cSrcweir     return createFillContext( *this, nElement, rxAttribs, mrFillProps );
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
createFillContext(ContextHandler & rParent,sal_Int32 nElement,const Reference<XFastAttributeList> & rxAttribs,FillProperties & rFillProps)271cdf0e10cSrcweir /*static*/ Reference< XFastContextHandler > FillPropertiesContext::createFillContext(
272cdf0e10cSrcweir         ContextHandler& rParent, sal_Int32 nElement,
273cdf0e10cSrcweir         const Reference< XFastAttributeList >& rxAttribs, FillProperties& rFillProps )
274cdf0e10cSrcweir {
275cdf0e10cSrcweir     switch( nElement )
276cdf0e10cSrcweir     {
277cdf0e10cSrcweir         case A_TOKEN( noFill ):     { rFillProps.moFillType = getBaseToken( nElement ); return 0; };
278cdf0e10cSrcweir         case A_TOKEN( solidFill ):  { rFillProps.moFillType = getBaseToken( nElement ); return new SolidFillContext( rParent, rxAttribs, rFillProps ); };
279cdf0e10cSrcweir         case A_TOKEN( gradFill ):   { rFillProps.moFillType = getBaseToken( nElement ); return new GradientFillContext( rParent, rxAttribs, rFillProps.maGradientProps ); };
280cdf0e10cSrcweir         case A_TOKEN( pattFill ):   { rFillProps.moFillType = getBaseToken( nElement ); return new PatternFillContext( rParent, rxAttribs, rFillProps.maPatternProps ); };
281cdf0e10cSrcweir         case A_TOKEN( blipFill ):   { rFillProps.moFillType = getBaseToken( nElement ); return new BlipFillContext( rParent, rxAttribs, rFillProps.maBlipProps ); };
282cdf0e10cSrcweir         case A_TOKEN( grpFill ):    { rFillProps.moFillType = getBaseToken( nElement ); return 0; };    // TODO
283cdf0e10cSrcweir     }
284cdf0e10cSrcweir     return 0;
285cdf0e10cSrcweir }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir // ============================================================================
288cdf0e10cSrcweir 
SimpleFillPropertiesContext(ContextHandler & rParent,Color & rColor)289cdf0e10cSrcweir SimpleFillPropertiesContext::SimpleFillPropertiesContext( ContextHandler& rParent, Color& rColor ) :
290cdf0e10cSrcweir     FillPropertiesContext( rParent, *this ),
291cdf0e10cSrcweir     mrColor( rColor )
292cdf0e10cSrcweir {
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
~SimpleFillPropertiesContext()295cdf0e10cSrcweir SimpleFillPropertiesContext::~SimpleFillPropertiesContext()
296cdf0e10cSrcweir {
297cdf0e10cSrcweir     mrColor = getBestSolidColor();
298cdf0e10cSrcweir }
299cdf0e10cSrcweir 
300cdf0e10cSrcweir // ============================================================================
301cdf0e10cSrcweir 
302cdf0e10cSrcweir } // namespace drawingml
303cdf0e10cSrcweir } // namespace oox
304cdf0e10cSrcweir 
305