• Home
  • History
  • Annotate
  • 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 
8 
9 #ifndef SkPDFUtils_DEFINED
10 #define SkPDFUtils_DEFINED
11 
12 #include "SkPaint.h"
13 #include "SkPath.h"
14 #include "SkStream.h"
15 #include "SkUtils.h"
16 
17 class SkMatrix;
18 class SkPDFArray;
19 struct SkRect;
20 
21 #if 0
22 #define PRINT_NOT_IMPL(str) fprintf(stderr, str)
23 #else
24 #define PRINT_NOT_IMPL(str)
25 #endif
26 
27 #define NOT_IMPLEMENTED(condition, assert)                         \
28     do {                                                           \
29         if ((bool)(condition)) {                                   \
30             PRINT_NOT_IMPL("NOT_IMPLEMENTED: " #condition "\n");   \
31             SkDEBUGCODE(SkASSERT(!assert);)                        \
32         }                                                          \
33     } while (0)
34 
35 namespace SkPDFUtils {
36 
37 sk_sp<SkPDFArray> RectToArray(const SkRect& rect);
38 sk_sp<SkPDFArray> MatrixToArray(const SkMatrix& matrix);
39 void AppendTransform(const SkMatrix& matrix, SkWStream* content);
40 
41 void MoveTo(SkScalar x, SkScalar y, SkWStream* content);
42 void AppendLine(SkScalar x, SkScalar y, SkWStream* content);
43 void AppendCubic(SkScalar ctl1X, SkScalar ctl1Y,
44                  SkScalar ctl2X, SkScalar ctl2Y,
45                  SkScalar dstX, SkScalar dstY, SkWStream* content);
46 void AppendRectangle(const SkRect& rect, SkWStream* content);
47 void EmitPath(const SkPath& path, SkPaint::Style paintStyle,
48               bool doConsumeDegerates, SkWStream* content, SkScalar tolerance = 0.25f);
49 inline void EmitPath(const SkPath& path, SkPaint::Style paintStyle,
50                      SkWStream* content, SkScalar tolerance = 0.25f) {
51     SkPDFUtils::EmitPath(path, paintStyle, true, content, tolerance);
52 }
53 void ClosePath(SkWStream* content);
54 void PaintPath(SkPaint::Style style, SkPath::FillType fill,
55                       SkWStream* content);
56 void StrokePath(SkWStream* content);
57 void DrawFormXObject(int objectIndex, SkWStream* content);
58 void ApplyGraphicState(int objectIndex, SkWStream* content);
59 void ApplyPattern(int objectIndex, SkWStream* content);
60 
61 // Converts (value / 255.0) with three significant digits of accuracy.
62 // Writes value as string into result.  Returns strlen() of result.
63 size_t ColorToDecimal(uint8_t value, char result[5]);
AppendColorComponent(uint8_t value,SkWStream * wStream)64 inline void AppendColorComponent(uint8_t value, SkWStream* wStream) {
65     char buffer[5];
66     size_t len = SkPDFUtils::ColorToDecimal(value, buffer);
67     wStream->write(buffer, len);
68 }
69 
70 // 3 = '-', '.', and '\0' characters.
71 // 9 = number of significant digits
72 // abs(FLT_MIN_10_EXP) = number of zeros in FLT_MIN
73 const size_t kMaximumFloatDecimalLength = 3 + 9 - FLT_MIN_10_EXP;
74 // FloatToDecimal is exposed for unit tests.
75 size_t FloatToDecimal(float value,
76                       char output[kMaximumFloatDecimalLength]);
77 void AppendScalar(SkScalar value, SkWStream* stream);
78 void WriteString(SkWStream* wStream, const char* input, size_t len);
79 
WriteUInt16BE(SkDynamicMemoryWStream * wStream,uint16_t value)80 inline void WriteUInt16BE(SkDynamicMemoryWStream* wStream, uint16_t value) {
81     static const char gHex[] = "0123456789ABCDEF";
82     char result[4];
83     result[0] = gHex[       value >> 12 ];
84     result[1] = gHex[0xF & (value >> 8 )];
85     result[2] = gHex[0xF & (value >> 4 )];
86     result[3] = gHex[0xF & (value      )];
87     wStream->write(result, 4);
88 }
WriteUInt8(SkDynamicMemoryWStream * wStream,uint8_t value)89 inline void WriteUInt8(SkDynamicMemoryWStream* wStream, uint8_t value) {
90     static const char gHex[] = "0123456789ABCDEF";
91     char result[2];
92     result[0] = gHex[value >> 4 ];
93     result[1] = gHex[0xF & value];
94     wStream->write(result, 2);
95 }
WriteUTF16beHex(SkDynamicMemoryWStream * wStream,SkUnichar utf32)96 inline void WriteUTF16beHex(SkDynamicMemoryWStream* wStream, SkUnichar utf32) {
97     uint16_t utf16[2] = {0, 0};
98     size_t len = SkUTF16_FromUnichar(utf32, utf16);
99     SkASSERT(len == 1 || len == 2);
100     SkPDFUtils::WriteUInt16BE(wStream, utf16[0]);
101     if (len == 2) {
102         SkPDFUtils::WriteUInt16BE(wStream, utf16[1]);
103     }
104 }
105 }  // namespace SkPDFUtils
106 
107 #endif
108