blob: 28a18c5bc549a5f482bd9775ab138815c564dfa5 [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.com3aac1f92013-07-02 22:42:53 +000023struct 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.com2ccc3af2013-07-23 17:43:18 +000034 NotOwnedString fColorSpace;
edisonn@google.coma0cefa12013-07-28 18:34:14 +000035
36
37 /*
38 color (various) The current color to be used during painting operations (see Section
39 4.5, “Color Spaces”). The type and interpretation of this parameter
40 depend on the current color space; for most color spaces, a color
41 value consists of one to four numbers. There are two separate color
42 parameters: one for stroking and one for all other painting opera-
43 tions. Initial value: black.
44 */
45
edisonn@google.comb857a0c2013-06-25 20:45:40 +000046 SkColor fColor;
47 double fOpacity; // ca or CA
48 // TODO(edisonn): add here other color space options.
49
50 void setRGBColor(SkColor color) {
51 // TODO(edisonn): ASSERT DeviceRGB is the color space.
52 fColor = color;
53 }
54 // TODO(edisonn): double check the default values for all fields.
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000055 SkPdfColorOperator() : fColor(SK_ColorBLACK), fOpacity(1) {
56 NotOwnedString::init(&fColorSpace);
57 }
edisonn@google.comb857a0c2013-06-25 20:45:40 +000058
59 void applyGraphicsState(SkPaint* paint) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +000060 paint->setColor(SkColorSetA(fColor, (U8CPU)(fOpacity * 255)));
edisonn@google.comb857a0c2013-06-25 20:45:40 +000061 }
62};
63
64// TODO(edisonn): better class design.
edisonn@google.com3aac1f92013-07-02 22:42:53 +000065struct SkPdfGraphicsState {
edisonn@google.coma0cefa12013-07-28 18:34:14 +000066 // TODO(edisonn): deprecate and remove these!
edisonn@google.comb857a0c2013-06-25 20:45:40 +000067 double fCurPosX;
68 double fCurPosY;
69
70 double fCurFontSize;
71 bool fTextBlock;
72 SkPdfFont* fSkFont;
73 SkPath fPath;
74 bool fPathClosed;
75
edisonn@google.coma0cefa12013-07-28 18:34:14 +000076
77
78 double fTextLeading;
79 double fWordSpace;
80 double fCharSpace;
81
82 SkPdfResourceDictionary* fResources;
83
84
85 // TODO(edisonn): move most of these in canvas/paint?
86 // we could have some in canvas (matrixes?),
87 // some in 2 paints (stroking paint and non stroking paint)
88
89// TABLE 4.2 Device-independent graphics state parameters
90/*
91 * CTM array The current transformation matrix, which maps positions from user
92 coordinates to device coordinates (see Section 4.2, “Coordinate Sys-
93 tems”). This matrix is modified by each application of the coordi-
94 nate transformation operator, cm. Initial value: a matrix that
95 transforms default user coordinates to device coordinates.
96 */
97 SkMatrix fCTM;
98
99/*
100clipping path (internal) The current clipping path, which defines the boundary against
101 which all output is to be cropped (see Section 4.4.3, “Clipping Path
102 Operators”). Initial value: the boundary of the entire imageable
103 portion of the output page.
104 */
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000105 // Clip that is applied after the drawing is done!!!
106 bool fHasClipPathToApply;
107 SkPath fClipPath;
108
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000109 SkPdfColorOperator fStroking;
110 SkPdfColorOperator fNonStroking;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000111
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000112/*
113text state (various) A set of nine graphics state parameters that pertain only to the
114 painting of text. These include parameters that select the font, scale
115 the glyphs to an appropriate size, and accomplish other effects. The
116 text state parameters are described in Section 5.2, “Text State
117 Parameters and Operators.”
118 */
119
120 // TODO(edisonn): add SkPdfTextState class. remove these two existing fields
121 SkMatrix fMatrixTm;
122 SkMatrix fMatrixTlm;
123
124
125/*
126line width number The thickness, in user space units, of paths to be stroked (see “Line
127 Width” on page 152). Initial value: 1.0.
128 */
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000129 double fLineWidth;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000130
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000131
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000132/*
133line cap integer A code specifying the shape of the endpoints for any open path that
134 is stroked (see “Line Cap Style” on page 153). Initial value: 0, for
135 square butt caps.
136 */
137 // TODO (edisonn): implement defaults - page 153
138 int fLineCap;
139
140/*
141line join integer A code specifying the shape of joints between connected segments
142 of a stroked path (see “Line Join Style” on page 153). Initial value: 0,
143 for mitered joins.
144 */
145 // TODO (edisonn): implement defaults - page 153
146 int fLineJoin;
147
148/*
149miter limit number The maximum length of mitered line joins for stroked paths (see
150 “Miter Limit” on page 153). This parameter limits the length of
151 “spikes” produced when line segments join at sharp angles. Initial
152 value: 10.0, for a miter cutoff below approximately 11.5 degrees.
153 */
154 // TODO (edisonn): implement defaults - page 153
155 double fMiterLimit;
156
157/*
158dash pattern array and A description of the dash pattern to be used when paths are
159 number stroked (see “Line Dash Pattern” on page 155). Initial value: a solid
160 line.
161 */
162 SkScalar fDashArray[256]; // TODO(edisonn): allocate array?
163 int fDashArrayLength;
164 SkScalar fDashPhase;
165
166
167/*
168rendering intent name The rendering intent to be used when converting CIE-based colors
169 to device colors (see “Rendering Intents” on page 197). Default
170 value: RelativeColorimetric.
171 */
172 // TODO(edisonn): seems paper only. Verify.
173
174/*
175stroke adjustment boolean (PDF 1.2) A flag specifying whether to compensate for possible ras-
176 terization effects when stroking a path with a line width that is
177 small relative to the pixel resolution of the output device (see Sec-
178 tion 6.5.4, “Automatic Stroke Adjustment”). Note that this is con-
179 sidered a device-independent parameter, even though the details of
180 its effects are device-dependent. Initial value: false.
181 */
182 // TODO(edisonn): stroke adjustment low priority.
183
184
185/*
186blend mode name or array (PDF 1.4) The current blend mode to be used in the transparent
187 imaging model (see Sections 7.2.4, “Blend Mode,” and 7.5.2, “Spec-
188 ifying Blending Color Space and Blend Mode”). This parameter is
189 implicitly reset to its initial value at the beginning of execution of a
190 transparency group XObject (see Section 7.5.5, “Transparency
191 Group XObjects”). Initial value: Normal.
192 */
edisonn@google.come878e722013-07-29 19:10:58 +0000193 SkXfermode::Mode fBlendModes[256];
194 int fBlendModesLength;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000195
196/*
197soft mask dictionary (PDF 1.4) A soft-mask dictionary (see “Soft-Mask Dictionaries” on
198 or name page 445) specifying the mask shape or mask opacity values to be
199 used in the transparent imaging model (see “Source Shape and
200 Opacity” on page 421 and “Mask Shape and Opacity” on page 443),
201 or the name None if no such mask is specified. This parameter is
202 implicitly reset to its initial value at the beginning of execution of a
203 transparency group XObject (see Section 7.5.5, “Transparency
204 Group XObjects”). Initial value: None.
205 */
edisonn@google.com4ef4bed2013-07-29 22:14:45 +0000206 SkPdfSoftMaskDictionary* fSoftMaskDictionary;
207 SkBitmap fSMask;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000208
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000209
210/*
211alpha constant number (PDF 1.4) The constant shape or constant opacity value to be used
212 in the transparent imaging model (see “Source Shape and Opacity”
213 on page 421 and “Constant Shape and Opacity” on page 444).
214 There are two separate alpha constant parameters: one for stroking
215 and one for all other painting operations. This parameter is implic-
216 itly reset to its initial value at the beginning of execution of a trans-
217 parency group XObject (see Section 7.5.5, “Transparency Group
218 XObjects”). Initial value: 1.0.
219 */
220 double fAphaConstant;
221
222/*
223alpha source boolean (PDF 1.4) A flag specifying whether the current soft mask and alpha
224 constant parameters are to be interpreted as shape values (true) or
225 opacity values (false). This flag also governs the interpretation of
226 the SMask entry, if any, in an image dictionary (see Section 4.8.4,
227 “Image Dictionaries”). Initial value: false.
228 */
229 bool fAlphaSource;
230
231
232// TODO(edisonn): Device-dependent seem to be required only on the actual physical printer?
233// TABLE 4.3 Device-dependent graphics state parameters
234/*
235overprint boolean (PDF 1.2) A flag specifying (on output devices that support the
236 overprint control feature) whether painting in one set of colorants
237 should cause the corresponding areas of other colorants to be
238 erased (false) or left unchanged (true); see Section 4.5.6, “Over-
239 print Control.” In PDF 1.3, there are two separate overprint param-
240 eters: one for stroking and one for all other painting operations.
241 Initial value: false.
242 */
243
244
245/*
246overprint mode number (PDF 1.3) A code specifying whether a color component value of 0
247 in a DeviceCMYK color space should erase that component (0) or
248 leave it unchanged (1) when overprinting (see Section 4.5.6, “Over-
249 print Control”). Initial value: 0.
250 */
251
252
253/*
254black generation function (PDF 1.2) A function that calculates the level of the black color
255 or name component to use when converting RGB colors to CMYK (see Sec-
256 tion 6.2.3, “Conversion from DeviceRGB to DeviceCMYK”). Initial
257 value: installation-dependent.
258 */
259
260
261/*
262undercolor removal function (PDF 1.2) A function that calculates the reduction in the levels of
263 or name the cyan, magenta, and yellow color components to compensate for
264 the amount of black added by black generation (see Section 6.2.3,
265 “Conversion from DeviceRGB to DeviceCMYK”). Initial value: in-
266 stallation-dependent.
267 */
268
269
270/*
271transfer function, (PDF 1.2) A function that adjusts device gray or color component
272 array, or name levels to compensate for nonlinear response in a particular out-
273 put device (see Section 6.3, “Transfer Functions”). Initial value:
274 installation-dependent.
275 */
276
277
278/*
279halftone dictionary, (PDF 1.2) A halftone screen for gray and color rendering, specified
280 stream, or name as a halftone dictionary or stream (see Section 6.4, “Halftones”).
281 Initial value: installation-dependent.
282 */
283
284
285/*
286flatness number The precision with which curves are to be rendered on the output
287 device (see Section 6.5.1, “Flatness Tolerance”). The value of this
288 parameter gives the maximum error tolerance, measured in output
289 device pixels; smaller numbers give smoother curves at the expense
290 of more computation and memory use. Initial value: 1.0.
291 */
292
293
294/*
295smoothness number (PDF 1.3) The precision with which color gradients are to be ren-
296 dered on the output device (see Section 6.5.2, “Smoothness Toler-
297 ance”). The value of this parameter gives the maximum error
298 tolerance, expressed as a fraction of the range of each color compo-
299 nent; smaller numbers give smoother color transitions at the
300 expense of more computation and memory use. Initial value:
301 installation-dependent.
302 */
303
304
305
306
307
308
309
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000310 SkPdfGraphicsState() {
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000311 fCurPosX = 0.0;
312 fCurPosY = 0.0;
313 fCurFontSize = 0.0;
314 fTextBlock = false;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000315 fCTM = SkMatrix::I();
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000316 fMatrixTm = SkMatrix::I();
317 fMatrixTlm = SkMatrix::I();
318 fPathClosed = true;
319 fLineWidth = 0;
320 fTextLeading = 0;
321 fWordSpace = 0;
322 fCharSpace = 0;
323 fHasClipPathToApply = false;
324 fResources = NULL;
325 fSkFont = NULL;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000326 fLineCap = 0;
327 fLineJoin = 0;
328 fMiterLimit = 10.0;
329 fAphaConstant = 1.0;
330 fAlphaSource = false;
331 fDashArrayLength = 0;
332 fDashPhase = 0;
edisonn@google.come878e722013-07-29 19:10:58 +0000333 fBlendModesLength = 1;
334 fBlendModes[0] = SkXfermode::kSrc_Mode; // PDF: Normal Blend mode
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000335 }
336
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000337 // TODO(edisonn): make two functons instead, stroking and non stoking, avoid branching
338 void applyGraphicsState(SkPaint* paint, bool stroking);
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000339};
340
341// TODO(edisonn): better class design.
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000342// TODO(edisonn): could we remove it?
343// TODO(edisonn): rename to SkPdfInlineImage
344struct SkPdfInlineImage {
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000345 std::map<std::string, std::string> fKeyValuePairs;
346 std::string fImageData;
347};
348
349// TODO(edisonn): better class design.
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000350// TODO(edisonn): rename to SkPdfContext
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000351struct PdfContext {
352 std::stack<SkPdfObject*> fObjectStack;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000353 std::stack<SkPdfGraphicsState> fStateStack;
354 SkPdfGraphicsState fGraphicsState;
edisonn@google.com571c70b2013-07-10 17:09:50 +0000355 SkNativeParsedPDF* fPdfDoc;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000356 // TODO(edisonn): the allocator, could be freed after the page is done drawing.
357 SkPdfAllocator* fTmpPageAllocator;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000358 SkMatrix fOriginalMatrix;
359
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000360 SkPdfInlineImage fInlineImage;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000361
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000362 PdfContext(SkNativeParsedPDF* doc);
363 ~PdfContext();
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000364};
365
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000366// TODO(edisonn): temporary code, to report how much of the PDF we actually think we rendered.
367// TODO(edisonn): rename to SkPdfResult
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000368enum PdfResult {
369 kOK_PdfResult,
370 kPartial_PdfResult,
371 kNYI_PdfResult,
372 kIgnoreError_PdfResult,
373 kError_PdfResult,
374 kUnsupported_PdfResult,
375
376 kCount_PdfResult
377};
378
379#endif // __DEFINED__SkPdfBasics