2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-12 07:46:28 +00:00
qpdf/libqpdf/QPDF_Real.cc

60 lines
1.4 KiB
C++
Raw Normal View History

#include <qpdf/QPDF_Real.hh>
2012-06-27 03:34:15 +00:00
#include <qpdf/QUtil.hh>
QPDF_Real::QPDF_Real(std::string const& val) :
QPDFValue(::ot_real, "real"),
val(val)
{
}
2023-05-21 17:35:09 +00:00
QPDF_Real::QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes) :
QPDFValue(::ot_real, "real"),
val(QUtil::double_to_string(value, decimal_places, trim_trailing_zeroes))
2012-06-27 03:34:15 +00:00
{
}
std::shared_ptr<QPDFObject>
QPDF_Real::create(std::string const& val)
{
return do_create(new QPDF_Real(val));
}
std::shared_ptr<QPDFObject>
QPDF_Real::create(double value, int decimal_places, bool trim_trailing_zeroes)
{
2023-05-21 17:35:09 +00:00
return do_create(new QPDF_Real(value, decimal_places, trim_trailing_zeroes));
}
std::shared_ptr<QPDFObject>
QPDF_Real::copy(bool shallow)
{
return create(val);
}
std::string
QPDF_Real::unparse()
{
return this->val;
}
2018-12-17 22:40:29 +00:00
JSON
QPDF_Real::getJSON(int json_version)
2018-12-17 22:40:29 +00:00
{
// While PDF allows .x or -.x, JSON does not. Rather than
2019-04-21 17:07:28 +00:00
// converting from string to double and back, just handle this as a
// special case for JSON.
std::string result;
if (this->val.length() == 0) {
// Can't really happen...
result = "0";
} else if (this->val.at(0) == '.') {
result = "0" + this->val;
2023-05-21 17:35:09 +00:00
} else if ((this->val.length() >= 2) && (this->val.at(0) == '-') && (this->val.at(1) == '.')) {
result = "-0." + this->val.substr(2);
} else {
result = this->val;
}
return JSON::makeNumber(result);
2018-12-17 22:40:29 +00:00
}