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
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         // nTransparency is in range [0..255], convert to [0..100] which is used in XFillTransparenceItem
66         rToSet.Put(XFillTransparenceItem((((sal_Int32)nTransparency * 100) + 127) / 255));
67     }
68     else if(GPOS_NONE != rBrush.GetGraphicPos())
69     {
70         // we have a graphic fill, set fill style
71         rToSet.Put(XFillStyleItem(XFILL_BITMAP));
72 
73         // set graphic (if available)
74         const Graphic* pGraphic = rBrush.GetGraphic();
75 
76         if(pGraphic)
77         {
78             rToSet.Put(XFillBitmapItem(String(), *pGraphic));
79         }
80         else
81         {
82             OSL_ENSURE(false, "Could not get Graphic from SvxBrushItem (!)");
83         }
84 
85         if(GPOS_AREA == rBrush.GetGraphicPos())
86         {
87             // stretch, also means no tile (both items are defaulted to true)
88             rToSet.Put(XFillBmpStretchItem(true));
89             rToSet.Put(XFillBmpTileItem(false));
90 
91             // default for strech is also top-left, but this will not be visible
92             rToSet.Put(XFillBmpPosItem(RP_LT));
93         }
94         else if(GPOS_TILED == rBrush.GetGraphicPos())
95         {
96             // tiled, also means no stretch (both items are defaulted to true)
97             rToSet.Put(XFillBmpStretchItem(false));
98             rToSet.Put(XFillBmpTileItem(true));
99 
100             // default for tiled is top-left
101             rToSet.Put(XFillBmpPosItem(RP_LT));
102         }
103         else
104         {
105             // everything else means no tile and no stretch
106             rToSet.Put(XFillBmpStretchItem(false));
107             rToSet.Put(XFillBmpTileItem(false));
108 
109             RECT_POINT aRectPoint(RP_MM);
110 
111             switch(rBrush.GetGraphicPos())
112             {
113                 case GPOS_LT: aRectPoint = RP_LT; break;
114                 case GPOS_MT: aRectPoint = RP_MT; break;
115                 case GPOS_RT: aRectPoint = RP_RT; break;
116                 case GPOS_LM: aRectPoint = RP_LM; break;
117                 case GPOS_MM: aRectPoint = RP_MM; break;
118                 case GPOS_RM: aRectPoint = RP_RM; break;
119                 case GPOS_LB: aRectPoint = RP_LB; break;
120                 case GPOS_MB: aRectPoint = RP_MB; break;
121                 case GPOS_RB: aRectPoint = RP_RB; break;
122                 default: break; // GPOS_NONE, GPOS_AREA and GPOS_TILED already handled
123             }
124 
125             rToSet.Put(XFillBmpPosItem(aRectPoint));
126         }
127 
128         // check for graphic's transparency
129         const sal_Int8 nGraphicTransparency(rBrush.getGraphicTransparency());
130 
131         if(0 != nGraphicTransparency)
132         {
133             // nGraphicTransparency is in range [0..100]
134             rToSet.Put(XFillTransparenceItem(nGraphicTransparency));
135         }
136     }
137     else
138     {
139         // GPOS_NONE == rBrush.GetGraphicPos() && 0xff == rBrush.GetColor().GetTransparency(),
140         // still need to rescue the color used. There are sequences used on the UNO API at
141         // import time (OLE. e.g. chart) which first set RGB color (MID_BACK_COLOR_R_G_B,
142         // color stays transparent) and then set transparency (MID_BACK_COLOR_TRANSPARENCY)
143         // to zero later. When not saving the color, it will be lost.
144         // Also need to set the FillStyle to NONE to express the 0xff transparency flag; this
145         // is needed when e.g. first transparency is set to 0xff and then a Graphic gets set.
146         // When not changing the FillStyle, the next getSvxBrushItemFromSourceSet *will* return
147         // to XFILL_SOLID with the rescued color.
148         const Color aColor(rBrush.GetColor().GetRGBColor());
149 
150         rToSet.Put(XFillStyleItem(XFILL_NONE));
151         rToSet.Put(XFillColorItem(String(), aColor));
152     }
153 }
154 
155 //UUUU
156 sal_uInt16 getTransparenceForSvxBrushItem(const SfxItemSet& rSourceSet, sal_Bool bSearchInParents)
157 {
158     sal_uInt16 nFillTransparence(static_cast< const XFillTransparenceItem& >(rSourceSet.Get(XATTR_FILLTRANSPARENCE, bSearchInParents)).GetValue());
159     const SfxPoolItem* pGradientItem = 0;
160 
161     if(SFX_ITEM_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 
175 //UUUU
176 SvxBrushItem getSvxBrushItemForSolid(const SfxItemSet& rSourceSet, sal_Bool bSearchInParents, sal_uInt16 nBackgroundID)
177 {
178     Color aFillColor(static_cast< const XFillColorItem& >(rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents)).GetColorValue());
179 
180     // get evtl. mixed transparence
181     const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
182 
183     if(0 != nFillTransparence)
184     {
185         // nFillTransparence is in range [0..100] and needs to be in [0..255] unsigned
186         aFillColor.SetTransparency(static_cast< sal_uInt8 >((nFillTransparence * 255) / 100));
187     }
188 
189     return SvxBrushItem(aFillColor, nBackgroundID);
190 }
191 
192 //UUUU
193 SvxBrushItem getSvxBrushItemFromSourceSet(const SfxItemSet& rSourceSet, sal_uInt16 nBackgroundID, sal_Bool bSearchInParents)
194 {
195     const XFillStyleItem* pXFillStyleItem(static_cast< const XFillStyleItem*  >(rSourceSet.GetItem(XATTR_FILLSTYLE, bSearchInParents)));
196 
197     if(!pXFillStyleItem || XFILL_NONE == pXFillStyleItem->GetValue())
198     {
199         // no fill, still need to rescue the evtl. set RGB color, but use as transparent color (we have XFILL_NONE)
200         Color aFillColor(static_cast< const XFillColorItem& >(rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents)).GetColorValue());
201         aFillColor.SetTransparency(0xff);
202 
203         return SvxBrushItem(aFillColor, nBackgroundID);
204     }
205 
206     SvxBrushItem aRetval(nBackgroundID);
207 
208     switch(pXFillStyleItem->GetValue())
209     {
210         case XFILL_NONE:
211         {
212             // already handled above, can not happen again
213             break;
214         }
215         case XFILL_SOLID:
216         {
217             // create SvxBrushItem with fill color
218             aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
219             break;
220         }
221         case XFILL_GRADIENT:
222         {
223             // cannot be directly supported, but do the best possible
224             const XGradient aXGradient(static_cast< const XFillGradientItem& >(rSourceSet.Get(XATTR_FILLGRADIENT)).GetGradientValue());
225             const basegfx::BColor aStartColor(aXGradient.GetStartColor().getBColor() * (aXGradient.GetStartIntens() * 0.01));
226             const basegfx::BColor aEndColor(aXGradient.GetEndColor().getBColor() * (aXGradient.GetEndIntens() * 0.01));
227 
228             // use half/half mixed color from gradient start and end
229             Color aMixedColor((aStartColor + aEndColor) * 0.5);
230 
231             // get evtl. mixed transparence
232             const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
233 
234             if(0 != nFillTransparence)
235             {
236                 // nFillTransparence is in range [0..100] and needs to be in [0..255] unsigned
237                 aMixedColor.SetTransparency(static_cast< sal_uInt8 >((nFillTransparence * 255) / 100));
238             }
239 
240             aRetval = SvxBrushItem(aMixedColor, nBackgroundID);
241             break;
242         }
243         case XFILL_HATCH:
244         {
245             // cannot be directly supported, but do the best possible
246             const XHatch& rHatch(static_cast< const XFillHatchItem& >(rSourceSet.Get(XATTR_FILLHATCH)).GetHatchValue());
247             const bool bFillBackground(static_cast< const XFillBackgroundItem& >(rSourceSet.Get(XATTR_FILLBACKGROUND)).GetValue());
248 
249             if(bFillBackground)
250             {
251                 // hatch is background-filled, use FillColor as if XFILL_SOLID
252                 aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents, nBackgroundID);
253             }
254             else
255             {
256                 // hatch is not background-filled and using hatch color would be too dark; compensate
257                 // somewhat by making it more transparent
258                 Color aHatchColor(rHatch.GetColor());
259 
260                 // get evtl. mixed transparence
261                 sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
262 
263                 // take half orig transparence, add half transparent, clamp result
264                 nFillTransparence = basegfx::clamp((sal_uInt16)((nFillTransparence / 2) + 50), (sal_uInt16)0, (sal_uInt16)255);
265 
266                 // nFillTransparence is in range [0..100] and needs to be in [0..255] unsigned
267                 aHatchColor.SetTransparency(static_cast< sal_uInt8 >((nFillTransparence * 255) / 100));
268 
269                 aRetval = SvxBrushItem(aHatchColor, nBackgroundID);
270             }
271 
272             break;
273         }
274         case XFILL_BITMAP:
275         {
276             // create SvxBrushItem with bitmap info and flags
277             const XFillBitmapItem& rBmpItm = static_cast< const XFillBitmapItem& >(rSourceSet.Get(XATTR_FILLBITMAP, bSearchInParents));
278             const Graphic aGraphic(rBmpItm.GetGraphicObject().GetGraphic());
279 
280             // continue idependent of evtl. GRAPHIC_NONE as aGraphic.GetType(), we still need to rescue positions
281             SvxGraphicPosition aSvxGraphicPosition(GPOS_NONE);
282             const XFillBmpStretchItem& rStretchItem = static_cast< const XFillBmpStretchItem& >(rSourceSet.Get(XATTR_FILLBMP_STRETCH, bSearchInParents));
283             const XFillBmpTileItem& rTileItem = static_cast< const XFillBmpTileItem& >(rSourceSet.Get(XATTR_FILLBMP_TILE, bSearchInParents));
284 
285             if(rTileItem.GetValue())
286             {
287                 aSvxGraphicPosition = GPOS_TILED;
288             }
289             else if(rStretchItem.GetValue())
290             {
291                 aSvxGraphicPosition = GPOS_AREA;
292             }
293             else
294             {
295                 const XFillBmpPosItem& rPosItem = static_cast< const XFillBmpPosItem& >(rSourceSet.Get(XATTR_FILLBMP_POS, bSearchInParents));
296 
297                 switch(rPosItem.GetValue())
298                 {
299                     case RP_LT: aSvxGraphicPosition = GPOS_LT; break;
300                     case RP_MT: aSvxGraphicPosition = GPOS_MT; break;
301                     case RP_RT: aSvxGraphicPosition = GPOS_RT; break;
302                     case RP_LM: aSvxGraphicPosition = GPOS_LM; break;
303                     case RP_MM: aSvxGraphicPosition = GPOS_MM; break;
304                     case RP_RM: aSvxGraphicPosition = GPOS_RM; break;
305                     case RP_LB: aSvxGraphicPosition = GPOS_LB; break;
306                     case RP_MB: aSvxGraphicPosition = GPOS_MB; break;
307                     case RP_RB: aSvxGraphicPosition = GPOS_RB; break;
308                 }
309             }
310 
311             // create with given graphic and position
312             aRetval = SvxBrushItem(aGraphic, aSvxGraphicPosition, nBackgroundID);
313 
314             // get evtl. mixed transparence
315             const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));
316 
317             if(0 != nFillTransparence)
318             {
319                 // nFillTransparence is in range [0..100] and needs to be in [0..100] signed
320                 aRetval.setGraphicTransparency(static_cast< sal_Int8 >(nFillTransparence));
321             }
322 
323             break;
324         }
325     }
326 
327     return aRetval;
328 }
329 
330 //eof
331