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 
38 using 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(rBrush.GetColor().GetTransparency());
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 
151 static sal_uInt16 getTransparenceForSvxBrushItem(const SfxItemSet& rSourceSet, bool bSearchInParents)
152 {
153  sal_uInt16 nFillTransparence(rSourceSet.Get(XATTR_FILLTRANSPARENCE, bSearchInParents).GetValue());
154  const SfxPoolItem* pGradientItem = nullptr;
155 
156  if(SfxItemState::SET == rSourceSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE, bSearchInParents, &pGradientItem)
157  && static_cast< const XFillFloatTransparenceItem* >(pGradientItem)->IsEnabled())
158  {
159  const XGradient& rGradient = static_cast< const XFillFloatTransparenceItem* >(pGradientItem)->GetGradientValue();
160  const sal_uInt16 nStartLuminance(rGradient.GetStartColor().GetLuminance());
161  const sal_uInt16 nEndLuminance(rGradient.GetEndColor().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 
170 static 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.SetTransparency(aTargetTrans);
185  }
186 
187  return std::make_unique<SvxBrushItem>(aFillColor, nBackgroundID);
188 }
189 
190 std::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.SetTransparency(0xff);
204 
205  return std::make_unique<SvxBrushItem>(aFillColor, nBackgroundID);
206  }
207 
208  auto aRetval = std::make_unique<SvxBrushItem>(nBackgroundID);
209 
210  switch(pXFillStyleItem->GetValue())
211  {
212  default:
213  case drawing::FillStyle_NONE:
214  {
215  // already handled above, can not happen again
216  break;
217  }
218  case drawing::FillStyle_SOLID:
219  {
220  // create SvxBrushItem with fill color
221  aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
222  break;
223  }
224  case drawing::FillStyle_GRADIENT:
225  {
226  // cannot be directly supported, but do the best possible
227  const XGradient aXGradient(rSourceSet.Get(XATTR_FILLGRADIENT).GetGradientValue());
228  const basegfx::BColor aStartColor(aXGradient.GetStartColor().getBColor() * (aXGradient.GetStartIntens() * 0.01));
229  const basegfx::BColor aEndColor(aXGradient.GetEndColor().getBColor() * (aXGradient.GetEndIntens() * 0.01));
230 
231  // use half/half mixed color from gradient start and end
232  Color aMixedColor((aStartColor + aEndColor) * 0.5);
233 
234  // get evtl. mixed transparence
235  const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
236 
237  if(0 != nFillTransparence)
238  {
239  // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
240  // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
241  // since the oxff value is used for special purposes (like no fill and derive from parent)
242  const sal_uInt8 aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8 >((nFillTransparence * 254) / 100)));
243 
244  aMixedColor.SetTransparency(aTargetTrans);
245  }
246 
247  aRetval = std::make_unique<SvxBrushItem>(aMixedColor, nBackgroundID);
248  break;
249  }
250  case drawing::FillStyle_HATCH:
251  {
252  // cannot be directly supported, but do the best possible
253  const XHatch& rHatch(rSourceSet.Get(XATTR_FILLHATCH).GetHatchValue());
254  const bool bFillBackground(rSourceSet.Get(XATTR_FILLBACKGROUND).GetValue());
255 
256  if(bFillBackground)
257  {
258  // hatch is background-filled, use FillColor as if drawing::FillStyle_SOLID
259  aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
260  }
261  else
262  {
263  // hatch is not background-filled and using hatch color would be too dark; compensate
264  // somewhat by making it more transparent
265  Color aHatchColor(rHatch.GetColor());
266 
267  // get evtl. mixed transparence
268  sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
269 
270  // take half orig transparence, add half transparent, clamp result
271  nFillTransparence = std::clamp(static_cast<sal_uInt16>((nFillTransparence / 2) + 50), sal_uInt16(0), sal_uInt16(255));
272 
273  // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
274  // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
275  // since the oxff value is used for special purposes (like no fill and derive from parent)
276  const sal_uInt8 aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8 >((nFillTransparence * 254) / 100)));
277 
278  aHatchColor.SetTransparency(aTargetTrans);
279  aRetval = std::make_unique<SvxBrushItem>(aHatchColor, nBackgroundID);
280  }
281 
282  break;
283  }
284  case drawing::FillStyle_BITMAP:
285  {
286  // create SvxBrushItem with bitmap info and flags
287  const XFillBitmapItem& rBmpItm = rSourceSet.Get(XATTR_FILLBITMAP, bSearchInParents);
288  const Graphic aGraphic(rBmpItm.GetGraphicObject().GetGraphic());
289 
290  // continue idependent of evtl. GraphicType::NONE as aGraphic.GetType(), we still need to rescue positions
291  SvxGraphicPosition aSvxGraphicPosition(GPOS_NONE);
292  const XFillBmpStretchItem& rStretchItem = rSourceSet.Get(XATTR_FILLBMP_STRETCH, bSearchInParents);
293  const XFillBmpTileItem& rTileItem = rSourceSet.Get(XATTR_FILLBMP_TILE, bSearchInParents);
294 
295  if(rTileItem.GetValue())
296  {
297  aSvxGraphicPosition = GPOS_TILED;
298  }
299  else if(rStretchItem.GetValue())
300  {
301  aSvxGraphicPosition = GPOS_AREA;
302  }
303  else
304  {
305  const XFillBmpPosItem& rPosItem = rSourceSet.Get(XATTR_FILLBMP_POS, bSearchInParents);
306 
307  switch(rPosItem.GetValue())
308  {
309  case RectPoint::LT: aSvxGraphicPosition = GPOS_LT; break;
310  case RectPoint::MT: aSvxGraphicPosition = GPOS_MT; break;
311  case RectPoint::RT: aSvxGraphicPosition = GPOS_RT; break;
312  case RectPoint::LM: aSvxGraphicPosition = GPOS_LM; break;
313  case RectPoint::MM: aSvxGraphicPosition = GPOS_MM; break;
314  case RectPoint::RM: aSvxGraphicPosition = GPOS_RM; break;
315  case RectPoint::LB: aSvxGraphicPosition = GPOS_LB; break;
316  case RectPoint::MB: aSvxGraphicPosition = GPOS_MB; break;
317  case RectPoint::RB: aSvxGraphicPosition = GPOS_RB; break;
318  }
319  }
320 
321  // create with given graphic and position
322  aRetval = std::make_unique<SvxBrushItem>(aGraphic, aSvxGraphicPosition, nBackgroundID);
323 
324  // get evtl. mixed transparence
325  const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
326 
327  if(0 != nFillTransparence)
328  {
329  // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..100] signed
330  aRetval->setGraphicTransparency(static_cast< sal_Int8 >(nFillTransparence));
331  }
332 
333  break;
334  }
335  }
336 
337  return aRetval;
338 }
339 
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool GetValue() const
GPOS_RM
std::unique_ptr< SvxBrushItem > getSvxBrushItemFromSourceSet(const SfxItemSet &rSourceSet, sal_uInt16 nBackgroundID, bool bSearchInParents, bool bXMLImportHack)
signed char sal_Int8
GPOS_NONE
GPOS_LT
sal_uInt8 GetTransparency() const
constexpr TypedWhichId< XFillStyleItem > XATTR_FILLSTYLE(XATTR_FILL_FIRST)
sal_uInt8 GetLuminance() const
constexpr TypedWhichId< XFillHatchItem > XATTR_FILLHATCH(XATTR_FILL_FIRST+3)
constexpr TypedWhichId< XFillBackgroundItem > XATTR_FILLBACKGROUND(XATTR_FILL_FIRST+19)
Color GetRGBColor() const
const Color & GetStartColor() const
Definition: xgrad.hxx:67
constexpr::Color COL_AUTO(0xFF, 0xFF, 0xFF, 0xFF)
GPOS_TILED
SvxGraphicPosition GetGraphicPos() const
GPOS_LM
const Graphic * GetGraphic(OUString const &referer=OUString()) const
static sal_uInt16 getTransparenceForSvxBrushItem(const SfxItemSet &rSourceSet, bool bSearchInParents)
const GraphicObject & GetGraphicObject() const
Definition: xbtmpit.hxx:56
const Color & GetColor() const
RectPoint
Definition: rectenum.hxx:22
sal_uInt16 ClearItem(sal_uInt16 nWhich=0)
constexpr TypedWhichId< XFillTransparenceItem > XATTR_FILLTRANSPARENCE(XATTR_FILL_FIRST+5)
constexpr TypedWhichId< XFillGradientItem > XATTR_FILLGRADIENT(XATTR_FILL_FIRST+2)
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
uno_Any a
constexpr TypedWhichId< XFillBitmapItem > XATTR_FILLBITMAP(XATTR_FILL_FIRST+4)
sal_uInt16 Count() const
constexpr TypedWhichId< XFillBmpPosItem > XATTR_FILLBMP_POS(XATTR_FILL_FIRST+8)
constexpr sal_uInt16 XATTR_FILL_FIRST(XATTRSET_LINE+1)
GPOS_MT
SvxGraphicPosition
static std::unique_ptr< SvxBrushItem > getSvxBrushItemForSolid(const SfxItemSet &rSourceSet, bool bSearchInParents, sal_uInt16 nBackgroundID)
GPOS_MM
void setSvxBrushItemAsFillAttributesToTargetSet(const SvxBrushItem &rBrush, SfxItemSet &rToSet)
GPOS_AREA
GPOS_MB
constexpr TypedWhichId< XFillColorItem > XATTR_FILLCOLOR(XATTR_FILL_FIRST+1)
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
constexpr TypedWhichId< XFillFloatTransparenceItem > XATTR_FILLFLOATTRANSPARENCE(XATTR_FILL_FIRST+11)
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
const Graphic & GetGraphic() const
unsigned char sal_uInt8
constexpr TypedWhichId< XFillBmpStretchItem > XATTR_FILLBMP_STRETCH(XATTR_FILL_FIRST+16)
constexpr TypedWhichId< XFillBmpTileItem > XATTR_FILLBMP_TILE(XATTR_FILL_FIRST+7)
constexpr sal_uInt16 XATTR_FILL_LAST(XATTR_FILLBACKGROUND)
GPOS_RB
GPOS_LB
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
GPOS_RT
sal_Int8 getGraphicTransparency() const
const Color & GetEndColor() const
Definition: xgrad.hxx:68
EnumT GetValue() const