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 11 void SkBBoxRecord::drawOval(const SkRect& rect, const SkPaint& paint) { 12 if (this->transformBounds(rect, &paint)) { 13 INHERITED::drawOval(rect, paint); 14 } 15 } 16 17 void SkBBoxRecord::drawRRect(const SkRRect& rrect, const SkPaint& paint) { 18 if (this->transformBounds(rrect.rect(), &paint)) { 19 INHERITED::drawRRect(rrect, paint); 20 } 21 } 22 23 void SkBBoxRecord::drawRect(const SkRect& rect, const SkPaint& paint) { 24 if (this->transformBounds(rect, &paint)) { 25 INHERITED::drawRect(rect, paint); 26 } 27 } 28 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. 33 SkIRect clipBounds; 34 if (this->getClipDeviceBounds(&clipBounds)) { 35 this->handleBBox(SkRect::Make(clipBounds)); 36 INHERITED::drawPath(path, paint); 37 } 38 } else if (this->transformBounds(path.getBounds(), &paint)) { 39 INHERITED::drawPath(path, paint); 40 } 41 } 42 43 void SkBBoxRecord::drawPoints(PointMode mode, size_t count, const SkPoint pts[], 44 const SkPaint& paint) { 45 SkRect bbox; 46 bbox.set(pts, count); 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); 61 } 62 } 63 64 void SkBBoxRecord::drawPaint(const SkPaint& paint) { 65 SkRect bbox; 66 if (this->getClipBounds(&bbox)) { 67 if (this->transformBounds(bbox, &paint)) { 68 INHERITED::drawPaint(paint); 69 } 70 } 71 } 72 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); 78 } 79 80 void SkBBoxRecord::drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, 81 const SkPaint& paint) { 82 SkRect bbox; 83 paint.measureText(text, byteLength, &bbox); 84 SkPaint::FontMetrics metrics; 85 paint.getFontMetrics(&metrics); 86 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) { 91 bbox.fTop -= h / 2; 92 bbox.fBottom -= h / 2; 93 } 94 // Pad top and bottom with max extents from FontMetrics 95 bbox.fBottom += metrics.fBottom; 96 bbox.fTop += metrics.fTop; 97 } else { 98 SkScalar w = bbox.fRight - bbox.fLeft; 99 if (paint.getTextAlign() == SkPaint::kCenter_Align) { 100 bbox.fLeft -= w / 2; 101 bbox.fRight -= w / 2; 102 } else if (paint.getTextAlign() == SkPaint::kRight_Align) { 103 bbox.fLeft -= w; 104 bbox.fRight -= w; 105 } 106 // Set vertical bounds to max extents from font metrics 107 bbox.fTop = metrics.fTop; 108 bbox.fBottom = metrics.fBottom; 109 } 110 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; 116 bbox.fLeft -= pad; 117 bbox.fRight += pad; 118 119 bbox.fLeft += x; 120 bbox.fRight += x; 121 bbox.fTop += y; 122 bbox.fBottom += y; 123 if (this->transformBounds(bbox, &paint)) { 124 INHERITED::drawText(text, byteLength, x, y, paint); 125 } 126 } 127 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); 133 } 134 } 135 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); 141 } 142 } 143 144 void SkBBoxRecord::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& mat, 145 const SkPaint* paint) { 146 SkMatrix m = mat; 147 SkRect bbox = {0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())}; 148 m.mapRect(&bbox); 149 if (this->transformBounds(bbox, paint)) { 150 INHERITED::drawBitmapMatrix(bitmap, mat, paint); 151 } 152 } 153 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); 158 } 159 } 160 161 void SkBBoxRecord::drawPosText(const void* text, size_t byteLength, 162 const SkPoint pos[], const SkPaint& paint) { 163 SkRect bbox; 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; 169 170 // pad on left and right by half of max vertical glyph extents 171 SkScalar pad = (metrics.fTop - metrics.fBottom) / 2; 172 bbox.fLeft += pad; 173 bbox.fRight -= pad; 174 175 if (this->transformBounds(bbox, &paint)) { 176 INHERITED::drawPosText(text, byteLength, pos, paint); 177 } 178 } 179 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); 183 if (numChars == 0) { 184 return; 185 } 186 187 const SkFlatData* flatPaintData = this->getFlatPaintData(paint); 188 WriteTopBot(paint, *flatPaintData); 189 190 SkScalar top = flatPaintData->topBot()[0]; 191 SkScalar bottom = flatPaintData->topBot()[1]; 192 SkScalar pad = top - bottom; 193 194 SkRect bbox; 195 bbox.fLeft = SK_ScalarMax; 196 bbox.fRight = SK_ScalarMin; 197 198 for (size_t i = 0; i < numChars; ++i) { 199 if (xpos[i] < bbox.fLeft) { 200 bbox.fLeft = xpos[i]; 201 } 202 if (xpos[i] > bbox.fRight) { 203 bbox.fRight = xpos[i]; 204 } 205 } 206 207 // pad horizontally by max glyph height 208 bbox.fLeft += pad; 209 bbox.fRight -= pad; 210 211 bbox.fTop = top + constY; 212 bbox.fBottom = bottom + constY; 213 214 if (!this->transformBounds(bbox, &paint)) { 215 return; 216 } 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); 221 } 222 223 void SkBBoxRecord::drawSprite(const SkBitmap& bitmap, int left, int top, 224 const SkPaint* paint) { 225 SkRect bbox; 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); 229 } 230 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); 237 238 // pad out all sides by the max glyph height above baseline 239 SkScalar pad = metrics.fTop; 240 bbox.fLeft += pad; 241 bbox.fRight -= pad; 242 bbox.fTop += pad; 243 bbox.fBottom -= pad; 244 245 if (this->transformBounds(bbox, &paint)) { 246 INHERITED::drawTextOnPath(text, byteLength, path, matrix, paint); 247 } 248 } 249 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) { 255 SkRect bbox; 256 bbox.set(vertices, vertexCount); 257 if (this->transformBounds(bbox, &paint)) { 258 INHERITED::drawVertices(mode, vertexCount, vertices, texs, 259 colors, xfer, indices, indexCount, paint); 260 } 261 } 262 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); 267 } 268 } 269 270 bool SkBBoxRecord::transformBounds(const SkRect& bounds, const SkPaint* paint) { 271 SkRect outBounds = bounds; 272 outBounds.sort(); 273 274 if (paint) { 275 // account for stroking, path effects, shadows, etc 276 if (paint->canComputeFastBounds()) { 277 SkRect temp; 278 outBounds = paint->computeFastBounds(outBounds, &temp); 279 } else { 280 // set bounds to current clip 281 if (!this->getClipBounds(&outBounds)) { 282 // current clip is empty 283 return false; 284 } 285 } 286 } 287 288 if (!outBounds.isEmpty() && !this->quickReject(outBounds)) { 289 this->getTotalMatrix().mapRect(&outBounds); 290 this->handleBBox(outBounds); 291 return true; 292 } 293 294 return false; 295 } 296