3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "SkBBoxRecord.h"
11 void SkBBoxRecord::drawOval(const SkRect& rect, const SkPaint& paint) {
12 if (this->transformBounds(rect, &paint)) {
13 INHERITED::drawOval(rect, paint);
17 void SkBBoxRecord::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
18 if (this->transformBounds(rrect.rect(), &paint)) {
19 INHERITED::drawRRect(rrect, paint);
23 void SkBBoxRecord::drawRect(const SkRect& rect, const SkPaint& paint) {
24 if (this->transformBounds(rect, &paint)) {
25 INHERITED::drawRect(rect, paint);
29 void SkBBoxRecord::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
30 const SkPaint& paint) {
31 if (this->transformBounds(outer.rect(), &paint)) {
32 this->INHERITED::onDrawDRRect(outer, inner, paint);
36 void SkBBoxRecord::drawPath(const SkPath& path, const SkPaint& paint) {
37 if (path.isInverseFillType()) {
38 // If path is inverse filled, use the current clip bounds as the
39 // path's device-space bounding box.
41 if (this->getClipDeviceBounds(&clipBounds)) {
42 this->handleBBox(SkRect::Make(clipBounds));
43 INHERITED::drawPath(path, paint);
45 } else if (this->transformBounds(path.getBounds(), &paint)) {
46 INHERITED::drawPath(path, paint);
50 void SkBBoxRecord::drawPoints(PointMode mode, size_t count, const SkPoint pts[],
51 const SkPaint& paint) {
53 bbox.set(pts, SkToInt(count));
54 // Small min width value, just to ensure hairline point bounding boxes aren't empty.
55 // Even though we know hairline primitives are drawn one pixel wide, we do not use a
56 // minimum of 1 because the playback scale factor is unknown at record time. Later
57 // outsets will take care of adding additional padding for antialiasing and rounding out
58 // to integer device coordinates, guaranteeing that the rasterized pixels will be included
59 // in the computed bounds.
60 // Note: The device coordinate outset in SkBBoxHierarchyRecord::handleBBox is currently
61 // done in the recording coordinate space, which is wrong.
62 // http://code.google.com/p/skia/issues/detail?id=1021
63 static const SkScalar kMinWidth = 0.01f;
64 SkScalar halfStrokeWidth = SkMaxScalar(paint.getStrokeWidth(), kMinWidth) / 2;
65 bbox.outset(halfStrokeWidth, halfStrokeWidth);
66 if (this->transformBounds(bbox, &paint)) {
67 INHERITED::drawPoints(mode, count, pts, paint);
71 void SkBBoxRecord::drawPaint(const SkPaint& paint) {
73 if (this->getClipBounds(&bbox)) {
74 if (this->transformBounds(bbox, &paint)) {
75 INHERITED::drawPaint(paint);
80 void SkBBoxRecord::clear(SkColor color) {
81 SkISize size = this->getDeviceSize();
82 SkRect bbox = {0, 0, SkIntToScalar(size.width()), SkIntToScalar(size.height())};
83 this->handleBBox(bbox);
84 INHERITED::clear(color);
87 void SkBBoxRecord::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
88 const SkPaint& paint) {
90 paint.measureText(text, byteLength, &bbox);
91 SkPaint::FontMetrics metrics;
92 paint.getFontMetrics(&metrics);
94 // Vertical and aligned text need to be offset
95 if (paint.isVerticalText()) {
96 SkScalar h = bbox.fBottom - bbox.fTop;
97 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
99 bbox.fBottom -= h / 2;
101 // Pad top and bottom with max extents from FontMetrics
102 bbox.fBottom += metrics.fBottom;
103 bbox.fTop += metrics.fTop;
105 SkScalar w = bbox.fRight - bbox.fLeft;
106 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
108 bbox.fRight -= w / 2;
109 } else if (paint.getTextAlign() == SkPaint::kRight_Align) {
113 // Set vertical bounds to max extents from font metrics
114 bbox.fTop = metrics.fTop;
115 bbox.fBottom = metrics.fBottom;
118 // Pad horizontal bounds on each side by half of max vertical extents (this is sort of
119 // arbitrary, but seems to produce reasonable results, if there were a way of getting max
120 // glyph X-extents to pad by, that may be better here, but FontMetrics fXMin and fXMax seem
121 // incorrect on most platforms (too small in Linux, never even set in Windows).
122 SkScalar pad = (metrics.fBottom - metrics.fTop) / 2;
130 if (this->transformBounds(bbox, &paint)) {
131 INHERITED::onDrawText(text, byteLength, x, y, paint);
135 void SkBBoxRecord::drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
136 const SkPaint* paint) {
137 SkRect bbox = {left, top, left + bitmap.width(), top + bitmap.height()};
138 if (this->transformBounds(bbox, paint)) {
139 INHERITED::drawBitmap(bitmap, left, top, paint);
143 void SkBBoxRecord::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
144 const SkRect& dst, const SkPaint* paint,
145 DrawBitmapRectFlags flags) {
146 if (this->transformBounds(dst, paint)) {
147 INHERITED::drawBitmapRectToRect(bitmap, src, dst, paint, flags);
151 void SkBBoxRecord::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& mat,
152 const SkPaint* paint) {
154 SkRect bbox = {0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())};
156 if (this->transformBounds(bbox, paint)) {
157 INHERITED::drawBitmapMatrix(bitmap, mat, paint);
161 void SkBBoxRecord::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
162 const SkRect& dst, const SkPaint* paint) {
163 if (this->transformBounds(dst, paint)) {
164 INHERITED::drawBitmapNine(bitmap, center, dst, paint);
168 void SkBBoxRecord::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
169 const SkPaint& paint) {
171 bbox.set(pos, paint.countText(text, byteLength));
172 SkPaint::FontMetrics metrics;
173 paint.getFontMetrics(&metrics);
174 bbox.fTop += metrics.fTop;
175 bbox.fBottom += metrics.fBottom;
177 // pad on left and right by half of max vertical glyph extents
178 SkScalar pad = (metrics.fTop - metrics.fBottom) / 2;
182 if (this->transformBounds(bbox, &paint)) {
183 INHERITED::onDrawPosText(text, byteLength, pos, paint);
187 void SkBBoxRecord::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
188 SkScalar constY, const SkPaint& paint) {
189 size_t numChars = paint.countText(text, byteLength);
194 const SkFlatData* flatPaintData = this->getFlatPaintData(paint);
195 WriteTopBot(paint, *flatPaintData);
197 SkScalar top = flatPaintData->topBot()[0];
198 SkScalar bottom = flatPaintData->topBot()[1];
199 SkScalar pad = top - bottom;
202 bbox.fLeft = SK_ScalarMax;
203 bbox.fRight = SK_ScalarMin;
205 for (size_t i = 0; i < numChars; ++i) {
206 if (xpos[i] < bbox.fLeft) {
207 bbox.fLeft = xpos[i];
209 if (xpos[i] > bbox.fRight) {
210 bbox.fRight = xpos[i];
214 // pad horizontally by max glyph height
218 bbox.fTop = top + constY;
219 bbox.fBottom = bottom + constY;
221 if (!this->transformBounds(bbox, &paint)) {
224 // This is the equivalent of calling:
225 // INHERITED::drawPosTextH(text, byteLength, xpos, constY, paint);
226 // but we filled our flat paint beforehand so that we could get font metrics.
227 drawPosTextHImpl(text, byteLength, xpos, constY, paint, flatPaintData);
230 void SkBBoxRecord::drawSprite(const SkBitmap& bitmap, int left, int top,
231 const SkPaint* paint) {
233 bbox.set(SkIRect::MakeXYWH(left, top, bitmap.width(), bitmap.height()));
234 this->handleBBox(bbox); // directly call handleBBox, matrix is ignored
235 INHERITED::drawSprite(bitmap, left, top, paint);
238 void SkBBoxRecord::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
239 const SkMatrix* matrix, const SkPaint& paint) {
240 SkRect bbox = path.getBounds();
241 SkPaint::FontMetrics metrics;
242 paint.getFontMetrics(&metrics);
244 // pad out all sides by the max glyph height above baseline
245 SkScalar pad = metrics.fTop;
251 if (this->transformBounds(bbox, &paint)) {
252 INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint);
256 void SkBBoxRecord::drawVertices(VertexMode mode, int vertexCount,
257 const SkPoint vertices[], const SkPoint texs[],
258 const SkColor colors[], SkXfermode* xfer,
259 const uint16_t indices[], int indexCount,
260 const SkPaint& paint) {
262 bbox.set(vertices, vertexCount);
263 if (this->transformBounds(bbox, &paint)) {
264 INHERITED::drawVertices(mode, vertexCount, vertices, texs,
265 colors, xfer, indices, indexCount, paint);
269 void SkBBoxRecord::drawPicture(SkPicture& picture) {
270 if (picture.width() > 0 && picture.height() > 0 &&
271 this->transformBounds(SkRect::MakeWH(picture.width(), picture.height()), NULL)) {
272 INHERITED::drawPicture(picture);
276 bool SkBBoxRecord::transformBounds(const SkRect& bounds, const SkPaint* paint) {
277 SkRect outBounds = bounds;
281 // account for stroking, path effects, shadows, etc
282 if (paint->canComputeFastBounds()) {
284 outBounds = paint->computeFastBounds(outBounds, &temp);
286 // set bounds to current clip
287 if (!this->getClipBounds(&outBounds)) {
288 // current clip is empty
294 if (!outBounds.isEmpty() && !this->quickReject(outBounds)) {
295 this->getTotalMatrix().mapRect(&outBounds);
296 this->handleBBox(outBounds);