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

Side by Side Diff: skia/ext/vector_platform_device_skia.cc

Issue 6499024: Connect Skia PDF backend to printing subsystem. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase (80721) Created 9 years, 8 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
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "skia/ext/vector_platform_device_skia.h"
6
7 #include "skia/ext/bitmap_platform_device.h"
8 #include "third_party/skia/include/core/SkClipStack.h"
9 #include "third_party/skia/include/core/SkDraw.h"
10 #include "third_party/skia/include/core/SkRect.h"
11 #include "third_party/skia/include/core/SkRegion.h"
12 #include "third_party/skia/include/core/SkScalar.h"
13
14 namespace skia {
15
16 SkDevice* VectorPlatformDeviceSkiaFactory::newDevice(SkCanvas* noUsed,
17 SkBitmap::Config config,
18 int width, int height,
19 bool isOpaque,
20 bool isForLayer) {
21 SkASSERT(config == SkBitmap::kARGB_8888_Config);
22 SkPDFDevice::OriginTransform flip = SkPDFDevice::kFlip_OriginTransform;
23 if (isForLayer)
24 flip = SkPDFDevice::kNoFlip_OriginTransform;
25 return new VectorPlatformDeviceSkia(width, height, flip);
26 }
27
28 static inline SkBitmap makeABitmap(int width, int height) {
29 SkBitmap bitmap;
30 bitmap.setConfig(SkBitmap::kNo_Config, width, height);
31 return bitmap;
32 }
33
34 VectorPlatformDeviceSkia::VectorPlatformDeviceSkia(
35 int width, int height, SkPDFDevice::OriginTransform flip)
36 : PlatformDevice(makeABitmap(width, height)),
37 pdf_device_(new SkPDFDevice(width, height, flip)) {
38 pdf_device_->unref(); // SkRefPtr and new both took a reference.
39 base_transform_.reset();
40 }
41
42 VectorPlatformDeviceSkia::~VectorPlatformDeviceSkia() {
43 }
44
45 bool VectorPlatformDeviceSkia::IsVectorial() {
46 return true;
47 }
48
49 bool VectorPlatformDeviceSkia::IsNativeFontRenderingAllowed() {
50 return false;
51 }
52
53 PlatformDevice::PlatformSurface VectorPlatformDeviceSkia::BeginPlatformPaint() {
54 // Even when drawing a vector representation of the page, we have to
55 // provide a raster surface for plugins to render into - they don't have
56 // a vector interface. Therefore we create a BitmapPlatformDevice here
57 // and return the context from it, then layer on the raster data as an
58 // image in EndPlatformPaint.
59 DCHECK(raster_surface_ == NULL);
60 #if defined(OS_WIN)
61 raster_surface_ = BitmapPlatformDevice::create(pdf_device_->width(),
62 pdf_device_->height(),
63 false, /* not opaque */
64 NULL);
65 #elif defined(OS_LINUX)
66 raster_surface_ = BitmapPlatformDevice::Create(pdf_device_->width(),
67 pdf_device_->height(),
68 false /* not opaque */);
69 #endif
70 raster_surface_->unref(); // SkRefPtr and create both took a reference.
71
72 // Set it to be transparent.
73 SkCanvas canvas(raster_surface_.get());
74 SkPaint transparent;
75 transparent.setColor(SK_ColorBLACK);
76 canvas.drawPaint(transparent);
77 return raster_surface_->BeginPlatformPaint();
78 }
79
80 void VectorPlatformDeviceSkia::EndPlatformPaint() {
81 DCHECK(raster_surface_ != NULL);
82 SkPaint paint;
83 pdf_device_->drawSprite(SkDraw(),
84 raster_surface_->accessBitmap(false),
85 base_transform_.getTranslateX(),
86 base_transform_.getTranslateY(),
87 paint);
88 raster_surface_ = NULL;
89 }
90
91 SkDeviceFactory* VectorPlatformDeviceSkia::getDeviceFactory() {
92 return SkNEW(VectorPlatformDeviceSkiaFactory);
93 }
94
95 uint32_t VectorPlatformDeviceSkia::getDeviceCapabilities() {
96 return kVector_Capability;
97 }
98
99 int VectorPlatformDeviceSkia::width() const {
100 return pdf_device_->width();
101 }
102
103 int VectorPlatformDeviceSkia::height() const {
104 return pdf_device_->height();
105 }
106
107 void VectorPlatformDeviceSkia::setMatrixClip(const SkMatrix& matrix,
108 const SkRegion& region,
109 const SkClipStack& stack) {
110 SkMatrix transform = base_transform_;
111 transform.preConcat(matrix);
112
113 DCHECK(SkMatrix::kTranslate_Mask == base_transform_.getType() ||
114 SkMatrix::kIdentity_Mask == base_transform_.getType());
115 SkRegion clip = region;
116 clip.translate(base_transform_.getTranslateX(),
117 base_transform_.getTranslateY());
118
119 pdf_device_->setMatrixClip(transform, clip, stack);
120 }
121
122 bool VectorPlatformDeviceSkia::readPixels(const SkIRect& srcRect,
123 SkBitmap* bitmap) {
124 return false;
125 }
126
127 void VectorPlatformDeviceSkia::drawPaint(const SkDraw& draw,
128 const SkPaint& paint) {
129 pdf_device_->drawPaint(draw, paint);
130 }
131
132 void VectorPlatformDeviceSkia::drawPoints(const SkDraw& draw,
133 SkCanvas::PointMode mode,
134 size_t count, const SkPoint pts[],
135 const SkPaint& paint) {
136 pdf_device_->drawPoints(draw, mode, count, pts, paint);
137 }
138
139 void VectorPlatformDeviceSkia::drawRect(const SkDraw& draw,
140 const SkRect& rect,
141 const SkPaint& paint) {
142 pdf_device_->drawRect(draw, rect, paint);
143 }
144
145 void VectorPlatformDeviceSkia::drawPath(const SkDraw& draw,
146 const SkPath& path,
147 const SkPaint& paint,
148 const SkMatrix* prePathMatrix,
149 bool pathIsMutable) {
150 pdf_device_->drawPath(draw, path, paint, prePathMatrix, pathIsMutable);
151 }
152
153 void VectorPlatformDeviceSkia::drawBitmap(const SkDraw& draw,
154 const SkBitmap& bitmap,
155 const SkIRect* srcRectOrNull,
156 const SkMatrix& matrix,
157 const SkPaint& paint) {
158 pdf_device_->drawBitmap(draw, bitmap, srcRectOrNull, matrix, paint);
159 }
160
161 void VectorPlatformDeviceSkia::drawSprite(const SkDraw& draw,
162 const SkBitmap& bitmap,
163 int x, int y,
164 const SkPaint& paint) {
165 pdf_device_->drawSprite(draw, bitmap, x, y, paint);
166 }
167
168 void VectorPlatformDeviceSkia::drawText(const SkDraw& draw,
169 const void* text,
170 size_t byteLength,
171 SkScalar x,
172 SkScalar y,
173 const SkPaint& paint) {
174 pdf_device_->drawText(draw, text, byteLength, x, y, paint);
175 }
176
177 void VectorPlatformDeviceSkia::drawPosText(const SkDraw& draw,
178 const void* text,
179 size_t len,
180 const SkScalar pos[],
181 SkScalar constY,
182 int scalarsPerPos,
183 const SkPaint& paint) {
184 pdf_device_->drawPosText(draw, text, len, pos, constY, scalarsPerPos, paint);
185 }
186
187 void VectorPlatformDeviceSkia::drawTextOnPath(const SkDraw& draw,
188 const void* text,
189 size_t len,
190 const SkPath& path,
191 const SkMatrix* matrix,
192 const SkPaint& paint) {
193 pdf_device_->drawTextOnPath(draw, text, len, path, matrix, paint);
194 }
195
196 void VectorPlatformDeviceSkia::drawVertices(const SkDraw& draw,
197 SkCanvas::VertexMode vmode,
198 int vertexCount,
199 const SkPoint vertices[],
200 const SkPoint texs[],
201 const SkColor colors[],
202 SkXfermode* xmode,
203 const uint16_t indices[],
204 int indexCount,
205 const SkPaint& paint) {
206 pdf_device_->drawVertices(draw, vmode, vertexCount, vertices, texs, colors,
207 xmode, indices, indexCount, paint);
208 }
209
210 void VectorPlatformDeviceSkia::drawDevice(const SkDraw& draw,
211 SkDevice* device,
212 int x,
213 int y,
214 const SkPaint& paint) {
215 SkDevice* real_device = device;
216 if ((device->getDeviceCapabilities() & kVector_Capability)) {
217 // Assume that a vectorial device means a VectorPlatformDeviceSkia, we need
218 // to unwrap the embedded SkPDFDevice.
219 VectorPlatformDeviceSkia* vector_device =
220 static_cast<VectorPlatformDeviceSkia*>(device);
221 real_device = vector_device->pdf_device_.get();
222 }
223 pdf_device_->drawDevice(draw, real_device, x, y, paint);
224 }
225
226 #if defined(OS_WIN)
227 void VectorPlatformDeviceSkia::drawToHDC(HDC dc,
228 int x,
229 int y,
230 const RECT* src_rect) {
231 SkASSERT(false);
232 }
233 #endif
234
235 void VectorPlatformDeviceSkia::setInitialTransform(int xOffset, int yOffset,
236 float scale_factor) {
237 // TODO(vandebo) Supporting a scale factor is some work because we have to
238 // transform both matrices and clips that come in, but Region only supports
239 // translation. Instead, we could change SkPDFDevice to include it in the
240 // initial transform. Delay that work until we would use it. Also checked
241 // in setMatrixClip.
242 DCHECK_EQ(1.0f, scale_factor);
243
244 base_transform_.setTranslate(xOffset, yOffset);
245 SkScalar scale = SkFloatToScalar(scale_factor);
246 base_transform_.postScale(scale, scale);
247
248 SkMatrix matrix;
249 matrix.reset();
250 SkRegion region;
251 SkClipStack stack;
252 setMatrixClip(matrix, region, stack);
253 }
254
255 } // namespace skia
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698