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::drawPath(const SkPath& path, const SkPaint& paint) {
30 if (path.isInverseFillType()) {
31 // If path is inverse filled, use the current clip bounds as the
32 // path's device-space bounding box.
34 if (this->getClipDeviceBounds(&clipBounds)) {
35 this->handleBBox(SkRect::Make(clipBounds));
36 INHERITED::drawPath(path, paint);
38 } else if (this->transformBounds(path.getBounds(), &paint)) {
39 INHERITED::drawPath(path, paint);
43 void SkBBoxRecord::drawPoints(PointMode mode, size_t count, const SkPoint pts[],
44 const SkPaint& paint) {
47 // Small min width value, just to ensure hairline point bounding boxes aren't empty.
48 // Even though we know hairline primitives are drawn one pixel wide, we do not use a
49 // minimum of 1 because the playback scale factor is unknown at record time. Later
50 // outsets will take care of adding additional padding for antialiasing and rounding out
51 // to integer device coordinates, guaranteeing that the rasterized pixels will be included
52 // in the computed bounds.
53 // Note: The device coordinate outset in SkBBoxHierarchyRecord::handleBBox is currently
54 // done in the recording coordinate space, which is wrong.
55 // http://code.google.com/p/skia/issues/detail?id=1021
56 static const SkScalar kMinWidth = 0.01f;
57 SkScalar halfStrokeWidth = SkMaxScalar(paint.getStrokeWidth(), kMinWidth) / 2;
58 bbox.outset(halfStrokeWidth, halfStrokeWidth);
59 if (this->transformBounds(bbox, &paint)) {
60 INHERITED::drawPoints(mode, count, pts, paint);
64 void SkBBoxRecord::drawPaint(const SkPaint& paint) {
66 if (this->getClipBounds(&bbox)) {
67 if (this->transformBounds(bbox, &paint)) {
68 INHERITED::drawPaint(paint);
73 void SkBBoxRecord::clear(SkColor color) {
74 SkISize size = this->getDeviceSize();
75 SkRect bbox = {0, 0, SkIntToScalar(size.width()), SkIntToScalar(size.height())};
76 this->handleBBox(bbox);
77 INHERITED::clear(color);
80 void SkBBoxRecord::drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
81 const SkPaint& paint) {
83 paint.measureText(text, byteLength, &bbox);
84 SkPaint::FontMetrics metrics;
85 paint.getFontMetrics(&metrics);
87 // Vertical and aligned text need to be offset
88 if (paint.isVerticalText()) {
89 SkScalar h = bbox.fBottom - bbox.fTop;
90 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
92 bbox.fBottom -= h / 2;
94 // Pad top and bottom with max extents from FontMetrics
95 bbox.fBottom += metrics.fBottom;
96 bbox.fTop += metrics.fTop;
98 SkScalar w = bbox.fRight - bbox.fLeft;
99 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
101 bbox.fRight -= w / 2;
102 } else if (paint.getTextAlign() == SkPaint::kRight_Align) {
106 // Set vertical bounds to max extents from font metrics
107 bbox.fTop = metrics.fTop;
108 bbox.fBottom = metrics.fBottom;
111 // Pad horizontal bounds on each side by half of max vertical extents (this is sort of
112 // arbitrary, but seems to produce reasonable results, if there were a way of getting max
113 // glyph X-extents to pad by, that may be better here, but FontMetrics fXMin and fXMax seem
114 // incorrect on most platforms (too small in Linux, never even set in Windows).
115 SkScalar pad = (metrics.fBottom - metrics.fTop) / 2;
123 if (this->transformBounds(bbox, &paint)) {
124 INHERITED::drawText(text, byteLength, x, y, paint);
128 void SkBBoxRecord::drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
129 const SkPaint* paint) {
130 SkRect bbox = {left, top, left + bitmap.width(), top + bitmap.height()};
131 if (this->transformBounds(bbox, paint)) {
132 INHERITED::drawBitmap(bitmap, left, top, paint);
136 void SkBBoxRecord::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
137 const SkRect& dst, const SkPaint* paint,
138 DrawBitmapRectFlags flags) {
139 if (this->transformBounds(dst, paint)) {
140 INHERITED::drawBitmapRectToRect(bitmap, src, dst, paint, flags);
144 void SkBBoxRecord::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& mat,
145 const SkPaint* paint) {
147 SkRect bbox = {0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())};
149 if (this->transformBounds(bbox, paint)) {
150 INHERITED::drawBitmapMatrix(bitmap, mat, paint);
154 void SkBBoxRecord::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
155 const SkRect& dst, const SkPaint* paint) {
156 if (this->transformBounds(dst, paint)) {
157 INHERITED::drawBitmapNine(bitmap, center, dst, paint);
161 void SkBBoxRecord::drawPosText(const void* text, size_t byteLength,
162 const SkPoint pos[], const SkPaint& paint) {
164 bbox.set(pos, paint.countText(text, byteLength));
165 SkPaint::FontMetrics metrics;
166 paint.getFontMetrics(&metrics);
167 bbox.fTop += metrics.fTop;
168 bbox.fBottom += metrics.fBottom;
170 // pad on left and right by half of max vertical glyph extents
171 SkScalar pad = (metrics.fTop - metrics.fBottom) / 2;
175 if (this->transformBounds(bbox, &paint)) {
176 INHERITED::drawPosText(text, byteLength, pos, paint);
180 void SkBBoxRecord::drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
181 SkScalar constY, const SkPaint& paint) {
182 size_t numChars = paint.countText(text, byteLength);
187 const SkFlatData* flatPaintData = this->getFlatPaintData(paint);
188 WriteTopBot(paint, *flatPaintData);
190 SkScalar top = flatPaintData->topBot()[0];
191 SkScalar bottom = flatPaintData->topBot()[1];
192 SkScalar pad = top - bottom;
195 bbox.fLeft = SK_ScalarMax;
196 bbox.fRight = SK_ScalarMin;
198 for (size_t i = 0; i < numChars; ++i) {
199 if (xpos[i] < bbox.fLeft) {
200 bbox.fLeft = xpos[i];
202 if (xpos[i] > bbox.fRight) {
203 bbox.fRight = xpos[i];
207 // pad horizontally by max glyph height
211 bbox.fTop = top + constY;
212 bbox.fBottom = bottom + constY;
214 if (!this->transformBounds(bbox, &paint)) {
217 // This is the equivalent of calling:
218 // INHERITED::drawPosTextH(text, byteLength, xpos, constY, paint);
219 // but we filled our flat paint beforehand so that we could get font metrics.
220 drawPosTextHImpl(text, byteLength, xpos, constY, paint, flatPaintData);
223 void SkBBoxRecord::drawSprite(const SkBitmap& bitmap, int left, int top,
224 const SkPaint* paint) {
226 bbox.set(SkIRect::MakeXYWH(left, top, bitmap.width(), bitmap.height()));
227 this->handleBBox(bbox); // directly call handleBBox, matrix is ignored
228 INHERITED::drawSprite(bitmap, left, top, paint);
231 void SkBBoxRecord::drawTextOnPath(const void* text, size_t byteLength,
232 const SkPath& path, const SkMatrix* matrix,
233 const SkPaint& paint) {
234 SkRect bbox = path.getBounds();
235 SkPaint::FontMetrics metrics;
236 paint.getFontMetrics(&metrics);
238 // pad out all sides by the max glyph height above baseline
239 SkScalar pad = metrics.fTop;
245 if (this->transformBounds(bbox, &paint)) {
246 INHERITED::drawTextOnPath(text, byteLength, path, matrix, paint);
250 void SkBBoxRecord::drawVertices(VertexMode mode, int vertexCount,
251 const SkPoint vertices[], const SkPoint texs[],
252 const SkColor colors[], SkXfermode* xfer,
253 const uint16_t indices[], int indexCount,
254 const SkPaint& paint) {
256 bbox.set(vertices, vertexCount);
257 if (this->transformBounds(bbox, &paint)) {
258 INHERITED::drawVertices(mode, vertexCount, vertices, texs,
259 colors, xfer, indices, indexCount, paint);
263 void SkBBoxRecord::drawPicture(SkPicture& picture) {
264 if (picture.width() > 0 && picture.height() > 0 &&
265 this->transformBounds(SkRect::MakeWH(picture.width(), picture.height()), NULL)) {
266 INHERITED::drawPicture(picture);
270 bool SkBBoxRecord::transformBounds(const SkRect& bounds, const SkPaint* paint) {
271 SkRect outBounds = bounds;
275 // account for stroking, path effects, shadows, etc
276 if (paint->canComputeFastBounds()) {
278 outBounds = paint->computeFastBounds(outBounds, &temp);
280 // set bounds to current clip
281 if (!this->getClipBounds(&outBounds)) {
282 // current clip is empty
288 if (!outBounds.isEmpty() && !this->quickReject(outBounds)) {
289 this->getTotalMatrix().mapRect(&outBounds);
290 this->handleBBox(outBounds);