From d6a6221fc832c7e292f0a4ab663c67c7a634aa9c Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 29 Jun 2017 16:14:35 -0700 Subject: [PATCH] Switch to hashmap caching of font collections to be resillient to alternating fonts and slight performance improvement. Change-Id: Ide3a311819c41e23e88e6dca06f0ce7669f6842c --- engine/src/flutter/src/paragraph.cc | 30 +++++++++++++---------------- engine/src/flutter/src/paragraph.h | 4 ++-- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index c6ee6be979..d4a7c7d13e 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -126,20 +126,21 @@ void Paragraph::SetText(std::vector text, StyledRuns runs) { } void Paragraph::AddRunsToLineBreaker( - std::shared_ptr& collection, - std::string& prev_font_family) { + std::unordered_map>& + collection_map) { minikin::FontStyle font; minikin::MinikinPaint paint; for (size_t i = 0; i < runs_.size(); ++i) { auto run = runs_.GetRun(i); // 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); + if (collection_map.count(run.style.font_family) == 0) { + collection_map[run.style.font_family] = + 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); + breaker_.addStyleRun(&paint, collection_map.at(run.style.font_family), font, + run.start, run.end, false); } } @@ -151,11 +152,11 @@ void Paragraph::Layout(double width, bool force) { width_ = width; - std::shared_ptr collection = nullptr; - std::string prev_font_family = ""; + std::unordered_map> + collection_map; breaker_.setLineWidths(0.0f, 0, width_); - AddRunsToLineBreaker(collection, prev_font_family); + AddRunsToLineBreaker(collection_map); breaker_.setJustified(paragraph_style_.text_align == TextAlign::justify); size_t breaks_count = breaker_.computeBreaks(); const int* breaks = breaker_.getBreaks(); @@ -221,12 +222,6 @@ void Paragraph::Layout(double width, bool force) { 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 = font_collection_->GetMinikinFontCollectionForFamily( - run.style.font_family); - } - prev_font_family = run.style.font_family; GetFontAndMinikinPaint(run.style, &font, &minikin_paint); GetPaint(run.style, &paint); @@ -244,7 +239,8 @@ void Paragraph::Layout(double width, bool force) { int bidiFlags = 0; layout.doLayout(text_.data(), layout_start, layout_end - layout_start, - text_.size(), bidiFlags, font, minikin_paint, collection); + text_.size(), bidiFlags, font, minikin_paint, + collection_map.at(run.style.font_family)); const size_t glyph_count = layout.nGlyphs(); size_t blob_start = 0; // Each blob. diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index 64739c8b95..97e30dae51 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -96,8 +96,8 @@ class Paragraph { void SetFontCollection(FontCollection* font_collection); void AddRunsToLineBreaker( - std::shared_ptr& collection, - std::string& prev_font_family); + std::unordered_map>& + collection_map); void JustifyLine(std::vector& buffers, std::vector& buffer_sizes,