TinyVGLoader.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <AK/OwnPtr.h>
  9. #include <AK/Vector.h>
  10. #include <LibGfx/Color.h>
  11. #include <LibGfx/Forward.h>
  12. #include <LibGfx/ImageFormats/ImageDecoder.h>
  13. #include <LibGfx/PaintStyle.h>
  14. #include <LibGfx/Path.h>
  15. #include <LibGfx/VectorGraphic.h>
  16. namespace Gfx {
  17. // Current recommended SVG to TVG conversion (without installing tools)
  18. // (FIXME: Implement our own converter!)
  19. // 1. (Optional) Convert strokes to fills
  20. // * Only round joins/linecaps exist in TVG, so for other stroke kinds converting
  21. // them to fills (that still beziers etc, so are scalable) works better.
  22. // * This site can do that: https://iconly.io/tools/svg-convert-stroke-to-fill
  23. // 2. Scale your SVG's width/height to large size (e.g. 1024x?)
  24. // * Current converters deal poorly with small values in paths.
  25. // * This site can do that: https://www.iloveimg.com/resize-image/resize-svg
  26. // (or just edit the viewbox if it has one).
  27. // 3. Convert the SVG to a TVG
  28. // * This site can do that: https://svg-to-tvg-server.fly.dev/
  29. // Decoder from the "Tiny Vector Graphics" format (v1.0).
  30. // https://tinyvg.tech/download/specification.pdf
  31. struct TinyVGHeader;
  32. class TinyVGDecodedImageData final : public VectorGraphic {
  33. public:
  34. using Style = Variant<Color, NonnullRefPtr<SVGGradientPaintStyle>>;
  35. struct DrawCommand {
  36. Path path;
  37. Optional<Style> fill {};
  38. Optional<Style> stroke {};
  39. float stroke_width { 0.0f };
  40. };
  41. virtual IntSize intrinsic_size() const override
  42. {
  43. return m_size;
  44. }
  45. virtual void draw_transformed(Painter&, AffineTransform) const override;
  46. ReadonlySpan<DrawCommand> draw_commands() const
  47. {
  48. return m_draw_commands;
  49. }
  50. static ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> decode(Stream& stream);
  51. static ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> decode(Stream& stream, TinyVGHeader const& header);
  52. private:
  53. TinyVGDecodedImageData(IntSize size, Vector<DrawCommand> draw_commands)
  54. : m_size(size)
  55. , m_draw_commands(move(draw_commands))
  56. {
  57. }
  58. IntSize m_size;
  59. Vector<DrawCommand> m_draw_commands;
  60. };
  61. struct TinyVGLoadingContext;
  62. class TinyVGImageDecoderPlugin final : public ImageDecoderPlugin {
  63. public:
  64. static bool sniff(ReadonlyBytes);
  65. static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
  66. virtual IntSize size() override;
  67. virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) override;
  68. virtual NaturalFrameFormat natural_frame_format() const override { return NaturalFrameFormat::Vector; }
  69. virtual ErrorOr<VectorImageFrameDescriptor> vector_frame(size_t index) override;
  70. virtual ~TinyVGImageDecoderPlugin() override = default;
  71. private:
  72. TinyVGImageDecoderPlugin(ReadonlyBytes);
  73. NonnullOwnPtr<TinyVGLoadingContext> m_context;
  74. };
  75. }