Now supporting initial integration with Flutter.
Change-Id: Id71461bd96e08eae0e24ab35a040874d960af8e0
This commit is contained in:
@@ -21,6 +21,10 @@ config("txt_config") {
|
||||
}
|
||||
|
||||
static_library("txt") {
|
||||
if (current_toolchain == host_toolchain) {
|
||||
defines = [ "DIRECTORY_FONT_MANAGER_AVAILABLE" ]
|
||||
}
|
||||
|
||||
sources = [
|
||||
"src/font_collection.cc",
|
||||
"src/font_collection.h",
|
||||
@@ -52,8 +56,7 @@ static_library("txt") {
|
||||
|
||||
deps = [
|
||||
"//lib/txt/libs/minikin",
|
||||
"//third_party/skia",
|
||||
"//third_party/gtest",
|
||||
"//third_party/skia",
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
@@ -35,36 +35,46 @@ FontCollection& FontCollection::GetDefaultFontCollection() {
|
||||
return *collection;
|
||||
}
|
||||
|
||||
FontCollection::FontCollection() = default;
|
||||
FontCollection::FontCollection() {
|
||||
#ifdef DIRECTORY_FONT_MANAGER_AVAILABLE
|
||||
skia_font_manager_ = dir.length() != 0
|
||||
? SkFontMgr_New_Custom_Directory(dir.c_str())
|
||||
: SkFontMgr::RefDefault();
|
||||
#else
|
||||
skia_font_manager_ = SkFontMgr::RefDefault();
|
||||
#endif
|
||||
}
|
||||
|
||||
FontCollection::~FontCollection() = default;
|
||||
|
||||
std::set<std::string> FontCollection::GetFamilyNames(const std::string& dir) {
|
||||
auto skia_font_manager = dir.length() != 0
|
||||
? SkFontMgr_New_Custom_Directory(dir.c_str())
|
||||
: SkFontMgr::RefDefault();
|
||||
std::set<std::string> names;
|
||||
SkString str;
|
||||
for (int i = 0; i < skia_font_manager->countFamilies(); i++) {
|
||||
skia_font_manager->getFamilyName(i, &str);
|
||||
for (int i = 0; i < skia_font_manager_->countFamilies(); i++) {
|
||||
skia_font_manager_->getFamilyName(i, &str);
|
||||
names.insert(std::string{str.writable_str()});
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
const std::string FontCollection::ProcessFamilyName(const std::string& family) {
|
||||
#ifdef DIRECTORY_FONT_MANAGER_AVAILABLE
|
||||
return family.length() == 0 ? DEFAULT_FAMILY_NAME : family;
|
||||
#else
|
||||
if (family.length() == 0) {
|
||||
return *GetFamilyNames().begin();
|
||||
} else if (GetFamilyNames().count(family) > 0) { // Ensure family exists.
|
||||
return family;
|
||||
} else {
|
||||
return *GetFamilyNames().begin(); // First family available.
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
std::shared_ptr<minikin::FontCollection>
|
||||
FontCollection::GetMinikinFontCollectionForFamily(const std::string& family,
|
||||
const std::string& dir) {
|
||||
// Get the Skia font manager.
|
||||
auto skia_font_manager = dir.length() != 0
|
||||
? SkFontMgr_New_Custom_Directory(dir.c_str())
|
||||
: SkFontMgr::RefDefault();
|
||||
|
||||
FTL_DCHECK(skia_font_manager != nullptr);
|
||||
FTL_DCHECK(skia_font_manager_ != nullptr);
|
||||
|
||||
// Ask Skia to resolve a font style set for a font family name.
|
||||
// FIXME(chinmaygarde): The name "Coolvetica" is hardcoded because CoreText
|
||||
@@ -72,7 +82,7 @@ FontCollection::GetMinikinFontCollectionForFamily(const std::string& family,
|
||||
// SkFontMgr explicitly says passing in nullptr gives the default font.
|
||||
|
||||
auto font_style_set =
|
||||
skia_font_manager->matchFamily(ProcessFamilyName(family).c_str());
|
||||
skia_font_manager_->matchFamily(ProcessFamilyName(family).c_str());
|
||||
FTL_DCHECK(font_style_set != nullptr);
|
||||
|
||||
std::vector<minikin::Font> minikin_fonts;
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
#include "minikin/FontCollection.h"
|
||||
#include "minikin/FontFamily.h"
|
||||
#include "third_party/gtest/include/gtest/gtest_prod.h"
|
||||
#include "third_party/skia/include/core/SkRefCnt.h"
|
||||
#include "third_party/skia/include/ports/SkFontMgr.h"
|
||||
|
||||
namespace txt {
|
||||
|
||||
@@ -35,18 +37,16 @@ class FontCollection {
|
||||
public:
|
||||
static FontCollection& GetDefaultFontCollection();
|
||||
|
||||
FontCollection();
|
||||
|
||||
~FontCollection();
|
||||
|
||||
std::shared_ptr<minikin::FontCollection> GetMinikinFontCollectionForFamily(
|
||||
const std::string& family,
|
||||
const std::string& dir = "");
|
||||
|
||||
// Provides a vector of all available family names.
|
||||
static std::set<std::string> GetFamilyNames(const std::string& dir = "");
|
||||
// Provides a set of all available family names.
|
||||
std::set<std::string> GetFamilyNames(const std::string& dir = "");
|
||||
|
||||
private:
|
||||
sk_sp<SkFontMgr> skia_font_manager_;
|
||||
|
||||
FRIEND_TEST(FontCollection, HasDefaultRegistrations);
|
||||
FRIEND_TEST(FontCollection, GetMinikinFontCollections);
|
||||
FRIEND_TEST(FontCollection, GetFamilyNames);
|
||||
@@ -57,6 +57,10 @@ class FontCollection {
|
||||
return DEFAULT_FAMILY_NAME;
|
||||
};
|
||||
|
||||
FontCollection();
|
||||
|
||||
~FontCollection();
|
||||
|
||||
// TODO(chinmaygarde): Caches go here.
|
||||
FTL_DISALLOW_COPY_AND_ASSIGN(FontCollection);
|
||||
};
|
||||
|
||||
@@ -90,6 +90,7 @@ void GetFontAndMinikinPaint(const TextStyle& style,
|
||||
*font = minikin::FontStyle(GetWeight(style), GetItalic(style));
|
||||
paint->size = style.font_size;
|
||||
paint->letterSpacing = style.letter_spacing;
|
||||
paint->wordSpacing = style.word_spacing; // Likely not working yet.
|
||||
// TODO(abarth): word_spacing.
|
||||
}
|
||||
|
||||
@@ -128,8 +129,11 @@ void Paragraph::AddRunsToLineBreaker(const std::string& rootdir) {
|
||||
}
|
||||
|
||||
void Paragraph::Layout(const ParagraphConstraints& constraints,
|
||||
const std::string& rootdir) {
|
||||
const std::string& rootdir,
|
||||
const double x_offset,
|
||||
const double y_offset) {
|
||||
breaker_.setLineWidths(0.0f, 0, constraints.width());
|
||||
width_ = constraints.width();
|
||||
AddRunsToLineBreaker(rootdir);
|
||||
size_t breaks_count = breaker_.computeBreaks();
|
||||
const int* breaks = breaker_.getBreaks();
|
||||
@@ -144,7 +148,8 @@ void Paragraph::Layout(const ParagraphConstraints& constraints,
|
||||
SkTextBlobBuilder builder;
|
||||
|
||||
minikin::Layout layout;
|
||||
SkScalar x = 0.0f;
|
||||
SkScalar x = x_offset;
|
||||
y_ = y_offset;
|
||||
size_t break_index = 0;
|
||||
for (size_t run_index = 0; run_index < runs_.size(); ++run_index) {
|
||||
auto run = runs_.GetRun(run_index);
|
||||
@@ -155,7 +160,7 @@ void Paragraph::Layout(const ParagraphConstraints& constraints,
|
||||
GetPaint(run.style, &paint);
|
||||
|
||||
size_t layout_start = run.start;
|
||||
while (layout_start < run.end) {
|
||||
while (layout_start < run.end && !DidExceedMaxLines()) {
|
||||
const size_t next_break = (break_index > breaks_count - 1)
|
||||
? std::numeric_limits<size_t>::max()
|
||||
: breaks[break_index];
|
||||
@@ -194,8 +199,8 @@ void Paragraph::Layout(const ParagraphConstraints& constraints,
|
||||
// TODO(abarth): Use the line height, which is something like the max
|
||||
// font_size for runs in this line times the paragraph's line height.
|
||||
y_ += run.style.font_size * run.style.height;
|
||||
lines_++;
|
||||
break_index += 1;
|
||||
lines_++;
|
||||
} else {
|
||||
x += layout.getAdvance();
|
||||
}
|
||||
@@ -209,8 +214,17 @@ const ParagraphStyle& Paragraph::GetParagraphStyle() const {
|
||||
return paragraph_style_;
|
||||
}
|
||||
|
||||
double Paragraph::GetAlphabeticBaseline() const {
|
||||
return FLT_MAX;
|
||||
}
|
||||
|
||||
double Paragraph::GetIdeographicBaseline() const {
|
||||
return FLT_MAX;
|
||||
}
|
||||
|
||||
double Paragraph::GetHeight() const {
|
||||
return y_;
|
||||
return paragraph_style_.font_size * paragraph_style_.line_height *
|
||||
paragraph_style_.max_lines;
|
||||
}
|
||||
|
||||
void Paragraph::SetParagraphStyle(const ParagraphStyle& style) {
|
||||
@@ -227,7 +241,9 @@ void Paragraph::Paint(SkCanvas* canvas, double x, double y) {
|
||||
}
|
||||
|
||||
bool Paragraph::DidExceedMaxLines() const {
|
||||
if (lines_ > paragraph_style_.max_lines)
|
||||
if (y_ / (paragraph_style_.font_size * paragraph_style_.line_height) >
|
||||
paragraph_style_.max_lines)
|
||||
// if (lines_ > paragraph_style_.max_lines)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -39,7 +39,9 @@ class Paragraph {
|
||||
~Paragraph();
|
||||
|
||||
void Layout(const ParagraphConstraints& constraints,
|
||||
const std::string& rootdir = "");
|
||||
const std::string& rootdir = "",
|
||||
const double x_offset = 0.0,
|
||||
const double y_offset = 0.0);
|
||||
|
||||
void Paint(SkCanvas* canvas, double x, double y);
|
||||
|
||||
@@ -47,6 +49,10 @@ class Paragraph {
|
||||
|
||||
double GetHeight() const;
|
||||
|
||||
double GetAlphabeticBaseline() const;
|
||||
|
||||
double GetIdeographicBaseline() const;
|
||||
|
||||
bool DidExceedMaxLines() const;
|
||||
|
||||
private:
|
||||
@@ -64,8 +70,9 @@ class Paragraph {
|
||||
minikin::LineBreaker breaker_;
|
||||
std::vector<PaintRecord> records_;
|
||||
ParagraphStyle paragraph_style_;
|
||||
int lines_ = 1; // Number of lines after Layout().
|
||||
SkScalar y_ = 0.0f; // Height of the paragraph after Layout().
|
||||
double width_ = 0.0f;
|
||||
int lines_ = 0;
|
||||
|
||||
void SetText(std::vector<uint16_t> text, StyledRuns runs);
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "lib/ftl/logging.h"
|
||||
|
||||
#include "lib/txt/src/paragraph_builder.h"
|
||||
#include "lib/txt/src/paragraph_style.h"
|
||||
@@ -23,6 +24,12 @@ namespace txt {
|
||||
ParagraphBuilder::ParagraphBuilder(ParagraphStyle style)
|
||||
: paragraph_style_(style) {}
|
||||
|
||||
ParagraphBuilder::ParagraphBuilder() {}
|
||||
|
||||
void ParagraphBuilder::SetParagraphStyle(const ParagraphStyle& style) {
|
||||
paragraph_style_ = style;
|
||||
}
|
||||
|
||||
ParagraphBuilder::~ParagraphBuilder() = default;
|
||||
|
||||
void ParagraphBuilder::PushStyle(const TextStyle& style) {
|
||||
|
||||
@@ -32,6 +32,8 @@ class ParagraphBuilder {
|
||||
public:
|
||||
explicit ParagraphBuilder(ParagraphStyle style);
|
||||
|
||||
ParagraphBuilder();
|
||||
|
||||
~ParagraphBuilder();
|
||||
|
||||
void PushStyle(const TextStyle& style);
|
||||
@@ -44,6 +46,8 @@ class ParagraphBuilder {
|
||||
|
||||
void AddText(const char* text);
|
||||
|
||||
void SetParagraphStyle(const ParagraphStyle& style);
|
||||
|
||||
std::unique_ptr<Paragraph> Build();
|
||||
|
||||
private:
|
||||
|
||||
@@ -28,7 +28,11 @@ namespace txt {
|
||||
class ParagraphStyle {
|
||||
public:
|
||||
TextAlign text_align = TextAlign::left;
|
||||
int max_lines = 0;
|
||||
FontWeight font_weight = FontWeight::w400;
|
||||
FontStyle font_style = FontStyle::normal;
|
||||
std::string font_family = "";
|
||||
double font_size = 14;
|
||||
int max_lines = 1;
|
||||
double line_height = 1.0;
|
||||
std::string ellipsis;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user