• 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 "SkPDFTypes.h"
11 #include "SkPaint.h"
12 #include "SkPath.h"
13 #include "SkShader.h"
14 #include "SkStream.h"
15 #include "SkUtils.h"
16 
17 class SkMatrix;
18 class SkPDFArray;
19 struct SkRect;
20 
21 template <typename T>
SkPackedArrayEqual(T * u,T * v,size_t n)22 bool SkPackedArrayEqual(T* u, T* v, size_t n) {
23     SkASSERT(u);
24     SkASSERT(v);
25     return 0 == memcmp(u, v, n * sizeof(T));
26 }
27 
28 #if 0
29 #define PRINT_NOT_IMPL(str) fprintf(stderr, str)
30 #else
31 #define PRINT_NOT_IMPL(str)
32 #endif
33 
34 #define NOT_IMPLEMENTED(condition, assert)                         \
35     do {                                                           \
36         if ((bool)(condition)) {                                   \
37             PRINT_NOT_IMPL("NOT_IMPLEMENTED: " #condition "\n");   \
38             SkDEBUGCODE(SkASSERT(!assert);)                        \
39         }                                                          \
40     } while (0)
41 
42 namespace SkPDFUtils {
43 
44 constexpr float kDpiForRasterScaleOne = 72.0f;
45 
46 sk_sp<SkPDFArray> RectToArray(const SkRect& rect);
47 sk_sp<SkPDFArray> MatrixToArray(const SkMatrix& matrix);
48 void AppendTransform(const SkMatrix& matrix, SkWStream* content);
49 
50 void MoveTo(SkScalar x, SkScalar y, SkWStream* content);
51 void AppendLine(SkScalar x, SkScalar y, SkWStream* content);
52 void AppendCubic(SkScalar ctl1X, SkScalar ctl1Y,
53                  SkScalar ctl2X, SkScalar ctl2Y,
54                  SkScalar dstX, SkScalar dstY, SkWStream* content);
55 void AppendRectangle(const SkRect& rect, SkWStream* content);
56 void EmitPath(const SkPath& path, SkPaint::Style paintStyle,
57               bool doConsumeDegerates, SkWStream* content, SkScalar tolerance = 0.25f);
58 inline void EmitPath(const SkPath& path, SkPaint::Style paintStyle,
59                      SkWStream* content, SkScalar tolerance = 0.25f) {
60     SkPDFUtils::EmitPath(path, paintStyle, true, content, tolerance);
61 }
62 void ClosePath(SkWStream* content);
63 void PaintPath(SkPaint::Style style, SkPath::FillType fill,
64                       SkWStream* content);
65 void StrokePath(SkWStream* content);
66 void DrawFormXObject(int objectIndex, SkWStream* content);
67 void ApplyGraphicState(int objectIndex, SkWStream* content);
68 void ApplyPattern(int objectIndex, SkWStream* content);
69 
70 // Converts (value / 255.0) with three significant digits of accuracy.
71 // Writes value as string into result.  Returns strlen() of result.
72 size_t ColorToDecimal(uint8_t value, char result[5]);
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 }
78 
79 // 3 = '-', '.', and '\0' characters.
80 // 9 = number of significant digits
81 // abs(FLT_MIN_10_EXP) = number of zeros in FLT_MIN
82 const size_t kMaximumFloatDecimalLength = 3 + 9 - FLT_MIN_10_EXP;
83 // FloatToDecimal is exposed for unit tests.
84 size_t FloatToDecimal(float value,
85                       char output[kMaximumFloatDecimalLength]);
86 void AppendScalar(SkScalar value, SkWStream* stream);
87 void WriteString(SkWStream* wStream, const char* input, size_t len);
88 
WriteUInt16BE(SkDynamicMemoryWStream * wStream,uint16_t value)89 inline void WriteUInt16BE(SkDynamicMemoryWStream* wStream, uint16_t value) {
90     char result[4];
91     result[0] = SkHexadecimalDigits::gUpper[       value >> 12 ];
92     result[1] = SkHexadecimalDigits::gUpper[0xF & (value >> 8 )];
93     result[2] = SkHexadecimalDigits::gUpper[0xF & (value >> 4 )];
94     result[3] = SkHexadecimalDigits::gUpper[0xF & (value      )];
95     wStream->write(result, 4);
96 }
97 
WriteUInt8(SkDynamicMemoryWStream * wStream,uint8_t value)98 inline void WriteUInt8(SkDynamicMemoryWStream* wStream, uint8_t value) {
99     char result[2] = { SkHexadecimalDigits::gUpper[value >> 4],
100                        SkHexadecimalDigits::gUpper[value & 0xF] };
101     wStream->write(result, 2);
102 }
103 
WriteUTF16beHex(SkDynamicMemoryWStream * wStream,SkUnichar utf32)104 inline void WriteUTF16beHex(SkDynamicMemoryWStream* wStream, SkUnichar utf32) {
105     uint16_t utf16[2] = {0, 0};
106     size_t len = SkUTF16_FromUnichar(utf32, utf16);
107     SkASSERT(len == 1 || len == 2);
108     SkPDFUtils::WriteUInt16BE(wStream, utf16[0]);
109     if (len == 2) {
110         SkPDFUtils::WriteUInt16BE(wStream, utf16[1]);
111     }
112 }
113 
GetShaderLocalMatrix(const SkShader * shader)114 inline SkMatrix GetShaderLocalMatrix(const SkShader* shader) {
115     SkMatrix localMatrix;
116     if (sk_sp<SkShader> s = shader->makeAsALocalMatrixShader(&localMatrix)) {
117         return SkMatrix::Concat(s->getLocalMatrix(), localMatrix);
118     }
119     return shader->getLocalMatrix();
120 }
121 bool InverseTransformBBox(const SkMatrix& matrix, SkRect* bbox);
122 void PopulateTilingPatternDict(SkPDFDict* pattern,
123                                SkRect& bbox,
124                                sk_sp<SkPDFDict> resources,
125                                const SkMatrix& matrix);
126 
127 bool ToBitmap(const SkImage* img, SkBitmap* dst);
128 }  // namespace SkPDFUtils
129 
130 #endif
131