Add additional test framework and tests for Italics. Overloaded paragraph text entry methods.
Change-Id: I0ae9abd6130edd276b1144a0092e03e47373427d
This commit is contained in:
@@ -17,9 +17,12 @@
|
||||
#include "lib/txt/src/font_collection.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include "lib/ftl/logging.h"
|
||||
#include "lib/txt/src/font_skia.h"
|
||||
#include "third_party/skia/include/core/SkString.h"
|
||||
#include "third_party/skia/include/ports/SkFontMgr.h"
|
||||
#include "third_party/skia/include/ports/SkFontMgr_directory.h"
|
||||
|
||||
@@ -36,6 +39,19 @@ FontCollection::FontCollection() = default;
|
||||
|
||||
FontCollection::~FontCollection() = default;
|
||||
|
||||
std::set<std::string> FontCollection::GetFamilyNames(const std::string& dir) {
|
||||
auto skia_font_manager = dir.length() != 0
|
||||
? SkFontMgr_New_Custom_Directory(dir.c_str())
|
||||
: SkFontMgr::RefDefault();
|
||||
std::set<std::string> names;
|
||||
SkString str;
|
||||
for (int i = 0; i < skia_font_manager->countFamilies(); i++) {
|
||||
skia_font_manager->getFamilyName(i, &str);
|
||||
names.insert(std::string{str.writable_str()});
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
const std::string FontCollection::ProcessFamilyName(const std::string& family) {
|
||||
return family.length() == 0 ? DEFAULT_FAMILY_NAME : family;
|
||||
}
|
||||
|
||||
@@ -20,13 +20,14 @@
|
||||
#define DEFAULT_FAMILY_NAME "Roboto"
|
||||
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "lib/ftl/macros.h"
|
||||
#include "lib/txt/tests/txt/render_test.h"
|
||||
#include "minikin/FontCollection.h"
|
||||
#include "minikin/FontFamily.h"
|
||||
#include "third_party/gtest/include/gtest/gtest_prod.h"
|
||||
|
||||
namespace txt {
|
||||
|
||||
@@ -41,17 +42,21 @@ class FontCollection {
|
||||
std::shared_ptr<minikin::FontCollection> GetMinikinFontCollectionForFamily(
|
||||
const std::string& family,
|
||||
const std::string& dir = "");
|
||||
// Temporarily public.
|
||||
|
||||
// Provides a vector of all available family names.
|
||||
static std::set<std::string> GetFamilyNames(const std::string& dir = "");
|
||||
|
||||
private:
|
||||
FRIEND_TEST(FontCollection, HasDefaultRegistrations);
|
||||
FRIEND_TEST(FontCollection, GetMinikinFontCollections);
|
||||
FRIEND_TEST(FontCollection, GetFamilyNames);
|
||||
|
||||
const std::string ProcessFamilyName(const std::string& family);
|
||||
|
||||
// For Testing. Temporarily public.
|
||||
static const std::string GetDefaultFamilyName() {
|
||||
return DEFAULT_FAMILY_NAME;
|
||||
};
|
||||
|
||||
private:
|
||||
friend RenderTest;
|
||||
|
||||
// TODO(chinmaygarde): Caches go here.
|
||||
FTL_DISALLOW_COPY_AND_ASSIGN(FontCollection);
|
||||
};
|
||||
|
||||
@@ -60,13 +60,13 @@ int GetWeight(const TextStyle& style) {
|
||||
return 2;
|
||||
case FontWeight::w300:
|
||||
return 3;
|
||||
case FontWeight::w400:
|
||||
case FontWeight::w400: // Normal.
|
||||
return 4;
|
||||
case FontWeight::w500:
|
||||
return 5;
|
||||
case FontWeight::w600:
|
||||
return 6;
|
||||
case FontWeight::w700:
|
||||
case FontWeight::w700: // Bold.
|
||||
return 7;
|
||||
case FontWeight::w800:
|
||||
return 8;
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "lib/txt/src/paragraph_constraints.h"
|
||||
#include "lib/txt/src/styled_runs.h"
|
||||
#include "minikin/LineBreaker.h"
|
||||
#include "third_party/gtest/include/gtest/gtest_prod.h"
|
||||
#include "third_party/skia/include/core/SkTextBlob.h"
|
||||
|
||||
class SkCanvas;
|
||||
@@ -43,6 +44,13 @@ class Paragraph {
|
||||
|
||||
private:
|
||||
friend class ParagraphBuilder;
|
||||
FRIEND_TEST(RenderTest, SimpleParagraph);
|
||||
FRIEND_TEST(RenderTest, SimpleRedParagraph);
|
||||
FRIEND_TEST(RenderTest, RainbowParagraph);
|
||||
FRIEND_TEST(RenderTest, DefaultStyleParagraph);
|
||||
FRIEND_TEST(RenderTest, BoldParagraph);
|
||||
FRIEND_TEST(RenderTest, LinebreakParagraph);
|
||||
FRIEND_TEST(RenderTest, ItalicsParagraph);
|
||||
|
||||
std::vector<uint16_t> text_;
|
||||
StyledRuns runs_;
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
#include "lib/txt/src/paragraph_builder.h"
|
||||
#include "third_party/icu/source/common/unicode/unistr.h"
|
||||
|
||||
namespace txt {
|
||||
|
||||
@@ -46,6 +47,20 @@ void ParagraphBuilder::AddText(const std::u16string& text) {
|
||||
text_.insert(text_.end(), text.begin(), text.end());
|
||||
}
|
||||
|
||||
void ParagraphBuilder::AddText(const std::string& text) {
|
||||
auto icu_text = icu::UnicodeString::fromUTF8(text);
|
||||
std::u16string u16_text(icu_text.getBuffer(),
|
||||
icu_text.getBuffer() + icu_text.length());
|
||||
AddText(u16_text);
|
||||
}
|
||||
|
||||
void ParagraphBuilder::AddText(const char* text) {
|
||||
auto icu_text = icu::UnicodeString::fromUTF8(text);
|
||||
std::u16string u16_text(icu_text.getBuffer(),
|
||||
icu_text.getBuffer() + icu_text.length());
|
||||
AddText(u16_text);
|
||||
}
|
||||
|
||||
std::unique_ptr<Paragraph> ParagraphBuilder::Build() {
|
||||
runs_.EndRunIfNeeded(text_.size());
|
||||
std::unique_ptr<Paragraph> paragraph = std::make_unique<Paragraph>();
|
||||
|
||||
@@ -40,6 +40,10 @@ class ParagraphBuilder {
|
||||
|
||||
void AddText(const std::u16string& text);
|
||||
|
||||
void AddText(const std::string& text);
|
||||
|
||||
void AddText(const char* text);
|
||||
|
||||
std::unique_ptr<Paragraph> Build();
|
||||
|
||||
private:
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
#include "lib/ftl/logging.h"
|
||||
#include "lib/txt/tests/txt/utils.h"
|
||||
|
||||
namespace txt {
|
||||
|
||||
TEST(FontCollection, HasDefaultRegistrations) {
|
||||
std::string defaultFamilyName = txt::FontCollection::GetDefaultFamilyName();
|
||||
|
||||
@@ -61,3 +63,12 @@ TEST(FontCollection, GetMinikinFontCollections) {
|
||||
ASSERT_NE(collectionHomemadeApple, collectionRoboto);
|
||||
ASSERT_NE(collectionDef.get(), nullptr);
|
||||
}
|
||||
|
||||
TEST(FontCollection, GetFamilyNames) {
|
||||
std::set<std::string> names =
|
||||
txt::FontCollection::GetFamilyNames(txt::GetFontDir());
|
||||
|
||||
ASSERT_EQ(names.size(), 19ull);
|
||||
}
|
||||
|
||||
} // namespace txt
|
||||
@@ -17,13 +17,15 @@
|
||||
#include "lib/ftl/logging.h"
|
||||
#include "lib/txt/src/font_style.h"
|
||||
#include "lib/txt/src/font_weight.h"
|
||||
#include "lib/txt/src/paragraph.h"
|
||||
#include "lib/txt/tests/txt/utils.h"
|
||||
#include "paragraph.h"
|
||||
#include "paragraph_builder.h"
|
||||
#include "render_test.h"
|
||||
#include "third_party/icu/source/common/unicode/unistr.h"
|
||||
#include "third_party/skia/include/core/SkColor.h"
|
||||
|
||||
namespace txt {
|
||||
|
||||
TEST_F(RenderTest, SimpleParagraph) {
|
||||
const char* text = "Hello World";
|
||||
auto icu_text = icu::UnicodeString::fromUTF8(text);
|
||||
@@ -46,6 +48,10 @@ TEST_F(RenderTest, SimpleParagraph) {
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 15.0);
|
||||
|
||||
ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
|
||||
for (size_t i = 0; i < u16_text.length(); i++) {
|
||||
ASSERT_EQ(paragraph->text_[i], u16_text[i]);
|
||||
}
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
@@ -72,6 +78,10 @@ TEST_F(RenderTest, SimpleRedParagraph) {
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 15.0);
|
||||
|
||||
ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
|
||||
for (size_t i = 0; i < u16_text.length(); i++) {
|
||||
ASSERT_EQ(paragraph->text_[i], u16_text[i]);
|
||||
}
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
@@ -108,7 +118,7 @@ TEST_F(RenderTest, RainbowParagraph) {
|
||||
builder.AddText(u16_text1);
|
||||
|
||||
txt::TextStyle text_style2;
|
||||
text_style2.font_size = 30;
|
||||
text_style2.font_size = 50;
|
||||
// Letter spacing not yet implemented
|
||||
text_style2.letter_spacing = 100;
|
||||
text_style2.font_weight = txt::FontWeight::w600;
|
||||
@@ -142,8 +152,12 @@ TEST_F(RenderTest, RainbowParagraph) {
|
||||
paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()},
|
||||
txt::GetFontDir());
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 30.0);
|
||||
paragraph->Paint(GetCanvas(), 10.0, 50.0);
|
||||
|
||||
u16_text1 += u16_text2 + u16_text3 + u16_text4;
|
||||
for (size_t i = 0; i < u16_text1.length(); i++) {
|
||||
ASSERT_EQ(paragraph->text_[i], u16_text1[i]);
|
||||
}
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
@@ -170,6 +184,10 @@ TEST_F(RenderTest, DefaultStyleParagraph) {
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 15.0);
|
||||
|
||||
ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
|
||||
for (size_t i = 0; i < u16_text.length(); i++) {
|
||||
ASSERT_EQ(paragraph->text_[i], u16_text[i]);
|
||||
}
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
@@ -186,7 +204,7 @@ TEST_F(RenderTest, BoldParagraph) {
|
||||
text_style.font_size = 60;
|
||||
// Letter spacing not yet implemented
|
||||
text_style.letter_spacing = 10;
|
||||
text_style.font_weight = txt::FontWeight::w900;
|
||||
text_style.font_weight = txt::FontWeight::w700;
|
||||
text_style.fake_bold = true;
|
||||
text_style.color = SK_ColorRED;
|
||||
builder.PushStyle(text_style);
|
||||
@@ -201,6 +219,10 @@ TEST_F(RenderTest, BoldParagraph) {
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 60.0);
|
||||
|
||||
ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
|
||||
for (size_t i = 0; i < u16_text.length(); i++) {
|
||||
ASSERT_EQ(paragraph->text_[i], u16_text[i]);
|
||||
}
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
@@ -257,5 +279,43 @@ TEST_F(RenderTest, LinebreakParagraph) {
|
||||
|
||||
paragraph->Paint(GetCanvas(), 5.0, 30.0);
|
||||
|
||||
ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
|
||||
for (size_t i = 0; i < u16_text.length(); i++) {
|
||||
ASSERT_EQ(paragraph->text_[i], u16_text[i]);
|
||||
}
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(RenderTest, ItalicsParagraph) {
|
||||
const char* text = "I am Italicized!";
|
||||
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;
|
||||
text_style.font_style = txt::FontStyle::italic;
|
||||
text_style.font_size = 35;
|
||||
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, 35.0);
|
||||
|
||||
ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
|
||||
for (size_t i = 0; i < u16_text.length(); i++) {
|
||||
ASSERT_EQ(paragraph->text_[i], u16_text[i]);
|
||||
}
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
} // namespace txt
|
||||
|
||||
Reference in New Issue
Block a user