2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Real.hh>
|
|
|
|
|
2012-06-27 05:34:15 +02:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF_Real::QPDF_Real(std::string const& val) :
|
2022-08-02 22:57:33 +01:00
|
|
|
QPDFValue(::ot_real, "real"),
|
2008-04-29 12:55:25 +00:00
|
|
|
val(val)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-02 17:14:10 -04:00
|
|
|
QPDF_Real::QPDF_Real(
|
|
|
|
double value, int decimal_places, bool trim_trailing_zeroes) :
|
2022-08-02 22:57:33 +01:00
|
|
|
QPDFValue(::ot_real, "real"),
|
2021-02-12 03:44:12 -05:00
|
|
|
val(QUtil::double_to_string(value, decimal_places, trim_trailing_zeroes))
|
2012-06-27 05:34:15 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:29:23 -04:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 17:45:04 +01:00
|
|
|
QPDF_Real::create(std::string const& val)
|
|
|
|
{
|
|
|
|
return do_create(new QPDF_Real(val));
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:29:23 -04:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 17:45:04 +01:00
|
|
|
QPDF_Real::create(double value, int decimal_places, bool trim_trailing_zeroes)
|
|
|
|
{
|
|
|
|
return do_create(
|
|
|
|
new QPDF_Real(value, decimal_places, trim_trailing_zeroes));
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:29:23 -04:00
|
|
|
std::shared_ptr<QPDFObject>
|
2022-06-16 17:45:04 +01:00
|
|
|
QPDF_Real::shallowCopy()
|
|
|
|
{
|
|
|
|
return create(val);
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_Real::unparse()
|
|
|
|
{
|
|
|
|
return this->val;
|
|
|
|
}
|
|
|
|
|
2018-12-17 17:40:29 -05:00
|
|
|
JSON
|
2022-05-07 07:53:45 -04:00
|
|
|
QPDF_Real::getJSON(int json_version)
|
2018-12-17 17:40:29 -05:00
|
|
|
{
|
2019-03-11 16:21:27 -04:00
|
|
|
// While PDF allows .x or -.x, JSON does not. Rather than
|
2019-04-21 13:07:28 -04:00
|
|
|
// converting from string to double and back, just handle this as a
|
2019-03-11 16:21:27 -04:00
|
|
|
// special case for JSON.
|
|
|
|
std::string result;
|
2022-04-02 17:14:10 -04:00
|
|
|
if (this->val.length() == 0) {
|
2019-03-11 16:21:27 -04:00
|
|
|
// Can't really happen...
|
|
|
|
result = "0";
|
2022-04-02 17:14:10 -04:00
|
|
|
} else if (this->val.at(0) == '.') {
|
2019-03-11 16:21:27 -04:00
|
|
|
result = "0" + this->val;
|
2022-04-02 17:14:10 -04:00
|
|
|
} else if (
|
|
|
|
(this->val.length() >= 2) && (this->val.at(0) == '-') &&
|
|
|
|
(this->val.at(1) == '.')) {
|
2019-03-11 16:21:27 -04:00
|
|
|
result = "-0." + this->val.substr(2);
|
2022-04-02 17:14:10 -04:00
|
|
|
} else {
|
2019-03-11 16:21:27 -04:00
|
|
|
result = this->val;
|
|
|
|
}
|
|
|
|
return JSON::makeNumber(result);
|
2018-12-17 17:40:29 -05:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_Real::getVal()
|
|
|
|
{
|
|
|
|
return this->val;
|
|
|
|
}
|