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 "drawingml/misccontexts.hxx"
21 #include "oox/helper/attributelist.hxx"
22 #include "oox/helper/graphichelper.hxx"
23 #include "oox/core/xmlfilterbase.hxx"
24 #include "oox/drawingml/drawingmltypes.hxx"
25 #include "drawingml/fillproperties.hxx"
26 #include <oox/token/namespaces.hxx>
27 #include <oox/token/tokens.hxx>
28 #include <sfx2/docfile.hxx>
30 using namespace ::com::sun::star
;
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::xml::sax
;
33 using ::oox::core::ContextHandler2
;
34 using ::oox::core::ContextHandlerRef
;
39 SolidFillContext::SolidFillContext( ContextHandler2Helper
& rParent
,
40 FillProperties
& rFillProps
) :
41 ColorContext( rParent
, rFillProps
.maFillColor
)
45 GradientFillContext::GradientFillContext( ContextHandler2Helper
& rParent
,
46 const AttributeList
& rAttribs
, GradientFillProperties
& rGradientProps
) :
47 ContextHandler2( rParent
),
48 mrGradientProps( rGradientProps
)
50 mrGradientProps
.moShadeFlip
= rAttribs
.getToken( XML_flip
);
51 mrGradientProps
.moRotateWithShape
= rAttribs
.getBool( XML_rotWithShape
);
54 ContextHandlerRef
GradientFillContext::onCreateContext(
55 sal_Int32 nElement
, const AttributeList
& rAttribs
)
59 case A_TOKEN( gsLst
):
60 return this; // for gs elements
63 if( rAttribs
.hasAttribute( XML_pos
) )
65 double fPosition
= getLimitedValue
< double >( rAttribs
.getDouble( XML_pos
, 0.0 ) / 100000.0, 0.0, 1.0 );
66 return new ColorContext( *this, mrGradientProps
.maGradientStops
[ fPosition
] );
71 mrGradientProps
.moShadeAngle
= rAttribs
.getInteger( XML_ang
);
72 mrGradientProps
.moShadeScaled
= rAttribs
.getBool( XML_scaled
);
76 // always set a path type, this disables linear gradient in conversion
77 mrGradientProps
.moGradientPath
= rAttribs
.getToken( XML_path
, XML_rect
);
78 return this; // for fillToRect element
80 case A_TOKEN( fillToRect
):
81 mrGradientProps
.moFillToRect
= GetRelativeRect( rAttribs
.getFastAttributeList() );
84 case A_TOKEN( tileRect
):
85 mrGradientProps
.moTileRect
= GetRelativeRect( rAttribs
.getFastAttributeList() );
91 PatternFillContext::PatternFillContext( ContextHandler2Helper
& rParent
,
92 const AttributeList
& rAttribs
, PatternFillProperties
& rPatternProps
) :
93 ContextHandler2( rParent
),
94 mrPatternProps( rPatternProps
)
96 mrPatternProps
.moPattPreset
= rAttribs
.getToken( XML_prst
);
99 ContextHandlerRef
PatternFillContext::onCreateContext(
100 sal_Int32 nElement
, const AttributeList
& )
104 case A_TOKEN( bgClr
):
105 return new ColorContext( *this, mrPatternProps
.maPattBgColor
);
106 case A_TOKEN( fgClr
):
107 return new ColorContext( *this, mrPatternProps
.maPattFgColor
);
112 ColorChangeContext::ColorChangeContext( ContextHandler2Helper
& rParent
,
113 const AttributeList
& rAttribs
, BlipFillProperties
& rBlipProps
) :
114 ContextHandler2( rParent
),
115 mrBlipProps( rBlipProps
)
117 mrBlipProps
.maColorChangeFrom
.setUnused();
118 mrBlipProps
.maColorChangeTo
.setUnused();
119 mbUseAlpha
= rAttribs
.getBool( XML_useA
, true );
122 ColorChangeContext::~ColorChangeContext()
125 mrBlipProps
.maColorChangeTo
.clearTransparence();
128 ContextHandlerRef
ColorChangeContext::onCreateContext(
129 sal_Int32 nElement
, const AttributeList
& )
133 case A_TOKEN( clrFrom
):
134 return new ColorContext( *this, mrBlipProps
.maColorChangeFrom
);
135 case A_TOKEN( clrTo
):
136 return new ColorContext( *this, mrBlipProps
.maColorChangeTo
);
141 BlipContext::BlipContext( ContextHandler2Helper
& rParent
,
142 const AttributeList
& rAttribs
, BlipFillProperties
& rBlipProps
) :
143 ContextHandler2( rParent
),
144 mrBlipProps( rBlipProps
)
146 if( rAttribs
.hasAttribute( R_TOKEN( embed
) ) )
148 // internal picture URL
149 OUString aFragmentPath
= getFragmentPathFromRelId( rAttribs
.getString( R_TOKEN( embed
), OUString() ) );
150 if( !aFragmentPath
.isEmpty() )
151 mrBlipProps
.mxGraphic
= getFilter().getGraphicHelper().importEmbeddedGraphic( aFragmentPath
);
153 else if( rAttribs
.hasAttribute( R_TOKEN( link
) ) )
157 // we will embed this link, this is better than just doing nothing..
158 // TODO: import this graphic as real link, but this requires some
160 OUString aRelId
= rAttribs
.getString( R_TOKEN( link
), OUString() );
161 OUString aTargetLink
= getFilter().getAbsoluteUrl( getRelations().getExternalTargetFromRelId( aRelId
) );
162 SfxMedium
aMed( aTargetLink
, StreamMode::STD_READ
);
164 Reference
< io::XInputStream
> xInStrm
= aMed
.GetInputStream();
166 mrBlipProps
.mxGraphic
= getFilter().getGraphicHelper().importGraphic( xInStrm
);
170 ContextHandlerRef
BlipContext::onCreateContext(
171 sal_Int32 nElement
, const AttributeList
& rAttribs
)
175 case A_TOKEN( biLevel
):
176 case A_TOKEN( grayscl
):
177 mrBlipProps
.moColorEffect
= getBaseToken( nElement
);
180 case A_TOKEN( clrChange
):
181 return new ColorChangeContext( *this, rAttribs
, mrBlipProps
);
183 case A_TOKEN( duotone
):
184 return new DuotoneContext( *this, mrBlipProps
);
186 case A_TOKEN( extLst
):
187 return new BlipExtensionContext( *this, mrBlipProps
);
190 mrBlipProps
.moBrightness
= rAttribs
.getInteger( XML_bright
);
191 mrBlipProps
.moContrast
= rAttribs
.getInteger( XML_contrast
);
193 case A_TOKEN( alphaModFix
):
194 mrBlipProps
.moAlphaModFix
= rAttribs
.getInteger(XML_amt
);
200 DuotoneContext::DuotoneContext( ContextHandler2Helper
& rParent
,
201 BlipFillProperties
& rBlipProps
) :
202 ContextHandler2( rParent
),
203 mrBlipProps( rBlipProps
),
206 mrBlipProps
.maDuotoneColors
[0].setUnused();
207 mrBlipProps
.maDuotoneColors
[1].setUnused();
210 DuotoneContext::~DuotoneContext()
214 ::oox::core::ContextHandlerRef
DuotoneContext::onCreateContext(
215 sal_Int32
/*nElement*/, const AttributeList
& /*rAttribs*/ )
217 if( mnColorIndex
< 2 )
218 return new ColorValueContext( *this, mrBlipProps
.maDuotoneColors
[mnColorIndex
++] );
222 BlipFillContext::BlipFillContext( ContextHandler2Helper
& rParent
,
223 const AttributeList
& rAttribs
, BlipFillProperties
& rBlipProps
) :
224 ContextHandler2( rParent
),
225 mrBlipProps( rBlipProps
)
227 mrBlipProps
.moRotateWithShape
= rAttribs
.getBool( XML_rotWithShape
);
230 ContextHandlerRef
BlipFillContext::onCreateContext(
231 sal_Int32 nElement
, const AttributeList
& rAttribs
)
235 case A_TOKEN( blip
):
236 return new BlipContext( *this, rAttribs
, mrBlipProps
);
238 case A_TOKEN( srcRect
):
239 mrBlipProps
.moClipRect
= GetRelativeRect( rAttribs
.getFastAttributeList() );
242 case A_TOKEN( tile
):
243 mrBlipProps
.moBitmapMode
= getBaseToken( nElement
);
244 mrBlipProps
.moTileOffsetX
= rAttribs
.getInteger( XML_tx
);
245 mrBlipProps
.moTileOffsetY
= rAttribs
.getInteger( XML_ty
);
246 mrBlipProps
.moTileScaleX
= rAttribs
.getInteger( XML_sx
);
247 mrBlipProps
.moTileScaleY
= rAttribs
.getInteger( XML_sy
);
248 mrBlipProps
.moTileAlign
= rAttribs
.getToken( XML_algn
);
249 mrBlipProps
.moTileFlip
= rAttribs
.getToken( XML_flip
);
252 case A_TOKEN( stretch
):
253 mrBlipProps
.moBitmapMode
= getBaseToken( nElement
);
254 return this; // for fillRect element
256 case A_TOKEN( fillRect
):
257 mrBlipProps
.moFillRect
= GetRelativeRect( rAttribs
.getFastAttributeList() );
263 FillPropertiesContext::FillPropertiesContext( ContextHandler2Helper
& rParent
, FillProperties
& rFillProps
) :
264 ContextHandler2( rParent
),
265 mrFillProps( rFillProps
)
269 ContextHandlerRef
FillPropertiesContext::onCreateContext(
270 sal_Int32 nElement
, const AttributeList
& rAttribs
)
272 return createFillContext( *this, nElement
, rAttribs
, mrFillProps
);
275 ContextHandlerRef
FillPropertiesContext::createFillContext(
276 ContextHandler2Helper
& rParent
, sal_Int32 nElement
,
277 const AttributeList
& rAttribs
, FillProperties
& rFillProps
)
281 case A_TOKEN( noFill
): { rFillProps
.moFillType
= getBaseToken( nElement
); return nullptr; };
282 case A_TOKEN( solidFill
): { rFillProps
.moFillType
= getBaseToken( nElement
); return new SolidFillContext( rParent
, rFillProps
); };
283 case A_TOKEN( gradFill
): { rFillProps
.moFillType
= getBaseToken( nElement
); return new GradientFillContext( rParent
, rAttribs
, rFillProps
.maGradientProps
); };
284 case A_TOKEN( pattFill
): { rFillProps
.moFillType
= getBaseToken( nElement
); return new PatternFillContext( rParent
, rAttribs
, rFillProps
.maPatternProps
); };
285 case A_TOKEN( blipFill
): { rFillProps
.moFillType
= getBaseToken( nElement
); return new BlipFillContext( rParent
, rAttribs
, rFillProps
.maBlipProps
); };
286 case A_TOKEN( grpFill
): { rFillProps
.moFillType
= getBaseToken( nElement
); return nullptr; }; // TODO
291 SimpleFillPropertiesContext::SimpleFillPropertiesContext( ContextHandler2Helper
& rParent
, Color
& rColor
) :
292 FillPropertiesContext( rParent
, *this ),
297 SimpleFillPropertiesContext::~SimpleFillPropertiesContext()
299 mrColor
= getBestSolidColor();
302 BlipExtensionContext::BlipExtensionContext( ContextHandler2Helper
& rParent
, BlipFillProperties
& rBlipProps
) :
303 ContextHandler2( rParent
),
304 mrBlipProps( rBlipProps
)
308 BlipExtensionContext::~BlipExtensionContext()
312 ContextHandlerRef
BlipExtensionContext::onCreateContext(
313 sal_Int32 nElement
, const AttributeList
& )
318 return new BlipExtensionContext( *this, mrBlipProps
);
320 case OOX_TOKEN( a14
, imgProps
):
321 return new ArtisticEffectContext( *this, mrBlipProps
.maEffect
);
326 ArtisticEffectContext::ArtisticEffectContext( ContextHandler2Helper
& rParent
, ArtisticEffectProperties
& rEffect
) :
327 ContextHandler2( rParent
),
332 ArtisticEffectContext::~ArtisticEffectContext()
336 ContextHandlerRef
ArtisticEffectContext::onCreateContext(
337 sal_Int32 nElement
, const AttributeList
& rAttribs
)
340 if( nElement
== OOX_TOKEN( a14
, imgLayer
) )
342 if( rAttribs
.hasAttribute( R_TOKEN( embed
) ) )
344 OUString aFragmentPath
= getFragmentPathFromRelId( rAttribs
.getString( R_TOKEN( embed
), OUString() ) );
345 if( !aFragmentPath
.isEmpty() )
347 getFilter().importBinaryData( maEffect
.mrOleObjectInfo
.maEmbeddedData
, aFragmentPath
);
348 maEffect
.mrOleObjectInfo
.maProgId
= aFragmentPath
;
351 return new ArtisticEffectContext( *this, maEffect
);
353 if( nElement
== OOX_TOKEN( a14
, imgEffect
) )
354 return new ArtisticEffectContext( *this, maEffect
);
357 maEffect
.msName
= ArtisticEffectProperties::getEffectString( nElement
);
358 if( maEffect
.isEmpty() )
362 sal_Int32 aAttribs
[19] = {
363 XML_visible
, XML_trans
, XML_crackSpacing
, XML_pressure
, XML_numberOfShades
,
364 XML_grainSize
, XML_intensity
, XML_smoothness
, XML_gridSize
, XML_pencilSize
,
365 XML_size
, XML_brushSize
, XML_scaling
, XML_detail
, XML_bright
, XML_contrast
,
366 XML_colorTemp
, XML_sat
, XML_amount
368 for(sal_Int32 nAttrib
: aAttribs
)
370 if( rAttribs
.hasAttribute( nAttrib
) )
372 OUString sName
= ArtisticEffectProperties::getEffectString( nAttrib
);
373 if( !sName
.isEmpty() )
374 maEffect
.maAttribs
[sName
] <<= rAttribs
.getInteger( nAttrib
, 0 );
381 } // namespace drawingml
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */