blob: 9306ad5d950f6796055131cbf6135846510791ba [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"
edisonn@google.com3aac1f92013-07-02 22:42:53 +00006#include "SkPdfConfig.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +00007
edisonn@google.comb857a0c2013-06-25 20:45:40 +00008#include <iostream>
9#include <cstdio>
edisonn@google.com3aac1f92013-07-02 22:42:53 +000010#include <map>
edisonn@google.comb857a0c2013-06-25 20:45:40 +000011#include <stack>
12
edisonn@google.comb857a0c2013-06-25 20:45:40 +000013class SkPdfFont;
14class SkPdfDoc;
edisonn@google.com131d4ee2013-06-26 17:48:12 +000015class SkPdfObject;
16class SkPdfResourceDictionary;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +000017class SkPdfSoftMaskDictionary;
edisonn@google.comb857a0c2013-06-25 20:45:40 +000018
edisonn@google.com571c70b2013-07-10 17:09:50 +000019class SkNativeParsedPDF;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000020class SkPdfAllocator;
edisonn@google.com3aac1f92013-07-02 22:42:53 +000021
edisonn@google.comb857a0c2013-06-25 20:45:40 +000022// TODO(edisonn): better class design.
edisonn@google.come2e01ff2013-08-02 20:24:48 +000023class SkPdfColorOperator {
edisonn@google.coma0cefa12013-07-28 18:34:14 +000024
25 /*
26 color space name or array The current color space in which color values are to be interpreted
27 (see Section 4.5, “Color Spaces”). There are two separate color space
28 parameters: one for stroking and one for all other painting opera-
29 tions. Initial value: DeviceGray.
30 */
31
32 // TODO(edisonn): implement the array part too
edisonn@google.com571c70b2013-07-10 17:09:50 +000033 // does not own the char*
edisonn@google.come2e01ff2013-08-02 20:24:48 +000034// TODO(edisonn): remove this public, let fields be private
35// TODO(edisonn): make color space an enum!
36public:
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000037 NotOwnedString fColorSpace;
edisonn@google.come2e01ff2013-08-02 20:24:48 +000038 SkPdfObject* fPattern;
edisonn@google.coma0cefa12013-07-28 18:34:14 +000039
40 /*
41 color (various) The current color to be used during painting operations (see Section
42 4.5, “Color Spaces”). The type and interpretation of this parameter
43 depend on the current color space; for most color spaces, a color
44 value consists of one to four numbers. There are two separate color
45 parameters: one for stroking and one for all other painting opera-
46 tions. Initial value: black.
47 */
48
edisonn@google.comb857a0c2013-06-25 20:45:40 +000049 SkColor fColor;
50 double fOpacity; // ca or CA
edisonn@google.come2e01ff2013-08-02 20:24:48 +000051
edisonn@google.comb857a0c2013-06-25 20:45:40 +000052 // TODO(edisonn): add here other color space options.
53
edisonn@google.come2e01ff2013-08-02 20:24:48 +000054public:
edisonn@google.comb857a0c2013-06-25 20:45:40 +000055 void setRGBColor(SkColor color) {
56 // TODO(edisonn): ASSERT DeviceRGB is the color space.
edisonn@google.come2e01ff2013-08-02 20:24:48 +000057 fPattern = NULL;
edisonn@google.comb857a0c2013-06-25 20:45:40 +000058 fColor = color;
59 }
60 // TODO(edisonn): double check the default values for all fields.
edisonn@google.come2e01ff2013-08-02 20:24:48 +000061 SkPdfColorOperator() : fPattern(NULL), fColor(SK_ColorBLACK), fOpacity(1) {
62 NotOwnedString::init(&fColorSpace, "DeviceRGB");
63 }
64
65 void setColorSpace(NotOwnedString* colorSpace) {
66 fColorSpace = *colorSpace;
67 fPattern = NULL;
68 }
69
70 void setPatternColorSpace(SkPdfObject* pattern) {
71 fColorSpace.fBuffer = (const unsigned char*)"Pattern";
72 fColorSpace.fBytes = 7; // strlen("Pattern")
73 fPattern = pattern;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000074 }
edisonn@google.comb857a0c2013-06-25 20:45:40 +000075
76 void applyGraphicsState(SkPaint* paint) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +000077 paint->setColor(SkColorSetA(fColor, (U8CPU)(fOpacity * 255)));
edisonn@google.comb857a0c2013-06-25 20:45:40 +000078 }
79};
80
81// TODO(edisonn): better class design.
edisonn@google.com3aac1f92013-07-02 22:42:53 +000082struct SkPdfGraphicsState {
edisonn@google.coma0cefa12013-07-28 18:34:14 +000083 // TODO(edisonn): deprecate and remove these!
edisonn@google.comb857a0c2013-06-25 20:45:40 +000084 double fCurPosX;
85 double fCurPosY;
86
87 double fCurFontSize;
88 bool fTextBlock;
89 SkPdfFont* fSkFont;
90 SkPath fPath;
91 bool fPathClosed;
92
edisonn@google.coma0cefa12013-07-28 18:34:14 +000093
94
95 double fTextLeading;
96 double fWordSpace;
97 double fCharSpace;
98
99 SkPdfResourceDictionary* fResources;
100
101
102 // TODO(edisonn): move most of these in canvas/paint?
103 // we could have some in canvas (matrixes?),
104 // some in 2 paints (stroking paint and non stroking paint)
105
106// TABLE 4.2 Device-independent graphics state parameters
107/*
108 * CTM array The current transformation matrix, which maps positions from user
109 coordinates to device coordinates (see Section 4.2, “Coordinate Sys-
110 tems”). This matrix is modified by each application of the coordi-
111 nate transformation operator, cm. Initial value: a matrix that
112 transforms default user coordinates to device coordinates.
113 */
114 SkMatrix fCTM;
115
edisonn@google.com0f901902013-08-07 11:56:16 +0000116 SkMatrix fContentStreamMatrix;
117
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000118/*
119clipping path (internal) The current clipping path, which defines the boundary against
120 which all output is to be cropped (see Section 4.4.3, “Clipping Path
121 Operators”). Initial value: the boundary of the entire imageable
122 portion of the output page.
123 */
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000124 // Clip that is applied after the drawing is done!!!
125 bool fHasClipPathToApply;
126 SkPath fClipPath;
127
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000128 SkPdfColorOperator fStroking;
129 SkPdfColorOperator fNonStroking;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000130
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000131/*
132text state (various) A set of nine graphics state parameters that pertain only to the
133 painting of text. These include parameters that select the font, scale
134 the glyphs to an appropriate size, and accomplish other effects. The
135 text state parameters are described in Section 5.2, “Text State
136 Parameters and Operators.”
137 */
138
139 // TODO(edisonn): add SkPdfTextState class. remove these two existing fields
140 SkMatrix fMatrixTm;
141 SkMatrix fMatrixTlm;
142
143
144/*
145line width number The thickness, in user space units, of paths to be stroked (see “Line
146 Width” on page 152). Initial value: 1.0.
147 */
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000148 double fLineWidth;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000149
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000150
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000151/*
152line cap integer A code specifying the shape of the endpoints for any open path that
153 is stroked (see “Line Cap Style” on page 153). Initial value: 0, for
154 square butt caps.
155 */
156 // TODO (edisonn): implement defaults - page 153
157 int fLineCap;
158
159/*
160line join integer A code specifying the shape of joints between connected segments
161 of a stroked path (see “Line Join Style” on page 153). Initial value: 0,
162 for mitered joins.
163 */
164 // TODO (edisonn): implement defaults - page 153
165 int fLineJoin;
166
167/*
168miter limit number The maximum length of mitered line joins for stroked paths (see
169 “Miter Limit” on page 153). This parameter limits the length of
170 “spikes” produced when line segments join at sharp angles. Initial
171 value: 10.0, for a miter cutoff below approximately 11.5 degrees.
172 */
173 // TODO (edisonn): implement defaults - page 153
174 double fMiterLimit;
175
176/*
177dash pattern array and A description of the dash pattern to be used when paths are
178 number stroked (see “Line Dash Pattern” on page 155). Initial value: a solid
179 line.
180 */
181 SkScalar fDashArray[256]; // TODO(edisonn): allocate array?
182 int fDashArrayLength;
183 SkScalar fDashPhase;
184
185
186/*
187rendering intent name The rendering intent to be used when converting CIE-based colors
188 to device colors (see “Rendering Intents” on page 197). Default
189 value: RelativeColorimetric.
190 */
191 // TODO(edisonn): seems paper only. Verify.
192
193/*
194stroke adjustment boolean (PDF 1.2) A flag specifying whether to compensate for possible ras-
195 terization effects when stroking a path with a line width that is
196 small relative to the pixel resolution of the output device (see Sec-
197 tion 6.5.4, “Automatic Stroke Adjustment”). Note that this is con-
198 sidered a device-independent parameter, even though the details of
199 its effects are device-dependent. Initial value: false.
200 */
201 // TODO(edisonn): stroke adjustment low priority.
202
203
204/*
205blend mode name or array (PDF 1.4) The current blend mode to be used in the transparent
206 imaging model (see Sections 7.2.4, “Blend Mode,” and 7.5.2, “Spec-
207 ifying Blending Color Space and Blend Mode”). This parameter is
208 implicitly reset to its initial value at the beginning of execution of a
209 transparency group XObject (see Section 7.5.5, “Transparency
210 Group XObjects”). Initial value: Normal.
211 */
edisonn@google.come878e722013-07-29 19:10:58 +0000212 SkXfermode::Mode fBlendModes[256];
213 int fBlendModesLength;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000214
215/*
216soft mask dictionary (PDF 1.4) A soft-mask dictionary (see “Soft-Mask Dictionaries” on
217 or name page 445) specifying the mask shape or mask opacity values to be
218 used in the transparent imaging model (see “Source Shape and
219 Opacity” on page 421 and “Mask Shape and Opacity” on page 443),
220 or the name None if no such mask is specified. This parameter is
221 implicitly reset to its initial value at the beginning of execution of a
222 transparency group XObject (see Section 7.5.5, “Transparency
223 Group XObjects”). Initial value: None.
224 */
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000225 SkPdfSoftMaskDictionary* fSoftMaskDictionary;
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000226 // TODO(edisonn): make sMask private, add setter and getter, ref/unref/..., at the moment we most likely leask
227 SkBitmap* fSMask;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000228
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000229
230/*
231alpha constant number (PDF 1.4) The constant shape or constant opacity value to be used
232 in the transparent imaging model (see “Source Shape and Opacity”
233 on page 421 and “Constant Shape and Opacity” on page 444).
234 There are two separate alpha constant parameters: one for stroking
235 and one for all other painting operations. This parameter is implic-
236 itly reset to its initial value at the beginning of execution of a trans-
237 parency group XObject (see Section 7.5.5, “Transparency Group
238 XObjects”). Initial value: 1.0.
239 */
240 double fAphaConstant;
241
242/*
243alpha source boolean (PDF 1.4) A flag specifying whether the current soft mask and alpha
244 constant parameters are to be interpreted as shape values (true) or
245 opacity values (false). This flag also governs the interpretation of
246 the SMask entry, if any, in an image dictionary (see Section 4.8.4,
247 “Image Dictionaries”). Initial value: false.
248 */
249 bool fAlphaSource;
250
251
252// TODO(edisonn): Device-dependent seem to be required only on the actual physical printer?
253// TABLE 4.3 Device-dependent graphics state parameters
254/*
255overprint boolean (PDF 1.2) A flag specifying (on output devices that support the
256 overprint control feature) whether painting in one set of colorants
257 should cause the corresponding areas of other colorants to be
258 erased (false) or left unchanged (true); see Section 4.5.6, “Over-
259 print Control.” In PDF 1.3, there are two separate overprint param-
260 eters: one for stroking and one for all other painting operations.
261 Initial value: false.
262 */
263
264
265/*
266overprint mode number (PDF 1.3) A code specifying whether a color component value of 0
267 in a DeviceCMYK color space should erase that component (0) or
268 leave it unchanged (1) when overprinting (see Section 4.5.6, “Over-
269 print Control”). Initial value: 0.
270 */
271
272
273/*
274black generation function (PDF 1.2) A function that calculates the level of the black color
275 or name component to use when converting RGB colors to CMYK (see Sec-
276 tion 6.2.3, “Conversion from DeviceRGB to DeviceCMYK”). Initial
277 value: installation-dependent.
278 */
279
280
281/*
282undercolor removal function (PDF 1.2) A function that calculates the reduction in the levels of
283 or name the cyan, magenta, and yellow color components to compensate for
284 the amount of black added by black generation (see Section 6.2.3,
285 “Conversion from DeviceRGB to DeviceCMYK”). Initial value: in-
286 stallation-dependent.
287 */
288
289
290/*
291transfer function, (PDF 1.2) A function that adjusts device gray or color component
292 array, or name levels to compensate for nonlinear response in a particular out-
293 put device (see Section 6.3, “Transfer Functions”). Initial value:
294 installation-dependent.
295 */
296
297
298/*
299halftone dictionary, (PDF 1.2) A halftone screen for gray and color rendering, specified
300 stream, or name as a halftone dictionary or stream (see Section 6.4, “Halftones”).
301 Initial value: installation-dependent.
302 */
303
304
305/*
306flatness number The precision with which curves are to be rendered on the output
307 device (see Section 6.5.1, “Flatness Tolerance”). The value of this
308 parameter gives the maximum error tolerance, measured in output
309 device pixels; smaller numbers give smoother curves at the expense
310 of more computation and memory use. Initial value: 1.0.
311 */
312
313
314/*
315smoothness number (PDF 1.3) The precision with which color gradients are to be ren-
316 dered on the output device (see Section 6.5.2, “Smoothness Toler-
317 ance”). The value of this parameter gives the maximum error
318 tolerance, expressed as a fraction of the range of each color compo-
319 nent; smaller numbers give smoother color transitions at the
320 expense of more computation and memory use. Initial value:
321 installation-dependent.
322 */
323
324
325
326
327
328
329
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000330 SkPdfGraphicsState() {
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000331 fCurPosX = 0.0;
332 fCurPosY = 0.0;
333 fCurFontSize = 0.0;
334 fTextBlock = false;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000335 fCTM = SkMatrix::I();
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000336 fMatrixTm = SkMatrix::I();
337 fMatrixTlm = SkMatrix::I();
338 fPathClosed = true;
339 fLineWidth = 0;
340 fTextLeading = 0;
341 fWordSpace = 0;
342 fCharSpace = 0;
343 fHasClipPathToApply = false;
344 fResources = NULL;
345 fSkFont = NULL;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000346 fLineCap = 0;
347 fLineJoin = 0;
348 fMiterLimit = 10.0;
349 fAphaConstant = 1.0;
350 fAlphaSource = false;
351 fDashArrayLength = 0;
352 fDashPhase = 0;
edisonn@google.come878e722013-07-29 19:10:58 +0000353 fBlendModesLength = 1;
354 fBlendModes[0] = SkXfermode::kSrc_Mode; // PDF: Normal Blend mode
edisonn@google.com91ce6982013-08-05 20:45:40 +0000355 fSMask = NULL;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000356 }
357
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000358 // TODO(edisonn): make two functons instead, stroking and non stoking, avoid branching
359 void applyGraphicsState(SkPaint* paint, bool stroking);
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000360};
361
362// TODO(edisonn): better class design.
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000363// TODO(edisonn): could we remove it?
364// TODO(edisonn): rename to SkPdfInlineImage
365struct SkPdfInlineImage {
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000366 std::map<std::string, std::string> fKeyValuePairs;
367 std::string fImageData;
368};
369
370// TODO(edisonn): better class design.
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000371// TODO(edisonn): rename to SkPdfContext
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000372struct PdfContext {
373 std::stack<SkPdfObject*> fObjectStack;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000374 std::stack<SkPdfGraphicsState> fStateStack;
375 SkPdfGraphicsState fGraphicsState;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000376 SkNativeParsedPDF* fPdfDoc;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000377 // TODO(edisonn): the allocator, could be freed after the page is done drawing.
378 SkPdfAllocator* fTmpPageAllocator;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000379 SkMatrix fOriginalMatrix;
380
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000381 SkPdfInlineImage fInlineImage;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000382
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000383 PdfContext(SkNativeParsedPDF* doc);
384 ~PdfContext();
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000385};
386
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000387// TODO(edisonn): temporary code, to report how much of the PDF we actually think we rendered.
388// TODO(edisonn): rename to SkPdfResult
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000389enum PdfResult {
390 kOK_PdfResult,
391 kPartial_PdfResult,
392 kNYI_PdfResult,
393 kIgnoreError_PdfResult,
394 kError_PdfResult,
395 kUnsupported_PdfResult,
396
397 kCount_PdfResult
398};
399
400#endif // __DEFINED__SkPdfBasics