blob: 894db10cd02b025558d09fb14e6acddeaa552c51 [file] [log] [blame]
edisonn@google.comb857a0c2013-06-25 20:45:40 +00001#ifndef __DEFINED__SkPdfBasics
2#define __DEFINED__SkPdfBasics
3
edisonn@google.com131d4ee2013-06-26 17:48:12 +00004#include "SkCanvas.h"
5#include "SkPaint.h"
6
edisonn@google.comb857a0c2013-06-25 20:45:40 +00007#include <iostream>
8#include <cstdio>
9#include <stack>
10
edisonn@google.comeee4b652013-06-27 13:22:42 +000011#define PDF_TRACE
edisonn@google.comb857a0c2013-06-25 20:45:40 +000012//#define PDF_TRACE_DIFF_IN_PNG
13//#define PDF_DEBUG_NO_CLIPING
14//#define PDF_DEBUG_NO_PAGE_CLIPING
15//#define PDF_DEBUG_3X
16
17class SkPdfFont;
18class SkPdfDoc;
edisonn@google.com131d4ee2013-06-26 17:48:12 +000019class SkPdfObject;
20class SkPdfResourceDictionary;
edisonn@google.comb857a0c2013-06-25 20:45:40 +000021
22// TODO(edisonn): better class design.
23struct PdfColorOperator {
24 std::string fColorSpace; // TODO(edisonn): use SkString
25 SkColor fColor;
26 double fOpacity; // ca or CA
27 // TODO(edisonn): add here other color space options.
28
29 void setRGBColor(SkColor color) {
30 // TODO(edisonn): ASSERT DeviceRGB is the color space.
31 fColor = color;
32 }
33 // TODO(edisonn): double check the default values for all fields.
34 PdfColorOperator() : fColor(SK_ColorBLACK), fOpacity(1) {}
35
36 void applyGraphicsState(SkPaint* paint) {
37 paint->setColor(SkColorSetA(fColor, fOpacity * 255));
38 }
39};
40
41// TODO(edisonn): better class design.
42struct PdfGraphicsState {
43 SkMatrix fMatrix;
44 SkMatrix fMatrixTm;
45 SkMatrix fMatrixTlm;
46
47 double fCurPosX;
48 double fCurPosY;
49
50 double fCurFontSize;
51 bool fTextBlock;
52 SkPdfFont* fSkFont;
53 SkPath fPath;
54 bool fPathClosed;
55
56 // Clip that is applied after the drawing is done!!!
57 bool fHasClipPathToApply;
58 SkPath fClipPath;
59
60 PdfColorOperator fStroking;
61 PdfColorOperator fNonStroking;
62
63 double fLineWidth;
64 double fTextLeading;
65 double fWordSpace;
66 double fCharSpace;
67
68 SkPdfResourceDictionary* fResources;
69
70 SkBitmap fSMask;
71
72 PdfGraphicsState() {
73 fCurPosX = 0.0;
74 fCurPosY = 0.0;
75 fCurFontSize = 0.0;
76 fTextBlock = false;
77 fMatrix = SkMatrix::I();
78 fMatrixTm = SkMatrix::I();
79 fMatrixTlm = SkMatrix::I();
80 fPathClosed = true;
81 fLineWidth = 0;
82 fTextLeading = 0;
83 fWordSpace = 0;
84 fCharSpace = 0;
85 fHasClipPathToApply = false;
86 fResources = NULL;
87 fSkFont = NULL;
88 }
89
90 void applyGraphicsState(SkPaint* paint, bool stroking) {
91 if (stroking) {
92 fStroking.applyGraphicsState(paint);
93 } else {
94 fNonStroking.applyGraphicsState(paint);
95 }
96
97 // TODO(edisonn): get this from pdfContext->options,
98 // or pdfContext->addPaintOptions(&paint);
99 paint->setAntiAlias(true);
100
101 // TODO(edisonn): dashing, miter, ...
102 paint->setStrokeWidth(SkDoubleToScalar(fLineWidth));
103 }
104};
105
106// TODO(edisonn): better class design.
107struct PdfInlineImage {
108 std::map<std::string, std::string> fKeyValuePairs;
109 std::string fImageData;
110};
111
112// TODO(edisonn): better class design.
113struct PdfContext {
114 std::stack<SkPdfObject*> fObjectStack;
115 std::stack<PdfGraphicsState> fStateStack;
116 PdfGraphicsState fGraphicsState;
117 SkPdfDoc* fPdfDoc;
118 SkMatrix fOriginalMatrix;
119
120 PdfInlineImage fInlineImage;
121
122 PdfContext(SkPdfDoc* doc) : fPdfDoc(doc) {}
123
124};
125
126// TODO(edisonn): temporary code, to report how much of the PDF we actually think we rendered.
127enum PdfResult {
128 kOK_PdfResult,
129 kPartial_PdfResult,
130 kNYI_PdfResult,
131 kIgnoreError_PdfResult,
132 kError_PdfResult,
133 kUnsupported_PdfResult,
134
135 kCount_PdfResult
136};
137
138#endif // __DEFINED__SkPdfBasics