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 <sal/config.h>
24 #include <osl/diagnose.h>
25 #include <svl/itemset.hxx>
26 #include <svx/unobrushitemhelper.hxx>
27 #include <svx/xfillit0.hxx>
28 #include <svx/xbtmpit.hxx>
29 #include <svx/xflbmtit.hxx>
30 #include <svx/xflbmpit.hxx>
31 #include <svx/xflftrit.hxx>
32 #include <svx/xflbstit.hxx>
33 #include <svx/xflbckit.hxx>
34 #include <svx/xflhtit.hxx>
35 #include <svx/xflclit.hxx>
36 #include <svx/xfltrit.hxx>
38 using namespace com::sun::star
;
40 void setSvxBrushItemAsFillAttributesToTargetSet(const SvxBrushItem
& rBrush
, SfxItemSet
& rToSet
)
42 // Clear all items from the DrawingLayer FillStyle range (if we have any). All
43 // items that need to be set will be set as hard attributes
44 for(sal_uInt16
a(XATTR_FILL_FIRST
); rToSet
.Count() && a
< XATTR_FILL_LAST
; a
++)
49 const sal_uInt8
nTransparency(255 - rBrush
.GetColor().GetAlpha());
51 // tdf#89478 check for image first
52 if (GPOS_NONE
!= rBrush
.GetGraphicPos())
54 // we have a graphic fill, set fill style
55 rToSet
.Put(XFillStyleItem(drawing::FillStyle_BITMAP
));
57 // set graphic (if available)
58 const Graphic
* pGraphic
= rBrush
.GetGraphic();
62 rToSet
.Put(XFillBitmapItem(OUString(), *pGraphic
));
66 OSL_ENSURE(false, "Could not get Graphic from SvxBrushItem (!)");
69 if(GPOS_AREA
== rBrush
.GetGraphicPos())
71 // stretch, also means no tile (both items are defaulted to true)
72 rToSet
.Put(XFillBmpStretchItem(true));
73 rToSet
.Put(XFillBmpTileItem(false));
75 // default for stretch is also top-left, but this will not be visible
76 rToSet
.Put(XFillBmpPosItem(RectPoint::LT
));
78 else if(GPOS_TILED
== rBrush
.GetGraphicPos())
80 // tiled, also means no stretch (both items are defaulted to true)
81 rToSet
.Put(XFillBmpStretchItem(false));
82 rToSet
.Put(XFillBmpTileItem(true));
84 // default for tiled is top-left
85 rToSet
.Put(XFillBmpPosItem(RectPoint::LT
));
89 // everything else means no tile and no stretch
90 rToSet
.Put(XFillBmpStretchItem(false));
91 rToSet
.Put(XFillBmpTileItem(false));
93 RectPoint
aRectPoint(RectPoint::MM
);
95 switch(rBrush
.GetGraphicPos())
97 case GPOS_LT
: aRectPoint
= RectPoint::LT
; break;
98 case GPOS_MT
: aRectPoint
= RectPoint::MT
; break;
99 case GPOS_RT
: aRectPoint
= RectPoint::RT
; break;
100 case GPOS_LM
: aRectPoint
= RectPoint::LM
; break;
101 case GPOS_MM
: aRectPoint
= RectPoint::MM
; break;
102 case GPOS_RM
: aRectPoint
= RectPoint::RM
; break;
103 case GPOS_LB
: aRectPoint
= RectPoint::LB
; break;
104 case GPOS_MB
: aRectPoint
= RectPoint::MB
; break;
105 case GPOS_RB
: aRectPoint
= RectPoint::RB
; break;
106 default: break; // GPOS_NONE, GPOS_AREA and GPOS_TILED already handled
109 rToSet
.Put(XFillBmpPosItem(aRectPoint
));
112 // check for graphic's transparency
113 const sal_Int8
nGraphicTransparency(rBrush
.getGraphicTransparency());
115 if(0 != nGraphicTransparency
)
117 // nGraphicTransparency is in range [0..100]
118 rToSet
.Put(XFillTransparenceItem(nGraphicTransparency
));
121 else if (0xff != nTransparency
)
123 // we have a color fill
124 const Color
aColor(rBrush
.GetColor().GetRGBColor());
126 rToSet
.Put(XFillStyleItem(drawing::FillStyle_SOLID
));
127 XFillColorItem
aFillColorItem(OUString(), aColor
);
128 aFillColorItem
.setComplexColor(rBrush
.getComplexColor());
129 rToSet
.Put(aFillColorItem
);
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(((static_cast<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 XFillColorItem
aFillColorItem(OUString(), aColor
);
150 rToSet
.Put(aFillColorItem
);
154 static sal_uInt16
getTransparenceForSvxBrushItem(const SfxItemSet
& rSourceSet
, bool bSearchInParents
)
156 sal_uInt16
nFillTransparence(rSourceSet
.Get(XATTR_FILLTRANSPARENCE
, bSearchInParents
).GetValue());
157 const XFillFloatTransparenceItem
* pGradientItem
= nullptr;
159 if((pGradientItem
= rSourceSet
.GetItemIfSet(XATTR_FILLFLOATTRANSPARENCE
, bSearchInParents
))
160 && pGradientItem
->IsEnabled())
162 const basegfx::BGradient
& rGradient
= pGradientItem
->GetGradientValue();
163 const sal_uInt16
nStartLuminance(Color(rGradient
.GetColorStops().front().getStopColor()).GetLuminance());
164 const sal_uInt16
nEndLuminance(Color(rGradient
.GetColorStops().back().getStopColor()).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
;
173 static std::unique_ptr
<SvxBrushItem
> getSvxBrushItemForSolid(const SfxItemSet
& rSourceSet
, bool bSearchInParents
, sal_uInt16 nBackgroundID
)
175 auto const& rFillColorItem
= rSourceSet
.Get(XATTR_FILLCOLOR
, bSearchInParents
);
176 model::ComplexColor aFillComplexColor
= rFillColorItem
.getComplexColor();
177 Color aFillColor
= rFillColorItem
.GetColorValue();
179 // get evtl. mixed transparence
180 const sal_uInt16
nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet
, bSearchInParents
));
182 if(0 != nFillTransparence
)
184 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
185 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
186 // since the oxff value is used for special purposes (like no fill and derive from parent)
187 const sal_uInt8
aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8
>((nFillTransparence
* 254) / 100)));
189 aFillColor
.SetAlpha(255 - aTargetTrans
);
192 return std::make_unique
<SvxBrushItem
>(aFillColor
, aFillComplexColor
, nBackgroundID
);
195 std::unique_ptr
<SvxBrushItem
> getSvxBrushItemFromSourceSet(const SfxItemSet
& rSourceSet
, sal_uInt16 nBackgroundID
, bool bSearchInParents
, bool bXMLImportHack
)
197 const XFillStyleItem
* pXFillStyleItem(rSourceSet
.GetItem
<XFillStyleItem
>(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(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
!= Color(0))
206 aFillColor
= COL_AUTO
;
208 aFillColor
.SetAlpha(0);
210 return std::make_unique
<SvxBrushItem
>(aFillColor
, nBackgroundID
);
213 std::unique_ptr
<SvxBrushItem
> xRetval
;
215 switch(pXFillStyleItem
->GetValue())
217 case drawing::FillStyle_NONE
:
219 // already handled above, can not happen again
222 case drawing::FillStyle_SOLID
:
224 // create SvxBrushItem with fill color
225 xRetval
= getSvxBrushItemForSolid(rSourceSet
, bSearchInParents
, nBackgroundID
);
228 case drawing::FillStyle_GRADIENT
:
230 // cannot be directly supported, but do the best possible
231 const basegfx::BGradient
aBGradient(rSourceSet
.Get(XATTR_FILLGRADIENT
).GetGradientValue());
232 const basegfx::BColor
aStartColor(aBGradient
.GetColorStops().front().getStopColor() * (aBGradient
.GetStartIntens() * 0.01));
233 const basegfx::BColor
aEndColor(aBGradient
.GetColorStops().back().getStopColor() * (aBGradient
.GetEndIntens() * 0.01));
235 // use half/half mixed color from gradient start and end
236 Color
aMixedColor((aStartColor
+ aEndColor
) * 0.5);
238 // get evtl. mixed transparence
239 const sal_uInt16
nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet
, bSearchInParents
));
241 if(0 != nFillTransparence
)
243 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
244 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
245 // since the oxff value is used for special purposes (like no fill and derive from parent)
246 const sal_uInt8
aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8
>((nFillTransparence
* 254) / 100)));
248 aMixedColor
.SetAlpha(255 - aTargetTrans
);
251 xRetval
= std::make_unique
<SvxBrushItem
>(aMixedColor
, nBackgroundID
);
254 case drawing::FillStyle_HATCH
:
256 // cannot be directly supported, but do the best possible
257 const XHatch
& rHatch(rSourceSet
.Get(XATTR_FILLHATCH
).GetHatchValue());
258 const bool bFillBackground(rSourceSet
.Get(XATTR_FILLBACKGROUND
).GetValue());
262 // hatch is background-filled, use FillColor as if drawing::FillStyle_SOLID
263 xRetval
= getSvxBrushItemForSolid(rSourceSet
, bSearchInParents
, nBackgroundID
);
267 // hatch is not background-filled and using hatch color would be too dark; compensate
268 // somewhat by making it more transparent
269 Color
aHatchColor(rHatch
.GetColor());
271 // get evtl. mixed transparence
272 sal_uInt16
nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet
, bSearchInParents
));
274 // take half orig transparence, add half transparent, clamp result
275 nFillTransparence
= std::clamp(static_cast<sal_uInt16
>((nFillTransparence
/ 2) + 50), sal_uInt16(0), sal_uInt16(255));
277 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
278 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
279 // since the oxff value is used for special purposes (like no fill and derive from parent)
280 const sal_uInt8
aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8
>((nFillTransparence
* 254) / 100)));
282 aHatchColor
.SetAlpha(255 - aTargetTrans
);
283 xRetval
= std::make_unique
<SvxBrushItem
>(aHatchColor
, nBackgroundID
);
288 case drawing::FillStyle_BITMAP
:
290 // create SvxBrushItem with bitmap info and flags
291 const XFillBitmapItem
& rBmpItm
= rSourceSet
.Get(XATTR_FILLBITMAP
, bSearchInParents
);
292 const Graphic
aGraphic(rBmpItm
.GetGraphicObject().GetGraphic());
294 // continue independent of evtl. GraphicType::NONE as aGraphic.GetType(), we still need to rescue positions
295 SvxGraphicPosition
aSvxGraphicPosition(GPOS_NONE
);
296 const XFillBmpStretchItem
& rStretchItem
= rSourceSet
.Get(XATTR_FILLBMP_STRETCH
, bSearchInParents
);
297 const XFillBmpTileItem
& rTileItem
= rSourceSet
.Get(XATTR_FILLBMP_TILE
, bSearchInParents
);
299 if(rTileItem
.GetValue())
301 aSvxGraphicPosition
= GPOS_TILED
;
303 else if(rStretchItem
.GetValue())
305 aSvxGraphicPosition
= GPOS_AREA
;
309 const XFillBmpPosItem
& rPosItem
= rSourceSet
.Get(XATTR_FILLBMP_POS
, bSearchInParents
);
311 switch(rPosItem
.GetValue())
313 case RectPoint::LT
: aSvxGraphicPosition
= GPOS_LT
; break;
314 case RectPoint::MT
: aSvxGraphicPosition
= GPOS_MT
; break;
315 case RectPoint::RT
: aSvxGraphicPosition
= GPOS_RT
; break;
316 case RectPoint::LM
: aSvxGraphicPosition
= GPOS_LM
; break;
317 case RectPoint::MM
: aSvxGraphicPosition
= GPOS_MM
; break;
318 case RectPoint::RM
: aSvxGraphicPosition
= GPOS_RM
; break;
319 case RectPoint::LB
: aSvxGraphicPosition
= GPOS_LB
; break;
320 case RectPoint::MB
: aSvxGraphicPosition
= GPOS_MB
; break;
321 case RectPoint::RB
: aSvxGraphicPosition
= GPOS_RB
; break;
325 // create with given graphic and position
326 xRetval
= std::make_unique
<SvxBrushItem
>(aGraphic
, aSvxGraphicPosition
, nBackgroundID
);
328 // get evtl. mixed transparence
329 const sal_uInt16
nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet
, bSearchInParents
));
331 if(0 != nFillTransparence
)
333 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..100] signed
334 xRetval
->setGraphicTransparency(static_cast< sal_Int8
>(nFillTransparence
));
340 xRetval
= std::make_unique
<SvxBrushItem
>(nBackgroundID
);
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */