2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-04-09 11:09:48 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2018-10-10 16:49:36 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-14 23:28:42 +01:00
|
|
|
#include <AK/Forward.h>
|
2020-02-15 00:58:14 +01:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2022-02-22 21:55:01 +00:00
|
|
|
#include <AK/Utf8View.h>
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibGfx/Color.h>
|
2022-04-09 09:28:38 +02:00
|
|
|
#include <LibGfx/Font/FontDatabase.h>
|
2020-02-14 23:28:42 +01:00
|
|
|
#include <LibGfx/Forward.h>
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <LibGfx/Point.h>
|
|
|
|
#include <LibGfx/Rect.h>
|
|
|
|
#include <LibGfx/Size.h>
|
2020-02-06 12:04:00 +01:00
|
|
|
#include <LibGfx/TextAlignment.h>
|
2021-07-25 21:20:11 +00:00
|
|
|
#include <LibGfx/TextDirection.h>
|
2020-02-06 12:04:00 +01:00
|
|
|
#include <LibGfx/TextElision.h>
|
2021-07-25 21:20:11 +00:00
|
|
|
#include <LibGfx/TextWrapping.h>
|
2018-10-10 16:49:36 +02:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
namespace Gfx {
|
|
|
|
|
2018-10-10 16:49:36 +02:00
|
|
|
class Painter {
|
|
|
|
public:
|
2021-01-19 12:10:47 -05:00
|
|
|
explicit Painter(Gfx::Bitmap&);
|
2022-03-14 13:26:37 -06:00
|
|
|
~Painter() = default;
|
2020-05-10 11:58:33 +01:00
|
|
|
|
|
|
|
enum class LineStyle {
|
|
|
|
Solid,
|
|
|
|
Dotted,
|
2020-05-10 16:17:26 +01:00
|
|
|
Dashed,
|
2020-05-10 11:58:33 +01:00
|
|
|
};
|
|
|
|
|
2021-09-20 19:48:56 +01:00
|
|
|
enum class ScalingMode {
|
|
|
|
NearestNeighbor,
|
2022-06-03 17:16:38 +02:00
|
|
|
SmoothPixels,
|
2021-09-20 19:48:56 +01:00
|
|
|
BilinearBlend,
|
|
|
|
};
|
|
|
|
|
2021-09-17 17:36:28 +04:30
|
|
|
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 const&, 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 const&, CharacterBitmap const&, Color = Color());
|
|
|
|
void draw_bitmap(IntPoint const&, GlyphBitmap const&, Color = Color());
|
2021-09-20 19:48:56 +01:00
|
|
|
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);
|
2022-09-24 13:00:53 +02:00
|
|
|
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);
|
2021-09-17 17:36:28 +04:30
|
|
|
void draw_triangle(IntPoint const&, IntPoint const&, IntPoint const&, Color);
|
2022-06-13 20:03:09 +02:00
|
|
|
void draw_triangle(IntPoint const& offset, Span<IntPoint const>, Color);
|
2021-09-17 17:36:28 +04:30
|
|
|
void draw_ellipse_intersecting(IntRect const&, Color, int thickness = 1);
|
2022-03-10 02:21:02 +00:00
|
|
|
void set_pixel(IntPoint const&, Color, bool blend = false);
|
|
|
|
void set_pixel(int x, int y, Color color, bool blend = false) { set_pixel({ x, y }, color, blend); }
|
2022-06-15 21:05:58 +01:00
|
|
|
Optional<Color> get_pixel(IntPoint const&);
|
2022-09-15 08:31:29 +01:00
|
|
|
ErrorOr<NonnullRefPtr<Bitmap>> get_region_bitmap(IntRect const&, BitmapFormat format, Optional<IntRect&> actual_region = {});
|
2021-09-17 17:36:28 +04:30
|
|
|
void draw_line(IntPoint const&, IntPoint const&, Color, int thickness = 1, LineStyle style = LineStyle::Solid, Color alternate_color = Color::Transparent);
|
2022-01-20 19:55:14 +01:00
|
|
|
void draw_triangle_wave(IntPoint const&, IntPoint const&, Color color, int amplitude, int thickness = 1);
|
2021-09-17 17:36:28 +04:30
|
|
|
void draw_quadratic_bezier_curve(IntPoint const& control_point, IntPoint const&, IntPoint const&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
|
2021-09-17 18:42:30 +04:30
|
|
|
void draw_cubic_bezier_curve(IntPoint const& control_point_0, IntPoint const& control_point_1, IntPoint const&, IntPoint const&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
|
2021-09-17 17:36:28 +04:30
|
|
|
void draw_elliptical_arc(IntPoint const& p1, IntPoint const& p2, IntPoint const& center, FloatPoint const& radii, float x_axis_rotation, float theta_1, float theta_delta, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
|
|
|
|
void blit(IntPoint const&, Gfx::Bitmap const&, IntRect const& src_rect, float opacity = 1.0f, bool apply_alpha = true);
|
|
|
|
void blit_dimmed(IntPoint const&, Gfx::Bitmap const&, IntRect const& src_rect);
|
|
|
|
void blit_brightened(IntPoint const&, Gfx::Bitmap const&, IntRect const& src_rect);
|
|
|
|
void blit_filtered(IntPoint const&, 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 const&, Gfx::Bitmap const&, IntRect const& src_rect, IntPoint const&);
|
|
|
|
void blit_disabled(IntPoint const&, Gfx::Bitmap const&, IntRect const&, Palette const&);
|
|
|
|
void blit_tiled(IntRect const&, Gfx::Bitmap const&, IntRect const& src_rect);
|
2021-11-11 00:55:02 +01:00
|
|
|
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);
|
2021-09-17 17:36:28 +04:30
|
|
|
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);
|
2022-02-22 21:55:01 +00:00
|
|
|
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);
|
2021-11-11 00:55:02 +01:00
|
|
|
void draw_ui_text(Gfx::IntRect const&, StringView, Gfx::Font const&, TextAlignment, Gfx::Color);
|
2021-09-17 17:36:28 +04:30
|
|
|
void draw_glyph(IntPoint const&, u32, Color);
|
|
|
|
void draw_glyph(IntPoint const&, u32, Font const&, Color);
|
|
|
|
void draw_emoji(IntPoint const&, Gfx::Bitmap const&, Font const&);
|
2022-02-22 21:55:01 +00:00
|
|
|
void draw_glyph_or_emoji(IntPoint const&, u32, Font const&, Color);
|
|
|
|
void draw_glyph_or_emoji(IntPoint const&, Utf8CodePointIterator&, Font const&, Color);
|
2021-09-17 17:36:28 +04:30
|
|
|
void draw_circle_arc_intersecting(IntRect const&, IntPoint const&, int radius, Color, int thickness);
|
2019-01-12 17:02:54 +01:00
|
|
|
|
2022-03-28 13:40:48 +02:00
|
|
|
// Streamlined text drawing routine that does no wrapping/elision/alignment.
|
|
|
|
void draw_text_run(FloatPoint const& baseline_start, Utf8View const&, Font const&, Color);
|
|
|
|
|
2021-05-15 23:33:21 +02:00
|
|
|
enum class CornerOrientation {
|
|
|
|
TopLeft,
|
|
|
|
TopRight,
|
|
|
|
BottomRight,
|
|
|
|
BottomLeft
|
|
|
|
};
|
2021-09-17 17:36:28 +04:30
|
|
|
void fill_rounded_corner(IntRect const&, int radius, Color, CornerOrientation);
|
2021-05-15 23:33:21 +02:00
|
|
|
|
2021-09-17 17:36:28 +04:30
|
|
|
static void for_each_line_segment_on_bezier_curve(FloatPoint const& control_point, FloatPoint const& p1, FloatPoint const& p2, Function<void(FloatPoint const&, FloatPoint const&)>&);
|
|
|
|
static void for_each_line_segment_on_bezier_curve(FloatPoint const& control_point, FloatPoint const& p1, FloatPoint const& p2, Function<void(FloatPoint const&, FloatPoint const&)>&&);
|
2020-05-06 11:55:12 +04:30
|
|
|
|
2021-09-17 18:42:30 +04:30
|
|
|
static void for_each_line_segment_on_cubic_bezier_curve(FloatPoint const& control_point_0, FloatPoint const& control_point_1, FloatPoint const& p1, FloatPoint const& p2, Function<void(FloatPoint const&, FloatPoint const&)>&);
|
|
|
|
static void for_each_line_segment_on_cubic_bezier_curve(FloatPoint const& control_point_0, FloatPoint const& control_point_1, FloatPoint const& p1, FloatPoint const& p2, Function<void(FloatPoint const&, FloatPoint const&)>&&);
|
|
|
|
|
2021-09-17 17:36:28 +04:30
|
|
|
static void for_each_line_segment_on_elliptical_arc(FloatPoint const& p1, FloatPoint const& p2, FloatPoint const& center, FloatPoint const radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(FloatPoint const&, FloatPoint const&)>&);
|
|
|
|
static void for_each_line_segment_on_elliptical_arc(FloatPoint const& p1, FloatPoint const& p2, FloatPoint const& center, FloatPoint const radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(FloatPoint const&, FloatPoint const&)>&&);
|
2020-07-21 23:46:15 -07:00
|
|
|
|
2021-09-17 17:36:28 +04:30
|
|
|
void stroke_path(Path const&, Color, int thickness);
|
2020-04-16 21:03:17 +02:00
|
|
|
|
2020-05-06 11:55:12 +04:30
|
|
|
enum class WindingRule {
|
|
|
|
Nonzero,
|
|
|
|
EvenOdd,
|
|
|
|
};
|
2021-09-17 13:41:03 +02:00
|
|
|
void fill_path(Path const&, Color, WindingRule rule = WindingRule::Nonzero);
|
2020-05-06 11:55:12 +04:30
|
|
|
|
2021-11-28 16:09:38 +01:00
|
|
|
Font const& font() const
|
|
|
|
{
|
|
|
|
if (!state().font)
|
|
|
|
return FontDatabase::default_font();
|
|
|
|
return *state().font;
|
|
|
|
}
|
2021-09-17 17:36:28 +04:30
|
|
|
void set_font(Font const& font) { state().font = &font; }
|
2018-10-11 23:45:57 +02:00
|
|
|
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class DrawOp {
|
2019-06-05 09:22:11 -07:00
|
|
|
Copy,
|
2020-09-07 22:25:30 -06:00
|
|
|
Xor,
|
|
|
|
Invert
|
2019-06-05 09:22:11 -07:00
|
|
|
};
|
2019-03-09 16:48:02 +01:00
|
|
|
void set_draw_op(DrawOp op) { state().draw_op = op; }
|
|
|
|
DrawOp draw_op() const { return state().draw_op; }
|
2019-01-11 03:52:09 +01:00
|
|
|
|
2021-09-17 17:36:28 +04:30
|
|
|
void add_clip_rect(IntRect const& rect);
|
2019-01-24 23:40:12 +01:00
|
|
|
void clear_clip_rect();
|
2019-02-04 15:37:23 +01:00
|
|
|
|
2021-05-03 16:37:05 +02:00
|
|
|
void translate(int dx, int dy) { translate({ dx, dy }); }
|
2021-09-17 17:36:28 +04:30
|
|
|
void translate(IntPoint const& delta) { state().translation.translate_by(delta); }
|
2019-03-07 13:13:25 +01:00
|
|
|
|
2022-10-01 14:04:05 +02:00
|
|
|
IntPoint translation() const { return state().translation; }
|
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Bitmap* target() { return m_target.ptr(); }
|
2019-02-10 14:28:39 +01:00
|
|
|
|
2019-03-09 16:48:02 +01:00
|
|
|
void save() { m_state_stack.append(m_state_stack.last()); }
|
2019-06-05 09:22:11 -07:00
|
|
|
void restore()
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_state_stack.size() > 1);
|
2019-06-05 09:22:11 -07:00
|
|
|
m_state_stack.take_last();
|
|
|
|
}
|
2019-03-09 16:48:02 +01:00
|
|
|
|
2021-02-15 19:30:47 -07:00
|
|
|
IntRect clip_rect() const { return state().clip_rect; }
|
|
|
|
|
2022-03-19 00:29:22 +00:00
|
|
|
int scale() const { return state().scale; }
|
|
|
|
|
2019-03-28 17:19:56 +01:00
|
|
|
protected:
|
2021-09-17 17:36:28 +04:30
|
|
|
IntRect to_physical(IntRect const& r) const { return r.translated(translation()) * scale(); }
|
|
|
|
IntPoint to_physical(IntPoint const& p) const { return p.translated(translation()) * scale(); }
|
|
|
|
void set_physical_pixel_with_draw_op(u32& pixel, Color const&);
|
|
|
|
void fill_physical_scanline_with_draw_op(int y, int x, int width, Color const& color);
|
|
|
|
void fill_rect_with_draw_op(IntRect const&, Color);
|
|
|
|
void blit_with_opacity(IntPoint const&, Gfx::Bitmap const&, IntRect const& src_rect, float opacity, bool apply_alpha = true);
|
|
|
|
void draw_physical_pixel(IntPoint const&, Color, int thickness = 1);
|
2019-01-11 03:52:09 +01:00
|
|
|
|
2019-03-09 16:48:02 +01:00
|
|
|
struct State {
|
2021-09-17 17:36:28 +04:30
|
|
|
Font const* font;
|
2021-05-03 16:37:05 +02:00
|
|
|
IntPoint translation;
|
|
|
|
int scale = 1;
|
2021-01-20 09:59:22 -05:00
|
|
|
IntRect clip_rect;
|
2019-03-09 16:48:02 +01:00
|
|
|
DrawOp draw_op;
|
|
|
|
};
|
|
|
|
|
|
|
|
State& state() { return m_state_stack.last(); }
|
2021-09-17 17:36:28 +04:30
|
|
|
State const& state() const { return m_state_stack.last(); }
|
2019-03-09 16:48:02 +01:00
|
|
|
|
2021-09-17 17:36:28 +04:30
|
|
|
void fill_physical_rect(IntRect const&, Color);
|
2021-05-03 16:37:05 +02:00
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
IntRect m_clip_origin;
|
2020-02-06 11:56:38 +01:00
|
|
|
NonnullRefPtr<Gfx::Bitmap> m_target;
|
2019-04-20 14:02:19 +02:00
|
|
|
Vector<State, 4> m_state_stack;
|
2021-07-25 21:20:11 +00:00
|
|
|
|
|
|
|
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);
|
2018-10-10 16:49:36 +02:00
|
|
|
};
|
2019-03-09 16:54:41 +01:00
|
|
|
|
|
|
|
class PainterStateSaver {
|
|
|
|
public:
|
2019-04-16 01:01:03 +02:00
|
|
|
explicit PainterStateSaver(Painter&);
|
|
|
|
~PainterStateSaver();
|
2019-03-09 16:54:41 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Painter& m_painter;
|
|
|
|
};
|
2020-02-06 11:56:38 +01:00
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
String parse_ampersand_string(StringView, Optional<size_t>* underline_offset = nullptr);
|
2021-04-11 01:09:44 +02:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
}
|