2023-07-02 21:24:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Forward.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibGfx/Color.h>
|
2024-08-08 08:22:03 +00:00
|
|
|
#include <LibGfx/DeprecatedPath.h>
|
2023-07-02 21:24:01 +00:00
|
|
|
#include <LibGfx/Forward.h>
|
|
|
|
#include <LibGfx/ImageFormats/ImageDecoder.h>
|
|
|
|
#include <LibGfx/PaintStyle.h>
|
2023-07-07 21:23:04 +00:00
|
|
|
#include <LibGfx/VectorGraphic.h>
|
2023-07-02 21:24:01 +00:00
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
|
|
// Current recommended SVG to TVG conversion (without installing tools)
|
|
|
|
// (FIXME: Implement our own converter!)
|
|
|
|
// 1. (Optional) Convert strokes to fills
|
|
|
|
// * Only round joins/linecaps exist in TVG, so for other stroke kinds converting
|
|
|
|
// them to fills (that still beziers etc, so are scalable) works better.
|
|
|
|
// * This site can do that: https://iconly.io/tools/svg-convert-stroke-to-fill
|
|
|
|
// 2. Scale your SVG's width/height to large size (e.g. 1024x?)
|
|
|
|
// * Current converters deal poorly with small values in paths.
|
|
|
|
// * This site can do that: https://www.iloveimg.com/resize-image/resize-svg
|
|
|
|
// (or just edit the viewbox if it has one).
|
|
|
|
// 3. Convert the SVG to a TVG
|
|
|
|
// * This site can do that: https://svg-to-tvg-server.fly.dev/
|
|
|
|
|
|
|
|
// Decoder from the "Tiny Vector Graphics" format (v1.0).
|
|
|
|
// https://tinyvg.tech/download/specification.pdf
|
|
|
|
|
2023-07-08 02:29:51 +00:00
|
|
|
struct TinyVGHeader;
|
|
|
|
|
2023-07-07 21:23:04 +00:00
|
|
|
class TinyVGDecodedImageData final : public VectorGraphic {
|
2023-07-02 21:24:01 +00:00
|
|
|
public:
|
|
|
|
using Style = Variant<Color, NonnullRefPtr<SVGGradientPaintStyle>>;
|
|
|
|
|
|
|
|
struct DrawCommand {
|
2024-08-08 08:22:03 +00:00
|
|
|
DeprecatedPath path;
|
2023-07-02 21:24:01 +00:00
|
|
|
Optional<Style> fill {};
|
|
|
|
Optional<Style> stroke {};
|
|
|
|
float stroke_width { 0.0f };
|
|
|
|
};
|
|
|
|
|
2023-07-07 21:23:04 +00:00
|
|
|
virtual IntSize intrinsic_size() const override
|
2023-07-02 21:24:01 +00:00
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
2024-07-05 08:40:29 +00:00
|
|
|
virtual void draw_transformed(DeprecatedPainter&, AffineTransform) const override;
|
2023-07-07 21:23:04 +00:00
|
|
|
|
2023-07-02 21:24:01 +00:00
|
|
|
ReadonlySpan<DrawCommand> draw_commands() const
|
|
|
|
{
|
|
|
|
return m_draw_commands;
|
|
|
|
}
|
|
|
|
|
2023-07-07 21:23:04 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> decode(Stream& stream);
|
2023-07-08 02:29:51 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> decode(Stream& stream, TinyVGHeader const& header);
|
2023-07-02 21:24:01 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
TinyVGDecodedImageData(IntSize size, Vector<DrawCommand> draw_commands)
|
|
|
|
: m_size(size)
|
|
|
|
, m_draw_commands(move(draw_commands))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize m_size;
|
|
|
|
Vector<DrawCommand> m_draw_commands;
|
|
|
|
};
|
|
|
|
|
2023-07-08 02:29:51 +00:00
|
|
|
struct TinyVGLoadingContext;
|
2023-07-02 21:24:01 +00:00
|
|
|
|
|
|
|
class TinyVGImageDecoderPlugin final : public ImageDecoderPlugin {
|
|
|
|
public:
|
|
|
|
static bool sniff(ReadonlyBytes);
|
|
|
|
static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
|
|
|
|
|
|
|
|
virtual IntSize size() override;
|
|
|
|
virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) override;
|
|
|
|
|
2024-01-08 01:41:32 +00:00
|
|
|
virtual NaturalFrameFormat natural_frame_format() const override { return NaturalFrameFormat::Vector; }
|
2023-07-07 21:23:04 +00:00
|
|
|
virtual ErrorOr<VectorImageFrameDescriptor> vector_frame(size_t index) override;
|
|
|
|
|
2024-06-16 08:38:35 +00:00
|
|
|
virtual ~TinyVGImageDecoderPlugin() override;
|
2023-07-02 21:24:01 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
TinyVGImageDecoderPlugin(ReadonlyBytes);
|
|
|
|
|
2023-07-08 02:29:51 +00:00
|
|
|
NonnullOwnPtr<TinyVGLoadingContext> m_context;
|
2023-07-02 21:24:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|