From 48193eb2ab628f5aa7dbfc07003c818c8bcdf996 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 4 Apr 2018 14:01:21 -0700 Subject: [PATCH] libtxt: render fake bold text if a bold font is not available (flutter/engine#4933) Fixes https://github.com/flutter/flutter/issues/16149 --- .../third_party/txt/src/txt/paragraph.cc | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc index b2d4f7c70fc..cad44199e7e 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc @@ -43,10 +43,31 @@ namespace txt { namespace { -const sk_sp& GetTypefaceForGlyph(const minikin::Layout& layout, - size_t index) { +class GlyphTypeface { + public: + GlyphTypeface(sk_sp typeface, minikin::FontFakery fakery) + : typeface_(std::move(typeface)), fake_bold_(fakery.isFakeBold()) {} + + bool operator==(GlyphTypeface& other) { + return other.typeface_.get() == typeface_.get() && + other.fake_bold_ == fake_bold_; + } + + bool operator!=(GlyphTypeface& other) { return !(*this == other); } + + void apply(SkPaint& paint) { + paint.setTypeface(typeface_); + paint.setFakeBoldText(fake_bold_); + } + + private: + sk_sp typeface_; + bool fake_bold_; +}; + +GlyphTypeface GetGlyphTypeface(const minikin::Layout& layout, size_t index) { const FontSkia* font = static_cast(layout.getFont(index)); - return font->GetSkTypeface(); + return GlyphTypeface(font->GetSkTypeface(), layout.getFakery(index)); } // Return ranges of text that have the same typeface in the layout. @@ -56,9 +77,9 @@ std::vector> GetLayoutTypefaceRuns( if (layout.nGlyphs() == 0) return result; size_t run_start = 0; - const SkTypeface* run_typeface = GetTypefaceForGlyph(layout, run_start).get(); + GlyphTypeface run_typeface = GetGlyphTypeface(layout, run_start); for (size_t i = 1; i < layout.nGlyphs(); ++i) { - const SkTypeface* typeface = GetTypefaceForGlyph(layout, i).get(); + GlyphTypeface typeface = GetGlyphTypeface(layout, i); if (typeface != run_typeface) { result.emplace_back(run_start, i); run_start = i; @@ -524,7 +545,7 @@ void Paragraph::Layout(double width, bool force) { for (const Range& glyph_blob : glyph_blobs) { std::vector glyph_positions; - paint.setTypeface(GetTypefaceForGlyph(layout, glyph_blob.start)); + GetGlyphTypeface(layout, glyph_blob.start).apply(paint); const SkTextBlobBuilder::RunBuffer& blob_buffer = builder.allocRunPos(paint, glyph_blob.end - glyph_blob.start);