After 2D canvas stops using Gfx::Painter as its drawing backend, the base Painter interface only needs to describe bitmap compositing work that still happens outside PainterSkia. Narrow the interface to clear_rect, fill_rect and draw_bitmap, which covers GIF/APNG frame compositing, CSS cursor bitmap painting, canvas readback and the Android UI blit.
35 lines
1.4 KiB
C++
35 lines
1.4 KiB
C++
/*
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibGfx/Bitmap.h>
|
|
#include <LibGfx/PainterSkia.h>
|
|
#include <LibGfx/VectorGraphic.h>
|
|
|
|
namespace Gfx {
|
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> VectorGraphic::bitmap(IntSize size, AffineTransform transform) const
|
|
{
|
|
auto bitmap = TRY(Bitmap::create(Gfx::BitmapFormat::BGRA8888, size));
|
|
auto painter = make<PainterSkia>(PaintingSurface::wrap_bitmap(bitmap));
|
|
|
|
// 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 { {}, this->size() });
|
|
auto scale = min(float(size.width()) / transformed_rect.width(), float(size.height()) / transformed_rect.height());
|
|
auto centered = FloatRect { {}, transformed_rect.size().scaled(scale) }.centered_within(IntRect { {}, size }.to_type<float>());
|
|
auto view_transform = AffineTransform {}
|
|
.translate(centered.location())
|
|
.multiply(AffineTransform {}.scale(scale, scale))
|
|
.multiply(AffineTransform {}.translate(-transformed_rect.location()))
|
|
.multiply(transform);
|
|
|
|
painter->set_transform(view_transform);
|
|
draw(*painter);
|
|
|
|
return bitmap;
|
|
}
|
|
|
|
}
|