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
|
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Utf8View.h>
#include <AK/Vector.h>
#include <LibGfx/Color.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>
#include <LibGfx/TextAlignment.h>
#include <LibGfx/TextDirection.h>
#include <LibGfx/TextElision.h>
#include <LibGfx/TextWrapping.h>
namespace Gfx {
class Painter {
public:
static constexpr int LINE_SPACING = 4;
explicit Painter(Gfx::Bitmap&);
~Painter() = default;
enum class LineStyle {
Solid,
Dotted,
Dashed,
};
enum class ScalingMode {
NearestFractional,
NearestNeighbor,
SmoothPixels,
BilinearBlend,
None,
};
void clear_rect(IntRect const&, Color);
void fill_rect(IntRect const&, Color);
void fill_rect_with_dither_pattern(IntRect const&, Color, Color);
void fill_rect_with_checkerboard(IntRect const&, IntSize, Color color_dark, Color color_light);
void fill_rect_with_gradient(Orientation, IntRect const&, Color gradient_start, Color gradient_end);
void fill_rect_with_gradient(IntRect const&, Color gradient_start, Color gradient_end);
void fill_rect_with_rounded_corners(IntRect const&, Color, int radius);
void fill_rect_with_rounded_corners(IntRect const&, Color, int top_left_radius, int top_right_radius, int bottom_right_radius, int bottom_left_radius);
void fill_ellipse(IntRect const&, Color);
void draw_rect(IntRect const&, Color, bool rough = false);
void draw_rect_with_thickness(IntRect const&, Color, int thickness);
void draw_focus_rect(IntRect const&, Color);
void draw_bitmap(IntPoint, CharacterBitmap const&, Color = Color());
void draw_bitmap(IntPoint, GlyphBitmap const&, Color = Color());
void draw_scaled_bitmap(IntRect const& dst_rect, Gfx::Bitmap const&, IntRect const& src_rect, float opacity = 1.0f, ScalingMode = ScalingMode::NearestNeighbor);
void draw_scaled_bitmap(IntRect const& dst_rect, Gfx::Bitmap const&, FloatRect const& src_rect, float opacity = 1.0f, ScalingMode = ScalingMode::NearestNeighbor);
void draw_scaled_bitmap_with_transform(IntRect const& dst_rect, Gfx::Bitmap const&, FloatRect const& src_rect, Gfx::AffineTransform const&, float opacity = 1.0f, ScalingMode = ScalingMode::NearestNeighbor);
void draw_triangle(IntPoint, IntPoint, IntPoint, Color);
void draw_triangle(IntPoint offset, Span<IntPoint const>, Color);
void draw_ellipse_intersecting(IntRect const&, Color, int thickness = 1);
void set_pixel(IntPoint, Color, bool blend = false);
void set_pixel(int x, int y, Color color, bool blend = false) { set_pixel({ x, y }, color, blend); }
Optional<Color> get_pixel(IntPoint);
ErrorOr<NonnullRefPtr<Bitmap>> get_region_bitmap(IntRect const&, BitmapFormat format, Optional<IntRect&> actual_region = {});
void draw_line(IntPoint, IntPoint, Color, int thickness = 1, LineStyle style = LineStyle::Solid, Color alternate_color = Color::Transparent);
void draw_triangle_wave(IntPoint, IntPoint, Color color, int amplitude, int thickness = 1);
void draw_quadratic_bezier_curve(IntPoint control_point, IntPoint, IntPoint, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
void draw_cubic_bezier_curve(IntPoint control_point_0, IntPoint control_point_1, IntPoint, IntPoint, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
void draw_elliptical_arc(IntPoint p1, IntPoint p2, IntPoint center, FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
void blit(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect, float opacity = 1.0f, bool apply_alpha = true);
void blit_dimmed(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect);
void blit_brightened(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect);
void blit_filtered(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect, Function<Color(Color)>);
void draw_tiled_bitmap(IntRect const& dst_rect, Gfx::Bitmap const&);
void blit_offset(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect, IntPoint);
void blit_disabled(IntPoint, Gfx::Bitmap const&, IntRect const&, Palette const&);
void blit_tiled(IntRect const&, Gfx::Bitmap const&, IntRect const& src_rect);
void draw_text(IntRect const&, StringView, Font const&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
void draw_text(IntRect const&, StringView, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
void draw_text(IntRect const&, Utf32View const&, Font const&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
void draw_text(IntRect const&, Utf32View const&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
void draw_text(Function<void(IntRect const&, Utf8CodePointIterator&)>, IntRect const&, StringView, Font const&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
void draw_text(Function<void(IntRect const&, Utf8CodePointIterator&)>, IntRect const&, Utf8View const&, Font const&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
void draw_text(Function<void(IntRect const&, Utf8CodePointIterator&)>, IntRect const&, Utf32View const&, Font const&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None, TextWrapping = TextWrapping::DontWrap);
void draw_ui_text(Gfx::IntRect const&, StringView, Gfx::Font const&, TextAlignment, Gfx::Color);
void draw_glyph(IntPoint, u32, Color);
void draw_glyph(IntPoint, u32, Font const&, Color);
void draw_emoji(IntPoint, Gfx::Bitmap const&, Font const&);
void draw_glyph_or_emoji(IntPoint, u32, Font const&, Color);
void draw_glyph_or_emoji(IntPoint, Utf8CodePointIterator&, Font const&, Color);
void draw_circle_arc_intersecting(IntRect const&, IntPoint, int radius, Color, int thickness);
// Streamlined text drawing routine that does no wrapping/elision/alignment.
void draw_text_run(IntPoint baseline_start, Utf8View const&, Font const&, Color);
void draw_text_run(FloatPoint baseline_start, Utf8View const&, Font const&, Color);
enum class CornerOrientation {
TopLeft,
TopRight,
BottomRight,
BottomLeft
};
void fill_rounded_corner(IntRect const&, int radius, Color, CornerOrientation);
static void for_each_line_segment_on_bezier_curve(FloatPoint control_point, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&);
static void for_each_line_segment_on_bezier_curve(FloatPoint control_point, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&&);
static void for_each_line_segment_on_cubic_bezier_curve(FloatPoint control_point_0, FloatPoint control_point_1, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&);
static void for_each_line_segment_on_cubic_bezier_curve(FloatPoint control_point_0, FloatPoint control_point_1, FloatPoint p1, FloatPoint p2, Function<void(FloatPoint, FloatPoint)>&&);
static void for_each_line_segment_on_elliptical_arc(FloatPoint p1, FloatPoint p2, FloatPoint center, FloatPoint const radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(FloatPoint, FloatPoint)>&);
static void for_each_line_segment_on_elliptical_arc(FloatPoint p1, FloatPoint p2, FloatPoint center, FloatPoint const radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(FloatPoint, FloatPoint)>&&);
void stroke_path(Path const&, Color, int thickness);
enum class WindingRule {
Nonzero,
EvenOdd,
};
void fill_path(Path const&, Color, WindingRule rule = WindingRule::Nonzero);
Font const& font() const
{
if (!state().font)
return FontDatabase::default_font();
return *state().font;
}
void set_font(Font const& font) { state().font = &font; }
enum class DrawOp {
Copy,
Xor,
Invert
};
void set_draw_op(DrawOp op) { state().draw_op = op; }
DrawOp draw_op() const { return state().draw_op; }
void add_clip_rect(IntRect const& rect);
void clear_clip_rect();
void translate(int dx, int dy) { translate({ dx, dy }); }
void translate(IntPoint delta) { state().translation.translate_by(delta); }
IntPoint translation() const { return state().translation; }
Gfx::Bitmap* target() { return m_target.ptr(); }
void save() { m_state_stack.append(m_state_stack.last()); }
void restore()
{
VERIFY(m_state_stack.size() > 1);
m_state_stack.take_last();
}
IntRect clip_rect() const { return state().clip_rect; }
int scale() const { return state().scale; }
template<typename TGetPixelCallback>
void fill_pixels(Gfx::IntRect const& region, TGetPixelCallback callback, bool blend = false)
{
// Note: This function paints physical pixels and therefore does not make sense when
// called on a scaled painter. Scaling the region painted may break the caller (this
// would be the case for gradients in LibWeb), and if you scale the pixels (to squares)
// then this is no longer filling pixels.
VERIFY(scale() == 1);
auto paint_region = region.translated(translation());
auto clipped_region = paint_region.intersected(clip_rect());
if (clipped_region.is_empty())
return;
auto start_offset = clipped_region.location() - paint_region.location();
for (int y = 0; y < clipped_region.height(); y++) {
for (int x = 0; x < clipped_region.width(); x++) {
auto pixel = callback(IntPoint(x, y).translated(start_offset));
set_physical_pixel(clipped_region.location().translated(x, y), pixel, blend);
}
}
}
protected:
IntRect to_physical(IntRect const& r) const { return r.translated(translation()) * scale(); }
IntPoint to_physical(IntPoint p) const { return p.translated(translation()) * scale(); }
void set_physical_pixel_with_draw_op(u32& pixel, Color);
void fill_physical_scanline_with_draw_op(int y, int x, int width, Color color);
void fill_rect_with_draw_op(IntRect const&, Color);
void blit_with_opacity(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect, float opacity, bool apply_alpha = true);
void draw_physical_pixel(IntPoint, Color, int thickness = 1);
void set_physical_pixel(IntPoint, Color color, bool blend);
struct State {
Font const* font;
IntPoint translation;
int scale = 1;
IntRect clip_rect;
DrawOp draw_op;
};
State& state() { return m_state_stack.last(); }
State const& state() const { return m_state_stack.last(); }
void fill_physical_rect(IntRect const&, Color);
IntRect m_clip_origin;
NonnullRefPtr<Gfx::Bitmap> m_target;
Vector<State, 4> m_state_stack;
private:
Vector<DirectionalRun> split_text_into_directional_runs(Utf8View const&, TextDirection initial_direction);
bool text_contains_bidirectional_text(Utf8View const&, TextDirection);
template<typename DrawGlyphFunction>
void do_draw_text(IntRect const&, Utf8View const& text, Font const&, TextAlignment, TextElision, TextWrapping, DrawGlyphFunction);
};
class PainterStateSaver {
public:
explicit PainterStateSaver(Painter&);
~PainterStateSaver();
private:
Painter& m_painter;
};
DeprecatedString parse_ampersand_string(StringView, Optional<size_t>* underline_offset = nullptr);
}
|