Coverage Report

Created: 2026-04-09 11:41

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