Merge pull request #1371 from siliconninja/issue_1277

Fix coordinate precision to 8 decimal places to reduce file size
presentation
JJones780 7 years ago committed by GitHub
commit a6276cd295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/control/settings/Settings.cpp
  2. 18
      src/control/xml/DoubleArrayAttribute.cpp
  3. 4
      src/control/xml/DoubleAttribute.cpp
  4. 12
      src/control/xml/XmlPointNode.cpp
  5. 36
      src/control/xml/XmlStrokeNode.cpp
  6. 5
      src/util/Util.cpp
  7. 15
      src/util/Util.h

@ -780,7 +780,8 @@ xmlNodePtr Settings::savePropertyDouble(const gchar* key, double value, xmlNodeP
XOJ_CHECK_TYPE(Settings);
char text[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( text, G_ASCII_DTOSTR_BUF_SIZE, value); // g_ascii_ version uses C locale always.
// g_ascii_ version uses C locale always.
g_ascii_formatd(text, G_ASCII_DTOSTR_BUF_SIZE, Util::PRECISION_FORMAT_STRING, value);
xmlNodePtr xmlNode = saveProperty(key, text, parent);
return xmlNode;
}
@ -1043,7 +1044,8 @@ void Settings::save()
xmlSetProp(xmlFont, (const xmlChar*) "font", (const xmlChar*) this->font.getName().c_str());
char sSize[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( sSize, G_ASCII_DTOSTR_BUF_SIZE, this->font.getSize()); //no locale
g_ascii_formatd(sSize, G_ASCII_DTOSTR_BUF_SIZE, Util::PRECISION_FORMAT_STRING, this->font.getSize()); // no locale
xmlSetProp(xmlFont, (const xmlChar*) "size", (const xmlChar*) sSize);
@ -1100,7 +1102,7 @@ void Settings::saveData(xmlNodePtr root, string name, SElement& elem)
type = "double";
char tmp[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( tmp, G_ASCII_DTOSTR_BUF_SIZE, attrib.dValue);
g_ascii_formatd(tmp, G_ASCII_DTOSTR_BUF_SIZE, Util::PRECISION_FORMAT_STRING, attrib.dValue);
value = tmp;
}
else if (attrib.type == ATTRIBUTE_TYPE_INT_HEX)

@ -1,4 +1,5 @@
#include "DoubleArrayAttribute.h"
#include "Util.h"
DoubleArrayAttribute::DoubleArrayAttribute(const char* name, double* values, int count) : XMLAttribute(name)
{
@ -25,15 +26,16 @@ void DoubleArrayAttribute::writeOut(OutputStream* out)
if (this->count > 0)
{
char str[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( str, G_ASCII_DTOSTR_BUF_SIZE, this->values[0]); // g_ascii_ version uses C locale always.
// g_ascii_ version uses C locale always.
g_ascii_formatd(str, G_ASCII_DTOSTR_BUF_SIZE, Util::PRECISION_FORMAT_STRING, this->values[0]);
out->write(str);
}
for (int i = 1; i < this->count; i++)
{
char str[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( str, G_ASCII_DTOSTR_BUF_SIZE, this->values[i]);
out->write(" ");
out->write(str);
for (int i = 1; i < this->count; i++)
{
// g_ascii_ version uses C locale always.
g_ascii_formatd(str, G_ASCII_DTOSTR_BUF_SIZE, Util::PRECISION_FORMAT_STRING, this->values[i]);
out->write(" ");
out->write(str);
}
}
}

@ -1,4 +1,5 @@
#include "DoubleAttribute.h"
#include "Util.h"
DoubleAttribute::DoubleAttribute(const char* name, double value) : XMLAttribute(name)
{
@ -17,7 +18,8 @@ void DoubleAttribute::writeOut(OutputStream* out)
XOJ_CHECK_TYPE(DoubleAttribute);
char str[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( str, G_ASCII_DTOSTR_BUF_SIZE, value); // g_ascii_ version uses C locale always.
// g_ascii_ version uses C locale always.
g_ascii_formatd(str, G_ASCII_DTOSTR_BUF_SIZE, Util::PRECISION_FORMAT_STRING, value);
out->write(str);
}

@ -1,3 +1,4 @@
#include "Util.h"
#include "XmlPointNode.h"
XmlPointNode::XmlPointNode(const char* tag)
@ -47,15 +48,10 @@ void XmlPointNode::writeOut(OutputStream* out)
{
out->write(" ");
}
char tmpX[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( tmpX, G_ASCII_DTOSTR_BUF_SIZE, p->x);
char tmpY[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( tmpY, G_ASCII_DTOSTR_BUF_SIZE, p->y);
char* tmp = g_strdup_printf("%s %s", tmpX, tmpY);
out->write(tmp);
g_free(tmp);
gchar coordinateStr[Util::PRECISION_FORMAT_BUFF_LEN];
Util::getCoordinateString(coordinateStr, Util::PRECISION_FORMAT_BUFF_LEN, p->x, p->y);
out->write(coordinateStr);
}
out->write("</");

@ -1,3 +1,4 @@
#include "Util.h"
#include "XmlStrokeNode.h"
XmlStrokeNode::XmlStrokeNode(const char* tag) : XmlNode(tag)
@ -65,17 +66,17 @@ void XmlStrokeNode::writeOut(OutputStream* out)
writeAttributes(out);
out->write(" width=\"");
char tmp[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( tmp, G_ASCII_DTOSTR_BUF_SIZE,width); // g_ascii_ version uses C locale always.
out->write(tmp);
char widthStr[G_ASCII_DTOSTR_BUF_SIZE];
// g_ascii_ version uses C locale always.
g_ascii_formatd(widthStr, G_ASCII_DTOSTR_BUF_SIZE, Util::PRECISION_FORMAT_STRING, width);
out->write(widthStr);
for (int i = 0; i < widthsLength; i++)
{
char tmp[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( tmp, G_ASCII_DTOSTR_BUF_SIZE,widths[i]);
g_ascii_formatd(widthStr, G_ASCII_DTOSTR_BUF_SIZE, Util::PRECISION_FORMAT_STRING, widths[i]);
out->write(" ");
out->write(tmp);
out->write(widthStr);
}
out->write("\"");
@ -88,26 +89,15 @@ void XmlStrokeNode::writeOut(OutputStream* out)
{
out->write(">");
char tmpX[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( tmpX, G_ASCII_DTOSTR_BUF_SIZE, points[0].x);
char tmpY[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( tmpY, G_ASCII_DTOSTR_BUF_SIZE, points[0].y);
char* tmp = g_strdup_printf("%s %s", tmpX, tmpY);
out->write(tmp);
g_free(tmp);
gchar coordinateStr[Util::PRECISION_FORMAT_BUFF_LEN];
Util::getCoordinateString(coordinateStr, Util::PRECISION_FORMAT_BUFF_LEN, points[0].x, points[0].y);
out->write(coordinateStr);
for (int i = 1; i < this->pointsLength; i++)
{
char tmpX[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( tmpX, G_ASCII_DTOSTR_BUF_SIZE, points[i].x);
char tmpY[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr( tmpY, G_ASCII_DTOSTR_BUF_SIZE, points[i].y);
char* tmp = g_strdup_printf("%s %s", tmpX, tmpY);
Util::getCoordinateString(coordinateStr, Util::PRECISION_FORMAT_BUFF_LEN, points[i].x, points[i].y);
out->write(" ");
out->write(tmp);
g_free(tmp);
out->write(coordinateStr);
}
out->write("</");

@ -170,3 +170,8 @@ gboolean Util::paintBackgroundWhite(GtkWidget* widget, cairo_t* cr, void*)
return false;
}
gchar* Util::getCoordinateString(gchar* buff, gulong buffLen, double xVal, double yVal)
{
g_snprintf(buff, buffLen, Util::PRECISION_FORMAT_STRING_XY, xVal, yVal);
return buff;
}

@ -53,6 +53,19 @@ void execInUiThread(std::function<void()>&& callback);
gboolean paintBackgroundWhite(GtkWidget* widget, cairo_t* cr, void* unused);
/**
* Format coordinates to use 8 digits of precision https://m.xkcd.com/2170/
* This function works like g_ascii_formatd in that it stores the result in buff, and
* also returns the result.
*/
extern gchar* getCoordinateString(gchar* buff, gulong buffLen, double xVal, double yVal);
constexpr const gchar* PRECISION_FORMAT_STRING = "%.8f";
constexpr const gchar* PRECISION_FORMAT_STRING_XY = "%.8f %.8f"; // note the space delimiter
constexpr const int PRECISION_FORMAT_BUFF_LEN = G_ASCII_DTOSTR_BUF_SIZE * 2 + 1;
} // namespace Util
static const size_t npos = std::numeric_limits<size_t>::max();
static const size_t npos = std::numeric_limits<size_t>::max();
Loading…
Cancel
Save