diff --git a/Userland/Libraries/LibGfx/CMakeLists.txt b/Userland/Libraries/LibGfx/CMakeLists.txt index 31370ff69e..b76d3eee7f 100644 --- a/Userland/Libraries/LibGfx/CMakeLists.txt +++ b/Userland/Libraries/LibGfx/CMakeLists.txt @@ -64,6 +64,7 @@ set(SOURCES TextDirection.cpp TextLayout.cpp Triangle.cpp + VectorGraphic.cpp WindowTheme.cpp ) diff --git a/Userland/Libraries/LibGfx/VectorGraphic.cpp b/Userland/Libraries/LibGfx/VectorGraphic.cpp new file mode 100644 index 0000000000..4f94463c23 --- /dev/null +++ b/Userland/Libraries/LibGfx/VectorGraphic.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023, MacDue + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace Gfx { + +void VectorGraphic::draw_into(Painter& painter, IntRect const& dest, AffineTransform transform) const +{ + // Apply the transform then center within destination rectangle (this ignores any translation from the transform): + // This allows you to easily rotate or flip the image before painting. + auto transformed_rect = transform.map(FloatRect { {}, size() }); + auto scale = min(float(dest.width()) / transformed_rect.width(), float(dest.height()) / transformed_rect.height()); + auto centered = FloatRect { {}, transformed_rect.size().scaled_by(scale) }.centered_within(dest.to_type()); + auto view_transform = AffineTransform {} + .translate(centered.location()) + .multiply(AffineTransform {}.scale(scale, scale)) + .multiply(AffineTransform {}.translate(-transformed_rect.location())) + .multiply(transform); + return draw_transformed(painter, view_transform); +} + +ErrorOr> VectorGraphic::bitmap(IntSize size, AffineTransform transform) const +{ + auto bitmap = TRY(Bitmap::create(Gfx::BitmapFormat::BGRA8888, size)); + Painter painter { *bitmap }; + draw_into(painter, IntRect { {}, size }, transform); + return bitmap; +} + +} diff --git a/Userland/Libraries/LibGfx/VectorGraphic.h b/Userland/Libraries/LibGfx/VectorGraphic.h new file mode 100644 index 0000000000..0b45f16448 --- /dev/null +++ b/Userland/Libraries/LibGfx/VectorGraphic.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023, MacDue + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Gfx { + +class VectorGraphic : public RefCounted { +public: + virtual IntSize intrinsic_size() const = 0; + virtual void draw_transformed(Painter&, AffineTransform) const = 0; + + IntSize size() const { return intrinsic_size(); } + IntRect rect() const { return { {}, size() }; } + + ErrorOr> bitmap(IntSize size, AffineTransform = {}) const; + void draw_into(Painter&, IntRect const& dest, AffineTransform = {}) const; + + virtual ~VectorGraphic() = default; +}; + +};