blob: a681eca7105ba58c13eeddd46fc316aa88386028 [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
116/*
117clipping path (internal) The current clipping path, which defines the boundary against
118 which all output is to be cropped (see Section 4.4.3, “Clipping Path
119 Operators”). Initial value: the boundary of the entire imageable
120 portion of the output page.
121 */
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000122 // Clip that is applied after the drawing is done!!!
123 bool fHasClipPathToApply;
124 SkPath fClipPath;
125
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000126 SkPdfColorOperator fStroking;
127 SkPdfColorOperator fNonStroking;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000128
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000129/*
130text state (various) A set of nine graphics state parameters that pertain only to the
131 painting of text. These include parameters that select the font, scale
132 the glyphs to an appropriate size, and accomplish other effects. The
133 text state parameters are described in Section 5.2, “Text State
134 Parameters and Operators.”
135 */
136
137 // TODO(edisonn): add SkPdfTextState class. remove these two existing fields
138 SkMatrix fMatrixTm;
139 SkMatrix fMatrixTlm;
140
141
142/*
143line width number The thickness, in user space units, of paths to be stroked (see “Line
144 Width” on page 152). Initial value: 1.0.
145 */
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000146 double fLineWidth;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000147
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000148
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000149/*
150line cap integer A code specifying the shape of the endpoints for any open path that
151 is stroked (see “Line Cap Style” on page 153). Initial value: 0, for
152 square butt caps.
153 */
154 // TODO (edisonn): implement defaults - page 153
155 int fLineCap;
156
157/*
158line join integer A code specifying the shape of joints between connected segments
159 of a stroked path (see “Line Join Style” on page 153). Initial value: 0,
160 for mitered joins.
161 */
162 // TODO (edisonn): implement defaults - page 153
163 int fLineJoin;
164
165/*
166miter limit number The maximum length of mitered line joins for stroked paths (see
167 “Miter Limit” on page 153). This parameter limits the length of
168 “spikes” produced when line segments join at sharp angles. Initial
169 value: 10.0, for a miter cutoff below approximately 11.5 degrees.
170 */
171 // TODO (edisonn): implement defaults - page 153
172 double fMiterLimit;
173
174/*
175dash pattern array and A description of the dash pattern to be used when paths are
176 number stroked (see “Line Dash Pattern” on page 155). Initial value: a solid
177 line.
178 */
179 SkScalar fDashArray[256]; // TODO(edisonn): allocate array?
180 int fDashArrayLength;
181 SkScalar fDashPhase;
182
183
184/*
185rendering intent name The rendering intent to be used when converting CIE-based colors
186 to device colors (see “Rendering Intents” on page 197). Default
187 value: RelativeColorimetric.
188 */
189 // TODO(edisonn): seems paper only. Verify.
190
191/*
192stroke adjustment boolean (PDF 1.2) A flag specifying whether to compensate for possible ras-
193 terization effects when stroking a path with a line width that is
194 small relative to the pixel resolution of the output device (see Sec-
195 tion 6.5.4, “Automatic Stroke Adjustment”). Note that this is con-
196 sidered a device-independent parameter, even though the details of
197 its effects are device-dependent. Initial value: false.
198 */
199 // TODO(edisonn): stroke adjustment low priority.
200
201
202/*
203blend mode name or array (PDF 1.4) The current blend mode to be used in the transparent
204 imaging model (see Sections 7.2.4, “Blend Mode,” and 7.5.2, “Spec-
205 ifying Blending Color Space and Blend Mode”). This parameter is
206 implicitly reset to its initial value at the beginning of execution of a
207 transparency group XObject (see Section 7.5.5, “Transparency
208 Group XObjects”). Initial value: Normal.
209 */
edisonn@google.come878e722013-07-29 19:10:58 +0000210 SkXfermode::Mode fBlendModes[256];
211 int fBlendModesLength;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000212
213/*
214soft mask dictionary (PDF 1.4) A soft-mask dictionary (see “Soft-Mask Dictionaries” on
215 or name page 445) specifying the mask shape or mask opacity values to be
216 used in the transparent imaging model (see “Source Shape and
217 Opacity” on page 421 and “Mask Shape and Opacity” on page 443),
218 or the name None if no such mask is specified. This parameter is
219 implicitly reset to its initial value at the beginning of execution of a
220 transparency group XObject (see Section 7.5.5, “Transparency
221 Group XObjects”). Initial value: None.
222 */
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000223 SkPdfSoftMaskDictionary* fSoftMaskDictionary;
224 SkBitmap fSMask;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000225
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000226
227/*
228alpha constant number (PDF 1.4) The constant shape or constant opacity value to be used
229 in the transparent imaging model (see “Source Shape and Opacity”
230 on page 421 and “Constant Shape and Opacity” on page 444).
231 There are two separate alpha constant parameters: one for stroking
232 and one for all other painting operations. This parameter is implic-
233 itly reset to its initial value at the beginning of execution of a trans-
234 parency group XObject (see Section 7.5.5, “Transparency Group
235 XObjects”). Initial value: 1.0.
236 */
237 double fAphaConstant;
238
239/*
240alpha source boolean (PDF 1.4) A flag specifying whether the current soft mask and alpha
241 constant parameters are to be interpreted as shape values (true) or
242 opacity values (false). This flag also governs the interpretation of
243 the SMask entry, if any, in an image dictionary (see Section 4.8.4,
244 “Image Dictionaries”). Initial value: false.
245 */
246 bool fAlphaSource;
247
248
249// TODO(edisonn): Device-dependent seem to be required only on the actual physical printer?
250// TABLE 4.3 Device-dependent graphics state parameters
251/*
252overprint boolean (PDF 1.2) A flag specifying (on output devices that support the
253 overprint control feature) whether painting in one set of colorants
254 should cause the corresponding areas of other colorants to be
255 erased (false) or left unchanged (true); see Section 4.5.6, “Over-
256 print Control.” In PDF 1.3, there are two separate overprint param-
257 eters: one for stroking and one for all other painting operations.
258 Initial value: false.
259 */
260
261
262/*
263overprint mode number (PDF 1.3) A code specifying whether a color component value of 0
264 in a DeviceCMYK color space should erase that component (0) or
265 leave it unchanged (1) when overprinting (see Section 4.5.6, “Over-
266 print Control”). Initial value: 0.
267 */
268
269
270/*
271black generation function (PDF 1.2) A function that calculates the level of the black color
272 or name component to use when converting RGB colors to CMYK (see Sec-
273 tion 6.2.3, “Conversion from DeviceRGB to DeviceCMYK”). Initial
274 value: installation-dependent.
275 */
276
277
278/*
279undercolor removal function (PDF 1.2) A function that calculates the reduction in the levels of
280 or name the cyan, magenta, and yellow color components to compensate for
281 the amount of black added by black generation (see Section 6.2.3,
282 “Conversion from DeviceRGB to DeviceCMYK”). Initial value: in-
283 stallation-dependent.
284 */
285
286
287/*
288transfer function, (PDF 1.2) A function that adjusts device gray or color component
289 array, or name levels to compensate for nonlinear response in a particular out-
290 put device (see Section 6.3, “Transfer Functions”). Initial value:
291 installation-dependent.
292 */
293
294
295/*
296halftone dictionary, (PDF 1.2) A halftone screen for gray and color rendering, specified
297 stream, or name as a halftone dictionary or stream (see Section 6.4, “Halftones”).
298 Initial value: installation-dependent.
299 */
300
301
302/*
303flatness number The precision with which curves are to be rendered on the output
304 device (see Section 6.5.1, “Flatness Tolerance”). The value of this
305 parameter gives the maximum error tolerance, measured in output
306 device pixels; smaller numbers give smoother curves at the expense
307 of more computation and memory use. Initial value: 1.0.
308 */
309
310
311/*
312smoothness number (PDF 1.3) The precision with which color gradients are to be ren-
313 dered on the output device (see Section 6.5.2, “Smoothness Toler-
314 ance”). The value of this parameter gives the maximum error
315 tolerance, expressed as a fraction of the range of each color compo-
316 nent; smaller numbers give smoother color transitions at the
317 expense of more computation and memory use. Initial value:
318 installation-dependent.
319 */
320
321
322
323
324
325
326
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000327 SkPdfGraphicsState() {
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000328 fCurPosX = 0.0;
329 fCurPosY = 0.0;
330 fCurFontSize = 0.0;
331 fTextBlock = false;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000332 fCTM = SkMatrix::I();
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000333 fMatrixTm = SkMatrix::I();
334 fMatrixTlm = SkMatrix::I();
335 fPathClosed = true;
336 fLineWidth = 0;
337 fTextLeading = 0;
338 fWordSpace = 0;
339 fCharSpace = 0;
340 fHasClipPathToApply = false;
341 fResources = NULL;
342 fSkFont = NULL;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000343 fLineCap = 0;
344 fLineJoin = 0;
345 fMiterLimit = 10.0;
346 fAphaConstant = 1.0;
347 fAlphaSource = false;
348 fDashArrayLength = 0;
349 fDashPhase = 0;
edisonn@google.come878e722013-07-29 19:10:58 +0000350 fBlendModesLength = 1;
351 fBlendModes[0] = SkXfermode::kSrc_Mode; // PDF: Normal Blend mode
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000352 }
353
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000354 // TODO(edisonn): make two functons instead, stroking and non stoking, avoid branching
355 void applyGraphicsState(SkPaint* paint, bool stroking);
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000356};
357
358// TODO(edisonn): better class design.
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000359// TODO(edisonn): could we remove it?
360// TODO(edisonn): rename to SkPdfInlineImage
361struct SkPdfInlineImage {
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000362 std::map<std::string, std::string> fKeyValuePairs;
363 std::string fImageData;
364};
365
366// TODO(edisonn): better class design.
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000367// TODO(edisonn): rename to SkPdfContext
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000368struct PdfContext {
369 std::stack<SkPdfObject*> fObjectStack;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000370 std::stack<SkPdfGraphicsState> fStateStack;
371 SkPdfGraphicsState fGraphicsState;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000372 SkNativeParsedPDF* fPdfDoc;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000373 // TODO(edisonn): the allocator, could be freed after the page is done drawing.
374 SkPdfAllocator* fTmpPageAllocator;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000375 SkMatrix fOriginalMatrix;
376
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000377 SkPdfInlineImage fInlineImage;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000378
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000379 PdfContext(SkNativeParsedPDF* doc);
380 ~PdfContext();
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000381};
382
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000383// TODO(edisonn): temporary code, to report how much of the PDF we actually think we rendered.
384// TODO(edisonn): rename to SkPdfResult
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000385enum PdfResult {
386 kOK_PdfResult,
387 kPartial_PdfResult,
388 kNYI_PdfResult,
389 kIgnoreError_PdfResult,
390 kError_PdfResult,
391 kUnsupported_PdfResult,
392
393 kCount_PdfResult
394};
395
396#endif // __DEFINED__SkPdfBasics