Skip to content

Commit

Permalink
ISSUE #687 added fixed width and height attributes to the svg exports…
Browse files Browse the repository at this point in the history
…, and added unit test to make sure they dont break in the future
  • Loading branch information
sebjf committed Aug 14, 2024
1 parent 9a5898c commit ee45987
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,23 @@ void FileProcessorDgn::importDrawing(OdDgDatabasePtr pDb, const ODCOLORREF* pPal

pHelperDevice->update();

// Finally copy the contents of the stream to the collector's buffer;
// getBytes advances OdMemoryStream, so we must first seek its start.
// Finally copy the contents of the stream to the collector's buffer

drawingCollector->data.resize(stream->tell());
// Copy the SVG contents into a string

std::vector<char> buffer;
buffer.resize(stream->tell());
stream->seek(0, OdDb::FilerSeekType::kSeekFromStart);
stream->getBytes(drawingCollector->data.data(), stream->length());
stream->getBytes(buffer.data(), stream->length());
std::string svg(buffer.data(), buffer.size());

// Perform any further necessary manipulations. In this case we add the width
// and height attributes.

svg.insert(61, "width=\"1024\" height=\"768\" "); // 61 is just after the svg tag. This offset is fixed for exporter version.

// Provide the string to the collector as a vector

std::copy(svg.c_str(), svg.c_str() + svg.length(), std::back_inserter(drawingCollector->data));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,22 @@ void importDrawing(OdDbDatabasePtr pDb, repo::manipulator::modelutility::Drawing

pHelperDevice->update();

collector->data.resize(stream->tell());
// Copy the SVG contents into a string

std::vector<char> buffer;
buffer.resize(stream->tell());
stream->seek(0, OdDb::FilerSeekType::kSeekFromStart);
stream->getBytes(collector->data.data(), stream->length());
stream->getBytes(buffer.data(), stream->length());
std::string svg(buffer.data(), buffer.size());

// Perform any further necessary manipulations. In this case we add the width
// and height attributes.

svg.insert(61, "width=\"1024\" height=\"768\" "); // 61 is just after the svg tag. This offset is fixed for exporter version.

// Provide the string to the collector as a vector

std::copy(svg.c_str(), svg.c_str() + svg.length(), std::back_inserter(collector->data));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "../../../../repo_test_utils.h"
#include "../../../../repo_test_database_info.h"
#include "boost/filesystem.hpp"
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/xml_parser.hpp"
#include "boost/iostreams/stream.hpp"
#include "boost/iostreams/device/array.hpp"
#include "../../bouncer/src/repo/error_codes.h"

using namespace repo::manipulator::modelconvertor;
Expand All @@ -34,6 +38,18 @@ TEST(DrawingImportManager, ImportDGN)

EXPECT_EQ(error, REPOERR_OK);
EXPECT_GT(drawing.data.size(), 0);

boost::iostreams::basic_array_source<char> svgArraySource((char*)drawing.data.data(), drawing.data.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char>> svgStream(svgArraySource);

boost::property_tree::ptree svgTree;
boost::property_tree::read_xml(svgStream, svgTree); // This will throw an exception if the xml is not well formed

// Check a number of properties that we expect to be consistent

EXPECT_EQ(svgTree.get<std::string>("svg.<xmlattr>.width"), "1024");
EXPECT_EQ(svgTree.get<std::string>("svg.<xmlattr>.height"), "768");
EXPECT_EQ(svgTree.get<std::string>("svg.<xmlattr>.viewBox"), "0 0 1024 768");
}

TEST(DrawingImportManager, ImportDWG)
Expand All @@ -45,5 +61,17 @@ TEST(DrawingImportManager, ImportDWG)

EXPECT_EQ(error, REPOERR_OK);
EXPECT_GT(drawing.data.size(), 0);

boost::iostreams::basic_array_source<char> svgArraySource((char*)drawing.data.data(), drawing.data.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char>> svgStream(svgArraySource);

boost::property_tree::ptree svgTree;
boost::property_tree::read_xml(svgStream, svgTree); // This will throw an exception if the xml is not well formed

// Check a number of properties that we expect to be consistent

EXPECT_EQ(svgTree.get<std::string>("svg.<xmlattr>.width"), "1024");
EXPECT_EQ(svgTree.get<std::string>("svg.<xmlattr>.height"), "768");
EXPECT_EQ(svgTree.get<std::string>("svg.<xmlattr>.viewBox"), "0 0 1024 768");
}

0 comments on commit ee45987

Please sign in to comment.