michael@0: michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkBBoxRecord.h" michael@0: michael@0: void SkBBoxRecord::drawOval(const SkRect& rect, const SkPaint& paint) { michael@0: if (this->transformBounds(rect, &paint)) { michael@0: INHERITED::drawOval(rect, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawRRect(const SkRRect& rrect, const SkPaint& paint) { michael@0: if (this->transformBounds(rrect.rect(), &paint)) { michael@0: INHERITED::drawRRect(rrect, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawRect(const SkRect& rect, const SkPaint& paint) { michael@0: if (this->transformBounds(rect, &paint)) { michael@0: INHERITED::drawRect(rect, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, michael@0: const SkPaint& paint) { michael@0: if (this->transformBounds(outer.rect(), &paint)) { michael@0: this->INHERITED::onDrawDRRect(outer, inner, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawPath(const SkPath& path, const SkPaint& paint) { michael@0: if (path.isInverseFillType()) { michael@0: // If path is inverse filled, use the current clip bounds as the michael@0: // path's device-space bounding box. michael@0: SkIRect clipBounds; michael@0: if (this->getClipDeviceBounds(&clipBounds)) { michael@0: this->handleBBox(SkRect::Make(clipBounds)); michael@0: INHERITED::drawPath(path, paint); michael@0: } michael@0: } else if (this->transformBounds(path.getBounds(), &paint)) { michael@0: INHERITED::drawPath(path, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawPoints(PointMode mode, size_t count, const SkPoint pts[], michael@0: const SkPaint& paint) { michael@0: SkRect bbox; michael@0: bbox.set(pts, SkToInt(count)); michael@0: // Small min width value, just to ensure hairline point bounding boxes aren't empty. michael@0: // Even though we know hairline primitives are drawn one pixel wide, we do not use a michael@0: // minimum of 1 because the playback scale factor is unknown at record time. Later michael@0: // outsets will take care of adding additional padding for antialiasing and rounding out michael@0: // to integer device coordinates, guaranteeing that the rasterized pixels will be included michael@0: // in the computed bounds. michael@0: // Note: The device coordinate outset in SkBBoxHierarchyRecord::handleBBox is currently michael@0: // done in the recording coordinate space, which is wrong. michael@0: // http://code.google.com/p/skia/issues/detail?id=1021 michael@0: static const SkScalar kMinWidth = 0.01f; michael@0: SkScalar halfStrokeWidth = SkMaxScalar(paint.getStrokeWidth(), kMinWidth) / 2; michael@0: bbox.outset(halfStrokeWidth, halfStrokeWidth); michael@0: if (this->transformBounds(bbox, &paint)) { michael@0: INHERITED::drawPoints(mode, count, pts, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawPaint(const SkPaint& paint) { michael@0: SkRect bbox; michael@0: if (this->getClipBounds(&bbox)) { michael@0: if (this->transformBounds(bbox, &paint)) { michael@0: INHERITED::drawPaint(paint); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::clear(SkColor color) { michael@0: SkISize size = this->getDeviceSize(); michael@0: SkRect bbox = {0, 0, SkIntToScalar(size.width()), SkIntToScalar(size.height())}; michael@0: this->handleBBox(bbox); michael@0: INHERITED::clear(color); michael@0: } michael@0: michael@0: void SkBBoxRecord::drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, michael@0: const SkPaint& paint) { michael@0: SkRect bbox; michael@0: paint.measureText(text, byteLength, &bbox); michael@0: SkPaint::FontMetrics metrics; michael@0: paint.getFontMetrics(&metrics); michael@0: michael@0: // Vertical and aligned text need to be offset michael@0: if (paint.isVerticalText()) { michael@0: SkScalar h = bbox.fBottom - bbox.fTop; michael@0: if (paint.getTextAlign() == SkPaint::kCenter_Align) { michael@0: bbox.fTop -= h / 2; michael@0: bbox.fBottom -= h / 2; michael@0: } michael@0: // Pad top and bottom with max extents from FontMetrics michael@0: bbox.fBottom += metrics.fBottom; michael@0: bbox.fTop += metrics.fTop; michael@0: } else { michael@0: SkScalar w = bbox.fRight - bbox.fLeft; michael@0: if (paint.getTextAlign() == SkPaint::kCenter_Align) { michael@0: bbox.fLeft -= w / 2; michael@0: bbox.fRight -= w / 2; michael@0: } else if (paint.getTextAlign() == SkPaint::kRight_Align) { michael@0: bbox.fLeft -= w; michael@0: bbox.fRight -= w; michael@0: } michael@0: // Set vertical bounds to max extents from font metrics michael@0: bbox.fTop = metrics.fTop; michael@0: bbox.fBottom = metrics.fBottom; michael@0: } michael@0: michael@0: // Pad horizontal bounds on each side by half of max vertical extents (this is sort of michael@0: // arbitrary, but seems to produce reasonable results, if there were a way of getting max michael@0: // glyph X-extents to pad by, that may be better here, but FontMetrics fXMin and fXMax seem michael@0: // incorrect on most platforms (too small in Linux, never even set in Windows). michael@0: SkScalar pad = (metrics.fBottom - metrics.fTop) / 2; michael@0: bbox.fLeft -= pad; michael@0: bbox.fRight += pad; michael@0: michael@0: bbox.fLeft += x; michael@0: bbox.fRight += x; michael@0: bbox.fTop += y; michael@0: bbox.fBottom += y; michael@0: if (this->transformBounds(bbox, &paint)) { michael@0: INHERITED::drawText(text, byteLength, x, y, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, michael@0: const SkPaint* paint) { michael@0: SkRect bbox = {left, top, left + bitmap.width(), top + bitmap.height()}; michael@0: if (this->transformBounds(bbox, paint)) { michael@0: INHERITED::drawBitmap(bitmap, left, top, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, michael@0: const SkRect& dst, const SkPaint* paint, michael@0: DrawBitmapRectFlags flags) { michael@0: if (this->transformBounds(dst, paint)) { michael@0: INHERITED::drawBitmapRectToRect(bitmap, src, dst, paint, flags); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& mat, michael@0: const SkPaint* paint) { michael@0: SkMatrix m = mat; michael@0: SkRect bbox = {0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())}; michael@0: m.mapRect(&bbox); michael@0: if (this->transformBounds(bbox, paint)) { michael@0: INHERITED::drawBitmapMatrix(bitmap, mat, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, michael@0: const SkRect& dst, const SkPaint* paint) { michael@0: if (this->transformBounds(dst, paint)) { michael@0: INHERITED::drawBitmapNine(bitmap, center, dst, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawPosText(const void* text, size_t byteLength, michael@0: const SkPoint pos[], const SkPaint& paint) { michael@0: SkRect bbox; michael@0: bbox.set(pos, paint.countText(text, byteLength)); michael@0: SkPaint::FontMetrics metrics; michael@0: paint.getFontMetrics(&metrics); michael@0: bbox.fTop += metrics.fTop; michael@0: bbox.fBottom += metrics.fBottom; michael@0: michael@0: // pad on left and right by half of max vertical glyph extents michael@0: SkScalar pad = (metrics.fTop - metrics.fBottom) / 2; michael@0: bbox.fLeft += pad; michael@0: bbox.fRight -= pad; michael@0: michael@0: if (this->transformBounds(bbox, &paint)) { michael@0: INHERITED::drawPosText(text, byteLength, pos, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], michael@0: SkScalar constY, const SkPaint& paint) { michael@0: size_t numChars = paint.countText(text, byteLength); michael@0: if (numChars == 0) { michael@0: return; michael@0: } michael@0: michael@0: const SkFlatData* flatPaintData = this->getFlatPaintData(paint); michael@0: WriteTopBot(paint, *flatPaintData); michael@0: michael@0: SkScalar top = flatPaintData->topBot()[0]; michael@0: SkScalar bottom = flatPaintData->topBot()[1]; michael@0: SkScalar pad = top - bottom; michael@0: michael@0: SkRect bbox; michael@0: bbox.fLeft = SK_ScalarMax; michael@0: bbox.fRight = SK_ScalarMin; michael@0: michael@0: for (size_t i = 0; i < numChars; ++i) { michael@0: if (xpos[i] < bbox.fLeft) { michael@0: bbox.fLeft = xpos[i]; michael@0: } michael@0: if (xpos[i] > bbox.fRight) { michael@0: bbox.fRight = xpos[i]; michael@0: } michael@0: } michael@0: michael@0: // pad horizontally by max glyph height michael@0: bbox.fLeft += pad; michael@0: bbox.fRight -= pad; michael@0: michael@0: bbox.fTop = top + constY; michael@0: bbox.fBottom = bottom + constY; michael@0: michael@0: if (!this->transformBounds(bbox, &paint)) { michael@0: return; michael@0: } michael@0: // This is the equivalent of calling: michael@0: // INHERITED::drawPosTextH(text, byteLength, xpos, constY, paint); michael@0: // but we filled our flat paint beforehand so that we could get font metrics. michael@0: drawPosTextHImpl(text, byteLength, xpos, constY, paint, flatPaintData); michael@0: } michael@0: michael@0: void SkBBoxRecord::drawSprite(const SkBitmap& bitmap, int left, int top, michael@0: const SkPaint* paint) { michael@0: SkRect bbox; michael@0: bbox.set(SkIRect::MakeXYWH(left, top, bitmap.width(), bitmap.height())); michael@0: this->handleBBox(bbox); // directly call handleBBox, matrix is ignored michael@0: INHERITED::drawSprite(bitmap, left, top, paint); michael@0: } michael@0: michael@0: void SkBBoxRecord::drawTextOnPath(const void* text, size_t byteLength, michael@0: const SkPath& path, const SkMatrix* matrix, michael@0: const SkPaint& paint) { michael@0: SkRect bbox = path.getBounds(); michael@0: SkPaint::FontMetrics metrics; michael@0: paint.getFontMetrics(&metrics); michael@0: michael@0: // pad out all sides by the max glyph height above baseline michael@0: SkScalar pad = metrics.fTop; michael@0: bbox.fLeft += pad; michael@0: bbox.fRight -= pad; michael@0: bbox.fTop += pad; michael@0: bbox.fBottom -= pad; michael@0: michael@0: if (this->transformBounds(bbox, &paint)) { michael@0: INHERITED::drawTextOnPath(text, byteLength, path, matrix, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawVertices(VertexMode mode, int vertexCount, michael@0: const SkPoint vertices[], const SkPoint texs[], michael@0: const SkColor colors[], SkXfermode* xfer, michael@0: const uint16_t indices[], int indexCount, michael@0: const SkPaint& paint) { michael@0: SkRect bbox; michael@0: bbox.set(vertices, vertexCount); michael@0: if (this->transformBounds(bbox, &paint)) { michael@0: INHERITED::drawVertices(mode, vertexCount, vertices, texs, michael@0: colors, xfer, indices, indexCount, paint); michael@0: } michael@0: } michael@0: michael@0: void SkBBoxRecord::drawPicture(SkPicture& picture) { michael@0: if (picture.width() > 0 && picture.height() > 0 && michael@0: this->transformBounds(SkRect::MakeWH(picture.width(), picture.height()), NULL)) { michael@0: INHERITED::drawPicture(picture); michael@0: } michael@0: } michael@0: michael@0: bool SkBBoxRecord::transformBounds(const SkRect& bounds, const SkPaint* paint) { michael@0: SkRect outBounds = bounds; michael@0: outBounds.sort(); michael@0: michael@0: if (paint) { michael@0: // account for stroking, path effects, shadows, etc michael@0: if (paint->canComputeFastBounds()) { michael@0: SkRect temp; michael@0: outBounds = paint->computeFastBounds(outBounds, &temp); michael@0: } else { michael@0: // set bounds to current clip michael@0: if (!this->getClipBounds(&outBounds)) { michael@0: // current clip is empty michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (!outBounds.isEmpty() && !this->quickReject(outBounds)) { michael@0: this->getTotalMatrix().mapRect(&outBounds); michael@0: this->handleBBox(outBounds); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: }