Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / svx / source / unodraw / unobrushitemhelper.cxx
blob90168e42c8b35ef9b76527a36b56a908dc61992d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <algorithm>
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++)
46 rToSet.ClearItem(a);
49 const sal_uInt8 nTransparency(rBrush.GetColor().GetTransparency());
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();
60 if(pGraphic)
62 rToSet.Put(XFillBitmapItem(OUString(), *pGraphic));
64 else
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));
87 else
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 rToSet.Put(XFillColorItem(OUString(), aColor));
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));
133 else
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());
146 rToSet.Put(XFillStyleItem(drawing::FillStyle_NONE));
147 rToSet.Put(XFillColorItem(OUString(), aColor));
151 static sal_uInt16 getTransparenceForSvxBrushItem(const SfxItemSet& rSourceSet, bool bSearchInParents)
153 sal_uInt16 nFillTransparence(rSourceSet.Get(XATTR_FILLTRANSPARENCE, bSearchInParents).GetValue());
154 const SfxPoolItem* pGradientItem = nullptr;
156 if(SfxItemState::SET == rSourceSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE, bSearchInParents, &pGradientItem)
157 && static_cast< const XFillFloatTransparenceItem* >(pGradientItem)->IsEnabled())
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());
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);
167 return nFillTransparence;
170 static std::unique_ptr<SvxBrushItem> getSvxBrushItemForSolid(const SfxItemSet& rSourceSet, bool bSearchInParents, sal_uInt16 nBackgroundID)
172 Color aFillColor(rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents).GetColorValue());
174 // get evtl. mixed transparence
175 const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
177 if(0 != nFillTransparence)
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)));
184 aFillColor.SetTransparency(aTargetTrans);
187 return std::make_unique<SvxBrushItem>(aFillColor, nBackgroundID);
190 std::unique_ptr<SvxBrushItem> getSvxBrushItemFromSourceSet(const SfxItemSet& rSourceSet, sal_uInt16 nBackgroundID, bool bSearchInParents, bool bXMLImportHack)
192 const XFillStyleItem* pXFillStyleItem(rSourceSet.GetItem<XFillStyleItem>(XATTR_FILLSTYLE, bSearchInParents));
194 if(!pXFillStyleItem || drawing::FillStyle_NONE == pXFillStyleItem->GetValue())
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());
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;
203 aFillColor.SetTransparency(0xff);
205 return std::make_unique<SvxBrushItem>(aFillColor, nBackgroundID);
208 auto aRetval = std::make_unique<SvxBrushItem>(nBackgroundID);
210 switch(pXFillStyleItem->GetValue())
212 default:
213 case drawing::FillStyle_NONE:
215 // already handled above, can not happen again
216 break;
218 case drawing::FillStyle_SOLID:
220 // create SvxBrushItem with fill color
221 aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
222 break;
224 case drawing::FillStyle_GRADIENT:
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));
231 // use half/half mixed color from gradient start and end
232 Color aMixedColor((aStartColor + aEndColor) * 0.5);
234 // get evtl. mixed transparence
235 const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
237 if(0 != nFillTransparence)
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)));
244 aMixedColor.SetTransparency(aTargetTrans);
247 aRetval = std::make_unique<SvxBrushItem>(aMixedColor, nBackgroundID);
248 break;
250 case drawing::FillStyle_HATCH:
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());
256 if(bFillBackground)
258 // hatch is background-filled, use FillColor as if drawing::FillStyle_SOLID
259 aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
261 else
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());
267 // get evtl. mixed transparence
268 sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
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));
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)));
278 aHatchColor.SetTransparency(aTargetTrans);
279 aRetval = std::make_unique<SvxBrushItem>(aHatchColor, nBackgroundID);
282 break;
284 case drawing::FillStyle_BITMAP:
286 // create SvxBrushItem with bitmap info and flags
287 const XFillBitmapItem& rBmpItm = rSourceSet.Get(XATTR_FILLBITMAP, bSearchInParents);
288 const Graphic aGraphic(rBmpItm.GetGraphicObject().GetGraphic());
290 // continue independent 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);
295 if(rTileItem.GetValue())
297 aSvxGraphicPosition = GPOS_TILED;
299 else if(rStretchItem.GetValue())
301 aSvxGraphicPosition = GPOS_AREA;
303 else
305 const XFillBmpPosItem& rPosItem = rSourceSet.Get(XATTR_FILLBMP_POS, bSearchInParents);
307 switch(rPosItem.GetValue())
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;
321 // create with given graphic and position
322 aRetval = std::make_unique<SvxBrushItem>(aGraphic, aSvxGraphicPosition, nBackgroundID);
324 // get evtl. mixed transparence
325 const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
327 if(0 != nFillTransparence)
329 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..100] signed
330 aRetval->setGraphicTransparency(static_cast< sal_Int8 >(nFillTransparence));
333 break;
337 return aRetval;
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */