VectorGraphic.cpp 1.4 KB

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