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