Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/outdev/wallpaper.cxx
Line
Count
Source
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 <vcl/metaact.hxx>
21
#include <vcl/virdev.hxx>
22
#include <salgdi.hxx>
23
24
#include <cassert>
25
26
Color OutputDevice::GetReadableFontColor(const Color& rFontColor, const Color& rBgColor) const
27
4.60M
{
28
4.60M
    if (rBgColor.IsDark() && rFontColor.IsDark())
29
4.60M
        return COL_WHITE;
30
0
    else if (rBgColor.IsBright() && rFontColor.IsBright())
31
0
        return COL_BLACK;
32
0
    else
33
0
        return rFontColor;
34
4.60M
}
35
36
void OutputDevice::DrawWallpaper( const tools::Rectangle& rRect,
37
                                  const Wallpaper& rWallpaper )
38
2.59k
{
39
2.59k
    assert(!is_double_buffered_window());
40
41
2.59k
    if ( mpMetaFile )
42
0
        mpMetaFile->AddAction( new MetaWallpaperAction( rRect, rWallpaper ) );
43
44
2.59k
    if ( !IsDeviceOutputNecessary() || ImplIsRecordLayout() )
45
0
        return;
46
47
2.59k
    if ( rWallpaper.GetStyle() != WallpaperStyle::NONE )
48
98
    {
49
98
        tools::Rectangle aRect = LogicToPixel( rRect );
50
98
        aRect.Normalize();
51
52
98
        if ( !aRect.IsEmpty() )
53
0
        {
54
0
            DrawWallpaper( aRect.Left(), aRect.Top(), aRect.GetWidth(), aRect.GetHeight(),
55
0
                               rWallpaper );
56
0
        }
57
98
    }
58
2.59k
}
59
60
void OutputDevice::DrawWallpaper( tools::Long nX, tools::Long nY,
61
                                  tools::Long nWidth, tools::Long nHeight,
62
                                  const Wallpaper& rWallpaper )
63
12.7k
{
64
12.7k
    assert(!is_double_buffered_window());
65
66
12.7k
    if( rWallpaper.IsBitmap() )
67
0
        DrawBitmapWallpaper( nX, nY, nWidth, nHeight, rWallpaper );
68
12.7k
    else if( rWallpaper.IsGradient() )
69
0
        DrawGradientWallpaper( nX, nY, nWidth, nHeight, rWallpaper );
70
12.7k
    else
71
12.7k
        DrawColorWallpaper(  nX, nY, nWidth, nHeight, rWallpaper );
72
12.7k
}
73
74
void OutputDevice::DrawColorWallpaper( tools::Long nX, tools::Long nY,
75
                                       tools::Long nWidth, tools::Long nHeight,
76
                                       const Wallpaper& rWallpaper )
77
12.7k
{
78
12.7k
    assert(!is_double_buffered_window());
79
80
    // draw wallpaper without border
81
12.7k
    bool bOldIsLineColor = IsLineColor();
82
12.7k
    Color aOldLineColor = GetLineColor();
83
12.7k
    bool bOldIsFillColor = IsFillColor();
84
12.7k
    Color aOldFillColor = GetFillColor();
85
12.7k
    bool bMap = mbMap;
86
87
12.7k
    SetLineColor();
88
12.7k
    SetFillColor( rWallpaper.GetColor() );
89
12.7k
    EnableMapMode( false );
90
91
12.7k
    DrawRect( tools::Rectangle( Point( nX, nY ), Size( nWidth, nHeight ) ) );
92
93
12.7k
    EnableMapMode( bMap );
94
12.7k
    if (bOldIsFillColor)
95
12.7k
        SetFillColor(aOldFillColor);
96
0
    else
97
0
        SetFillColor();
98
12.7k
    if (bOldIsLineColor)
99
12.7k
        SetLineColor(aOldLineColor);
100
0
    else
101
0
        SetLineColor();
102
12.7k
}
103
104
void OutputDevice::Erase()
105
17.3k
{
106
17.3k
    if ( !IsDeviceOutputNecessary() || ImplIsRecordLayout() )
107
4.57k
        return;
108
109
12.7k
    if ( mbBackground )
110
12.7k
    {
111
12.7k
        RasterOp eRasterOp = GetRasterOp();
112
12.7k
        if ( eRasterOp != RasterOp::OverPaint )
113
0
            SetRasterOp( RasterOp::OverPaint );
114
12.7k
        DrawWallpaper( 0, 0, mnOutWidth, mnOutHeight, maBackground );
115
12.7k
        if ( eRasterOp != RasterOp::OverPaint )
116
0
            SetRasterOp( eRasterOp );
117
12.7k
    }
118
12.7k
}
119
120
void OutputDevice::Erase(const tools::Rectangle& rRect)
121
0
{
122
0
    const RasterOp eRasterOp = GetRasterOp();
123
0
    if ( eRasterOp != RasterOp::OverPaint )
124
0
        SetRasterOp( RasterOp::OverPaint );
125
0
    DrawWallpaper(rRect, GetBackground());
126
0
    if ( eRasterOp != RasterOp::OverPaint )
127
0
        SetRasterOp( eRasterOp );
128
0
}
129
130
void OutputDevice::DrawBitmapWallpaper( tools::Long nX, tools::Long nY,
131
                                            tools::Long nWidth, tools::Long nHeight,
132
                                            const Wallpaper& rWallpaper )
133
0
{
134
0
    assert(!is_double_buffered_window());
135
136
0
    if( ImplIsRecordLayout() )
137
0
        return;
138
139
0
    const Bitmap* pCached = rWallpaper.ImplGetCachedBitmap();
140
141
0
    GDIMetaFile* pOldMetaFile = mpMetaFile;
142
0
    const bool bOldMap = mbMap;
143
144
0
    Bitmap aBmp;
145
0
    if( pCached )
146
0
        aBmp = *pCached;
147
0
    else
148
0
        aBmp = rWallpaper.GetBitmap();
149
150
0
    const tools::Long nBmpWidth = aBmp.GetSizePixel().Width();
151
0
    const tools::Long nBmpHeight = aBmp.GetSizePixel().Height();
152
0
    const bool bTransparent = aBmp.HasAlpha();
153
154
0
    const WallpaperStyle eStyle = rWallpaper.GetStyle();
155
156
0
    bool bDrawGradientBackground = false;
157
0
    bool bDrawColorBackground = false;
158
159
    // draw background
160
0
    if( bTransparent )
161
0
    {
162
0
        if( rWallpaper.IsGradient() )
163
0
            bDrawGradientBackground = true;
164
0
        else
165
0
        {
166
0
            if( !pCached && !rWallpaper.GetColor().IsTransparent() )
167
0
            {
168
0
                ScopedVclPtrInstance< VirtualDevice > aVDev(  *this  );
169
0
                aVDev->SetBackground( rWallpaper.GetColor() );
170
0
                aVDev->SetOutputSizePixel( Size( nBmpWidth, nBmpHeight ) );
171
0
                aVDev->DrawBitmap( Point(), aBmp );
172
0
                aBmp = aVDev->GetBitmap( Point(), aVDev->GetOutputSizePixel() );
173
0
            }
174
175
0
            bDrawColorBackground = true;
176
0
        }
177
0
    }
178
0
    else if( eStyle != WallpaperStyle::Tile && eStyle != WallpaperStyle::Scale )
179
0
    {
180
0
        if( rWallpaper.IsGradient() )
181
0
            bDrawGradientBackground = true;
182
0
        else
183
0
            bDrawColorBackground = true;
184
0
    }
185
186
    // background of bitmap?
187
0
    if( bDrawGradientBackground )
188
0
    {
189
0
        DrawGradientWallpaper( nX, nY, nWidth, nHeight, rWallpaper );
190
0
    }
191
0
    else if( bDrawColorBackground && bTransparent )
192
0
    {
193
0
        DrawColorWallpaper( nX, nY, nWidth, nHeight, rWallpaper );
194
0
        bDrawColorBackground = false;
195
0
    }
196
197
0
    Point aPos;
198
0
    Size aSize;
199
200
    // calc pos and size
201
0
    if( rWallpaper.IsRect() )
202
0
    {
203
0
        const tools::Rectangle aBound( LogicToPixel( rWallpaper.GetRect() ) );
204
0
        aPos = aBound.TopLeft();
205
0
        aSize = aBound.GetSize();
206
0
    }
207
0
    else
208
0
    {
209
0
        aPos = Point( 0, 0 );
210
0
        aSize = Size( nWidth, nHeight );
211
0
    }
212
213
0
    mpMetaFile = nullptr;
214
0
    EnableMapMode( false );
215
0
    Push( vcl::PushFlags::CLIPREGION );
216
0
    IntersectClipRegion( tools::Rectangle( Point( nX, nY ), Size( nWidth, nHeight ) ) );
217
218
0
    bool bDrawn = false;
219
220
0
    switch( eStyle )
221
0
    {
222
0
    case WallpaperStyle::Scale:
223
0
        if( !pCached || ( pCached->GetSizePixel() != aSize ) )
224
0
        {
225
0
            if( pCached )
226
0
                rWallpaper.ImplReleaseCachedBitmap();
227
228
0
            aBmp = rWallpaper.GetBitmap();
229
0
            aBmp.Scale( aSize );
230
0
            aBmp = aBmp.CreateDisplayBitmap( this );
231
0
        }
232
0
        break;
233
234
0
    case WallpaperStyle::TopLeft:
235
0
        break;
236
237
0
    case WallpaperStyle::Top:
238
0
        aPos.AdjustX(( aSize.Width() - nBmpWidth ) >> 1 );
239
0
        break;
240
241
0
    case WallpaperStyle::TopRight:
242
0
        aPos.AdjustX( aSize.Width() - nBmpWidth);
243
0
        break;
244
245
0
    case WallpaperStyle::Left:
246
0
        aPos.AdjustY(( aSize.Height() - nBmpHeight ) >> 1 );
247
0
        break;
248
249
0
    case WallpaperStyle::Center:
250
0
        aPos.AdjustX(( aSize.Width() - nBmpWidth ) >> 1 );
251
0
        aPos.AdjustY(( aSize.Height() - nBmpHeight ) >> 1 );
252
0
        break;
253
254
0
    case WallpaperStyle::Right:
255
0
        aPos.AdjustX(aSize.Width() - nBmpWidth);
256
0
        aPos.AdjustY(( aSize.Height() - nBmpHeight ) >> 1 );
257
0
        break;
258
259
0
    case WallpaperStyle::BottomLeft:
260
0
        aPos.AdjustY( aSize.Height() - nBmpHeight );
261
0
        break;
262
263
0
    case WallpaperStyle::Bottom:
264
0
        aPos.AdjustX(( aSize.Width() - nBmpWidth ) >> 1 );
265
0
        aPos.AdjustY( aSize.Height() - nBmpHeight );
266
0
        break;
267
268
0
    case WallpaperStyle::BottomRight:
269
0
        aPos.AdjustX( aSize.Width() - nBmpWidth );
270
0
        aPos.AdjustY( aSize.Height() - nBmpHeight );
271
0
        break;
272
273
0
    default:
274
0
        {
275
0
            const tools::Long nRight = nX + nWidth - 1;
276
0
            const tools::Long nBottom = nY + nHeight - 1;
277
0
            tools::Long nFirstX;
278
0
            tools::Long nFirstY;
279
280
0
            if( eStyle == WallpaperStyle::Tile )
281
0
            {
282
0
                nFirstX = aPos.X();
283
0
                nFirstY = aPos.Y();
284
0
            }
285
0
            else
286
0
            {
287
0
                nFirstX = aPos.X() + ( ( aSize.Width() - nBmpWidth ) >> 1 );
288
0
                nFirstY = aPos.Y() + ( ( aSize.Height() - nBmpHeight ) >> 1 );
289
0
            }
290
0
            const tools::Long nOffX = ( nFirstX - nX ) % nBmpWidth;
291
0
            const tools::Long nOffY = ( nFirstY - nY ) % nBmpHeight;
292
0
            tools::Long nStartX = nX + nOffX;
293
0
            tools::Long nStartY = nY + nOffY;
294
295
0
            if( nOffX > 0 )
296
0
                nStartX -= nBmpWidth;
297
298
0
            if( nOffY > 0 )
299
0
                nStartY -= nBmpHeight;
300
301
302
            // if possible use accelerated path
303
0
            if( eStyle == WallpaperStyle::Tile
304
0
                && (meRasterOp == RasterOp::OverPaint)
305
0
                && (mnDrawMode == DrawModeFlags::Default)
306
0
                && nWidth > 0 && nHeight > 0 )
307
0
                bDrawn = mpGraphics->DrawBitmapWallpaper(nStartX, nStartY, nRight, nBottom, nBmpWidth, nBmpHeight, *aBmp.ImplGetSalBitmap());
308
309
0
            if (!bDrawn)
310
0
            {
311
0
                for( tools::Long nBmpY = nStartY; nBmpY <= nBottom; nBmpY += nBmpHeight )
312
0
                {
313
0
                    for( tools::Long nBmpX = nStartX; nBmpX <= nRight; nBmpX += nBmpWidth )
314
0
                    {
315
0
                        DrawBitmap(Point( nBmpX, nBmpY), aBmp);
316
0
                    }
317
0
                }
318
0
                bDrawn = true;
319
0
            }
320
0
        }
321
0
        break;
322
0
    }
323
324
0
    if( !bDrawn )
325
0
    {
326
        // optimized for non-transparent bitmaps
327
0
        if( bDrawColorBackground )
328
0
        {
329
0
            const Size aBmpSize( aBmp.GetSizePixel() );
330
0
            const Point aTmpPoint;
331
0
            const tools::Rectangle aOutRect( aTmpPoint, GetOutputSizePixel() );
332
0
            const tools::Rectangle aColRect( Point( nX, nY ), Size( nWidth, nHeight ) );
333
334
0
            tools::Rectangle aWorkRect( 0, 0, aOutRect.Right(), aPos.Y() - 1 );
335
0
            aWorkRect.Normalize();
336
0
            aWorkRect.Intersection( aColRect );
337
0
            if( !aWorkRect.IsEmpty() )
338
0
            {
339
0
                DrawColorWallpaper( aWorkRect.Left(), aWorkRect.Top(),
340
0
                                    aWorkRect.GetWidth(), aWorkRect.GetHeight(),
341
0
                                    rWallpaper );
342
0
            }
343
344
0
            aWorkRect = tools::Rectangle( 0, aPos.Y(), aPos.X() - 1, aPos.Y() + aBmpSize.Height() - 1 );
345
0
            aWorkRect.Normalize();
346
0
            aWorkRect.Intersection( aColRect );
347
0
            if( !aWorkRect.IsEmpty() )
348
0
            {
349
0
                DrawColorWallpaper( aWorkRect.Left(), aWorkRect.Top(),
350
0
                                    aWorkRect.GetWidth(), aWorkRect.GetHeight(),
351
0
                                    rWallpaper );
352
0
            }
353
354
0
            aWorkRect = tools::Rectangle( aPos.X() + aBmpSize.Width(), aPos.Y(),
355
0
                                   aOutRect.Right(), aPos.Y() + aBmpSize.Height() - 1 );
356
0
            aWorkRect.Normalize();
357
0
            aWorkRect.Intersection( aColRect );
358
0
            if( !aWorkRect.IsEmpty() )
359
0
            {
360
0
                DrawColorWallpaper( aWorkRect.Left(), aWorkRect.Top(),
361
0
                                    aWorkRect.GetWidth(), aWorkRect.GetHeight(),
362
0
                                    rWallpaper );
363
0
            }
364
365
0
            aWorkRect = tools::Rectangle( 0, aPos.Y() + aBmpSize.Height(),
366
0
                                   aOutRect.Right(), aOutRect.Bottom() );
367
0
            aWorkRect.Normalize();
368
0
            aWorkRect.Intersection( aColRect );
369
0
            if( !aWorkRect.IsEmpty() )
370
0
            {
371
0
                DrawColorWallpaper( aWorkRect.Left(), aWorkRect.Top(),
372
0
                                    aWorkRect.GetWidth(), aWorkRect.GetHeight(),
373
0
                                    rWallpaper );
374
0
            }
375
0
        }
376
377
0
        DrawBitmap( aPos, aBmp );
378
0
    }
379
380
0
    rWallpaper.ImplSetCachedBitmap( aBmp );
381
382
0
    Pop();
383
0
    EnableMapMode( bOldMap );
384
0
    mpMetaFile = pOldMetaFile;
385
0
}
386
387
void OutputDevice::DrawGradientWallpaper( tools::Long nX, tools::Long nY,
388
                                          tools::Long nWidth, tools::Long nHeight,
389
                                          const Wallpaper& rWallpaper )
390
0
{
391
0
    assert(!is_double_buffered_window());
392
393
0
    tools::Rectangle aBound;
394
0
    GDIMetaFile* pOldMetaFile = mpMetaFile;
395
0
    const bool bOldMap = mbMap;
396
397
0
    aBound = tools::Rectangle( Point( nX, nY ), Size( nWidth, nHeight ) );
398
399
0
    mpMetaFile = nullptr;
400
0
    EnableMapMode( false );
401
0
    Push( vcl::PushFlags::CLIPREGION );
402
0
    IntersectClipRegion( tools::Rectangle( Point( nX, nY ), Size( nWidth, nHeight ) ) );
403
404
0
    DrawGradient( aBound, rWallpaper.GetGradient() );
405
406
0
    Pop();
407
0
    EnableMapMode( bOldMap );
408
0
    mpMetaFile = pOldMetaFile;
409
0
}
410
411
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */