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