mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
LibGfx: Add simple VectorGraphic abstract base class
This is currently very bare-bones with just the size and a few methods to rasterize/draw the graphic.
This commit is contained in:
parent
aaf1b762ea
commit
16b487c270
Notes:
sideshowbarker
2024-07-17 03:16:02 +09:00
Author: https://github.com/MacDue Commit: https://github.com/SerenityOS/serenity/commit/16b487c270 Pull-request: https://github.com/SerenityOS/serenity/pull/19870 Reviewed-by: https://github.com/LucasChollet
3 changed files with 66 additions and 0 deletions
|
@ -64,6 +64,7 @@ set(SOURCES
|
|||
TextDirection.cpp
|
||||
TextLayout.cpp
|
||||
Triangle.cpp
|
||||
VectorGraphic.cpp
|
||||
WindowTheme.cpp
|
||||
)
|
||||
|
||||
|
|
35
Userland/Libraries/LibGfx/VectorGraphic.cpp
Normal file
35
Userland/Libraries/LibGfx/VectorGraphic.cpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGfx/Painter.h>
|
||||
#include <LibGfx/VectorGraphic.h>
|
||||
|
||||
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<float>());
|
||||
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<NonnullRefPtr<Gfx::Bitmap>> 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;
|
||||
}
|
||||
|
||||
}
|
30
Userland/Libraries/LibGfx/VectorGraphic.h
Normal file
30
Userland/Libraries/LibGfx/VectorGraphic.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/RefCounted.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/Forward.h>
|
||||
#include <LibGfx/Size.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
class VectorGraphic : public RefCounted<VectorGraphic> {
|
||||
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<NonnullRefPtr<Gfx::Bitmap>> bitmap(IntSize size, AffineTransform = {}) const;
|
||||
void draw_into(Painter&, IntRect const& dest, AffineTransform = {}) const;
|
||||
|
||||
virtual ~VectorGraphic() = default;
|
||||
};
|
||||
|
||||
};
|
Loading…
Add table
Reference in a new issue