Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: gm/aaclip.cpp

Issue 16660002: SkDocument base for pdf, xps, etc. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | src/doc/SkDocument_PDF.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "gm.h" 8 #include "gm.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkPath.h" 10 #include "SkPath.h"
11 11
12 class SkWStream;
13 class SkDocument : public SkRefCnt {
14 public:
15 static SkDocument* CreatePDF(SkWStream*);
16
17 SkCanvas* beginPage(SkScalar width, SkScalar height,
18 const SkRect* content = NULL);
19 void endPage();
20 void close();
21
22 protected:
23 SkDocument(SkWStream*);
24 virtual ~SkDocument();
25
26 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height,
27 const SkRect& content) = 0;
28 virtual void onEndPage() = 0;
29 virtual void onClose(SkWStream*) = 0;
30
31 enum State {
32 kBeforePage_State,
33 kInPage_State,
34 kAfterPage_State,
35 kClosed_State
36 };
37 State getState() const { return fState; }
38
39 private:
40 SkWStream* fStream;
41
42 enum State {
43 kBeforePage_State,
44 kInPage_State,
45 kAfterPage_State,
46 kClosed_State
47 };
48 State fState;
49 };
50
51 #include "SkPDFDocument.h"
52 #include "SkPDFDevice.h"
53
54 class SkDocument_PDF : public SkDocument {
55 public:
56 SkDocument_PDF(SkWStream* stream) : SkDocument(stream) {
57 fDoc = new SkPDFDocument;
58 fCanvas = NULL;
59 fDevice = NULL;
60 }
61
62 virtual ~SkDocument_PDF() {
63 this->close();
64 delete fDoc;
65 }
66
67 protected:
68 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height,
69 const SkRect& content) SK_OVERRIDE {
70 this->endPage();
71
72 SkISize pageS, contentS;
73 SkMatrix matrix;
74
75 pageS.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height));
76 contentS.set(SkScalarRoundToInt(content.width()),
77 SkScalarRoundToInt(content.height()));
78 matrix.setTranslate(content.fLeft, content.fTop);
79
80 fDevice = new SkPDFDevice(pageS, contentS, matrix);
81 fCanvas = new SkCanvas(fDevice);
82 return fCanvas;
83 }
84
85 virtual void onEndPage() SK_OVERRIDE {
86 if (fCanvas) {
87 fCanvas->flush();
88 fDoc->appendPage(fDevice);
89
90 fCanvas->unref();
91 fDevice->unref();
92
93 fCanvas = NULL;
94 fDevice = NULL;
95 }
96 }
97
98 virtual void onClose(SkWStream* stream) SK_OVERRIDE {
99 fDoc->emitPDF(stream);
100
101 delete fDoc;
102 SkSafeUnref(fCanvas);
103 SkSafeUnref(fDevice);
104
105 fDoc = NULL;
106 fCanvas = NULL;
107 fDevice = NULL;
108 }
109
110 private:
111 SkPDFDocument* fDoc;
112 SkPDFDevice* fDevice;
113 SkCanvas* fCanvas;
114 };
115
116 ///////////////////////////////////////////////////////////////////////////////
117
118 SkDocument* SkDocument::CreatePDF(SkScalar width, SkScalar height,
119 const SkRect* content) {
120 if (width > 0 && height > 0) {
121 SkRect outer = SkRect::MakeWH(width, height);
122 SkRect inner;
123 if (content) {
124 inner = *content;
125 if (!inner.intersect(outer)) {
126 return NULL;
127 }
128 } else {
129 inner = outer;
130 }
131 return new SkDocument_PDF(width, height, inner);
132 }
133 return NULL;
134 }
135
136 SkDocument::SkDocument() : fState(kBeforePage_State) {
137 }
138
139 SkDocument::~SkDocument() {
140 }
141
142 SkCanvas* SkDocument::beginPage(SkScalar width, SkScalar height,
143 const SkRect* content) {
144 if (width <= 0 || height <= 0) {
145 return NULL;
146 }
147
148 SkRect outer = SkRect::MakeWH(width, height);
149 SkRect inner;
150 if (content) {
151 inner = *content;
152 if (!inner.intersect(outer)) {
153 return NULL;
154 }
155 } else {
156 inner = outer;
157 }
158
159 for (;;) {
160 switch (fState) {
161 case kBeforePage_State:
162 case kAfterPage_State:
163 fState = kInPage_State;
164 return this->beginPage(width, height, inner);
165 case kInPage_State:
166 this->endPage();
167 break;
168 case kClosed_State:
169 return NULL;
170 }
171 }
172 SkASSERT(!"never get here");
173 return NULL;
174 }
175
176 void SkDocument::endPage() {
177 if (kInPage_State == fState) {
178 this->onEndPage();
179 }
180 }
181
182 void SkDocument::close() {
183 for (;;) {
184 switch (fState) {
185 case kBeforePage_State:
186 case kAfterPage_State:
187 fState = kClosed_State;
188 this->onClose(fStream);
189 return;
190 case kInPage_State:
191 this->endPage();
192 break;
193 case kClosed_State:
194 return;
195 }
196 }
197 }
198
199 static void draw(SkCanvas* canvas) {
200 SkPaint paint;
201 paint.setAntiAlias(true);
202 paint.setTextSize(50);
203 canvas->drawText("Hello PDF", 9, 100, 100, paint);
204
205 paint.setStyle(SkPaint::kStroke_Style);
206 paint.setStrokeWidth(2);
207 SkRect r = { 0, 0, 8.5f * 72, 11.0f * 72 };
208 r.inset(72 * 3 / 4, 72 * 3 / 4);
209 canvas->drawRect(r, paint);
210 }
211
212 static void test_pdf(SkCanvas* canvas) {
213 SkDocument* doc = SkDocument::CreatePDF(72 * 8.5f, 72 * 11.0f);
214 draw(doc->beginPage());
215 doc->endPage();
216
217 SkFILEWStream stream("/skia/trunk/test.pdf");
218 doc->close(&stream);
219 doc->unref();
220 }
221
222 ///////////////////////////////////////////////////////////////////////////////
223
12 #include "SkGradientShader.h" 224 #include "SkGradientShader.h"
13 static void test_shallow_gradient(SkCanvas* canvas, SkScalar width, SkScalar hei ght) { 225 static void test_shallow_gradient(SkCanvas* canvas, SkScalar width, SkScalar hei ght) {
14 SkColor colors[] = { 0xFF7F7F7F, 0xFF7F7F7F, 0xFF000000 }; 226 SkColor colors[] = { 0xFF7F7F7F, 0xFF7F7F7F, 0xFF000000 };
15 SkScalar pos[] = { 0, 0.35f, SK_Scalar1 }; 227 SkScalar pos[] = { 0, 0.35f, SK_Scalar1 };
16 SkPoint pts[] = { { 0, 0 }, { width, height } }; 228 SkPoint pts[] = { { 0, 0 }, { width, height } };
17 SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos, 229 SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos,
18 SK_ARRAY_COUNT(colors), 230 SK_ARRAY_COUNT(colors),
19 SkShader::kClamp_TileMode); 231 SkShader::kClamp_TileMode);
20 SkPaint paint; 232 SkPaint paint;
21 paint.setShader(s)->unref(); 233 paint.setShader(s)->unref();
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 protected: 424 protected:
213 virtual SkString onShortName() SK_OVERRIDE { 425 virtual SkString onShortName() SK_OVERRIDE {
214 return SkString("aaclip"); 426 return SkString("aaclip");
215 } 427 }
216 428
217 virtual SkISize onISize() SK_OVERRIDE { 429 virtual SkISize onISize() SK_OVERRIDE {
218 return SkISize::Make(640, 480); 430 return SkISize::Make(640, 480);
219 } 431 }
220 432
221 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 433 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
434 if (true) {
435 test_pdf(canvas); return;
436 }
222 if (false) { 437 if (false) {
223 SkRect bounds; 438 SkRect bounds;
224 canvas->getClipBounds(&bounds); 439 canvas->getClipBounds(&bounds);
225 test_shallow_gradient(canvas, bounds.width(), bounds.height()); retu rn; 440 test_shallow_gradient(canvas, bounds.width(), bounds.height()); retu rn;
226 } 441 }
227 if (false) { 442 if (false) {
228 test_giant_dash(canvas); return; 443 test_giant_dash(canvas); return;
229 } 444 }
230 if (false) { 445 if (false) {
231 test_grad(canvas); return; 446 test_grad(canvas); return;
(...skipping 23 matching lines...) Expand all
255 draw_rect_tests(canvas); 470 draw_rect_tests(canvas);
256 } 471 }
257 472
258 virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; } 473 virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; }
259 474
260 private: 475 private:
261 typedef skiagm::GM INHERITED; 476 typedef skiagm::GM INHERITED;
262 }; 477 };
263 478
264 DEF_GM( return SkNEW(AAClipGM); ) 479 DEF_GM( return SkNEW(AAClipGM); )
OLDNEW
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | src/doc/SkDocument_PDF.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698