Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(439)

Side by Side Diff: src/core/SkRecordDraw.cpp

Issue 680363003: Use SkTypeface::getBounds() in bounding-box calculations. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fudge differently Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests/RecordDrawTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkRecordDraw.h" 8 #include "SkRecordDraw.h"
9 #include "SkPatchUtils.h" 9 #include "SkPatchUtils.h"
10 #include "SkTypeface.h"
10 11
11 void SkRecordDraw(const SkRecord& record, 12 void SkRecordDraw(const SkRecord& record,
12 SkCanvas* canvas, 13 SkCanvas* canvas,
13 const SkBBoxHierarchy* bbh, 14 const SkBBoxHierarchy* bbh,
14 SkDrawPictureCallback* callback) { 15 SkDrawPictureCallback* callback) {
15 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); 16 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
16 17
17 if (bbh) { 18 if (bbh) {
18 // Draw only ops that affect pixels in the canvas's current clip. 19 // Draw only ops that affect pixels in the canvas's current clip.
19 // The SkRecord and BBH were recorded in identity space. This canvas 20 // The SkRecord and BBH were recorded in identity space. This canvas
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 left = SkMinScalar(left, op.xpos[i]); 438 left = SkMinScalar(left, op.xpos[i]);
438 right = SkMaxScalar(right, op.xpos[i]); 439 right = SkMaxScalar(right, op.xpos[i]);
439 } 440 }
440 SkRect dst = { left, op.y, right, op.y }; 441 SkRect dst = { left, op.y, right, op.y };
441 AdjustTextForFontMetrics(&dst, op.paint); 442 AdjustTextForFontMetrics(&dst, op.paint);
442 return this->adjustAndMap(dst, &op.paint); 443 return this->adjustAndMap(dst, &op.paint);
443 } 444 }
444 Bounds bounds(const DrawTextOnPath& op) const { 445 Bounds bounds(const DrawTextOnPath& op) const {
445 SkRect dst = op.path.getBounds(); 446 SkRect dst = op.path.getBounds();
446 447
447 // Pad all sides by the maximum padding in any direction we'd normally a pply. 448 // We don't know how the text will curve aroudn the path, so
449 // pad all sides by the maximum padding in any direction we'd normally a pply.
448 SkRect pad = { 0, 0, 0, 0}; 450 SkRect pad = { 0, 0, 0, 0};
449 AdjustTextForFontMetrics(&pad, op.paint); 451 AdjustTextForFontMetrics(&pad, op.paint);
450 452 SkScalar max = SkTMax(SkTMax(-pad.fLeft, pad.fRight),
451 // That maximum padding happens to always be the right pad today. 453 SkTMax(-pad.fTop, pad.fBottom));
452 SkASSERT(pad.fLeft == -pad.fRight); 454 dst.outset(max, max);
453 SkASSERT(pad.fTop == -pad.fBottom);
454 SkASSERT(pad.fRight > pad.fBottom);
455 dst.outset(pad.fRight, pad.fRight);
456 455
457 return this->adjustAndMap(dst, &op.paint); 456 return this->adjustAndMap(dst, &op.paint);
458 } 457 }
459 458
460 Bounds bounds(const DrawTextBlob& op) const { 459 Bounds bounds(const DrawTextBlob& op) const {
461 SkRect dst = op.blob->bounds(); 460 SkRect dst = op.blob->bounds();
462 dst.offset(op.x, op.y); 461 dst.offset(op.x, op.y);
463 return this->adjustAndMap(dst, &op.paint); 462 return this->adjustAndMap(dst, &op.paint);
464 } 463 }
465 464
466 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) { 465 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
467 #ifdef SK_DEBUG 466 // rect was built from only the text's origin points, so we need
468 SkRect correct = *rect; 467 // to outset it by the worst-case bounds of the typeface.
469 #endif 468 SkAutoTUnref<SkTypeface> au;
470 // crbug.com/373785 ~~> xPad = 4x yPad 469 SkTypeface* tf = paint.getTypeface();
471 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x 470 if (!tf) {
472 const SkScalar yPad = 2.5f * paint.getTextSize(), 471 au.reset(SkTypeface::RefDefault());
473 xPad = 4.0f * yPad; 472 tf = au.get();
474 rect->outset(xPad, yPad); 473 }
474 const SkScalar size = paint.getTextSize();
475 const SkRect tb = tf->getBounds();
476 rect->fLeft += size * tb.fLeft;
477 rect->fRight += size * tb.fRight;
478 rect->fTop += size * tb.fTop;
479 rect->fBottom += size * tb.fBottom;
475 #ifdef SK_DEBUG 480 #ifdef SK_DEBUG
476 SkPaint::FontMetrics metrics; 481 SkPaint::FontMetrics metrics;
477 paint.getFontMetrics(&metrics); 482 paint.getFontMetrics(&metrics);
478 correct.fLeft += metrics.fXMin; 483 // Our numbers are so close to correct now that we're sometimes _slightl y_ under. Add
479 correct.fTop += metrics.fTop; 484 // a little fudge factor here to make sure the assert just catches reall y bad failures.
480 correct.fRight += metrics.fXMax; 485 const SkScalar kFudge = 0.0625f;
481 correct.fBottom += metrics.fBottom; 486 SkRect fudged = {
487 size * (tb.fLeft - kFudge),
488 size * (tb.fTop - kFudge),
489 size * (tb.fRight + kFudge),
490 size * (tb.fBottom + kFudge),
491 };
492 SkRect correct = { metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.f Bottom };
482 // See skia:2862 for why we ignore small text sizes. 493 // See skia:2862 for why we ignore small text sizes.
483 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct), 494 SkASSERTF(paint.getTextSize() < 0.001f || fudged.contains(correct),
484 "%f %f %f %f vs. %f %f %f %f\n", 495 "%f %f %f %f vs. %f %f %f %f, text size %f\n",
485 -xPad, -yPad, +xPad, +yPad, 496 size*tb.fLeft, size*tb.fTop, size*tb.fRight, size*tb.fBottom,
486 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom); 497 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom,
498 paint.getTextSize());
487 #endif 499 #endif
488 } 500 }
489 501
490 // Returns true if rect was meaningfully adjusted for the effects of paint, 502 // Returns true if rect was meaningfully adjusted for the effects of paint,
491 // false if the paint could affect the rect in unknown ways. 503 // false if the paint could affect the rect in unknown ways.
492 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) { 504 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
493 if (paint) { 505 if (paint) {
494 if (paint->canComputeFastBounds()) { 506 if (paint->canComputeFastBounds()) {
495 *rect = paint->computeFastBounds(*rect, rect); 507 *rect = paint->computeFastBounds(*rect, rect);
496 return true; 508 return true;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 // Used to track the bounds of Save/Restore blocks and the control ops insid e them. 560 // Used to track the bounds of Save/Restore blocks and the control ops insid e them.
549 SkTDArray<SaveBounds> fSaveStack; 561 SkTDArray<SaveBounds> fSaveStack;
550 SkTDArray<unsigned> fControlIndices; 562 SkTDArray<unsigned> fControlIndices;
551 }; 563 };
552 564
553 } // namespace SkRecords 565 } // namespace SkRecords
554 566
555 void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) { 567 void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) {
556 SkRecords::FillBounds(record, bbh); 568 SkRecords::FillBounds(record, bbh);
557 } 569 }
OLDNEW
« no previous file with comments | « no previous file | tests/RecordDrawTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698