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/SkRefCnt.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkStream.h"
15 #include "include/private/base/SkAssert.h"
16 #include "include/private/base/SkDebug.h"
17 #include "src/base/SkUTF.h"
18 #include "src/base/SkUtils.h"
19 #include "src/shaders/SkShaderBase.h"
20 #include "src/utils/SkFloatToDecimal.h"
21
22 #include <cstdint>
23 #include <cstring>
24 #include <memory>
25
26 class SkBitmap;
27 class SkImage;
28 class SkPDFArray;
29 class SkPDFDict;
30 class SkPath;
31 class SkShader;
32 enum class SkBlendMode;
33 enum class SkPathFillType;
34 struct SkRect;
35
36 namespace SkPDF { struct DateTime; }
37
38 template <typename T>
SkPackedArrayEqual(T * u,T * v,size_t n)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 void EmitPath(const SkPath& path, SkPaint::Style paintStyle,
70 bool doConsumeDegerates, SkWStream* content, SkScalar tolerance = 0.25f);
71 inline void EmitPath(const SkPath& path, SkPaint::Style paintStyle,
72 SkWStream* content, SkScalar tolerance = 0.25f) {
73 SkPDFUtils::EmitPath(path, paintStyle, true, content, tolerance);
74 }
75 void ClosePath(SkWStream* content);
76 void PaintPath(SkPaint::Style style, SkPathFillType fill, SkWStream* content);
77 void StrokePath(SkWStream* content);
78 void ApplyGraphicState(int objectIndex, SkWStream* content);
79 void ApplyPattern(int objectIndex, SkWStream* content);
80
81 // Converts (value / 255.0) with three significant digits of accuracy.
82 // Writes value as string into result. Returns strlen() of result.
83 size_t ColorToDecimal(uint8_t value, char result[5]);
84
85 static constexpr unsigned kFloatColorDecimalCount = 4;
86 size_t ColorToDecimalF(float value, char result[kFloatColorDecimalCount + 2]);
AppendColorComponent(uint8_t value,SkWStream * wStream)87 inline void AppendColorComponent(uint8_t value, SkWStream* wStream) {
88 char buffer[5];
89 size_t len = SkPDFUtils::ColorToDecimal(value, buffer);
90 wStream->write(buffer, len);
91 }
AppendColorComponentF(float value,SkWStream * wStream)92 inline void AppendColorComponentF(float value, SkWStream* wStream) {
93 char buffer[kFloatColorDecimalCount + 2];
94 size_t len = SkPDFUtils::ColorToDecimalF(value, buffer);
95 wStream->write(buffer, len);
96 }
97
AppendScalar(SkScalar value,SkWStream * stream)98 inline void AppendScalar(SkScalar value, SkWStream* stream) {
99 char result[kMaximumSkFloatToDecimalLength];
100 size_t len = SkFloatToDecimal(value, result);
101 SkASSERT(len < kMaximumSkFloatToDecimalLength);
102 stream->write(result, len);
103 }
104
WriteUInt16BE(SkWStream * wStream,uint16_t value)105 inline void WriteUInt16BE(SkWStream* wStream, uint16_t value) {
106 char result[4] = { SkHexadecimalDigits::gUpper[ value >> 12 ],
107 SkHexadecimalDigits::gUpper[0xF & (value >> 8 )],
108 SkHexadecimalDigits::gUpper[0xF & (value >> 4 )],
109 SkHexadecimalDigits::gUpper[0xF & (value )] };
110 wStream->write(result, 4);
111 }
112
WriteUInt8(SkWStream * wStream,uint8_t value)113 inline void WriteUInt8(SkWStream* wStream, uint8_t value) {
114 char result[2] = { SkHexadecimalDigits::gUpper[value >> 4],
115 SkHexadecimalDigits::gUpper[value & 0xF] };
116 wStream->write(result, 2);
117 }
118
WriteUTF16beHex(SkWStream * wStream,SkUnichar utf32)119 inline void WriteUTF16beHex(SkWStream* wStream, SkUnichar utf32) {
120 uint16_t utf16[2] = {0, 0};
121 size_t len = SkUTF::ToUTF16(utf32, utf16);
122 SkASSERT(len == 1 || len == 2);
123 SkPDFUtils::WriteUInt16BE(wStream, utf16[0]);
124 if (len == 2) {
125 SkPDFUtils::WriteUInt16BE(wStream, utf16[1]);
126 }
127 }
128
GetShaderLocalMatrix(const SkShader * shader)129 inline SkMatrix GetShaderLocalMatrix(const SkShader* shader) {
130 SkMatrix localMatrix;
131 if (sk_sp<SkShader> s = as_SB(shader)->makeAsALocalMatrixShader(&localMatrix)) {
132 return localMatrix;
133 }
134 return SkMatrix::I();
135 }
136 bool InverseTransformBBox(const SkMatrix& matrix, SkRect* bbox);
137 void PopulateTilingPatternDict(SkPDFDict* pattern,
138 SkRect& bbox,
139 std::unique_ptr<SkPDFDict> resources,
140 const SkMatrix& matrix);
141
142 bool ToBitmap(const SkImage* img, SkBitmap* dst);
143
144 #ifdef SK_PDF_BASE85_BINARY
145 void Base85Encode(std::unique_ptr<SkStreamAsset> src, SkDynamicMemoryWStream* dst);
146 #endif // SK_PDF_BASE85_BINARY
147
148 void AppendTransform(const SkMatrix&, SkWStream*);
149
150 // Takes SkTime::GetNSecs() [now] and puts it into the provided struct.
151 void GetDateTime(SkPDF::DateTime*);
152
153 } // namespace SkPDFUtils
154
155 #endif
156