File indexing completed on 2025-01-05 04:01:19

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include "io/svg/svg_parser.hpp"
0010 #include "io/svg/parse_error.hpp"
0011 #include "io/svg/svg_renderer.hpp"
0012 #include "io/mime/mime_serializer.hpp"
0013 
0014 namespace glaxnimate::io::svg {
0015 
0016 class SvgMime : public io::mime::MimeSerializer
0017 {
0018 public:
0019     QString slug() const override { return "svg"; }
0020     QString name() const override { return i18n("SVG"); }
0021     QStringList mime_types() const override { return {"image/svg+xml"}; }
0022 
0023     QByteArray serialize(const std::vector<model::DocumentNode*>& selection) const override
0024     {
0025         io::svg::SvgRenderer svg_rend(io::svg::NotAnimated, io::svg::CssFontType::FontFace);
0026         for ( auto node : selection )
0027             svg_rend.write_node(node);
0028         return svg_rend.dom().toByteArray(0);
0029     }
0030 
0031     io::mime::DeserializedData deserialize(const QByteArray& data) const override
0032     {
0033         QBuffer buffer(const_cast<QByteArray*>(&data));
0034         buffer.open(QIODevice::ReadOnly);
0035 
0036         auto on_error = [this](const QString& s){message(s);};
0037         try {
0038             return io::svg::SvgParser(&buffer, deserialize_group_mode, nullptr, on_error)
0039                 .parse_to_objects();
0040         } catch ( const io::svg::SvgParseError& err ) {
0041             message(err.formatted("Clipboard"));
0042             return {};
0043         }
0044     }
0045 
0046     bool can_deserialize() const override { return true; }
0047 
0048     /// \todo show in settings
0049     io::svg::SvgParser::GroupMode deserialize_group_mode = io::svg::SvgParser::Inkscape;
0050 
0051 private:
0052     static Autoreg<SvgMime> autoreg;
0053 };
0054 
0055 } // namespace glaxnimate::io::svg