/src/serenity/Userland/Libraries/LibGfx/VectorGraphic.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2023, MacDue <macdue@dueutil.tech> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibGfx/Painter.h> |
8 | | #include <LibGfx/VectorGraphic.h> |
9 | | |
10 | | namespace Gfx { |
11 | | |
12 | | void VectorGraphic::draw_into(Painter& painter, IntRect const& dest, AffineTransform transform) const |
13 | 730 | { |
14 | | // Apply the transform then center within destination rectangle (this ignores any translation from the transform): |
15 | | // This allows you to easily rotate or flip the image before painting. |
16 | 730 | auto transformed_rect = transform.map(FloatRect { {}, size() }); |
17 | 730 | auto scale = min(float(dest.width()) / transformed_rect.width(), float(dest.height()) / transformed_rect.height()); |
18 | 730 | auto centered = FloatRect { {}, transformed_rect.size().scaled(scale) }.centered_within(dest.to_type<float>()); |
19 | 730 | auto view_transform = AffineTransform {} |
20 | 730 | .translate(centered.location()) |
21 | 730 | .multiply(AffineTransform {}.scale(scale, scale)) |
22 | 730 | .multiply(AffineTransform {}.translate(-transformed_rect.location())) |
23 | 730 | .multiply(transform); |
24 | 730 | return draw_transformed(painter, view_transform); |
25 | 730 | } |
26 | | |
27 | | ErrorOr<NonnullRefPtr<Gfx::Bitmap>> VectorGraphic::bitmap(IntSize size, AffineTransform transform) const |
28 | 730 | { |
29 | 730 | auto bitmap = TRY(Bitmap::create(Gfx::BitmapFormat::BGRA8888, size)); |
30 | 0 | Painter painter { *bitmap }; |
31 | 730 | draw_into(painter, IntRect { {}, size }, transform); |
32 | 730 | return bitmap; |
33 | 730 | } |
34 | | |
35 | | } |