LibreOffice Module svx (master) 1
unobrushitemhelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <algorithm>
23
24#include <osl/diagnose.h>
25#include <svl/itemset.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>
37
38using namespace com::sun::star;
39
41{
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++)
45 {
46 rToSet.ClearItem(a);
47 }
48
49 const sal_uInt8 nTransparency(255 - rBrush.GetColor().GetAlpha());
50
51 // tdf#89478 check for image first
52 if (GPOS_NONE != rBrush.GetGraphicPos())
53 {
54 // we have a graphic fill, set fill style
55 rToSet.Put(XFillStyleItem(drawing::FillStyle_BITMAP));
56
57 // set graphic (if available)
58 const Graphic* pGraphic = rBrush.GetGraphic();
59
60 if(pGraphic)
61 {
62 rToSet.Put(XFillBitmapItem(OUString(), *pGraphic));
63 }
64 else
65 {
66 OSL_ENSURE(false, "Could not get Graphic from SvxBrushItem (!)");
67 }
68
69 if(GPOS_AREA == rBrush.GetGraphicPos())
70 {
71 // stretch, also means no tile (both items are defaulted to true)
72 rToSet.Put(XFillBmpStretchItem(true));
73 rToSet.Put(XFillBmpTileItem(false));
74
75 // default for stretch is also top-left, but this will not be visible
77 }
78 else if(GPOS_TILED == rBrush.GetGraphicPos())
79 {
80 // tiled, also means no stretch (both items are defaulted to true)
81 rToSet.Put(XFillBmpStretchItem(false));
82 rToSet.Put(XFillBmpTileItem(true));
83
84 // default for tiled is top-left
86 }
87 else
88 {
89 // everything else means no tile and no stretch
90 rToSet.Put(XFillBmpStretchItem(false));
91 rToSet.Put(XFillBmpTileItem(false));
92
93 RectPoint aRectPoint(RectPoint::MM);
94
95 switch(rBrush.GetGraphicPos())
96 {
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
107 }
108
109 rToSet.Put(XFillBmpPosItem(aRectPoint));
110 }
111
112 // check for graphic's transparency
113 const sal_Int8 nGraphicTransparency(rBrush.getGraphicTransparency());
114
115 if(0 != nGraphicTransparency)
116 {
117 // nGraphicTransparency is in range [0..100]
118 rToSet.Put(XFillTransparenceItem(nGraphicTransparency));
119 }
120 }
121 else if (0xff != nTransparency)
122 {
123 // we have a color fill
124 const Color aColor(rBrush.GetColor().GetRGBColor());
125
126 rToSet.Put(XFillStyleItem(drawing::FillStyle_SOLID));
127 XFillColorItem aFillColorItem(OUString(), aColor);
128 aFillColorItem.setComplexColor(rBrush.getComplexColor());
129 rToSet.Put(aFillColorItem);
130
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));
134 }
135 else
136 {
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();
147
148 rToSet.Put(XFillStyleItem(drawing::FillStyle_NONE));
149 XFillColorItem aFillColorItem(OUString(), aColor);
150 rToSet.Put(aFillColorItem);
151 }
152}
153
154static sal_uInt16 getTransparenceForSvxBrushItem(const SfxItemSet& rSourceSet, bool bSearchInParents)
155{
156 sal_uInt16 nFillTransparence(rSourceSet.Get(XATTR_FILLTRANSPARENCE, bSearchInParents).GetValue());
157 const XFillFloatTransparenceItem* pGradientItem = nullptr;
158
159 if((pGradientItem = rSourceSet.GetItemIfSet(XATTR_FILLFLOATTRANSPARENCE, bSearchInParents))
160 && pGradientItem->IsEnabled())
161 {
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());
165
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);
168 }
169
170 return nFillTransparence;
171}
172
173static std::unique_ptr<SvxBrushItem> getSvxBrushItemForSolid(const SfxItemSet& rSourceSet, bool bSearchInParents, sal_uInt16 nBackgroundID)
174{
175 auto const& rFillColorItem = rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents);
176 model::ComplexColor aFillComplexColor = rFillColorItem.getComplexColor();
177 Color aFillColor = rFillColorItem.GetColorValue();
178
179 // get evtl. mixed transparence
180 const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
181
182 if(0 != nFillTransparence)
183 {
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)));
188
189 aFillColor.SetAlpha(255 - aTargetTrans);
190 }
191
192 return std::make_unique<SvxBrushItem>(aFillColor, aFillComplexColor, nBackgroundID);
193}
194
195std::unique_ptr<SvxBrushItem> getSvxBrushItemFromSourceSet(const SfxItemSet& rSourceSet, sal_uInt16 nBackgroundID, bool bSearchInParents, bool bXMLImportHack)
196{
197 const XFillStyleItem* pXFillStyleItem(rSourceSet.GetItem<XFillStyleItem>(XATTR_FILLSTYLE, bSearchInParents));
198
199 if(!pXFillStyleItem || drawing::FillStyle_NONE == pXFillStyleItem->GetValue())
200 {
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());
203
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;
207
208 aFillColor.SetAlpha(0);
209
210 return std::make_unique<SvxBrushItem>(aFillColor, nBackgroundID);
211 }
212
213 std::unique_ptr<SvxBrushItem> xRetval;
214
215 switch(pXFillStyleItem->GetValue())
216 {
217 case drawing::FillStyle_NONE:
218 {
219 // already handled above, can not happen again
220 break;
221 }
222 case drawing::FillStyle_SOLID:
223 {
224 // create SvxBrushItem with fill color
225 xRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
226 break;
227 }
228 case drawing::FillStyle_GRADIENT:
229 {
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));
234
235 // use half/half mixed color from gradient start and end
236 Color aMixedColor((aStartColor + aEndColor) * 0.5);
237
238 // get evtl. mixed transparence
239 const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
240
241 if(0 != nFillTransparence)
242 {
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)));
247
248 aMixedColor.SetAlpha(255 - aTargetTrans);
249 }
250
251 xRetval = std::make_unique<SvxBrushItem>(aMixedColor, nBackgroundID);
252 break;
253 }
254 case drawing::FillStyle_HATCH:
255 {
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());
259
260 if(bFillBackground)
261 {
262 // hatch is background-filled, use FillColor as if drawing::FillStyle_SOLID
263 xRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
264 }
265 else
266 {
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());
270
271 // get evtl. mixed transparence
272 sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
273
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));
276
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)));
281
282 aHatchColor.SetAlpha(255 - aTargetTrans);
283 xRetval = std::make_unique<SvxBrushItem>(aHatchColor, nBackgroundID);
284 }
285
286 break;
287 }
288 case drawing::FillStyle_BITMAP:
289 {
290 // create SvxBrushItem with bitmap info and flags
291 const XFillBitmapItem& rBmpItm = rSourceSet.Get(XATTR_FILLBITMAP, bSearchInParents);
292 const Graphic aGraphic(rBmpItm.GetGraphicObject().GetGraphic());
293
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);
298
299 if(rTileItem.GetValue())
300 {
301 aSvxGraphicPosition = GPOS_TILED;
302 }
303 else if(rStretchItem.GetValue())
304 {
305 aSvxGraphicPosition = GPOS_AREA;
306 }
307 else
308 {
309 const XFillBmpPosItem& rPosItem = rSourceSet.Get(XATTR_FILLBMP_POS, bSearchInParents);
310
311 switch(rPosItem.GetValue())
312 {
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;
322 }
323 }
324
325 // create with given graphic and position
326 xRetval = std::make_unique<SvxBrushItem>(aGraphic, aSvxGraphicPosition, nBackgroundID);
327
328 // get evtl. mixed transparence
329 const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
330
331 if(0 != nFillTransparence)
332 {
333 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..100] signed
334 xRetval->setGraphicTransparency(static_cast< sal_Int8 >(nFillTransparence));
335 }
336
337 break;
338 }
339 default:
340 xRetval = std::make_unique<SvxBrushItem>(nBackgroundID);
341 break;
342 }
343
344 return xRetval;
345}
346
347/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvxGraphicPosition
GPOS_MT
GPOS_LT
GPOS_RT
GPOS_NONE
GPOS_MM
GPOS_TILED
GPOS_AREA
GPOS_MB
GPOS_LB
GPOS_LM
GPOS_RM
GPOS_RB
Color GetRGBColor() const
sal_uInt8 GetLuminance() const
sal_uInt8 GetAlpha() const
void SetAlpha(sal_uInt8 nAlpha)
const Graphic & GetGraphic() const
bool GetValue() const
EnumT GetValue() const
sal_uInt16 Count() const
const T * GetItemIfSet(TypedWhichId< T > nWhich, bool bSrchInParent=true) const
sal_uInt16 ClearItem(sal_uInt16 nWhich=0)
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
const Color & GetColor() const
sal_Int8 getGraphicTransparency() const
const Graphic * GetGraphic(OUString const &referer=OUString()) const
const model::ComplexColor & getComplexColor() const
SvxGraphicPosition GetGraphicPos() const
void setComplexColor(model::ComplexColor const &rComplexColor)
Definition: xcolit.hxx:53
const GraphicObject & GetGraphicObject() const
Definition: xbtmpit.hxx:56
const basegfx::BGradient & GetGradientValue() const
Definition: xattr.cxx:2226
const Color & GetColor() const
Definition: xhatch.hxx:52
sal_uInt16 GetStartIntens() const
const basegfx::BColorStops & GetColorStops() const
sal_uInt16 GetEndIntens() const
constexpr ::Color COL_AUTO(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
uno_Any a
RectPoint
Definition: rectenum.hxx:23
unsigned char sal_uInt8
signed char sal_Int8
static std::unique_ptr< SvxBrushItem > getSvxBrushItemForSolid(const SfxItemSet &rSourceSet, bool bSearchInParents, sal_uInt16 nBackgroundID)
void setSvxBrushItemAsFillAttributesToTargetSet(const SvxBrushItem &rBrush, SfxItemSet &rToSet)
std::unique_ptr< SvxBrushItem > getSvxBrushItemFromSourceSet(const SfxItemSet &rSourceSet, sal_uInt16 nBackgroundID, bool bSearchInParents, bool bXMLImportHack)
static sal_uInt16 getTransparenceForSvxBrushItem(const SfxItemSet &rSourceSet, bool bSearchInParents)
constexpr TypedWhichId< XFillBmpPosItem > XATTR_FILLBMP_POS(XATTR_FILL_FIRST+8)
constexpr TypedWhichId< XFillColorItem > XATTR_FILLCOLOR(XATTR_FILL_FIRST+1)
constexpr TypedWhichId< XFillTransparenceItem > XATTR_FILLTRANSPARENCE(XATTR_FILL_FIRST+5)
constexpr TypedWhichId< XFillBmpStretchItem > XATTR_FILLBMP_STRETCH(XATTR_FILL_FIRST+16)
constexpr sal_uInt16 XATTR_FILL_FIRST(XATTRSET_LINE+1)
constexpr TypedWhichId< XFillHatchItem > XATTR_FILLHATCH(XATTR_FILL_FIRST+3)
constexpr sal_uInt16 XATTR_FILL_LAST(XATTR_FILLUSESLIDEBACKGROUND)
constexpr TypedWhichId< XFillBmpTileItem > XATTR_FILLBMP_TILE(XATTR_FILL_FIRST+7)
constexpr TypedWhichId< XFillBitmapItem > XATTR_FILLBITMAP(XATTR_FILL_FIRST+4)
constexpr TypedWhichId< XFillBackgroundItem > XATTR_FILLBACKGROUND(XATTR_FILL_FIRST+19)
constexpr TypedWhichId< XFillFloatTransparenceItem > XATTR_FILLFLOATTRANSPARENCE(XATTR_FILL_FIRST+11)
constexpr TypedWhichId< XFillStyleItem > XATTR_FILLSTYLE(XATTR_FILL_FIRST)
constexpr TypedWhichId< XFillGradientItem > XATTR_FILLGRADIENT(XATTR_FILL_FIRST+2)