Home | History | Annotate | Download | only in core
      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::MakeFromIRect(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 = SkFloatToScalar(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     if (this->transformBounds(dst, paint)) {
    139         INHERITED::drawBitmapRectToRect(bitmap, src, dst, paint);
    140     }
    141 }
    142 
    143 void SkBBoxRecord::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& mat,
    144                                     const SkPaint* paint) {
    145     SkMatrix m = mat;
    146     SkRect bbox = {0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())};
    147     m.mapRect(&bbox);
    148     if (this->transformBounds(bbox, paint)) {
    149         INHERITED::drawBitmapMatrix(bitmap, mat, paint);
    150     }
    151 }
    152 
    153 void SkBBoxRecord::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
    154                                   const SkRect& dst, const SkPaint* paint) {
    155     if (this->transformBounds(dst, paint)) {
    156         INHERITED::drawBitmapNine(bitmap, center, dst, paint);
    157     }
    158 }
    159 
    160 void SkBBoxRecord::drawPosText(const void* text, size_t byteLength,
    161                                const SkPoint pos[], const SkPaint& paint) {
    162     SkRect bbox;
    163     bbox.set(pos, paint.countText(text, byteLength));
    164     SkPaint::FontMetrics metrics;
    165     paint.getFontMetrics(&metrics);
    166     bbox.fTop += metrics.fTop;
    167     bbox.fBottom += metrics.fBottom;
    168 
    169     // pad on left and right by half of max vertical glyph extents
    170     SkScalar pad = (metrics.fTop - metrics.fBottom) / 2;
    171     bbox.fLeft += pad;
    172     bbox.fRight -= pad;
    173 
    174     if (this->transformBounds(bbox, &paint)) {
    175         INHERITED::drawPosText(text, byteLength, pos, paint);
    176     }
    177 }
    178 
    179 void SkBBoxRecord::drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
    180                                 SkScalar constY, const SkPaint& paint) {
    181     SkRect bbox;
    182     size_t numChars = paint.countText(text, byteLength);
    183     if (numChars > 0) {
    184         bbox.fLeft  = xpos[0];
    185         bbox.fRight = xpos[numChars - 1];
    186         // if we had a guarantee that these will be monotonically increasing, this could be sped up
    187         for (size_t i = 1; i < numChars; ++i) {
    188             if (xpos[i] < bbox.fLeft) {
    189                 bbox.fLeft = xpos[i];
    190             }
    191             if (xpos[i] > bbox.fRight) {
    192                 bbox.fRight = xpos[i];
    193             }
    194         }
    195         SkPaint::FontMetrics metrics;
    196         paint.getFontMetrics(&metrics);
    197 
    198         // pad horizontally by max glyph height
    199         SkScalar pad = (metrics.fTop - metrics.fBottom);
    200         bbox.fLeft  += pad;
    201         bbox.fRight -= pad;
    202 
    203         bbox.fTop    = metrics.fTop + constY;
    204         bbox.fBottom = metrics.fBottom + constY;
    205         if (!this->transformBounds(bbox, &paint)) {
    206             return;
    207         }
    208     }
    209     INHERITED::drawPosTextH(text, byteLength, xpos, constY, paint);
    210 }
    211 
    212 void SkBBoxRecord::drawSprite(const SkBitmap& bitmap, int left, int top,
    213                               const SkPaint* paint) {
    214     SkRect bbox;
    215     bbox.set(SkIRect::MakeXYWH(left, top, bitmap.width(), bitmap.height()));
    216     this->handleBBox(bbox); // directly call handleBBox, matrix is ignored
    217     INHERITED::drawSprite(bitmap, left, top, paint);
    218 }
    219 
    220 void SkBBoxRecord::drawTextOnPath(const void* text, size_t byteLength,
    221                                   const SkPath& path, const SkMatrix* matrix,
    222                                   const SkPaint& paint) {
    223     SkRect bbox = path.getBounds();
    224     SkPaint::FontMetrics metrics;
    225     paint.getFontMetrics(&metrics);
    226 
    227     // pad out all sides by the max glyph height above baseline
    228     SkScalar pad = metrics.fTop;
    229     bbox.fLeft += pad;
    230     bbox.fRight -= pad;
    231     bbox.fTop += pad;
    232     bbox.fBottom -= pad;
    233 
    234     if (this->transformBounds(bbox, &paint)) {
    235         INHERITED::drawTextOnPath(text, byteLength, path, matrix, paint);
    236     }
    237 }
    238 
    239 void SkBBoxRecord::drawVertices(VertexMode mode, int vertexCount,
    240                                 const SkPoint vertices[], const SkPoint texs[],
    241                                 const SkColor colors[], SkXfermode* xfer,
    242                                 const uint16_t indices[], int indexCount,
    243                                 const SkPaint& paint) {
    244     SkRect bbox;
    245     bbox.set(vertices, vertexCount);
    246     if (this->transformBounds(bbox, &paint)) {
    247         INHERITED::drawVertices(mode, vertexCount, vertices, texs,
    248                                 colors, xfer, indices, indexCount, paint);
    249     }
    250 }
    251 
    252 void SkBBoxRecord::drawPicture(SkPicture& picture) {
    253     if (picture.width() > 0 && picture.height() > 0 &&
    254         this->transformBounds(SkRect::MakeWH(picture.width(), picture.height()), NULL)) {
    255         INHERITED::drawPicture(picture);
    256     }
    257 }
    258 
    259 bool SkBBoxRecord::transformBounds(const SkRect& bounds, const SkPaint* paint) {
    260     SkRect outBounds = bounds;
    261     outBounds.sort();
    262 
    263     if (paint) {
    264         // account for stroking, path effects, shadows, etc
    265         if (paint->canComputeFastBounds()) {
    266             SkRect temp;
    267             outBounds = paint->computeFastBounds(outBounds, &temp);
    268         } else {
    269             // set bounds to current clip
    270             if (!this->getClipBounds(&outBounds)) {
    271                 // current clip is empty
    272                 return false;
    273             }
    274         }
    275     }
    276 
    277     if (!outBounds.isEmpty() && !this->quickReject(outBounds)) {
    278         this->getTotalMatrix().mapRect(&outBounds);
    279         this->handleBBox(outBounds);
    280         return true;
    281     }
    282 
    283     return false;
    284 }
    285