diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index ede2dc6614..4539efc8d8 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -208,6 +208,8 @@ add_library(libslic3r STATIC Tesselate.cpp Tesselate.hpp TextConfiguration.hpp + TextConfigurationSerialization.cpp + TextConfigurationSerialization.hpp TriangleMesh.cpp TriangleMesh.hpp TriangleMeshSlicer.cpp diff --git a/src/libslic3r/Format/3mf.cpp b/src/libslic3r/Format/3mf.cpp index 295c1413d4..2c4bb68ecd 100644 --- a/src/libslic3r/Format/3mf.cpp +++ b/src/libslic3r/Format/3mf.cpp @@ -32,6 +32,8 @@ namespace pt = boost::property_tree; #include #include "miniz_extension.hpp" +#include "TextConfigurationSerialization.hpp" + // Slightly faster than sprintf("%.9g"), but there is an issue with the karma floating point formatter, // https://github.com/boostorg/spirit/pull/586 // where the exported string is one digit shorter than it should be to guarantee lossless round trip. @@ -2941,6 +2943,10 @@ namespace Slic3r { stream << " <" << METADATA_TAG << " " << TYPE_ATTR << "=\"" << VOLUME_TYPE << "\" " << KEY_ATTR << "=\"" << VOLUME_TYPE_KEY << "\" " << VALUE_ATTR << "=\"" << ModelVolume::type_to_string(volume->type()) << "\"/>\n"; + // stores volume's text data + if (volume->text_configuration.has_value()) + stream << " " << TextConfigurationSerialization::to_string(*volume->text_configuration) << "\n"; + // stores volume's local matrix stream << " <" << METADATA_TAG << " " << TYPE_ATTR << "=\"" << VOLUME_TYPE << "\" " << KEY_ATTR << "=\"" << MATRIX_KEY << "\" " << VALUE_ATTR << "=\""; Transform3d matrix = volume->get_matrix() * volume->source.transform.get_matrix(); diff --git a/src/libslic3r/TextConfigurationSerialization.cpp b/src/libslic3r/TextConfigurationSerialization.cpp new file mode 100644 index 0000000000..e508bbf9d8 --- /dev/null +++ b/src/libslic3r/TextConfigurationSerialization.cpp @@ -0,0 +1,68 @@ +#include "TextConfigurationSerialization.hpp" + +#include +#include + +using namespace Slic3r; + +const std::string TextConfigurationSerialization::font_item = "FontItem"; +const std::string TextConfigurationSerialization::font_prop = "FontProp"; +const std::string TextConfigurationSerialization::text = "Text"; + +namespace cereal { +template +void serialize(Archive &archive, FontItem &font_item) +{ + archive( + CEREAL_NVP(font_item.name), + CEREAL_NVP(font_item.path) + //,CEREAL_NVP(font_item.type) + ); +} + +template +void serialize(Archive &archive, FontProp &font_prop) +{ + archive( + CEREAL_NVP(font_prop.char_gap), + CEREAL_NVP(font_prop.line_gap), + CEREAL_NVP(font_prop.flatness), + CEREAL_NVP(font_prop.size_in_mm), + CEREAL_NVP(font_prop.emboss) + ); +} + +template +void serialize(Archive &archive, TextConfiguration &text_configuration) +{ + archive(CEREAL_NVP(text_configuration.font_item), + CEREAL_NVP(text_configuration.font_prop), + CEREAL_NVP(text_configuration.text)); +} +} // namespace cereal + +std::string TextConfigurationSerialization::to_string( + const TextConfiguration &text_configuration) +{ + std::strstream ss; + { + cereal::XMLOutputArchive archive(ss); + // CEREAL_NVP - Names the output the same as the variable name + archive(CEREAL_NVP(text_configuration)); + } + std::string result = ss.str(); + static size_t start = std::string("\n\n\t").size(); + size_t end = result.find("\n", start); + result = result.substr(start, end - start); + return result; +} + +std::optional TextConfigurationSerialization::from_string(const std::string &data) +{ + std::strstream ss; + ss << data; + cereal::XMLInputArchive archive(ss); + TextConfiguration tc; + archive(tc); + return tc; +} \ No newline at end of file diff --git a/src/libslic3r/TextConfigurationSerialization.hpp b/src/libslic3r/TextConfigurationSerialization.hpp new file mode 100644 index 0000000000..38eb604956 --- /dev/null +++ b/src/libslic3r/TextConfigurationSerialization.hpp @@ -0,0 +1,24 @@ +#ifndef slic3r_TextConfigurationSerialization_hpp_ +#define slic3r_TextConfigurationSerialization_hpp_ + +#include "TextConfiguration.hpp" +#include + +namespace Slic3r { + +// utility for convert TextConfiguration into string and vice versa +class TextConfigurationSerialization +{ +public: + TextConfigurationSerialization() = delete; // only static functions + static std::string to_string(const TextConfiguration &text_configuration); + static std::optional from_string(const std::string &data); + +private: + static const std::string font_item; + static const std::string font_prop; + static const std::string text; +}; +} // namespace Slic3r + +#endif // slic3r_TextConfigurationSerialization_hpp_