• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/SkUTF.h"
15 #include "src/base/SkUtils.h"
16 #include "src/pdf/SkPDFTypes.h"
17 #include "src/shaders/SkShaderBase.h"
18 #include "src/utils/SkFloatToDecimal.h"
19 
20 class SkMatrix;
21 class SkPDFArray;
22 struct SkRect;
23 
24 template <typename T>
SkPackedArrayEqual(T * u,T * v,size_t n)25 bool SkPackedArrayEqual(T* u, T* v, size_t n) {
26     SkASSERT(u);
27     SkASSERT(v);
28     return 0 == memcmp(u, v, n * sizeof(T));
29 }
30 
31 #if 0
32 #define PRINT_NOT_IMPL(str) fprintf(stderr, str)
33 #else
34 #define PRINT_NOT_IMPL(str)
35 #endif
36 
37 #define NOT_IMPLEMENTED(condition, assert)                         \
38     do {                                                           \
39         if ((bool)(condition)) {                                   \
40             PRINT_NOT_IMPL("NOT_IMPLEMENTED: " #condition "\n");   \
41             SkDEBUGCODE(SkASSERT(!assert);)                        \
42         }                                                          \
43     } while (0)
44 
45 namespace SkPDFUtils {
46 
47 const char* BlendModeName(SkBlendMode);
48 
49 std::unique_ptr<SkPDFArray> RectToArray(const SkRect& rect);
50 std::unique_ptr<SkPDFArray> MatrixToArray(const SkMatrix& matrix);
51 
52 void MoveTo(SkScalar x, SkScalar y, SkWStream* content);
53 void AppendLine(SkScalar x, SkScalar y, SkWStream* content);
54 void AppendRectangle(const SkRect& rect, SkWStream* content);
55 void EmitPath(const SkPath& path, SkPaint::Style paintStyle,
56               bool doConsumeDegerates, SkWStream* content, SkScalar tolerance = 0.25f);
57 inline void EmitPath(const SkPath& path, SkPaint::Style paintStyle,
58                      SkWStream* content, SkScalar tolerance = 0.25f) {
59     SkPDFUtils::EmitPath(path, paintStyle, true, content, tolerance);
60 }
61 void ClosePath(SkWStream* content);
62 void PaintPath(SkPaint::Style style, SkPathFillType fill, 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(SkWStream * wStream,uint16_t value)91 inline void WriteUInt16BE(SkWStream* 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(SkWStream * wStream,uint8_t value)99 inline void WriteUInt8(SkWStream* 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(SkWStream * wStream,SkUnichar utf32)105 inline void WriteUTF16beHex(SkWStream* 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 localMatrix;
119     }
120     return SkMatrix::I();
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