Commit 075c4b3d authored by Allison Vacanti's avatar Allison Vacanti Committed by Kitware Robot

Merge topic 'fix_pdf_float_truncation'

3e041ca5 Merge branch 'upstream-libharu' into fix_pdf_float_truncation
2d0072d7 libharu 2018-10-04 (a49e5a49)
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: Scott Wittenburg's avatarScott Wittenburg <scott.wittenburg@kitware.com>
Acked-by: Allison Vacanti's avatarAllison Vacanti <allison.vacanti@kitware.com>
Merge-request: !4751
parents d571d53e 3e041ca5
......@@ -32,7 +32,7 @@
/* buffer size which is required when we convert to character string. */
#define HPDF_TMP_BUF_SIZ 512
#define HPDF_SHORT_BUF_SIZ 32
#define HPDF_REAL_LEN 11
#define HPDF_REAL_LEN 64
#define HPDF_INT_LEN 11
#define HPDF_TEXT_DEFAULT_LEN 256
#define HPDF_UNICODE_HEADER_LEN 2
......
......@@ -45,6 +45,12 @@ typedef signed int HPDF_INT;
typedef unsigned int HPDF_UINT;
/* 64bit integer types
*/
typedef signed long long HPDF_INT64;
typedef unsigned long long HPDF_UINT64;
/* 32bit integer types
*/
typedef signed int HPDF_INT32;
......
......@@ -15,6 +15,7 @@
*
*/
#include <math.h>
#include <stdlib.h>
#include "hpdf_utils.h"
#include "hpdf_consts.h"
......@@ -188,6 +189,10 @@ HPDF_FToA (char *s,
char* sptr = s;
char* t;
HPDF_UINT32 i;
HPDF_UINT32 prec;
HPDF_INT32 logVal;
HPDF_REAL roundInc;
HPDF_INT64 fShift;
if (val > HPDF_LIMIT_MAX_REAL)
val = HPDF_LIMIT_MAX_REAL;
......@@ -203,12 +208,24 @@ HPDF_FToA (char *s,
val = -val;
}
/* Compute the decimal precision to write at least 5 significant figures */
logVal = (HPDF_INT32)(val > 1e-20 ? log10(val) : 0.);
if (logVal >= 0) {
prec = 5;
}
else {
prec = -logVal + 5;
}
roundInc = 0.5 * (1. / pow((HPDF_REAL)10, (HPDF_REAL)prec));
fShift = (HPDF_INT64)pow((HPDF_REAL)10, (HPDF_REAL)prec);
/* separate an integer part and a decimal part. */
int_val = (HPDF_INT32)(val + 0.000005);
fpart_val = (HPDF_INT32)((HPDF_REAL)(val - int_val + 0.000005) * 100000);
int_val = (HPDF_INT64)(val + roundInc);
fpart_val = (HPDF_INT64)((HPDF_REAL)(val - int_val + roundInc) * fShift);
/* process decimal part */
for (i = 0; i < 5; i++) {
for (i = 0; i < prec; i++) {
*t = (char)((char)(fpart_val % 10) + '0');
fpart_val /= 10;
t--;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment