blob: d17dc816f7fdc6ff496b9e3b6292dcecc9de122c [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.com3aa35552013-08-14 18:26:20 +00007#include "SkPdfUtils.h"
edisonn@google.com131d4ee2013-06-26 17:48:12 +00008
edisonn@google.comb857a0c2013-06-25 20:45:40 +00009#include <stack>
10
edisonn@google.comb857a0c2013-06-25 20:45:40 +000011class SkPdfFont;
12class SkPdfDoc;
edisonn@google.com3aa35552013-08-14 18:26:20 +000013class SkPdfNativeObject;
edisonn@google.com131d4ee2013-06-26 17:48:12 +000014class SkPdfResourceDictionary;
edisonn@google.com4ef4bed2013-07-29 22:14:45 +000015class SkPdfSoftMaskDictionary;
edisonn@google.comb857a0c2013-06-25 20:45:40 +000016
edisonn@google.com3aa35552013-08-14 18:26:20 +000017class SkPdfNativeDoc;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000018class SkPdfAllocator;
edisonn@google.com3aac1f92013-07-02 22:42:53 +000019
edisonn@google.comb857a0c2013-06-25 20:45:40 +000020// TODO(edisonn): better class design.
edisonn@google.come2e01ff2013-08-02 20:24:48 +000021class SkPdfColorOperator {
edisonn@google.coma0cefa12013-07-28 18:34:14 +000022
23 /*
24 color space name or array The current color space in which color values are to be interpreted
25 (see Section 4.5, “Color Spaces”). There are two separate color space
26 parameters: one for stroking and one for all other painting opera-
27 tions. Initial value: DeviceGray.
28 */
29
30 // TODO(edisonn): implement the array part too
edisonn@google.com571c70b2013-07-10 17:09:50 +000031 // does not own the char*
edisonn@google.come2e01ff2013-08-02 20:24:48 +000032// TODO(edisonn): remove this public, let fields be private
33// TODO(edisonn): make color space an enum!
34public:
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000035 NotOwnedString fColorSpace;
edisonn@google.com3aa35552013-08-14 18:26:20 +000036 SkPdfNativeObject* fPattern;
edisonn@google.coma0cefa12013-07-28 18:34:14 +000037
38 /*
39 color (various) The current color to be used during painting operations (see Section
40 4.5, “Color Spaces”). The type and interpretation of this parameter
41 depend on the current color space; for most color spaces, a color
42 value consists of one to four numbers. There are two separate color
43 parameters: one for stroking and one for all other painting opera-
44 tions. Initial value: black.
45 */
46
edisonn@google.comb857a0c2013-06-25 20:45:40 +000047 SkColor fColor;
48 double fOpacity; // ca or CA
edisonn@google.come2e01ff2013-08-02 20:24:48 +000049
edisonn@google.comb857a0c2013-06-25 20:45:40 +000050 // TODO(edisonn): add here other color space options.
51
edisonn@google.come2e01ff2013-08-02 20:24:48 +000052public:
edisonn@google.comb857a0c2013-06-25 20:45:40 +000053 void setRGBColor(SkColor color) {
54 // TODO(edisonn): ASSERT DeviceRGB is the color space.
edisonn@google.come2e01ff2013-08-02 20:24:48 +000055 fPattern = NULL;
edisonn@google.comb857a0c2013-06-25 20:45:40 +000056 fColor = color;
57 }
58 // TODO(edisonn): double check the default values for all fields.
edisonn@google.come2e01ff2013-08-02 20:24:48 +000059 SkPdfColorOperator() : fPattern(NULL), fColor(SK_ColorBLACK), fOpacity(1) {
60 NotOwnedString::init(&fColorSpace, "DeviceRGB");
61 }
62
63 void setColorSpace(NotOwnedString* colorSpace) {
64 fColorSpace = *colorSpace;
65 fPattern = NULL;
66 }
67
edisonn@google.com3aa35552013-08-14 18:26:20 +000068 void setPatternColorSpace(SkPdfNativeObject* pattern) {
edisonn@google.come2e01ff2013-08-02 20:24:48 +000069 fColorSpace.fBuffer = (const unsigned char*)"Pattern";
70 fColorSpace.fBytes = 7; // strlen("Pattern")
71 fPattern = pattern;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +000072 }
edisonn@google.comb857a0c2013-06-25 20:45:40 +000073
74 void applyGraphicsState(SkPaint* paint) {
edisonn@google.com96ba3aa2013-07-28 20:04:35 +000075 paint->setColor(SkColorSetA(fColor, (U8CPU)(fOpacity * 255)));
edisonn@google.comb857a0c2013-06-25 20:45:40 +000076 }
77};
78
79// TODO(edisonn): better class design.
edisonn@google.com3aac1f92013-07-02 22:42:53 +000080struct SkPdfGraphicsState {
edisonn@google.coma0cefa12013-07-28 18:34:14 +000081 // TODO(edisonn): deprecate and remove these!
edisonn@google.comb857a0c2013-06-25 20:45:40 +000082 double fCurPosX;
83 double fCurPosY;
84
85 double fCurFontSize;
86 bool fTextBlock;
87 SkPdfFont* fSkFont;
88 SkPath fPath;
89 bool fPathClosed;
90
edisonn@google.coma0cefa12013-07-28 18:34:14 +000091
92
93 double fTextLeading;
94 double fWordSpace;
95 double fCharSpace;
96
97 SkPdfResourceDictionary* fResources;
98
99
100 // TODO(edisonn): move most of these in canvas/paint?
101 // we could have some in canvas (matrixes?),
102 // some in 2 paints (stroking paint and non stroking paint)
103
104// TABLE 4.2 Device-independent graphics state parameters
105/*
106 * CTM array The current transformation matrix, which maps positions from user
107 coordinates to device coordinates (see Section 4.2, “Coordinate Sys-
108 tems”). This matrix is modified by each application of the coordi-
109 nate transformation operator, cm. Initial value: a matrix that
110 transforms default user coordinates to device coordinates.
111 */
112 SkMatrix fCTM;
113
edisonn@google.com0f901902013-08-07 11:56:16 +0000114 SkMatrix fContentStreamMatrix;
115
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000116/*
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;
edisonn@google.comb0145ce2013-08-05 16:23:23 +0000224 // TODO(edisonn): make sMask private, add setter and getter, ref/unref/..., at the moment we most likely leask
225 SkBitmap* fSMask;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000226
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000227
228/*
229alpha constant number (PDF 1.4) The constant shape or constant opacity value to be used
230 in the transparent imaging model (see “Source Shape and Opacity”
231 on page 421 and “Constant Shape and Opacity” on page 444).
232 There are two separate alpha constant parameters: one for stroking
233 and one for all other painting operations. This parameter is implic-
234 itly reset to its initial value at the beginning of execution of a trans-
235 parency group XObject (see Section 7.5.5, “Transparency Group
236 XObjects”). Initial value: 1.0.
237 */
238 double fAphaConstant;
239
240/*
241alpha source boolean (PDF 1.4) A flag specifying whether the current soft mask and alpha
242 constant parameters are to be interpreted as shape values (true) or
243 opacity values (false). This flag also governs the interpretation of
244 the SMask entry, if any, in an image dictionary (see Section 4.8.4,
245 “Image Dictionaries”). Initial value: false.
246 */
247 bool fAlphaSource;
248
249
250// TODO(edisonn): Device-dependent seem to be required only on the actual physical printer?
251// TABLE 4.3 Device-dependent graphics state parameters
252/*
253overprint boolean (PDF 1.2) A flag specifying (on output devices that support the
254 overprint control feature) whether painting in one set of colorants
255 should cause the corresponding areas of other colorants to be
256 erased (false) or left unchanged (true); see Section 4.5.6, “Over-
257 print Control.” In PDF 1.3, there are two separate overprint param-
258 eters: one for stroking and one for all other painting operations.
259 Initial value: false.
260 */
261
262
263/*
264overprint mode number (PDF 1.3) A code specifying whether a color component value of 0
265 in a DeviceCMYK color space should erase that component (0) or
266 leave it unchanged (1) when overprinting (see Section 4.5.6, “Over-
267 print Control”). Initial value: 0.
268 */
269
270
271/*
272black generation function (PDF 1.2) A function that calculates the level of the black color
273 or name component to use when converting RGB colors to CMYK (see Sec-
274 tion 6.2.3, “Conversion from DeviceRGB to DeviceCMYK”). Initial
275 value: installation-dependent.
276 */
277
278
279/*
280undercolor removal function (PDF 1.2) A function that calculates the reduction in the levels of
281 or name the cyan, magenta, and yellow color components to compensate for
282 the amount of black added by black generation (see Section 6.2.3,
283 “Conversion from DeviceRGB to DeviceCMYK”). Initial value: in-
284 stallation-dependent.
285 */
286
287
288/*
289transfer function, (PDF 1.2) A function that adjusts device gray or color component
290 array, or name levels to compensate for nonlinear response in a particular out-
291 put device (see Section 6.3, “Transfer Functions”). Initial value:
292 installation-dependent.
293 */
294
295
296/*
297halftone dictionary, (PDF 1.2) A halftone screen for gray and color rendering, specified
298 stream, or name as a halftone dictionary or stream (see Section 6.4, “Halftones”).
299 Initial value: installation-dependent.
300 */
301
302
303/*
304flatness number The precision with which curves are to be rendered on the output
305 device (see Section 6.5.1, “Flatness Tolerance”). The value of this
306 parameter gives the maximum error tolerance, measured in output
307 device pixels; smaller numbers give smoother curves at the expense
308 of more computation and memory use. Initial value: 1.0.
309 */
310
311
312/*
313smoothness number (PDF 1.3) The precision with which color gradients are to be ren-
314 dered on the output device (see Section 6.5.2, “Smoothness Toler-
315 ance”). The value of this parameter gives the maximum error
316 tolerance, expressed as a fraction of the range of each color compo-
317 nent; smaller numbers give smoother color transitions at the
318 expense of more computation and memory use. Initial value:
319 installation-dependent.
320 */
321
322
323
324
325
326
327
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000328 SkPdfGraphicsState() {
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000329 fCurPosX = 0.0;
330 fCurPosY = 0.0;
331 fCurFontSize = 0.0;
332 fTextBlock = false;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000333 fCTM = SkMatrix::I();
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000334 fMatrixTm = SkMatrix::I();
335 fMatrixTlm = SkMatrix::I();
336 fPathClosed = true;
337 fLineWidth = 0;
338 fTextLeading = 0;
339 fWordSpace = 0;
340 fCharSpace = 0;
341 fHasClipPathToApply = false;
342 fResources = NULL;
343 fSkFont = NULL;
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000344 fLineCap = 0;
345 fLineJoin = 0;
346 fMiterLimit = 10.0;
347 fAphaConstant = 1.0;
348 fAlphaSource = false;
349 fDashArrayLength = 0;
350 fDashPhase = 0;
edisonn@google.come878e722013-07-29 19:10:58 +0000351 fBlendModesLength = 1;
352 fBlendModes[0] = SkXfermode::kSrc_Mode; // PDF: Normal Blend mode
edisonn@google.com91ce6982013-08-05 20:45:40 +0000353 fSMask = NULL;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000354 }
355
edisonn@google.coma0cefa12013-07-28 18:34:14 +0000356 // TODO(edisonn): make two functons instead, stroking and non stoking, avoid branching
357 void applyGraphicsState(SkPaint* paint, bool stroking);
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000358};
359
360// TODO(edisonn): better class design.
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000361// TODO(edisonn): rename to SkPdfContext
edisonn@google.com33f11b62013-08-14 21:35:27 +0000362class SkPdfContext {
363public:
364 std::stack<SkPdfNativeObject*> fObjectStack;
edisonn@google.com3aac1f92013-07-02 22:42:53 +0000365 std::stack<SkPdfGraphicsState> fStateStack;
366 SkPdfGraphicsState fGraphicsState;
edisonn@google.com33f11b62013-08-14 21:35:27 +0000367 SkPdfNativeDoc* fPdfDoc;
edisonn@google.com2ccc3af2013-07-23 17:43:18 +0000368 // TODO(edisonn): the allocator, could be freed after the page is done drawing.
369 SkPdfAllocator* fTmpPageAllocator;
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000370 SkMatrix fOriginalMatrix;
371
edisonn@google.com3aa35552013-08-14 18:26:20 +0000372 SkPdfContext(SkPdfNativeDoc* doc);
373 ~SkPdfContext();
edisonn@google.comb857a0c2013-06-25 20:45:40 +0000374};
375
376#endif // __DEFINED__SkPdfBasics