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 <svx/unobrushitemhelper.hxx>
21 #include <svx/xfillit0.hxx>
22 #include <svx/xbtmpit.hxx>
23 #include <svx/xgrscit.hxx>
24 #include <svx/xflbmtit.hxx>
25 #include <svx/xflbmpit.hxx>
26 #include <svx/xflbmsxy.hxx>
27 #include <svx/xflftrit.hxx>
28 #include <svx/xsflclit.hxx>
29 #include <svx/xflbmsli.hxx>
30 #include <svx/xflbtoxy.hxx>
31 #include <svx/xflbstit.hxx>
32 #include <svx/xflboxy.hxx>
33 #include <svx/xflbckit.hxx>
34 #include <svx/xflhtit.hxx>
35 #include <svx/xflclit.hxx>
36 #include <svx/xfltrit.hxx>
37 #include <svx/unoshape.hxx>
39 using namespace com::sun::star
;
42 void setSvxBrushItemAsFillAttributesToTargetSet(const SvxBrushItem
& rBrush
, SfxItemSet
& rToSet
)
44 // Clear all items from the DrawingLayer FillStyle range (if we have any). All
45 // items that need to be set will be set as hard attributes
46 for(sal_uInt16
a(XATTR_FILL_FIRST
); rToSet
.Count() && a
< XATTR_FILL_LAST
; a
++)
51 const sal_uInt8
nTransparency(rBrush
.GetColor().GetTransparency());
53 // tdf#89478 check for image first
54 if (GPOS_NONE
!= rBrush
.GetGraphicPos())
56 // we have a graphic fill, set fill style
57 rToSet
.Put(XFillStyleItem(drawing::FillStyle_BITMAP
));
59 // set graphic (if available)
60 const Graphic
* pGraphic
= rBrush
.GetGraphic();
64 rToSet
.Put(XFillBitmapItem(OUString(), *pGraphic
));
68 OSL_ENSURE(false, "Could not get Graphic from SvxBrushItem (!)");
71 if(GPOS_AREA
== rBrush
.GetGraphicPos())
73 // stretch, also means no tile (both items are defaulted to true)
74 rToSet
.Put(XFillBmpStretchItem(true));
75 rToSet
.Put(XFillBmpTileItem(false));
77 // default for stretch is also top-left, but this will not be visible
78 rToSet
.Put(XFillBmpPosItem(RP_LT
));
80 else if(GPOS_TILED
== rBrush
.GetGraphicPos())
82 // tiled, also means no stretch (both items are defaulted to true)
83 rToSet
.Put(XFillBmpStretchItem(false));
84 rToSet
.Put(XFillBmpTileItem(true));
86 // default for tiled is top-left
87 rToSet
.Put(XFillBmpPosItem(RP_LT
));
91 // everything else means no tile and no stretch
92 rToSet
.Put(XFillBmpStretchItem(false));
93 rToSet
.Put(XFillBmpTileItem(false));
95 RECT_POINT
aRectPoint(RP_MM
);
97 switch(rBrush
.GetGraphicPos())
99 case GPOS_LT
: aRectPoint
= RP_LT
; break;
100 case GPOS_MT
: aRectPoint
= RP_MT
; break;
101 case GPOS_RT
: aRectPoint
= RP_RT
; break;
102 case GPOS_LM
: aRectPoint
= RP_LM
; break;
103 case GPOS_MM
: aRectPoint
= RP_MM
; break;
104 case GPOS_RM
: aRectPoint
= RP_RM
; break;
105 case GPOS_LB
: aRectPoint
= RP_LB
; break;
106 case GPOS_MB
: aRectPoint
= RP_MB
; break;
107 case GPOS_RB
: aRectPoint
= RP_RB
; break;
108 default: break; // GPOS_NONE, GPOS_AREA and GPOS_TILED already handled
111 rToSet
.Put(XFillBmpPosItem(aRectPoint
));
114 // check for graphic's transparency
115 const sal_Int8
nGraphicTransparency(rBrush
.getGraphicTransparency());
117 if(0 != nGraphicTransparency
)
119 // nGraphicTransparency is in range [0..100]
120 rToSet
.Put(XFillTransparenceItem(nGraphicTransparency
));
123 else if (0xff != nTransparency
)
125 // we have a color fill
126 const Color
aColor(rBrush
.GetColor().GetRGBColor());
128 rToSet
.Put(XFillStyleItem(drawing::FillStyle_SOLID
));
129 rToSet
.Put(XFillColorItem(OUString(), aColor
));
131 // #125189# nTransparency is in range [0..254], convert to [0..100] which is used in
132 // XFillTransparenceItem (caution with the range which is in an *item-specific* range)
133 rToSet
.Put(XFillTransparenceItem((((sal_Int32
)nTransparency
* 100) + 127) / 254));
137 // GPOS_NONE == rBrush.GetGraphicPos() && 0xff == rBrush.GetColor().GetTransparency(),
138 // still need to rescue the color used. There are sequences used on the UNO API at
139 // import time (OLE. e.g. chart) which first set RGB color (MID_BACK_COLOR_R_G_B,
140 // color stays transparent) and then set transparency (MID_BACK_COLOR_TRANSPARENCY)
141 // to zero later. When not saving the color, it will be lost.
142 // Also need to set the FillStyle to NONE to express the 0xff transparency flag; this
143 // is needed when e.g. first transparency is set to 0xff and then a Graphic gets set.
144 // When not changing the FillStyle, the next getSvxBrushItemFromSourceSet *will* return
145 // to drawing::FillStyle_SOLID with the rescued color.
146 const Color
aColor(rBrush
.GetColor().GetRGBColor());
148 rToSet
.Put(XFillStyleItem(drawing::FillStyle_NONE
));
149 rToSet
.Put(XFillColorItem(OUString(), aColor
));
154 sal_uInt16
getTransparenceForSvxBrushItem(const SfxItemSet
& rSourceSet
, bool bSearchInParents
)
156 sal_uInt16
nFillTransparence(static_cast< const XFillTransparenceItem
& >(rSourceSet
.Get(XATTR_FILLTRANSPARENCE
, bSearchInParents
)).GetValue());
157 const SfxPoolItem
* pGradientItem
= 0;
159 if(SfxItemState::SET
== rSourceSet
.GetItemState(XATTR_FILLFLOATTRANSPARENCE
, bSearchInParents
, &pGradientItem
)
160 && static_cast< const XFillFloatTransparenceItem
* >(pGradientItem
)->IsEnabled())
162 const XGradient
& rGradient
= static_cast< const XFillFloatTransparenceItem
* >(pGradientItem
)->GetGradientValue();
163 const sal_uInt16
nStartLuminance(rGradient
.GetStartColor().GetLuminance());
164 const sal_uInt16
nEndLuminance(rGradient
.GetEndColor().GetLuminance());
166 // luminance is [0..255], transparence needs to be in [0..100].Maximum is 51200, thus sal_uInt16 is okay to use
167 nFillTransparence
= static_cast< sal_uInt16
>(((nStartLuminance
+ nEndLuminance
) * 100) / 512);
170 return nFillTransparence
;
174 SvxBrushItem
getSvxBrushItemForSolid(const SfxItemSet
& rSourceSet
, bool bSearchInParents
, sal_uInt16 nBackgroundID
)
176 Color
aFillColor(static_cast< const XFillColorItem
& >(rSourceSet
.Get(XATTR_FILLCOLOR
, bSearchInParents
)).GetColorValue());
178 // get evtl. mixed transparence
179 const sal_uInt16
nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet
, bSearchInParents
));
181 if(0 != nFillTransparence
)
183 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
184 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
185 // since the oxff value is used for special purposes (like no fill and derive from parent)
186 const sal_uInt8
aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8
>((nFillTransparence
* 254) / 100)));
188 aFillColor
.SetTransparency(aTargetTrans
);
191 return SvxBrushItem(aFillColor
, nBackgroundID
);
195 SvxBrushItem
getSvxBrushItemFromSourceSet(const SfxItemSet
& rSourceSet
, sal_uInt16 nBackgroundID
, bool bSearchInParents
, bool bXMLImportHack
)
197 const XFillStyleItem
* pXFillStyleItem(static_cast< const XFillStyleItem
* >(rSourceSet
.GetItem(XATTR_FILLSTYLE
, bSearchInParents
)));
199 if(!pXFillStyleItem
|| drawing::FillStyle_NONE
== pXFillStyleItem
->GetValue())
201 // no fill, still need to rescue the evtl. set RGB color, but use as transparent color (we have drawing::FillStyle_NONE)
202 Color
aFillColor(static_cast< const XFillColorItem
& >(rSourceSet
.Get(XATTR_FILLCOLOR
, bSearchInParents
)).GetColorValue());
204 // for writerfilter: when fill style is none, then don't allow anything other than 0 or auto.
205 if (!bXMLImportHack
&& aFillColor
.GetColor() != 0)
206 aFillColor
.SetColor(COL_AUTO
);
208 aFillColor
.SetTransparency(0xff);
210 return SvxBrushItem(aFillColor
, nBackgroundID
);
213 SvxBrushItem
aRetval(nBackgroundID
);
215 switch(pXFillStyleItem
->GetValue())
218 case drawing::FillStyle_NONE
:
220 // already handled above, can not happen again
223 case drawing::FillStyle_SOLID
:
225 // create SvxBrushItem with fill color
226 aRetval
= getSvxBrushItemForSolid(rSourceSet
, bSearchInParents
, nBackgroundID
);
229 case drawing::FillStyle_GRADIENT
:
231 // cannot be directly supported, but do the best possible
232 const XGradient
aXGradient(static_cast< const XFillGradientItem
& >(rSourceSet
.Get(XATTR_FILLGRADIENT
)).GetGradientValue());
233 const basegfx::BColor
aStartColor(aXGradient
.GetStartColor().getBColor() * (aXGradient
.GetStartIntens() * 0.01));
234 const basegfx::BColor
aEndColor(aXGradient
.GetEndColor().getBColor() * (aXGradient
.GetEndIntens() * 0.01));
236 // use half/half mixed color from gradient start and end
237 Color
aMixedColor((aStartColor
+ aEndColor
) * 0.5);
239 // get evtl. mixed transparence
240 const sal_uInt16
nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet
, bSearchInParents
));
242 if(0 != nFillTransparence
)
244 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
245 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
246 // since the oxff value is used for special purposes (like no fill and derive from parent)
247 const sal_uInt8
aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8
>((nFillTransparence
* 254) / 100)));
249 aMixedColor
.SetTransparency(aTargetTrans
);
252 aRetval
= SvxBrushItem(aMixedColor
, nBackgroundID
);
255 case drawing::FillStyle_HATCH
:
257 // cannot be directly supported, but do the best possible
258 const XHatch
& rHatch(static_cast< const XFillHatchItem
& >(rSourceSet
.Get(XATTR_FILLHATCH
)).GetHatchValue());
259 const bool bFillBackground(static_cast< const XFillBackgroundItem
& >(rSourceSet
.Get(XATTR_FILLBACKGROUND
)).GetValue());
263 // hatch is background-filled, use FillColor as if drawing::FillStyle_SOLID
264 aRetval
= getSvxBrushItemForSolid(rSourceSet
, bSearchInParents
, nBackgroundID
);
268 // hatch is not background-filled and using hatch color would be too dark; compensate
269 // somewhat by making it more transparent
270 Color
aHatchColor(rHatch
.GetColor());
272 // get evtl. mixed transparence
273 sal_uInt16
nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet
, bSearchInParents
));
275 // take half orig transparence, add half transparent, clamp result
276 nFillTransparence
= basegfx::clamp((sal_uInt16
)((nFillTransparence
/ 2) + 50), (sal_uInt16
)0, (sal_uInt16
)255);
278 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
279 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
280 // since the oxff value is used for special purposes (like no fill and derive from parent)
281 const sal_uInt8
aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8
>((nFillTransparence
* 254) / 100)));
283 aHatchColor
.SetTransparency(aTargetTrans
);
284 aRetval
= SvxBrushItem(aHatchColor
, nBackgroundID
);
289 case drawing::FillStyle_BITMAP
:
291 // create SvxBrushItem with bitmap info and flags
292 const XFillBitmapItem
& rBmpItm
= static_cast< const XFillBitmapItem
& >(rSourceSet
.Get(XATTR_FILLBITMAP
, bSearchInParents
));
293 const Graphic
aGraphic(rBmpItm
.GetGraphicObject().GetGraphic());
295 // continue idependent of evtl. GRAPHIC_NONE as aGraphic.GetType(), we still need to rescue positions
296 SvxGraphicPosition
aSvxGraphicPosition(GPOS_NONE
);
297 const XFillBmpStretchItem
& rStretchItem
= static_cast< const XFillBmpStretchItem
& >(rSourceSet
.Get(XATTR_FILLBMP_STRETCH
, bSearchInParents
));
298 const XFillBmpTileItem
& rTileItem
= static_cast< const XFillBmpTileItem
& >(rSourceSet
.Get(XATTR_FILLBMP_TILE
, bSearchInParents
));
300 if(rTileItem
.GetValue())
302 aSvxGraphicPosition
= GPOS_TILED
;
304 else if(rStretchItem
.GetValue())
306 aSvxGraphicPosition
= GPOS_AREA
;
310 const XFillBmpPosItem
& rPosItem
= static_cast< const XFillBmpPosItem
& >(rSourceSet
.Get(XATTR_FILLBMP_POS
, bSearchInParents
));
312 switch(rPosItem
.GetValue())
314 case RP_LT
: aSvxGraphicPosition
= GPOS_LT
; break;
315 case RP_MT
: aSvxGraphicPosition
= GPOS_MT
; break;
316 case RP_RT
: aSvxGraphicPosition
= GPOS_RT
; break;
317 case RP_LM
: aSvxGraphicPosition
= GPOS_LM
; break;
318 case RP_MM
: aSvxGraphicPosition
= GPOS_MM
; break;
319 case RP_RM
: aSvxGraphicPosition
= GPOS_RM
; break;
320 case RP_LB
: aSvxGraphicPosition
= GPOS_LB
; break;
321 case RP_MB
: aSvxGraphicPosition
= GPOS_MB
; break;
322 case RP_RB
: aSvxGraphicPosition
= GPOS_RB
; break;
326 // create with given graphic and position
327 aRetval
= SvxBrushItem(aGraphic
, aSvxGraphicPosition
, nBackgroundID
);
329 // get evtl. mixed transparence
330 const sal_uInt16
nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet
, bSearchInParents
));
332 if(0 != nFillTransparence
)
334 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..100] signed
335 aRetval
.setGraphicTransparency(static_cast< sal_Int8
>(nFillTransparence
));
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */