gfx/skia/trunk/src/core/SkBBoxRecord.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     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  */
     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);
    14     }
    15 }
    17 void SkBBoxRecord::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
    18     if (this->transformBounds(rrect.rect(), &paint)) {
    19         INHERITED::drawRRect(rrect, paint);
    20     }
    21 }
    23 void SkBBoxRecord::drawRect(const SkRect& rect, const SkPaint& paint) {
    24     if (this->transformBounds(rect, &paint)) {
    25         INHERITED::drawRect(rect, paint);
    26     }
    27 }
    29 void 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 }
    36 void 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 }
    50 void 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 }
    71 void 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 }
    80 void 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 }
    87 void SkBBoxRecord::drawText(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);
    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     }
   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;
   126     bbox.fLeft += x;
   127     bbox.fRight += x;
   128     bbox.fTop += y;
   129     bbox.fBottom += y;
   130     if (this->transformBounds(bbox, &paint)) {
   131         INHERITED::drawText(text, byteLength, x, y, paint);
   132     }
   133 }
   135 void 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 }
   143 void 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 }
   151 void 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 }
   161 void 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 }
   168 void SkBBoxRecord::drawPosText(const void* text, size_t byteLength,
   169                                const SkPoint pos[], 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;
   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;
   182     if (this->transformBounds(bbox, &paint)) {
   183         INHERITED::drawPosText(text, byteLength, pos, paint);
   184     }
   185 }
   187 void SkBBoxRecord::drawPosTextH(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     }
   194     const SkFlatData* flatPaintData = this->getFlatPaintData(paint);
   195     WriteTopBot(paint, *flatPaintData);
   197     SkScalar top = flatPaintData->topBot()[0];
   198     SkScalar bottom = flatPaintData->topBot()[1];
   199     SkScalar pad = top - bottom;
   201     SkRect bbox;
   202     bbox.fLeft = SK_ScalarMax;
   203     bbox.fRight = SK_ScalarMin;
   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     }
   214     // pad horizontally by max glyph height
   215     bbox.fLeft  += pad;
   216     bbox.fRight -= pad;
   218     bbox.fTop    = top + constY;
   219     bbox.fBottom = bottom + constY;
   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 }
   230 void 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 }
   238 void SkBBoxRecord::drawTextOnPath(const void* text, size_t byteLength,
   239                                   const SkPath& path, const SkMatrix* matrix,
   240                                   const SkPaint& paint) {
   241     SkRect bbox = path.getBounds();
   242     SkPaint::FontMetrics metrics;
   243     paint.getFontMetrics(&metrics);
   245     // pad out all sides by the max glyph height above baseline
   246     SkScalar pad = metrics.fTop;
   247     bbox.fLeft += pad;
   248     bbox.fRight -= pad;
   249     bbox.fTop += pad;
   250     bbox.fBottom -= pad;
   252     if (this->transformBounds(bbox, &paint)) {
   253         INHERITED::drawTextOnPath(text, byteLength, path, matrix, paint);
   254     }
   255 }
   257 void SkBBoxRecord::drawVertices(VertexMode mode, int vertexCount,
   258                                 const SkPoint vertices[], const SkPoint texs[],
   259                                 const SkColor colors[], SkXfermode* xfer,
   260                                 const uint16_t indices[], int indexCount,
   261                                 const SkPaint& paint) {
   262     SkRect bbox;
   263     bbox.set(vertices, vertexCount);
   264     if (this->transformBounds(bbox, &paint)) {
   265         INHERITED::drawVertices(mode, vertexCount, vertices, texs,
   266                                 colors, xfer, indices, indexCount, paint);
   267     }
   268 }
   270 void SkBBoxRecord::drawPicture(SkPicture& picture) {
   271     if (picture.width() > 0 && picture.height() > 0 &&
   272         this->transformBounds(SkRect::MakeWH(picture.width(), picture.height()), NULL)) {
   273         INHERITED::drawPicture(picture);
   274     }
   275 }
   277 bool SkBBoxRecord::transformBounds(const SkRect& bounds, const SkPaint* paint) {
   278     SkRect outBounds = bounds;
   279     outBounds.sort();
   281     if (paint) {
   282         // account for stroking, path effects, shadows, etc
   283         if (paint->canComputeFastBounds()) {
   284             SkRect temp;
   285             outBounds = paint->computeFastBounds(outBounds, &temp);
   286         } else {
   287             // set bounds to current clip
   288             if (!this->getClipBounds(&outBounds)) {
   289                 // current clip is empty
   290                 return false;
   291             }
   292         }
   293     }
   295     if (!outBounds.isEmpty() && !this->quickReject(outBounds)) {
   296         this->getTotalMatrix().mapRect(&outBounds);
   297         this->handleBBox(outBounds);
   298         return true;
   299     }
   301     return false;
   302 }

mercurial