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

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 #include "io/aep/gradient_xml.hpp"
0007 
0008 #include "io/svg/detail.hpp"
0009 
0010 using namespace glaxnimate::io;
0011 using namespace glaxnimate::io::aep;
0012 
0013 CosValue aep::xml_value(const QDomElement& element)
0014 {
0015     if ( element.tagName() == "prop.map" )
0016         return xml_value(element.firstChildElement());
0017     else if ( element.tagName() == "prop.list" )
0018         return xml_list(element);
0019     else if ( element.tagName() == "array" )
0020         return xml_array(element);
0021     else if ( element.tagName() == "int" )
0022         return element.text().toDouble();
0023     else if ( element.tagName() == "float" )
0024         return element.text().toDouble();
0025     else if ( element.tagName() == "string" )
0026         return element.text();
0027     else
0028         return {};
0029 }
0030 
0031 CosArray aep::xml_array(const QDomElement& element)
0032 {
0033     auto data = std::make_unique<CosArray::element_type>();
0034 
0035     for ( const auto& child : svg::detail::ElementRange(element) )
0036     {
0037         if ( child.tagName() != "array.type" )
0038             data->push_back(xml_value(child));
0039     }
0040     return data;
0041 }
0042 
0043 CosObject aep::xml_list(const QDomElement& element)
0044 {
0045     auto data = std::make_unique<CosObject::element_type>();
0046     for ( const auto& pair : svg::detail::ElementRange(element, "prop.pair") )
0047     {
0048         QString key;
0049         CosValue value;
0050         for ( const auto& ch : svg::detail::ElementRange(pair) )
0051         {
0052             if ( ch.tagName() == "key" )
0053                 key = ch.text();
0054             else
0055                 value = xml_value(ch);
0056         }
0057         data->emplace(key, std::move(value));
0058     }
0059 
0060     return data;
0061 }
0062 
0063 Gradient aep::parse_gradient_xml(const CosValue& value)
0064 {
0065     Gradient gradient;
0066     auto& data = get(value, "Gradient Color Data");
0067     gradient.color_stops = get_gradient_stops<GradientStopColor>(data);
0068     gradient.alpha_stops = get_gradient_stops<GradientStopAlpha>(data);
0069     return gradient;
0070 }
0071 
0072 Gradient aep::parse_gradient_xml(const QString& xml)
0073 {
0074     QDomDocument dom;
0075     dom.setContent(xml.trimmed());
0076     return parse_gradient_xml(xml_value(dom.documentElement()));
0077 }