Skip to content

Commit

Permalink
LibGfx: Let Painter care about TinyVG transforms
Browse files Browse the repository at this point in the history
This helps us with non-uniform scales, and makes things simple
  • Loading branch information
shlyakpavel authored and gmta committed Dec 14, 2024
1 parent 0b1c7d6 commit ea469fb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 31 deletions.
16 changes: 6 additions & 10 deletions Libraries/LibGfx/ImageFormats/TinyVGLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,29 +471,25 @@ ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(St
return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) TinyVGDecodedImageData({ header.width, header.height }, move(draw_commands))));
}

void TinyVGDecodedImageData::draw_transformed(Painter& painter, AffineTransform transform) const
void TinyVGDecodedImageData::draw(Painter& painter) const
{
// FIXME: Correctly handle non-uniform scales.
auto scale = max(transform.x_scale(), transform.y_scale());
for (auto const& command : draw_commands()) {
auto draw_path = command.path.copy_transformed(transform);
auto draw_path = command.path;
if (command.fill.has_value()) {
auto fill_path = draw_path;
fill_path.close_all_subpaths();
command.fill->visit(
[&](Color color) { painter.fill_path(fill_path, color, WindingRule::EvenOdd); },
[&](NonnullRefPtr<SVGGradientPaintStyle> style) {
const_cast<SVGGradientPaintStyle&>(*style).set_gradient_transform(transform);
[&](NonnullRefPtr<SVGGradientPaintStyle> const& style) {
painter.fill_path(fill_path, style, 1.0f, WindingRule::EvenOdd);
});
}

if (command.stroke.has_value()) {
command.stroke->visit(
[&](Color color) { painter.stroke_path(draw_path, color, command.stroke_width * scale); },
[&](NonnullRefPtr<SVGGradientPaintStyle> style) {
const_cast<SVGGradientPaintStyle&>(*style).set_gradient_transform(transform);
painter.stroke_path(draw_path, style, command.stroke_width * scale, 1.0f);
[&](Color color) { painter.stroke_path(draw_path, color, command.stroke_width); },
[&](NonnullRefPtr<SVGGradientPaintStyle> const& style) {
painter.stroke_path(draw_path, style, command.stroke_width, 1.0f);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibGfx/ImageFormats/TinyVGLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TinyVGDecodedImageData final : public VectorGraphic {
return m_size;
}

virtual void draw_transformed(Painter&, AffineTransform) const override;
virtual void draw(Painter&) const override;

ReadonlySpan<DrawCommand> draw_commands() const
{
Expand Down
21 changes: 10 additions & 11 deletions Libraries/LibGfx/VectorGraphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@

namespace Gfx {

void VectorGraphic::draw_into(Painter& painter, IntRect const& dest, AffineTransform transform) const
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> VectorGraphic::bitmap(IntSize size, AffineTransform transform) const
{
auto bitmap = TRY(Bitmap::create(Gfx::BitmapFormat::BGRA8888, size));
auto painter = PainterSkia::create(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 { {}, 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 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);
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));
auto painter = PainterSkia::create(bitmap);
draw_into(*painter, IntRect { {}, size }, transform);
painter->set_transform(view_transform);
draw(*painter);

return bitmap;
}

Expand Down
3 changes: 1 addition & 2 deletions Libraries/LibGfx/VectorGraphic.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ namespace Gfx {
class VectorGraphic : public RefCounted<VectorGraphic> {
public:
virtual IntSize intrinsic_size() const = 0;
virtual void draw_transformed(Painter&, AffineTransform) const = 0;
virtual void draw(Painter&) const = 0;

IntSize size() const { return intrinsic_size(); }
IntRect rect() const { return { {}, size() }; }

ErrorOr<NonnullRefPtr<Gfx::Bitmap>> bitmap(IntSize size, AffineTransform = {}) const;
void draw_into(Painter& painter, IntRect const& dest, AffineTransform = {}) const;

virtual ~VectorGraphic() = default;
};
Expand Down
8 changes: 1 addition & 7 deletions UI/Qt/TVGIconEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <AK/MemoryStream.h>
#include <AK/String.h>
#include <LibGfx/PainterSkia.h>
#include <LibGfx/Rect.h>
#include <UI/Qt/StringUtils.h>
#include <UI/Qt/TVGIconEngine.h>
Expand Down Expand Up @@ -34,12 +33,7 @@ QPixmap TVGIconEngine::pixmap(QSize const& size, QIcon::Mode mode, QIcon::State
auto key = pixmap_cache_key(size, mode, state);
if (QPixmapCache::find(key, &pixmap))
return pixmap;
auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { size.width(), size.height() }));

auto painter = Gfx::PainterSkia::create(bitmap);
painter->clear_rect(bitmap->rect().to_type<float>(), Gfx::Color::Transparent);

m_image_data->draw_into(*painter, bitmap->rect());
auto bitmap = MUST(m_image_data->bitmap({ size.width(), size.height() }));

for (auto const& filter : m_filters) {
if (filter->mode() == mode) {
Expand Down

0 comments on commit ea469fb

Please sign in to comment.