summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Parser/PayloadParsers/WhiteboardParser.cpp')
-rw-r--r--Swiften/Parser/PayloadParsers/WhiteboardParser.cpp87
1 files changed, 79 insertions, 8 deletions
diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp
index d0fa9f9..7182a8c 100644
--- a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp
+++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp
@@ -6,9 +6,11 @@
#include <Swiften/Parser/PayloadParsers/WhiteboardParser.h>
#include <Swiften/Whiteboard/Elements/WhiteboardLineElement.h>
+#include <Swiften/Whiteboard/Elements/WhiteboardFreehandPathElement.h>
#include <Swiften/Whiteboard/Elements/Color.h>
#include <boost/optional.hpp>
#include <boost/smart_ptr/make_shared.hpp>
+#include <boost/lexical_cast.hpp>
namespace Swift {
WhiteboardParser::WhiteboardParser() : level_(0) {
@@ -19,24 +21,93 @@ namespace Swift {
getPayloadInternal()->setType(stringToType(attributes.getAttributeValue("type").get_value_or("")));
} else if (level_ == 1) {
if (element == "line") {
- int x1 = std::atoi(attributes.getAttributeValue("x1").get_value_or("0").c_str());
- int y1 = std::atoi(attributes.getAttributeValue("y1").get_value_or("0").c_str());
- int x2 = std::atoi(attributes.getAttributeValue("x2").get_value_or("0").c_str());
- int y2 = std::atoi(attributes.getAttributeValue("y2").get_value_or("0").c_str());
+ int x1 = 0;
+ int y1 = 0;
+ int x2 = 0;
+ int y2 = 0;
+ try {
+ x1 = boost::lexical_cast<int>(attributes.getAttributeValue("x1").get_value_or("0"));
+ y1 = boost::lexical_cast<int>(attributes.getAttributeValue("y1").get_value_or("0"));
+ x2 = boost::lexical_cast<int>(attributes.getAttributeValue("x2").get_value_or("0"));
+ y2 = boost::lexical_cast<int>(attributes.getAttributeValue("y2").get_value_or("0"));
+ } catch (boost::bad_lexical_cast&) {
+ }
WhiteboardLineElement::ref whiteboardElement = boost::make_shared<WhiteboardLineElement>(x1, y1, x2, y2);
- Color color(attributes.getAttributeValue("stroke").get_value_or("#00000"));
+ Color color(attributes.getAttributeValue("stroke").get_value_or("#000000"));
std::string opacity = attributes.getAttributeValue("opacity").get_value_or("1");
if (opacity.find('.') != std::string::npos) {
opacity = opacity.substr(opacity.find('.')+1, 2);
- color.setAlpha(std::atoi(opacity.c_str())*255/100);
+ try {
+ color.setAlpha(boost::lexical_cast<int>(opacity)*255/100);
+ } catch (boost::bad_lexical_cast&) {
+ }
}
-
whiteboardElement->setColor(color);
- int penWidth = std::atoi(attributes.getAttributeValue("stroke-width").get_value_or("1").c_str());
+ int penWidth = 1;
+ try {
+ penWidth = boost::lexical_cast<int>(attributes.getAttributeValue("stroke-width").get_value_or("1"));
+ } catch (boost::bad_lexical_cast&) {
+ }
+ whiteboardElement->setPenWidth(penWidth);
+
+ getPayloadInternal()->setElement(whiteboardElement);
+ } else if (element == "path") {
+ WhiteboardFreehandPathElement::ref whiteboardElement = boost::make_shared<WhiteboardFreehandPathElement>();
+ std::string pathData = attributes.getAttributeValue("d").get_value_or("");
+ std::vector<std::pair<int, int> > points;
+ if (pathData[0] == 'M') {
+ int pos = 1;
+ int npos;
+ int x, y;
+ if (pathData[pos] == ' ') {
+ pos++;
+ }
+ try {
+ npos = pathData.find(' ', pos);
+ x = boost::lexical_cast<int>(pathData.substr(pos, npos-pos));
+ pos = npos+1;
+ npos = pathData.find('L', pos);
+ y = boost::lexical_cast<int>(pathData.substr(pos, npos-pos));
+ pos = npos+1;
+ if (pathData[pos] == ' ') {
+ pos++;
+ }
+ points.push_back(std::pair<int, int>(x, y));
+ while (pos < pathData.size()) {
+ npos = pathData.find(' ', pos);
+ x = boost::lexical_cast<int>(pathData.substr(pos, npos-pos));
+ pos = npos+1;
+ npos = pathData.find(' ', pos);
+ y = boost::lexical_cast<int>(pathData.substr(pos, npos-pos));
+ pos = npos+1;
+ points.push_back(std::pair<int, int>(x, y));
+ }
+ } catch (boost::bad_lexical_cast&) {
+ }
+ }
+ whiteboardElement->setPoints(points);
+
+ int penWidth = 1;
+ try {
+ penWidth = boost::lexical_cast<int>(attributes.getAttributeValue("stroke-width").get_value_or("1"));
+ } catch (boost::bad_lexical_cast&) {
+ }
whiteboardElement->setPenWidth(penWidth);
+ Color color(attributes.getAttributeValue("stroke").get_value_or("#000000"));
+
+ std::string opacity = attributes.getAttributeValue("opacity").get_value_or("1");
+ if (opacity.find('.') != std::string::npos) {
+ opacity = opacity.substr(opacity.find('.')+1, 2);
+ try {
+ color.setAlpha(boost::lexical_cast<int>(opacity)*255/100);
+ } catch (boost::bad_lexical_cast&) {
+ }
+ }
+ whiteboardElement->setColor(color);
+
getPayloadInternal()->setElement(whiteboardElement);
}
}