summaryrefslogtreecommitdiff
path: root/sw/source/core/unocore/unobrushitemhelper.cxx
blob: d28c40e1543d84da4105bba4c42530f141104e91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <unobrushitemhelper.hxx>
#include <svx/xfillit0.hxx>
#include <svx/xbtmpit.hxx>
#include <svx/xgrscit.hxx>
#include <svx/xflbmtit.hxx>
#include <svx/xflbmpit.hxx>
#include <svx/xflbmsxy.hxx>
#include <svx/xflbmsxy.hxx>
#include <svx/xflftrit.hxx>
#include <svx/xsflclit.hxx>
#include <svx/xflbmsli.hxx>
#include <svx/xflbtoxy.hxx>
#include <svx/xflbstit.hxx>
#include <svx/xflboxy.hxx>
#include <svx/xflbckit.hxx>
#include <svx/unoshape.hxx>
#include <hintids.hxx>
#include <svx/xflclit.hxx>
#include <svx/xfltrit.hxx>
#include <svx/xflhtit.hxx>

namespace sw {

//UUUU
void setSvxBrushItemAsFillAttributesToTargetSet(const SvxBrushItem& rBrush, SfxItemSet& rToSet)
{
    if(0xff != rBrush.GetColor().GetTransparency())
    {
        const Color aColor(rBrush.GetColor().GetRGBColor());
        const sal_uInt8 nTransparency(rBrush.GetColor().GetTransparency());

        rToSet.Put(XFillStyleItem(XFILL_SOLID));
        rToSet.Put(XFillColorItem(OUString(), aColor));

        if(0xff != nTransparency)
        {
            // nTransparency is in range [0..255]
            rToSet.Put(XFillTransparenceItem((((sal_Int32)nTransparency * 100) + 127) / 255));
        }
    }
    else if(GPOS_NONE != rBrush.GetGraphicPos())
    {
        const Graphic* pGraphic = rBrush.GetGraphic();

        if(pGraphic)
        {
            // set fill style and graphic itself
            rToSet.Put(XFillStyleItem(XFILL_BITMAP));
            rToSet.Put(XFillBitmapItem(OUString(), *pGraphic));

            // set defaults
            // already pool default rToSet.Put(XFillBmpPosItem(RP_MM));
            // already pool default rToSet.Put(XFillBmpTileOffsetXItem(0));
            // already pool default rToSet.Put(XFillBmpTileOffsetYItem(0));
            // already pool default rToSet.Put(XFillBmpPosOffsetXItem(0));
            // already pool default rToSet.Put(XFillBmpPosOffsetYItem(0));
            // already pool default rToSet.Put(XFillBmpSizeLogItem(true));
            // already pool default rToSet.Put(XFillBmpSizeXItem(0));
            // already pool default rToSet.Put(XFillBmpSizeYItem(0));

            if(GPOS_AREA == rBrush.GetGraphicPos())
            {
                // stretch, also means no tile (both items are defaulted to true)
                // rToSet.Put(XFillBmpStretchItem(true));
                rToSet.Put(XFillBmpTileItem(false));

                // default for strech is also top-left, but this will not be visible
                // rToSet.Put(XFillBmpPosItem(RP_LT));
            }
            else if(GPOS_TILED == rBrush.GetGraphicPos())
            {
                // tiled, also means no stretch (both items are defaulted to true)
                rToSet.Put(XFillBmpStretchItem(false));
                //rToSet.Put(XFillBmpTileItem(true));

                // default for tiled is top-left
                rToSet.Put(XFillBmpPosItem(RP_LT));
            }
            else
            {
                // everything else means no tile and no stretch
                rToSet.Put(XFillBmpStretchItem(false));
                rToSet.Put(XFillBmpTileItem(false));

                switch(rBrush.GetGraphicPos())
                {
                    case GPOS_LT: rToSet.Put(XFillBmpPosItem(RP_LT)); break;
                    case GPOS_MT: rToSet.Put(XFillBmpPosItem(RP_MT)); break;
                    case GPOS_RT: rToSet.Put(XFillBmpPosItem(RP_RT)); break;
                    case GPOS_LM: rToSet.Put(XFillBmpPosItem(RP_LM)); break;
                    case GPOS_MM: rToSet.Put(XFillBmpPosItem(RP_MM)); break;
                    case GPOS_RM: rToSet.Put(XFillBmpPosItem(RP_RM)); break;
                    case GPOS_LB: rToSet.Put(XFillBmpPosItem(RP_LB)); break;
                    case GPOS_MB: rToSet.Put(XFillBmpPosItem(RP_MB)); break;
                    case GPOS_RB: rToSet.Put(XFillBmpPosItem(RP_RB)); break;
                    default: break; // already handled GPOS_AREA, GPOS_TILED and GPOS_NONE
                }
            }

            // check for transparency
            const sal_Int8 nTransparency(rBrush.getGraphicTransparency());

            if(0 != nTransparency)
            {
                // nTransparency is in range [0..100]
                rToSet.Put(XFillTransparenceItem(nTransparency));
            }
        }
        else
        {
            OSL_ENSURE(false, "Could not get Graphic from SvxBrushItem (!)");
        }
    }
    else
    {
        // GPOS_NONE == rBrush.GetGraphicPos() && 0xff == rBrush.GetColor().GetTransparency(),
        // still need to rescue the color used. There are sequences used on the UNO API at
        // import time (OLE. e.g. chart) which first set RGB color (MID_BACK_COLOR_R_G_B,
        // color stays transparent) and then set transparency (MID_BACK_COLOR_TRANSPARENCY)
        // to zero later. When not saving the color, it will be lost
        const Color aColor(rBrush.GetColor().GetRGBColor());

        // rToSet.Put(XFillStyleItem(XFILL_NONE));
        rToSet.Put(XFillColorItem(OUString(), aColor));
    }
}

//UUUU
sal_uInt16 getTransparenceForSvxBrushItem(const SfxItemSet& rSourceSet, sal_Bool bSearchInParents)
{
    sal_uInt16 nFillTransparence(static_cast< const XFillTransparenceItem& >(rSourceSet.Get(XATTR_FILLTRANSPARENCE, bSearchInParents)).GetValue());
    const SfxPoolItem* pGradientItem = 0;

    if(SFX_ITEM_SET == rSourceSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE, bSearchInParents, &pGradientItem)
        && static_cast< const XFillFloatTransparenceItem* >(pGradientItem)->IsEnabled())
    {
        const XGradient& rGradient = static_cast< const XFillFloatTransparenceItem* >(pGradientItem)->GetGradientValue();
        const sal_uInt16 nStartLuminance(rGradient.GetStartColor().GetLuminance());
        const sal_uInt16 nEndLuminance(rGradient.GetEndColor().GetLuminance());

        // luminance is [0..255], transparence needs to be in [0..100].Maximum is 51200, thus sal_uInt16 is okay to use
        nFillTransparence = static_cast< sal_uInt16 >(((nStartLuminance + nEndLuminance) * 100) / 512);
    }

    return nFillTransparence;
}

//UUUU
SvxBrushItem getSvxBrushItemForSolid(const SfxItemSet& rSourceSet, sal_Bool bSearchInParents)
{
    Color aFillColor(static_cast< const XFillColorItem& >(rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents)).GetColorValue());

    // get evtl. mixed transparence
    const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));

    if(0 != nFillTransparence)
    {
        // nFillTransparence is in range [0..100] and needs to be in [0..255] unsigned
        aFillColor.SetTransparency(static_cast< sal_uInt8 >((nFillTransparence * 255) / 100));
    }

    return SvxBrushItem(aFillColor, RES_BACKGROUND);
}

//UUUU
SvxBrushItem getSvxBrushItemFromSourceSet(const SfxItemSet& rSourceSet, sal_Bool bSearchInParents)
{
    const XFillStyleItem* pXFillStyleItem(static_cast< const XFillStyleItem*  >(rSourceSet.GetItem(XATTR_FILLSTYLE, bSearchInParents)));

    if(!pXFillStyleItem || XFILL_NONE == pXFillStyleItem->GetValue())
    {
        // need to rescue the evtl. set RGB color, but use as transparent color (we have XFILL_NONE)
        Color aFillColor(static_cast< const XFillColorItem& >(rSourceSet.Get(XATTR_FILLCOLOR, bSearchInParents)).GetColorValue());
        aFillColor.SetTransparency(0xff);

        return SvxBrushItem(aFillColor, RES_BACKGROUND);
    }

    SvxBrushItem aRetval(RES_BACKGROUND);

    switch(pXFillStyleItem->GetValue())
    {
        case XFILL_NONE:
        {
            // already handled above, can not happen again
            break;
        }
        case XFILL_SOLID:
        {
            // create SvxBrushItem with fill color
            aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents);
            break;
        }
        case XFILL_GRADIENT:
        {
            // cannot be directly supported, but do the best possible
            const XGradient aXGradient(static_cast< const XFillGradientItem& >(rSourceSet.Get(XATTR_FILLGRADIENT)).GetGradientValue());
            const basegfx::BColor aStartColor(aXGradient.GetStartColor().getBColor() * (aXGradient.GetStartIntens() * 0.01));
            const basegfx::BColor aEndColor(aXGradient.GetEndColor().getBColor() * (aXGradient.GetEndIntens() * 0.01));

            // use half/half mixed color from gradient start and end
            Color aMixedColor((aStartColor + aEndColor) * 0.5);

            // get evtl. mixed transparence
            const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));

            if(0 != nFillTransparence)
            {
                // nFillTransparence is in range [0..100] and needs to be in [0..255] unsigned
                aMixedColor.SetTransparency(static_cast< sal_uInt8 >((nFillTransparence * 255) / 100));
            }

            aRetval = SvxBrushItem(aMixedColor, RES_BACKGROUND);
            break;
        }
        case XFILL_HATCH:
        {
            // cannot be directly supported, but do the best possible
            const XHatch& rHatch(static_cast< const XFillHatchItem& >(rSourceSet.Get(XATTR_FILLHATCH)).GetHatchValue());
            const bool bFillBackground(static_cast< const XFillBackgroundItem& >(rSourceSet.Get(XATTR_FILLBACKGROUND)).GetValue());

            if(bFillBackground)
            {
                // hatch is background-filled, use FillColor as if XFILL_SOLID
                aRetval = getSvxBrushItemForSolid(rSourceSet, bSearchInParents);
            }
            else
            {
                // hatch is not background-filled and using hatch color would be too dark; compensate
                // somewhat by making it more transparent
                Color aHatchColor(rHatch.GetColor());

                // get evtl. mixed transparence
                sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));

                // take half orig transparence, add half transparent, clamp result
                nFillTransparence = basegfx::clamp((sal_uInt16)((nFillTransparence / 2) + 50), (sal_uInt16)0, (sal_uInt16)255);

                // nFillTransparence is in range [0..100] and needs to be in [0..255] unsigned
                aHatchColor.SetTransparency(static_cast< sal_uInt8 >((nFillTransparence * 255) / 100));

                aRetval = SvxBrushItem(aHatchColor, RES_BACKGROUND);
            }

            break;
        }
        case XFILL_BITMAP:
        {
            // create SvxBrushItem with bitmap info and flags
            const XFillBitmapItem& rBmpItm = static_cast< const XFillBitmapItem& >(rSourceSet.Get(XATTR_FILLBITMAP, bSearchInParents));
            const Graphic aGraphic(rBmpItm.GetGraphicObject().GetGraphic());

            if(GRAPHIC_NONE != aGraphic.GetType())
            {
                // get graphic position
                SvxGraphicPosition aSvxGraphicPosition(GPOS_NONE);
                const XFillBmpStretchItem& rStretchItem = static_cast< const XFillBmpStretchItem& >(rSourceSet.Get(XATTR_FILLBMP_STRETCH, bSearchInParents));
                const XFillBmpTileItem& rTileItem = static_cast< const XFillBmpTileItem& >(rSourceSet.Get(XATTR_FILLBMP_TILE, bSearchInParents));

                if(rTileItem.GetValue())
                {
                    aSvxGraphicPosition = GPOS_TILED;
                }
                else if(rStretchItem.GetValue())
                {
                    aSvxGraphicPosition = GPOS_AREA;
                }
                else
                {
                    const XFillBmpPosItem& rPosItem = static_cast< const XFillBmpPosItem& >(rSourceSet.Get(XATTR_FILLBMP_POS, bSearchInParents));

                    switch(rPosItem.GetValue())
                    {
                        case RP_LT: aSvxGraphicPosition = GPOS_LT; break;
                        case RP_MT: aSvxGraphicPosition = GPOS_MT; break;
                        case RP_RT: aSvxGraphicPosition = GPOS_RT; break;
                        case RP_LM: aSvxGraphicPosition = GPOS_LM; break;
                        case RP_MM: aSvxGraphicPosition = GPOS_MM; break;
                        case RP_RM: aSvxGraphicPosition = GPOS_RM; break;
                        case RP_LB: aSvxGraphicPosition = GPOS_LB; break;
                        case RP_MB: aSvxGraphicPosition = GPOS_MB; break;
                        case RP_RB: aSvxGraphicPosition = GPOS_RB; break;
                    }
                }

                // create with given graphic and position
                aRetval = SvxBrushItem(aGraphic, aSvxGraphicPosition, RES_BACKGROUND);

                // get evtl. mixed transparence
                const sal_uInt16 nFillTransparence(getTransparenceForSvxBrushItem(rSourceSet, bSearchInParents));

                if(0 != nFillTransparence)
                {
                    // nFillTransparence is in range [0..100] and needs to be in [0..100] signed
                    aRetval.setGraphicTransparency(static_cast< sal_Int8 >(nFillTransparence));
                }
            }

            break;
        }
    }

    return aRetval;
}

} // namespace sw

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */