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 <svx/unobrushitemhelper.hxx>
25 #include <svx/xfillit0.hxx>
26 #include <svx/xbtmpit.hxx>
27 #include <svx/xgrscit.hxx>
28 #include <svx/xflbmtit.hxx>
29 #include <svx/xflbmpit.hxx>
30 #include <svx/xflbmsxy.hxx>
31 #include <svx/xflftrit.hxx>
32 #include <svx/xsflclit.hxx>
33 #include <svx/xflbmsli.hxx>
34 #include <svx/xflbtoxy.hxx>
35 #include <svx/xflbstit.hxx>
36 #include <svx/xflboxy.hxx>
37 #include <svx/xflbckit.hxx>
38 #include <svx/xflhtit.hxx>
39 #include <svx/xflclit.hxx>
40 #include <svx/xfltrit.hxx>
41 #include <svx/unoshape.hxx>
42 
43 using namespace com::sun::star;
44 
setSvxBrushItemAsFillAttributesToTargetSet(const SvxBrushItem & rBrush,SfxItemSet & rToSet)45 void setSvxBrushItemAsFillAttributesToTargetSet(const SvxBrushItem& rBrush, SfxItemSet& rToSet)
46 {
47     // Clear all items from the DrawingLayer FillStyle range (if we have any). All
48     // items that need to be set will be set as hard attributes
49     for(sal_uInt16 a(XATTR_FILL_FIRST); rToSet.Count() && a < XATTR_FILL_LAST; a++)
50     {
51         rToSet.ClearItem(a);
52     }
53 
54     const sal_uInt8 nTransparency(rBrush.GetColor().GetTransparency());
55 
56     // tdf#89478 check for image first
57     if (GPOS_NONE != rBrush.GetGraphicPos())
58     {
59         // we have a graphic fill, set fill style
60         rToSet.Put(XFillStyleItem(drawing::FillStyle_BITMAP));
61 
62         // set graphic (if available)
63         const Graphic* pGraphic = rBrush.GetGraphic();
64 
65         if(pGraphic)
66         {
67             rToSet.Put(XFillBitmapItem(OUString(), *pGraphic));
68         }
69         else
70         {
71             OSL_ENSURE(false, "Could not get Graphic from SvxBrushItem (!)");
72         }
73 
74         if(GPOS_AREA == rBrush.GetGraphicPos())
75         {
76             // stretch, also means no tile (both items are defaulted to true)
77             rToSet.Put(XFillBmpStretchItem(true));
78             rToSet.Put(XFillBmpTileItem(false));
79 
80             // default for stretch is also top-left, but this will not be visible
81             rToSet.Put(XFillBmpPosItem(RectPoint::LT));
82         }
83         else if(GPOS_TILED == rBrush.GetGraphicPos())
84         {
85             // tiled, also means no stretch (both items are defaulted to true)
86             rToSet.Put(XFillBmpStretchItem(false));
87             rToSet.Put(XFillBmpTileItem(true));
88 
89             // default for tiled is top-left
90             rToSet.Put(XFillBmpPosItem(RectPoint::LT));
91         }
92         else
93         {
94             // everything else means no tile and no stretch
95             rToSet.Put(XFillBmpStretchItem(false));
96             rToSet.Put(XFillBmpTileItem(false));
97 
98             RectPoint aRectPoint(RectPoint::MM);
99 
100             switch(rBrush.GetGraphicPos())
101             {
102                 case GPOS_LT: aRectPoint = RectPoint::LT; break;
103                 case GPOS_MT: aRectPoint = RectPoint::MT; break;
104                 case GPOS_RT: aRectPoint = RectPoint::RT; break;
105                 case GPOS_LM: aRectPoint = RectPoint::LM; break;
106                 case GPOS_MM: aRectPoint = RectPoint::MM; break;
107                 case GPOS_RM: aRectPoint = RectPoint::RM; break;
108                 case GPOS_LB: aRectPoint = RectPoint::LB; break;
109                 case GPOS_MB: aRectPoint = RectPoint::MB; break;
110                 case GPOS_RB: aRectPoint = RectPoint::RB; break;
111                 default: break; // GPOS_NONE, GPOS_AREA and GPOS_TILED already handled
112             }
113 
114             rToSet.Put(XFillBmpPosItem(aRectPoint));
115         }
116 
117         // check for graphic's transparency
118         const sal_Int8 nGraphicTransparency(rBrush.getGraphicTransparency());
119 
120         if(0 != nGraphicTransparency)
121         {
122             // nGraphicTransparency is in range [0..100]
123             rToSet.Put(XFillTransparenceItem(nGraphicTransparency));
124         }
125     }
126     else if (0xff != nTransparency)
127     {
128         // we have a color fill
129         const Color aColor(rBrush.GetColor().GetRGBColor());
130 
131         rToSet.Put(XFillStyleItem(drawing::FillStyle_SOLID));
132         rToSet.Put(XFillColorItem(OUString(), aColor));
133 
134         // #125189# nTransparency is in range [0..254], convert to [0..100] which is used in
135         // XFillTransparenceItem (caution with the range which is in an *item-specific* range)
136         rToSet.Put(XFillTransparenceItem(((static_cast<sal_Int32>(nTransparency) * 100) + 127) / 254));
137     }
138     else
139     {
140         // GPOS_NONE == rBrush.GetGraphicPos() && 0xff == rBrush.GetColor().GetTransparency(),
141         // still need to rescue the color used. There are sequences used on the UNO API at
142         // import time (OLE. e.g. chart) which first set RGB color (MID_BACK_COLOR_R_G_B,
143         // color stays transparent) and then set transparency (MID_BACK_COLOR_TRANSPARENCY)
144         // to zero later. When not saving the color, it will be lost.
145         // Also need to set the FillStyle to NONE to express the 0xff transparency flag; this
146         // is needed when e.g. first transparency is set to 0xff and then a Graphic gets set.
147         // When not changing the FillStyle, the next getSvxBrushItemFromSourceSet *will* return
148         // to drawing::FillStyle_SOLID with the rescued color.
149         const Color aColor(rBrush.GetColor().GetRGBColor());
150 
151         rToSet.Put(XFillStyleItem(drawing::FillStyle_NONE));
152         rToSet.Put(XFillColorItem(OUString(), aColor));
153     }
154 }
155 
getTransparenceForSvxBrushItem(const SfxItemSet & rSourceSet,bool bSearchInParents)156 static sal_uInt16 getTransparenceForSvxBrushItem(const SfxItemSet& rSourceSet, bool bSearchInParents)
157 {
158     sal_uInt16 nFillTransparence(rSourceSet.Get(XATTR_FILLTRANSPARENCE, bSearchInParents).GetValue());
159     const SfxPoolItem* pGradientItem = nullptr;
160 
161     if(SfxItemState::SET == rSourceSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE, bSearchInParents, &pGradientItem)
162         && static_cast< const XFillFloatTransparenceItem* >(pGradientItem)->IsEnabled())
163     {
164         const XGradient& rGradient = static_cast< const XFillFloatTransparenceItem* >(pGradientItem)->GetGradientValue();
165         const sal_uInt16 nStartLuminance(rGradient.GetStartColor().GetLuminance());
166         const sal_uInt16 nEndLuminance(rGradient.GetEndColor().GetLuminance());
167 
168         // luminance is [0..255], transparence needs to be in [0..100].Maximum is 51200, thus sal_uInt16 is okay to use
169         nFillTransparence = static_cast< sal_uInt16 >(((nStartLuminance + nEndLuminance) * 100) / 512);
170     }
171 
172     return nFillTransparence;
173 }
174 
getSvxBrushItemForSolid(const SfxItemSet & rSourceSet,bool bSearchInParents,sal_uInt16 nBackgroundID)175 static std::shared_ptr<SvxBrushItem> getSvxBrushItemForSolid(const SfxItemSet& rSourceSet, bool bSearchInParents, sal_uInt16 nBackgroundID)
176 {
177     Color aFillColor(rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents).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.SetTransparency(aTargetTrans);
190     }
191 
192     return std::shared_ptr<SvxBrushItem>(new SvxBrushItem(aFillColor, nBackgroundID));
193 }
194 
getSvxBrushItemFromSourceSet(const SfxItemSet & rSourceSet,sal_uInt16 nBackgroundID,bool bSearchInParents,bool bXMLImportHack)195 std::shared_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.SetTransparency(0xff);
209 
210         return std::shared_ptr<SvxBrushItem>(new SvxBrushItem(aFillColor, nBackgroundID));
211     }
212 
213     std::shared_ptr<SvxBrushItem> aRetval(new SvxBrushItem(nBackgroundID));
214 
215     switch(pXFillStyleItem->GetValue())
216     {
217         default:
218         case drawing::FillStyle_NONE:
219         {
220             // already handled above, can not happen again
221             break;
222         }
223         case drawing::FillStyle_SOLID:
224         {
225             // create SvxBrushItem with fill color
226             aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
227             break;
228         }
229         case drawing::FillStyle_GRADIENT:
230         {
231             // cannot be directly supported, but do the best possible
232             const XGradient aXGradient(rSourceSet.Get(XATTR_FILLGRADIENT).GetGradientValue());
233             const basegfx::BColor aStartColor(aXGradient.GetStartColor().getBColor() * (aXGradient.GetStartIntens() * 0.01));
234             const basegfx::BColor aEndColor(aXGradient.GetEndColor().getBColor() * (aXGradient.GetEndIntens() * 0.01));
235 
236             // use half/half mixed color from gradient start and end
237             Color aMixedColor((aStartColor + aEndColor) * 0.5);
238 
239             // get evtl. mixed transparence
240             const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
241 
242             if(0 != nFillTransparence)
243             {
244                 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
245                 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
246                 // since the oxff value is used for special purposes (like no fill and derive from parent)
247                 const sal_uInt8 aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8 >((nFillTransparence * 254) / 100)));
248 
249                 aMixedColor.SetTransparency(aTargetTrans);
250             }
251 
252             aRetval = std::shared_ptr<SvxBrushItem>(new SvxBrushItem(aMixedColor, nBackgroundID));
253             break;
254         }
255         case drawing::FillStyle_HATCH:
256         {
257             // cannot be directly supported, but do the best possible
258             const XHatch& rHatch(rSourceSet.Get(XATTR_FILLHATCH).GetHatchValue());
259             const bool bFillBackground(rSourceSet.Get(XATTR_FILLBACKGROUND).GetValue());
260 
261             if(bFillBackground)
262             {
263                 // hatch is background-filled, use FillColor as if drawing::FillStyle_SOLID
264                 aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
265             }
266             else
267             {
268                 // hatch is not background-filled and using hatch color would be too dark; compensate
269                 // somewhat by making it more transparent
270                 Color aHatchColor(rHatch.GetColor());
271 
272                 // get evtl. mixed transparence
273                 sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
274 
275                 // take half orig transparence, add half transparent, clamp result
276                 nFillTransparence = std::clamp(static_cast<sal_uInt16>((nFillTransparence / 2) + 50), sal_uInt16(0), sal_uInt16(255));
277 
278                 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
279                 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
280                 // since the oxff value is used for special purposes (like no fill and derive from parent)
281                 const sal_uInt8 aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8 >((nFillTransparence * 254) / 100)));
282 
283                 aHatchColor.SetTransparency(aTargetTrans);
284                 aRetval = std::shared_ptr<SvxBrushItem>(new SvxBrushItem(aHatchColor, nBackgroundID));
285             }
286 
287             break;
288         }
289         case drawing::FillStyle_BITMAP:
290         {
291             // create SvxBrushItem with bitmap info and flags
292             const XFillBitmapItem& rBmpItm = rSourceSet.Get(XATTR_FILLBITMAP, bSearchInParents);
293             const Graphic aGraphic(rBmpItm.GetGraphicObject().GetGraphic());
294 
295             // continue idependent of evtl. GraphicType::NONE as aGraphic.GetType(), we still need to rescue positions
296             SvxGraphicPosition aSvxGraphicPosition(GPOS_NONE);
297             const XFillBmpStretchItem& rStretchItem = rSourceSet.Get(XATTR_FILLBMP_STRETCH, bSearchInParents);
298             const XFillBmpTileItem& rTileItem = rSourceSet.Get(XATTR_FILLBMP_TILE, bSearchInParents);
299 
300             if(rTileItem.GetValue())
301             {
302                 aSvxGraphicPosition = GPOS_TILED;
303             }
304             else if(rStretchItem.GetValue())
305             {
306                 aSvxGraphicPosition = GPOS_AREA;
307             }
308             else
309             {
310                 const XFillBmpPosItem& rPosItem = rSourceSet.Get(XATTR_FILLBMP_POS, bSearchInParents);
311 
312                 switch(rPosItem.GetValue())
313                 {
314                     case RectPoint::LT: aSvxGraphicPosition = GPOS_LT; break;
315                     case RectPoint::MT: aSvxGraphicPosition = GPOS_MT; break;
316                     case RectPoint::RT: aSvxGraphicPosition = GPOS_RT; break;
317                     case RectPoint::LM: aSvxGraphicPosition = GPOS_LM; break;
318                     case RectPoint::MM: aSvxGraphicPosition = GPOS_MM; break;
319                     case RectPoint::RM: aSvxGraphicPosition = GPOS_RM; break;
320                     case RectPoint::LB: aSvxGraphicPosition = GPOS_LB; break;
321                     case RectPoint::MB: aSvxGraphicPosition = GPOS_MB; break;
322                     case RectPoint::RB: aSvxGraphicPosition = GPOS_RB; break;
323                 }
324             }
325 
326             // create with given graphic and position
327             aRetval = std::shared_ptr<SvxBrushItem>(new SvxBrushItem(aGraphic, aSvxGraphicPosition, nBackgroundID));
328 
329             // get evtl. mixed transparence
330             const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
331 
332             if(0 != nFillTransparence)
333             {
334                 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..100] signed
335                 aRetval->setGraphicTransparency(static_cast< sal_Int8 >(nFillTransparence));
336             }
337 
338             break;
339         }
340     }
341 
342     return aRetval;
343 }
344 
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
346