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 rToSet.Put(XFillColorItem(OUString(), aColor));
128
129 // #125189# nTransparency is in range [0..254], convert to [0..100] which is used in
130 // XFillTransparenceItem (caution with the range which is in an *item-specific* range)
131 rToSet.Put(XFillTransparenceItem(((static_cast<sal_Int32>(nTransparency) * 100) + 127) / 254));
132 }
133 else
134 {
135 // GPOS_NONE == rBrush.GetGraphicPos() && 0xff == rBrush.GetColor().GetTransparency(),
136 // still need to rescue the color used. There are sequences used on the UNO API at
137 // import time (OLE. e.g. chart) which first set RGB color (MID_BACK_COLOR_R_G_B,
138 // color stays transparent) and then set transparency (MID_BACK_COLOR_TRANSPARENCY)
139 // to zero later. When not saving the color, it will be lost.
140 // Also need to set the FillStyle to NONE to express the 0xff transparency flag; this
141 // is needed when e.g. first transparency is set to 0xff and then a Graphic gets set.
142 // When not changing the FillStyle, the next getSvxBrushItemFromSourceSet *will* return
143 // to drawing::FillStyle_SOLID with the rescued color.
144 const Color aColor(rBrush.GetColor().GetRGBColor());
145
146 rToSet.Put(XFillStyleItem(drawing::FillStyle_NONE));
147 rToSet.Put(XFillColorItem(OUString(), aColor));
148 }
149}
150
151static sal_uInt16 getTransparenceForSvxBrushItem(const SfxItemSet& rSourceSet, bool bSearchInParents)
152{
153 sal_uInt16 nFillTransparence(rSourceSet.Get(XATTR_FILLTRANSPARENCE, bSearchInParents).GetValue());
154 const XFillFloatTransparenceItem* pGradientItem = nullptr;
155
156 if((pGradientItem = rSourceSet.GetItemIfSet(XATTR_FILLFLOATTRANSPARENCE, bSearchInParents))
157 && pGradientItem->IsEnabled())
158 {
159 const basegfx::BGradient& rGradient = pGradientItem->GetGradientValue();
160 const sal_uInt16 nStartLuminance(Color(rGradient.GetColorStops().front().getStopColor()).GetLuminance());
161 const sal_uInt16 nEndLuminance(Color(rGradient.GetColorStops().back().getStopColor()).GetLuminance());
162
163 // luminance is [0..255], transparence needs to be in [0..100].Maximum is 51200, thus sal_uInt16 is okay to use
164 nFillTransparence = static_cast< sal_uInt16 >(((nStartLuminance + nEndLuminance) * 100) / 512);
165 }
166
167 return nFillTransparence;
168}
169
170static std::unique_ptr<SvxBrushItem> getSvxBrushItemForSolid(const SfxItemSet& rSourceSet, bool bSearchInParents, sal_uInt16 nBackgroundID)
171{
172 Color aFillColor(rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents).GetColorValue());
173
174 // get evtl. mixed transparence
175 const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
176
177 if(0 != nFillTransparence)
178 {
179 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
180 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
181 // since the oxff value is used for special purposes (like no fill and derive from parent)
182 const sal_uInt8 aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8 >((nFillTransparence * 254) / 100)));
183
184 aFillColor.SetAlpha(255 - aTargetTrans);
185 }
186
187 return std::make_unique<SvxBrushItem>(aFillColor, nBackgroundID);
188}
189
190std::unique_ptr<SvxBrushItem> getSvxBrushItemFromSourceSet(const SfxItemSet& rSourceSet, sal_uInt16 nBackgroundID, bool bSearchInParents, bool bXMLImportHack)
191{
192 const XFillStyleItem* pXFillStyleItem(rSourceSet.GetItem<XFillStyleItem>(XATTR_FILLSTYLE, bSearchInParents));
193
194 if(!pXFillStyleItem || drawing::FillStyle_NONE == pXFillStyleItem->GetValue())
195 {
196 // no fill, still need to rescue the evtl. set RGB color, but use as transparent color (we have drawing::FillStyle_NONE)
197 Color aFillColor(rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents).GetColorValue());
198
199 // for writerfilter: when fill style is none, then don't allow anything other than 0 or auto.
200 if (!bXMLImportHack && aFillColor != Color(0))
201 aFillColor = COL_AUTO;
202
203 aFillColor.SetAlpha(0);
204
205 return std::make_unique<SvxBrushItem>(aFillColor, nBackgroundID);
206 }
207
208 std::unique_ptr<SvxBrushItem> xRetval;
209
210 switch(pXFillStyleItem->GetValue())
211 {
212 case drawing::FillStyle_NONE:
213 {
214 // already handled above, can not happen again
215 break;
216 }
217 case drawing::FillStyle_SOLID:
218 {
219 // create SvxBrushItem with fill color
220 xRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
221 break;
222 }
223 case drawing::FillStyle_GRADIENT:
224 {
225 // cannot be directly supported, but do the best possible
226 const basegfx::BGradient aBGradient(rSourceSet.Get(XATTR_FILLGRADIENT).GetGradientValue());
227 const basegfx::BColor aStartColor(aBGradient.GetColorStops().front().getStopColor() * (aBGradient.GetStartIntens() * 0.01));
228 const basegfx::BColor aEndColor(aBGradient.GetColorStops().back().getStopColor() * (aBGradient.GetEndIntens() * 0.01));
229
230 // use half/half mixed color from gradient start and end
231 Color aMixedColor((aStartColor + aEndColor) * 0.5);
232
233 // get evtl. mixed transparence
234 const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
235
236 if(0 != nFillTransparence)
237 {
238 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
239 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
240 // since the oxff value is used for special purposes (like no fill and derive from parent)
241 const sal_uInt8 aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8 >((nFillTransparence * 254) / 100)));
242
243 aMixedColor.SetAlpha(255 - aTargetTrans);
244 }
245
246 xRetval = std::make_unique<SvxBrushItem>(aMixedColor, nBackgroundID);
247 break;
248 }
249 case drawing::FillStyle_HATCH:
250 {
251 // cannot be directly supported, but do the best possible
252 const XHatch& rHatch(rSourceSet.Get(XATTR_FILLHATCH).GetHatchValue());
253 const bool bFillBackground(rSourceSet.Get(XATTR_FILLBACKGROUND).GetValue());
254
255 if(bFillBackground)
256 {
257 // hatch is background-filled, use FillColor as if drawing::FillStyle_SOLID
258 xRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
259 }
260 else
261 {
262 // hatch is not background-filled and using hatch color would be too dark; compensate
263 // somewhat by making it more transparent
264 Color aHatchColor(rHatch.GetColor());
265
266 // get evtl. mixed transparence
267 sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
268
269 // take half orig transparence, add half transparent, clamp result
270 nFillTransparence = std::clamp(static_cast<sal_uInt16>((nFillTransparence / 2) + 50), sal_uInt16(0), sal_uInt16(255));
271
272 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
273 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
274 // since the oxff value is used for special purposes (like no fill and derive from parent)
275 const sal_uInt8 aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8 >((nFillTransparence * 254) / 100)));
276
277 aHatchColor.SetAlpha(255 - aTargetTrans);
278 xRetval = std::make_unique<SvxBrushItem>(aHatchColor, nBackgroundID);
279 }
280
281 break;
282 }
283 case drawing::FillStyle_BITMAP:
284 {
285 // create SvxBrushItem with bitmap info and flags
286 const XFillBitmapItem& rBmpItm = rSourceSet.Get(XATTR_FILLBITMAP, bSearchInParents);
287 const Graphic aGraphic(rBmpItm.GetGraphicObject().GetGraphic());
288
289 // continue independent of evtl. GraphicType::NONE as aGraphic.GetType(), we still need to rescue positions
290 SvxGraphicPosition aSvxGraphicPosition(GPOS_NONE);
291 const XFillBmpStretchItem& rStretchItem = rSourceSet.Get(XATTR_FILLBMP_STRETCH, bSearchInParents);
292 const XFillBmpTileItem& rTileItem = rSourceSet.Get(XATTR_FILLBMP_TILE, bSearchInParents);
293
294 if(rTileItem.GetValue())
295 {
296 aSvxGraphicPosition = GPOS_TILED;
297 }
298 else if(rStretchItem.GetValue())
299 {
300 aSvxGraphicPosition = GPOS_AREA;
301 }
302 else
303 {
304 const XFillBmpPosItem& rPosItem = rSourceSet.Get(XATTR_FILLBMP_POS, bSearchInParents);
305
306 switch(rPosItem.GetValue())
307 {
308 case RectPoint::LT: aSvxGraphicPosition = GPOS_LT; break;
309 case RectPoint::MT: aSvxGraphicPosition = GPOS_MT; break;
310 case RectPoint::RT: aSvxGraphicPosition = GPOS_RT; break;
311 case RectPoint::LM: aSvxGraphicPosition = GPOS_LM; break;
312 case RectPoint::MM: aSvxGraphicPosition = GPOS_MM; break;
313 case RectPoint::RM: aSvxGraphicPosition = GPOS_RM; break;
314 case RectPoint::LB: aSvxGraphicPosition = GPOS_LB; break;
315 case RectPoint::MB: aSvxGraphicPosition = GPOS_MB; break;
316 case RectPoint::RB: aSvxGraphicPosition = GPOS_RB; break;
317 }
318 }
319
320 // create with given graphic and position
321 xRetval = std::make_unique<SvxBrushItem>(aGraphic, aSvxGraphicPosition, nBackgroundID);
322
323 // get evtl. mixed transparence
324 const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
325
326 if(0 != nFillTransparence)
327 {
328 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..100] signed
329 xRetval->setGraphicTransparency(static_cast< sal_Int8 >(nFillTransparence));
330 }
331
332 break;
333 }
334 default:
335 xRetval = std::make_unique<SvxBrushItem>(nBackgroundID);
336 break;
337 }
338
339 return xRetval;
340}
341
342/* 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
SvxGraphicPosition GetGraphicPos() const
const GraphicObject & GetGraphicObject() const
Definition: xbtmpit.hxx:56
const basegfx::BGradient & GetGradientValue() const
Definition: xattr.cxx:2134
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)