From 22345d608eece1cef845b8781d0f67ec3458fc58 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Mon, 5 Jun 2017 17:33:38 -0700 Subject: [PATCH] Add fonts to Paragraph and additional tests. Change-Id: I023cc9bc413975dabc770f6b59d4ffd1fc842ab2 --- engine/src/flutter/src/font_collection.cc | 5 +- engine/src/flutter/src/paragraph.cc | 11 +- engine/src/flutter/src/text_style.h | 2 +- .../flutter/tests/txt/paragraph_unittests.cc | 119 ++++++++++++++++++ 4 files changed, 130 insertions(+), 7 deletions(-) diff --git a/engine/src/flutter/src/font_collection.cc b/engine/src/flutter/src/font_collection.cc index 74596e102f..dfbfbc14b2 100644 --- a/engine/src/flutter/src/font_collection.cc +++ b/engine/src/flutter/src/font_collection.cc @@ -47,11 +47,12 @@ FontCollection::GetMinikinFontCollectionForFamily(const std::string& family, FTL_DCHECK(skia_font_manager != nullptr); // Ask Skia to resolve a font style set for a font family name. - // FIXME(chinmaygarde): The name "Sample Font" is hardcoded because CoreText + // FIXME(chinmaygarde): The name "Coolvetica" is hardcoded because CoreText // crashes when passed a null string. This seems to be a bug in Skia as // SkFontMgr explicitly says passing in nullptr gives the default font. + auto font_style_set = skia_font_manager->matchFamily( - family.length() == 0 ? "Sample Font" : family.c_str()); + family.length() == 0 ? "Roboto" : family.c_str()); FTL_DCHECK(font_style_set != nullptr); std::vector minikin_fonts; diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index dad0c69127..705a137921 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -114,12 +114,13 @@ void Paragraph::SetText(std::vector text, StyledRuns runs) { } void Paragraph::AddRunsToLineBreaker(const std::string& rootdir) { - auto collection = FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily("", rootdir); minikin::FontStyle font; minikin::MinikinPaint paint; for (size_t i = 0; i < runs_.size(); ++i) { auto run = runs_.GetRun(i); + auto collection = + FontCollection::GetDefaultFontCollection() + .GetMinikinFontCollectionForFamily(run.style.font_family, rootdir); GetFontAndMinikinPaint(run.style, &font, &paint); breaker_.addStyleRun(&paint, collection, font, run.start, run.end, false); } @@ -140,14 +141,16 @@ void Paragraph::Layout(const ParagraphConstraints& constraints, minikin::MinikinPaint minikin_paint; SkTextBlobBuilder builder; - auto collection = FontCollection::GetDefaultFontCollection() - .GetMinikinFontCollectionForFamily("", rootdir); + minikin::Layout layout; SkScalar x = 0.0f; SkScalar y = 0.0f; size_t break_index = 0; for (size_t run_index = 0; run_index < runs_.size(); ++run_index) { auto run = runs_.GetRun(run_index); + auto collection = + FontCollection::GetDefaultFontCollection() + .GetMinikinFontCollectionForFamily(run.style.font_family, rootdir); GetFontAndMinikinPaint(run.style, &font, &minikin_paint); GetPaint(run.style, &paint); diff --git a/engine/src/flutter/src/text_style.h b/engine/src/flutter/src/text_style.h index f2482dd406..2b6c2cc632 100644 --- a/engine/src/flutter/src/text_style.h +++ b/engine/src/flutter/src/text_style.h @@ -34,7 +34,7 @@ class TextStyle { FontWeight font_weight = FontWeight::w400; FontStyle font_style = FontStyle::normal; // TextBaseline text_baseline; - std::string font_family; + std::string font_family = ""; double font_size = 14.0; double letter_spacing = 0.0; double word_spacing = 0.0; diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc index 723b5d828f..dd98be7ea2 100644 --- a/engine/src/flutter/tests/txt/paragraph_unittests.cc +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -34,6 +34,31 @@ TEST_F(RenderTest, SimpleParagraph) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; builder.PushStyle(text_style); + builder.AddText(u16_text); + + builder.Pop(); + + auto paragraph = builder.Build(); + paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()}, + txt::GetFontDir()); + + paragraph->Paint(GetCanvas(), 10.0, 10.0); + + ASSERT_TRUE(Snapshot()); +} + +TEST_F(RenderTest, SimpleRedParagraph) { + const char* text = "I am RED"; + auto icu_text = icu::UnicodeString::fromUTF8(text); + std::u16string u16_text(icu_text.getBuffer(), + icu_text.getBuffer() + icu_text.length()); + + txt::ParagraphStyle paragraph_style; + txt::ParagraphBuilder builder(paragraph_style); + + txt::TextStyle text_style; + text_style.color = SK_ColorRED; + builder.PushStyle(text_style); builder.AddText(u16_text); @@ -47,3 +72,97 @@ TEST_F(RenderTest, SimpleParagraph) { ASSERT_TRUE(Snapshot()); } + +TEST_F(RenderTest, LongParagraph) { + const char* text1 = "RedRoboto"; + auto icu_text1 = icu::UnicodeString::fromUTF8(text1); + std::u16string u16_text1(icu_text1.getBuffer(), + icu_text1.getBuffer() + icu_text1.length()); + const char* text2 = "BigGreenDefault"; + auto icu_text2 = icu::UnicodeString::fromUTF8(text2); + std::u16string u16_text2(icu_text2.getBuffer(), + icu_text2.getBuffer() + icu_text2.length()); + const char* text3 = "DefcolorHomemadeApple"; + auto icu_text3 = icu::UnicodeString::fromUTF8(text3); + std::u16string u16_text3(icu_text3.getBuffer(), + icu_text3.getBuffer() + icu_text3.length()); + const char* text4 = "SmallBlueRoboto"; + auto icu_text4 = icu::UnicodeString::fromUTF8(text4); + std::u16string u16_text4(icu_text4.getBuffer(), + icu_text4.getBuffer() + icu_text4.length()); + const char* text5 = "ContinueLastStyle"; + auto icu_text5 = icu::UnicodeString::fromUTF8(text5); + std::u16string u16_text5(icu_text5.getBuffer(), + icu_text5.getBuffer() + icu_text5.length()); + + txt::ParagraphStyle paragraph_style; + txt::ParagraphBuilder builder(paragraph_style); + + txt::TextStyle text_style1; + text_style1.color = SK_ColorRED; + text_style1.font_family = "Roboto"; + builder.PushStyle(text_style1); + + builder.AddText(u16_text1); + + txt::TextStyle text_style2; + text_style2.font_size = 30; + // Letter spacing not yet implemented + text_style2.letter_spacing = 10; + text_style2.color = SK_ColorGREEN; + builder.PushStyle(text_style2); + + builder.AddText(u16_text2); + + txt::TextStyle text_style3; + text_style3.font_family = "Homemade Apple"; + builder.PushStyle(text_style3); + + builder.AddText(u16_text3); + + txt::TextStyle text_style4; + text_style4.font_size = 10; + text_style4.color = SK_ColorBLUE; + text_style4.font_family = "Roboto"; + builder.PushStyle(text_style4); + + builder.AddText(u16_text4); + + // extra text to see if it goes to default + builder.AddText(u16_text5); + + builder.Pop(); + + auto paragraph = builder.Build(); + paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()}, + txt::GetFontDir()); + + paragraph->Paint(GetCanvas(), 10.0, 30.0); + + ASSERT_TRUE(Snapshot()); +} + +TEST_F(RenderTest, DefaultStyleParagraph) { + const char* text = "No TextStyle! Uh Oh!"; + auto icu_text = icu::UnicodeString::fromUTF8(text); + std::u16string u16_text(icu_text.getBuffer(), + icu_text.getBuffer() + icu_text.length()); + + txt::ParagraphStyle paragraph_style; + txt::ParagraphBuilder builder(paragraph_style); + + txt::TextStyle text_style; + text_style.color = SK_ColorRED; + + builder.AddText(u16_text); + + builder.Pop(); + + auto paragraph = builder.Build(); + paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()}, + txt::GetFontDir()); + + paragraph->Paint(GetCanvas(), 10.0, 10.0); + + ASSERT_TRUE(Snapshot()); +} \ No newline at end of file