diff --git a/engine/src/flutter/.clang-format b/engine/src/flutter/.clang-format new file mode 100644 index 0000000000..6fdf1dc888 --- /dev/null +++ b/engine/src/flutter/.clang-format @@ -0,0 +1,8 @@ +# Defines the Chromium style for automatic reformatting. +# http://clang.llvm.org/docs/ClangFormatStyleOptions.html +BasedOnStyle: Chromium +# This defaults to 'Auto'. Explicitly set it for a while, so that +# 'vector >' in existing files gets formatted to +# 'vector>'. ('Auto' means that clang-format will only use +# 'int>>' if the file already contains at least one such instance.) +Standard: Cpp11 diff --git a/engine/src/flutter/.gitattributes b/engine/src/flutter/.gitattributes new file mode 100644 index 0000000000..da2e8a59e5 --- /dev/null +++ b/engine/src/flutter/.gitattributes @@ -0,0 +1,10 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Always perform LF normalization on these files +*.c text +*.cc text +*.cpp text +*.h text +*.gn text +*.md text diff --git a/engine/src/flutter/.gitignore b/engine/src/flutter/.gitignore new file mode 100644 index 0000000000..59ec4abf16 --- /dev/null +++ b/engine/src/flutter/.gitignore @@ -0,0 +1,17 @@ +*.pyc +*~ +.*.sw? +.DS_Store +.classpath +.cproject +.gdb_history +.gdbinit +.landmines +.project +.pydevproject +.checkstyle +.vscode +cscope.* +Session.vim +tags +Thumbs.db diff --git a/engine/src/flutter/src/BUILD.gn b/engine/src/flutter/src/BUILD.gn index e10013e470..44b6cfa1f8 100644 --- a/engine/src/flutter/src/BUILD.gn +++ b/engine/src/flutter/src/BUILD.gn @@ -14,13 +14,22 @@ static_library("src") { sources = [ - "font_collection.cc", - "font_collection.h", + "font_provider.cc", + "font_provider.h", "font_skia.cc", "font_skia.h", + "font_style.h", + "font_weight.h", + "paragraph_builder.cc", + "paragraph_builder.h", + "paragraph_style.h", + "styled_runs.h", + "text_align.h", + "text_style.h", ] deps = [ + "//lib/ftl", "//lib/txt/libs/minikin", "//third_party/skia", ] diff --git a/engine/src/flutter/src/font_collection.cc b/engine/src/flutter/src/font_provider.cc similarity index 72% rename from engine/src/flutter/src/font_collection.cc rename to engine/src/flutter/src/font_provider.cc index d94df028fa..a8a3c8fe9c 100644 --- a/engine/src/flutter/src/font_collection.cc +++ b/engine/src/flutter/src/font_provider.cc @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "lib/txt/src/font_collection.h" +#include "lib/txt/src/font_provider.h" #include @@ -31,28 +31,28 @@ bool IsItalic(SkFontStyle::Slant slant) { } // namespace -FontCollection& FontCollection::GetDefaultFontCollection() { - static FontCollection* collection = nullptr; +FontProvider& FontProvider::GetDefault() { + static FontProvider* provider = nullptr; static std::once_flag once; - std::call_once(once, []() { collection = new FontCollection(); }); - return *collection; + std::call_once(once, []() { provider = new FontProvider(); }); + return *provider; } -FontCollection::FontCollection() = default; +FontProvider::FontProvider() = default; -FontCollection::~FontCollection() = default; +FontProvider::~FontProvider() = default; std::shared_ptr -FontCollection::GetAndroidFontCollectionForFamily(const std::string& family) { +FontProvider::GetFontCollectionForFamily(const std::string& family) { // Get the Skia font manager. - auto skia_font_manager = SkFontMgr::RefDefault(); - FTL_DCHECK(skia_font_manager != nullptr); + 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 = skia_font_manager->matchFamily( + auto font_style_set = font_manager->matchFamily( family.length() == 0 ? "Helvetica" : family.c_str()); FTL_DCHECK(font_style_set != nullptr); @@ -64,9 +64,9 @@ FontCollection::GetAndroidFontCollectionForFamily(const std::string& family) { sk_ref_sp(font_style_set->createTypeface(i)); auto typeface = std::make_shared(std::move(skia_typeface)); - SkFontStyle skia_font_style; - font_style_set->getStyle(i, &skia_font_style, nullptr); - minikin::FontStyle style(skia_font_style.weight(), IsItalic(skia_font_style.slant())); + 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)); } diff --git a/engine/src/flutter/src/font_collection.h b/engine/src/flutter/src/font_provider.h similarity index 81% rename from engine/src/flutter/src/font_collection.h rename to engine/src/flutter/src/font_provider.h index d4df45b0e9..264ee149fc 100644 --- a/engine/src/flutter/src/font_collection.h +++ b/engine/src/flutter/src/font_provider.h @@ -27,20 +27,20 @@ namespace txt { -class FontCollection { +class FontProvider { public: - static FontCollection& GetDefaultFontCollection(); + static FontProvider& GetDefault(); - FontCollection(); + FontProvider(); - ~FontCollection(); + ~FontProvider(); - std::shared_ptr GetAndroidFontCollectionForFamily( + std::shared_ptr GetFontCollectionForFamily( const std::string& family); private: // TODO(chinmaygarde): Caches go here. - FTL_DISALLOW_COPY_AND_ASSIGN(FontCollection); + FTL_DISALLOW_COPY_AND_ASSIGN(FontProvider); }; } // namespace txt diff --git a/engine/src/flutter/src/font_style.h b/engine/src/flutter/src/font_style.h new file mode 100644 index 0000000000..ee903dbea0 --- /dev/null +++ b/engine/src/flutter/src/font_style.h @@ -0,0 +1,29 @@ +/* + * 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_FONT_STYLE_H_ +#define LIB_TXT_SRC_FONT_STYLE_H_ + +namespace txt { + +enum class FontStyle { + normal, + italic, +}; + +} // namespace txt + +#endif // LIB_TXT_SRC_FONT_STYLE_H_ diff --git a/engine/src/flutter/src/font_weight.h b/engine/src/flutter/src/font_weight.h new file mode 100644 index 0000000000..cd1811e14d --- /dev/null +++ b/engine/src/flutter/src/font_weight.h @@ -0,0 +1,36 @@ +/* + * 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_FONT_WEIGHT_H_ +#define LIB_TXT_SRC_FONT_WEIGHT_H_ + +namespace txt { + +enum class FontWeight { + w100, + w200, + w300, + w400, + w500, + w600, + w700, + w800, + w900, +}; + +} // namespace txt + +#endif // LIB_TXT_SRC_FONT_WEIGHT_H_ diff --git a/engine/src/flutter/src/paragraph_builder.cc b/engine/src/flutter/src/paragraph_builder.cc new file mode 100644 index 0000000000..3fd1738d9e --- /dev/null +++ b/engine/src/flutter/src/paragraph_builder.cc @@ -0,0 +1,49 @@ +/* + * 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_builder.h" + +namespace txt { + +ParagraphBuilder::ParagraphBuilder(ParagraphStyle style) {} + +ParagraphBuilder::~ParagraphBuilder() = default; + +void ParagraphBuilder::PushStyle(const TextStyle& style) { + const size_t text_index = text_.size(); + runs_.EndRunIfNeeded(text_index); + const size_t style_index = runs_.AddStyle(style); + runs_.StartRun(style_index, text_index); + style_stack_.push_back(style_index); +} + +void ParagraphBuilder::Pop() { + if (style_stack_.empty()) + return; + const size_t text_index = text_.size(); + runs_.EndRunIfNeeded(text_index); + style_stack_.pop_back(); + if (style_stack_.empty()) + return; + const size_t style_index = style_stack_.back(); + runs_.StartRun(style_index, text_index); +} + +void ParagraphBuilder::AddText(const uint16_t* text, size_t length) { + text_.insert(text_.end(), text, text + length); +} + +} // namespace txt diff --git a/engine/src/flutter/src/paragraph_builder.h b/engine/src/flutter/src/paragraph_builder.h new file mode 100644 index 0000000000..cb313b01f0 --- /dev/null +++ b/engine/src/flutter/src/paragraph_builder.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. + */ + +#ifndef LIB_TXT_SRC_PARAGRAPH_BUILDER_H_ +#define LIB_TXT_SRC_PARAGRAPH_BUILDER_H_ + +#include +#include + +#include "lib/ftl/macros.h" +#include "lib/txt/src/paragraph_style.h" +#include "lib/txt/src/styled_runs.h" +#include "lib/txt/src/text_style.h" + +namespace txt { + +class ParagraphBuilder { + public: + explicit ParagraphBuilder(ParagraphStyle style); + + ~ParagraphBuilder(); + + void PushStyle(const TextStyle& style); + + void Pop(); + + void AddText(const uint16_t* text, size_t length); + + private: + std::vector text_; + std::vector style_stack_; + StyledRuns runs_; + + FTL_DISALLOW_COPY_AND_ASSIGN(ParagraphBuilder); +}; + +} // namespace txt + +#endif // LIB_TXT_SRC_PARAGRAPH_BUILDER_H_ diff --git a/engine/src/flutter/src/paragraph_style.h b/engine/src/flutter/src/paragraph_style.h new file mode 100644 index 0000000000..eec58159da --- /dev/null +++ b/engine/src/flutter/src/paragraph_style.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_STYLE_H_ +#define LIB_TXT_SRC_PARAGRAPH_STYLE_H_ + +#include + +#include "lib/txt/src/font_style.h" +#include "lib/txt/src/font_weight.h" +#include "lib/txt/src/text_align.h" + +namespace txt { + +class ParagraphStyle { + public: + TextAlign text_align = TextAlign::left; + int max_lines = 0; + double line_height = 1.0; + std::string ellipsis; +}; + +} // namespace txt + +#endif // LIB_TXT_SRC_PARAGRAPH_STYLE_H_ diff --git a/engine/src/flutter/src/styled_runs.h b/engine/src/flutter/src/styled_runs.h new file mode 100644 index 0000000000..781e1eb69b --- /dev/null +++ b/engine/src/flutter/src/styled_runs.h @@ -0,0 +1,69 @@ +/* + * 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_STYLED_RUNS_H_ +#define LIB_TXT_SRC_STYLED_RUNS_H_ + +#include + +#include "lib/txt/src/text_style.h" + +namespace txt { + +class StyledRuns { + public: + struct Run { + const TextStyle& style; + size_t start; + size_t end; + }; + + StyledRuns(); + + ~StyledRuns(); + + StyledRuns(const StyledRuns& other) = delete; + + StyledRuns(StyledRuns&& other); + + const StyledRuns& operator=(StyledRuns&& other); + + void swap(StyledRuns& other); + + size_t AddStyle(const TextStyle& style); + + void StartRun(size_t style_index, size_t start); + + void EndRunIfNeeded(size_t end); + + size_t size() const { return runs_.size(); } + + Run GetRun(size_t index) const; + + private: + struct IndexedRun { + size_t style_index = 0; + size_t start = 0; + size_t end = 0; + }; + + std::vector styles_; + std::vector runs_; +}; + +} // namespace txt + +#endif // LIB_TXT_SRC_STYLED_RUNS_H_ diff --git a/engine/src/flutter/src/text_align.h b/engine/src/flutter/src/text_align.h new file mode 100644 index 0000000000..91a291ff9f --- /dev/null +++ b/engine/src/flutter/src/text_align.h @@ -0,0 +1,31 @@ +/* + * 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_TEXT_ALIGN_H_ +#define LIB_TXT_SRC_TEXT_ALIGN_H_ + +namespace txt { + +enum class TextAlign { + left, + right, + center, + // justify, +}; + +} // namespace txt + +#endif // LIB_TXT_SRC_TEXT_ALIGN_H_ diff --git a/engine/src/flutter/src/text_style.h b/engine/src/flutter/src/text_style.h new file mode 100644 index 0000000000..f2482dd406 --- /dev/null +++ b/engine/src/flutter/src/text_style.h @@ -0,0 +1,46 @@ +/* + * 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_TEXT_STYLE_H_ +#define LIB_TXT_SRC_TEXT_STYLE_H_ + +#include + +#include "lib/txt/src/font_style.h" +#include "lib/txt/src/font_weight.h" +#include "third_party/skia/include/core/SkColor.h" + +namespace txt { + +class TextStyle { + public: + SkColor color = SK_ColorWHITE; + // TextDecoration decoration, + // SkColor decoration_color; + // TextDecorationStyle decoration_style + FontWeight font_weight = FontWeight::w400; + FontStyle font_style = FontStyle::normal; + // TextBaseline text_baseline; + std::string font_family; + double font_size = 14.0; + double letter_spacing = 0.0; + double word_spacing = 0.0; + double height = 1.0; +}; + +} // namespace txt + +#endif // LIB_TXT_SRC_TEXT_STYLE_H_