mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Major efficiency improvements, new benches, and API change to allow changing font collections.
Change-Id: Id33cdcd161d659310d28dd36f415dc01da2e03a7
This commit is contained in:
parent
7e02287cf9
commit
4ddb034139
@ -23,6 +23,7 @@ executable("benchmarks") {
|
||||
|
||||
sources = [
|
||||
"font_collection_benchmarks.cc",
|
||||
"paint_record_benchmarks.cc",
|
||||
"paragraph_benchmarks.cc",
|
||||
"paragraph_builder_benchmarks.cc",
|
||||
"styled_runs_benchmarks.cc",
|
||||
|
||||
@ -19,6 +19,8 @@
|
||||
#include "lib/ftl/command_line.h"
|
||||
#include "lib/ftl/logging.h"
|
||||
#include "lib/txt/src/font_collection.h"
|
||||
#include "third_party/skia/include/ports/SkFontMgr.h"
|
||||
#include "third_party/skia/include/ports/SkFontMgr_directory.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace txt {
|
||||
@ -32,18 +34,33 @@ static void BM_FAKE_BENCHMARK(benchmark::State& state) {
|
||||
}
|
||||
BENCHMARK(BM_FAKE_BENCHMARK);
|
||||
|
||||
static void BM_FontCollectionCustomInit(benchmark::State& state) {
|
||||
while (state.KeepRunning()) {
|
||||
benchmark::DoNotOptimize(
|
||||
FontCollection::GetFontCollection(txt::GetFontDir()));
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_FontCollectionCustomInit);
|
||||
|
||||
static void BM_FontCollectionInit(benchmark::State& state) {
|
||||
while (state.KeepRunning()) {
|
||||
FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
benchmark::DoNotOptimize(FontCollection::GetFontCollection());
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_FontCollectionInit);
|
||||
|
||||
static void BM_FontCollectionSkFontMgr(benchmark::State& state) {
|
||||
while (state.KeepRunning()) {
|
||||
auto mgr = SkFontMgr_New_Custom_Directory(txt::GetFontDir().c_str());
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_FontCollectionSkFontMgr);
|
||||
|
||||
static void BM_FontCollectionGetMinikinFontCollectionForFamily(
|
||||
benchmark::State& state) {
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
FontCollection::GetFontCollection(txt::GetFontDir())
|
||||
.GetMinikinFontCollectionForFamily("Roboto");
|
||||
font_collection.GetMinikinFontCollectionForFamily("Roboto");
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_FontCollectionGetMinikinFontCollectionForFamily);
|
||||
|
||||
46
engine/src/flutter/benchmarks/paint_record_benchmarks.cc
Normal file
46
engine/src/flutter/benchmarks/paint_record_benchmarks.cc
Normal 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.
|
||||
*/
|
||||
|
||||
#include "third_party/benchmark/include/benchmark/benchmark_api.h"
|
||||
|
||||
#include "lib/ftl/command_line.h"
|
||||
#include "lib/ftl/logging.h"
|
||||
#include "lib/txt/src/paint_record.h"
|
||||
#include "lib/txt/src/text_style.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace txt {
|
||||
|
||||
static void BM_PaintRecordInit(benchmark::State& state) {
|
||||
TextStyle style;
|
||||
|
||||
SkPaint paint;
|
||||
paint.setAntiAlias(true);
|
||||
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
||||
paint.setTextSize(14);
|
||||
paint.setFakeBoldText(false);
|
||||
|
||||
SkTextBlobBuilder builder;
|
||||
builder.allocRunPos(paint, 100);
|
||||
auto text_blob = builder.make();
|
||||
|
||||
while (state.KeepRunning()) {
|
||||
PaintRecord PaintRecord(style, text_blob, SkPaint::FontMetrics(), 0);
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_PaintRecordInit);
|
||||
|
||||
} // namespace txt
|
||||
@ -43,15 +43,16 @@ static void BM_ParagraphShortLayout(benchmark::State& state) {
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorBLACK;
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
builder.PushStyle(text_style);
|
||||
builder.AddText(u16_text);
|
||||
builder.Pop();
|
||||
auto paragraph = builder.Build();
|
||||
|
||||
paragraph->Layout(300, txt::GetFontDir(), true);
|
||||
paragraph->Layout(300, true);
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_ParagraphShortLayout);
|
||||
@ -83,15 +84,16 @@ static void BM_ParagraphLongLayout(benchmark::State& state) {
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorBLACK;
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
builder.PushStyle(text_style);
|
||||
builder.AddText(u16_text);
|
||||
builder.Pop();
|
||||
auto paragraph = builder.Build();
|
||||
|
||||
paragraph->Layout(300, txt::GetFontDir(), true);
|
||||
paragraph->Layout(300, true);
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_ParagraphLongLayout);
|
||||
@ -106,14 +108,15 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) {
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorBLACK;
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
builder.PushStyle(text_style);
|
||||
builder.AddText(u16_text);
|
||||
}
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(300, txt::GetFontDir(), true);
|
||||
paragraph->Layout(300, true);
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_ParagraphManyStylesLayout);
|
||||
@ -128,15 +131,16 @@ static void BM_ParagraphTextBigO(benchmark::State& state) {
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorBLACK;
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
builder.PushStyle(text_style);
|
||||
builder.AddText(u16_text);
|
||||
builder.Pop();
|
||||
auto paragraph = builder.Build();
|
||||
|
||||
paragraph->Layout(300, txt::GetFontDir(), true);
|
||||
paragraph->Layout(300, true);
|
||||
}
|
||||
state.SetComplexityN(state.range(0));
|
||||
}
|
||||
@ -155,15 +159,16 @@ static void BM_ParagraphStylesBigO(benchmark::State& state) {
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorBLACK;
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
for (int i = 0; i < state.range(0); ++i) {
|
||||
builder.PushStyle(text_style);
|
||||
builder.AddText(u16_text);
|
||||
}
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(300, txt::GetFontDir(), true);
|
||||
paragraph->Layout(300, true);
|
||||
}
|
||||
state.SetComplexityN(state.range(0));
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
#include "third_party/benchmark/include/benchmark/benchmark_api.h"
|
||||
|
||||
#include "lib/ftl/logging.h"
|
||||
#include "lib/txt/src/font_collection.h"
|
||||
#include "lib/txt/src/font_style.h"
|
||||
#include "lib/txt/src/font_weight.h"
|
||||
#include "lib/txt/src/paragraph.h"
|
||||
@ -24,6 +25,7 @@
|
||||
#include "lib/txt/src/text_align.h"
|
||||
#include "third_party/icu/source/common/unicode/unistr.h"
|
||||
#include "third_party/skia/include/core/SkColor.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace txt {
|
||||
|
||||
@ -40,8 +42,9 @@ static void BM_ParagraphBuilderPushStyle(benchmark::State& state) {
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorBLACK;
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
builder.PushStyle(text_style);
|
||||
}
|
||||
}
|
||||
@ -76,9 +79,9 @@ static void BM_ParagraphBuilderAddTextChar(benchmark::State& state) {
|
||||
const char* text = "Hello World";
|
||||
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
builder.AddText(text);
|
||||
}
|
||||
}
|
||||
@ -91,9 +94,9 @@ static void BM_ParagraphBuilderAddTextU16stringShort(benchmark::State& state) {
|
||||
icu_text.getBuffer() + icu_text.length());
|
||||
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
builder.AddText(u16_text);
|
||||
}
|
||||
}
|
||||
@ -142,9 +145,9 @@ static void BM_ParagraphBuilderShortParagraphConstruct(
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorBLACK;
|
||||
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
builder.PushStyle(text_style);
|
||||
builder.AddText(u16_text);
|
||||
builder.Pop();
|
||||
@ -180,9 +183,9 @@ static void BM_ParagraphBuilderLongParagraphConstruct(benchmark::State& state) {
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorBLACK;
|
||||
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
while (state.KeepRunning()) {
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
builder.PushStyle(text_style);
|
||||
builder.AddText(u16_text);
|
||||
builder.Pop();
|
||||
|
||||
@ -28,11 +28,13 @@
|
||||
|
||||
namespace txt {
|
||||
|
||||
// Will be deprecated when full compatibility with Flutter Engine is complete.
|
||||
FontCollection& FontCollection::GetFontCollection(std::string dir) {
|
||||
std::vector<std::string> dirs = {dir};
|
||||
return GetFontCollection(std::move(dirs));
|
||||
}
|
||||
|
||||
// Will be deprecated when full compatibility with Flutter Engine is complete.
|
||||
FontCollection& FontCollection::GetFontCollection(
|
||||
std::vector<std::string> dirs) {
|
||||
static FontCollection* collection = nullptr;
|
||||
@ -41,10 +43,20 @@ FontCollection& FontCollection::GetFontCollection(
|
||||
return *collection;
|
||||
}
|
||||
|
||||
// Will be deprecated when full compatibility with Flutter Engine is complete.
|
||||
FontCollection& FontCollection::GetDefaultFontCollection() {
|
||||
return GetFontCollection("");
|
||||
}
|
||||
|
||||
FontCollection::FontCollection() {
|
||||
FontCollection("");
|
||||
}
|
||||
|
||||
FontCollection::FontCollection(std::string dir) {
|
||||
std::vector<std::string> dirs = {dir};
|
||||
FontCollection(std::move(dirs));
|
||||
}
|
||||
|
||||
FontCollection::FontCollection(const std::vector<std::string>& dirs) {
|
||||
#ifdef DIRECTORY_FONT_MANAGER_AVAILABLE
|
||||
for (std::string dir : dirs) {
|
||||
|
||||
@ -35,15 +35,26 @@ namespace txt {
|
||||
|
||||
class FontCollection {
|
||||
public:
|
||||
// Will be deprecated when full compatibility with Flutter Engine is complete.
|
||||
static FontCollection& GetDefaultFontCollection();
|
||||
|
||||
// Will be deprecated when full compatibility with Flutter Engine is complete.
|
||||
static FontCollection& GetFontCollection(std::string dir = "");
|
||||
|
||||
// Will be deprecated when full compatibility with Flutter Engine is complete.
|
||||
static FontCollection& GetFontCollection(std::vector<std::string> dirs);
|
||||
|
||||
std::shared_ptr<minikin::FontCollection> GetMinikinFontCollectionForFamily(
|
||||
const std::string& family);
|
||||
|
||||
FontCollection(const std::vector<std::string>& dirs);
|
||||
|
||||
FontCollection(std::string dir);
|
||||
|
||||
FontCollection();
|
||||
|
||||
~FontCollection();
|
||||
|
||||
// Provides a set of all available family names.
|
||||
std::set<std::string> GetFamilyNames();
|
||||
|
||||
@ -62,12 +73,7 @@ class FontCollection {
|
||||
return DEFAULT_FAMILY_NAME;
|
||||
};
|
||||
|
||||
FontCollection(const std::vector<std::string>& dirs);
|
||||
|
||||
~FontCollection();
|
||||
|
||||
// TODO(chinmaygarde): Caches go here.
|
||||
FTL_DISALLOW_COPY_AND_ASSIGN(FontCollection);
|
||||
};
|
||||
|
||||
} // namespace txt
|
||||
|
||||
@ -125,24 +125,25 @@ void Paragraph::SetText(std::vector<uint16_t> text, StyledRuns runs) {
|
||||
breaker_.setText();
|
||||
}
|
||||
|
||||
void Paragraph::AddRunsToLineBreaker(const std::string& rootdir) {
|
||||
void Paragraph::AddRunsToLineBreaker(
|
||||
std::shared_ptr<minikin::FontCollection>& collection,
|
||||
std::string& prev_font_family) {
|
||||
minikin::FontStyle font;
|
||||
minikin::MinikinPaint paint;
|
||||
for (size_t i = 0; i < runs_.size(); ++i) {
|
||||
auto run = runs_.GetRun(i);
|
||||
auto collection =
|
||||
FontCollection::GetFontCollection(rootdir)
|
||||
.GetMinikinFontCollectionForFamily(run.style.font_family);
|
||||
// Only obtain new font family if the font has changed between runs.
|
||||
if (run.style.font_family != prev_font_family || collection == nullptr) {
|
||||
collection = font_collection_->GetMinikinFontCollectionForFamily(
|
||||
run.style.font_family);
|
||||
}
|
||||
prev_font_family = run.style.font_family;
|
||||
GetFontAndMinikinPaint(run.style, &font, &paint);
|
||||
breaker_.addStyleRun(&paint, collection, font, run.start, run.end, false);
|
||||
}
|
||||
}
|
||||
|
||||
void Paragraph::Layout(double width,
|
||||
const std::string& rootdir,
|
||||
bool force,
|
||||
const double x_offset,
|
||||
const double y_offset) {
|
||||
void Paragraph::Layout(double width, bool force) {
|
||||
// Do not allow calling layout multiple times without changing anything.
|
||||
if (!needs_layout_ && !force)
|
||||
return;
|
||||
@ -150,8 +151,11 @@ void Paragraph::Layout(double width,
|
||||
|
||||
width_ = width;
|
||||
|
||||
std::shared_ptr<minikin::FontCollection> collection = nullptr;
|
||||
std::string prev_font_family = "";
|
||||
|
||||
breaker_.setLineWidths(0.0f, 0, width_);
|
||||
AddRunsToLineBreaker(rootdir);
|
||||
AddRunsToLineBreaker(collection, prev_font_family);
|
||||
breaker_.setJustified(paragraph_style_.text_align == TextAlign::justify);
|
||||
size_t breaks_count = breaker_.computeBreaks();
|
||||
const int* breaks = breaker_.getBreaks();
|
||||
@ -170,8 +174,8 @@ void Paragraph::Layout(double width,
|
||||
max_intrinsic_width_ = 0.0f;
|
||||
lines_ = 0;
|
||||
|
||||
SkScalar x = x_offset;
|
||||
SkScalar y = y_offset;
|
||||
SkScalar x = 0.0f;
|
||||
SkScalar y = 0.0f;
|
||||
size_t break_index = 0;
|
||||
double letter_spacing_offset = 0.0f;
|
||||
double max_line_spacing = 0.0f;
|
||||
@ -214,16 +218,13 @@ void Paragraph::Layout(double width,
|
||||
}
|
||||
x_queue.clear();
|
||||
};
|
||||
std::shared_ptr<minikin::FontCollection> collection = nullptr;
|
||||
std::string prev_font_family = "";
|
||||
for (size_t run_index = 0; run_index < runs_.size(); ++run_index) {
|
||||
auto run = runs_.GetRun(run_index);
|
||||
|
||||
// Only obtain new font family if the font has changed between runs.
|
||||
if (run.style.font_family != prev_font_family || collection == nullptr) {
|
||||
collection =
|
||||
FontCollection::GetFontCollection(rootdir)
|
||||
.GetMinikinFontCollectionForFamily(run.style.font_family);
|
||||
collection = font_collection_->GetMinikinFontCollectionForFamily(
|
||||
run.style.font_family);
|
||||
}
|
||||
prev_font_family = run.style.font_family;
|
||||
GetFontAndMinikinPaint(run.style, &font, &minikin_paint);
|
||||
@ -434,6 +435,10 @@ void Paragraph::SetParagraphStyle(const ParagraphStyle& style) {
|
||||
paragraph_style_ = style;
|
||||
}
|
||||
|
||||
void Paragraph::SetFontCollection(FontCollection* font_collection) {
|
||||
font_collection_ = font_collection;
|
||||
}
|
||||
|
||||
void Paragraph::Paint(SkCanvas* canvas, double x, double y) {
|
||||
for (const auto& record : records_) {
|
||||
SkPaint paint;
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "lib/ftl/macros.h"
|
||||
#include "lib/txt/src/font_collection.h"
|
||||
#include "lib/txt/src/paint_record.h"
|
||||
#include "lib/txt/src/paragraph_style.h"
|
||||
#include "lib/txt/src/styled_runs.h"
|
||||
@ -38,11 +39,7 @@ class Paragraph {
|
||||
|
||||
~Paragraph();
|
||||
|
||||
void Layout(double width,
|
||||
const std::string& rootdir = "",
|
||||
bool force = false,
|
||||
const double x_offset = 0.0,
|
||||
const double y_offset = 0.0);
|
||||
void Layout(double width, bool force = false);
|
||||
|
||||
void Paint(SkCanvas* canvas, double x, double y);
|
||||
|
||||
@ -81,6 +78,7 @@ class Paragraph {
|
||||
std::vector<PaintRecord> records_;
|
||||
std::vector<double> line_widths_;
|
||||
ParagraphStyle paragraph_style_;
|
||||
FontCollection* font_collection_;
|
||||
// TODO(garyq): Height of the paragraph after Layout().
|
||||
SkScalar height_ = 0.0f;
|
||||
double width_ = 0.0f;
|
||||
@ -95,7 +93,11 @@ class Paragraph {
|
||||
|
||||
void SetParagraphStyle(const ParagraphStyle& style);
|
||||
|
||||
void AddRunsToLineBreaker(const std::string& rootdir = "");
|
||||
void SetFontCollection(FontCollection* font_collection);
|
||||
|
||||
void AddRunsToLineBreaker(
|
||||
std::shared_ptr<minikin::FontCollection>& collection,
|
||||
std::string& prev_font_family);
|
||||
|
||||
void JustifyLine(std::vector<const SkTextBlobBuilder::RunBuffer*>& buffers,
|
||||
std::vector<size_t>& buffer_sizes,
|
||||
|
||||
@ -21,6 +21,10 @@
|
||||
|
||||
namespace txt {
|
||||
|
||||
ParagraphBuilder::ParagraphBuilder(ParagraphStyle style,
|
||||
FontCollection* font_collection)
|
||||
: paragraph_style_(style), font_collection_(font_collection) {}
|
||||
|
||||
ParagraphBuilder::ParagraphBuilder(ParagraphStyle style)
|
||||
: paragraph_style_(style) {}
|
||||
|
||||
@ -30,6 +34,10 @@ void ParagraphBuilder::SetParagraphStyle(const ParagraphStyle& style) {
|
||||
paragraph_style_ = style;
|
||||
}
|
||||
|
||||
void ParagraphBuilder::SetFontCollection(FontCollection* font_collection) {
|
||||
font_collection_ = font_collection;
|
||||
}
|
||||
|
||||
ParagraphBuilder::~ParagraphBuilder() = default;
|
||||
|
||||
void ParagraphBuilder::PushStyle(const TextStyle& style) {
|
||||
@ -71,10 +79,19 @@ void ParagraphBuilder::AddText(const char* text) {
|
||||
}
|
||||
|
||||
std::unique_ptr<Paragraph> ParagraphBuilder::Build() {
|
||||
if (font_collection_ == nullptr) {
|
||||
// Will be deprecated when full compatibility with Flutter Engine is
|
||||
// complete.
|
||||
FTL_LOG(WARNING) << "No font collection provided. Falling back to default "
|
||||
"fonts.";
|
||||
font_collection_ = &FontCollection::GetFontCollection("");
|
||||
}
|
||||
|
||||
runs_.EndRunIfNeeded(text_.size());
|
||||
std::unique_ptr<Paragraph> paragraph = std::make_unique<Paragraph>();
|
||||
paragraph->SetText(std::move(text_), std::move(runs_));
|
||||
paragraph->SetParagraphStyle(paragraph_style_);
|
||||
paragraph->SetFontCollection(font_collection_);
|
||||
return paragraph;
|
||||
}
|
||||
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "lib/ftl/macros.h"
|
||||
#include "lib/txt/src/font_collection.h"
|
||||
#include "lib/txt/src/paragraph.h"
|
||||
#include "lib/txt/src/paragraph_style.h"
|
||||
#include "lib/txt/src/styled_runs.h"
|
||||
@ -32,6 +33,8 @@ class ParagraphBuilder {
|
||||
public:
|
||||
explicit ParagraphBuilder(ParagraphStyle style);
|
||||
|
||||
ParagraphBuilder(ParagraphStyle style, FontCollection* font_collection);
|
||||
|
||||
ParagraphBuilder();
|
||||
|
||||
~ParagraphBuilder();
|
||||
@ -48,6 +51,8 @@ class ParagraphBuilder {
|
||||
|
||||
void SetParagraphStyle(const ParagraphStyle& style);
|
||||
|
||||
void SetFontCollection(FontCollection* font_collection);
|
||||
|
||||
std::unique_ptr<Paragraph> Build();
|
||||
|
||||
private:
|
||||
@ -55,6 +60,7 @@ class ParagraphBuilder {
|
||||
std::vector<size_t> style_stack_;
|
||||
StyledRuns runs_;
|
||||
ParagraphStyle paragraph_style_;
|
||||
FontCollection* font_collection_ = nullptr;
|
||||
|
||||
FTL_DISALLOW_COPY_AND_ASSIGN(ParagraphBuilder);
|
||||
};
|
||||
|
||||
@ -34,7 +34,8 @@ TEST_F(RenderTest, SimpleParagraph) {
|
||||
icu_text.getBuffer() + icu_text.length());
|
||||
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorBLACK;
|
||||
@ -44,7 +45,7 @@ TEST_F(RenderTest, SimpleParagraph) {
|
||||
builder.Pop();
|
||||
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
|
||||
paragraph->Layout(GetTestCanvasWidth());
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 15.0);
|
||||
|
||||
@ -66,7 +67,8 @@ TEST_F(RenderTest, SimpleRedParagraph) {
|
||||
icu_text.getBuffer() + icu_text.length());
|
||||
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorRED;
|
||||
@ -77,7 +79,7 @@ TEST_F(RenderTest, SimpleRedParagraph) {
|
||||
builder.Pop();
|
||||
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
|
||||
paragraph->Layout(GetTestCanvasWidth());
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 15.0);
|
||||
|
||||
@ -119,7 +121,8 @@ TEST_F(RenderTest, RainbowParagraph) {
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
paragraph_style.max_lines = 2;
|
||||
paragraph_style.text_align = TextAlign::left;
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
txt::TextStyle text_style1;
|
||||
text_style1.color = SK_ColorRED;
|
||||
@ -162,7 +165,7 @@ TEST_F(RenderTest, RainbowParagraph) {
|
||||
builder.Pop();
|
||||
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
|
||||
paragraph->Layout(GetTestCanvasWidth());
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 50.0);
|
||||
|
||||
@ -191,7 +194,8 @@ TEST_F(RenderTest, DefaultStyleParagraph) {
|
||||
icu_text.getBuffer() + icu_text.length());
|
||||
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorRED;
|
||||
@ -201,7 +205,7 @@ TEST_F(RenderTest, DefaultStyleParagraph) {
|
||||
builder.Pop();
|
||||
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
|
||||
paragraph->Layout(GetTestCanvasWidth());
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 15.0);
|
||||
|
||||
@ -221,7 +225,8 @@ TEST_F(RenderTest, BoldParagraph) {
|
||||
icu_text.getBuffer() + icu_text.length());
|
||||
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.font_size = 60;
|
||||
@ -236,7 +241,7 @@ TEST_F(RenderTest, BoldParagraph) {
|
||||
builder.Pop();
|
||||
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
|
||||
paragraph->Layout(GetTestCanvasWidth());
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 60.0);
|
||||
|
||||
@ -277,7 +282,8 @@ TEST_F(RenderTest, LeftAlignParagraph) {
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
paragraph_style.max_lines = 14;
|
||||
paragraph_style.text_align = TextAlign::left;
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.font_size = 26;
|
||||
@ -294,7 +300,7 @@ TEST_F(RenderTest, LeftAlignParagraph) {
|
||||
builder.Pop();
|
||||
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(GetTestCanvasWidth() - 100, txt::GetFontDir());
|
||||
paragraph->Layout(GetTestCanvasWidth() - 100);
|
||||
|
||||
paragraph->Paint(GetCanvas(), 0, 0);
|
||||
ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
|
||||
@ -363,7 +369,8 @@ TEST_F(RenderTest, RightAlignParagraph) {
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
paragraph_style.max_lines = 14;
|
||||
paragraph_style.text_align = TextAlign::right;
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.font_size = 26;
|
||||
@ -380,7 +387,7 @@ TEST_F(RenderTest, RightAlignParagraph) {
|
||||
builder.Pop();
|
||||
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(GetTestCanvasWidth() - 100, txt::GetFontDir());
|
||||
paragraph->Layout(GetTestCanvasWidth() - 100);
|
||||
|
||||
paragraph->Paint(GetCanvas(), 0, 0);
|
||||
ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
|
||||
@ -464,7 +471,8 @@ TEST_F(RenderTest, CenterAlignParagraph) {
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
paragraph_style.max_lines = 14;
|
||||
paragraph_style.text_align = TextAlign::center;
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.font_size = 26;
|
||||
@ -481,7 +489,7 @@ TEST_F(RenderTest, CenterAlignParagraph) {
|
||||
builder.Pop();
|
||||
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(GetTestCanvasWidth() - 100, txt::GetFontDir());
|
||||
paragraph->Layout(GetTestCanvasWidth() - 100);
|
||||
|
||||
paragraph->Paint(GetCanvas(), 0, 0);
|
||||
ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
|
||||
@ -569,7 +577,8 @@ TEST_F(RenderTest, JustifyAlignParagraph) {
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
paragraph_style.max_lines = 14;
|
||||
paragraph_style.text_align = TextAlign::justify;
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.font_size = 26;
|
||||
@ -586,7 +595,7 @@ TEST_F(RenderTest, JustifyAlignParagraph) {
|
||||
builder.Pop();
|
||||
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(GetTestCanvasWidth() - 100, txt::GetFontDir());
|
||||
paragraph->Layout(GetTestCanvasWidth() - 100);
|
||||
|
||||
paragraph->Paint(GetCanvas(), 0, 0);
|
||||
ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
|
||||
@ -636,7 +645,8 @@ TEST_F(RenderTest, ItalicsParagraph) {
|
||||
icu_text.getBuffer() + icu_text.length());
|
||||
|
||||
txt::ParagraphStyle paragraph_style;
|
||||
txt::ParagraphBuilder builder(paragraph_style);
|
||||
auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir());
|
||||
txt::ParagraphBuilder builder(paragraph_style, &font_collection);
|
||||
|
||||
txt::TextStyle text_style;
|
||||
text_style.color = SK_ColorRED;
|
||||
@ -649,7 +659,7 @@ TEST_F(RenderTest, ItalicsParagraph) {
|
||||
builder.Pop();
|
||||
|
||||
auto paragraph = builder.Build();
|
||||
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
|
||||
paragraph->Layout(GetTestCanvasWidth());
|
||||
|
||||
paragraph->Paint(GetCanvas(), 10.0, 35.0);
|
||||
|
||||
|
||||
@ -40,4 +40,4 @@ void SetCommandLine(ftl::CommandLine cmd) {
|
||||
gCommandLine = std::move(cmd);
|
||||
}
|
||||
|
||||
} // namespace txt
|
||||
} // namespace txt
|
||||
Loading…
x
Reference in New Issue
Block a user