TinyVGLoader.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. namespace Gfx {
  16. // Current recommended SVG to TVG conversion (without installing tools)
  17. // (FIXME: Implement our own converter!)
  18. // 1. (Optional) Convert strokes to fills
  19. // * Only round joins/linecaps exist in TVG, so for other stroke kinds converting
  20. // them to fills (that still beziers etc, so are scalable) works better.
  21. // * This site can do that: https://iconly.io/tools/svg-convert-stroke-to-fill
  22. // 2. Scale your SVG's width/height to large size (e.g. 1024x?)
  23. // * Current converters deal poorly with small values in paths.
  24. // * This site can do that: https://www.iloveimg.com/resize-image/resize-svg
  25. // (or just edit the viewbox if it has one).
  26. // 3. Convert the SVG to a TVG
  27. // * This site can do that: https://svg-to-tvg-server.fly.dev/
  28. // Decoder from the "Tiny Vector Graphics" format (v1.0).
  29. // https://tinyvg.tech/download/specification.pdf
  30. class TinyVGDecodedImageData {
  31. public:
  32. using Style = Variant<Color, NonnullRefPtr<SVGGradientPaintStyle>>;
  33. struct DrawCommand {
  34. Path path;
  35. Optional<Style> fill {};
  36. Optional<Style> stroke {};
  37. float stroke_width { 0.0f };
  38. };
  39. ErrorOr<RefPtr<Gfx::Bitmap>> bitmap(IntSize size) const;
  40. IntSize size() const
  41. {
  42. return m_size;
  43. }
  44. ReadonlySpan<DrawCommand> draw_commands() const
  45. {
  46. return m_draw_commands;
  47. }
  48. static ErrorOr<TinyVGDecodedImageData> decode(Stream& stream);
  49. private:
  50. TinyVGDecodedImageData(IntSize size, Vector<DrawCommand> draw_commands)
  51. : m_size(size)
  52. , m_draw_commands(move(draw_commands))
  53. {
  54. }
  55. IntSize m_size;
  56. Vector<DrawCommand> m_draw_commands;
  57. };
  58. struct TinyVGLoadingContext {
  59. ReadonlyBytes data;
  60. OwnPtr<TinyVGDecodedImageData> decoded_image {};
  61. RefPtr<Bitmap> bitmap {};
  62. };
  63. class TinyVGImageDecoderPlugin final : public ImageDecoderPlugin {
  64. public:
  65. static bool sniff(ReadonlyBytes);
  66. static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
  67. virtual IntSize size() override;
  68. virtual ErrorOr<void> initialize() override;
  69. virtual bool is_animated() override { return false; }
  70. virtual size_t loop_count() override { return 0; }
  71. virtual size_t frame_count() override { return 1; }
  72. virtual size_t first_animated_frame_index() override { return 0; }
  73. virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) override;
  74. virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() override { return OptionalNone {}; };
  75. virtual ~TinyVGImageDecoderPlugin() override = default;
  76. private:
  77. TinyVGImageDecoderPlugin(ReadonlyBytes);
  78. TinyVGLoadingContext m_context;
  79. };
  80. }