LCOV - code coverage report
Current view: top level - svx/source/unodraw - unobrushitemhelper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 86 122 70.5 %
Date: 2014-11-03 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10