• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "core/fpdfapi/edit/cpdf_contentstream_write_utils.h"
6 
7 #include <cassert>
8 #include <cfloat>
9 #include <climits>
10 #include <cmath>
11 #include <ostream>
12 
13 #include "core/fxcrt/compiler_specific.h"
14 #include "core/fxcrt/span.h"
15 
16 namespace {
17 
18 constexpr unsigned kMaximumSkFloatToDecimalLength = 49;
19 
20 // Return pow(10.0, e), optimized for common cases.
pow10(int e)21 double pow10(int e) {
22   switch (e) {
23     case 0:
24       return 1.0;  // common cases
25     case 1:
26       return 10.0;
27     case 2:
28       return 100.0;
29     case 3:
30       return 1e+03;
31     case 4:
32       return 1e+04;
33     case 5:
34       return 1e+05;
35     case 6:
36       return 1e+06;
37     case 7:
38       return 1e+07;
39     case 8:
40       return 1e+08;
41     case 9:
42       return 1e+09;
43     case 10:
44       return 1e+10;
45     case 11:
46       return 1e+11;
47     case 12:
48       return 1e+12;
49     case 13:
50       return 1e+13;
51     case 14:
52       return 1e+14;
53     case 15:
54       return 1e+15;
55     default:
56       if (e > 15) {
57         double value = 1e+15;
58         while (e-- > 15) {
59           value *= 10.0;
60         }
61         return value;
62       } else {
63         assert(e < 0);
64         double value = 1.0;
65         while (e++ < 0) {
66           value /= 10.0;
67         }
68         return value;
69       }
70   }
71 }
72 
73 // SkFloatToDecimal
74 //
75 // Convert a float into a decimal string.
76 //
77 // The resulting string will be in the form `[-]?([0-9]*\.)?[0-9]+` (It does
78 // not use scientific notation.) and `sscanf(output, "%f", &x)` will return
79 // the original value if the value is finite. This function accepts all
80 // possible input values.
81 //
82 // INFINITY and -INFINITY are rounded to FLT_MAX and -FLT_MAX.
83 //
84 // NAN values are converted to 0.
85 //
86 // This function will always add a terminating '\0' to the output.
87 //
88 // @param value  Any floating-point number
89 // @param output The buffer to write the string into.  Must be non-null.
90 //
91 // @return strlen(output)
92 //
93 // Write a string into output, including a terminating '\0' (for
94 // unit testing).  Return strlen(output) (for SkWStream::write) The
95 // resulting string will be in the form /[-]?([0-9]*.)?[0-9]+/ and
96 // sscanf(output, "%f", &x) will return the original value iff the
97 // value is finite. This function accepts all possible input values.
98 //
99 // Motivation: "PDF does not support [numbers] in exponential format
100 // (such as 6.02e23)."  Otherwise, this function would rely on a
101 // sprintf-type function from the standard library.
SkFloatToDecimal(float value,pdfium::span<char,kMaximumSkFloatToDecimalLength> output)102 unsigned SkFloatToDecimal(
103     float value,
104     pdfium::span<char, kMaximumSkFloatToDecimalLength> output) {
105   // The longest result is -FLT_MIN.
106   // We serialize it as "-.0000000000000000000000000000000000000117549435"
107   // which has 48 characters plus a terminating '\0'.
108   static_assert(kMaximumSkFloatToDecimalLength == 49, "");
109 
110   // 3 = '-', '.', and '\0' characters.
111   // 9 = number of significant digits
112   // abs(FLT_MIN_10_EXP) = number of zeros in FLT_MIN
113   static_assert(kMaximumSkFloatToDecimalLength == 3 + 9 - FLT_MIN_10_EXP, "");
114 
115   // section C.1 of the PDF 1.4 spec (http://goo.gl/0SCswJ) says that
116   // most PDF rasterizers will use fixed-point scalars that lack the
117   // dynamic range of floats.  Even if this is the case, I want to
118   // serialize these (uncommon) very small and very large scalar
119   // values with enough precision to allow a floating-point
120   // rasterizer to read them in with perfect accuracy.
121   // Experimentally, rasterizers such as pdfium do seem to benefit
122   // from this.  Rasterizers that rely on fixed-point scalars should
123   // gracefully ignore these values that they can not parse.
124   char* output_ptr = output.data();
125 
126   // last(1) leaves space for '\0'.
127   const char* const end = output.last(1u).data();
128 
129   // This function is written to accept any possible input value,
130   // including non-finite values such as INF and NAN.  In that case,
131   // we ignore value-correctness and output a syntacticly-valid
132   // number.
133   if (value == INFINITY) {
134     value = FLT_MAX;  // nearest finite float.
135   }
136   if (value == -INFINITY) {
137     value = -FLT_MAX;  // nearest finite float.
138   }
139   UNSAFE_TODO({
140     if (!std::isfinite(value) || value == 0.0f) {
141       // NAN is unsupported in PDF.  Always output a valid number.
142       // Also catch zero here, as a special case.
143       *output_ptr++ = '0';
144       *output_ptr = '\0';
145       return static_cast<unsigned>(output_ptr - output.data());
146     }
147     if (value < 0.0) {
148       *output_ptr++ = '-';
149       value = -value;
150     }
151     assert(value >= 0.0f);
152 
153     int binaryExponent;
154     (void)std::frexp(value, &binaryExponent);
155     static const double kLog2 = 0.3010299956639812;  // log10(2.0);
156     int decimalExponent = static_cast<int>(std::floor(kLog2 * binaryExponent));
157     int decimalShift = decimalExponent - 8;
158     double power = pow10(-decimalShift);
159     assert(value * power <= (double)INT_MAX);
160     int d = static_cast<int>(value * power + 0.5);
161     // assert(value == (float)(d * pow(10.0, decimalShift)));
162     assert(d <= 999999999);
163     if (d > 167772159) {  // floor(pow(10,1+log10(1<<24)))
164       // need one fewer decimal digits for 24-bit precision.
165       decimalShift = decimalExponent - 7;
166       // assert(power * 0.1 = pow10(-decimalShift));
167       // recalculate to get rounding right.
168       d = static_cast<int>(value * (power * 0.1) + 0.5);
169       assert(d <= 99999999);
170     }
171     while (d % 10 == 0) {
172       d /= 10;
173       ++decimalShift;
174     }
175     assert(d > 0);
176     // assert(value == (float)(d * pow(10.0, decimalShift)));
177     unsigned char buffer[9];  // decimal value buffer.
178     int bufferIndex = 0;
179     do {
180       buffer[bufferIndex++] = d % 10;
181       d /= 10;
182     } while (d != 0);
183     assert(bufferIndex <= (int)sizeof(buffer) && bufferIndex > 0);
184     if (decimalShift >= 0) {
185       do {
186         --bufferIndex;
187         *output_ptr++ = '0' + buffer[bufferIndex];
188       } while (bufferIndex);
189       for (int i = 0; i < decimalShift; ++i) {
190         *output_ptr++ = '0';
191       }
192     } else {
193       int placesBeforeDecimal = bufferIndex + decimalShift;
194       if (placesBeforeDecimal > 0) {
195         while (placesBeforeDecimal-- > 0) {
196           --bufferIndex;
197           *output_ptr++ = '0' + buffer[bufferIndex];
198         }
199         *output_ptr++ = '.';
200       } else {
201         *output_ptr++ = '.';
202         int placesAfterDecimal = -placesBeforeDecimal;
203         while (placesAfterDecimal-- > 0) {
204           *output_ptr++ = '0';
205         }
206       }
207       while (bufferIndex > 0) {
208         --bufferIndex;
209         *output_ptr++ = '0' + buffer[bufferIndex];
210         if (output_ptr == end) {
211           break;  // denormalized: don't need extra precision.
212           // Note: denormalized numbers will not have the same number
213           // of significantDigits, but do not need them to round-trip.
214         }
215       }
216     }
217     assert(output_ptr <= end);
218     *output_ptr = '\0';
219     return static_cast<unsigned>(output_ptr - output.data());
220   });
221 }
222 
223 }  // namespace
224 
WriteFloat(std::ostream & stream,float value)225 std::ostream& WriteFloat(std::ostream& stream, float value) {
226   char buffer[kMaximumSkFloatToDecimalLength];
227   unsigned size = SkFloatToDecimal(value, buffer);
228   stream.write(buffer, size);
229   return stream;
230 }
231 
WriteMatrix(std::ostream & stream,const CFX_Matrix & matrix)232 std::ostream& WriteMatrix(std::ostream& stream, const CFX_Matrix& matrix) {
233   WriteFloat(stream, matrix.a) << " ";
234   WriteFloat(stream, matrix.b) << " ";
235   WriteFloat(stream, matrix.c) << " ";
236   WriteFloat(stream, matrix.d) << " ";
237   WriteFloat(stream, matrix.e) << " ";
238   WriteFloat(stream, matrix.f);
239   return stream;
240 }
241 
WritePoint(std::ostream & stream,const CFX_PointF & point)242 std::ostream& WritePoint(std::ostream& stream, const CFX_PointF& point) {
243   WriteFloat(stream, point.x) << " ";
244   WriteFloat(stream, point.y);
245   return stream;
246 }
247 
WriteRect(std::ostream & stream,const CFX_FloatRect & rect)248 std::ostream& WriteRect(std::ostream& stream, const CFX_FloatRect& rect) {
249   WriteFloat(stream, rect.left) << " ";
250   WriteFloat(stream, rect.bottom) << " ";
251   WriteFloat(stream, rect.Width()) << " ";
252   WriteFloat(stream, rect.Height());
253   return stream;
254 }
255