SkBBoxRecord.cpp revision e0d9ce890e67d02727ac2811bb456ddb64f827d4
1 2/* 3 * Copyright 2012 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9#include "SkBBoxRecord.h" 10 11void SkBBoxRecord::drawOval(const SkRect& rect, const SkPaint& paint) { 12 if (this->transformBounds(rect, &paint)) { 13 INHERITED::drawOval(rect, paint); 14 } 15} 16 17void SkBBoxRecord::drawRRect(const SkRRect& rrect, const SkPaint& paint) { 18 if (this->transformBounds(rrect.rect(), &paint)) { 19 INHERITED::drawRRect(rrect, paint); 20 } 21} 22 23void SkBBoxRecord::drawRect(const SkRect& rect, const SkPaint& paint) { 24 if (this->transformBounds(rect, &paint)) { 25 INHERITED::drawRect(rect, paint); 26 } 27} 28 29void 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); 33 } 34} 35 36void 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. 40 SkIRect clipBounds; 41 if (this->getClipDeviceBounds(&clipBounds)) { 42 this->handleBBox(SkRect::Make(clipBounds)); 43 INHERITED::drawPath(path, paint); 44 } 45 } else if (this->transformBounds(path.getBounds(), &paint)) { 46 INHERITED::drawPath(path, paint); 47 } 48} 49 50void SkBBoxRecord::drawPoints(PointMode mode, size_t count, const SkPoint pts[], 51 const SkPaint& paint) { 52 SkRect bbox; 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); 68 } 69} 70 71void SkBBoxRecord::drawPaint(const SkPaint& paint) { 72 SkRect bbox; 73 if (this->getClipBounds(&bbox)) { 74 if (this->transformBounds(bbox, &paint)) { 75 INHERITED::drawPaint(paint); 76 } 77 } 78} 79 80void 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); 85} 86 87void SkBBoxRecord::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, 88 const SkPaint& paint) { 89 SkRect bbox; 90 paint.measureText(text, byteLength, &bbox); 91 SkPaint::FontMetrics metrics; 92 paint.getFontMetrics(&metrics); 93 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) { 98 bbox.fTop -= h / 2; 99 bbox.fBottom -= h / 2; 100 } 101 // Pad top and bottom with max extents from FontMetrics 102 bbox.fBottom += metrics.fBottom; 103 bbox.fTop += metrics.fTop; 104 } else { 105 SkScalar w = bbox.fRight - bbox.fLeft; 106 if (paint.getTextAlign() == SkPaint::kCenter_Align) { 107 bbox.fLeft -= w / 2; 108 bbox.fRight -= w / 2; 109 } else if (paint.getTextAlign() == SkPaint::kRight_Align) { 110 bbox.fLeft -= w; 111 bbox.fRight -= w; 112 } 113 // Set vertical bounds to max extents from font metrics 114 bbox.fTop = metrics.fTop; 115 bbox.fBottom = metrics.fBottom; 116 } 117 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; 123 bbox.fLeft -= pad; 124 bbox.fRight += pad; 125 126 bbox.fLeft += x; 127 bbox.fRight += x; 128 bbox.fTop += y; 129 bbox.fBottom += y; 130 if (this->transformBounds(bbox, &paint)) { 131 INHERITED::onDrawText(text, byteLength, x, y, paint); 132 } 133} 134 135void 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); 140 } 141} 142 143void 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); 148 } 149} 150 151void SkBBoxRecord::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& mat, 152 const SkPaint* paint) { 153 SkMatrix m = mat; 154 SkRect bbox = {0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())}; 155 m.mapRect(&bbox); 156 if (this->transformBounds(bbox, paint)) { 157 INHERITED::drawBitmapMatrix(bitmap, mat, paint); 158 } 159} 160 161void 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); 165 } 166} 167 168void SkBBoxRecord::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], 169 const SkPaint& paint) { 170 SkRect bbox; 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; 176 177 // pad on left and right by half of max vertical glyph extents 178 SkScalar pad = (metrics.fTop - metrics.fBottom) / 2; 179 bbox.fLeft += pad; 180 bbox.fRight -= pad; 181 182 if (this->transformBounds(bbox, &paint)) { 183 INHERITED::onDrawPosText(text, byteLength, pos, paint); 184 } 185} 186 187void 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); 190 if (numChars == 0) { 191 return; 192 } 193 194 const SkFlatData* flatPaintData = this->getFlatPaintData(paint); 195 WriteTopBot(paint, *flatPaintData); 196 197 SkScalar top = flatPaintData->topBot()[0]; 198 SkScalar bottom = flatPaintData->topBot()[1]; 199 SkScalar pad = top - bottom; 200 201 SkRect bbox; 202 bbox.fLeft = SK_ScalarMax; 203 bbox.fRight = SK_ScalarMin; 204 205 for (size_t i = 0; i < numChars; ++i) { 206 if (xpos[i] < bbox.fLeft) { 207 bbox.fLeft = xpos[i]; 208 } 209 if (xpos[i] > bbox.fRight) { 210 bbox.fRight = xpos[i]; 211 } 212 } 213 214 // pad horizontally by max glyph height 215 bbox.fLeft += pad; 216 bbox.fRight -= pad; 217 218 bbox.fTop = top + constY; 219 bbox.fBottom = bottom + constY; 220 221 if (!this->transformBounds(bbox, &paint)) { 222 return; 223 } 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); 228} 229 230void SkBBoxRecord::drawSprite(const SkBitmap& bitmap, int left, int top, 231 const SkPaint* paint) { 232 SkRect bbox; 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); 236} 237 238void 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); 243 244 // pad out all sides by the max glyph height above baseline 245 SkScalar pad = metrics.fTop; 246 bbox.fLeft += pad; 247 bbox.fRight -= pad; 248 bbox.fTop += pad; 249 bbox.fBottom -= pad; 250 251 if (this->transformBounds(bbox, &paint)) { 252 INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint); 253 } 254} 255 256void 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) { 261 SkRect bbox; 262 bbox.set(vertices, vertexCount); 263 if (this->transformBounds(bbox, &paint)) { 264 INHERITED::drawVertices(mode, vertexCount, vertices, texs, 265 colors, xfer, indices, indexCount, paint); 266 } 267} 268 269void 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); 273 } 274} 275 276bool SkBBoxRecord::transformBounds(const SkRect& bounds, const SkPaint* paint) { 277 SkRect outBounds = bounds; 278 outBounds.sort(); 279 280 if (paint) { 281 // account for stroking, path effects, shadows, etc 282 if (paint->canComputeFastBounds()) { 283 SkRect temp; 284 outBounds = paint->computeFastBounds(outBounds, &temp); 285 } else { 286 // set bounds to current clip 287 if (!this->getClipBounds(&outBounds)) { 288 // current clip is empty 289 return false; 290 } 291 } 292 } 293 294 if (!outBounds.isEmpty() && !this->quickReject(outBounds)) { 295 this->getTotalMatrix().mapRect(&outBounds); 296 this->handleBBox(outBounds); 297 return true; 298 } 299 300 return false; 301} 302