mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-08-02 06:10:36 +08:00
change char[] to std::string as svg file data storage
This commit is contained in:
parent
41f1c2caa5
commit
fc11639679
@ -100,7 +100,7 @@ struct EmbossShape
|
|||||||
std::shared_ptr<NSVGimage> image = nullptr;
|
std::shared_ptr<NSVGimage> image = nullptr;
|
||||||
|
|
||||||
// Loaded string data from file
|
// Loaded string data from file
|
||||||
std::shared_ptr<char[]> file_data = nullptr;
|
std::shared_ptr<std::string> file_data = nullptr;
|
||||||
};
|
};
|
||||||
SvgFile svg_file;
|
SvgFile svg_file;
|
||||||
|
|
||||||
|
@ -474,7 +474,7 @@ namespace Slic3r {
|
|||||||
typedef std::map<int, CutObjectInfo> IdToCutObjectInfoMap;
|
typedef std::map<int, CutObjectInfo> IdToCutObjectInfoMap;
|
||||||
typedef std::map<int, std::vector<sla::SupportPoint>> IdToSlaSupportPointsMap;
|
typedef std::map<int, std::vector<sla::SupportPoint>> IdToSlaSupportPointsMap;
|
||||||
typedef std::map<int, std::vector<sla::DrainHole>> IdToSlaDrainHolesMap;
|
typedef std::map<int, std::vector<sla::DrainHole>> IdToSlaDrainHolesMap;
|
||||||
using PathToEmbossShapeFileMap = std::map<std::string, std::shared_ptr<char[]>>;
|
using PathToEmbossShapeFileMap = std::map<std::string, std::shared_ptr<std::string>>;
|
||||||
// Version of the 3mf file
|
// Version of the 3mf file
|
||||||
unsigned int m_version;
|
unsigned int m_version;
|
||||||
bool m_check_version;
|
bool m_check_version;
|
||||||
@ -1387,25 +1387,18 @@ namespace Slic3r {
|
|||||||
|
|
||||||
void _3MF_Importer::_extract_embossed_svg_shape_file(const std::string &filename, mz_zip_archive &archive, const mz_zip_archive_file_stat &stat){
|
void _3MF_Importer::_extract_embossed_svg_shape_file(const std::string &filename, mz_zip_archive &archive, const mz_zip_archive_file_stat &stat){
|
||||||
assert(m_path_to_emboss_shape_files.find(filename) == m_path_to_emboss_shape_files.end());
|
assert(m_path_to_emboss_shape_files.find(filename) == m_path_to_emboss_shape_files.end());
|
||||||
|
auto file = std::make_unique<std::string>(stat.m_uncomp_size, '\0');
|
||||||
std::unique_ptr<char[]> file{new char[stat.m_uncomp_size + 1]};
|
mz_bool res = mz_zip_reader_extract_to_mem(&archive, stat.m_file_index, (void *) file->data(), stat.m_uncomp_size, 0);
|
||||||
if (file == nullptr){
|
|
||||||
add_error("Cannot alocate space for SVG file.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
mz_bool res = mz_zip_reader_extract_to_mem(&archive, stat.m_file_index, (void *) file.get(), (size_t) stat.m_uncomp_size, 0);
|
|
||||||
if (res == 0) {
|
if (res == 0) {
|
||||||
add_error("Error while reading svg shape for emboss");
|
add_error("Error while reading svg shape for emboss");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
file.get()[stat.m_uncomp_size] = '\0'; // Must be null terminated.
|
|
||||||
|
|
||||||
// store for case svg is loaded before volume
|
// store for case svg is loaded before volume
|
||||||
m_path_to_emboss_shape_files[filename] = std::move(file);
|
m_path_to_emboss_shape_files[filename] = std::move(file);
|
||||||
|
|
||||||
// find embossed volume, for case svg is loaded after volume
|
// find embossed volume, for case svg is loaded after volume
|
||||||
for (ModelObject* object : m_model->objects)
|
for (const ModelObject* object : m_model->objects)
|
||||||
for (ModelVolume *volume : object->volumes) {
|
for (ModelVolume *volume : object->volumes) {
|
||||||
std::optional<EmbossShape> &es = volume->emboss_shape;
|
std::optional<EmbossShape> &es = volume->emboss_shape;
|
||||||
if (!es.has_value())
|
if (!es.has_value())
|
||||||
@ -3752,15 +3745,8 @@ bool to_xml(std::stringstream &stream, const EmbossShape::SvgFile &svg, const Mo
|
|||||||
stream << SVG_FILE_PATH_ATTR << "=\"" << xml_escape_double_quotes_attribute_value(svg.path) << "\" ";
|
stream << SVG_FILE_PATH_ATTR << "=\"" << xml_escape_double_quotes_attribute_value(svg.path) << "\" ";
|
||||||
stream << SVG_FILE_PATH_IN_3MF_ATTR << "=\"" << xml_escape_double_quotes_attribute_value(svg.path_in_3mf) << "\" ";
|
stream << SVG_FILE_PATH_IN_3MF_ATTR << "=\"" << xml_escape_double_quotes_attribute_value(svg.path_in_3mf) << "\" ";
|
||||||
|
|
||||||
char *data = svg.file_data.get();
|
const std::string &file_data = *svg.file_data;
|
||||||
assert(data != nullptr);
|
if (!mz_zip_writer_add_mem(&archive, svg.path_in_3mf.c_str(), (const void *) file_data.c_str(), file_data.size(), MZ_DEFAULT_COMPRESSION))
|
||||||
if (data == nullptr)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// NOTE: file data must be null terminated
|
|
||||||
size_t size = 0;
|
|
||||||
for (char *c = data; *c != '\0'; ++c) ++size;
|
|
||||||
if (!mz_zip_writer_add_mem(&archive, svg.path_in_3mf.c_str(), (const void *) data, size, MZ_DEFAULT_COMPRESSION))
|
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -98,41 +98,28 @@ void bounds(const NSVGimage &image, Vec2f& min, Vec2f &max)
|
|||||||
NSVGimage_ptr nsvgParseFromFile(const std::string &filename, const char *units, float dpi)
|
NSVGimage_ptr nsvgParseFromFile(const std::string &filename, const char *units, float dpi)
|
||||||
{
|
{
|
||||||
NSVGimage *image = ::nsvgParseFromFile(filename.c_str(), units, dpi);
|
NSVGimage *image = ::nsvgParseFromFile(filename.c_str(), units, dpi);
|
||||||
return {image, ::nsvgDelete};
|
return {image, &nsvgDelete};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<char[]> read_from_disk(const std::string& path)
|
std::unique_ptr<std::string> read_from_disk(const std::string &path)
|
||||||
{
|
{
|
||||||
FILE *fp = boost::nowide::fopen(path.c_str(), "rb");
|
std::ifstream fs{path};
|
||||||
if (!fp)
|
if (!fs.is_open())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
std::stringstream ss;
|
||||||
fseek(fp, 0, SEEK_END);
|
ss << fs.rdbuf();
|
||||||
size_t size = ftell(fp);
|
return std::make_unique<std::string>(ss.str());
|
||||||
fseek(fp, 0, SEEK_SET);
|
|
||||||
|
|
||||||
std::unique_ptr<char[]> result{new char[size + 1]};
|
|
||||||
if (result == nullptr)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
if (fread(result.get(), 1, size, fp) != size)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
result.get()[size] = '\0'; // Must be null terminated.
|
|
||||||
fclose(fp);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NSVGimage_ptr nsvgParse(const std::shared_ptr<char[]> file_data, const char *units, float dpi){
|
NSVGimage_ptr nsvgParse(const std::string& file_data, const char *units, float dpi){
|
||||||
size_t size = 0;
|
// NOTE: nsvg parser consume data from input(char *)
|
||||||
for (char *c = file_data.get(); *c != '\0'; ++c)
|
size_t size = file_data.size();
|
||||||
++size;
|
// file data could be big, so it is allocated on heap
|
||||||
|
std::unique_ptr<char[]> data_copy(new char[size+1]);
|
||||||
// NOTE: nsvg parser consume data from pointer
|
memcpy(data_copy.get(), file_data.c_str(), size);
|
||||||
std::unique_ptr<char[]> data_copy(new char[size]);
|
data_copy[size] = '\0'; // data for nsvg must be null terminated
|
||||||
memcpy(data_copy.get(), file_data.get(), size);
|
|
||||||
NSVGimage *image = ::nsvgParse(data_copy.get(), units, dpi);
|
NSVGimage *image = ::nsvgParse(data_copy.get(), units, dpi);
|
||||||
return {image, ::nsvgDelete};
|
return {image, &nsvgDelete};
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t get_shapes_count(const NSVGimage &image)
|
size_t get_shapes_count(const NSVGimage &image)
|
||||||
|
@ -66,11 +66,11 @@ Polygons to_polygons(const NSVGimage &image, const NSVGLineParams ¶m);
|
|||||||
void bounds(const NSVGimage &image, Vec2f &min, Vec2f &max);
|
void bounds(const NSVGimage &image, Vec2f &min, Vec2f &max);
|
||||||
|
|
||||||
// read text data from file
|
// read text data from file
|
||||||
std::unique_ptr<char[]> read_from_disk(const std::string &path);
|
std::unique_ptr<std::string> read_from_disk(const std::string &path);
|
||||||
|
|
||||||
using NSVGimage_ptr = std::unique_ptr<NSVGimage, void (*)(NSVGimage*)>;
|
using NSVGimage_ptr = std::unique_ptr<NSVGimage, void (*)(NSVGimage*)>;
|
||||||
NSVGimage_ptr nsvgParseFromFile(const std::string &svg_file_path, const char *units = "mm", float dpi = 96.0f);
|
NSVGimage_ptr nsvgParseFromFile(const std::string &svg_file_path, const char *units = "mm", float dpi = 96.0f);
|
||||||
NSVGimage_ptr nsvgParse(const std::shared_ptr<char[]> file_data, const char *units = "mm", float dpi = 96.0f);
|
NSVGimage_ptr nsvgParse(const std::string& file_data, const char *units = "mm", float dpi = 96.0f);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Iterate over shapes and calculate count
|
/// Iterate over shapes and calculate count
|
||||||
|
@ -570,7 +570,7 @@ NSVGimage* init_image(EmbossShape::SvgFile &svg_file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// init svg image
|
// init svg image
|
||||||
svg_file.image = nsvgParse(svg_file.file_data);
|
svg_file.image = nsvgParse(*svg_file.file_data);
|
||||||
if (svg_file.image.get() == NULL)
|
if (svg_file.image.get() == NULL)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
@ -1375,20 +1375,13 @@ void GLGizmoSVG::draw_filename(){
|
|||||||
EmbossShape::SvgFile &svg = m_volume_shape.svg_file;
|
EmbossShape::SvgFile &svg = m_volume_shape.svg_file;
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
Slic3r::save(*svg.image, ss);
|
Slic3r::save(*svg.image, ss);
|
||||||
ss << '\0'; // Must be null terminated.
|
svg.file_data = std::make_unique<std::string>(ss.str());
|
||||||
std::string str = ss.str();
|
svg.image = nsvgParse(*svg.file_data);
|
||||||
std::unique_ptr<char[]> new_data{new char[str.size() + 1]};
|
assert(svg.image.get() != NULL);
|
||||||
assert(new_data != nullptr);
|
if (svg.image.get() != NULL) {
|
||||||
if (new_data != nullptr) {
|
m_volume->emboss_shape->svg_file = svg; // copy - write changes into volume
|
||||||
std::copy(str.begin(), str.end(), new_data.get());
|
} else {
|
||||||
svg.file_data = std::move(new_data);
|
svg = m_volume->emboss_shape->svg_file; // revert changes
|
||||||
svg.image = nsvgParse(svg.file_data);
|
|
||||||
assert(svg.image.get() != NULL);
|
|
||||||
if (svg.image.get() != NULL) {
|
|
||||||
m_volume->emboss_shape->svg_file = svg; // copy - write changes into volume
|
|
||||||
} else {
|
|
||||||
svg = m_volume->emboss_shape->svg_file; // revert changes
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (ImGui::IsItemHovered()) {
|
} else if (ImGui::IsItemHovered()) {
|
||||||
ImGui::SetTooltip("%s", _u8L("Use only paths from svg - recreate svg").c_str());
|
ImGui::SetTooltip("%s", _u8L("Use only paths from svg - recreate svg").c_str());
|
||||||
@ -2000,8 +1993,7 @@ EmbossShape select_shape(std::string_view filepath, double tesselation_tolerance
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
init_image(shape.svg_file);
|
if(init_image(shape.svg_file) == nullptr) {
|
||||||
if (shape.svg_file.image.get() == NULL) {
|
|
||||||
show_error(nullptr, GUI::format(_u8L("Nano SVG parser can't load from file(%1%)."), shape.svg_file.path));
|
show_error(nullptr, GUI::format(_u8L("Nano SVG parser can't load from file(%1%)."), shape.svg_file.path));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user