Implement wavy text decoration.

Change-Id: Ie28dc4e833a24a9661e72f32f54a3e7489f81417
This commit is contained in:
Gary Qian
2017-07-05 16:07:55 -07:00
parent 42179e5403
commit b6c6f1780e
6 changed files with 166 additions and 23 deletions

View File

@@ -351,8 +351,7 @@ void Paragraph::Layout(double width, bool force) {
letter_spacing_offset = 0.0f;
word_count = 0;
line_width = 0.0f;
// TODO(abarth): Use the line height, which is something like the max
// font_size for runs in this line times the paragraph's line height.
character_index = layout_end;
break_index += 1;
lines_++;
} else {
@@ -472,9 +471,17 @@ void Paragraph::PaintDecorations(SkCanvas* canvas,
// This is set to 2 for the double line style
int decoration_count = 1;
std::vector<WaveCoordinates> wave_coords;
double width = blob->bounds().fRight + blob->bounds().fLeft;
paint.setStrokeWidth(metrics.fUnderlineThickness *
style.decoration_thickness);
switch (style.decoration_style) {
case TextDecorationStyle::kSolid:
case TextDecorationStyle::kSolid: {
break;
}
case TextDecorationStyle::kDouble: {
decoration_count = 2;
break;
@@ -496,42 +503,71 @@ void Paragraph::PaintDecorations(SkCanvas* canvas,
break;
}
case TextDecorationStyle::kWavy: {
// TODO(garyq): Wave currently does a random wave instead of an ordered
// wave.
const SkScalar intervals[] = {1};
size_t count = sizeof(intervals) / sizeof(intervals[0]);
paint.setPathEffect(SkPathEffect::MakeCompose(
SkDashPathEffect::Make(intervals, count, 0.0f),
SkDiscretePathEffect::Make(metrics.fAvgCharWidth / 10.0f,
metrics.fAvgCharWidth / 10.0f)));
int wave_count = 0;
double x_start = 0;
double y_top = -metrics.fUnderlineThickness;
double y_bottom = metrics.fUnderlineThickness;
while (x_start + metrics.fUnderlineThickness * 2 < x + width) {
wave_coords.push_back(
WaveCoordinates(x_start, wave_count % 2 == 0 ? y_bottom : y_top,
x_start + metrics.fUnderlineThickness * 2,
wave_count % 2 == 0 ? y_top : y_bottom));
x_start += metrics.fUnderlineThickness * 2;
++wave_count;
}
break;
}
}
double width = blob->bounds().fRight + blob->bounds().fLeft;
// Use a for loop for "kDouble" decoration style
for (int i = 0; i < decoration_count; i++) {
double y_offset = i * metrics.fUnderlineThickness * 3.0f;
// Underline
if (style.decoration & 0x1) {
paint.setStrokeWidth(metrics.fUnderlineThickness);
canvas->drawLine(x, y + metrics.fUnderlineThickness + y_offset,
x + width, y + metrics.fUnderlineThickness + y_offset,
paint);
if (style.decoration_style != TextDecorationStyle::kWavy)
canvas->drawLine(x, y + metrics.fUnderlineThickness + y_offset,
x + width,
y + metrics.fUnderlineThickness + y_offset, paint);
else
PaintWavyDecoration(canvas, wave_coords, paint, x, y,
metrics.fUnderlineThickness, width);
}
// Overline
if (style.decoration & 0x2) {
paint.setStrokeWidth(metrics.fUnderlineThickness);
canvas->drawLine(x, y + metrics.fAscent + y_offset, x + width,
y + metrics.fAscent + y_offset, paint);
if (style.decoration_style != TextDecorationStyle::kWavy)
canvas->drawLine(x, y + metrics.fAscent + y_offset, x + width,
y + metrics.fAscent + y_offset, paint);
else
PaintWavyDecoration(canvas, wave_coords, paint, x, y, metrics.fAscent,
width);
}
// Strikethrough
if (style.decoration & 0x4) {
paint.setStrokeWidth(metrics.fUnderlineThickness);
canvas->drawLine(x, y - metrics.fXHeight / 2 + y_offset, x + width,
y - metrics.fXHeight / 2 + y_offset, paint);
if (style.decoration_style != TextDecorationStyle::kWavy)
canvas->drawLine(x, y - metrics.fXHeight / 2 + y_offset, x + width,
y - metrics.fXHeight / 2 + y_offset, paint);
else
PaintWavyDecoration(canvas, wave_coords, paint, x, y,
-metrics.fXHeight / 2, width);
}
}
}
}
void Paragraph::PaintWavyDecoration(SkCanvas* canvas,
std::vector<WaveCoordinates> wave_coords,
SkPaint paint,
double x,
double y,
double y_offset,
double width) {
for (size_t i = 0; i < wave_coords.size(); ++i) {
WaveCoordinates coords = wave_coords[i];
canvas->drawLine(x + coords.x_start, y + y_offset + coords.y_start,
x + coords.x_end, y + y_offset + coords.y_end, paint);
}
}
int Paragraph::GetLineCount() const {
return lines_;
}

View File

@@ -70,6 +70,7 @@ class Paragraph {
FRIEND_TEST(RenderTest, RightAlignParagraph);
FRIEND_TEST(RenderTest, CenterAlignParagraph);
FRIEND_TEST(RenderTest, JustifyAlignParagraph);
FRIEND_TEST(RenderTest, DecorationsParagraph);
FRIEND_TEST(RenderTest, ItalicsParagraph);
std::vector<uint16_t> text_;
@@ -89,6 +90,16 @@ class Paragraph {
double ideographic_baseline_ = FLT_MAX;
bool needs_layout_ = true;
struct WaveCoordinates {
double x_start;
double y_start;
double x_end;
double y_end;
WaveCoordinates(double x_s, double y_s, double x_e, double y_e)
: x_start(x_s), y_start(y_s), x_end(x_e), y_end(y_e) {}
};
void SetText(std::vector<uint16_t> text, StyledRuns runs);
void SetParagraphStyle(const ParagraphStyle& style);
@@ -111,6 +122,14 @@ class Paragraph {
SkPaint::FontMetrics metrics,
SkTextBlob* blob);
void PaintWavyDecoration(SkCanvas* canvas,
std::vector<WaveCoordinates> wave_coords,
SkPaint paint,
double x,
double y,
double y_offset,
double width);
FTL_DISALLOW_COPY_AND_ASSIGN(Paragraph);
};

View File

@@ -64,6 +64,7 @@ class StyledRuns {
FRIEND_TEST(RenderTest, RightAlignParagraph);
FRIEND_TEST(RenderTest, CenterAlignParagraph);
FRIEND_TEST(RenderTest, JustifyAlignParagraph);
FRIEND_TEST(RenderTest, DecorationsParagraph);
FRIEND_TEST(RenderTest, ItalicsParagraph);
struct IndexedRun {

View File

@@ -24,6 +24,14 @@ namespace txt {
bool TextStyle::equals(const TextStyle& other) const {
if (color != other.color)
return false;
if (decoration != other.decoration)
return false;
if (decoration_color != other.decoration_color)
return false;
if (decoration_style != other.decoration_style)
return false;
if (decoration_thickness != other.decoration_thickness)
return false;
if (font_weight != other.font_weight)
return false;
if (font_style != other.font_style)

View File

@@ -33,6 +33,7 @@ class TextStyle {
TextDecoration decoration = TextDecoration::kNone;
SkColor decoration_color = SK_ColorWHITE;
TextDecorationStyle decoration_style = TextDecorationStyle::kSolid;
double decoration_thickness = 1.0;
FontWeight font_weight = FontWeight::w400;
FontStyle font_style = FontStyle::normal;
bool fake_bold = false;

View File

@@ -636,6 +636,84 @@ TEST_F(RenderTest, JustifyAlignParagraph) {
ASSERT_TRUE(Snapshot());
}
TEST_F(RenderTest, DecorationsParagraph) {
txt::ParagraphStyle paragraph_style;
paragraph_style.max_lines = 14;
paragraph_style.text_align = TextAlign::left;
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
txt::TextStyle text_style;
text_style.font_size = 26;
text_style.letter_spacing = 0;
text_style.word_spacing = 5;
text_style.color = SK_ColorBLACK;
text_style.height = 2;
text_style.decoration = txt::TextDecoration(0x1 | 0x2 | 0x4);
text_style.decoration_style = txt::TextDecorationStyle::kSolid;
text_style.decoration_color = SK_ColorBLACK;
builder.PushStyle(text_style);
builder.AddText("This text should be");
text_style.decoration_style = txt::TextDecorationStyle::kDouble;
text_style.decoration_color = SK_ColorBLUE;
builder.PushStyle(text_style);
builder.AddText(" decorated even when");
text_style.decoration_style = txt::TextDecorationStyle::kDotted;
text_style.decoration_color = SK_ColorBLACK;
builder.PushStyle(text_style);
builder.AddText(" wrapped around to");
text_style.decoration_style = txt::TextDecorationStyle::kDashed;
text_style.decoration_color = SK_ColorBLACK;
builder.PushStyle(text_style);
builder.AddText(" the next line.");
text_style.decoration_style = txt::TextDecorationStyle::kWavy;
text_style.decoration_color = SK_ColorRED;
builder.PushStyle(text_style);
builder.AddText(" Otherwise, bad things happen.");
builder.Pop();
auto paragraph = builder.Build();
paragraph->Layout(GetTestCanvasWidth() - 100);
paragraph->Paint(GetCanvas(), 0, 0);
ASSERT_EQ(paragraph->runs_.size(), 5ull);
ASSERT_EQ(paragraph->records_.size(), 6ull);
for (size_t i = 0; i < 6; ++i) {
ASSERT_EQ(paragraph->records_[i].style().decoration,
txt::TextDecoration(0x1 | 0x2 | 0x4));
}
ASSERT_EQ(paragraph->records_[0].style().decoration_style,
txt::TextDecorationStyle::kSolid);
ASSERT_EQ(paragraph->records_[1].style().decoration_style,
txt::TextDecorationStyle::kDouble);
ASSERT_EQ(paragraph->records_[2].style().decoration_style,
txt::TextDecorationStyle::kDotted);
ASSERT_EQ(paragraph->records_[3].style().decoration_style,
txt::TextDecorationStyle::kDashed);
ASSERT_EQ(paragraph->records_[4].style().decoration_style,
txt::TextDecorationStyle::kDashed);
ASSERT_EQ(paragraph->records_[5].style().decoration_style,
txt::TextDecorationStyle::kWavy);
ASSERT_EQ(paragraph->records_[0].style().decoration_color, SK_ColorBLACK);
ASSERT_EQ(paragraph->records_[1].style().decoration_color, SK_ColorBLUE);
ASSERT_EQ(paragraph->records_[2].style().decoration_color, SK_ColorBLACK);
ASSERT_EQ(paragraph->records_[3].style().decoration_color, SK_ColorBLACK);
ASSERT_EQ(paragraph->records_[4].style().decoration_color, SK_ColorBLACK);
ASSERT_EQ(paragraph->records_[5].style().decoration_color, SK_ColorRED);
ASSERT_TRUE(Snapshot());
}
TEST_F(RenderTest, ItalicsParagraph) {
const char* text = "I am Italicized! ";
auto icu_text = icu::UnicodeString::fromUTF8(text);