Calculating drawing area for a line with shadows

Change-Id: Ifb2de5573f4362332cf174012e7ef9bf8248cd59
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/265524
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Julia Lavrova <jlavrova@google.com>
This commit is contained in:
Julia Lavrova 2020-01-21 13:55:44 -05:00 committed by Skia Commit-Bot
parent 9ed88b3101
commit 51a813db80
2 changed files with 86 additions and 1 deletions

View File

@ -138,6 +138,41 @@ SkRect TextLine::calculateBoundaries() {
}
}
// We need to take in account all the shadows when we calculate the boundaries
// TODO: Need to find a better solution
if (fHasShadows) {
SkRect shadowRect = SkRect::MakeEmpty();
this->iterateThroughVisualRuns(false,
[this, &shadowRect, boundaries]
(const Run* run, SkScalar runOffsetInLine, TextRange textRange, SkScalar* runWidthInLine) {
*runWidthInLine = this->iterateThroughSingleRunByStyles(
run, runOffsetInLine, textRange, StyleType::kShadow,
[&shadowRect, boundaries](TextRange textRange, const TextStyle& style, const ClipContext& context) {
for (TextShadow shadow : style.getShadows()) {
if (!shadow.hasShadow()) continue;
SkPaint paint;
paint.setColor(shadow.fColor);
if (shadow.fBlurRadius != 0.0) {
auto filter = SkMaskFilter::MakeBlur(
kNormal_SkBlurStyle,
SkDoubleToScalar(shadow.fBlurRadius),
false);
paint.setMaskFilter(filter);
SkRect bound;
paint.doComputeFastBounds(boundaries, &bound, SkPaint::Style::kFill_Style);
shadowRect.joinPossiblyEmptyRect(bound);
}
}
});
return true;
});
boundaries.fLeft += shadowRect.fLeft;
boundaries.fTop += shadowRect.fTop;
boundaries.fRight += shadowRect.fRight;
boundaries.fBottom += shadowRect.fBottom;
}
boundaries.offset(this->fOffset); // Line offset from the beginning of the para
boundaries.offset(this->fShift, 0); // Shift produced by formatting
boundaries.offset(0, this->baseline()); // Down by baseline

View File

@ -1657,7 +1657,6 @@ protected:
builder.addText(text);
auto paragraph = builder.Build();
paragraph->layout(this->width());
paragraph->paint(canvas, 0, 0);
}
@ -1769,6 +1768,56 @@ private:
bool direction;
};
class ParagraphView23 : public ParagraphView_Base {
protected:
SkString name() override { return SkString("Paragraph23"); }
void onDrawContent(SkCanvas* canvas) override {
canvas->drawColor(SK_ColorWHITE);
const char* text = "Text with shadow";
ParagraphStyle paragraph_style;
TextStyle text_style;
text_style.setColor(SK_ColorBLACK);
text_style.setFontFamilies({SkString("Google Sans")});
text_style.setFontSize(24);
auto draw = [&](SkScalar h, SkScalar v, SkScalar b) {
text_style.resetShadows();
text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(h, v), b));
ParagraphBuilderImpl builder(paragraph_style, getFontCollection());
builder.pushStyle(text_style);
builder.addText(text);
auto paragraph = builder.Build();
paragraph->layout(300);
paragraph->paint(canvas, 0, 0);
auto rect = SkRect::MakeXYWH(0, 0, paragraph->getMaxWidth(), paragraph->getHeight());
SkPaint paint;
paint.setColor(SK_ColorRED);
paint.setStyle(SkPaint::kStroke_Style);
paint.setAntiAlias(true);
paint.setStrokeWidth(1);
canvas->drawRect(rect, paint);
};
draw(10, 10, 5);
canvas->translate(0, 100);
draw(10, -10, 5);
canvas->translate(0, 100);
draw(-10, -10, 5);
canvas->translate(0, 100);
draw(-10, 10, 5);
canvas->translate(0, 100);
}
private:
typedef Sample INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_SAMPLE(return new ParagraphView1();)
@ -1792,3 +1841,4 @@ DEF_SAMPLE(return new ParagraphView19();)
DEF_SAMPLE(return new ParagraphView20();)
DEF_SAMPLE(return new ParagraphView21();)
DEF_SAMPLE(return new ParagraphView22();)
DEF_SAMPLE(return new ParagraphView23();)