Import more code from the prototype

Change-Id: Ic5656c3ffcc3c3da8ed1fb4a44355b16c21c2f1e
This commit is contained in:
Adam Barth
2017-05-11 09:12:22 -07:00
parent ef1a552f3c
commit 966ef0ace4
14 changed files with 416 additions and 22 deletions

View File

@@ -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<vector<int> >' in existing files gets formatted to
# 'vector<vector<int>>'. ('Auto' means that clang-format will only use
# 'int>>' if the file already contains at least one such instance.)
Standard: Cpp11

10
engine/src/flutter/.gitattributes vendored Normal file
View File

@@ -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

17
engine/src/flutter/.gitignore vendored Normal file
View File

@@ -0,0 +1,17 @@
*.pyc
*~
.*.sw?
.DS_Store
.classpath
.cproject
.gdb_history
.gdbinit
.landmines
.project
.pydevproject
.checkstyle
.vscode
cscope.*
Session.vim
tags
Thumbs.db

View File

@@ -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",
]

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "lib/txt/src/font_collection.h"
#include "lib/txt/src/font_provider.h"
#include <mutex>
@@ -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<minikin::FontCollection>
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<SkTypeface>(font_style_set->createTypeface(i));
auto typeface = std::make_shared<FontSkia>(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));
}

View File

@@ -27,20 +27,20 @@
namespace txt {
class FontCollection {
class FontProvider {
public:
static FontCollection& GetDefaultFontCollection();
static FontProvider& GetDefault();
FontCollection();
FontProvider();
~FontCollection();
~FontProvider();
std::shared_ptr<minikin::FontCollection> GetAndroidFontCollectionForFamily(
std::shared_ptr<minikin::FontCollection> 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

View File

@@ -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_

View File

@@ -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_

View File

@@ -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

View File

@@ -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 <memory>
#include <string>
#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<uint16_t> text_;
std::vector<size_t> style_stack_;
StyledRuns runs_;
FTL_DISALLOW_COPY_AND_ASSIGN(ParagraphBuilder);
};
} // namespace txt
#endif // LIB_TXT_SRC_PARAGRAPH_BUILDER_H_

View File

@@ -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 <string>
#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_

View File

@@ -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 <vector>
#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<TextStyle> styles_;
std::vector<IndexedRun> runs_;
};
} // namespace txt
#endif // LIB_TXT_SRC_STYLED_RUNS_H_

View File

@@ -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_

View File

@@ -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 <string>
#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_