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