5#include "core/fpdfapi/edit/cpdf_contentstream_write_utils.h"
15constexpr unsigned kMaximumSkFloatToDecimalLength = 49;
99unsigned SkFloatToDecimal(
float value,
100 char output[kMaximumSkFloatToDecimalLength]) {
105 static_assert(kMaximumSkFloatToDecimalLength == 49,
"");
109 static_assert(kMaximumSkFloatToDecimalLength == 3 + 9 - FLT_MIN_10_EXP,
"");
120 char* output_ptr = &output[0];
121 const char*
const end = &output[kMaximumSkFloatToDecimalLength - 1];
128 if (value == INFINITY) {
131 if (value == -INFINITY) {
134 if (!
std::isfinite(value) || value == 0.0f) {
139 return static_cast<
unsigned>(output_ptr - output);
148 (
void)
std::frexp(value, &binaryExponent);
149 static const double kLog2 = 0.3010299956639812;
150 int decimalExponent =
static_cast<
int>(
std::floor(kLog2 * binaryExponent));
151 int decimalShift = decimalExponent - 8;
152 double power = pow10(-decimalShift);
153 assert(value * power <= (
double)INT_MAX);
154 int d =
static_cast<
int>(value * power + 0.5);
159 decimalShift = decimalExponent - 7;
162 d =
static_cast<
int>(value * (power * 0.1) + 0.5);
165 while (d % 10 == 0) {
171 unsigned char buffer[9];
174 buffer[bufferIndex++] = d % 10;
177 assert(bufferIndex <= (
int)
sizeof(buffer) && bufferIndex > 0);
178 if (decimalShift >= 0) {
181 *output_ptr++ =
'0' + buffer[bufferIndex];
182 }
while (bufferIndex);
183 for (
int i = 0; i < decimalShift; ++i) {
187 int placesBeforeDecimal = bufferIndex + decimalShift;
188 if (placesBeforeDecimal > 0) {
189 while (placesBeforeDecimal-- > 0) {
191 *output_ptr++ =
'0' + buffer[bufferIndex];
196 int placesAfterDecimal = -placesBeforeDecimal;
197 while (placesAfterDecimal-- > 0) {
201 while (bufferIndex > 0) {
203 *output_ptr++ =
'0' + buffer[bufferIndex];
204 if (output_ptr == end) {
211 assert(output_ptr <= end);
213 return static_cast<
unsigned>(output_ptr - output);
219 char buffer[kMaximumSkFloatToDecimalLength];
220 unsigned size = SkFloatToDecimal(value, buffer);
221 stream.write(buffer, size);
236 WriteFloat(stream, point.x) <<
" ";
237 WriteFloat(stream, point.y);
std::ostream & WritePoint(std::ostream &stream, const CFX_PointF &point)
std::ostream & WriteFloat(std::ostream &stream, float value)
std::ostream & WriteRect(std::ostream &stream, const CFX_FloatRect &rect)
std::ostream & WriteMatrix(std::ostream &stream, const CFX_Matrix &matrix)