1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_svx.hxx"
24 
25 #include <svx/unobrushitemhelper.hxx>
26 #include <svx/xfillit0.hxx>
27 #include <svx/xbtmpit.hxx>
28 #include <svx/xgrscit.hxx>
29 #include <svx/xflbmtit.hxx>
30 #include <svx/xflbmpit.hxx>
31 #include <svx/xflbmsxy.hxx>
32 #include <svx/xflbmsxy.hxx>
33 #include <svx/xflftrit.hxx>
34 #include <svx/xsflclit.hxx>
35 #include <svx/xflbmsli.hxx>
36 #include <svx/xflbtoxy.hxx>
37 #include <svx/xflbstit.hxx>
38 #include <svx/xflboxy.hxx>
39 #include <svx/xflbckit.hxx>
40 #include <svx/xflclit.hxx>
41 #include <svx/xfltrit.hxx>
42 #include <svx/xflhtit.hxx>
43 #include <svx/unoshape.hxx>
44 
45 //UUUU
setSvxBrushItemAsFillAttributesToTargetSet(const SvxBrushItem & rBrush,SfxItemSet & rToSet)46 void setSvxBrushItemAsFillAttributesToTargetSet(const SvxBrushItem& rBrush, SfxItemSet& rToSet)
47 {
48     // Clear all items from the DrawingLayer FillStyle range (if we have any). All
49     // items that need to be set will be set as hard attributes
50     for(sal_uInt16 a(XATTR_FILL_FIRST); rToSet.Count() && a < XATTR_FILL_LAST; a++)
51     {
52         rToSet.ClearItem(a);
53     }
54 
55     const sal_uInt8 nTransparency(rBrush.GetColor().GetTransparency());
56 
57     if(0xff != nTransparency)
58     {
59         // we have a color fill
60         const Color aColor(rBrush.GetColor().GetRGBColor());
61 
62         rToSet.Put(XFillStyleItem(XFILL_SOLID));
63         rToSet.Put(XFillColorItem(String(), aColor));
64 
65         // #125189# nTransparency is in range [0..254], convert to [0..100] which is used in
66         // XFillTransparenceItem (caution with the range which is in an *item-specific* range)
67         rToSet.Put(XFillTransparenceItem((((sal_Int32)nTransparency * 100) + 127) / 254));
68     }
69     else if(GPOS_NONE != rBrush.GetGraphicPos())
70     {
71         // we have a graphic fill, set fill style
72         rToSet.Put(XFillStyleItem(XFILL_BITMAP));
73 
74         // set graphic (if available)
75         const Graphic* pGraphic = rBrush.GetGraphic();
76 
77         if(pGraphic)
78         {
79             rToSet.Put(XFillBitmapItem(String(), *pGraphic));
80         }
81         else
82         {
83             OSL_ENSURE(false, "Could not get Graphic from SvxBrushItem (!)");
84         }
85 
86         if(GPOS_AREA == rBrush.GetGraphicPos())
87         {
88             // stretch, also means no tile (both items are defaulted to true)
89             rToSet.Put(XFillBmpStretchItem(true));
90             rToSet.Put(XFillBmpTileItem(false));
91 
92             // default for strech is also top-left, but this will not be visible
93             rToSet.Put(XFillBmpPosItem(RP_LT));
94         }
95         else if(GPOS_TILED == rBrush.GetGraphicPos())
96         {
97             // tiled, also means no stretch (both items are defaulted to true)
98             rToSet.Put(XFillBmpStretchItem(false));
99             rToSet.Put(XFillBmpTileItem(true));
100 
101             // default for tiled is top-left
102             rToSet.Put(XFillBmpPosItem(RP_LT));
103         }
104         else
105         {
106             // everything else means no tile and no stretch
107             rToSet.Put(XFillBmpStretchItem(false));
108             rToSet.Put(XFillBmpTileItem(false));
109 
110             RECT_POINT aRectPoint(RP_MM);
111 
112             switch(rBrush.GetGraphicPos())
113             {
114                 case GPOS_LT: aRectPoint = RP_LT; break;
115                 case GPOS_MT: aRectPoint = RP_MT; break;
116                 case GPOS_RT: aRectPoint = RP_RT; break;
117                 case GPOS_LM: aRectPoint = RP_LM; break;
118                 case GPOS_MM: aRectPoint = RP_MM; break;
119                 case GPOS_RM: aRectPoint = RP_RM; break;
120                 case GPOS_LB: aRectPoint = RP_LB; break;
121                 case GPOS_MB: aRectPoint = RP_MB; break;
122                 case GPOS_RB: aRectPoint = RP_RB; break;
123                 default: break; // GPOS_NONE, GPOS_AREA and GPOS_TILED already handled
124             }
125 
126             rToSet.Put(XFillBmpPosItem(aRectPoint));
127         }
128 
129         // check for graphic's transparency
130         const sal_Int8 nGraphicTransparency(rBrush.getGraphicTransparency());
131 
132         if(0 != nGraphicTransparency)
133         {
134             // nGraphicTransparency is in range [0..100]
135             rToSet.Put(XFillTransparenceItem(nGraphicTransparency));
136         }
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 XFILL_SOLID with the rescued color.
149         const Color aColor(rBrush.GetColor().GetRGBColor());
150 
151         rToSet.Put(XFillStyleItem(XFILL_NONE));
152         rToSet.Put(XFillColorItem(String(), aColor));
153     }
154 }
155 
156 //UUUU
getTransparenceForSvxBrushItem(const SfxItemSet & rSourceSet,sal_Bool bSearchInParents)157 sal_uInt16 getTransparenceForSvxBrushItem(const SfxItemSet& rSourceSet, sal_Bool bSearchInParents)
158 {
159     sal_uInt16 nFillTransparence(static_cast< const XFillTransparenceItem& >(rSourceSet.Get(XATTR_FILLTRANSPARENCE, bSearchInParents)).GetValue());
160     const SfxPoolItem* pGradientItem = 0;
161 
162     if(SFX_ITEM_SET == rSourceSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE, bSearchInParents, &pGradientItem)
163         && static_cast< const XFillFloatTransparenceItem* >(pGradientItem)->IsEnabled())
164     {
165         const XGradient& rGradient = static_cast< const XFillFloatTransparenceItem* >(pGradientItem)->GetGradientValue();
166         const sal_uInt16 nStartLuminance(rGradient.GetStartColor().GetLuminance());
167         const sal_uInt16 nEndLuminance(rGradient.GetEndColor().GetLuminance());
168 
169         // luminance is [0..255], transparence needs to be in [0..100].Maximum is 51200, thus sal_uInt16 is okay to use
170         nFillTransparence = static_cast< sal_uInt16 >(((nStartLuminance + nEndLuminance) * 100) / 512);
171     }
172 
173     return nFillTransparence;
174 }
175 
176 //UUUU
getSvxBrushItemForSolid(const SfxItemSet & rSourceSet,sal_Bool bSearchInParents,sal_uInt16 nBackgroundID)177 SvxBrushItem getSvxBrushItemForSolid(const SfxItemSet& rSourceSet, sal_Bool bSearchInParents, sal_uInt16 nBackgroundID)
178 {
179     Color aFillColor(static_cast< const XFillColorItem& >(rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents)).GetColorValue());
180 
181     // get evtl. mixed transparence
182     const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
183 
184     if(0 != nFillTransparence)
185     {
186         // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
187         // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
188         // since the oxff value is used for special purposes (like no fill and derive from parent)
189         const sal_uInt8 aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8 >((nFillTransparence * 254) / 100)));
190 
191         aFillColor.SetTransparency(aTargetTrans);
192     }
193 
194     return SvxBrushItem(aFillColor, nBackgroundID);
195 }
196 
197 //UUUU
getSvxBrushItemFromSourceSet(const SfxItemSet & rSourceSet,sal_uInt16 nBackgroundID,sal_Bool bSearchInParents)198 SvxBrushItem getSvxBrushItemFromSourceSet(const SfxItemSet& rSourceSet, sal_uInt16 nBackgroundID, sal_Bool bSearchInParents)
199 {
200     const XFillStyleItem* pXFillStyleItem(static_cast< const XFillStyleItem*  >(rSourceSet.GetItem(XATTR_FILLSTYLE, bSearchInParents)));
201 
202     if(!pXFillStyleItem || XFILL_NONE == pXFillStyleItem->GetValue())
203     {
204         // no fill, still need to rescue the evtl. set RGB color, but use as transparent color (we have XFILL_NONE)
205         Color aFillColor(static_cast< const XFillColorItem& >(rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents)).GetColorValue());
206         aFillColor.SetTransparency(0xff);
207 
208         return SvxBrushItem(aFillColor, nBackgroundID);
209     }
210 
211     SvxBrushItem aRetval(nBackgroundID);
212 
213     switch(pXFillStyleItem->GetValue())
214     {
215         case XFILL_NONE:
216         {
217             // already handled above, can not happen again
218             break;
219         }
220         case XFILL_SOLID:
221         {
222             // create SvxBrushItem with fill color
223             aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
224             break;
225         }
226         case XFILL_GRADIENT:
227         {
228             // cannot be directly supported, but do the best possible
229             const XGradient aXGradient(static_cast< const XFillGradientItem& >(rSourceSet.Get(XATTR_FILLGRADIENT)).GetGradientValue());
230             const basegfx::BColor aStartColor(aXGradient.GetStartColor().getBColor() * (aXGradient.GetStartIntens() * 0.01));
231             const basegfx::BColor aEndColor(aXGradient.GetEndColor().getBColor() * (aXGradient.GetEndIntens() * 0.01));
232 
233             // use half/half mixed color from gradient start and end
234             Color aMixedColor((aStartColor + aEndColor) * 0.5);
235 
236             // get evtl. mixed transparence
237             const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
238 
239             if(0 != nFillTransparence)
240             {
241                 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
242                 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
243                 // since the oxff value is used for special purposes (like no fill and derive from parent)
244                 const sal_uInt8 aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8 >((nFillTransparence * 254) / 100)));
245 
246                 aMixedColor.SetTransparency(aTargetTrans);
247             }
248 
249             aRetval = SvxBrushItem(aMixedColor, nBackgroundID);
250             break;
251         }
252         case XFILL_HATCH:
253         {
254             // cannot be directly supported, but do the best possible
255             const XHatch& rHatch(static_cast< const XFillHatchItem& >(rSourceSet.Get(XATTR_FILLHATCH)).GetHatchValue());
256             const bool bFillBackground(static_cast< const XFillBackgroundItem& >(rSourceSet.Get(XATTR_FILLBACKGROUND)).GetValue());
257 
258             if(bFillBackground)
259             {
260                 // hatch is background-filled, use FillColor as if XFILL_SOLID
261                 aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
262             }
263             else
264             {
265                 // hatch is not background-filled and using hatch color would be too dark; compensate
266                 // somewhat by making it more transparent
267                 Color aHatchColor(rHatch.GetColor());
268 
269                 // get evtl. mixed transparence
270                 sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
271 
272                 // take half orig transparence, add half transparent, clamp result
273                 nFillTransparence = basegfx::clamp((sal_uInt16)((nFillTransparence / 2) + 50), (sal_uInt16)0, (sal_uInt16)255);
274 
275                 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..254] unsigned
276                 // It is necessary to use the maximum of 0xfe for transparence for the SvxBrushItem
277                 // since the oxff value is used for special purposes (like no fill and derive from parent)
278                 const sal_uInt8 aTargetTrans(std::min(sal_uInt8(0xfe), static_cast< sal_uInt8 >((nFillTransparence * 254) / 100)));
279 
280                 aHatchColor.SetTransparency(aTargetTrans);
281                 aRetval = SvxBrushItem(aHatchColor, nBackgroundID);
282             }
283 
284             break;
285         }
286         case XFILL_BITMAP:
287         {
288             // create SvxBrushItem with bitmap info and flags
289             const XFillBitmapItem& rBmpItm = static_cast< const XFillBitmapItem& >(rSourceSet.Get(XATTR_FILLBITMAP, bSearchInParents));
290             const Graphic aGraphic(rBmpItm.GetGraphicObject().GetGraphic());
291 
292             // continue idependent of evtl. GRAPHIC_NONE as aGraphic.GetType(), we still need to rescue positions
293             SvxGraphicPosition aSvxGraphicPosition(GPOS_NONE);
294             const XFillBmpStretchItem& rStretchItem = static_cast< const XFillBmpStretchItem& >(rSourceSet.Get(XATTR_FILLBMP_STRETCH, bSearchInParents));
295             const XFillBmpTileItem& rTileItem = static_cast< const XFillBmpTileItem& >(rSourceSet.Get(XATTR_FILLBMP_TILE, bSearchInParents));
296 
297             if(rTileItem.GetValue())
298             {
299                 aSvxGraphicPosition = GPOS_TILED;
300             }
301             else if(rStretchItem.GetValue())
302             {
303                 aSvxGraphicPosition = GPOS_AREA;
304             }
305             else
306             {
307                 const XFillBmpPosItem& rPosItem = static_cast< const XFillBmpPosItem& >(rSourceSet.Get(XATTR_FILLBMP_POS, bSearchInParents));
308 
309                 switch(rPosItem.GetValue())
310                 {
311                     case RP_LT: aSvxGraphicPosition = GPOS_LT; break;
312                     case RP_MT: aSvxGraphicPosition = GPOS_MT; break;
313                     case RP_RT: aSvxGraphicPosition = GPOS_RT; break;
314                     case RP_LM: aSvxGraphicPosition = GPOS_LM; break;
315                     case RP_MM: aSvxGraphicPosition = GPOS_MM; break;
316                     case RP_RM: aSvxGraphicPosition = GPOS_RM; break;
317                     case RP_LB: aSvxGraphicPosition = GPOS_LB; break;
318                     case RP_MB: aSvxGraphicPosition = GPOS_MB; break;
319                     case RP_RB: aSvxGraphicPosition = GPOS_RB; break;
320                 }
321             }
322 
323             // create with given graphic and position
324             aRetval = SvxBrushItem(aGraphic, aSvxGraphicPosition, nBackgroundID);
325 
326             // get evtl. mixed transparence
327             const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
328 
329             if(0 != nFillTransparence)
330             {
331                 // #i125189# nFillTransparence is in range [0..100] and needs to be in [0..100] signed
332                 aRetval.setGraphicTransparency(static_cast< sal_Int8 >(nFillTransparence));
333             }
334 
335             break;
336         }
337     }
338 
339     return aRetval;
340 }
341 
342 //eof
343