| 1 |
/* |
| 2 |
* Copyright 2011 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 |
#ifndef SkPDFUtils_DEFINED |
| 8 |
#define SkPDFUtils_DEFINED |
| 9 |
|
| 10 |
#include "include/core/SkMatrix.h" |
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkPathTypes.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkScalar.h" |
| 15 |
#include "include/core/SkStream.h" |
| 16 |
#include "include/private/base/SkAssert.h" |
| 17 |
#include "include/private/base/SkDebug.h" |
| 18 |
#include "src/base/SkUTF.h" |
| 19 |
#include "src/base/SkUtils.h" |
| 20 |
#include "src/shaders/SkShaderBase.h" |
| 21 |
#include "src/utils/SkFloatToDecimal.h" |
| 22 |
|
| 23 |
#include <cstdint> |
| 24 |
#include <cstring> |
| 25 |
#include <memory> |
| 26 |
|
| 27 |
class SkBitmap; |
| 28 |
class SkImage; |
| 29 |
class SkPDFArray; |
| 30 |
class SkPDFDict; |
| 31 |
class SkPath; |
| 32 |
class SkShader; |
| 33 |
enum class SkBlendMode; |
| 34 |
struct SkRect; |
| 35 |
|
| 36 |
namespace SkPDF { struct DateTime; } |
| 37 |
|
| 38 |
template <typename T> |
| 39 |
bool SkPackedArrayEqual(T* u, T* v, size_t n) { |
| 40 |
SkASSERT(u); |
| 41 |
SkASSERT(v); |
| 42 |
return 0 == memcmp(u, v, n * sizeof(T)); |
| 43 |
} |
| 44 |
|
| 45 |
#if 0 |
| 46 |
#define PRINT_NOT_IMPL(str) fprintf(stderr, str) |
| 47 |
#else |
| 48 |
#define PRINT_NOT_IMPL(str) |
| 49 |
#endif |
| 50 |
|
| 51 |
#define NOT_IMPLEMENTED(condition, assertion) \ |
| 52 |
do { \ |
| 53 |
if ((bool)(condition)) { \ |
| 54 |
PRINT_NOT_IMPL("NOT_IMPLEMENTED: " #condition "\n"); \ |
| 55 |
SkDEBUGCODE(SkASSERT(!assertion);) \ |
| 56 |
} \ |
| 57 |
} while (0) |
| 58 |
|
| 59 |
namespace SkPDFUtils { |
| 60 |
|
| 61 |
const char* BlendModeName(SkBlendMode); |
| 62 |
|
| 63 |
std::unique_ptr<SkPDFArray> RectToArray(const SkRect& rect); |
| 64 |
std::unique_ptr<SkPDFArray> MatrixToArray(const SkMatrix& matrix); |
| 65 |
|
| 66 |
void MoveTo(SkScalar x, SkScalar y, SkWStream* content); |
| 67 |
void AppendLine(SkScalar x, SkScalar y, SkWStream* content); |
| 68 |
void AppendRectangle(const SkRect& rect, SkWStream* content); |
| 69 |
enum class EmptyPath : bool { Discard, Preserve }; |
| 70 |
enum class EmptyVerb : bool { Discard, Preserve }; |
| 71 |
[[nodiscard]] bool EmitPath(const SkPath&, SkPaint::Style, EmptyPath, EmptyVerb, |
| 72 |
SkWStream* content, SkScalar tolerance = 0.25f); |
| 73 |
void ClosePath(SkWStream* content); |
| 74 |
void PaintPath(SkPaint::Style style, SkPathFillType fill, SkWStream* content); |
| 75 |
void StrokePath(SkWStream* content); |
| 76 |
void ApplyGraphicState(int objectIndex, SkWStream* content); |
| 77 |
void ApplyPattern(int objectIndex, SkWStream* content); |
| 78 |
|
| 79 |
// Converts (value / 255.0) with three significant digits of accuracy. |
| 80 |
// Writes value as string into result. Returns strlen() of result. |
| 81 |
size_t ColorToDecimal(uint8_t value, char result[5]); |
| 82 |
|
| 83 |
static constexpr unsigned kFloatColorDecimalCount = 4; |
| 84 |
size_t ColorToDecimalF(float value, char (&result)[kFloatColorDecimalCount + 2]); |
| 85 |
inline void AppendColorComponent(uint8_t value, SkWStream* wStream) { |
| 86 |
char buffer[5]; |
| 87 |
size_t len = SkPDFUtils::ColorToDecimal(value, buffer); |
| 88 |
wStream->write(buffer, len); |
| 89 |
} |
| 90 |
inline void AppendColorComponentF(float value, SkWStream* wStream) { |
| 91 |
char buffer[kFloatColorDecimalCount + 2]; |
| 92 |
size_t len = SkPDFUtils::ColorToDecimalF(value, buffer); |
| 93 |
wStream->write(buffer, len); |
| 94 |
} |
| 95 |
|
| 96 |
inline void AppendScalar(SkScalar value, SkWStream* stream) { |
| 97 |
char result[kMaximumSkFloatToDecimalLength]; |
| 98 |
size_t len = SkFloatToDecimal(value, result); |
| 99 |
SkASSERT(len < kMaximumSkFloatToDecimalLength); |
| 100 |
stream->write(result, len); |
| 101 |
} |
| 102 |
|
| 103 |
inline void WriteUInt16BE(SkWStream* wStream, uint16_t value) { |
| 104 |
char result[4] = { SkHexadecimalDigits::gUpper[ value >> 12 ], |
| 105 |
SkHexadecimalDigits::gUpper[0xF & (value >> 8 )], |
| 106 |
SkHexadecimalDigits::gUpper[0xF & (value >> 4 )], |
| 107 |
SkHexadecimalDigits::gUpper[0xF & (value )] }; |
| 108 |
wStream->write(result, 4); |
| 109 |
} |
| 110 |
|
| 111 |
inline void WriteUInt8(SkWStream* wStream, uint8_t value) { |
| 112 |
char result[2] = { SkHexadecimalDigits::gUpper[value >> 4], |
| 113 |
SkHexadecimalDigits::gUpper[value & 0xF] }; |
| 114 |
wStream->write(result, 2); |
| 115 |
} |
| 116 |
|
| 117 |
inline void WriteUTF16beHex(SkWStream* wStream, SkUnichar utf32) { |
| 118 |
uint16_t utf16[2] = {0, 0}; |
| 119 |
size_t len = SkUTF::ToUTF16(utf32, utf16); |
| 120 |
SkASSERT(len == 1 || len == 2); |
| 121 |
SkPDFUtils::WriteUInt16BE(wStream, utf16[0]); |
| 122 |
if (len == 2) { |
| 123 |
SkPDFUtils::WriteUInt16BE(wStream, utf16[1]); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
inline SkMatrix GetShaderLocalMatrix(const SkShader* shader) { |
| 128 |
SkMatrix localMatrix; |
| 129 |
if (sk_sp<SkShader> s = as_SB(shader)->makeAsALocalMatrixShader(&localMatrix)) { |
| 130 |
return localMatrix; |
| 131 |
} |
| 132 |
return SkMatrix::I(); |
| 133 |
} |
| 134 |
bool InverseTransformBBox(const SkMatrix& matrix, SkRect* bbox); |
| 135 |
void PopulateTilingPatternDict(SkPDFDict* pattern, |
| 136 |
SkRect& bbox, |
| 137 |
bool tileX, bool tileY, |
| 138 |
std::unique_ptr<SkPDFDict> resources, |
| 139 |
const SkMatrix& matrix); |
| 140 |
|
| 141 |
bool ToBitmap(const SkImage* img, SkBitmap* dst); |
| 142 |
|
| 143 |
void AppendTransform(const SkMatrix&, SkWStream*); |
| 144 |
|
| 145 |
// Takes SkTime::GetNSecs() [now] and puts it into the provided struct. |
| 146 |
void GetDateTime(SkPDF::DateTime*); |
| 147 |
|
| 148 |
} // namespace SkPDFUtils |
| 149 |
|
| 150 |
#endif |