/* * 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(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); draw_transformed(painter, view_transform); } ErrorOr> VectorGraphic::bitmap(IntSize size, AffineTransform transform) const { auto bitmap = TRY(Bitmap::create(Gfx::BitmapFormat::BGRA8888, size)); auto painter = PainterSkia::create(bitmap); draw_into(*painter, IntRect { {}, size }, transform); return bitmap; } }