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

Generated by: LCOV version 1.11