1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkFloatToDecimal.h"
9
10 #include <cfloat>
11 #include <climits>
12 #include <cmath>
13
14 //#include "SkTypes.h"
15 #include <cassert>
16 #define SkASSERT assert
17
18 namespace pdfium {
19 namespace skia {
20 namespace {
21
22 // Return pow(10.0, e), optimized for common cases.
pow10(int e)23 double pow10(int e) {
24 switch (e) {
25 case 0: return 1.0; // common cases
26 case 1: return 10.0;
27 case 2: return 100.0;
28 case 3: return 1e+03;
29 case 4: return 1e+04;
30 case 5: return 1e+05;
31 case 6: return 1e+06;
32 case 7: return 1e+07;
33 case 8: return 1e+08;
34 case 9: return 1e+09;
35 case 10: return 1e+10;
36 case 11: return 1e+11;
37 case 12: return 1e+12;
38 case 13: return 1e+13;
39 case 14: return 1e+14;
40 case 15: return 1e+15;
41 default:
42 if (e > 15) {
43 double value = 1e+15;
44 while (e-- > 15) { value *= 10.0; }
45 return value;
46 } else {
47 SkASSERT(e < 0);
48 double value = 1.0;
49 while (e++ < 0) { value /= 10.0; }
50 return value;
51 }
52 }
53 }
54
55 } // namespace
56
57 /** Write a string into output, including a terminating '\0' (for
58 unit testing). Return strlen(output) (for SkWStream::write) The
59 resulting string will be in the form /[-]?([0-9]*.)?[0-9]+/ and
60 sscanf(output, "%f", &x) will return the original value iff the
61 value is finite. This function accepts all possible input values.
62
63 Motivation: "PDF does not support [numbers] in exponential format
64 (such as 6.02e23)." Otherwise, this function would rely on a
65 sprintf-type function from the standard library. */
SkFloatToDecimal(float value,char output[kMaximumSkFloatToDecimalLength])66 unsigned SkFloatToDecimal(float value, char output[kMaximumSkFloatToDecimalLength]) {
67 /* The longest result is -FLT_MIN.
68 We serialize it as "-.0000000000000000000000000000000000000117549435"
69 which has 48 characters plus a terminating '\0'. */
70
71 static_assert(kMaximumSkFloatToDecimalLength == 49, "");
72 // 3 = '-', '.', and '\0' characters.
73 // 9 = number of significant digits
74 // abs(FLT_MIN_10_EXP) = number of zeros in FLT_MIN
75 static_assert(kMaximumSkFloatToDecimalLength == 3 + 9 - FLT_MIN_10_EXP, "");
76
77 /* section C.1 of the PDF1.4 spec (http://goo.gl/0SCswJ) says that
78 most PDF rasterizers will use fixed-point scalars that lack the
79 dynamic range of floats. Even if this is the case, I want to
80 serialize these (uncommon) very small and very large scalar
81 values with enough precision to allow a floating-point
82 rasterizer to read them in with perfect accuracy.
83 Experimentally, rasterizers such as pdfium do seem to benefit
84 from this. Rasterizers that rely on fixed-point scalars should
85 gracefully ignore these values that they can not parse. */
86 char* output_ptr = &output[0];
87 const char* const end = &output[kMaximumSkFloatToDecimalLength - 1];
88 // subtract one to leave space for '\0'.
89
90 /* This function is written to accept any possible input value,
91 including non-finite values such as INF and NAN. In that case,
92 we ignore value-correctness and output a syntacticly-valid
93 number. */
94 if (value == INFINITY) {
95 value = FLT_MAX; // nearest finite float.
96 }
97 if (value == -INFINITY) {
98 value = -FLT_MAX; // nearest finite float.
99 }
100 if (!std::isfinite(value) || value == 0.0f) {
101 // NAN is unsupported in PDF. Always output a valid number.
102 // Also catch zero here, as a special case.
103 *output_ptr++ = '0';
104 *output_ptr = '\0';
105 return static_cast<unsigned>(output_ptr - output);
106 }
107 if (value < 0.0) {
108 *output_ptr++ = '-';
109 value = -value;
110 }
111 SkASSERT(value >= 0.0f);
112
113 int binaryExponent;
114 (void)std::frexp(value, &binaryExponent);
115 static const double kLog2 = 0.3010299956639812; // log10(2.0);
116 int decimalExponent = static_cast<int>(std::floor(kLog2 * binaryExponent));
117 int decimalShift = decimalExponent - 8;
118 double power = pow10(-decimalShift);
119 SkASSERT(value * power <= (double)INT_MAX);
120 int d = static_cast<int>(value * power + 0.5);
121 // SkASSERT(value == (float)(d * pow(10.0, decimalShift)));
122 SkASSERT(d <= 999999999);
123 if (d > 167772159) { // floor(pow(10,1+log10(1<<24)))
124 // need one fewer decimal digits for 24-bit precision.
125 decimalShift = decimalExponent - 7;
126 // SkASSERT(power * 0.1 = pow10(-decimalShift));
127 // recalculate to get rounding right.
128 d = static_cast<int>(value * (power * 0.1) + 0.5);
129 SkASSERT(d <= 99999999);
130 }
131 while (d % 10 == 0) {
132 d /= 10;
133 ++decimalShift;
134 }
135 SkASSERT(d > 0);
136 // SkASSERT(value == (float)(d * pow(10.0, decimalShift)));
137 unsigned char buffer[9]; // decimal value buffer.
138 int bufferIndex = 0;
139 do {
140 buffer[bufferIndex++] = d % 10;
141 d /= 10;
142 } while (d != 0);
143 SkASSERT(bufferIndex <= (int)sizeof(buffer) && bufferIndex > 0);
144 if (decimalShift >= 0) {
145 do {
146 --bufferIndex;
147 *output_ptr++ = '0' + buffer[bufferIndex];
148 } while (bufferIndex);
149 for (int i = 0; i < decimalShift; ++i) {
150 *output_ptr++ = '0';
151 }
152 } else {
153 int placesBeforeDecimal = bufferIndex + decimalShift;
154 if (placesBeforeDecimal > 0) {
155 while (placesBeforeDecimal-- > 0) {
156 --bufferIndex;
157 *output_ptr++ = '0' + buffer[bufferIndex];
158 }
159 *output_ptr++ = '.';
160 } else {
161 *output_ptr++ = '.';
162 int placesAfterDecimal = -placesBeforeDecimal;
163 while (placesAfterDecimal-- > 0) {
164 *output_ptr++ = '0';
165 }
166 }
167 while (bufferIndex > 0) {
168 --bufferIndex;
169 *output_ptr++ = '0' + buffer[bufferIndex];
170 if (output_ptr == end) {
171 break; // denormalized: don't need extra precision.
172 // Note: denormalized numbers will not have the same number of
173 // significantDigits, but do not need them to round-trip.
174 }
175 }
176 }
177 SkASSERT(output_ptr <= end);
178 *output_ptr = '\0';
179 return static_cast<unsigned>(output_ptr - output);
180 }
181 } // namespace skia
182 } // namespace pdfium
183