diff options
author | Hal Canary <halcanary@google.com> | 2018-01-02 16:25:53 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-01-03 19:59:31 +0000 |
commit | 3c36ef6be93400368fee3ea2e0dca6cff4972bb0 (patch) | |
tree | 69ce7be26f6771d4fcb3d1da3bc979fc0120285f | |
parent | 0d825666f79f4d9baea489b21506da3163f73aa9 (diff) |
SkFloatToDecimal moved to src/utils
This change stages SkFloatToDecimal() for possible re-use by pdfium.
Change-Id: Iedc0c78c8a633f0b0973365d2d8b540b5443590d
Reviewed-on: https://skia-review.googlesource.com/90400
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Cary Clark <caryclark@google.com>
-rw-r--r-- | bench/PDFBench.cpp | 39 | ||||
-rw-r--r-- | gn/utils.gni | 2 | ||||
-rw-r--r-- | src/pdf/SkPDFUtils.cpp | 159 | ||||
-rw-r--r-- | src/pdf/SkPDFUtils.h | 16 | ||||
-rw-r--r-- | src/utils/SkFloatToDecimal.cpp | 172 | ||||
-rw-r--r-- | src/utils/SkFloatToDecimal.h | 34 | ||||
-rw-r--r-- | tests/PDFPrimitivesTest.cpp | 4 |
7 files changed, 238 insertions, 188 deletions
diff --git a/bench/PDFBench.cpp b/bench/PDFBench.cpp index 0dd3fa1eb8..599539026f 100644 --- a/bench/PDFBench.cpp +++ b/bench/PDFBench.cpp @@ -10,6 +10,7 @@ #include "Resources.h" #include "SkAutoPixmapStorage.h" #include "SkData.h" +#include "SkFloatToDecimal.h" #include "SkGradientShader.h" #include "SkImage.h" #include "SkPixmap.h" @@ -36,12 +37,30 @@ struct WStreamWriteTextBenchmark : public Benchmark { DEF_BENCH(return new WStreamWriteTextBenchmark;) +// Test speed of SkFloatToDecimal for typical floats that +// might be found in a PDF document. +struct PDFScalarBench : public Benchmark { + bool isSuitableFor(Backend b) override { + return b == kNonRendering_Backend; + } + const char* onGetName() override { return "PDFScalar"; } + void onDraw(int loops, SkCanvas*) override { + SkRandom random; + char dst[kMaximumSkFloatToDecimalLength]; + while (loops-- > 0) { + auto f = random.nextRangeF(-500.0f, 1500.0f); + (void)SkFloatToDecimal(f, dst); + } + } +}; + +DEF_BENCH(return new PDFScalarBench;) + #ifdef SK_SUPPORT_PDF #include "SkPDFBitmap.h" #include "SkPDFDocument.h" #include "SkPDFShader.h" -#include "SkPDFUtils.h" namespace { static void test_pdf_object_serialization(const sk_sp<SkPDFObject> object) { @@ -164,23 +183,6 @@ private: std::unique_ptr<SkStreamAsset> fAsset; }; -// Test speed of SkPDFUtils::FloatToDecimal for typical floats that -// might be found in a PDF document. -struct PDFScalarBench : public Benchmark { - bool isSuitableFor(Backend b) override { - return b == kNonRendering_Backend; - } - const char* onGetName() override { return "PDFScalar"; } - void onDraw(int loops, SkCanvas*) override { - SkRandom random; - char dst[SkPDFUtils::kMaximumFloatDecimalLength]; - while (loops-- > 0) { - auto f = random.nextRangeF(-500.0f, 1500.0f); - (void)SkPDFUtils::FloatToDecimal(f, dst); - } - } -}; - struct PDFColorComponentBench : public Benchmark { bool isSuitableFor(Backend b) override { return b == kNonRendering_Backend; @@ -244,7 +246,6 @@ struct WritePDFTextBenchmark : public Benchmark { DEF_BENCH(return new PDFImageBench;) DEF_BENCH(return new PDFJpegImageBench;) DEF_BENCH(return new PDFCompressionBench;) -DEF_BENCH(return new PDFScalarBench;) DEF_BENCH(return new PDFColorComponentBench;) DEF_BENCH(return new PDFShaderBench;) DEF_BENCH(return new WritePDFTextBenchmark;) diff --git a/gn/utils.gni b/gn/utils.gni index e8d624d88d..59f665c314 100644 --- a/gn/utils.gni +++ b/gn/utils.gni @@ -38,6 +38,8 @@ skia_utils_sources = [ "$_src/utils/SkDashPathPriv.h", "$_src/utils/SkDumpCanvas.cpp", "$_src/utils/SkEventTracer.cpp", + "$_src/utils/SkFloatToDecimal.cpp", + "$_src/utils/SkFloatToDecimal.h", "$_src/utils/SkFloatUtils.h", "$_src/utils/SkInsetConvexPolygon.cpp", "$_src/utils/SkInsetConvexPolygon.h", diff --git a/src/pdf/SkPDFUtils.cpp b/src/pdf/SkPDFUtils.cpp index c9f5806211..c225fbda7a 100644 --- a/src/pdf/SkPDFUtils.cpp +++ b/src/pdf/SkPDFUtils.cpp @@ -287,165 +287,6 @@ size_t SkPDFUtils::ColorToDecimal(uint8_t value, char result[5]) { return j + 1; } -void SkPDFUtils::AppendScalar(SkScalar value, SkWStream* stream) { - char result[kMaximumFloatDecimalLength]; - size_t len = SkPDFUtils::FloatToDecimal(SkScalarToFloat(value), result); - SkASSERT(len < kMaximumFloatDecimalLength); - stream->write(result, len); -} - -// Return pow(10.0, e), optimized for common cases. -inline double pow10(int e) { - switch (e) { - case 0: return 1.0; // common cases - case 1: return 10.0; - case 2: return 100.0; - case 3: return 1e+03; - case 4: return 1e+04; - case 5: return 1e+05; - case 6: return 1e+06; - case 7: return 1e+07; - case 8: return 1e+08; - case 9: return 1e+09; - case 10: return 1e+10; - case 11: return 1e+11; - case 12: return 1e+12; - case 13: return 1e+13; - case 14: return 1e+14; - case 15: return 1e+15; - default: - if (e > 15) { - double value = 1e+15; - while (e-- > 15) { value *= 10.0; } - return value; - } else { - SkASSERT(e < 0); - double value = 1.0; - while (e++ < 0) { value /= 10.0; } - return value; - } - } -} - -/** Write a string into result, includeing a terminating '\0' (for - unit testing). Return strlen(result) (for SkWStream::write) The - resulting string will be in the form /[-]?([0-9]*.)?[0-9]+/ and - sscanf(result, "%f", &x) will return the original value iff the - value is finite. This function accepts all possible input values. - - Motivation: "PDF does not support [numbers] in exponential format - (such as 6.02e23)." Otherwise, this function would rely on a - sprintf-type function from the standard library. */ -size_t SkPDFUtils::FloatToDecimal(float value, - char result[kMaximumFloatDecimalLength]) { - /* The longest result is -FLT_MIN. - We serialize it as "-.0000000000000000000000000000000000000117549435" - which has 48 characters plus a terminating '\0'. */ - - /* section C.1 of the PDF1.4 spec (http://goo.gl/0SCswJ) says that - most PDF rasterizers will use fixed-point scalars that lack the - dynamic range of floats. Even if this is the case, I want to - serialize these (uncommon) very small and very large scalar - values with enough precision to allow a floating-point - rasterizer to read them in with perfect accuracy. - Experimentally, rasterizers such as pdfium do seem to benefit - from this. Rasterizers that rely on fixed-point scalars should - gracefully ignore these values that they can not parse. */ - char* output = &result[0]; - const char* const end = &result[kMaximumFloatDecimalLength - 1]; - // subtract one to leave space for '\0'. - - /* This function is written to accept any possible input value, - including non-finite values such as INF and NAN. In that case, - we ignore value-correctness and and output a syntacticly-valid - number. */ - if (value == SK_FloatInfinity) { - value = FLT_MAX; // nearest finite float. - } - if (value == SK_FloatNegativeInfinity) { - value = -FLT_MAX; // nearest finite float. - } - if (!std::isfinite(value) || value == 0.0f) { - // NAN is unsupported in PDF. Always output a valid number. - // Also catch zero here, as a special case. - *output++ = '0'; - *output = '\0'; - return output - result; - } - if (value < 0.0) { - *output++ = '-'; - value = -value; - } - SkASSERT(value >= 0.0f); - - int binaryExponent; - (void)std::frexp(value, &binaryExponent); - static const double kLog2 = 0.3010299956639812; // log10(2.0); - int decimalExponent = static_cast<int>(std::floor(kLog2 * binaryExponent)); - int decimalShift = decimalExponent - 8; - double power = pow10(-decimalShift); - int32_t d = static_cast<int32_t>(value * power + 0.5); - // SkASSERT(value == (float)(d * pow(10.0, decimalShift))); - SkASSERT(d <= 999999999); - if (d > 167772159) { // floor(pow(10,1+log10(1<<24))) - // need one fewer decimal digits for 24-bit precision. - decimalShift = decimalExponent - 7; - // SkASSERT(power * 0.1 = pow10(-decimalShift)); - // recalculate to get rounding right. - d = static_cast<int32_t>(value * (power * 0.1) + 0.5); - SkASSERT(d <= 99999999); - } - while (d % 10 == 0) { - d /= 10; - ++decimalShift; - } - SkASSERT(d > 0); - // SkASSERT(value == (float)(d * pow(10.0, decimalShift))); - uint8_t buffer[9]; // decimal value buffer. - int bufferIndex = 0; - do { - buffer[bufferIndex++] = d % 10; - d /= 10; - } while (d != 0); - SkASSERT(bufferIndex <= (int)sizeof(buffer) && bufferIndex > 0); - if (decimalShift >= 0) { - do { - --bufferIndex; - *output++ = '0' + buffer[bufferIndex]; - } while (bufferIndex); - for (int i = 0; i < decimalShift; ++i) { - *output++ = '0'; - } - } else { - int placesBeforeDecimal = bufferIndex + decimalShift; - if (placesBeforeDecimal > 0) { - while (placesBeforeDecimal-- > 0) { - --bufferIndex; - *output++ = '0' + buffer[bufferIndex]; - } - *output++ = '.'; - } else { - *output++ = '.'; - int placesAfterDecimal = -placesBeforeDecimal; - while (placesAfterDecimal-- > 0) { - *output++ = '0'; - } - } - while (bufferIndex > 0) { - --bufferIndex; - *output++ = '0' + buffer[bufferIndex]; - if (output == end) { - break; // denormalized: don't need extra precision. - // Note: denormalized numbers will not have the same number of - // significantDigits, but do not need them to round-trip. - } - } - } - SkASSERT(output <= end); - *output = '\0'; - return output - result; -} - void SkPDFUtils::WriteString(SkWStream* wStream, const char* cin, size_t len) { SkDEBUGCODE(static const size_t kMaxLen = 65535;) SkASSERT(len <= kMaxLen); diff --git a/src/pdf/SkPDFUtils.h b/src/pdf/SkPDFUtils.h index 27d0a3adbe..a291914a5a 100644 --- a/src/pdf/SkPDFUtils.h +++ b/src/pdf/SkPDFUtils.h @@ -7,6 +7,7 @@ #ifndef SkPDFUtils_DEFINED #define SkPDFUtils_DEFINED +#include "SkFloatToDecimal.h" #include "SkPDFTypes.h" #include "SkPaint.h" #include "SkPath.h" @@ -76,14 +77,13 @@ inline void AppendColorComponent(uint8_t value, SkWStream* wStream) { wStream->write(buffer, len); } -// 3 = '-', '.', and '\0' characters. -// 9 = number of significant digits -// abs(FLT_MIN_10_EXP) = number of zeros in FLT_MIN -const size_t kMaximumFloatDecimalLength = 3 + 9 - FLT_MIN_10_EXP; -// FloatToDecimal is exposed for unit tests. -size_t FloatToDecimal(float value, - char output[kMaximumFloatDecimalLength]); -void AppendScalar(SkScalar value, SkWStream* stream); +inline void AppendScalar(SkScalar value, SkWStream* stream) { + char result[kMaximumSkFloatToDecimalLength]; + size_t len = SkFloatToDecimal(SkScalarToFloat(value), result); + SkASSERT(len < kMaximumSkFloatToDecimalLength); + stream->write(result, len); +} + void WriteString(SkWStream* wStream, const char* input, size_t len); inline void WriteUInt16BE(SkDynamicMemoryWStream* wStream, uint16_t value) { diff --git a/src/utils/SkFloatToDecimal.cpp b/src/utils/SkFloatToDecimal.cpp new file mode 100644 index 0000000000..273175a897 --- /dev/null +++ b/src/utils/SkFloatToDecimal.cpp @@ -0,0 +1,172 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkFloatToDecimal.h" + +#include <cfloat> +#include <climits> +#include <cmath> + +#include "SkTypes.h" + +// Return pow(10.0, e), optimized for common cases. +static double pow10(int e) { + switch (e) { + case 0: return 1.0; // common cases + case 1: return 10.0; + case 2: return 100.0; + case 3: return 1e+03; + case 4: return 1e+04; + case 5: return 1e+05; + case 6: return 1e+06; + case 7: return 1e+07; + case 8: return 1e+08; + case 9: return 1e+09; + case 10: return 1e+10; + case 11: return 1e+11; + case 12: return 1e+12; + case 13: return 1e+13; + case 14: return 1e+14; + case 15: return 1e+15; + default: + if (e > 15) { + double value = 1e+15; + while (e-- > 15) { value *= 10.0; } + return value; + } else { + SkASSERT(e < 0); + double value = 1.0; + while (e++ < 0) { value /= 10.0; } + return value; + } + } +} + +/** Write a string into result, includeing a terminating '\0' (for + unit testing). Return strlen(result) (for SkWStream::write) The + resulting string will be in the form /[-]?([0-9]*.)?[0-9]+/ and + sscanf(result, "%f", &x) will return the original value iff the + value is finite. This function accepts all possible input values. + + Motivation: "PDF does not support [numbers] in exponential format + (such as 6.02e23)." Otherwise, this function would rely on a + sprintf-type function from the standard library. */ +unsigned SkFloatToDecimal(float value, char result[kMaximumSkFloatToDecimalLength]) { + /* The longest result is -FLT_MIN. + We serialize it as "-.0000000000000000000000000000000000000117549435" + which has 48 characters plus a terminating '\0'. */ + + static_assert(kMaximumSkFloatToDecimalLength == 49, ""); + // 3 = '-', '.', and '\0' characters. + // 9 = number of significant digits + // abs(FLT_MIN_10_EXP) = number of zeros in FLT_MIN + static_assert(kMaximumSkFloatToDecimalLength == 3 + 9 - FLT_MIN_10_EXP, ""); + + /* section C.1 of the PDF1.4 spec (http://goo.gl/0SCswJ) says that + most PDF rasterizers will use fixed-point scalars that lack the + dynamic range of floats. Even if this is the case, I want to + serialize these (uncommon) very small and very large scalar + values with enough precision to allow a floating-point + rasterizer to read them in with perfect accuracy. + Experimentally, rasterizers such as pdfium do seem to benefit + from this. Rasterizers that rely on fixed-point scalars should + gracefully ignore these values that they can not parse. */ + char* output = &result[0]; + const char* const end = &result[kMaximumSkFloatToDecimalLength - 1]; + // subtract one to leave space for '\0'. + + /* This function is written to accept any possible input value, + including non-finite values such as INF and NAN. In that case, + we ignore value-correctness and and output a syntacticly-valid + number. */ + if (value == INFINITY) { + value = FLT_MAX; // nearest finite float. + } + if (value == -INFINITY) { + value = -FLT_MAX; // nearest finite float. + } + if (!std::isfinite(value) || value == 0.0f) { + // NAN is unsupported in PDF. Always output a valid number. + // Also catch zero here, as a special case. + *output++ = '0'; + *output = '\0'; + return static_cast<unsigned>(output - result); + } + if (value < 0.0) { + *output++ = '-'; + value = -value; + } + SkASSERT(value >= 0.0f); + + int binaryExponent; + (void)std::frexp(value, &binaryExponent); + static const double kLog2 = 0.3010299956639812; // log10(2.0); + int decimalExponent = static_cast<int>(std::floor(kLog2 * binaryExponent)); + int decimalShift = decimalExponent - 8; + double power = pow10(-decimalShift); + SkASSERT(value * power <= (double)INT_MAX); + int d = static_cast<int>(value * power + 0.5); + // SkASSERT(value == (float)(d * pow(10.0, decimalShift))); + SkASSERT(d <= 999999999); + if (d > 167772159) { // floor(pow(10,1+log10(1<<24))) + // need one fewer decimal digits for 24-bit precision. + decimalShift = decimalExponent - 7; + // SkASSERT(power * 0.1 = pow10(-decimalShift)); + // recalculate to get rounding right. + d = static_cast<int>(value * (power * 0.1) + 0.5); + SkASSERT(d <= 99999999); + } + while (d % 10 == 0) { + d /= 10; + ++decimalShift; + } + SkASSERT(d > 0); + // SkASSERT(value == (float)(d * pow(10.0, decimalShift))); + unsigned char buffer[9]; // decimal value buffer. + int bufferIndex = 0; + do { + buffer[bufferIndex++] = d % 10; + d /= 10; + } while (d != 0); + SkASSERT(bufferIndex <= (int)sizeof(buffer) && bufferIndex > 0); + if (decimalShift >= 0) { + do { + --bufferIndex; + *output++ = '0' + buffer[bufferIndex]; + } while (bufferIndex); + for (int i = 0; i < decimalShift; ++i) { + *output++ = '0'; + } + } else { + int placesBeforeDecimal = bufferIndex + decimalShift; + if (placesBeforeDecimal > 0) { + while (placesBeforeDecimal-- > 0) { + --bufferIndex; + *output++ = '0' + buffer[bufferIndex]; + } + *output++ = '.'; + } else { + *output++ = '.'; + int placesAfterDecimal = -placesBeforeDecimal; + while (placesAfterDecimal-- > 0) { + *output++ = '0'; + } + } + while (bufferIndex > 0) { + --bufferIndex; + *output++ = '0' + buffer[bufferIndex]; + if (output == end) { + break; // denormalized: don't need extra precision. + // Note: denormalized numbers will not have the same number of + // significantDigits, but do not need them to round-trip. + } + } + } + SkASSERT(output <= end); + *output = '\0'; + return static_cast<unsigned>(output - result); +} diff --git a/src/utils/SkFloatToDecimal.h b/src/utils/SkFloatToDecimal.h new file mode 100644 index 0000000000..ac1042dbfb --- /dev/null +++ b/src/utils/SkFloatToDecimal.h @@ -0,0 +1,34 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkFloatToDecimal_DEFINED +#define SkFloatToDecimal_DEFINED + +constexpr unsigned kMaximumSkFloatToDecimalLength = 49; + +/** \fn SkFloatToDecimal + Convert a float into a decimal string. + + The resulting string will be in the form `[-]?([0-9]*\.)?[0-9]+` (It does + not use scientific notation.) and `sscanf(output, "%f", &x)` will return + the original value if the value is finite. This function accepts all + possible input values. + + INFINITY and -INFINITY are rounded to FLT_MAX and -FLT_MAX. + + NAN values are converted to 0. + + This function will always add a terminating '\0' to the output. + + @param value Any floating-point number + @param output The buffer to write the string into. Must be non-null. + + @return strlen(output) +*/ +unsigned SkFloatToDecimal(float value, char output[kMaximumSkFloatToDecimalLength]); + +#endif // SkFloatToDecimal_DEFINED diff --git a/tests/PDFPrimitivesTest.cpp b/tests/PDFPrimitivesTest.cpp index 19e403530f..34eada94c2 100644 --- a/tests/PDFPrimitivesTest.cpp +++ b/tests/PDFPrimitivesTest.cpp @@ -436,8 +436,8 @@ DEF_TEST(SkPDF_FontCanEmbedTypeface, reporter) { // test to see that all finite scalars round trip via scanf(). static void check_pdf_scalar_serialization( skiatest::Reporter* reporter, float inputFloat) { - char floatString[SkPDFUtils::kMaximumFloatDecimalLength]; - size_t len = SkPDFUtils::FloatToDecimal(inputFloat, floatString); + char floatString[kMaximumSkFloatToDecimalLength]; + size_t len = SkFloatToDecimal(inputFloat, floatString); if (len >= sizeof(floatString)) { ERRORF(reporter, "string too long: %u", (unsigned)len); return; |