-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathPainter.cpp
2527 lines (2166 loc) · 99.4 KB
/
Painter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Painter.h"
#include "Bitmap.h"
#include "Font/Emoji.h"
#include "Font/Font.h"
#include <AK/Assertions.h>
#include <AK/Debug.h>
#include <AK/Function.h>
#include <AK/Math.h>
#include <AK/Memory.h>
#include <AK/Queue.h>
#include <AK/QuickSort.h>
#include <AK/Stack.h>
#include <AK/StdLibExtras.h>
#include <AK/StringBuilder.h>
#include <AK/Utf32View.h>
#include <AK/Utf8View.h>
#include <LibGfx/CharacterBitmap.h>
#include <LibGfx/Palette.h>
#include <LibGfx/Path.h>
#include <LibGfx/Quad.h>
#include <LibGfx/TextDirection.h>
#include <LibGfx/TextLayout.h>
#include <LibUnicode/CharacterTypes.h>
#include <LibUnicode/Emoji.h>
#include <stdio.h>
#if defined(AK_COMPILER_GCC)
# pragma GCC optimize("O3")
#endif
namespace Gfx {
template<BitmapFormat format = BitmapFormat::Invalid>
ALWAYS_INLINE Color get_pixel(Gfx::Bitmap const& bitmap, int x, int y)
{
if constexpr (format == BitmapFormat::BGRx8888)
return Color::from_rgb(bitmap.scanline(y)[x]);
if constexpr (format == BitmapFormat::BGRA8888)
return Color::from_argb(bitmap.scanline(y)[x]);
return bitmap.get_pixel(x, y);
}
Painter::Painter(Gfx::Bitmap& bitmap)
: m_target(bitmap)
{
int scale = bitmap.scale();
VERIFY(bitmap.format() == Gfx::BitmapFormat::BGRx8888 || bitmap.format() == Gfx::BitmapFormat::BGRA8888);
VERIFY(bitmap.physical_width() % scale == 0);
VERIFY(bitmap.physical_height() % scale == 0);
m_state_stack.append(State());
state().font = nullptr;
state().clip_rect = { { 0, 0 }, bitmap.size() };
state().scale = scale;
m_clip_origin = state().clip_rect;
}
void Painter::fill_rect_with_draw_op(IntRect const& a_rect, Color color)
{
VERIFY(scale() == 1); // FIXME: Add scaling support.
auto rect = a_rect.translated(translation()).intersected(clip_rect());
if (rect.is_empty())
return;
ARGB32* dst = target().scanline(rect.top()) + rect.left();
size_t const dst_skip = target().pitch() / sizeof(ARGB32);
for (int i = rect.height() - 1; i >= 0; --i) {
for (int j = 0; j < rect.width(); ++j)
set_physical_pixel_with_draw_op(dst[j], color);
dst += dst_skip;
}
}
void Painter::clear_rect(IntRect const& a_rect, Color color)
{
auto rect = a_rect.translated(translation()).intersected(clip_rect());
if (rect.is_empty())
return;
VERIFY(target().rect().contains(rect));
rect *= scale();
ARGB32* dst = target().scanline(rect.top()) + rect.left();
size_t const dst_skip = target().pitch() / sizeof(ARGB32);
for (int i = rect.height() - 1; i >= 0; --i) {
fast_u32_fill(dst, color.value(), rect.width());
dst += dst_skip;
}
}
void Painter::fill_physical_rect(IntRect const& physical_rect, Color color)
{
// Callers must do clipping.
ARGB32* dst = target().scanline(physical_rect.top()) + physical_rect.left();
size_t const dst_skip = target().pitch() / sizeof(ARGB32);
auto dst_format = target().format();
for (int i = physical_rect.height() - 1; i >= 0; --i) {
for (int j = 0; j < physical_rect.width(); ++j)
dst[j] = color_for_format(dst_format, dst[j]).blend(color).value();
dst += dst_skip;
}
}
void Painter::fill_rect(IntRect const& a_rect, Color color)
{
if (color.alpha() == 0)
return;
if (draw_op() != DrawOp::Copy) {
fill_rect_with_draw_op(a_rect, color);
return;
}
if (color.alpha() == 0xff) {
clear_rect(a_rect, color);
return;
}
auto rect = a_rect.translated(translation()).intersected(clip_rect());
if (rect.is_empty())
return;
VERIFY(target().rect().contains(rect));
fill_physical_rect(rect * scale(), color);
}
void Painter::fill_rect(IntRect const& rect, PaintStyle const& paint_style)
{
auto a_rect = rect.translated(translation());
auto clipped_rect = a_rect.intersected(clip_rect());
if (clipped_rect.is_empty())
return;
a_rect *= scale();
clipped_rect *= scale();
auto start_offset = clipped_rect.location() - a_rect.location();
paint_style.paint(a_rect, [&](PaintStyle::SamplerFunction sample) {
for (int y = 0; y < clipped_rect.height(); ++y) {
for (int x = 0; x < clipped_rect.width(); ++x) {
IntPoint point(x, y);
set_physical_pixel(point + clipped_rect.location(), sample(point + start_offset), true);
}
}
});
}
void Painter::fill_rect_with_dither_pattern(IntRect const& a_rect, Color color_a, Color color_b)
{
VERIFY(scale() == 1); // FIXME: Add scaling support.
auto rect = a_rect.translated(translation()).intersected(clip_rect());
if (rect.is_empty())
return;
ARGB32* dst = target().scanline(rect.top()) + rect.left();
size_t const dst_skip = target().pitch() / sizeof(ARGB32);
for (int i = 0; i < rect.height(); ++i) {
for (int j = 0; j < rect.width(); ++j) {
bool checkboard_use_a = ((rect.left() + i) & 1) ^ ((rect.top() + j) & 1);
if (checkboard_use_a && !color_a.alpha())
continue;
if (!checkboard_use_a && !color_b.alpha())
continue;
dst[j] = checkboard_use_a ? color_a.value() : color_b.value();
}
dst += dst_skip;
}
}
void Painter::fill_rect_with_checkerboard(IntRect const& a_rect, IntSize cell_size, Color color_dark, Color color_light)
{
VERIFY(scale() == 1); // FIXME: Add scaling support.
auto translated_rect = a_rect.translated(translation());
auto rect = translated_rect.intersected(clip_rect());
if (rect.is_empty())
return;
ARGB32* dst = target().scanline(rect.top()) + rect.left();
size_t const dst_skip = target().pitch() / sizeof(ARGB32);
int first_cell_column = (rect.x() - translated_rect.x()) / cell_size.width();
int prologue_length = min(rect.width(), cell_size.width() - ((rect.x() - translated_rect.x()) % cell_size.width()));
int number_of_aligned_strips = (rect.width() - prologue_length) / cell_size.width();
for (int i = 0; i < rect.height(); ++i) {
int y = rect.y() - translated_rect.y() + i;
int cell_row = y / cell_size.height();
bool odd_row = cell_row & 1;
// Prologue: Paint the unaligned part up to the first intersection.
int j = 0;
int cell_column = first_cell_column;
{
bool odd_cell = cell_column & 1;
auto color = (odd_row ^ odd_cell) ? color_light.value() : color_dark.value();
fast_u32_fill(&dst[j], color, prologue_length);
j += prologue_length;
}
// Aligned run: Paint the maximum number of aligned cell strips.
for (int strip = 0; strip < number_of_aligned_strips; ++strip) {
++cell_column;
bool odd_cell = cell_column & 1;
auto color = (odd_row ^ odd_cell) ? color_light.value() : color_dark.value();
fast_u32_fill(&dst[j], color, cell_size.width());
j += cell_size.width();
}
// Epilogue: Paint the unaligned part until the end of the rect.
if (j != rect.width()) {
++cell_column;
bool odd_cell = cell_column & 1;
auto color = (odd_row ^ odd_cell) ? color_light.value() : color_dark.value();
int epilogue_length = rect.width() - j;
fast_u32_fill(&dst[j], color, epilogue_length);
j += epilogue_length;
}
dst += dst_skip;
}
}
void Painter::fill_rect_with_gradient(Orientation orientation, IntRect const& a_rect, Color gradient_start, Color gradient_end)
{
if (gradient_start == gradient_end) {
fill_rect(a_rect, gradient_start);
return;
}
return fill_rect_with_linear_gradient(a_rect, Array { ColorStop { gradient_start, 0 }, ColorStop { gradient_end, 1 } }, orientation == Orientation::Horizontal ? 90.0f : 0.0f);
}
void Painter::fill_rect_with_gradient(IntRect const& a_rect, Color gradient_start, Color gradient_end)
{
return fill_rect_with_gradient(Orientation::Horizontal, a_rect, gradient_start, gradient_end);
}
void Painter::fill_rect_with_rounded_corners(IntRect const& a_rect, Color color, int radius)
{
return fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
}
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)
{
// Fasttrack for rects without any border radii
if (!top_left_radius && !top_right_radius && !bottom_right_radius && !bottom_left_radius)
return fill_rect(a_rect, color);
// Fully transparent, dont care.
if (color.alpha() == 0)
return;
// FIXME: Allow for elliptically rounded corners
IntRect top_left_corner = {
a_rect.x(),
a_rect.y(),
top_left_radius,
top_left_radius
};
IntRect top_right_corner = {
a_rect.x() + a_rect.width() - top_right_radius,
a_rect.y(),
top_right_radius,
top_right_radius
};
IntRect bottom_right_corner = {
a_rect.x() + a_rect.width() - bottom_right_radius,
a_rect.y() + a_rect.height() - bottom_right_radius,
bottom_right_radius,
bottom_right_radius
};
IntRect bottom_left_corner = {
a_rect.x(),
a_rect.y() + a_rect.height() - bottom_left_radius,
bottom_left_radius,
bottom_left_radius
};
IntRect top_rect = {
a_rect.x() + top_left_radius,
a_rect.y(),
a_rect.width() - top_left_radius - top_right_radius, top_left_radius
};
IntRect right_rect = {
a_rect.x() + a_rect.width() - top_right_radius,
a_rect.y() + top_right_radius,
top_right_radius,
a_rect.height() - top_right_radius - bottom_right_radius
};
IntRect bottom_rect = {
a_rect.x() + bottom_left_radius,
a_rect.y() + a_rect.height() - bottom_right_radius,
a_rect.width() - bottom_left_radius - bottom_right_radius,
bottom_right_radius
};
IntRect left_rect = {
a_rect.x(),
a_rect.y() + top_left_radius,
bottom_left_radius,
a_rect.height() - top_left_radius - bottom_left_radius
};
IntRect inner = {
left_rect.x() + left_rect.width(),
left_rect.y(),
a_rect.width() - left_rect.width() - right_rect.width(),
a_rect.height() - top_rect.height() - bottom_rect.height()
};
fill_rect(top_rect, color);
fill_rect(right_rect, color);
fill_rect(bottom_rect, color);
fill_rect(left_rect, color);
fill_rect(inner, color);
if (top_left_radius)
fill_rounded_corner(top_left_corner, top_left_radius, color, CornerOrientation::TopLeft);
if (top_right_radius)
fill_rounded_corner(top_right_corner, top_right_radius, color, CornerOrientation::TopRight);
if (bottom_left_radius)
fill_rounded_corner(bottom_left_corner, bottom_left_radius, color, CornerOrientation::BottomLeft);
if (bottom_right_radius)
fill_rounded_corner(bottom_right_corner, bottom_right_radius, color, CornerOrientation::BottomRight);
}
void Painter::fill_rounded_corner(IntRect const& a_rect, int radius, Color color, CornerOrientation orientation)
{
// Care about clipping
auto translated_a_rect = a_rect.translated(translation());
auto rect = translated_a_rect.intersected(clip_rect());
if (rect.is_empty())
return;
VERIFY(target().rect().contains(rect));
// We got cut on the top!
// FIXME: Also account for clipping on the x-axis
int clip_offset = 0;
if (translated_a_rect.y() < rect.y())
clip_offset = rect.y() - translated_a_rect.y();
radius *= scale();
rect *= scale();
clip_offset *= scale();
ARGB32* dst = target().scanline(rect.top()) + rect.left();
size_t const dst_skip = target().pitch() / sizeof(ARGB32);
IntPoint circle_center;
switch (orientation) {
case CornerOrientation::TopLeft:
circle_center = { radius, radius + 1 };
break;
case CornerOrientation::TopRight:
circle_center = { -1, radius + 1 };
break;
case CornerOrientation::BottomRight:
circle_center = { -1, 0 };
break;
case CornerOrientation::BottomLeft:
circle_center = { radius, 0 };
break;
default:
VERIFY_NOT_REACHED();
}
int radius2 = radius * radius;
auto is_in_circle = [&](int x, int y) {
int distance2 = (circle_center.x() - x) * (circle_center.x() - x) + (circle_center.y() - y) * (circle_center.y() - y);
// To reflect the grid and be compatible with the draw_circle_arc_intersecting algorithm
// add 1/2 to the radius
return distance2 <= (radius2 + radius + 0.25);
};
auto dst_format = target().format();
for (int i = rect.height() - 1; i >= 0; --i) {
for (int j = 0; j < rect.width(); ++j)
if (is_in_circle(j, rect.height() - i + clip_offset))
dst[j] = color_for_format(dst_format, dst[j]).blend(color).value();
dst += dst_skip;
}
}
void Painter::draw_circle_arc_intersecting(IntRect const& a_rect, IntPoint center, int radius, Color color, int thickness)
{
if (thickness <= 0 || radius <= 0)
return;
// Care about clipping
auto translated_a_rect = a_rect.translated(translation());
auto rect = translated_a_rect.intersected(clip_rect());
if (rect.is_empty())
return;
VERIFY(target().rect().contains(rect));
// We got cut on the top!
// FIXME: Also account for clipping on the x-axis
int clip_offset = 0;
if (translated_a_rect.y() < rect.y())
clip_offset = rect.y() - translated_a_rect.y();
if (thickness > radius)
thickness = radius;
int radius2 = radius * radius;
auto is_on_arc = [&](int x, int y) {
int distance2 = (center.x() - x) * (center.x() - x) + (center.y() - y) * (center.y() - y);
// Is within a circle of radius 1/2 around (x,y), so basically within the current pixel.
// Technically this is angle-dependent and should be between 1/2 and sqrt(2)/2, but this works.
return distance2 <= (radius2 + radius + 0.25) && distance2 >= (radius2 - radius + 0.25);
};
ARGB32* dst = target().scanline(rect.top()) + rect.left();
auto dst_format = target().format();
size_t const dst_skip = target().pitch() / sizeof(ARGB32);
for (int i = rect.height() - 1; i >= 0; --i) {
for (int j = 0; j < rect.width(); ++j)
if (is_on_arc(j, rect.height() - i + clip_offset))
dst[j] = color_for_format(dst_format, dst[j]).blend(color).value();
dst += dst_skip;
}
return draw_circle_arc_intersecting(a_rect, center, radius - 1, color, thickness - 1);
}
// The callback will only be called for a quarter of the ellipse, the user is intended to deduce other points.
// As the coordinate space is relative to the center of the rectangle, it's simply (x, y), (x, -y), (-x, y) and (-x, -y).
static void on_each_ellipse_point(IntRect const& rect, Function<void(IntPoint)>&& callback)
{
// Note: This is an implementation of the Midpoint Ellipse Algorithm.
double const a = rect.width() / 2;
double const a_square = a * a;
double const b = rect.height() / 2;
double const b_square = b * b;
int x = 0;
auto y = static_cast<int>(b);
double dx = 2 * b_square * x;
double dy = 2 * a_square * y;
// For region 1:
auto decision_parameter = b_square - a_square * b + .25 * a_square;
while (dx < dy) {
callback({ x, y });
if (decision_parameter >= 0) {
y--;
dy -= 2 * a_square;
decision_parameter -= dy;
}
x++;
dx += 2 * b_square;
decision_parameter += dx + b_square;
}
// For region 2:
decision_parameter = b_square * ((x + 0.5) * (x + 0.5)) + a_square * ((y - 1) * (y - 1)) - a_square * b_square;
while (y >= 0) {
callback({ x, y });
if (decision_parameter <= 0) {
x++;
dx += 2 * b_square;
decision_parameter += dx;
}
y--;
dy -= 2 * a_square;
decision_parameter += a_square - dy;
}
}
void Painter::fill_ellipse(IntRect const& a_rect, Color color)
{
VERIFY(scale() == 1); // FIXME: Add scaling support.
auto rect = a_rect.translated(translation()).intersected(clip_rect());
if (rect.is_empty())
return;
VERIFY(target().rect().contains(rect));
auto const center = a_rect.center();
on_each_ellipse_point(rect, [this, &color, center](IntPoint position) {
IntPoint const directions[4] = { { position.x(), position.y() }, { -position.x(), position.y() }, { position.x(), -position.y() }, { -position.x(), -position.y() } };
draw_line(center + directions[0], center + directions[1], color);
draw_line(center + directions[2], center + directions[3], color);
});
}
void Painter::draw_ellipse_intersecting(IntRect const& rect, Color color, int thickness)
{
VERIFY(scale() == 1); // FIXME: Add scaling support.
if (thickness <= 0)
return;
auto const center = rect.center();
on_each_ellipse_point(rect, [this, &color, thickness, center](IntPoint position) {
IntPoint const directions[4] = { { position.x(), position.y() }, { position.x(), -position.y() }, { -position.x(), position.y() }, { -position.x(), -position.y() } };
for (auto const delta : directions) {
auto const point = center + delta;
draw_line(point, point, color, thickness);
}
});
}
template<typename RectType, typename Callback>
static void for_each_pixel_around_rect_clockwise(RectType const& rect, Callback callback)
{
if (rect.is_empty())
return;
for (auto x = rect.left(); x < rect.right(); ++x)
callback(x, rect.top());
for (auto y = rect.top() + 1; y < rect.bottom(); ++y)
callback(rect.right() - 1, y);
for (auto x = rect.right() - 2; x >= rect.left(); --x)
callback(x, rect.bottom() - 1);
for (auto y = rect.bottom() - 2; y > rect.top(); --y)
callback(rect.left(), y);
}
void Painter::draw_focus_rect(IntRect const& rect, Color color)
{
VERIFY(scale() == 1); // FIXME: Add scaling support.
if (rect.is_empty())
return;
bool state = false;
for_each_pixel_around_rect_clockwise(rect, [&](auto x, auto y) {
if (state)
set_pixel(x, y, color);
state = !state;
});
}
void Painter::draw_rect(IntRect const& a_rect, Color color, bool rough)
{
IntRect rect = a_rect.translated(translation());
auto clipped_rect = rect.intersected(clip_rect());
if (clipped_rect.is_empty())
return;
int min_y = clipped_rect.top();
int max_y = clipped_rect.bottom() - 1;
int scale = this->scale();
if (rect.top() >= clipped_rect.top() && rect.top() < clipped_rect.bottom()) {
int width = rough ? max(0, min(rect.width() - 2, clipped_rect.width())) : clipped_rect.width();
if (width > 0) {
int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
for (int i = 0; i < scale; ++i)
fill_physical_scanline_with_draw_op(rect.top() * scale + i, start_x * scale, width * scale, color);
}
++min_y;
}
if (rect.bottom() > clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) {
int width = rough ? max(0, min(rect.width() - 2, clipped_rect.width())) : clipped_rect.width();
if (width > 0) {
int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x();
for (int i = 0; i < scale; ++i)
fill_physical_scanline_with_draw_op(max_y * scale + i, start_x * scale, width * scale, color);
}
--max_y;
}
bool draw_left_side = rect.left() >= clipped_rect.left();
bool draw_right_side = rect.right() == clipped_rect.right();
if (draw_left_side && draw_right_side) {
// Specialized loop when drawing both sides.
for (int y = min_y * scale; y <= max_y * scale; ++y) {
auto* bits = target().scanline(y);
for (int i = 0; i < scale; ++i)
set_physical_pixel_with_draw_op(bits[rect.left() * scale + i], color);
for (int i = 0; i < scale; ++i)
set_physical_pixel_with_draw_op(bits[(rect.right() - 1) * scale + i], color);
}
} else {
for (int y = min_y * scale; y <= max_y * scale; ++y) {
auto* bits = target().scanline(y);
if (draw_left_side)
for (int i = 0; i < scale; ++i)
set_physical_pixel_with_draw_op(bits[rect.left() * scale + i], color);
if (draw_right_side)
for (int i = 0; i < scale; ++i)
set_physical_pixel_with_draw_op(bits[(rect.right() - 1) * scale + i], color);
}
}
}
void Painter::draw_rect_with_thickness(IntRect const& rect, Color color, int thickness)
{
if (thickness <= 0)
return;
IntPoint p1 = rect.location();
IntPoint p2 = { rect.location().x() + rect.width(), rect.location().y() };
IntPoint p3 = { rect.location().x() + rect.width(), rect.location().y() + rect.height() };
IntPoint p4 = { rect.location().x(), rect.location().y() + rect.height() };
draw_line(p1.translated(thickness, 0), p2.translated(-thickness, 0), color, thickness);
draw_line(p2, p3, color, thickness);
draw_line(p4.translated(thickness, 0), p3.translated(-thickness, 0), color, thickness);
draw_line(p4, p1, color, thickness);
}
void Painter::draw_bitmap(IntPoint p, CharacterBitmap const& bitmap, Color color)
{
VERIFY(scale() == 1); // FIXME: Add scaling support.
auto rect = IntRect(p, bitmap.size()).translated(translation());
auto clipped_rect = rect.intersected(clip_rect());
if (clipped_rect.is_empty())
return;
int const first_row = clipped_rect.top() - rect.top();
int const last_row = clipped_rect.bottom() - rect.top();
int const first_column = clipped_rect.left() - rect.left();
int const last_column = clipped_rect.right() - rect.left();
ARGB32* dst = target().scanline(clipped_rect.y()) + clipped_rect.x();
size_t const dst_skip = target().pitch() / sizeof(ARGB32);
char const* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column];
size_t const bitmap_skip = bitmap.width();
for (int row = first_row; row < last_row; ++row) {
for (int j = 0; j < (last_column - first_column); ++j) {
char fc = bitmap_row[j];
if (fc == '#')
dst[j] = color.value();
}
bitmap_row += bitmap_skip;
dst += dst_skip;
}
}
void Painter::draw_bitmap(IntPoint p, GlyphBitmap const& bitmap, Color color)
{
auto dst_rect = IntRect(p, bitmap.size()).translated(translation());
auto clipped_rect = dst_rect.intersected(clip_rect());
if (clipped_rect.is_empty())
return;
int const first_row = clipped_rect.top() - dst_rect.top();
int const last_row = clipped_rect.bottom() - dst_rect.top();
int const first_column = clipped_rect.left() - dst_rect.left();
int const last_column = clipped_rect.right() - dst_rect.left();
int scale = this->scale();
ARGB32* dst = target().scanline(clipped_rect.y() * scale) + clipped_rect.x() * scale;
auto dst_format = target().format();
size_t const dst_skip = target().pitch() / sizeof(ARGB32);
if (scale == 1) {
for (int row = first_row; row < last_row; ++row) {
for (int j = 0; j < (last_column - first_column); ++j) {
if (bitmap.bit_at(j + first_column, row))
dst[j] = color_for_format(dst_format, dst[j]).blend(color).value();
}
dst += dst_skip;
}
} else {
for (int row = first_row; row < last_row; ++row) {
for (int j = 0; j < (last_column - first_column); ++j) {
if (bitmap.bit_at((j + first_column), row)) {
for (int iy = 0; iy < scale; ++iy)
for (int ix = 0; ix < scale; ++ix) {
auto pixel_index = j * scale + ix + iy * dst_skip;
dst[pixel_index] = color_for_format(dst_format, dst[pixel_index]).blend(color).value();
}
}
}
dst += dst_skip * scale;
}
}
}
void Painter::draw_triangle(IntPoint offset, ReadonlySpan<IntPoint> control_points, Color color)
{
VERIFY(control_points.size() == 3);
draw_triangle(control_points[0] + offset, control_points[1] + offset, control_points[2] + offset, color);
}
void Painter::draw_triangle(IntPoint a, IntPoint b, IntPoint c, Color color)
{
IntPoint p0(to_physical(a));
IntPoint p1(to_physical(b));
IntPoint p2(to_physical(c));
// sort points from top to bottom
if (p0.y() > p1.y())
swap(p0, p1);
if (p0.y() > p2.y())
swap(p0, p2);
if (p1.y() > p2.y())
swap(p1, p2);
// return if top and bottom points are on same line
if (p0.y() == p2.y())
return;
// return if all points are on the same line vertically
if (p0.x() == p1.x() && p1.x() == p2.x())
return;
// return if top is below clip rect or bottom is above clip rect
auto clip = clip_rect();
if (p0.y() >= clip.bottom() - 1)
return;
if (p2.y() < clip.top())
return;
class BoundaryLine {
private:
IntPoint m_base {};
IntPoint m_path {};
public:
BoundaryLine(IntPoint a, IntPoint b)
{
VERIFY(a.y() <= b.y());
m_base = a;
m_path = b - a;
}
int top_y() const { return m_base.y(); }
int bottom_y() const { return m_base.y() + m_path.y(); }
bool is_vertical() const { return m_path.x() == 0; }
bool is_horizontal() const { return m_path.y() == 0; }
bool in_y_range(int y) const { return y >= top_y() && y <= bottom_y(); }
Optional<int> intersection_on_x(int y) const
{
if (!in_y_range(y))
return {};
if (is_horizontal())
return {};
if (is_vertical())
return m_base.x();
int y_diff = y - top_y();
int x_d = m_path.x() * y_diff, y_d = m_path.y();
return (x_d / y_d) + m_base.x();
}
};
BoundaryLine l0(p0, p1), l1(p0, p2), l2(p1, p2);
int rgba = color.value();
for (int y = max(p0.y(), clip.top()); y < min(p2.y() + 1, clip.bottom()); y++) {
Optional<int>
x0 = l0.intersection_on_x(y),
x1 = l1.intersection_on_x(y),
x2 = l2.intersection_on_x(y);
int result_a = 0, result_b = 0;
if (x0.has_value()) {
result_a = x0.value();
if (x1.has_value() && ((!x2.has_value()) || (result_a != x1.value()))) {
result_b = x1.value();
} else {
result_b = x2.value();
}
} else if (x1.has_value()) {
result_a = x1.value();
result_b = x2.value();
}
if (result_a > result_b)
swap(result_a, result_b);
int left_bound = result_a, right_bound = result_b;
ARGB32* scanline = target().scanline(y);
for (int x = max(left_bound, clip.left()); x <= min(right_bound, clip.right() - 1); x++)
scanline[x] = rgba;
}
}
struct BlitState {
enum AlphaState {
NoAlpha = 0,
SrcAlpha = 1,
DstAlpha = 2,
BothAlpha = SrcAlpha | DstAlpha
};
ARGB32 const* src;
ARGB32* dst;
size_t src_pitch;
size_t dst_pitch;
int row_count;
int column_count;
float opacity;
BitmapFormat src_format;
};
// FIXME: This is a hack to support blit_with_opacity() with RGBA8888 source.
// Ideally we'd have a more generic solution that allows any source format.
static void swap_red_and_blue_channels(Color& color)
{
u32 rgba = color.value();
u32 bgra = (rgba & 0xff00ff00)
| ((rgba & 0x000000ff) << 16)
| ((rgba & 0x00ff0000) >> 16);
color = Color::from_argb(bgra);
}
// FIXME: This function is very unoptimized.
template<BlitState::AlphaState has_alpha>
static void do_blit_with_opacity(BlitState& state)
{
for (int row = 0; row < state.row_count; ++row) {
for (int x = 0; x < state.column_count; ++x) {
Color dest_color = (has_alpha & BlitState::DstAlpha) ? Color::from_argb(state.dst[x]) : Color::from_rgb(state.dst[x]);
if constexpr (has_alpha & BlitState::SrcAlpha) {
Color src_color_with_alpha = Color::from_argb(state.src[x]);
if (state.src_format == BitmapFormat::RGBA8888)
swap_red_and_blue_channels(src_color_with_alpha);
float pixel_opacity = src_color_with_alpha.alpha() / 255.0;
src_color_with_alpha.set_alpha(255 * (state.opacity * pixel_opacity));
state.dst[x] = dest_color.blend(src_color_with_alpha).value();
} else {
Color src_color_with_alpha = Color::from_rgb(state.src[x]);
if (state.src_format == BitmapFormat::RGBA8888)
swap_red_and_blue_channels(src_color_with_alpha);
src_color_with_alpha.set_alpha(state.opacity * 255);
state.dst[x] = dest_color.blend(src_color_with_alpha).value();
}
}
state.dst += state.dst_pitch;
state.src += state.src_pitch;
}
}
void Painter::blit_with_opacity(IntPoint position, Gfx::Bitmap const& source, IntRect const& a_src_rect, float opacity, bool apply_alpha)
{
VERIFY(scale() >= source.scale() && "painter doesn't support downsampling scale factors");
if (opacity >= 1.0f && !(source.has_alpha_channel() && apply_alpha))
return blit(position, source, a_src_rect);
IntRect safe_src_rect = IntRect::intersection(a_src_rect, source.rect());
if (scale() != source.scale())
return draw_scaled_bitmap({ position, safe_src_rect.size() }, source, safe_src_rect, opacity);
auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
auto clipped_rect = dst_rect.intersected(clip_rect());
if (clipped_rect.is_empty())
return;
int scale = this->scale();
auto src_rect = a_src_rect * scale;
clipped_rect *= scale;
dst_rect *= scale;
int const first_row = clipped_rect.top() - dst_rect.top();
int const last_row = clipped_rect.bottom() - dst_rect.top();
int const first_column = clipped_rect.left() - dst_rect.left();
int const last_column = clipped_rect.right() - dst_rect.left();
BlitState blit_state {
.src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column,
.dst = target().scanline(clipped_rect.y()) + clipped_rect.x(),
.src_pitch = source.pitch() / sizeof(ARGB32),
.dst_pitch = target().pitch() / sizeof(ARGB32),
.row_count = last_row - first_row,
.column_count = last_column - first_column,
.opacity = opacity,
.src_format = source.format(),
};
if (source.has_alpha_channel() && apply_alpha) {
if (target().has_alpha_channel())
do_blit_with_opacity<BlitState::BothAlpha>(blit_state);
else
do_blit_with_opacity<BlitState::SrcAlpha>(blit_state);
} else {
if (target().has_alpha_channel())
do_blit_with_opacity<BlitState::DstAlpha>(blit_state);
else
do_blit_with_opacity<BlitState::NoAlpha>(blit_state);
}
}
void Painter::blit_filtered(IntPoint position, Gfx::Bitmap const& source, IntRect const& src_rect, Function<Color(Color)> const& filter, bool apply_alpha)
{
VERIFY((source.scale() == 1 || source.scale() == scale()) && "blit_filtered only supports integer upsampling");
IntRect safe_src_rect = src_rect.intersected(source.rect());
auto dst_rect = IntRect(position, safe_src_rect.size()).translated(translation());
auto clipped_rect = dst_rect.intersected(clip_rect());
if (clipped_rect.is_empty())
return;
int scale = this->scale();
clipped_rect *= scale;
dst_rect *= scale;
safe_src_rect *= source.scale();
int const first_row = clipped_rect.top() - dst_rect.top();
int const last_row = clipped_rect.bottom() - dst_rect.top();
int const first_column = clipped_rect.left() - dst_rect.left();
int const last_column = clipped_rect.right() - dst_rect.left();
ARGB32* dst = target().scanline(clipped_rect.y()) + clipped_rect.x();
size_t const dst_skip = target().pitch() / sizeof(ARGB32);
auto dst_format = target().format();
auto src_format = source.format();
int s = scale / source.scale();
if (s == 1) {
ARGB32 const* src = source.scanline(safe_src_rect.top() + first_row) + safe_src_rect.left() + first_column;
size_t const src_skip = source.pitch() / sizeof(ARGB32);
for (int row = first_row; row < last_row; ++row) {
for (int x = 0; x < (last_column - first_column); ++x) {
auto source_color = color_for_format(src_format, src[x]);
if (source_color.alpha() == 0)
continue;
auto filtered_color = filter(source_color);
if (!apply_alpha || filtered_color.alpha() == 0xff)
dst[x] = filtered_color.value();
else
dst[x] = color_for_format(dst_format, dst[x]).blend(filtered_color).value();
}
dst += dst_skip;
src += src_skip;
}
} else {
for (int row = first_row; row < last_row; ++row) {
ARGB32 const* src = source.scanline(safe_src_rect.top() + row / s) + safe_src_rect.left() + first_column / s;
for (int x = 0; x < (last_column - first_column); ++x) {
auto source_color = color_for_format(src_format, src[x / s]);
if (source_color.alpha() == 0)
continue;
auto filtered_color = filter(source_color);
if (!apply_alpha || filtered_color.alpha() == 0xff)
dst[x] = filtered_color.value();
else
dst[x] = color_for_format(dst_format, dst[x]).blend(filtered_color).value();
}
dst += dst_skip;
}
}
}
void Painter::blit_brightened(IntPoint position, Gfx::Bitmap const& source, IntRect const& src_rect)
{
return blit_filtered(position, source, src_rect, [](Color src) {
return src.lightened();
});
}
void Painter::blit_dimmed(IntPoint position, Gfx::Bitmap const& source, IntRect const& src_rect)
{
return blit_filtered(position, source, src_rect, [](Color src) {
return src.to_grayscale().lightened();
});
}
void Painter::draw_tiled_bitmap(IntRect const& a_dst_rect, Gfx::Bitmap const& source)
{
VERIFY((source.scale() == 1 || source.scale() == scale()) && "draw_tiled_bitmap only supports integer upsampling");
auto dst_rect = a_dst_rect.translated(translation());
auto clipped_rect = dst_rect.intersected(clip_rect());
if (clipped_rect.is_empty())
return;
int scale = this->scale();
clipped_rect *= scale;