ladybird/Userland/Libraries/LibGfx/VectorGraphic.cpp
Timothy Flynn 7c4b0b0389 LibGfx+Userland: Rename Size::scaled_by to Size::scaled
Ignoring Size for a second, we currently have:

    Rect::scale_by
    Rect::scaled

    Point::scale_by
    Point::scaled

In Size, before this patch, we have:

    Size::scale_by
    Size::scaled_by

This aligns Size to use the same method name as Rect and Point. While
subjectively providing API symmetry, this is mostly to allow using this
method in templated helpers without caring what the exact underlying
type is.
2023-08-17 09:57:30 -04:00

36 lines
1.4 KiB
C++

/*
* 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(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;
}
}