From 79f20a88865c362f2caa3f4c8325eebc1f82ff47 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 2 Jun 2017 14:43:10 -0700 Subject: [PATCH] Update libTXT sources and tests from initial prototype. This separates libTXT tests and Minikin tests and accounts for building with an older version of ICU. Once ICU has been updated, the workarounds for emoji handling will be removed. Change-Id: Ic184e653a2561629b01f98aeb4f6fb88aebbfa88 --- engine/src/flutter/BUILD.gn | 6 +- engine/src/flutter/examples/BUILD.gn | 1 + engine/src/flutter/examples/main.cc | 12 +-- .../src/flutter/include/minikin/Hyphenator.h | 4 + .../src/flutter/include/minikin/LineBreaker.h | 4 + engine/src/flutter/libs/minikin/BUILD.gn | 2 + engine/src/flutter/libs/minikin/Emoji.cpp | 12 +++ engine/src/flutter/libs/minikin/Layout.cpp | 5 ++ engine/src/flutter/src/BUILD.gn | 13 +-- engine/src/flutter/src/font_collection.cc | 87 +++++++++++++++++++ .../{font_provider.h => font_collection.h} | 16 ++-- engine/src/flutter/src/font_provider.cc | 78 ----------------- engine/src/flutter/src/paragraph.cc | 12 +-- engine/src/flutter/src/paragraph.h | 6 +- engine/src/flutter/src/paragraph_builder.cc | 4 +- engine/src/flutter/src/paragraph_builder.h | 2 +- .../src/flutter/src/paragraph_constraints.cc | 23 +++++ .../src/flutter/src/paragraph_constraints.h | 38 ++++++++ engine/src/flutter/src/paragraph_style.cc | 17 ++++ engine/src/flutter/src/text_style.cc | 17 ++++ engine/src/flutter/tests/txt/BUILD.gn | 40 +++++++++ .../tests/txt/font_collection_unittests.cc | 25 ++++++ .../flutter/tests/txt/paragraph_unittests.cc | 47 ++++++++++ engine/src/flutter/tests/txt/render_test.cc | 67 ++++++++++++++ engine/src/flutter/tests/txt/render_test.h | 52 +++++++++++ .../tests/txt/txt_run_all_unittests.cc | 26 ++++++ engine/src/flutter/tests/unittest/BUILD.gn | 15 ++-- 27 files changed, 514 insertions(+), 117 deletions(-) create mode 100644 engine/src/flutter/src/font_collection.cc rename engine/src/flutter/src/{font_provider.h => font_collection.h} (75%) delete mode 100644 engine/src/flutter/src/font_provider.cc create mode 100644 engine/src/flutter/src/paragraph_constraints.cc create mode 100644 engine/src/flutter/src/paragraph_constraints.h create mode 100644 engine/src/flutter/src/paragraph_style.cc create mode 100644 engine/src/flutter/src/text_style.cc create mode 100644 engine/src/flutter/tests/txt/BUILD.gn create mode 100644 engine/src/flutter/tests/txt/font_collection_unittests.cc create mode 100644 engine/src/flutter/tests/txt/paragraph_unittests.cc create mode 100644 engine/src/flutter/tests/txt/render_test.cc create mode 100644 engine/src/flutter/tests/txt/render_test.h create mode 100644 engine/src/flutter/tests/txt/txt_run_all_unittests.cc diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index 36804272fd..34c9c145fa 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -16,9 +16,9 @@ group("txt") { testonly = true deps = [ - "libs/minikin", - "src", - "tests/unittest($host_toolchain)", "examples:txt_example($host_toolchain)", + "src", + "tests/txt($host_toolchain)", # txt_unittests + "tests/unittest($host_toolchain)", # minikin_unittest ] } diff --git a/engine/src/flutter/examples/BUILD.gn b/engine/src/flutter/examples/BUILD.gn index bd19515179..20780a3d65 100644 --- a/engine/src/flutter/examples/BUILD.gn +++ b/engine/src/flutter/examples/BUILD.gn @@ -20,6 +20,7 @@ executable("txt_example") { deps = [ "//dart/runtime:libdart_jit", "//flutter/fml", + "//lib/txt/libs/minikin", "//lib/txt/src", "//third_party/icu:icuuc", "//third_party/skia", diff --git a/engine/src/flutter/examples/main.cc b/engine/src/flutter/examples/main.cc index 4d1b5b88a5..3137207c38 100644 --- a/engine/src/flutter/examples/main.cc +++ b/engine/src/flutter/examples/main.cc @@ -31,7 +31,9 @@ int runTest() { "I can tell. Rock, rock on. " "\xe0\xa4\xa8\xe0\xa4\xae\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\xa4\xe0\xa5" "\x87"; - icu::UnicodeString text = icu::UnicodeString::fromUTF8(utf8_text); + icu::UnicodeString icu_text = icu::UnicodeString::fromUTF8(utf8_text); + std::u16string u16_text(icu_text.getBuffer(), + icu_text.getBuffer() + icu_text.length()); ParagraphStyle paragraph_style; ParagraphBuilder builder(paragraph_style); @@ -39,18 +41,18 @@ int runTest() { style.color = SK_ColorBLUE; style.font_size = 32.0; builder.PushStyle(style); - builder.AddText(text.getBuffer(), text.length()); + builder.AddText(u16_text); style.color = SK_ColorYELLOW; builder.PushStyle(style); - builder.AddText(text.getBuffer(), text.length()); + builder.AddText(u16_text); builder.Pop(); - builder.AddText(text.getBuffer(), text.length()); + builder.AddText(u16_text); builder.Pop(); auto paragraph = builder.Build(); int width = 800; int height = 600; - paragraph->Layout(width); + paragraph->Layout(ParagraphConstraints(width)); SkAutoGraphics ag; SkBitmap bitmap; diff --git a/engine/src/flutter/include/minikin/Hyphenator.h b/engine/src/flutter/include/minikin/Hyphenator.h index 2b8ccb71a7..c2d9d3b63a 100644 --- a/engine/src/flutter/include/minikin/Hyphenator.h +++ b/engine/src/flutter/include/minikin/Hyphenator.h @@ -18,6 +18,10 @@ * An implementation of Liang's hyphenation algorithm. */ +#ifndef U_USING_ICU_NAMESPACE +#define U_USING_ICU_NAMESPACE 0 +#endif // U_USING_ICU_NAMESPACE + #include "unicode/locid.h" #include #include diff --git a/engine/src/flutter/include/minikin/LineBreaker.h b/engine/src/flutter/include/minikin/LineBreaker.h index 46936bbd57..c2f546db76 100644 --- a/engine/src/flutter/include/minikin/LineBreaker.h +++ b/engine/src/flutter/include/minikin/LineBreaker.h @@ -22,6 +22,10 @@ #ifndef MINIKIN_LINE_BREAKER_H #define MINIKIN_LINE_BREAKER_H +#ifndef U_USING_ICU_NAMESPACE +#define U_USING_ICU_NAMESPACE 0 +#endif // U_USING_ICU_NAMESPACE + #include "unicode/brkiter.h" #include "unicode/locid.h" #include diff --git a/engine/src/flutter/libs/minikin/BUILD.gn b/engine/src/flutter/libs/minikin/BUILD.gn index 5ce005f45a..f723d0a8e1 100644 --- a/engine/src/flutter/libs/minikin/BUILD.gn +++ b/engine/src/flutter/libs/minikin/BUILD.gn @@ -17,6 +17,8 @@ config("minikin_config") { } static_library("minikin") { + defines = [ "WIP_NEEDS_ICU_UPDATE" ] + sources = [ "CmapCoverage.cpp", "Emoji.cpp", diff --git a/engine/src/flutter/libs/minikin/Emoji.cpp b/engine/src/flutter/libs/minikin/Emoji.cpp index fbe68ca843..df43c75b36 100644 --- a/engine/src/flutter/libs/minikin/Emoji.cpp +++ b/engine/src/flutter/libs/minikin/Emoji.cpp @@ -37,16 +37,27 @@ bool isNewEmoji(uint32_t c) { } bool isEmoji(uint32_t c) { +#if WIP_NEEDS_ICU_UPDATE + return false; +#else // WIP_NEEDS_ICU_UPDATE return isNewEmoji(c) || u_hasBinaryProperty(c, UCHAR_EMOJI); +#endif // WIP_NEEDS_ICU_UPDATE } bool isEmojiModifier(uint32_t c) { +#if WIP_NEEDS_ICU_UPDATE + return false; +#else // WIP_NEEDS_ICU_UPDATE // Emoji modifier are not expected to change, so there's a small change we need to customize // this. return u_hasBinaryProperty(c, UCHAR_EMOJI_MODIFIER); +#endif // WIP_NEEDS_ICU_UPDATE } bool isEmojiBase(uint32_t c) { +#if WIP_NEEDS_ICU_UPDATE + return false; +#else // WIP_NEEDS_ICU_UPDATE // These two characters were removed from Emoji_Modifier_Base in Emoji 4.0, but we need to keep // them as emoji modifier bases since there are fonts and user-generated text out there that // treats these as potential emoji bases. @@ -62,6 +73,7 @@ bool isEmojiBase(uint32_t c) { return true; } return u_hasBinaryProperty(c, UCHAR_EMOJI_MODIFIER_BASE); +#endif // WIP_NEEDS_ICU_UPDATE } UCharDirection emojiBidiOverride(const void* /* context */, UChar32 c) { diff --git a/engine/src/flutter/libs/minikin/Layout.cpp b/engine/src/flutter/libs/minikin/Layout.cpp index e6c5bc912d..697fbc2ba2 100644 --- a/engine/src/flutter/libs/minikin/Layout.cpp +++ b/engine/src/flutter/libs/minikin/Layout.cpp @@ -711,6 +711,10 @@ static void addFeatures(const string &str, vector* features) { static const hb_codepoint_t CHAR_HYPHEN = 0x2010; /* HYPHEN */ static inline hb_codepoint_t determineHyphenChar(hb_codepoint_t preferredHyphen, hb_font_t* font) { +#if WIP_NEEDS_ICU_UPDATE + (void)CHAR_HYPHEN; + return 0x002D; // HYPHEN-MINUS +#else // WIP_NEEDS_ICU_UPDATE hb_codepoint_t glyph; if (preferredHyphen == 0x058A /* ARMENIAN_HYPHEN */ || preferredHyphen == 0x05BE /* HEBREW PUNCTUATION MAQAF */ @@ -732,6 +736,7 @@ static inline hb_codepoint_t determineHyphenChar(hb_codepoint_t preferredHyphen, } } return preferredHyphen; +#endif // WIP_NEEDS_ICU_UPDATE } static inline void addHyphenToHbBuffer(hb_buffer_t* buffer, hb_font_t* font, uint32_t hyphen, diff --git a/engine/src/flutter/src/BUILD.gn b/engine/src/flutter/src/BUILD.gn index 2d603bb38a..e8520ec113 100644 --- a/engine/src/flutter/src/BUILD.gn +++ b/engine/src/flutter/src/BUILD.gn @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -static_library("src") { +source_set("src") { sources = [ - "font_provider.cc", - "font_provider.h", + "font_collection.cc", + "font_collection.h", "font_skia.cc", "font_skia.h", "font_style.h", @@ -26,15 +26,18 @@ static_library("src") { "paragraph.h", "paragraph_builder.cc", "paragraph_builder.h", + "paragraph_constraints.cc", + "paragraph_constraints.h", + "paragraph_style.cc", "paragraph_style.h", "styled_runs.cc", "styled_runs.h", "text_align.h", + "text_style.cc", "text_style.h", ] - public_deps = [ - "//lib/ftl", + deps = [ "//lib/txt/libs/minikin", "//third_party/skia", ] diff --git a/engine/src/flutter/src/font_collection.cc b/engine/src/flutter/src/font_collection.cc new file mode 100644 index 0000000000..dc0207b4e5 --- /dev/null +++ b/engine/src/flutter/src/font_collection.cc @@ -0,0 +1,87 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lib/txt/src/font_collection.h" + +#include + +#include "lib/ftl/logging.h" +#include "lib/txt/src/font_skia.h" +#include "third_party/skia/include/ports/SkFontMgr.h" + +namespace txt { + +FontCollection& FontCollection::GetDefaultFontCollection() { + static FontCollection* collection = nullptr; + static std::once_flag once; + std::call_once(once, []() { collection = new FontCollection(); }); + return *collection; +} + +FontCollection::FontCollection() = default; + +FontCollection::~FontCollection() = default; + +std::shared_ptr +FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) { + // Get the Skia font manager. + auto skia_font_manager = SkFontMgr::RefDefault(); + FTL_DCHECK(skia_font_manager != nullptr); + + // Ask Skia to resolve a font style set for a font family name. + // FIXME(chinmaygarde): The name "Hevetica" 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 ? "Helvetica" : family.c_str()); + FTL_DCHECK(font_style_set != nullptr); + + std::vector minikin_fonts; + + // Add fonts to the Minikin font family. + for (int i = 0, style_count = font_style_set->count(); i < style_count; ++i) { + // Create the skia typeface + auto skia_typeface = + sk_ref_sp(font_style_set->createTypeface(i)); + + if (skia_typeface == nullptr) { + continue; + } + + // Create the minikin font from the skia typeface. + minikin::Font minikin_font( + std::make_shared(skia_typeface), + minikin::FontStyle{skia_typeface->fontStyle().weight(), + skia_typeface->isItalic()}); + + minikin_fonts.emplace_back(std::move(minikin_font)); + } + + // Create a Minikin font family. + auto minikin_family = + std::make_shared(std::move(minikin_fonts)); + + // Create a vector of font families for the Minkin font collection. For now, + // we only have one family in our collection. + std::vector> minikin_families = { + minikin_family, + }; + + // Return the font collection. + return std::make_shared(minikin_families); +} + +} // namespace txt diff --git a/engine/src/flutter/src/font_provider.h b/engine/src/flutter/src/font_collection.h similarity index 75% rename from engine/src/flutter/src/font_provider.h rename to engine/src/flutter/src/font_collection.h index 264ee149fc..efca9e35c2 100644 --- a/engine/src/flutter/src/font_provider.h +++ b/engine/src/flutter/src/font_collection.h @@ -22,25 +22,25 @@ #include #include "lib/ftl/macros.h" -#include "lib/txt/include/minikin/FontCollection.h" -#include "lib/txt/include/minikin/FontFamily.h" +#include "minikin/FontCollection.h" +#include "minikin/FontFamily.h" namespace txt { -class FontProvider { +class FontCollection { public: - static FontProvider& GetDefault(); + static FontCollection& GetDefaultFontCollection(); - FontProvider(); + FontCollection(); - ~FontProvider(); + ~FontCollection(); - std::shared_ptr GetFontCollectionForFamily( + std::shared_ptr GetMinikinFontCollectionForFamily( const std::string& family); private: // TODO(chinmaygarde): Caches go here. - FTL_DISALLOW_COPY_AND_ASSIGN(FontProvider); + FTL_DISALLOW_COPY_AND_ASSIGN(FontCollection); }; } // namespace txt diff --git a/engine/src/flutter/src/font_provider.cc b/engine/src/flutter/src/font_provider.cc deleted file mode 100644 index a8a3c8fe9c..0000000000 --- a/engine/src/flutter/src/font_provider.cc +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2017 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "lib/txt/src/font_provider.h" - -#include - -#include "lib/ftl/logging.h" -#include "lib/txt/src/font_skia.h" -#include "third_party/skia/include/ports/SkFontMgr.h" - -namespace txt { -namespace { - -bool IsItalic(SkFontStyle::Slant slant) { - return slant != SkFontStyle::Slant::kUpright_Slant; -} - -} // namespace - -FontProvider& FontProvider::GetDefault() { - static FontProvider* provider = nullptr; - static std::once_flag once; - std::call_once(once, []() { provider = new FontProvider(); }); - return *provider; -} - -FontProvider::FontProvider() = default; - -FontProvider::~FontProvider() = default; - -std::shared_ptr -FontProvider::GetFontCollectionForFamily(const std::string& family) { - // Get the Skia font manager. - auto font_manager = SkFontMgr::RefDefault(); - FTL_DCHECK(font_manager != nullptr); - - // Ask Skia to resolve a font style set for a font family name. - // FIXME(chinmaygarde): The name "Hevetica" 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 = font_manager->matchFamily( - family.length() == 0 ? "Helvetica" : family.c_str()); - FTL_DCHECK(font_style_set != nullptr); - - std::vector fonts; - - // Add fonts to the Minikin font family. - for (int i = 0, style_count = font_style_set->count(); i < style_count; ++i) { - auto skia_typeface = - sk_ref_sp(font_style_set->createTypeface(i)); - auto typeface = std::make_shared(std::move(skia_typeface)); - - SkFontStyle font_style; - font_style_set->getStyle(i, &font_style, nullptr); - minikin::FontStyle style(font_style.weight(), IsItalic(font_style.slant())); - - fonts.push_back(minikin::Font(std::move(typeface), style)); - } - - auto minikin_family = std::make_shared(std::move(fonts)); - return std::make_shared(std::move(minikin_family)); -} - -} // namespace txt diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index e4a35e7ed4..4fe7d785e7 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -26,7 +26,7 @@ #include "lib/ftl/logging.h" -#include "lib/txt/src/font_provider.h" +#include "lib/txt/src/font_collection.h" #include "lib/txt/src/font_skia.h" #include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkPaint.h" @@ -114,7 +114,8 @@ void Paragraph::SetText(std::vector text, StyledRuns runs) { } void Paragraph::AddRunsToLineBreaker() { - auto collection = FontProvider::GetDefault().GetFontCollectionForFamily(""); + auto collection = FontCollection::GetDefaultFontCollection() + .GetMinikinFontCollectionForFamily(""); minikin::FontStyle font; minikin::MinikinPaint paint; for (size_t i = 0; i < runs_.size(); ++i) { @@ -124,8 +125,8 @@ void Paragraph::AddRunsToLineBreaker() { } } -void Paragraph::Layout(double width) { - breaker_.setLineWidths(0.0f, 0, width); +void Paragraph::Layout(const ParagraphConstraints& constraints) { + breaker_.setLineWidths(0.0f, 0, constraints.width()); AddRunsToLineBreaker(); size_t breaks_count = breaker_.computeBreaks(); const int* breaks = breaker_.getBreaks(); @@ -138,7 +139,8 @@ void Paragraph::Layout(double width) { minikin::MinikinPaint minikin_paint; SkTextBlobBuilder builder; - auto collection = FontProvider::GetDefault().GetFontCollectionForFamily(""); + auto collection = FontCollection::GetDefaultFontCollection() + .GetMinikinFontCollectionForFamily(""); minikin::Layout layout; SkScalar x = 0.0f; SkScalar y = 0.0f; diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index 3b4bcb8757..4fd5d5aff2 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -19,11 +19,11 @@ #include -#include - #include "lib/ftl/macros.h" #include "lib/txt/src/paint_record.h" +#include "lib/txt/src/paragraph_constraints.h" #include "lib/txt/src/styled_runs.h" +#include "minikin/LineBreaker.h" #include "third_party/skia/include/core/SkTextBlob.h" class SkCanvas; @@ -36,7 +36,7 @@ class Paragraph { ~Paragraph(); - void Layout(double width); + void Layout(const ParagraphConstraints& constraints); void Paint(SkCanvas* canvas, double x, double y); diff --git a/engine/src/flutter/src/paragraph_builder.cc b/engine/src/flutter/src/paragraph_builder.cc index 342279cd32..a6db3492a8 100644 --- a/engine/src/flutter/src/paragraph_builder.cc +++ b/engine/src/flutter/src/paragraph_builder.cc @@ -42,8 +42,8 @@ void ParagraphBuilder::Pop() { runs_.StartRun(style_index, text_index); } -void ParagraphBuilder::AddText(const uint16_t* text, size_t length) { - text_.insert(text_.end(), text, text + length); +void ParagraphBuilder::AddText(const std::u16string& text) { + text_.insert(text_.end(), text.begin(), text.end()); } std::unique_ptr ParagraphBuilder::Build() { diff --git a/engine/src/flutter/src/paragraph_builder.h b/engine/src/flutter/src/paragraph_builder.h index 243e6394c8..8c6941af9b 100644 --- a/engine/src/flutter/src/paragraph_builder.h +++ b/engine/src/flutter/src/paragraph_builder.h @@ -38,7 +38,7 @@ class ParagraphBuilder { void Pop(); - void AddText(const uint16_t* text, size_t length); + void AddText(const std::u16string& text); std::unique_ptr Build(); diff --git a/engine/src/flutter/src/paragraph_constraints.cc b/engine/src/flutter/src/paragraph_constraints.cc new file mode 100644 index 0000000000..26a85e0463 --- /dev/null +++ b/engine/src/flutter/src/paragraph_constraints.cc @@ -0,0 +1,23 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lib/txt/src/paragraph_constraints.h" + +namespace txt { + +ParagraphConstraints::ParagraphConstraints(double width) : width_(width) {} + +} // namespace txt diff --git a/engine/src/flutter/src/paragraph_constraints.h b/engine/src/flutter/src/paragraph_constraints.h new file mode 100644 index 0000000000..e666451527 --- /dev/null +++ b/engine/src/flutter/src/paragraph_constraints.h @@ -0,0 +1,38 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIB_TXT_SRC_PARAGRAPH_CONSTRAINTS_H_ +#define LIB_TXT_SRC_PARAGRAPH_CONSTRAINTS_H_ + +#include "lib/ftl/macros.h" + +namespace txt { + +class ParagraphConstraints { + public: + explicit ParagraphConstraints(double width); + + double width() const { return width_; } + + private: + double width_; + + FTL_DISALLOW_COPY_AND_ASSIGN(ParagraphConstraints); +}; + +} // namespace txt + +#endif // LIB_TXT_SRC_PARAGRAPH_CONSTRAINTS_H_ diff --git a/engine/src/flutter/src/paragraph_style.cc b/engine/src/flutter/src/paragraph_style.cc new file mode 100644 index 0000000000..a55c3a37b2 --- /dev/null +++ b/engine/src/flutter/src/paragraph_style.cc @@ -0,0 +1,17 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lib/txt/src/paragraph_style.h" diff --git a/engine/src/flutter/src/text_style.cc b/engine/src/flutter/src/text_style.cc new file mode 100644 index 0000000000..d7d0ab7a2d --- /dev/null +++ b/engine/src/flutter/src/text_style.cc @@ -0,0 +1,17 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lib/txt/src/text_style.h" diff --git a/engine/src/flutter/tests/txt/BUILD.gn b/engine/src/flutter/tests/txt/BUILD.gn new file mode 100644 index 0000000000..93e97a1c86 --- /dev/null +++ b/engine/src/flutter/tests/txt/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright 2017 Google, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +executable("txt") { + output_name = "txt_unittests" + + include_dirs = [ + "//lib/txt/src", + "//lib/txt/include", + "//third_party/icu/source/common", + ] + + sources = [ + "font_collection_unittests.cc", + "paragraph_unittests.cc", + "render_test.cc", + "txt_run_all_unittests.cc", + ] + + deps = [ + "//dart/runtime:libdart_jit", # Logging + "//flutter/fml", + "//lib/txt/shims", + "//lib/txt/src", + "//third_party/gtest", + "//third_party/harfbuzz", + "//third_party/skia", + ] +} diff --git a/engine/src/flutter/tests/txt/font_collection_unittests.cc b/engine/src/flutter/tests/txt/font_collection_unittests.cc new file mode 100644 index 0000000000..9eaf955daa --- /dev/null +++ b/engine/src/flutter/tests/txt/font_collection_unittests.cc @@ -0,0 +1,25 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "font_collection.h" +#include "gtest/gtest.h" + +TEST(FontCollection, HasDefaultRegistrations) { + auto collection = txt::FontCollection::GetDefaultFontCollection() + .GetMinikinFontCollectionForFamily(""); + + ASSERT_NE(collection.get(), nullptr); +} diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc new file mode 100644 index 0000000000..9ea2d3d306 --- /dev/null +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -0,0 +1,47 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#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" + +TEST_F(RenderTest, SimpleParagraph) { + const char* text = "Hello World"; + 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_ColorBLACK; + builder.PushStyle(text_style); + + builder.AddText(u16_text); + + builder.Pop(); + + auto paragraph = builder.Build(); + + paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()}); + + paragraph->Paint(GetCanvas(), 10.0, 10.0); + + ASSERT_TRUE(Snapshot()); +} diff --git a/engine/src/flutter/tests/txt/render_test.cc b/engine/src/flutter/tests/txt/render_test.cc new file mode 100644 index 0000000000..68b45e98ac --- /dev/null +++ b/engine/src/flutter/tests/txt/render_test.cc @@ -0,0 +1,67 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "render_test.h" +#include "third_party/skia/include/core/SkImageEncoder.h" +#include "third_party/skia/include/core/SkStream.h" + +RenderTest::RenderTest() : snapshots_(0) {} + +RenderTest::~RenderTest() {} + +SkCanvas* RenderTest::GetCanvas() { + return canvas_ == nullptr ? nullptr : canvas_.get(); +} + +std::string RenderTest::GetNextSnapshotName() { + const auto& test_info = + ::testing::UnitTest::GetInstance()->current_test_info(); + + std::stringstream stream; + stream << test_info->test_case_name() << "_" << test_info->name(); + stream << "_" << ++snapshots_ << ".png"; + + return stream.str(); +} + +bool RenderTest::Snapshot() { + if (!canvas_ || !bitmap_) { + return false; + } + auto file_name = GetNextSnapshotName(); + SkFILEWStream file(file_name.c_str()); + return SkEncodeImage(&file, *bitmap_, SkEncodedImageFormat::kPNG, 100); +} + +size_t RenderTest::GetTestCanvasWidth() const { + return 800; +} + +size_t RenderTest::GetTestCanvasHeight() const { + return 600; +} + +void RenderTest::SetUp() { + bitmap_ = std::make_unique(); + bitmap_->allocN32Pixels(GetTestCanvasWidth(), GetTestCanvasHeight()); + canvas_ = std::make_unique(*bitmap_); + canvas_->clear(SK_ColorTRANSPARENT); +} + +void RenderTest::TearDown() { + canvas_ = nullptr; + bitmap_ = nullptr; +} diff --git a/engine/src/flutter/tests/txt/render_test.h b/engine/src/flutter/tests/txt/render_test.h new file mode 100644 index 0000000000..ef32311156 --- /dev/null +++ b/engine/src/flutter/tests/txt/render_test.h @@ -0,0 +1,52 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "gtest/gtest.h" +#include "lib/ftl/macros.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "third_party/skia/include/core/SkCanvas.h" + +class RenderTest : public ::testing::Test { + public: + RenderTest(); + + ~RenderTest(); + + SkCanvas* GetCanvas(); + + bool Snapshot(); + + size_t GetTestCanvasWidth() const; + + size_t GetTestCanvasHeight() const; + + protected: + void SetUp() override; + + void TearDown() override; + + private: + size_t snapshots_; + std::unique_ptr bitmap_; + std::unique_ptr canvas_; + + std::string GetNextSnapshotName(); + + FTL_DISALLOW_COPY_AND_ASSIGN(RenderTest); +}; diff --git a/engine/src/flutter/tests/txt/txt_run_all_unittests.cc b/engine/src/flutter/tests/txt/txt_run_all_unittests.cc new file mode 100644 index 0000000000..a105847fea --- /dev/null +++ b/engine/src/flutter/tests/txt/txt_run_all_unittests.cc @@ -0,0 +1,26 @@ +/* + * Copyright 2017 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flutter/fml/icu_util.h" +#include "gtest/gtest.h" +#include "third_party/skia/include/core/SkGraphics.h" + +int main(int argc, char** argv) { + fml::icu::InitializeICU(); + SkGraphics::Init(); + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/engine/src/flutter/tests/unittest/BUILD.gn b/engine/src/flutter/tests/unittest/BUILD.gn index 5f747f7e59..3355b6880e 100644 --- a/engine/src/flutter/tests/unittest/BUILD.gn +++ b/engine/src/flutter/tests/unittest/BUILD.gn @@ -3,38 +3,39 @@ # found in the LICENSE file. executable("unittest") { - output_name = "txt_unittests" + output_name = "minikin_unittests" testonly = true sources = [ + # "WordBreakerTests.cpp", + "//lib/ftl/test/run_all_unittests.cc", "CmapCoverageTest.cpp", "EmojiTest.cpp", + # TODO(abarth): Re-enable once we have wired up SkFontMgr. # "FontCollectionItemizeTest.cpp", # "FontCollectionTest.cpp", # "FontFamilyTest.cpp", # "FontLanguageListCacheTest.cpp", "GraphemeBreakTests.cpp", + # "HbFontCacheTest.cpp", # "HyphenatorTest.cpp", "ICUTestBase.h", + # "LayoutTest.cpp", "LayoutUtilsTest.cpp", "MeasurementTests.cpp", "SparseBitSetTest.cpp", "UnicodeUtilsTest.cpp", - # "WordBreakerTests.cpp", - "//lib/ftl/test/run_all_unittests.cc", ] - defines = [ - "kTestFontDir=\"/data/nativetest/minikin_tests/data/\"", - ] + defines = [ "kTestFontDir=\"/data/nativetest/minikin_tests/data/\"" ] deps = [ - "//lib/txt/tests/util", "//lib/txt/libs/minikin", + "//lib/txt/tests/util", "//third_party/gtest", ] }