Coverage Report

Created: 2026-07-25 07:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibGfx/Painter.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
3
 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
4
 * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
5
 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
6
 * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
7
 * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
8
 * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
9
 *
10
 * SPDX-License-Identifier: BSD-2-Clause
11
 */
12
13
#include "Painter.h"
14
#include "Bitmap.h"
15
#include "Font/Emoji.h"
16
#include "Font/Font.h"
17
#include <AK/Assertions.h>
18
#include <AK/Debug.h>
19
#include <AK/Function.h>
20
#include <AK/Math/Trigonometry.h>
21
#include <AK/Memory.h>
22
#include <AK/Queue.h>
23
#include <AK/QuickSort.h>
24
#include <AK/Stack.h>
25
#include <AK/StdLibExtras.h>
26
#include <AK/StringBuilder.h>
27
#include <AK/Utf32View.h>
28
#include <AK/Utf8View.h>
29
#include <LibGfx/CharacterBitmap.h>
30
#include <LibGfx/Font/ScaledFont.h>
31
#include <LibGfx/Palette.h>
32
#include <LibGfx/Path.h>
33
#include <LibGfx/Quad.h>
34
#include <LibGfx/TextDirection.h>
35
#include <LibGfx/TextLayout.h>
36
#include <LibUnicode/CharacterTypes.h>
37
#include <LibUnicode/Emoji.h>
38
#include <stdio.h>
39
40
#if defined(AK_COMPILER_GCC)
41
#    pragma GCC optimize("O3")
42
#endif
43
44
namespace Gfx {
45
46
template<BitmapFormat format = BitmapFormat::Invalid>
47
ALWAYS_INLINE Color get_pixel(Gfx::Bitmap const& bitmap, int x, int y)
48
0
{
49
    if constexpr (format == BitmapFormat::BGRx8888)
50
0
        return Color::from_rgb(bitmap.scanline(y)[x]);
51
    if constexpr (format == BitmapFormat::BGRA8888)
52
0
        return Color::from_argb(bitmap.scanline(y)[x]);
53
0
    return bitmap.get_pixel(x, y);
54
0
}
Unexecuted instantiation: Gfx::Color Gfx::get_pixel<(Gfx::BitmapFormat)1>(Gfx::Bitmap const&, int, int)
Unexecuted instantiation: Gfx::Color Gfx::get_pixel<(Gfx::BitmapFormat)2>(Gfx::Bitmap const&, int, int)
Unexecuted instantiation: Gfx::Color Gfx::get_pixel<(Gfx::BitmapFormat)0>(Gfx::Bitmap const&, int, int)
55
56
Painter::Painter(Gfx::Bitmap& bitmap)
57
705
    : m_target(bitmap)
58
705
{
59
705
    int scale = bitmap.scale();
60
705
    VERIFY(bitmap.format() == Gfx::BitmapFormat::BGRx8888 || bitmap.format() == Gfx::BitmapFormat::BGRA8888);
61
705
    VERIFY(bitmap.physical_width() % scale == 0);
62
705
    VERIFY(bitmap.physical_height() % scale == 0);
63
705
    m_state_stack.append(State());
64
705
    state().font = nullptr;
65
705
    state().clip_rect = { { 0, 0 }, bitmap.size() };
66
705
    state().scale = scale;
67
705
    m_clip_origin = state().clip_rect;
68
705
}
69
70
void Painter::fill_rect_with_draw_op(IntRect const& a_rect, Color color)
71
0
{
72
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
73
74
0
    auto rect = a_rect.translated(translation()).intersected(clip_rect());
75
0
    if (rect.is_empty())
76
0
        return;
77
78
0
    ARGB32* dst = target().scanline(rect.top()) + rect.left();
79
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
80
81
0
    for (int i = rect.height() - 1; i >= 0; --i) {
82
0
        for (int j = 0; j < rect.width(); ++j)
83
0
            set_physical_pixel_with_draw_op(dst[j], color);
84
0
        dst += dst_skip;
85
0
    }
86
0
}
87
88
void Painter::clear_rect(IntRect const& a_rect, Color color)
89
0
{
90
0
    auto rect = a_rect.translated(translation()).intersected(clip_rect());
91
0
    if (rect.is_empty())
92
0
        return;
93
94
0
    VERIFY(target().rect().contains(rect));
95
0
    rect *= scale();
96
97
0
    ARGB32* dst = target().scanline(rect.top()) + rect.left();
98
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
99
100
0
    for (int i = rect.height() - 1; i >= 0; --i) {
101
0
        fast_u32_fill(dst, color.value(), rect.width());
102
0
        dst += dst_skip;
103
0
    }
104
0
}
105
106
void Painter::fill_physical_rect(IntRect const& physical_rect, Color color)
107
0
{
108
    // Callers must do clipping.
109
0
    ARGB32* dst = target().scanline(physical_rect.top()) + physical_rect.left();
110
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
111
112
0
    auto dst_format = target().format();
113
0
    for (int i = physical_rect.height() - 1; i >= 0; --i) {
114
0
        for (int j = 0; j < physical_rect.width(); ++j)
115
0
            dst[j] = color_for_format(dst_format, dst[j]).blend(color).value();
116
0
        dst += dst_skip;
117
0
    }
118
0
}
119
120
void Painter::fill_rect(IntRect const& a_rect, Color color)
121
0
{
122
0
    if (color.alpha() == 0)
123
0
        return;
124
125
0
    if (draw_op() != DrawOp::Copy) {
126
0
        fill_rect_with_draw_op(a_rect, color);
127
0
        return;
128
0
    }
129
130
0
    if (color.alpha() == 0xff) {
131
0
        clear_rect(a_rect, color);
132
0
        return;
133
0
    }
134
135
0
    auto rect = a_rect.translated(translation()).intersected(clip_rect());
136
0
    if (rect.is_empty())
137
0
        return;
138
0
    VERIFY(target().rect().contains(rect));
139
140
0
    fill_physical_rect(rect * scale(), color);
141
0
}
142
143
void Painter::fill_rect(IntRect const& rect, PaintStyle const& paint_style)
144
0
{
145
0
    auto a_rect = rect.translated(translation());
146
0
    auto clipped_rect = a_rect.intersected(clip_rect());
147
0
    if (clipped_rect.is_empty())
148
0
        return;
149
0
    a_rect *= scale();
150
0
    clipped_rect *= scale();
151
0
    auto start_offset = clipped_rect.location() - a_rect.location();
152
0
    paint_style.paint(a_rect, [&](PaintStyle::SamplerFunction sample) {
153
0
        for (int y = 0; y < clipped_rect.height(); ++y) {
154
0
            for (int x = 0; x < clipped_rect.width(); ++x) {
155
0
                IntPoint point(x, y);
156
0
                set_physical_pixel(point + clipped_rect.location(), sample(point + start_offset), true);
157
0
            }
158
0
        }
159
0
    });
160
0
}
161
162
void Painter::fill_rect_with_dither_pattern(IntRect const& a_rect, Color color_a, Color color_b)
163
0
{
164
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
165
166
0
    auto rect = a_rect.translated(translation()).intersected(clip_rect());
167
0
    if (rect.is_empty())
168
0
        return;
169
170
0
    ARGB32* dst = target().scanline(rect.top()) + rect.left();
171
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
172
173
0
    for (int i = 0; i < rect.height(); ++i) {
174
0
        for (int j = 0; j < rect.width(); ++j) {
175
0
            bool checkboard_use_a = ((rect.left() + i) & 1) ^ ((rect.top() + j) & 1);
176
0
            if (checkboard_use_a && !color_a.alpha())
177
0
                continue;
178
0
            if (!checkboard_use_a && !color_b.alpha())
179
0
                continue;
180
0
            dst[j] = checkboard_use_a ? color_a.value() : color_b.value();
181
0
        }
182
0
        dst += dst_skip;
183
0
    }
184
0
}
185
186
void Painter::fill_rect_with_checkerboard(IntRect const& a_rect, IntSize cell_size, Color color_dark, Color color_light)
187
0
{
188
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
189
190
0
    auto translated_rect = a_rect.translated(translation());
191
0
    auto rect = translated_rect.intersected(clip_rect());
192
0
    if (rect.is_empty())
193
0
        return;
194
195
0
    ARGB32* dst = target().scanline(rect.top()) + rect.left();
196
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
197
198
0
    int first_cell_column = (rect.x() - translated_rect.x()) / cell_size.width();
199
0
    int prologue_length = min(rect.width(), cell_size.width() - ((rect.x() - translated_rect.x()) % cell_size.width()));
200
0
    int number_of_aligned_strips = (rect.width() - prologue_length) / cell_size.width();
201
202
0
    for (int i = 0; i < rect.height(); ++i) {
203
0
        int y = rect.y() - translated_rect.y() + i;
204
0
        int cell_row = y / cell_size.height();
205
0
        bool odd_row = cell_row & 1;
206
207
        // Prologue: Paint the unaligned part up to the first intersection.
208
0
        int j = 0;
209
0
        int cell_column = first_cell_column;
210
211
0
        {
212
0
            bool odd_cell = cell_column & 1;
213
0
            auto color = (odd_row ^ odd_cell) ? color_light.value() : color_dark.value();
214
0
            fast_u32_fill(&dst[j], color, prologue_length);
215
0
            j += prologue_length;
216
0
        }
217
218
        // Aligned run: Paint the maximum number of aligned cell strips.
219
0
        for (int strip = 0; strip < number_of_aligned_strips; ++strip) {
220
0
            ++cell_column;
221
0
            bool odd_cell = cell_column & 1;
222
0
            auto color = (odd_row ^ odd_cell) ? color_light.value() : color_dark.value();
223
0
            fast_u32_fill(&dst[j], color, cell_size.width());
224
0
            j += cell_size.width();
225
0
        }
226
227
        // Epilogue: Paint the unaligned part until the end of the rect.
228
0
        if (j != rect.width()) {
229
0
            ++cell_column;
230
0
            bool odd_cell = cell_column & 1;
231
0
            auto color = (odd_row ^ odd_cell) ? color_light.value() : color_dark.value();
232
0
            int epilogue_length = rect.width() - j;
233
0
            fast_u32_fill(&dst[j], color, epilogue_length);
234
0
            j += epilogue_length;
235
0
        }
236
237
0
        dst += dst_skip;
238
0
    }
239
0
}
240
241
void Painter::fill_rect_with_gradient(Orientation orientation, IntRect const& a_rect, Color gradient_start, Color gradient_end)
242
0
{
243
0
    if (gradient_start == gradient_end) {
244
0
        fill_rect(a_rect, gradient_start);
245
0
        return;
246
0
    }
247
0
    return fill_rect_with_linear_gradient(a_rect, Array { ColorStop { gradient_start, 0 }, ColorStop { gradient_end, 1 } }, orientation == Orientation::Horizontal ? 90.0f : 0.0f);
248
0
}
249
250
void Painter::fill_rect_with_gradient(IntRect const& a_rect, Color gradient_start, Color gradient_end)
251
0
{
252
0
    return fill_rect_with_gradient(Orientation::Horizontal, a_rect, gradient_start, gradient_end);
253
0
}
254
255
void Painter::fill_rect_with_rounded_corners(IntRect const& a_rect, Color color, int radius)
256
0
{
257
0
    return fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
258
0
}
259
260
void Painter::fill_rect_with_rounded_corners(IntRect const& a_rect, Color color, int top_left_radius, int top_right_radius, int bottom_right_radius, int bottom_left_radius)
261
0
{
262
    // Fasttrack for rects without any border radii
263
0
    if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius)
264
0
        return fill_rect(a_rect, color);
265
266
    // Fully transparent, dont care.
267
0
    if (color.alpha() == 0)
268
0
        return;
269
270
    // FIXME: Allow for elliptically rounded corners
271
0
    IntRect top_left_corner = {
272
0
        a_rect.x(),
273
0
        a_rect.y(),
274
0
        top_left_radius,
275
0
        top_left_radius
276
0
    };
277
0
    IntRect top_right_corner = {
278
0
        a_rect.x() + a_rect.width() - top_right_radius,
279
0
        a_rect.y(),
280
0
        top_right_radius,
281
0
        top_right_radius
282
0
    };
283
0
    IntRect bottom_right_corner = {
284
0
        a_rect.x() + a_rect.width() - bottom_right_radius,
285
0
        a_rect.y() + a_rect.height() - bottom_right_radius,
286
0
        bottom_right_radius,
287
0
        bottom_right_radius
288
0
    };
289
0
    IntRect bottom_left_corner = {
290
0
        a_rect.x(),
291
0
        a_rect.y() + a_rect.height() - bottom_left_radius,
292
0
        bottom_left_radius,
293
0
        bottom_left_radius
294
0
    };
295
296
0
    IntRect top_rect = {
297
0
        a_rect.x() + top_left_radius,
298
0
        a_rect.y(),
299
0
        a_rect.width() - top_left_radius - top_right_radius, top_left_radius
300
0
    };
301
0
    IntRect right_rect = {
302
0
        a_rect.x() + a_rect.width() - top_right_radius,
303
0
        a_rect.y() + top_right_radius,
304
0
        top_right_radius,
305
0
        a_rect.height() - top_right_radius - bottom_right_radius
306
0
    };
307
0
    IntRect bottom_rect = {
308
0
        a_rect.x() + bottom_left_radius,
309
0
        a_rect.y() + a_rect.height() - bottom_right_radius,
310
0
        a_rect.width() - bottom_left_radius - bottom_right_radius,
311
0
        bottom_right_radius
312
0
    };
313
0
    IntRect left_rect = {
314
0
        a_rect.x(),
315
0
        a_rect.y() + top_left_radius,
316
0
        bottom_left_radius,
317
0
        a_rect.height() - top_left_radius - bottom_left_radius
318
0
    };
319
320
0
    IntRect inner = {
321
0
        left_rect.x() + left_rect.width(),
322
0
        left_rect.y(),
323
0
        a_rect.width() - left_rect.width() - right_rect.width(),
324
0
        a_rect.height() - top_rect.height() - bottom_rect.height()
325
0
    };
326
327
0
    fill_rect(top_rect, color);
328
0
    fill_rect(right_rect, color);
329
0
    fill_rect(bottom_rect, color);
330
0
    fill_rect(left_rect, color);
331
332
0
    fill_rect(inner, color);
333
334
0
    if (top_left_radius)
335
0
        fill_rounded_corner(top_left_corner, top_left_radius, color, CornerOrientation::TopLeft);
336
0
    if (top_right_radius)
337
0
        fill_rounded_corner(top_right_corner, top_right_radius, color, CornerOrientation::TopRight);
338
0
    if (bottom_left_radius)
339
0
        fill_rounded_corner(bottom_left_corner, bottom_left_radius, color, CornerOrientation::BottomLeft);
340
0
    if (bottom_right_radius)
341
0
        fill_rounded_corner(bottom_right_corner, bottom_right_radius, color, CornerOrientation::BottomRight);
342
0
}
343
344
void Painter::fill_rounded_corner(IntRect const& a_rect, int radius, Color color, CornerOrientation orientation)
345
0
{
346
    // Care about clipping
347
0
    auto translated_a_rect = a_rect.translated(translation());
348
0
    auto rect = translated_a_rect.intersected(clip_rect());
349
350
0
    if (rect.is_empty())
351
0
        return;
352
0
    VERIFY(target().rect().contains(rect));
353
354
    // We got cut on the top!
355
    // FIXME: Also account for clipping on the x-axis
356
0
    int clip_offset = 0;
357
0
    if (translated_a_rect.y() < rect.y())
358
0
        clip_offset = rect.y() - translated_a_rect.y();
359
360
0
    radius *= scale();
361
0
    rect *= scale();
362
0
    clip_offset *= scale();
363
364
0
    ARGB32* dst = target().scanline(rect.top()) + rect.left();
365
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
366
367
0
    IntPoint circle_center;
368
0
    switch (orientation) {
369
0
    case CornerOrientation::TopLeft:
370
0
        circle_center = { radius, radius + 1 };
371
0
        break;
372
0
    case CornerOrientation::TopRight:
373
0
        circle_center = { -1, radius + 1 };
374
0
        break;
375
0
    case CornerOrientation::BottomRight:
376
0
        circle_center = { -1, 0 };
377
0
        break;
378
0
    case CornerOrientation::BottomLeft:
379
0
        circle_center = { radius, 0 };
380
0
        break;
381
0
    default:
382
0
        VERIFY_NOT_REACHED();
383
0
    }
384
385
0
    int radius2 = radius * radius;
386
0
    auto is_in_circle = [&](int x, int y) {
387
0
        int distance2 = (circle_center.x() - x) * (circle_center.x() - x) + (circle_center.y() - y) * (circle_center.y() - y);
388
        // To reflect the grid and be compatible with the draw_circle_arc_intersecting algorithm
389
        // add 1/2 to the radius
390
0
        return distance2 <= (radius2 + radius + 0.25);
391
0
    };
392
393
0
    auto dst_format = target().format();
394
0
    for (int i = rect.height() - 1; i >= 0; --i) {
395
0
        for (int j = 0; j < rect.width(); ++j)
396
0
            if (is_in_circle(j, rect.height() - i + clip_offset))
397
0
                dst[j] = color_for_format(dst_format, dst[j]).blend(color).value();
398
0
        dst += dst_skip;
399
0
    }
400
0
}
401
402
void Painter::draw_circle_arc_intersecting(IntRect const& a_rect, IntPoint center, int radius, Color color, int thickness)
403
0
{
404
0
    if (thickness <= 0 || radius <= 0)
405
0
        return;
406
407
    // Care about clipping
408
0
    auto translated_a_rect = a_rect.translated(translation());
409
0
    auto rect = translated_a_rect.intersected(clip_rect());
410
411
0
    if (rect.is_empty())
412
0
        return;
413
0
    VERIFY(target().rect().contains(rect));
414
415
    // We got cut on the top!
416
    // FIXME: Also account for clipping on the x-axis
417
0
    int clip_offset = 0;
418
0
    if (translated_a_rect.y() < rect.y())
419
0
        clip_offset = rect.y() - translated_a_rect.y();
420
421
0
    if (thickness > radius)
422
0
        thickness = radius;
423
424
0
    int radius2 = radius * radius;
425
0
    auto is_on_arc = [&](int x, int y) {
426
0
        int distance2 = (center.x() - x) * (center.x() - x) + (center.y() - y) * (center.y() - y);
427
        // Is within a circle of radius 1/2 around (x,y), so basically within the current pixel.
428
        // Technically this is angle-dependent and should be between 1/2 and sqrt(2)/2, but this works.
429
0
        return distance2 <= (radius2 + radius + 0.25) && distance2 >= (radius2 - radius + 0.25);
430
0
    };
431
432
0
    ARGB32* dst = target().scanline(rect.top()) + rect.left();
433
0
    auto dst_format = target().format();
434
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
435
436
0
    for (int i = rect.height() - 1; i >= 0; --i) {
437
0
        for (int j = 0; j < rect.width(); ++j)
438
0
            if (is_on_arc(j, rect.height() - i + clip_offset))
439
0
                dst[j] = color_for_format(dst_format, dst[j]).blend(color).value();
440
0
        dst += dst_skip;
441
0
    }
442
443
0
    return draw_circle_arc_intersecting(a_rect, center, radius - 1, color, thickness - 1);
444
0
}
445
446
// The callback will only be called for a quarter of the ellipse, the user is intended to deduce other points.
447
// As the coordinate space is relative to the center of the rectangle, it's simply (x, y), (x, -y), (-x, y) and (-x, -y).
448
static void on_each_ellipse_point(IntRect const& rect, Function<void(IntPoint)>&& callback)
449
0
{
450
    // Note: This is an implementation of the Midpoint Ellipse Algorithm.
451
0
    double const a = rect.width() / 2;
452
0
    double const a_square = a * a;
453
0
    double const b = rect.height() / 2;
454
0
    double const b_square = b * b;
455
456
0
    int x = 0;
457
0
    auto y = static_cast<int>(b);
458
459
0
    double dx = 2 * b_square * x;
460
0
    double dy = 2 * a_square * y;
461
462
    // For region 1:
463
0
    auto decision_parameter = b_square - a_square * b + .25 * a_square;
464
465
0
    while (dx < dy) {
466
0
        callback({ x, y });
467
468
0
        if (decision_parameter >= 0) {
469
0
            y--;
470
0
            dy -= 2 * a_square;
471
0
            decision_parameter -= dy;
472
0
        }
473
0
        x++;
474
0
        dx += 2 * b_square;
475
0
        decision_parameter += dx + b_square;
476
0
    }
477
478
    // For region 2:
479
0
    decision_parameter = b_square * ((x + 0.5) * (x + 0.5)) + a_square * ((y - 1) * (y - 1)) - a_square * b_square;
480
481
0
    while (y >= 0) {
482
0
        callback({ x, y });
483
484
0
        if (decision_parameter <= 0) {
485
0
            x++;
486
0
            dx += 2 * b_square;
487
0
            decision_parameter += dx;
488
0
        }
489
0
        y--;
490
0
        dy -= 2 * a_square;
491
0
        decision_parameter += a_square - dy;
492
0
    }
493
0
}
494
495
void Painter::fill_ellipse(IntRect const& a_rect, Color color)
496
0
{
497
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
498
499
0
    auto rect = a_rect.translated(translation()).intersected(clip_rect());
500
0
    if (rect.is_empty())
501
0
        return;
502
503
0
    VERIFY(target().rect().contains(rect));
504
505
0
    auto const center = a_rect.center();
506
507
0
    on_each_ellipse_point(rect, [this, &color, center](IntPoint position) {
508
0
        IntPoint const directions[4] = { { position.x(), position.y() }, { -position.x(), position.y() }, { position.x(), -position.y() }, { -position.x(), -position.y() } };
509
510
0
        draw_line(center + directions[0], center + directions[1], color);
511
0
        draw_line(center + directions[2], center + directions[3], color);
512
0
    });
513
0
}
514
515
void Painter::draw_ellipse_intersecting(IntRect const& rect, Color color, int thickness)
516
0
{
517
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
518
519
0
    if (thickness <= 0)
520
0
        return;
521
522
0
    auto const center = rect.center();
523
524
0
    on_each_ellipse_point(rect, [this, &color, thickness, center](IntPoint position) {
525
0
        IntPoint const directions[4] = { { position.x(), position.y() }, { position.x(), -position.y() }, { -position.x(), position.y() }, { -position.x(), -position.y() } };
526
0
        for (auto const delta : directions) {
527
0
            auto const point = center + delta;
528
0
            draw_line(point, point, color, thickness);
529
0
        }
530
0
    });
531
0
}
532
533
template<typename RectType, typename Callback>
534
static void for_each_pixel_around_rect_clockwise(RectType const& rect, Callback callback)
535
0
{
536
0
    if (rect.is_empty())
537
0
        return;
538
0
    for (auto x = rect.left(); x < rect.right(); ++x)
539
0
        callback(x, rect.top());
540
0
    for (auto y = rect.top() + 1; y < rect.bottom(); ++y)
541
0
        callback(rect.right() - 1, y);
542
0
    for (auto x = rect.right() - 2; x >= rect.left(); --x)
543
0
        callback(x, rect.bottom() - 1);
544
0
    for (auto y = rect.bottom() - 2; y > rect.top(); --y)
545
0
        callback(rect.left(), y);
546
0
}
547
548
void Painter::draw_focus_rect(IntRect const& rect, Color color)
549
0
{
550
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
551
552
0
    if (rect.is_empty())
553
0
        return;
554
0
    bool state = false;
555
0
    for_each_pixel_around_rect_clockwise(rect, [&](auto x, auto y) {
556
0
        if (state)
557
0
            set_pixel(x, y, color);
558
0
        state = !state;
559
0
    });
560
0
}
561
562
void Painter::draw_rect(IntRect const& a_rect, Color color, bool rough)
563
0
{
564
0
    IntRect rect = a_rect.translated(translation());
565
0
    auto clipped_rect = rect.intersected(clip_rect());
566
0
    if (clipped_rect.is_empty())
567
0
        return;
568
569
0
    int min_y = clipped_rect.top();
570
0
    int max_y = clipped_rect.bottom() - 1;
571
0
    int scale = this->scale();
572
573
0
    if (rect.top() >= clipped_rect.top() && rect.top() < clipped_rect.bottom()) {
574
0
        int width = rough ? max(0, min(rect.width() - 2, clipped_rect.width())) : clipped_rect.width();
575
0
        if (width > 0) {
576
0
            int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
577
0
            for (int i = 0; i < scale; ++i)
578
0
                fill_physical_scanline_with_draw_op(rect.top() * scale + i, start_x * scale, width * scale, color);
579
0
        }
580
0
        ++min_y;
581
0
    }
582
0
    if (rect.bottom() > clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) {
583
0
        int width = rough ? max(0, min(rect.width() - 2, clipped_rect.width())) : clipped_rect.width();
584
0
        if (width > 0) {
585
0
            int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
586
0
            for (int i = 0; i < scale; ++i)
587
0
                fill_physical_scanline_with_draw_op(max_y * scale + i, start_x * scale, width * scale, color);
588
0
        }
589
0
        --max_y;
590
0
    }
591
592
0
    bool draw_left_side = rect.left() >= clipped_rect.left();
593
0
    bool draw_right_side = rect.right() == clipped_rect.right();
594
595
0
    if (draw_left_side && draw_right_side) {
596
        // Specialized loop when drawing both sides.
597
0
        for (int y = min_y * scale; y <= max_y * scale; ++y) {
598
0
            auto* bits = target().scanline(y);
599
0
            for (int i = 0; i < scale; ++i)
600
0
                set_physical_pixel_with_draw_op(bits[rect.left() * scale + i], color);
601
0
            for (int i = 0; i < scale; ++i)
602
0
                set_physical_pixel_with_draw_op(bits[(rect.right() - 1) * scale + i], color);
603
0
        }
604
0
    } else {
605
0
        for (int y = min_y * scale; y <= max_y * scale; ++y) {
606
0
            auto* bits = target().scanline(y);
607
0
            if (draw_left_side)
608
0
                for (int i = 0; i < scale; ++i)
609
0
                    set_physical_pixel_with_draw_op(bits[rect.left() * scale + i], color);
610
0
            if (draw_right_side)
611
0
                for (int i = 0; i < scale; ++i)
612
0
                    set_physical_pixel_with_draw_op(bits[(rect.right() - 1) * scale + i], color);
613
0
        }
614
0
    }
615
0
}
616
617
void Painter::draw_rect_with_thickness(IntRect const& rect, Color color, int thickness)
618
0
{
619
0
    if (thickness <= 0)
620
0
        return;
621
622
0
    IntPoint p1 = rect.location();
623
0
    IntPoint p2 = { rect.location().x() + rect.width(), rect.location().y() };
624
0
    IntPoint p3 = { rect.location().x() + rect.width(), rect.location().y() + rect.height() };
625
0
    IntPoint p4 = { rect.location().x(), rect.location().y() + rect.height() };
626
627
0
    draw_line(p1.translated(thickness, 0), p2.translated(-thickness, 0), color, thickness);
628
0
    draw_line(p2, p3, color, thickness);
629
0
    draw_line(p4.translated(thickness, 0), p3.translated(-thickness, 0), color, thickness);
630
0
    draw_line(p4, p1, color, thickness);
631
0
}
632
633
void Painter::draw_bitmap(IntPoint p, CharacterBitmap const& bitmap, Color color)
634
0
{
635
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
636
637
0
    auto rect = IntRect(p, bitmap.size()).translated(translation());
638
0
    auto clipped_rect = rect.intersected(clip_rect());
639
0
    if (clipped_rect.is_empty())
640
0
        return;
641
0
    int const first_row = clipped_rect.top() - rect.top();
642
0
    int const last_row = clipped_rect.bottom() - rect.top();
643
0
    int const first_column = clipped_rect.left() - rect.left();
644
0
    int const last_column = clipped_rect.right() - rect.left();
645
0
    ARGB32* dst = target().scanline(clipped_rect.y()) + clipped_rect.x();
646
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
647
0
    char const* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column];
648
0
    size_t const bitmap_skip = bitmap.width();
649
650
0
    for (int row = first_row; row < last_row; ++row) {
651
0
        for (int j = 0; j < (last_column - first_column); ++j) {
652
0
            char fc = bitmap_row[j];
653
0
            if (fc == '#')
654
0
                dst[j] = color.value();
655
0
        }
656
0
        bitmap_row += bitmap_skip;
657
0
        dst += dst_skip;
658
0
    }
659
0
}
660
661
void Painter::draw_bitmap(IntPoint p, GlyphBitmap const& bitmap, Color color)
662
0
{
663
0
    auto dst_rect = IntRect(p, bitmap.size()).translated(translation());
664
0
    auto clipped_rect = dst_rect.intersected(clip_rect());
665
0
    if (clipped_rect.is_empty())
666
0
        return;
667
0
    int const first_row = clipped_rect.top() - dst_rect.top();
668
0
    int const last_row = clipped_rect.bottom() - dst_rect.top();
669
0
    int const first_column = clipped_rect.left() - dst_rect.left();
670
0
    int const last_column = clipped_rect.right() - dst_rect.left();
671
672
0
    int scale = this->scale();
673
0
    ARGB32* dst = target().scanline(clipped_rect.y() * scale) + clipped_rect.x() * scale;
674
0
    auto dst_format = target().format();
675
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
676
677
0
    if (scale == 1) {
678
0
        for (int row = first_row; row < last_row; ++row) {
679
0
            for (int j = 0; j < (last_column - first_column); ++j) {
680
0
                if (bitmap.bit_at(j + first_column, row))
681
0
                    dst[j] = color_for_format(dst_format, dst[j]).blend(color).value();
682
0
            }
683
0
            dst += dst_skip;
684
0
        }
685
0
    } else {
686
0
        for (int row = first_row; row < last_row; ++row) {
687
0
            for (int j = 0; j < (last_column - first_column); ++j) {
688
0
                if (bitmap.bit_at((j + first_column), row)) {
689
0
                    for (int iy = 0; iy < scale; ++iy)
690
0
                        for (int ix = 0; ix < scale; ++ix) {
691
0
                            auto pixel_index = j * scale + ix + iy * dst_skip;
692
0
                            dst[pixel_index] = color_for_format(dst_format, dst[pixel_index]).blend(color).value();
693
0
                        }
694
0
                }
695
0
            }
696
0
            dst += dst_skip * scale;
697
0
        }
698
0
    }
699
0
}
700
701
void Painter::draw_triangle(IntPoint offset, ReadonlySpan<IntPoint> control_points, Color color)
702
0
{
703
0
    VERIFY(control_points.size() == 3);
704
0
    draw_triangle(control_points[0] + offset, control_points[1] + offset, control_points[2] + offset, color);
705
0
}
706
707
void Painter::draw_triangle(IntPoint a, IntPoint b, IntPoint c, Color color)
708
0
{
709
0
    IntPoint p0(to_physical(a));
710
0
    IntPoint p1(to_physical(b));
711
0
    IntPoint p2(to_physical(c));
712
713
    // sort points from top to bottom
714
0
    if (p0.y() > p1.y())
715
0
        swap(p0, p1);
716
0
    if (p0.y() > p2.y())
717
0
        swap(p0, p2);
718
0
    if (p1.y() > p2.y())
719
0
        swap(p1, p2);
720
721
    // return if top and bottom points are on same line
722
0
    if (p0.y() == p2.y())
723
0
        return;
724
725
    // return if all points are on the same line vertically
726
0
    if (p0.x() == p1.x() && p1.x() == p2.x())
727
0
        return;
728
729
    // return if top is below clip rect or bottom is above clip rect
730
0
    auto clip = clip_rect();
731
0
    if (p0.y() >= clip.bottom() - 1)
732
0
        return;
733
0
    if (p2.y() < clip.top())
734
0
        return;
735
736
0
    class BoundaryLine {
737
0
    private:
738
0
        IntPoint m_base {};
739
0
        IntPoint m_path {};
740
741
0
    public:
742
0
        BoundaryLine(IntPoint a, IntPoint b)
743
0
        {
744
0
            VERIFY(a.y() <= b.y());
745
0
            m_base = a;
746
0
            m_path = b - a;
747
0
        }
748
749
0
        int top_y() const { return m_base.y(); }
750
751
0
        int bottom_y() const { return m_base.y() + m_path.y(); }
752
753
0
        bool is_vertical() const { return m_path.x() == 0; }
754
755
0
        bool is_horizontal() const { return m_path.y() == 0; }
756
757
0
        bool in_y_range(int y) const { return y >= top_y() && y <= bottom_y(); }
758
759
0
        Optional<int> intersection_on_x(int y) const
760
0
        {
761
0
            if (!in_y_range(y))
762
0
                return {};
763
0
            if (is_horizontal())
764
0
                return {};
765
0
            if (is_vertical())
766
0
                return m_base.x();
767
768
0
            int y_diff = y - top_y();
769
0
            int x_d = m_path.x() * y_diff, y_d = m_path.y();
770
771
0
            return (x_d / y_d) + m_base.x();
772
0
        }
773
0
    };
774
775
0
    BoundaryLine l0(p0, p1), l1(p0, p2), l2(p1, p2);
776
777
0
    int rgba = color.value();
778
779
0
    for (int y = max(p0.y(), clip.top()); y < min(p2.y() + 1, clip.bottom()); y++) {
780
0
        Optional<int>
781
0
            x0 = l0.intersection_on_x(y),
782
0
            x1 = l1.intersection_on_x(y),
783
0
            x2 = l2.intersection_on_x(y);
784
785
0
        int result_a = 0, result_b = 0;
786
787
0
        if (x0.has_value()) {
788
0
            result_a = x0.value();
789
0
            if (x1.has_value() && ((!x2.has_value()) || (result_a != x1.value()))) {
790
0
                result_b = x1.value();
791
0
            } else {
792
0
                result_b = x2.value();
793
0
            }
794
0
        } else if (x1.has_value()) {
795
0
            result_a = x1.value();
796
0
            result_b = x2.value();
797
0
        }
798
799
0
        if (result_a > result_b)
800
0
            swap(result_a, result_b);
801
802
0
        int left_bound = result_a, right_bound = result_b;
803
804
0
        ARGB32* scanline = target().scanline(y);
805
0
        for (int x = max(left_bound, clip.left()); x <= min(right_bound, clip.right() - 1); x++)
806
0
            scanline[x] = rgba;
807
0
    }
808
0
}
809
810
struct BlitState {
811
    enum AlphaState {
812
        NoAlpha = 0,
813
        SrcAlpha = 1,
814
        DstAlpha = 2,
815
        BothAlpha = SrcAlpha | DstAlpha
816
    };
817
818
    ARGB32 const* src;
819
    ARGB32* dst;
820
    size_t src_pitch;
821
    size_t dst_pitch;
822
    int row_count;
823
    int column_count;
824
    float opacity;
825
    BitmapFormat src_format;
826
};
827
828
// FIXME: This is a hack to support blit_with_opacity() with RGBA8888 source.
829
//        Ideally we'd have a more generic solution that allows any source format.
830
static void swap_red_and_blue_channels(Color& color)
831
0
{
832
0
    u32 rgba = color.value();
833
0
    u32 bgra = (rgba & 0xff00ff00)
834
0
        | ((rgba & 0x000000ff) << 16)
835
0
        | ((rgba & 0x00ff0000) >> 16);
836
0
    color = Color::from_argb(bgra);
837
0
}
838
839
// FIXME: This function is very unoptimized.
840
template<BlitState::AlphaState has_alpha>
841
static void do_blit_with_opacity(BlitState& state)
842
49
{
843
4.90k
    for (int row = 0; row < state.row_count; ++row) {
844
33.6k
        for (int x = 0; x < state.column_count; ++x) {
845
28.7k
            Color dest_color = (has_alpha & BlitState::DstAlpha) ? Color::from_argb(state.dst[x]) : Color::from_rgb(state.dst[x]);
846
28.7k
            if constexpr (has_alpha & BlitState::SrcAlpha) {
847
28.7k
                Color src_color_with_alpha = Color::from_argb(state.src[x]);
848
28.7k
                if (state.src_format == BitmapFormat::RGBA8888)
849
0
                    swap_red_and_blue_channels(src_color_with_alpha);
850
28.7k
                float pixel_opacity = src_color_with_alpha.alpha() / 255.0;
851
28.7k
                src_color_with_alpha.set_alpha(255 * (state.opacity * pixel_opacity));
852
28.7k
                state.dst[x] = dest_color.blend(src_color_with_alpha).value();
853
28.7k
            } else {
854
0
                Color src_color_with_alpha = Color::from_rgb(state.src[x]);
855
0
                if (state.src_format == BitmapFormat::RGBA8888)
856
0
                    swap_red_and_blue_channels(src_color_with_alpha);
857
0
                src_color_with_alpha.set_alpha(state.opacity * 255);
858
0
                state.dst[x] = dest_color.blend(src_color_with_alpha).value();
859
0
            }
860
28.7k
        }
861
4.85k
        state.dst += state.dst_pitch;
862
4.85k
        state.src += state.src_pitch;
863
4.85k
    }
864
49
}
Painter.cpp:void Gfx::do_blit_with_opacity<(Gfx::BlitState::AlphaState)3>(Gfx::BlitState&)
Line
Count
Source
842
17
{
843
4.38k
    for (int row = 0; row < state.row_count; ++row) {
844
23.1k
        for (int x = 0; x < state.column_count; ++x) {
845
18.8k
            Color dest_color = (has_alpha & BlitState::DstAlpha) ? Color::from_argb(state.dst[x]) : Color::from_rgb(state.dst[x]);
846
18.8k
            if constexpr (has_alpha & BlitState::SrcAlpha) {
847
18.8k
                Color src_color_with_alpha = Color::from_argb(state.src[x]);
848
18.8k
                if (state.src_format == BitmapFormat::RGBA8888)
849
0
                    swap_red_and_blue_channels(src_color_with_alpha);
850
18.8k
                float pixel_opacity = src_color_with_alpha.alpha() / 255.0;
851
18.8k
                src_color_with_alpha.set_alpha(255 * (state.opacity * pixel_opacity));
852
18.8k
                state.dst[x] = dest_color.blend(src_color_with_alpha).value();
853
            } else {
854
                Color src_color_with_alpha = Color::from_rgb(state.src[x]);
855
                if (state.src_format == BitmapFormat::RGBA8888)
856
                    swap_red_and_blue_channels(src_color_with_alpha);
857
                src_color_with_alpha.set_alpha(state.opacity * 255);
858
                state.dst[x] = dest_color.blend(src_color_with_alpha).value();
859
            }
860
18.8k
        }
861
4.36k
        state.dst += state.dst_pitch;
862
4.36k
        state.src += state.src_pitch;
863
4.36k
    }
864
17
}
Painter.cpp:void Gfx::do_blit_with_opacity<(Gfx::BlitState::AlphaState)1>(Gfx::BlitState&)
Line
Count
Source
842
32
{
843
525
    for (int row = 0; row < state.row_count; ++row) {
844
10.4k
        for (int x = 0; x < state.column_count; ++x) {
845
9.94k
            Color dest_color = (has_alpha & BlitState::DstAlpha) ? Color::from_argb(state.dst[x]) : Color::from_rgb(state.dst[x]);
846
9.94k
            if constexpr (has_alpha & BlitState::SrcAlpha) {
847
9.94k
                Color src_color_with_alpha = Color::from_argb(state.src[x]);
848
9.94k
                if (state.src_format == BitmapFormat::RGBA8888)
849
0
                    swap_red_and_blue_channels(src_color_with_alpha);
850
9.94k
                float pixel_opacity = src_color_with_alpha.alpha() / 255.0;
851
9.94k
                src_color_with_alpha.set_alpha(255 * (state.opacity * pixel_opacity));
852
9.94k
                state.dst[x] = dest_color.blend(src_color_with_alpha).value();
853
            } else {
854
                Color src_color_with_alpha = Color::from_rgb(state.src[x]);
855
                if (state.src_format == BitmapFormat::RGBA8888)
856
                    swap_red_and_blue_channels(src_color_with_alpha);
857
                src_color_with_alpha.set_alpha(state.opacity * 255);
858
                state.dst[x] = dest_color.blend(src_color_with_alpha).value();
859
            }
860
9.94k
        }
861
493
        state.dst += state.dst_pitch;
862
493
        state.src += state.src_pitch;
863
493
    }
864
32
}
Unexecuted instantiation: Painter.cpp:void Gfx::do_blit_with_opacity<(Gfx::BlitState::AlphaState)2>(Gfx::BlitState&)
Unexecuted instantiation: Painter.cpp:void Gfx::do_blit_with_opacity<(Gfx::BlitState::AlphaState)0>(Gfx::BlitState&)
865
866
void Painter::blit_with_opacity(IntPoint position, Gfx::Bitmap const& source, IntRect const& a_src_rect, float opacity, bool apply_alpha)
867
49
{
868
49
    VERIFY(scale() >= source.scale() && "painter doesn't support downsampling scale factors");
869
870
49
    if (opacity >= 1.0f && !(source.has_alpha_channel() && apply_alpha))
871
0
        return blit(position, source, a_src_rect);
872
873
49
    IntRect safe_src_rect = IntRect::intersection(a_src_rect, source.rect());
874
49
    if (scale() != source.scale())
875
0
        return draw_scaled_bitmap({ position, safe_src_rect.size() }, source, safe_src_rect, opacity);
876
877
49
    auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
878
49
    auto clipped_rect = dst_rect.intersected(clip_rect());
879
49
    if (clipped_rect.is_empty())
880
0
        return;
881
882
49
    int scale = this->scale();
883
49
    auto src_rect = a_src_rect * scale;
884
49
    clipped_rect *= scale;
885
49
    dst_rect *= scale;
886
887
49
    int const first_row = clipped_rect.top() - dst_rect.top();
888
49
    int const last_row = clipped_rect.bottom() - dst_rect.top();
889
49
    int const first_column = clipped_rect.left() - dst_rect.left();
890
49
    int const last_column = clipped_rect.right() - dst_rect.left();
891
892
49
    BlitState blit_state {
893
49
        .src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column,
894
49
        .dst = target().scanline(clipped_rect.y()) + clipped_rect.x(),
895
49
        .src_pitch = source.pitch() / sizeof(ARGB32),
896
49
        .dst_pitch = target().pitch() / sizeof(ARGB32),
897
49
        .row_count = last_row - first_row,
898
49
        .column_count = last_column - first_column,
899
49
        .opacity = opacity,
900
49
        .src_format = source.format(),
901
49
    };
902
903
49
    if (source.has_alpha_channel() && apply_alpha) {
904
49
        if (target().has_alpha_channel())
905
17
            do_blit_with_opacity<BlitState::BothAlpha>(blit_state);
906
32
        else
907
32
            do_blit_with_opacity<BlitState::SrcAlpha>(blit_state);
908
49
    } else {
909
0
        if (target().has_alpha_channel())
910
0
            do_blit_with_opacity<BlitState::DstAlpha>(blit_state);
911
0
        else
912
0
            do_blit_with_opacity<BlitState::NoAlpha>(blit_state);
913
0
    }
914
49
}
915
916
void Painter::blit_filtered(IntPoint position, Gfx::Bitmap const& source, IntRect const& src_rect, Function<Color(Color)> const& filter, bool apply_alpha)
917
0
{
918
0
    VERIFY((source.scale() == 1 || source.scale() == scale()) && "blit_filtered only supports integer upsampling");
919
920
0
    IntRect safe_src_rect = src_rect.intersected(source.rect());
921
0
    auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
922
0
    auto clipped_rect = dst_rect.intersected(clip_rect());
923
0
    if (clipped_rect.is_empty())
924
0
        return;
925
926
0
    int scale = this->scale();
927
0
    clipped_rect *= scale;
928
0
    dst_rect *= scale;
929
0
    safe_src_rect *= source.scale();
930
931
0
    int const first_row = clipped_rect.top() - dst_rect.top();
932
0
    int const last_row = clipped_rect.bottom() - dst_rect.top();
933
0
    int const first_column = clipped_rect.left() - dst_rect.left();
934
0
    int const last_column = clipped_rect.right() - dst_rect.left();
935
0
    ARGB32* dst = target().scanline(clipped_rect.y()) + clipped_rect.x();
936
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
937
0
    auto dst_format = target().format();
938
0
    auto src_format = source.format();
939
940
0
    int s = scale / source.scale();
941
0
    if (s == 1) {
942
0
        ARGB32 const* src = source.scanline(safe_src_rect.top() + first_row) + safe_src_rect.left() + first_column;
943
0
        size_t const src_skip = source.pitch() / sizeof(ARGB32);
944
945
0
        for (int row = first_row; row < last_row; ++row) {
946
0
            for (int x = 0; x < (last_column - first_column); ++x) {
947
0
                auto source_color = color_for_format(src_format, src[x]);
948
0
                if (source_color.alpha() == 0)
949
0
                    continue;
950
0
                auto filtered_color = filter(source_color);
951
0
                if (!apply_alpha || filtered_color.alpha() == 0xff)
952
0
                    dst[x] = filtered_color.value();
953
0
                else
954
0
                    dst[x] = color_for_format(dst_format, dst[x]).blend(filtered_color).value();
955
0
            }
956
0
            dst += dst_skip;
957
0
            src += src_skip;
958
0
        }
959
0
    } else {
960
0
        for (int row = first_row; row < last_row; ++row) {
961
0
            ARGB32 const* src = source.scanline(safe_src_rect.top() + row / s) + safe_src_rect.left() + first_column / s;
962
0
            for (int x = 0; x < (last_column - first_column); ++x) {
963
0
                auto source_color = color_for_format(src_format, src[x / s]);
964
0
                if (source_color.alpha() == 0)
965
0
                    continue;
966
0
                auto filtered_color = filter(source_color);
967
0
                if (!apply_alpha || filtered_color.alpha() == 0xff)
968
0
                    dst[x] = filtered_color.value();
969
0
                else
970
0
                    dst[x] = color_for_format(dst_format, dst[x]).blend(filtered_color).value();
971
0
            }
972
0
            dst += dst_skip;
973
0
        }
974
0
    }
975
0
}
976
977
void Painter::blit_brightened(IntPoint position, Gfx::Bitmap const& source, IntRect const& src_rect)
978
0
{
979
0
    return blit_filtered(position, source, src_rect, [](Color src) {
980
0
        return src.lightened();
981
0
    });
982
0
}
983
984
void Painter::blit_dimmed(IntPoint position, Gfx::Bitmap const& source, IntRect const& src_rect)
985
0
{
986
0
    return blit_filtered(position, source, src_rect, [](Color src) {
987
0
        return src.to_grayscale().lightened();
988
0
    });
989
0
}
990
991
void Painter::draw_tiled_bitmap(IntRect const& a_dst_rect, Gfx::Bitmap const& source)
992
0
{
993
0
    VERIFY((source.scale() == 1 || source.scale() == scale()) && "draw_tiled_bitmap only supports integer upsampling");
994
995
0
    auto dst_rect = a_dst_rect.translated(translation());
996
0
    auto clipped_rect = dst_rect.intersected(clip_rect());
997
0
    if (clipped_rect.is_empty())
998
0
        return;
999
1000
0
    int scale = this->scale();
1001
0
    clipped_rect *= scale;
1002
0
    dst_rect *= scale;
1003
1004
0
    int const first_row = clipped_rect.top() - dst_rect.top();
1005
0
    int const last_row = clipped_rect.bottom() - dst_rect.top();
1006
0
    int const first_column = clipped_rect.left() - dst_rect.left();
1007
0
    ARGB32* dst = target().scanline(clipped_rect.y()) + clipped_rect.x();
1008
0
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
1009
1010
0
    if (source.format() == BitmapFormat::BGRx8888 || source.format() == BitmapFormat::BGRA8888) {
1011
0
        int s = scale / source.scale();
1012
0
        if (s == 1) {
1013
0
            int x_start = first_column + a_dst_rect.left() * scale;
1014
0
            for (int row = first_row; row < last_row; ++row) {
1015
0
                ARGB32 const* sl = source.scanline((row + a_dst_rect.top() * scale) % source.physical_height());
1016
0
                for (int x = x_start; x < clipped_rect.width() + x_start; ++x)
1017
0
                    dst[x - x_start] = sl[x % source.physical_width()];
1018
0
                dst += dst_skip;
1019
0
            }
1020
0
        } else {
1021
0
            int x_start = first_column + a_dst_rect.left() * scale;
1022
0
            for (int row = first_row; row < last_row; ++row) {
1023
0
                ARGB32 const* sl = source.scanline(((row + a_dst_rect.top() * scale) / s) % source.physical_height());
1024
0
                for (int x = x_start; x < clipped_rect.width() + x_start; ++x)
1025
0
                    dst[x - x_start] = sl[(x / s) % source.physical_width()];
1026
0
                dst += dst_skip;
1027
0
            }
1028
0
        }
1029
0
        return;
1030
0
    }
1031
1032
0
    VERIFY_NOT_REACHED();
1033
0
}
1034
1035
void Painter::blit_offset(IntPoint a_position, Gfx::Bitmap const& source, IntRect const& a_src_rect, IntPoint offset)
1036
0
{
1037
0
    auto src_rect = IntRect { a_src_rect.location() - offset, a_src_rect.size() };
1038
0
    auto position = a_position;
1039
0
    if (src_rect.x() < 0) {
1040
0
        position.set_x(position.x() - src_rect.x());
1041
0
        src_rect.set_x(0);
1042
0
    }
1043
0
    if (src_rect.y() < 0) {
1044
0
        position.set_y(position.y() - src_rect.y());
1045
0
        src_rect.set_y(0);
1046
0
    }
1047
0
    blit(position, source, src_rect);
1048
0
}
1049
1050
void Painter::blit(IntPoint position, Gfx::Bitmap const& source, IntRect const& a_src_rect, float opacity, bool apply_alpha)
1051
83
{
1052
83
    VERIFY(scale() >= source.scale() && "painter doesn't support downsampling scale factors");
1053
1054
83
    if (opacity < 1.0f || (source.has_alpha_channel() && apply_alpha))
1055
49
        return blit_with_opacity(position, source, a_src_rect, opacity, apply_alpha);
1056
1057
34
    auto safe_src_rect = a_src_rect.intersected(source.rect());
1058
34
    if (scale() != source.scale())
1059
0
        return draw_scaled_bitmap({ position, safe_src_rect.size() }, source, safe_src_rect, opacity);
1060
1061
    // If we get here, the Painter might have a scale factor, but the source bitmap has the same scale factor.
1062
    // We need to transform from logical to physical coordinates, but we can just copy pixels without resampling.
1063
34
    auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
1064
34
    auto clipped_rect = dst_rect.intersected(clip_rect());
1065
34
    if (clipped_rect.is_empty())
1066
0
        return;
1067
1068
    // All computations below are in physical coordinates.
1069
34
    int scale = this->scale();
1070
34
    auto src_rect = a_src_rect * scale;
1071
34
    clipped_rect *= scale;
1072
34
    dst_rect *= scale;
1073
1074
34
    int const first_row = clipped_rect.top() - dst_rect.top();
1075
34
    int const last_row = clipped_rect.bottom() - dst_rect.top();
1076
34
    int const first_column = clipped_rect.left() - dst_rect.left();
1077
34
    ARGB32* dst = target().scanline(clipped_rect.y()) + clipped_rect.x();
1078
34
    size_t const dst_skip = target().pitch() / sizeof(ARGB32);
1079
1080
34
    if (source.format() == BitmapFormat::BGRx8888 || source.format() == BitmapFormat::BGRA8888) {
1081
34
        ARGB32 const* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
1082
34
        size_t const src_skip = source.pitch() / sizeof(ARGB32);
1083
4.73k
        for (int row = first_row; row < last_row; ++row) {
1084
4.69k
            memcpy(dst, src, sizeof(ARGB32) * clipped_rect.width());
1085
4.69k
            dst += dst_skip;
1086
4.69k
            src += src_skip;
1087
4.69k
        }
1088
34
        return;
1089
34
    }
1090
1091
0
    if (source.format() == BitmapFormat::RGBA8888) {
1092
0
        u32 const* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column;
1093
0
        size_t const src_skip = source.pitch() / sizeof(u32);
1094
0
        for (int row = first_row; row < last_row; ++row) {
1095
0
            for (int i = 0; i < clipped_rect.width(); ++i) {
1096
0
                u32 rgba = src[i];
1097
0
                u32 bgra = (rgba & 0xff00ff00)
1098
0
                    | ((rgba & 0x000000ff) << 16)
1099
0
                    | ((rgba & 0x00ff0000) >> 16);
1100
0
                dst[i] = bgra;
1101
0
            }
1102
0
            dst += dst_skip;
1103
0
            src += src_skip;
1104
0
        }
1105
0
        return;
1106
0
    }
1107
1108
0
    VERIFY_NOT_REACHED();
1109
0
}
1110
1111
template<bool has_alpha_channel, typename GetPixel>
1112
ALWAYS_INLINE static void do_draw_integer_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& src_rect, Gfx::Bitmap const& source, int hfactor, int vfactor, GetPixel get_pixel, float opacity)
1113
0
{
1114
0
    bool has_opacity = opacity != 1.0f;
1115
0
    for (int y = 0; y < src_rect.height(); ++y) {
1116
0
        int dst_y = dst_rect.y() + y * vfactor;
1117
0
        for (int x = 0; x < src_rect.width(); ++x) {
1118
0
            auto src_pixel = get_pixel(source, x + src_rect.left(), y + src_rect.top());
1119
0
            if (has_opacity)
1120
0
                src_pixel.set_alpha(src_pixel.alpha() * opacity);
1121
0
            for (int yo = 0; yo < vfactor; ++yo) {
1122
0
                auto* scanline = (Color*)target.scanline(dst_y + yo);
1123
0
                int dst_x = dst_rect.x() + x * hfactor;
1124
0
                for (int xo = 0; xo < hfactor; ++xo) {
1125
                    if constexpr (has_alpha_channel)
1126
0
                        scanline[dst_x + xo] = scanline[dst_x + xo].blend(src_pixel);
1127
                    else
1128
0
                        scanline[dst_x + xo] = src_pixel;
1129
0
                }
1130
0
            }
1131
0
        }
1132
0
    }
1133
0
}
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_integer_scaled_bitmap<true, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, int, int, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_integer_scaled_bitmap<false, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, int, int, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
1134
1135
template<bool has_alpha_channel, typename GetPixel>
1136
ALWAYS_INLINE static void do_draw_box_sampled_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& clipped_rect, Gfx::Bitmap const& source, FloatRect const& src_rect, GetPixel get_pixel, float opacity)
1137
0
{
1138
0
    float source_pixel_width = src_rect.width() / dst_rect.width();
1139
0
    float source_pixel_height = src_rect.height() / dst_rect.height();
1140
0
    float source_pixel_area = source_pixel_width * source_pixel_height;
1141
0
    FloatRect const pixel_box = { 0.f, 0.f, 1.f, 1.f };
1142
1143
0
    for (int y = clipped_rect.top(); y < clipped_rect.bottom(); ++y) {
1144
0
        auto* scanline = reinterpret_cast<Color*>(target.scanline(y));
1145
0
        for (int x = clipped_rect.left(); x < clipped_rect.right(); ++x) {
1146
            // Project the destination pixel in the source image
1147
0
            FloatRect const source_box = {
1148
0
                src_rect.left() + (x - dst_rect.x()) * source_pixel_width,
1149
0
                src_rect.top() + (y - dst_rect.y()) * source_pixel_height,
1150
0
                source_pixel_width,
1151
0
                source_pixel_height,
1152
0
            };
1153
0
            IntRect enclosing_source_box = enclosing_int_rect(source_box).intersected(source.rect());
1154
1155
            // Sum the contribution of all source pixels inside the projected pixel
1156
0
            float red_accumulator = 0.f;
1157
0
            float green_accumulator = 0.f;
1158
0
            float blue_accumulator = 0.f;
1159
0
            float total_area = 0.f;
1160
0
            for (int sy = enclosing_source_box.y(); sy < enclosing_source_box.bottom(); ++sy) {
1161
0
                for (int sx = enclosing_source_box.x(); sx < enclosing_source_box.right(); ++sx) {
1162
0
                    float area = source_box.intersected(pixel_box.translated(sx, sy)).size().area();
1163
1164
0
                    auto pixel = get_pixel(source, sx, sy);
1165
0
                    area *= pixel.alpha() / 255.f;
1166
1167
0
                    red_accumulator += pixel.red() * area;
1168
0
                    green_accumulator += pixel.green() * area;
1169
0
                    blue_accumulator += pixel.blue() * area;
1170
0
                    total_area += area;
1171
0
                }
1172
0
            }
1173
1174
0
            Color src_pixel = {
1175
0
                round_to<u8>(min(red_accumulator / total_area, 255.f)),
1176
0
                round_to<u8>(min(green_accumulator / total_area, 255.f)),
1177
0
                round_to<u8>(min(blue_accumulator / total_area, 255.f)),
1178
0
                round_to<u8>(min(total_area * 255.f / source_pixel_area * opacity, 255.f)),
1179
0
            };
1180
1181
            if constexpr (has_alpha_channel)
1182
0
                scanline[x] = scanline[x].blend(src_pixel);
1183
            else
1184
0
                scanline[x] = src_pixel;
1185
0
        }
1186
0
    }
1187
0
}
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_box_sampled_scaled_bitmap<true, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_box_sampled_scaled_bitmap<false, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
1188
1189
template<bool has_alpha_channel, ScalingMode scaling_mode, typename GetPixel>
1190
ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& clipped_rect, Gfx::Bitmap const& source, FloatRect const& src_rect, GetPixel get_pixel, float opacity)
1191
0
{
1192
0
    auto int_src_rect = enclosing_int_rect(src_rect);
1193
0
    auto clipped_src_rect = int_src_rect.intersected(source.rect());
1194
0
    if (clipped_src_rect.is_empty())
1195
0
        return;
1196
1197
0
    if constexpr (scaling_mode == ScalingMode::NearestNeighbor || scaling_mode == ScalingMode::SmoothPixels) {
1198
0
        if (dst_rect == clipped_rect && int_src_rect == src_rect && !(dst_rect.width() % int_src_rect.width()) && !(dst_rect.height() % int_src_rect.height())) {
1199
0
            int hfactor = dst_rect.width() / int_src_rect.width();
1200
0
            int vfactor = dst_rect.height() / int_src_rect.height();
1201
0
            if (hfactor == 2 && vfactor == 2)
1202
0
                return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, 2, 2, get_pixel, opacity);
1203
0
            if (hfactor == 3 && vfactor == 3)
1204
0
                return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, 3, 3, get_pixel, opacity);
1205
0
            if (hfactor == 4 && vfactor == 4)
1206
0
                return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, 4, 4, get_pixel, opacity);
1207
0
            return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, int_src_rect, source, hfactor, vfactor, get_pixel, opacity);
1208
0
        }
1209
0
    }
1210
1211
    if constexpr (scaling_mode == ScalingMode::BoxSampling)
1212
0
        return do_draw_box_sampled_scaled_bitmap<has_alpha_channel>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
1213
1214
0
    bool has_opacity = opacity != 1.f;
1215
0
    i64 shift = 1ll << 32;
1216
0
    i64 fractional_mask = shift - 1;
1217
0
    i64 bilinear_offset_x = (1ll << 31) * (src_rect.width() / dst_rect.width() - 1);
1218
0
    i64 bilinear_offset_y = (1ll << 31) * (src_rect.height() / dst_rect.height() - 1);
1219
0
    i64 hscale = src_rect.width() * shift / dst_rect.width();
1220
0
    i64 vscale = src_rect.height() * shift / dst_rect.height();
1221
0
    i64 src_left = src_rect.left() * shift;
1222
0
    i64 src_top = src_rect.top() * shift;
1223
1224
0
    for (int y = clipped_rect.top(); y < clipped_rect.bottom(); ++y) {
1225
0
        auto* scanline = reinterpret_cast<Color*>(target.scanline(y));
1226
0
        auto desired_y = (y - dst_rect.y()) * vscale + src_top;
1227
1228
0
        for (int x = clipped_rect.left(); x < clipped_rect.right(); ++x) {
1229
0
            auto desired_x = (x - dst_rect.x()) * hscale + src_left;
1230
1231
0
            Color src_pixel;
1232
0
            if constexpr (scaling_mode == ScalingMode::BilinearBlend) {
1233
0
                auto shifted_x = desired_x + bilinear_offset_x;
1234
0
                auto shifted_y = desired_y + bilinear_offset_y;
1235
1236
0
                auto scaled_x0 = clamp(shifted_x >> 32, clipped_src_rect.left(), clipped_src_rect.right() - 1);
1237
0
                auto scaled_x1 = clamp((shifted_x >> 32) + 1, clipped_src_rect.left(), clipped_src_rect.right() - 1);
1238
0
                auto scaled_y0 = clamp(shifted_y >> 32, clipped_src_rect.top(), clipped_src_rect.bottom() - 1);
1239
0
                auto scaled_y1 = clamp((shifted_y >> 32) + 1, clipped_src_rect.top(), clipped_src_rect.bottom() - 1);
1240
1241
0
                float x_ratio = (shifted_x & fractional_mask) / static_cast<float>(shift);
1242
0
                float y_ratio = (shifted_y & fractional_mask) / static_cast<float>(shift);
1243
1244
0
                auto top_left = get_pixel(source, scaled_x0, scaled_y0);
1245
0
                auto top_right = get_pixel(source, scaled_x1, scaled_y0);
1246
0
                auto bottom_left = get_pixel(source, scaled_x0, scaled_y1);
1247
0
                auto bottom_right = get_pixel(source, scaled_x1, scaled_y1);
1248
1249
0
                auto top = top_left.mixed_with(top_right, x_ratio);
1250
0
                auto bottom = bottom_left.mixed_with(bottom_right, x_ratio);
1251
1252
0
                src_pixel = top.mixed_with(bottom, y_ratio);
1253
0
            } else if constexpr (scaling_mode == ScalingMode::SmoothPixels) {
1254
0
                auto scaled_x1 = clamp(desired_x >> 32, clipped_src_rect.left(), clipped_src_rect.right() - 1);
1255
0
                auto scaled_x0 = clamp(scaled_x1 - 1, clipped_src_rect.left(), clipped_src_rect.right() - 1);
1256
0
                auto scaled_y1 = clamp(desired_y >> 32, clipped_src_rect.top(), clipped_src_rect.bottom() - 1);
1257
0
                auto scaled_y0 = clamp(scaled_y1 - 1, clipped_src_rect.top(), clipped_src_rect.bottom() - 1);
1258
1259
0
                float x_ratio = (desired_x & fractional_mask) / (float)shift;
1260
0
                float y_ratio = (desired_y & fractional_mask) / (float)shift;
1261
1262
0
                float scaled_x_ratio = clamp(x_ratio * dst_rect.width() / (float)src_rect.width(), 0.f, 1.f);
1263
0
                float scaled_y_ratio = clamp(y_ratio * dst_rect.height() / (float)src_rect.height(), 0.f, 1.f);
1264
1265
0
                auto top_left = get_pixel(source, scaled_x0, scaled_y0);
1266
0
                auto top_right = get_pixel(source, scaled_x1, scaled_y0);
1267
0
                auto bottom_left = get_pixel(source, scaled_x0, scaled_y1);
1268
0
                auto bottom_right = get_pixel(source, scaled_x1, scaled_y1);
1269
1270
0
                auto top = top_left.mixed_with(top_right, scaled_x_ratio);
1271
0
                auto bottom = bottom_left.mixed_with(bottom_right, scaled_x_ratio);
1272
1273
0
                src_pixel = top.mixed_with(bottom, scaled_y_ratio);
1274
0
            } else {
1275
0
                auto scaled_x = clamp(desired_x >> 32, clipped_src_rect.left(), clipped_src_rect.right() - 1);
1276
0
                auto scaled_y = clamp(desired_y >> 32, clipped_src_rect.top(), clipped_src_rect.bottom() - 1);
1277
0
                src_pixel = get_pixel(source, scaled_x, scaled_y);
1278
0
            }
1279
1280
0
            if (has_opacity)
1281
0
                src_pixel.set_alpha(src_pixel.alpha() * opacity);
1282
1283
            if constexpr (has_alpha_channel)
1284
0
                scanline[x] = scanline[x].blend(src_pixel);
1285
            else
1286
0
                scanline[x] = src_pixel;
1287
0
        }
1288
0
    }
1289
0
}
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<true, (Gfx::ScalingMode)0, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<true, (Gfx::ScalingMode)1, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<true, (Gfx::ScalingMode)2, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<true, (Gfx::ScalingMode)3, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<true, (Gfx::ScalingMode)4, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<false, (Gfx::ScalingMode)0, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<false, (Gfx::ScalingMode)1, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<false, (Gfx::ScalingMode)2, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<false, (Gfx::ScalingMode)3, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<false, (Gfx::ScalingMode)4, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float)
1290
1291
template<bool has_alpha_channel, typename GetPixel>
1292
ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& clipped_rect, Gfx::Bitmap const& source, FloatRect const& src_rect, GetPixel get_pixel, float opacity, ScalingMode scaling_mode)
1293
0
{
1294
0
    switch (scaling_mode) {
1295
0
    case ScalingMode::NearestNeighbor:
1296
0
        do_draw_scaled_bitmap<has_alpha_channel, ScalingMode::NearestNeighbor>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
1297
0
        break;
1298
0
    case ScalingMode::SmoothPixels:
1299
0
        do_draw_scaled_bitmap<has_alpha_channel, ScalingMode::SmoothPixels>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
1300
0
        break;
1301
0
    case ScalingMode::BilinearBlend:
1302
0
        do_draw_scaled_bitmap<has_alpha_channel, ScalingMode::BilinearBlend>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
1303
0
        break;
1304
0
    case ScalingMode::BoxSampling:
1305
0
        do_draw_scaled_bitmap<has_alpha_channel, ScalingMode::BoxSampling>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
1306
0
        break;
1307
0
    case ScalingMode::None:
1308
0
        do_draw_scaled_bitmap<has_alpha_channel, ScalingMode::None>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
1309
0
        break;
1310
0
    }
1311
0
}
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<true, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float, Gfx::ScalingMode)
Unexecuted instantiation: Painter.cpp:void Gfx::do_draw_scaled_bitmap<false, Gfx::Color (*)(Gfx::Bitmap const&, int, int)>(Gfx::Bitmap&, Gfx::Rect<int> const&, Gfx::Rect<int> const&, Gfx::Bitmap const&, Gfx::Rect<float> const&, Gfx::Color (*)(Gfx::Bitmap const&, int, int), float, Gfx::ScalingMode)
1312
1313
void Painter::draw_scaled_bitmap(IntRect const& a_dst_rect, Gfx::Bitmap const& source, IntRect const& a_src_rect, float opacity, ScalingMode scaling_mode)
1314
0
{
1315
0
    draw_scaled_bitmap(a_dst_rect, source, FloatRect { a_src_rect }, opacity, scaling_mode);
1316
0
}
1317
1318
void Painter::draw_scaled_bitmap(IntRect const& a_dst_rect, Gfx::Bitmap const& source, FloatRect const& a_src_rect, float opacity, ScalingMode scaling_mode)
1319
0
{
1320
0
    IntRect int_src_rect = enclosing_int_rect(a_src_rect);
1321
0
    if (scale() == source.scale() && a_src_rect == int_src_rect && a_dst_rect.size() == int_src_rect.size())
1322
0
        return blit(a_dst_rect.location(), source, int_src_rect, opacity);
1323
1324
0
    if (scaling_mode == ScalingMode::None) {
1325
0
        IntRect clipped_draw_rect { (int)a_src_rect.location().x(), (int)a_src_rect.location().y(), a_dst_rect.size().width(), a_dst_rect.size().height() };
1326
0
        return blit(a_dst_rect.location(), source, clipped_draw_rect, opacity);
1327
0
    }
1328
1329
0
    auto dst_rect = to_physical(a_dst_rect);
1330
0
    auto src_rect = a_src_rect * source.scale();
1331
0
    auto clipped_rect = dst_rect.intersected(clip_rect() * scale());
1332
0
    if (clipped_rect.is_empty())
1333
0
        return;
1334
1335
0
    if (source.has_alpha_channel() || opacity != 1.0f) {
1336
0
        switch (source.format()) {
1337
0
        case BitmapFormat::BGRx8888:
1338
0
            do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel<BitmapFormat::BGRx8888>, opacity, scaling_mode);
1339
0
            break;
1340
0
        case BitmapFormat::BGRA8888:
1341
0
            do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel<BitmapFormat::BGRA8888>, opacity, scaling_mode);
1342
0
            break;
1343
0
        default:
1344
0
            do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel<BitmapFormat::Invalid>, opacity, scaling_mode);
1345
0
            break;
1346
0
        }
1347
0
    } else {
1348
0
        switch (source.format()) {
1349
0
        case BitmapFormat::BGRx8888:
1350
0
            do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel<BitmapFormat::BGRx8888>, opacity, scaling_mode);
1351
0
            break;
1352
0
        default:
1353
0
            do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel<BitmapFormat::Invalid>, opacity, scaling_mode);
1354
0
            break;
1355
0
        }
1356
0
    }
1357
0
}
1358
1359
ALWAYS_INLINE void Painter::draw_glyph(FloatPoint point, u32 code_point, Color color)
1360
0
{
1361
0
    draw_glyph(point, code_point, font(), color);
1362
0
}
1363
1364
FLATTEN void Painter::draw_glyph(FloatPoint point, u32 code_point, Font const& font, Color color)
1365
0
{
1366
0
    auto top_left = point + FloatPoint(font.glyph_left_bearing(code_point), 0);
1367
0
    auto glyph_position = Gfx::GlyphRasterPosition::get_nearest_fit_for(top_left);
1368
0
    auto maybe_glyph = font.glyph(code_point, glyph_position.subpixel_offset);
1369
0
    if (!maybe_glyph.has_value())
1370
0
        return;
1371
0
    auto glyph = maybe_glyph.release_value();
1372
0
    draw_glyph_internal(point, glyph_position, top_left, glyph, color);
1373
0
}
1374
1375
FLATTEN void Painter::draw_glyph_via_glyph_id(FloatPoint point, Gfx::ScaledFont const& font, u32 glyph_id, Color color)
1376
0
{
1377
0
    auto top_left = point + Gfx::FloatPoint(font.glyph_metrics(glyph_id).left_side_bearing, 0);
1378
0
    auto glyph_position = Gfx::GlyphRasterPosition::get_nearest_fit_for(top_left);
1379
0
    auto maybe_glyph = font.glyph_for_id(glyph_id, glyph_position.subpixel_offset);
1380
0
    if (!maybe_glyph.has_value())
1381
0
        return;
1382
0
    auto glyph = maybe_glyph.release_value();
1383
0
    draw_glyph_internal(point, glyph_position, top_left, glyph, color);
1384
0
}
1385
1386
FLATTEN void Painter::draw_glyph_internal(FloatPoint point, GlyphRasterPosition const& glyph_position, FloatPoint top_left, Glyph const& glyph, Color color)
1387
0
{
1388
0
    if (glyph.is_glyph_bitmap()) {
1389
0
        draw_bitmap(top_left.to_type<int>(), glyph.glyph_bitmap(), color);
1390
0
    } else if (glyph.is_color_bitmap()) {
1391
0
        float scaled_width = glyph.advance();
1392
0
        float ratio = static_cast<float>(glyph.bitmap()->height()) / static_cast<float>(glyph.bitmap()->width());
1393
0
        float scaled_height = scaled_width * ratio;
1394
1395
0
        FloatRect rect(point.x(), point.y(), scaled_width, scaled_height);
1396
0
        draw_scaled_bitmap(rect.to_rounded<int>(), *glyph.bitmap(), glyph.bitmap()->rect(), 1.0f, ScalingMode::BilinearBlend);
1397
0
    } else if (color.alpha() != 255) {
1398
0
        blit_filtered(glyph_position.blit_position, *glyph.bitmap(), glyph.bitmap()->rect(), [color](Color pixel) -> Color {
1399
0
            return pixel.multiply(color);
1400
0
        });
1401
0
    } else {
1402
0
        blit_filtered(glyph_position.blit_position, *glyph.bitmap(), glyph.bitmap()->rect(), [color](Color pixel) -> Color {
1403
0
            return color.with_alpha(pixel.alpha());
1404
0
        });
1405
0
    }
1406
0
}
1407
1408
void Painter::draw_emoji(IntPoint point, Gfx::Bitmap const& emoji, Font const& font)
1409
0
{
1410
0
    IntRect dst_rect {
1411
0
        point.x(),
1412
0
        point.y(),
1413
0
        font.pixel_size_rounded_up() * emoji.width() / emoji.height(),
1414
0
        font.pixel_size_rounded_up(),
1415
0
    };
1416
0
    draw_scaled_bitmap(dst_rect, emoji, emoji.rect());
1417
0
}
1418
1419
void Painter::draw_glyph_or_emoji(FloatPoint point, u32 code_point, Font const& font, Color color)
1420
0
{
1421
0
    StringBuilder builder;
1422
0
    builder.append_code_point(code_point);
1423
0
    auto it = Utf8View { builder.string_view() }.begin();
1424
0
    return draw_glyph_or_emoji(point, it, font, color);
1425
0
}
1426
1427
void Painter::draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font, Color color)
1428
0
{
1429
0
    auto draw_glyph_or_emoji = prepare_draw_glyph_or_emoji(point, it, font);
1430
0
    if (draw_glyph_or_emoji.has<DrawGlyph>()) {
1431
0
        auto& glyph = draw_glyph_or_emoji.get<DrawGlyph>();
1432
0
        draw_glyph(glyph.position, glyph.code_point, font, color);
1433
0
    } else {
1434
0
        auto& emoji = draw_glyph_or_emoji.get<DrawEmoji>();
1435
0
        draw_emoji(emoji.position.to_type<int>(), *emoji.emoji, font);
1436
0
    }
1437
0
}
1438
1439
void Painter::draw_glyph(IntPoint point, u32 code_point, Color color)
1440
0
{
1441
0
    draw_glyph(point.to_type<float>(), code_point, font(), color);
1442
0
}
1443
1444
void Painter::draw_glyph(IntPoint point, u32 code_point, Font const& font, Color color)
1445
0
{
1446
0
    draw_glyph(point.to_type<float>(), code_point, font, color);
1447
0
}
1448
1449
void Painter::draw_glyph_or_emoji(IntPoint point, u32 code_point, Font const& font, Color color)
1450
0
{
1451
0
    draw_glyph_or_emoji(point.to_type<float>(), code_point, font, color);
1452
0
}
1453
1454
void Painter::draw_glyph_or_emoji(IntPoint point, Utf8CodePointIterator& it, Font const& font, Color color)
1455
0
{
1456
0
    draw_glyph_or_emoji(point.to_type<float>(), it, font, color);
1457
0
}
1458
1459
template<typename DrawGlyphFunction>
1460
void draw_text_line(FloatRect const& a_rect, Utf8View const& text, Font const& font, TextAlignment alignment, TextDirection direction, DrawGlyphFunction draw_glyph)
1461
0
{
1462
0
    auto rect = a_rect;
1463
1464
0
    switch (alignment) {
1465
0
    case TextAlignment::TopLeft:
1466
0
    case TextAlignment::CenterLeft:
1467
0
    case TextAlignment::BottomLeft:
1468
0
        break;
1469
0
    case TextAlignment::TopRight:
1470
0
    case TextAlignment::CenterRight:
1471
0
    case TextAlignment::BottomRight:
1472
0
        rect.set_x(rect.right() - 1 - font.width(text));
1473
0
        break;
1474
0
    case TextAlignment::TopCenter:
1475
0
    case TextAlignment::BottomCenter:
1476
0
    case TextAlignment::Center: {
1477
0
        auto shrunken_rect = rect;
1478
0
        shrunken_rect.set_width(font.width(text));
1479
0
        shrunken_rect.center_within(rect);
1480
0
        rect = shrunken_rect;
1481
0
        break;
1482
0
    }
1483
0
    default:
1484
0
        VERIFY_NOT_REACHED();
1485
0
    }
1486
1487
0
    auto point = rect.location();
1488
0
    auto space_width = font.glyph_width(' ') + font.glyph_spacing();
1489
1490
0
    if (direction == TextDirection::RTL) {
1491
0
        point.translate_by(rect.width(), 0); // Start drawing from the end
1492
0
        space_width = -space_width;          // Draw spaces backwards
1493
0
    }
1494
1495
0
    u32 last_code_point { 0 };
1496
0
    for (auto it = text.begin(); it != text.end(); ++it) {
1497
0
        auto code_point = *it;
1498
0
        if (should_paint_as_space(code_point)) {
1499
0
            point.translate_by(space_width, 0);
1500
0
            last_code_point = code_point;
1501
0
            continue;
1502
0
        }
1503
1504
0
        auto kerning = font.glyphs_horizontal_kerning(last_code_point, code_point);
1505
0
        if (kerning != 0.0f)
1506
0
            point.translate_by(direction == TextDirection::LTR ? kerning : -kerning, 0);
1507
1508
0
        auto it_copy = it; // The callback function will advance the iterator, so create a copy for this lookup.
1509
0
        FloatSize glyph_size(font.glyph_or_emoji_width(it_copy) + font.glyph_spacing(), font.pixel_size());
1510
1511
0
        if (direction == TextDirection::RTL)
1512
0
            point.translate_by(-glyph_size.width(), 0); // If we are drawing right to left, we have to move backwards before drawing the glyph
1513
0
        draw_glyph({ point, glyph_size }, it);
1514
0
        if (direction == TextDirection::LTR)
1515
0
            point.translate_by(glyph_size.width(), 0);
1516
        // The callback function might have exhausted the iterator.
1517
0
        if (it == text.end())
1518
0
            break;
1519
0
        last_code_point = code_point;
1520
0
    }
1521
0
}
Unexecuted instantiation: Painter.cpp:void Gfx::draw_text_line<Gfx::Painter::draw_text(Gfx::Rect<float> const&, AK::StringView, Gfx::Font const&, Gfx::TextAlignment, Gfx::Color, Gfx::TextElision, Gfx::TextWrapping)::$_0>(Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextDirection, Gfx::Painter::draw_text(Gfx::Rect<float> const&, AK::StringView, Gfx::Font const&, Gfx::TextAlignment, Gfx::Color, Gfx::TextElision, Gfx::TextWrapping)::$_0)
Unexecuted instantiation: Painter.cpp:void Gfx::draw_text_line<Gfx::Painter::draw_text(Gfx::Rect<float> const&, AK::Utf32View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::Color, Gfx::TextElision, Gfx::TextWrapping)::$_0>(Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextDirection, Gfx::Painter::draw_text(Gfx::Rect<float> const&, AK::Utf32View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::Color, Gfx::TextElision, Gfx::TextWrapping)::$_0)
Unexecuted instantiation: Painter.cpp:void Gfx::draw_text_line<Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0>(Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextDirection, Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0)
Unexecuted instantiation: Painter.cpp:void Gfx::draw_text_line<Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::StringView, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0>(Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextDirection, Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::StringView, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0)
Unexecuted instantiation: Painter.cpp:void Gfx::draw_text_line<Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::Utf32View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0>(Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextDirection, Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::Utf32View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0)
1522
1523
static inline size_t draw_text_get_length(Utf8View const& text)
1524
0
{
1525
0
    return text.byte_length();
1526
0
}
1527
1528
Vector<DirectionalRun> Painter::split_text_into_directional_runs(Utf8View const& text, TextDirection initial_direction)
1529
0
{
1530
    // FIXME: This is a *very* simplified version of the UNICODE BIDIRECTIONAL ALGORITHM (https://www.unicode.org/reports/tr9/), that can render most bidirectional text
1531
    //  but also produces awkward results in a large amount of edge cases. This should probably be replaced with a fully spec compliant implementation at some point.
1532
1533
    // FIXME: Support HTML "dir" attribute (how?)
1534
0
    u8 paragraph_embedding_level = initial_direction == TextDirection::LTR ? 0 : 1;
1535
0
    Vector<u8> embedding_levels;
1536
0
    embedding_levels.ensure_capacity(text.length());
1537
0
    for (size_t i = 0; i < text.length(); i++)
1538
0
        embedding_levels.unchecked_append(paragraph_embedding_level);
1539
1540
    // FIXME: Support Explicit Directional Formatting Characters
1541
1542
0
    Vector<BidirectionalClass> character_classes;
1543
0
    character_classes.ensure_capacity(text.length());
1544
0
    for (u32 code_point : text)
1545
0
        character_classes.unchecked_append(get_char_bidi_class(code_point));
1546
1547
    // resolving weak types
1548
0
    BidirectionalClass paragraph_class = initial_direction == TextDirection::LTR ? BidirectionalClass::STRONG_LTR : BidirectionalClass::STRONG_RTL;
1549
0
    for (size_t i = 0; i < character_classes.size(); i++) {
1550
0
        if (character_classes[i] != BidirectionalClass::WEAK_SEPARATORS)
1551
0
            continue;
1552
0
        for (ssize_t j = i - 1; j >= 0; j--) {
1553
0
            auto character_class = character_classes[j];
1554
0
            if (character_class != BidirectionalClass::STRONG_RTL && character_class != BidirectionalClass::STRONG_LTR)
1555
0
                continue;
1556
0
            character_classes[i] = character_class;
1557
0
            break;
1558
0
        }
1559
0
        if (character_classes[i] == BidirectionalClass::WEAK_SEPARATORS)
1560
0
            character_classes[i] = paragraph_class;
1561
0
    }
1562
1563
    // resolving neutral types
1564
0
    auto left_side = BidirectionalClass::NEUTRAL;
1565
0
    auto sequence_length = 0;
1566
0
    for (size_t i = 0; i < character_classes.size(); i++) {
1567
0
        auto character_class = character_classes[i];
1568
0
        if (left_side == BidirectionalClass::NEUTRAL) {
1569
0
            if (character_class != BidirectionalClass::NEUTRAL)
1570
0
                left_side = character_class;
1571
0
            else
1572
0
                character_classes[i] = paragraph_class;
1573
0
            continue;
1574
0
        }
1575
0
        if (character_class != BidirectionalClass::NEUTRAL) {
1576
0
            BidirectionalClass sequence_class;
1577
0
            if (bidi_class_to_direction(left_side) == bidi_class_to_direction(character_class)) {
1578
0
                sequence_class = left_side == BidirectionalClass::STRONG_RTL ? BidirectionalClass::STRONG_RTL : BidirectionalClass::STRONG_LTR;
1579
0
            } else {
1580
0
                sequence_class = paragraph_class;
1581
0
            }
1582
0
            for (auto j = 0; j < sequence_length; j++) {
1583
0
                character_classes[i - j - 1] = sequence_class;
1584
0
            }
1585
0
            sequence_length = 0;
1586
0
            left_side = character_class;
1587
0
        } else {
1588
0
            sequence_length++;
1589
0
        }
1590
0
    }
1591
0
    for (auto i = 0; i < sequence_length; i++)
1592
0
        character_classes[character_classes.size() - i - 1] = paragraph_class;
1593
1594
    // resolving implicit levels
1595
0
    for (size_t i = 0; i < character_classes.size(); i++) {
1596
0
        auto character_class = character_classes[i];
1597
0
        if ((embedding_levels[i] % 2) == 0) {
1598
0
            if (character_class == BidirectionalClass::STRONG_RTL)
1599
0
                embedding_levels[i] += 1;
1600
0
            else if (character_class == BidirectionalClass::WEAK_NUMBERS || character_class == BidirectionalClass::WEAK_SEPARATORS)
1601
0
                embedding_levels[i] += 2;
1602
0
        } else {
1603
0
            if (character_class == BidirectionalClass::STRONG_LTR || character_class == BidirectionalClass::WEAK_NUMBERS || character_class == BidirectionalClass::WEAK_SEPARATORS)
1604
0
                embedding_levels[i] += 1;
1605
0
        }
1606
0
    }
1607
1608
    // splitting into runs
1609
0
    auto run_code_points_start = text.begin();
1610
0
    auto next_code_points_slice = [&](auto length) {
1611
0
        Vector<u32> run_code_points;
1612
0
        run_code_points.ensure_capacity(length);
1613
0
        for (size_t j = 0; j < length; ++j, ++run_code_points_start)
1614
0
            run_code_points.unchecked_append(*run_code_points_start);
1615
0
        return run_code_points;
1616
0
    };
1617
0
    Vector<DirectionalRun> runs;
1618
0
    size_t start = 0;
1619
0
    u8 level = embedding_levels[0];
1620
0
    for (size_t i = 1; i < embedding_levels.size(); ++i) {
1621
0
        if (embedding_levels[i] == level)
1622
0
            continue;
1623
0
        auto code_points_slice = next_code_points_slice(i - start);
1624
0
        runs.append({ move(code_points_slice), level });
1625
0
        start = i;
1626
0
        level = embedding_levels[i];
1627
0
    }
1628
0
    auto code_points_slice = next_code_points_slice(embedding_levels.size() - start);
1629
0
    runs.append({ move(code_points_slice), level });
1630
1631
    // reordering resolved levels
1632
    // FIXME: missing special cases for trailing whitespace characters
1633
0
    u8 minimum_level = 128;
1634
0
    u8 maximum_level = 0;
1635
0
    for (auto& run : runs) {
1636
0
        minimum_level = min(minimum_level, run.embedding_level());
1637
0
        maximum_level = max(minimum_level, run.embedding_level());
1638
0
    }
1639
0
    if ((minimum_level % 2) == 0)
1640
0
        minimum_level++;
1641
0
    auto runs_count = runs.size() - 1;
1642
0
    while (maximum_level <= minimum_level) {
1643
0
        size_t run_index = 0;
1644
0
        while (run_index < runs_count) {
1645
0
            while (run_index < runs_count && runs[run_index].embedding_level() < maximum_level)
1646
0
                run_index++;
1647
0
            auto reverse_start = run_index;
1648
0
            while (run_index <= runs_count && runs[run_index].embedding_level() >= maximum_level)
1649
0
                run_index++;
1650
0
            auto reverse_end = run_index - 1;
1651
0
            while (reverse_start < reverse_end) {
1652
0
                swap(runs[reverse_start], runs[reverse_end]);
1653
0
                reverse_start++;
1654
0
                reverse_end--;
1655
0
            }
1656
0
        }
1657
0
        maximum_level--;
1658
0
    }
1659
1660
    // mirroring RTL mirror characters
1661
0
    for (auto& run : runs) {
1662
0
        if (run.direction() == TextDirection::LTR)
1663
0
            continue;
1664
0
        for (auto& code_point : run.code_points()) {
1665
0
            code_point = get_mirror_char(code_point);
1666
0
        }
1667
0
    }
1668
1669
0
    return runs;
1670
0
}
1671
1672
bool Painter::text_contains_bidirectional_text(Utf8View const& text, TextDirection initial_direction)
1673
0
{
1674
0
    for (u32 code_point : text) {
1675
0
        auto char_class = get_char_bidi_class(code_point);
1676
0
        if (char_class == BidirectionalClass::NEUTRAL)
1677
0
            continue;
1678
0
        if (bidi_class_to_direction(char_class) != initial_direction)
1679
0
            return true;
1680
0
    }
1681
0
    return false;
1682
0
}
1683
1684
template<typename DrawGlyphFunction>
1685
void Painter::do_draw_text(FloatRect const& rect, Utf8View const& text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping, DrawGlyphFunction draw_glyph)
1686
0
{
1687
0
    if (draw_text_get_length(text) == 0)
1688
0
        return;
1689
1690
0
    TextLayout layout(font, text, rect);
1691
1692
0
    auto line_height = font.preferred_line_height();
1693
1694
0
    auto lines = layout.lines(elision, wrapping);
1695
0
    auto bounding_rect = layout.bounding_rect(wrapping);
1696
1697
0
    bounding_rect.align_within(rect, alignment);
1698
1699
0
    for (size_t i = 0; i < lines.size(); ++i) {
1700
0
        auto line = Utf8View { lines[i] };
1701
1702
0
        FloatRect line_rect { bounding_rect.x(), bounding_rect.y() + i * line_height, bounding_rect.width(), line_height };
1703
1704
0
        TextDirection line_direction = get_text_direction(line);
1705
0
        if (text_contains_bidirectional_text(line, line_direction)) { // Slow Path: The line contains mixed BiDi classes
1706
0
            auto directional_runs = split_text_into_directional_runs(line, line_direction);
1707
0
            auto current_dx = line_direction == TextDirection::LTR ? 0 : line_rect.width();
1708
0
            for (auto& directional_run : directional_runs) {
1709
0
                auto run_width = font.width(directional_run.text());
1710
0
                if (line_direction == TextDirection::RTL)
1711
0
                    current_dx -= run_width;
1712
0
                auto run_rect = line_rect.translated(current_dx, 0);
1713
0
                run_rect.set_width(run_width);
1714
1715
                // NOTE: DirectionalRun returns Utf32View which isn't
1716
                // compatible with draw_text_line.
1717
0
                StringBuilder builder;
1718
0
                builder.append(directional_run.text());
1719
0
                auto line_text = Utf8View { builder.string_view() };
1720
1721
0
                draw_text_line(run_rect, line_text, font, alignment, directional_run.direction(), draw_glyph);
1722
0
                if (line_direction == TextDirection::LTR)
1723
0
                    current_dx += run_width;
1724
0
            }
1725
0
        } else {
1726
0
            draw_text_line(line_rect, line, font, alignment, line_direction, draw_glyph);
1727
0
        }
1728
0
    }
1729
0
}
Unexecuted instantiation: Painter.cpp:void Gfx::Painter::do_draw_text<Gfx::Painter::draw_text(Gfx::Rect<float> const&, AK::StringView, Gfx::Font const&, Gfx::TextAlignment, Gfx::Color, Gfx::TextElision, Gfx::TextWrapping)::$_0>(Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping, Gfx::Painter::draw_text(Gfx::Rect<float> const&, AK::StringView, Gfx::Font const&, Gfx::TextAlignment, Gfx::Color, Gfx::TextElision, Gfx::TextWrapping)::$_0)
Unexecuted instantiation: Painter.cpp:void Gfx::Painter::do_draw_text<Gfx::Painter::draw_text(Gfx::Rect<float> const&, AK::Utf32View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::Color, Gfx::TextElision, Gfx::TextWrapping)::$_0>(Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping, Gfx::Painter::draw_text(Gfx::Rect<float> const&, AK::Utf32View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::Color, Gfx::TextElision, Gfx::TextWrapping)::$_0)
Unexecuted instantiation: Painter.cpp:void Gfx::Painter::do_draw_text<Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0>(Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping, Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0)
Unexecuted instantiation: Painter.cpp:void Gfx::Painter::do_draw_text<Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::StringView, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0>(Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping, Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::StringView, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0)
Unexecuted instantiation: Painter.cpp:void Gfx::Painter::do_draw_text<Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::Utf32View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0>(Gfx::Rect<float> const&, AK::Utf8View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping, Gfx::Painter::draw_text(AK::Function<void (Gfx::Rect<float> const&, AK::Utf8CodePointIterator&)>, Gfx::Rect<float> const&, AK::Utf32View const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, Gfx::TextWrapping)::$_0)
1730
1731
void Painter::draw_text(FloatRect const& rect, StringView text, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
1732
0
{
1733
0
    draw_text(rect, text, font(), alignment, color, elision, wrapping);
1734
0
}
1735
1736
void Painter::draw_text(FloatRect const& rect, Utf32View const& text, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
1737
0
{
1738
0
    draw_text(rect, text, font(), alignment, color, elision, wrapping);
1739
0
}
1740
1741
void Painter::draw_text(FloatRect const& rect, StringView raw_text, Font const& font, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
1742
0
{
1743
0
    Utf8View text { raw_text };
1744
0
    do_draw_text(rect, text, font, alignment, elision, wrapping, [&](FloatRect const& r, Utf8CodePointIterator& it) {
1745
0
        draw_glyph_or_emoji(r.location(), it, font, color);
1746
0
    });
1747
0
}
1748
1749
void Painter::draw_text(FloatRect const& rect, Utf32View const& raw_text, Font const& font, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
1750
0
{
1751
    // FIXME: UTF-32 should eventually be completely removed, but for the time
1752
    // being some places might depend on it, so we do some internal conversion.
1753
0
    StringBuilder builder;
1754
0
    builder.append(raw_text);
1755
0
    auto text = Utf8View { builder.string_view() };
1756
0
    do_draw_text(rect, text, font, alignment, elision, wrapping, [&](FloatRect const& r, Utf8CodePointIterator& it) {
1757
0
        draw_glyph_or_emoji(r.location(), it, font, color);
1758
0
    });
1759
0
}
1760
1761
void Painter::draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)> draw_one_glyph, FloatRect const& rect, Utf8View const& text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping)
1762
0
{
1763
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
1764
1765
0
    do_draw_text(rect, text, font, alignment, elision, wrapping, [&](FloatRect const& r, Utf8CodePointIterator& it) {
1766
0
        draw_one_glyph(r, it);
1767
0
    });
1768
0
}
1769
1770
void Painter::draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)> draw_one_glyph, FloatRect const& rect, StringView raw_text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping)
1771
0
{
1772
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
1773
1774
0
    Utf8View text { raw_text };
1775
0
    do_draw_text(rect, text, font, alignment, elision, wrapping, [&](FloatRect const& r, Utf8CodePointIterator& it) {
1776
0
        draw_one_glyph(r, it);
1777
0
    });
1778
0
}
1779
1780
void Painter::draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)> draw_one_glyph, FloatRect const& rect, Utf32View const& raw_text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping)
1781
0
{
1782
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
1783
1784
    // FIXME: UTF-32 should eventually be completely removed, but for the time
1785
    // being some places might depend on it, so we do some internal conversion.
1786
0
    StringBuilder builder;
1787
0
    builder.append(raw_text);
1788
0
    auto text = Utf8View { builder.string_view() };
1789
0
    do_draw_text(rect, text, font, alignment, elision, wrapping, [&](FloatRect const& r, Utf8CodePointIterator& it) {
1790
0
        draw_one_glyph(r, it);
1791
0
    });
1792
0
}
1793
1794
void Painter::draw_text(IntRect const& rect, StringView text, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
1795
0
{
1796
0
    draw_text(rect.to_type<float>(), text, font(), alignment, color, elision, wrapping);
1797
0
}
1798
1799
void Painter::draw_text(IntRect const& rect, Utf32View const& text, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
1800
0
{
1801
0
    draw_text(rect.to_type<float>(), text, font(), alignment, color, elision, wrapping);
1802
0
}
1803
1804
void Painter::draw_text(IntRect const& rect, StringView raw_text, Font const& font, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
1805
0
{
1806
0
    draw_text(rect.to_type<float>(), raw_text, font, alignment, color, elision, wrapping);
1807
0
}
1808
1809
void Painter::draw_text(IntRect const& rect, Utf32View const& raw_text, Font const& font, TextAlignment alignment, Color color, TextElision elision, TextWrapping wrapping)
1810
0
{
1811
0
    return draw_text(rect.to_type<float>(), raw_text, font, alignment, color, elision, wrapping);
1812
0
}
1813
1814
void Painter::draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)> draw_one_glyph, IntRect const& rect, Utf8View const& text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping)
1815
0
{
1816
0
    return draw_text(move(draw_one_glyph), rect.to_type<float>(), text, font, alignment, elision, wrapping);
1817
0
}
1818
1819
void Painter::draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)> draw_one_glyph, IntRect const& rect, StringView raw_text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping)
1820
0
{
1821
0
    return draw_text(move(draw_one_glyph), rect.to_type<float>(), raw_text, font, alignment, elision, wrapping);
1822
0
}
1823
1824
void Painter::draw_text(Function<void(FloatRect const&, Utf8CodePointIterator&)> draw_one_glyph, IntRect const& rect, Utf32View const& raw_text, Font const& font, TextAlignment alignment, TextElision elision, TextWrapping wrapping)
1825
0
{
1826
0
    return draw_text(move(draw_one_glyph), rect.to_type<float>(), raw_text, font, alignment, elision, wrapping);
1827
0
}
1828
1829
void Painter::set_pixel(IntPoint p, Color color, bool blend)
1830
0
{
1831
0
    auto point = p;
1832
0
    point.translate_by(state().translation);
1833
    // Use the scale only to avoid clipping pixels set in drawing functions that handle
1834
    // scaling and call set_pixel() -- do not scale the pixel.
1835
0
    if (!clip_rect().contains(point / scale()))
1836
0
        return;
1837
0
    set_physical_pixel(point, color, blend);
1838
0
}
1839
1840
void Painter::set_physical_pixel(IntPoint physical_point, Color color, bool blend)
1841
0
{
1842
    // This function should only be called after translation, clipping, etc has been handled elsewhere
1843
    // if not use set_pixel().
1844
0
    auto& dst = target().scanline(physical_point.y())[physical_point.x()];
1845
0
    if (!blend || color.alpha() == 255)
1846
0
        dst = color.value();
1847
0
    else if (color.alpha())
1848
0
        dst = color_for_format(target().format(), dst).blend(color).value();
1849
0
}
1850
1851
Optional<Color> Painter::get_pixel(IntPoint p)
1852
0
{
1853
0
    auto point = p;
1854
0
    point.translate_by(state().translation);
1855
0
    if (!clip_rect().contains(point / scale()))
1856
0
        return {};
1857
0
    return target().get_pixel(point);
1858
0
}
1859
1860
ErrorOr<NonnullRefPtr<Bitmap>> Painter::get_region_bitmap(IntRect const& region, BitmapFormat format, Optional<IntRect&> actual_region)
1861
0
{
1862
0
    VERIFY(scale() == 1);
1863
0
    auto bitmap_region = region.translated(state().translation).intersected(target().rect());
1864
0
    if (actual_region.has_value())
1865
0
        actual_region.value() = bitmap_region.translated(-state().translation);
1866
0
    return target().cropped(bitmap_region, format);
1867
0
}
1868
1869
ALWAYS_INLINE void Painter::set_physical_pixel_with_draw_op(u32& pixel, Color color)
1870
0
{
1871
    // This always sets a single physical pixel, independent of scale().
1872
    // This should only be called by routines that already handle scale.
1873
1874
0
    switch (draw_op()) {
1875
0
    case DrawOp::Copy:
1876
0
        pixel = color.value();
1877
0
        break;
1878
0
    case DrawOp::Xor:
1879
0
        pixel = color.xored(Color::from_argb(pixel)).value();
1880
0
        break;
1881
0
    case DrawOp::Invert:
1882
0
        pixel = Color::from_argb(pixel).inverted().value();
1883
0
        break;
1884
0
    }
1885
0
}
1886
1887
ALWAYS_INLINE void Painter::fill_physical_scanline_with_draw_op(int y, int x, int width, Color color)
1888
0
{
1889
    // This always draws a single physical scanline, independent of scale().
1890
    // This should only be called by routines that already handle scale.
1891
0
    auto dst_format = target().format();
1892
0
    switch (draw_op()) {
1893
0
    case DrawOp::Copy:
1894
0
        fast_u32_fill(target().scanline(y) + x, color.value(), width);
1895
0
        break;
1896
0
    case DrawOp::Xor: {
1897
0
        auto* pixel = target().scanline(y) + x;
1898
0
        auto* end = pixel + width;
1899
0
        while (pixel < end) {
1900
0
            *pixel = color_for_format(dst_format, *pixel).xored(color).value();
1901
0
            pixel++;
1902
0
        }
1903
0
        break;
1904
0
    }
1905
0
    case DrawOp::Invert: {
1906
0
        auto* pixel = target().scanline(y) + x;
1907
0
        auto* end = pixel + width;
1908
0
        while (pixel < end) {
1909
0
            *pixel = color_for_format(dst_format, *pixel).inverted().value();
1910
0
            pixel++;
1911
0
        }
1912
0
        break;
1913
0
    }
1914
0
    }
1915
0
}
1916
1917
void Painter::draw_physical_pixel(IntPoint physical_position, Color color, int thickness)
1918
0
{
1919
    // This always draws a single physical pixel, independent of scale().
1920
    // This should only be called by routines that already handle scale
1921
    // (including scaling thickness).
1922
0
    VERIFY(draw_op() == DrawOp::Copy);
1923
1924
0
    if (thickness <= 0)
1925
0
        return;
1926
1927
0
    if (thickness == 1) { // Implies scale() == 1.
1928
0
        auto& pixel = target().scanline(physical_position.y())[physical_position.x()];
1929
0
        return set_physical_pixel_with_draw_op(pixel, color_for_format(target().format(), pixel).blend(color));
1930
0
    }
1931
1932
0
    IntRect rect { physical_position, { thickness, thickness } };
1933
0
    rect.intersect(clip_rect() * scale());
1934
0
    if (rect.is_empty())
1935
0
        return;
1936
0
    fill_physical_rect(rect, color);
1937
0
}
1938
1939
void Painter::draw_line(IntPoint a_p1, IntPoint a_p2, Color color, int thickness, LineStyle style, Color alternate_color)
1940
0
{
1941
0
    if (clip_rect().is_empty())
1942
0
        return;
1943
1944
0
    if (thickness <= 0)
1945
0
        return;
1946
1947
0
    if (color.alpha() == 0)
1948
0
        return;
1949
1950
0
    auto clip_rect = this->clip_rect() * scale();
1951
1952
0
    auto const p1 = thickness > 1 ? a_p1.translated(-(thickness / 2), -(thickness / 2)) : a_p1;
1953
0
    auto const p2 = thickness > 1 ? a_p2.translated(-(thickness / 2), -(thickness / 2)) : a_p2;
1954
1955
0
    auto point1 = to_physical(p1);
1956
0
    auto point2 = to_physical(p2);
1957
0
    thickness *= scale();
1958
1959
0
    auto alternate_color_is_transparent = alternate_color == Color::Transparent;
1960
1961
    // Special case: vertical line.
1962
0
    if (point1.x() == point2.x()) {
1963
0
        int const x = point1.x();
1964
0
        if (x < clip_rect.left() || x >= clip_rect.right())
1965
0
            return;
1966
0
        if (point1.y() > point2.y())
1967
0
            swap(point1, point2);
1968
0
        if (point1.y() >= clip_rect.bottom())
1969
0
            return;
1970
0
        if (point2.y() < clip_rect.top())
1971
0
            return;
1972
0
        int min_y = max(point1.y(), clip_rect.top());
1973
0
        int max_y = min(point2.y(), clip_rect.bottom() - 1);
1974
0
        if (style == LineStyle::Dotted) {
1975
0
            for (int y = min_y; y <= max_y; y += thickness * 2)
1976
0
                draw_physical_pixel({ x, y }, color, thickness);
1977
0
        } else if (style == LineStyle::Dashed) {
1978
0
            for (int y = min_y; y <= max_y; y += thickness * 6) {
1979
0
                draw_physical_pixel({ x, y }, color, thickness);
1980
0
                draw_physical_pixel({ x, min(y + thickness, max_y) }, color, thickness);
1981
0
                draw_physical_pixel({ x, min(y + thickness * 2, max_y) }, color, thickness);
1982
0
                if (!alternate_color_is_transparent) {
1983
0
                    draw_physical_pixel({ x, min(y + thickness * 3, max_y) }, alternate_color, thickness);
1984
0
                    draw_physical_pixel({ x, min(y + thickness * 4, max_y) }, alternate_color, thickness);
1985
0
                    draw_physical_pixel({ x, min(y + thickness * 5, max_y) }, alternate_color, thickness);
1986
0
                }
1987
0
            }
1988
0
        } else {
1989
0
            fill_physical_rect({ x, min_y, thickness, max_y - min_y + thickness }, color);
1990
0
        }
1991
0
        return;
1992
0
    }
1993
1994
    // Special case: horizontal line.
1995
0
    if (point1.y() == point2.y()) {
1996
0
        int const y = point1.y();
1997
0
        if (y < clip_rect.top() || y >= clip_rect.bottom())
1998
0
            return;
1999
0
        if (point1.x() > point2.x())
2000
0
            swap(point1, point2);
2001
0
        if (point1.x() >= clip_rect.right())
2002
0
            return;
2003
0
        if (point2.x() < clip_rect.left())
2004
0
            return;
2005
0
        int min_x = max(point1.x(), clip_rect.left());
2006
0
        int max_x = min(point2.x(), clip_rect.right() - 1);
2007
0
        if (style == LineStyle::Dotted) {
2008
0
            for (int x = min_x; x <= max_x; x += thickness * 2)
2009
0
                draw_physical_pixel({ x, y }, color, thickness);
2010
0
        } else if (style == LineStyle::Dashed) {
2011
0
            for (int x = min_x; x <= max_x; x += thickness * 6) {
2012
0
                draw_physical_pixel({ x, y }, color, thickness);
2013
0
                draw_physical_pixel({ min(x + thickness, max_x), y }, color, thickness);
2014
0
                draw_physical_pixel({ min(x + thickness * 2, max_x), y }, color, thickness);
2015
0
                if (!alternate_color_is_transparent) {
2016
0
                    draw_physical_pixel({ min(x + thickness * 3, max_x), y }, alternate_color, thickness);
2017
0
                    draw_physical_pixel({ min(x + thickness * 4, max_x), y }, alternate_color, thickness);
2018
0
                    draw_physical_pixel({ min(x + thickness * 5, max_x), y }, alternate_color, thickness);
2019
0
                }
2020
0
            }
2021
0
        } else {
2022
0
            fill_physical_rect({ min_x, y, max_x - min_x + thickness, thickness }, color);
2023
0
        }
2024
0
        return;
2025
0
    }
2026
2027
0
    int const adx = abs(point2.x() - point1.x());
2028
0
    int const ady = abs(point2.y() - point1.y());
2029
2030
0
    if (adx > ady) {
2031
0
        if (point1.x() > point2.x())
2032
0
            swap(point1, point2);
2033
0
    } else {
2034
0
        if (point1.y() > point2.y())
2035
0
            swap(point1, point2);
2036
0
    }
2037
2038
0
    int const dx = point2.x() - point1.x();
2039
0
    int const dy = point2.y() - point1.y();
2040
0
    int error = 0;
2041
2042
0
    size_t number_of_pixels_drawn = 0;
2043
2044
0
    auto draw_pixel_in_line = [&](int x, int y) {
2045
0
        bool should_draw_line = true;
2046
0
        if (style == LineStyle::Dotted && number_of_pixels_drawn % 2 == 1)
2047
0
            should_draw_line = false;
2048
0
        else if (style == LineStyle::Dashed && number_of_pixels_drawn % 6 >= 3)
2049
0
            should_draw_line = false;
2050
2051
0
        if (should_draw_line)
2052
0
            draw_physical_pixel({ x, y }, color, thickness);
2053
0
        else if (!alternate_color_is_transparent)
2054
0
            draw_physical_pixel({ x, y }, alternate_color, thickness);
2055
2056
0
        number_of_pixels_drawn++;
2057
0
    };
2058
2059
0
    if (dx > dy) {
2060
0
        int const y_step = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
2061
0
        int const delta_error = 2 * abs(dy);
2062
0
        int y = point1.y();
2063
0
        for (int x = point1.x(); x <= point2.x(); ++x) {
2064
0
            if (clip_rect.contains(x, y))
2065
0
                draw_pixel_in_line(x, y);
2066
0
            error += delta_error;
2067
0
            if (error >= dx) {
2068
0
                y += y_step;
2069
0
                error -= 2 * dx;
2070
0
            }
2071
0
        }
2072
0
    } else {
2073
0
        int const x_step = dx == 0 ? 0 : (dx > 0 ? 1 : -1);
2074
0
        int const delta_error = 2 * abs(dx);
2075
0
        int x = point1.x();
2076
0
        for (int y = point1.y(); y <= point2.y(); ++y) {
2077
0
            if (clip_rect.contains(x, y))
2078
0
                draw_pixel_in_line(x, y);
2079
0
            error += delta_error;
2080
0
            if (error >= dy) {
2081
0
                x += x_step;
2082
0
                error -= 2 * dy;
2083
0
            }
2084
0
        }
2085
0
    }
2086
0
}
2087
2088
void Painter::draw_triangle_wave(IntPoint a_p1, IntPoint a_p2, Color color, int amplitude, int thickness)
2089
0
{
2090
    // FIXME: Support more than horizontal waves
2091
0
    VERIFY(a_p1.y() == a_p2.y());
2092
2093
0
    auto const p1 = thickness > 1 ? a_p1.translated(-(thickness / 2), -(thickness / 2)) : a_p1;
2094
0
    auto const p2 = thickness > 1 ? a_p2.translated(-(thickness / 2), -(thickness / 2)) : a_p2;
2095
2096
0
    auto point1 = to_physical(p1);
2097
0
    auto point2 = to_physical(p2);
2098
2099
0
    auto y = point1.y();
2100
2101
0
    for (int x = 0; x <= point2.x() - point1.x(); ++x) {
2102
0
        auto y_offset = abs(x % (2 * amplitude) - amplitude) - amplitude;
2103
0
        draw_physical_pixel({ point1.x() + x, y + y_offset }, color, thickness);
2104
0
    }
2105
0
}
2106
2107
static bool can_approximate_bezier_curve(FloatPoint p1, FloatPoint p2, FloatPoint control)
2108
26.4M
{
2109
    // TODO: Somehow calculate the required number of splits based on the curve (and its size).
2110
26.4M
    constexpr float tolerance = 0.5f;
2111
2112
26.4M
    auto p1x = 3 * control.x() - 2 * p1.x() - p2.x();
2113
26.4M
    auto p1y = 3 * control.y() - 2 * p1.y() - p2.y();
2114
26.4M
    auto p2x = 3 * control.x() - 2 * p2.x() - p1.x();
2115
26.4M
    auto p2y = 3 * control.y() - 2 * p2.y() - p1.y();
2116
2117
26.4M
    p1x = p1x * p1x;
2118
26.4M
    p1y = p1y * p1y;
2119
26.4M
    p2x = p2x * p2x;
2120
26.4M
    p2y = p2y * p2y;
2121
2122
26.4M
    auto error = max(p1x, p2x) + max(p1y, p2y);
2123
26.4M
    VERIFY(isfinite(error));
2124
2125
26.4M
    return error <= tolerance;
2126
26.4M
}
2127
2128
static float approximate_bezier_curve_length(FloatPoint control_point, FloatPoint p1, FloatPoint p2)
2129
6.28k
{
2130
6.28k
    return p1.distance_from(control_point) + control_point.distance_from(p2);
2131
6.28k
}
2132
2133
// static
2134
void Painter::for_each_line_segment_on_bezier_curve(FloatPoint control_point, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>& callback)
2135
6.28k
{
2136
6.28k
    struct SegmentDescriptor {
2137
6.28k
        FloatPoint control_point;
2138
6.28k
        FloatPoint p1;
2139
6.28k
        FloatPoint p2;
2140
6.28k
        int depth { 0 };
2141
2142
6.28k
        void split(Vector<SegmentDescriptor>& segments)
2143
13.8M
        {
2144
13.8M
            auto po1_midpoint = control_point + p1;
2145
13.8M
            po1_midpoint /= 2;
2146
2147
13.8M
            auto po2_midpoint = control_point + p2;
2148
13.8M
            po2_midpoint /= 2;
2149
2150
13.8M
            auto new_segment = po1_midpoint + po2_midpoint;
2151
13.8M
            new_segment /= 2;
2152
2153
13.8M
            segments.append({ po2_midpoint, new_segment, p2, depth + 1 });
2154
13.8M
            segments.append({ po1_midpoint, p1, new_segment, depth + 1 });
2155
13.8M
        }
2156
6.28k
    };
2157
2158
    // Limit splitting to the point where curves are approximately half a pixel in length.
2159
6.28k
    int max_split_depth = ceilf(log2(approximate_bezier_curve_length(control_point, p1, p2))) + 1;
2160
2161
6.28k
    Vector<SegmentDescriptor> segments;
2162
6.28k
    segments.append({ control_point, p1, p2 });
2163
27.7M
    while (!segments.is_empty()) {
2164
27.7M
        auto segment = segments.take_last();
2165
2166
27.7M
        if (segment.depth >= max_split_depth
2167
26.4M
            || can_approximate_bezier_curve(segment.p1, segment.p2, segment.control_point))
2168
13.8M
            callback(segment.p1, segment.p2);
2169
13.8M
        else
2170
13.8M
            segment.split(segments);
2171
27.7M
    }
2172
6.28k
}
2173
2174
void Painter::for_each_line_segment_on_bezier_curve(FloatPoint control_point, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&& callback)
2175
6.28k
{
2176
6.28k
    for_each_line_segment_on_bezier_curve(control_point, p1, p2, callback);
2177
6.28k
}
2178
2179
void Painter::draw_quadratic_bezier_curve(IntPoint control_point, IntPoint p1, IntPoint p2, Color color, int thickness, LineStyle style)
2180
0
{
2181
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
2182
2183
0
    if (thickness <= 0)
2184
0
        return;
2185
2186
0
    for_each_line_segment_on_bezier_curve(FloatPoint(control_point), FloatPoint(p1), FloatPoint(p2), [&](FloatPoint fp1, FloatPoint fp2) {
2187
0
        draw_line(IntPoint(fp1.x(), fp1.y()), IntPoint(fp2.x(), fp2.y()), color, thickness, style);
2188
0
    });
2189
0
}
2190
2191
void Painter::for_each_line_segment_on_cubic_bezier_curve(FloatPoint control_point_0, FloatPoint control_point_1, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&& callback)
2192
1.67M
{
2193
1.67M
    for_each_line_segment_on_cubic_bezier_curve(control_point_0, control_point_1, p1, p2, callback);
2194
1.67M
}
2195
2196
static bool can_approximate_cubic_bezier_curve(FloatPoint p1, FloatPoint p2, FloatPoint control_0, FloatPoint control_1)
2197
52.2M
{
2198
    // TODO: Somehow calculate the required number of splits based on the curve (and its size).
2199
52.2M
    constexpr float tolerance = 0.5f;
2200
2201
52.2M
    auto ax = 3 * control_0.x() - 2 * p1.x() - p2.x();
2202
52.2M
    auto ay = 3 * control_0.y() - 2 * p1.y() - p2.y();
2203
52.2M
    auto bx = 3 * control_1.x() - p1.x() - 2 * p2.x();
2204
52.2M
    auto by = 3 * control_1.y() - p1.y() - 2 * p2.y();
2205
2206
52.2M
    ax *= ax;
2207
52.2M
    ay *= ay;
2208
52.2M
    bx *= bx;
2209
52.2M
    by *= by;
2210
2211
52.2M
    auto error = max(ax, bx) + max(ay, by);
2212
52.2M
    VERIFY(isfinite(error));
2213
2214
52.2M
    return error <= tolerance;
2215
52.2M
}
2216
2217
static float approximate_cubic_bezier_curve_length(FloatPoint control_point_0, FloatPoint control_point_1, FloatPoint p1, FloatPoint p2)
2218
1.67M
{
2219
1.67M
    return p1.distance_from(control_point_0) + control_point_0.distance_from(control_point_1) + control_point_1.distance_from(p2);
2220
1.67M
}
2221
2222
// static
2223
void Painter::for_each_line_segment_on_cubic_bezier_curve(FloatPoint control_point_0, FloatPoint control_point_1, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>& callback)
2224
1.67M
{
2225
1.67M
    struct ControlPair {
2226
1.67M
        FloatPoint control_point_0;
2227
1.67M
        FloatPoint control_point_1;
2228
1.67M
    };
2229
1.67M
    struct SegmentDescriptor {
2230
1.67M
        ControlPair control_points;
2231
1.67M
        FloatPoint p1;
2232
1.67M
        FloatPoint p2;
2233
1.67M
        int depth { 0 };
2234
2235
1.67M
        void split(Vector<SegmentDescriptor>& segments)
2236
39.5M
        {
2237
39.5M
            Array level_1_midpoints {
2238
39.5M
                (p1 + control_points.control_point_0) / 2,
2239
39.5M
                (control_points.control_point_0 + control_points.control_point_1) / 2,
2240
39.5M
                (control_points.control_point_1 + p2) / 2,
2241
39.5M
            };
2242
39.5M
            Array level_2_midpoints {
2243
39.5M
                (level_1_midpoints[0] + level_1_midpoints[1]) / 2,
2244
39.5M
                (level_1_midpoints[1] + level_1_midpoints[2]) / 2,
2245
39.5M
            };
2246
39.5M
            auto level_3_midpoint = (level_2_midpoints[0] + level_2_midpoints[1]) / 2;
2247
2248
39.5M
            segments.append({ { level_2_midpoints[1], level_1_midpoints[2] }, level_3_midpoint, p2, depth + 1 });
2249
39.5M
            segments.append({ { level_1_midpoints[0], level_2_midpoints[0] }, p1, level_3_midpoint, depth + 1 });
2250
39.5M
        }
2251
1.67M
    };
2252
2253
    // Limit splitting to the point where curves are approximately half a pixel in length.
2254
1.67M
    int max_split_depth = ceilf(log2(approximate_cubic_bezier_curve_length(control_point_0, control_point_1, p1, p2))) + 1;
2255
2256
1.67M
    Vector<SegmentDescriptor> segments;
2257
1.67M
    segments.append({ { control_point_0, control_point_1 }, p1, p2 });
2258
82.4M
    while (!segments.is_empty()) {
2259
80.7M
        auto segment = segments.take_last();
2260
80.7M
        if (segment.depth >= max_split_depth
2261
52.2M
            || can_approximate_cubic_bezier_curve(segment.p1, segment.p2, segment.control_points.control_point_0, segment.control_points.control_point_1))
2262
41.2M
            callback(segment.p1, segment.p2);
2263
39.5M
        else
2264
39.5M
            segment.split(segments);
2265
80.7M
    }
2266
1.67M
}
2267
2268
void Painter::draw_cubic_bezier_curve(IntPoint control_point_0, IntPoint control_point_1, IntPoint p1, IntPoint p2, Color color, int thickness, LineStyle style)
2269
0
{
2270
0
    for_each_line_segment_on_cubic_bezier_curve(FloatPoint(control_point_0), FloatPoint(control_point_1), FloatPoint(p1), FloatPoint(p2), [&](FloatPoint fp1, FloatPoint fp2) {
2271
0
        draw_line(IntPoint(fp1.x(), fp1.y()), IntPoint(fp2.x(), fp2.y()), color, thickness, style);
2272
0
    });
2273
0
}
2274
2275
// static
2276
void Painter::for_each_line_segment_on_elliptical_arc(FloatPoint p1, FloatPoint p2, FloatPoint center, FloatSize radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(FloatPoint, FloatPoint)>& callback)
2277
0
{
2278
0
    if (radii.width() <= 0 || radii.height() <= 0)
2279
0
        return;
2280
2281
0
    auto start = p1;
2282
0
    auto end = p2;
2283
2284
0
    bool start_swapped = false;
2285
0
    if (theta_delta < 0) {
2286
0
        swap(start, end);
2287
0
        theta_1 = theta_1 + theta_delta;
2288
0
        theta_delta = fabsf(theta_delta);
2289
0
        start_swapped = true;
2290
0
    }
2291
2292
0
    auto relative_start = start - center;
2293
2294
0
    auto a = radii.width();
2295
0
    auto b = radii.height();
2296
2297
    // The segments are at most 1 long
2298
0
    auto largest_radius = max(a, b);
2299
0
    float theta_step = AK::atan2(1.f, (float)largest_radius);
2300
2301
0
    FloatPoint current_point = relative_start;
2302
0
    FloatPoint next_point = { 0, 0 };
2303
2304
0
    float sin_x_axis, cos_x_axis;
2305
0
    AK::sincos(x_axis_rotation, sin_x_axis, cos_x_axis);
2306
0
    auto rotate_point = [sin_x_axis, cos_x_axis](FloatPoint& p) {
2307
0
        auto original_x = p.x();
2308
0
        auto original_y = p.y();
2309
2310
0
        p.set_x(original_x * cos_x_axis - original_y * sin_x_axis);
2311
0
        p.set_y(original_x * sin_x_axis + original_y * cos_x_axis);
2312
0
    };
2313
2314
0
    auto emit_point = [&](auto p0, auto p1) {
2315
        // NOTE: If we swap the start/end we must swap the emitted points, so correct winding orders can be calculated.
2316
0
        if (start_swapped)
2317
0
            swap(p0, p1);
2318
0
        callback(p0, p1);
2319
0
    };
2320
2321
0
    for (float theta = theta_1; theta <= theta_1 + theta_delta; theta += theta_step) {
2322
0
        float s, c;
2323
0
        AK::sincos(theta, s, c);
2324
0
        next_point.set_x(a * c);
2325
0
        next_point.set_y(b * s);
2326
0
        rotate_point(next_point);
2327
2328
0
        emit_point(current_point + center, next_point + center);
2329
2330
0
        current_point = next_point;
2331
0
    }
2332
2333
0
    emit_point(current_point + center, end);
2334
0
}
2335
2336
// static
2337
void Painter::for_each_line_segment_on_elliptical_arc(FloatPoint p1, FloatPoint p2, FloatPoint center, FloatSize radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(FloatPoint, FloatPoint)>&& callback)
2338
0
{
2339
0
    for_each_line_segment_on_elliptical_arc(p1, p2, center, radii, x_axis_rotation, theta_1, theta_delta, callback);
2340
0
}
2341
2342
void Painter::draw_elliptical_arc(IntPoint p1, IntPoint p2, IntPoint center, FloatSize radii, float x_axis_rotation, float theta_1, float theta_delta, Color color, int thickness, LineStyle style)
2343
0
{
2344
0
    VERIFY(scale() == 1); // FIXME: Add scaling support.
2345
2346
0
    if (thickness <= 0)
2347
0
        return;
2348
2349
0
    for_each_line_segment_on_elliptical_arc(FloatPoint(p1), FloatPoint(p2), FloatPoint(center), radii, x_axis_rotation, theta_1, theta_delta, [&](FloatPoint fp1, FloatPoint fp2) {
2350
0
        draw_line(IntPoint(fp1.x(), fp1.y()), IntPoint(fp2.x(), fp2.y()), color, thickness, style);
2351
0
    });
2352
0
}
2353
2354
void Painter::add_clip_rect(IntRect const& rect)
2355
0
{
2356
0
    state().clip_rect.intersect(rect.translated(translation()));
2357
0
    state().clip_rect.intersect(target().rect()); // FIXME: This shouldn't be necessary?
2358
0
}
2359
2360
void Painter::clear_clip_rect()
2361
0
{
2362
0
    state().clip_rect = m_clip_origin;
2363
0
}
2364
2365
PainterStateSaver::PainterStateSaver(Painter& painter)
2366
0
    : m_painter(painter)
2367
0
{
2368
0
    m_painter.save();
2369
0
}
2370
2371
PainterStateSaver::~PainterStateSaver()
2372
0
{
2373
0
    m_painter.restore();
2374
0
}
2375
2376
void Painter::stroke_path(Path const& path, Color color, int thickness)
2377
0
{
2378
0
    if (thickness <= 0)
2379
0
        return;
2380
0
    fill_path(path.stroke_to_fill({ .thickness = static_cast<float>(thickness) }), color);
2381
0
}
2382
2383
void Painter::blit_disabled(IntPoint location, Gfx::Bitmap const& bitmap, IntRect const& rect, Palette const& palette)
2384
0
{
2385
0
    auto bright_color = palette.threed_highlight();
2386
0
    auto dark_color = palette.threed_shadow1();
2387
0
    blit_filtered(location.translated(1, 1), bitmap, rect, [&](auto) {
2388
0
        return bright_color;
2389
0
    });
2390
0
    blit_filtered(location, bitmap, rect, [&](Color src) {
2391
0
        int gray = src.to_grayscale().red();
2392
0
        if (gray > 160)
2393
0
            return bright_color;
2394
0
        return dark_color;
2395
0
    });
2396
0
}
2397
2398
void Painter::blit_tiled(IntRect const& dst_rect, Gfx::Bitmap const& bitmap, IntRect const& rect)
2399
0
{
2400
0
    auto tile_width = rect.width();
2401
0
    auto tile_height = rect.height();
2402
0
    auto dst_right = dst_rect.right() - 1;
2403
0
    auto dst_bottom = dst_rect.bottom() - 1;
2404
0
    for (int tile_y = dst_rect.top(); tile_y < dst_bottom; tile_y += tile_height) {
2405
0
        for (int tile_x = dst_rect.left(); tile_x < dst_right; tile_x += tile_width) {
2406
0
            IntRect tile_src_rect = rect;
2407
0
            auto tile_x_overflow = tile_x + tile_width - dst_right;
2408
0
            if (tile_x_overflow > 0)
2409
0
                tile_src_rect.set_width(tile_width - tile_x_overflow);
2410
0
            auto tile_y_overflow = tile_y + tile_height - dst_bottom;
2411
0
            if (tile_y_overflow > 0)
2412
0
                tile_src_rect.set_height(tile_height - tile_y_overflow);
2413
0
            blit(IntPoint(tile_x, tile_y), bitmap, tile_src_rect);
2414
0
        }
2415
0
    }
2416
0
}
2417
2418
ByteString parse_ampersand_string(StringView raw_text, Optional<size_t>* underline_offset)
2419
0
{
2420
0
    if (raw_text.is_empty())
2421
0
        return ByteString::empty();
2422
2423
0
    StringBuilder builder;
2424
2425
0
    for (size_t i = 0; i < raw_text.length(); ++i) {
2426
0
        if (raw_text[i] == '&') {
2427
0
            if (i != (raw_text.length() - 1) && raw_text[i + 1] == '&') {
2428
0
                builder.append(raw_text[i]);
2429
0
                ++i;
2430
0
            } else if (underline_offset && !(*underline_offset).has_value()) {
2431
0
                *underline_offset = i;
2432
0
            }
2433
0
            continue;
2434
0
        }
2435
0
        builder.append(raw_text[i]);
2436
0
    }
2437
0
    return builder.to_byte_string();
2438
0
}
2439
2440
void Gfx::Painter::draw_ui_text(Gfx::IntRect const& rect, StringView text, Gfx::Font const& font, Gfx::TextAlignment text_alignment, Gfx::Color color)
2441
0
{
2442
0
    Optional<size_t> underline_offset;
2443
0
    auto name_to_draw = parse_ampersand_string(text, &underline_offset);
2444
2445
0
    Gfx::IntRect text_rect { 0, 0, font.width_rounded_up(name_to_draw), font.pixel_size_rounded_up() };
2446
0
    text_rect.align_within(rect, text_alignment);
2447
2448
0
    draw_text(text_rect, name_to_draw, font, text_alignment, color);
2449
2450
0
    if (underline_offset.has_value()) {
2451
0
        Utf8View utf8_view { name_to_draw };
2452
0
        float width = 0;
2453
0
        for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
2454
0
            if (utf8_view.byte_offset_of(it) >= underline_offset.value()) {
2455
0
                int y = text_rect.bottom();
2456
0
                int x1 = text_rect.left() + width;
2457
0
                int x2 = x1 + font.glyph_or_emoji_width(it);
2458
0
                draw_line({ x1, y }, { x2, y }, color);
2459
0
                break;
2460
0
            }
2461
0
            width += font.glyph_or_emoji_width(it) + font.glyph_spacing();
2462
0
        }
2463
0
    }
2464
0
}
2465
2466
void Painter::draw_text_run(IntPoint baseline_start, Utf8View const& string, Font const& font, Color color)
2467
0
{
2468
0
    draw_text_run(baseline_start.to_type<float>(), string, font, color);
2469
0
}
2470
2471
void Painter::draw_text_run(FloatPoint baseline_start, Utf8View const& string, Font const& font, Color color)
2472
0
{
2473
0
    for_each_glyph_position(baseline_start, string, font, [&](DrawGlyphOrEmoji glyph_or_emoji) {
2474
0
        if (glyph_or_emoji.has<DrawGlyph>()) {
2475
0
            auto& glyph = glyph_or_emoji.get<DrawGlyph>();
2476
0
            draw_glyph(glyph.position, glyph.code_point, font, color);
2477
0
        } else {
2478
0
            auto& emoji = glyph_or_emoji.get<DrawEmoji>();
2479
0
            draw_emoji(emoji.position.to_type<int>(), *emoji.emoji, font);
2480
0
        }
2481
0
    });
2482
0
}
2483
2484
void Painter::draw_scaled_bitmap_with_transform(IntRect const& dst_rect, Bitmap const& bitmap, FloatRect const& src_rect, AffineTransform const& transform, float opacity, ScalingMode scaling_mode)
2485
0
{
2486
0
    if (transform.is_identity_or_translation_or_scale(Gfx::AffineTransform::AllowNegativeScaling::No)) {
2487
0
        draw_scaled_bitmap(transform.map(dst_rect.to_type<float>()).to_rounded<int>(), bitmap, src_rect, opacity, scaling_mode);
2488
0
    } else {
2489
        // The painter has an affine transform, we have to draw through it!
2490
2491
        // FIXME: This is kinda inefficient.
2492
        // What we currently do, roughly:
2493
        // - Map the destination rect through the context's transform.
2494
        // - Compute the bounding rect of the destination quad.
2495
        // - For each point in the clipped bounding rect, reverse-map it to a point in the source image.
2496
        //   - Sample the source image at the computed point.
2497
        //   - Set or blend (depending on alpha values) one pixel in the canvas.
2498
        //   - Loop.
2499
2500
        // FIXME: Painter should have an affine transform as part of its state and handle all of this instead.
2501
2502
0
        if (opacity == 0.0f)
2503
0
            return;
2504
2505
0
        auto inverse_transform = transform.inverse();
2506
0
        if (!inverse_transform.has_value())
2507
0
            return;
2508
2509
0
        auto destination_quad = transform.map_to_quad(dst_rect.to_type<float>());
2510
0
        auto destination_bounding_rect = destination_quad.bounding_rect().to_rounded<int>();
2511
0
        auto source_rect = enclosing_int_rect(src_rect).intersected(bitmap.rect());
2512
2513
0
        Gfx::AffineTransform source_transform;
2514
0
        source_transform.translate(src_rect.x(), src_rect.y());
2515
0
        source_transform.scale(src_rect.width() / dst_rect.width(), src_rect.height() / dst_rect.height());
2516
0
        source_transform.translate(-dst_rect.x(), -dst_rect.y());
2517
2518
0
        auto translated_dest_rect = destination_bounding_rect.translated(translation());
2519
0
        auto clipped_bounding_rect = translated_dest_rect.intersected(clip_rect());
2520
0
        if (clipped_bounding_rect.is_empty())
2521
0
            return;
2522
2523
0
        auto sample_transform = source_transform.multiply(*inverse_transform);
2524
0
        auto start_offset = destination_bounding_rect.location() + (clipped_bounding_rect.location() - translated_dest_rect.location());
2525
0
        for (int y = 0; y < clipped_bounding_rect.height(); ++y) {
2526
0
            for (int x = 0; x < clipped_bounding_rect.width(); ++x) {
2527
0
                auto point = Gfx::IntPoint { x, y };
2528
0
                auto sample_point = point + start_offset;
2529
2530
                // AffineTransform::map(IntPoint) rounds internally, which is wrong here. So explicitly call the FloatPoint version, and then truncate the result.
2531
0
                auto source_point = Gfx::IntPoint { sample_transform.map(Gfx::FloatPoint { sample_point }) };
2532
2533
0
                if (!source_rect.contains(source_point))
2534
0
                    continue;
2535
0
                auto source_color = bitmap.get_pixel(source_point);
2536
0
                if (source_color.alpha() == 0)
2537
0
                    continue;
2538
0
                if (opacity != 1.0f)
2539
0
                    source_color = source_color.with_opacity(opacity);
2540
0
                set_physical_pixel(point + clipped_bounding_rect.location(), source_color, true);
2541
0
            }
2542
0
        }
2543
0
    }
2544
0
}
2545
2546
}